ha_partition.cc 205 KB
Newer Older
1
/* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2 3 4

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
  the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15 16

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/*
unknown's avatar
unknown committed
17
  This handler was developed by Mikael Ronstrom for version 5.1 of MySQL.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  It is an abstraction layer on top of other handlers such as MyISAM,
  InnoDB, Federated, Berkeley DB and so forth. Partitioned tables can also
  be handled by a storage engine. The current example of this is NDB
  Cluster that has internally handled partitioning. This have benefits in
  that many loops needed in the partition handler can be avoided.

  Partitioning has an inherent feature which in some cases is positive and
  in some cases is negative. It splits the data into chunks. This makes
  the data more manageable, queries can easily be parallelised towards the
  parts and indexes are split such that there are less levels in the
  index trees. The inherent disadvantage is that to use a split index
  one has to scan all index parts which is ok for large queries but for
  small queries it can be a disadvantage.

  Partitioning lays the foundation for more manageable databases that are
  extremely large. It does also lay the foundation for more parallelism
  in the execution of queries. This functionality will grow with later
  versions of MySQL.

  You can enable it in your buld by doing the following during your build
  process:
  ./configure --with-partition

  The partition is setup to use table locks. It implements an partition "SHARE"
  that is inserted into a hash by table name. You can use this to store
  information of state that any partition handler object will be able to see
  if it is using the same table.

  Please read the object definition in ha_partition.h before reading the rest
  if this file.
*/

#ifdef __GNUC__
#pragma implementation				// gcc: Class implementation
#endif

54 55
#include "sql_priv.h"
#include "sql_parse.h"                          // append_file_to_dir
56

unknown's avatar
unknown committed
57
#ifdef WITH_PARTITION_STORAGE_ENGINE
58
#include "ha_partition.h"
59 60 61
#include "sql_table.h"                        // tablename_to_filename
#include "key.h"
#include "sql_plugin.h"
Mattias Jonsson's avatar
Mattias Jonsson committed
62
#include "table.h"                           /* HA_DATA_PARTITION */
unknown's avatar
unknown committed
63

64
#include "debug_sync.h"
unknown's avatar
unknown committed
65

66 67 68 69 70 71
static const char *ha_par_ext= ".par";

/****************************************************************************
                MODULE create/delete handler object
****************************************************************************/

72 73
static handler *partition_create_handler(handlerton *hton,
                                         TABLE_SHARE *share,
74
                                         MEM_ROOT *mem_root);
unknown's avatar
unknown committed
75 76
static uint partition_flags();
static uint alter_table_flags(uint flags);
77

unknown's avatar
unknown committed
78

79
static int partition_initialize(void *p)
unknown's avatar
unknown committed
80
{
81

82
  handlerton *partition_hton;
83 84 85 86 87 88 89
  partition_hton= (handlerton *)p;

  partition_hton->state= SHOW_OPTION_YES;
  partition_hton->db_type= DB_TYPE_PARTITION_DB;
  partition_hton->create= partition_create_handler;
  partition_hton->partition_flags= partition_flags;
  partition_hton->alter_table_flags= alter_table_flags;
90 91 92
  partition_hton->flags= HTON_NOT_USER_SELECTABLE |
                         HTON_HIDDEN |
                         HTON_TEMPORARY_NOT_SUPPORTED;
93

unknown's avatar
unknown committed
94 95
  return 0;
}
unknown's avatar
merge  
unknown committed
96

unknown's avatar
unknown committed
97 98 99 100 101 102 103 104 105 106 107
/*
  Create new partition handler

  SYNOPSIS
    partition_create_handler()
    table                       Table object

  RETURN VALUE
    New partition object
*/

108 109
static handler *partition_create_handler(handlerton *hton, 
                                         TABLE_SHARE *share,
110
                                         MEM_ROOT *mem_root)
111
{
112
  ha_partition *file= new (mem_root) ha_partition(hton, share);
113
  if (file && file->initialize_partition(mem_root))
114 115 116 117 118
  {
    delete file;
    file= 0;
  }
  return file;
119 120
}

unknown's avatar
unknown committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/*
  HA_CAN_PARTITION:
  Used by storage engines that can handle partitioning without this
  partition handler
  (Partition, NDB)

  HA_CAN_UPDATE_PARTITION_KEY:
  Set if the handler can update fields that are part of the partition
  function.

  HA_CAN_PARTITION_UNIQUE:
  Set if the handler can handle unique indexes where the fields of the
  unique key are not part of the fields of the partition function. Thus
  a unique key can be set on all fields.

  HA_USE_AUTO_PARTITION
  Set if the handler sets all tables to be partitioned by default.
*/

static uint partition_flags()
{
  return HA_CAN_PARTITION;
}

static uint alter_table_flags(uint flags __attribute__((unused)))
{
  return (HA_PARTITION_FUNCTION_SUPPORTED |
          HA_FAST_CHANGE_PARTITION);
}

151 152
const uint ha_partition::NO_CURRENT_PART_ID= 0xFFFFFFFF;

unknown's avatar
unknown committed
153 154 155 156 157 158 159 160 161 162
/*
  Constructor method

  SYNOPSIS
    ha_partition()
    table                       Table object

  RETURN VALUE
    NONE
*/
unknown's avatar
unknown committed
163

164 165
ha_partition::ha_partition(handlerton *hton, TABLE_SHARE *share)
  :handler(hton, share), m_part_info(NULL), m_create_handler(FALSE),
166
   m_is_sub_partitioned(0)
167 168 169 170 171 172 173
{
  DBUG_ENTER("ha_partition::ha_partition(table)");
  init_handler_variables();
  DBUG_VOID_RETURN;
}


unknown's avatar
unknown committed
174 175 176 177 178 179 180 181 182 183 184
/*
  Constructor method

  SYNOPSIS
    ha_partition()
    part_info                       Partition info

  RETURN VALUE
    NONE
*/

185
ha_partition::ha_partition(handlerton *hton, partition_info *part_info)
186 187
  :handler(hton, NULL), m_part_info(part_info), m_create_handler(TRUE),
   m_is_sub_partitioned(m_part_info->is_sub_partitioned())
188 189 190 191 192 193 194 195
{
  DBUG_ENTER("ha_partition::ha_partition(part_info)");
  init_handler_variables();
  DBUG_ASSERT(m_part_info);
  DBUG_VOID_RETURN;
}


unknown's avatar
unknown committed
196
/*
197
  Initialize handler object
unknown's avatar
unknown committed
198 199 200 201 202 203 204 205

  SYNOPSIS
    init_handler_variables()

  RETURN VALUE
    NONE
*/

206 207 208
void ha_partition::init_handler_variables()
{
  active_index= MAX_KEY;
unknown's avatar
unknown committed
209 210
  m_mode= 0;
  m_open_test_lock= 0;
211 212 213 214
  m_file_buffer= NULL;
  m_name_buffer_ptr= NULL;
  m_engine_array= NULL;
  m_file= NULL;
unknown's avatar
unknown committed
215
  m_file_tot_parts= 0;
unknown's avatar
unknown committed
216
  m_reorged_file= NULL;
217
  m_new_file= NULL;
unknown's avatar
unknown committed
218 219
  m_reorged_parts= 0;
  m_added_file= NULL;
220 221 222 223 224 225 226 227 228 229 230 231 232 233
  m_tot_parts= 0;
  m_pkey_is_clustered= 0;
  m_lock_type= F_UNLCK;
  m_part_spec.start_part= NO_CURRENT_PART_ID;
  m_scan_value= 2;
  m_ref_length= 0;
  m_part_spec.end_part= NO_CURRENT_PART_ID;
  m_index_scan_type= partition_no_index_scan;
  m_start_key.key= NULL;
  m_start_key.length= 0;
  m_myisam= FALSE;
  m_innodb= FALSE;
  m_extra_cache= FALSE;
  m_extra_cache_size= 0;
234
  m_extra_prepare_for_update= FALSE;
235
  m_extra_cache_part_id= NO_CURRENT_PART_ID;
236
  m_handler_status= handler_not_initialized;
237 238 239 240 241 242 243
  m_low_byte_first= 1;
  m_part_field_array= NULL;
  m_ordered_rec_buffer= NULL;
  m_top_entry= NO_CURRENT_PART_ID;
  m_rec_length= 0;
  m_last_part= 0;
  m_rec0= 0;
244 245
  m_curr_key_info[0]= NULL;
  m_curr_key_info[1]= NULL;
246
  is_clone= FALSE,
247
  m_part_func_monotonicity_info= NON_MONOTONIC;
248 249
  auto_increment_lock= FALSE;
  auto_increment_safe_stmt_log_lock= FALSE;
unknown's avatar
unknown committed
250 251 252
  /*
    this allows blackhole to work properly
  */
253
  m_num_locks= 0;
254 255 256 257 258 259 260 261

#ifdef DONT_HAVE_TO_BE_INITALIZED
  m_start_key.flag= 0;
  m_ordered= TRUE;
#endif
}


262 263 264 265 266 267 268
const char *ha_partition::table_type() const
{ 
  // we can do this since we only support a single engine type
  return m_file[0]->table_type(); 
}


unknown's avatar
unknown committed
269 270 271 272 273 274 275 276 277 278
/*
  Destructor method

  SYNOPSIS
    ~ha_partition()

  RETURN VALUE
    NONE
*/

279 280 281 282 283 284 285 286 287
ha_partition::~ha_partition()
{
  DBUG_ENTER("ha_partition::~ha_partition()");
  if (m_file != NULL)
  {
    uint i;
    for (i= 0; i < m_tot_parts; i++)
      delete m_file[i];
  }
288
  my_free(m_ordered_rec_buffer);
289 290 291 292 293 294 295

  clear_handler_file();
  DBUG_VOID_RETURN;
}


/*
296
  Initialize partition handler object
unknown's avatar
unknown committed
297 298

  SYNOPSIS
299
    initialize_partition()
300
    mem_root			Allocate memory through this
unknown's avatar
unknown committed
301 302 303 304 305 306 307

  RETURN VALUE
    1                         Error
    0                         Success

  DESCRIPTION

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  The partition handler is only a layer on top of other engines. Thus it
  can't really perform anything without the underlying handlers. Thus we
  add this method as part of the allocation of a handler object.

  1) Allocation of underlying handlers
     If we have access to the partition info we will allocate one handler
     instance for each partition.
  2) Allocation without partition info
     The cases where we don't have access to this information is when called
     in preparation for delete_table and rename_table and in that case we
     only need to set HA_FILE_BASED. In that case we will use the .par file
     that contains information about the partitions and their engines and
     the names of each partition.
  3) Table flags initialisation
     We need also to set table flags for the partition handler. This is not
     static since it depends on what storage engines are used as underlying
     handlers.
     The table flags is set in this routine to simulate the behaviour of a
     normal storage engine
     The flag HA_FILE_BASED will be set independent of the underlying handlers
  4) Index flags initialisation
329 330
     When knowledge exists on the indexes it is also possible to initialize the
     index flags. Again the index flags must be initialized by using the under-
331 332 333 334 335 336
     lying handlers since this is storage engine dependent.
     The flag HA_READ_ORDER will be reset for the time being to indicate no
     ordered output is available from partition handler indexes. Later a merge
     sort will be performed using the underlying handlers.
  5) primary_key_is_clustered, has_transactions and low_byte_first is
     calculated here.
unknown's avatar
unknown committed
337

338 339
*/

340
bool ha_partition::initialize_partition(MEM_ROOT *mem_root)
341 342
{
  handler **file_array, *file;
343 344
  ulonglong check_table_flags;
  DBUG_ENTER("ha_partition::initialize_partition");
345

unknown's avatar
unknown committed
346
  if (m_create_handler)
347
  {
348
    m_tot_parts= m_part_info->get_tot_partitions();
349
    DBUG_ASSERT(m_tot_parts > 0);
350
    if (new_handlers_from_part_info(mem_root))
351
      DBUG_RETURN(1);
unknown's avatar
unknown committed
352 353 354
  }
  else if (!table_share || !table_share->normalized_path.str)
  {
355
    /*
356 357
      Called with dummy table share (delete, rename and alter table).
      Don't need to set-up anything.
358
    */
unknown's avatar
unknown committed
359 360
    DBUG_RETURN(0);
  }
361
  else if (get_from_handler_file(table_share->normalized_path.str, mem_root))
unknown's avatar
unknown committed
362
  {
363
    my_error(ER_FAILED_READ_FROM_PAR_FILE, MYF(0));
unknown's avatar
unknown committed
364
    DBUG_RETURN(1);
365
  }
unknown's avatar
unknown committed
366 367 368 369
  /*
    We create all underlying table handlers here. We do it in this special
    method to be able to report allocation errors.

370
    Set up low_byte_first, primary_key_is_clustered and
unknown's avatar
unknown committed
371 372
    has_transactions since they are called often in all kinds of places,
    other parameters are calculated on demand.
373
    Verify that all partitions have the same table_flags.
unknown's avatar
unknown committed
374
  */
375
  check_table_flags= m_file[0]->ha_table_flags();
unknown's avatar
unknown committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389
  m_low_byte_first= m_file[0]->low_byte_first();
  m_pkey_is_clustered= TRUE;
  file_array= m_file;
  do
  {
    file= *file_array;
    if (m_low_byte_first != file->low_byte_first())
    {
      // Cannot have handlers with different endian
      my_error(ER_MIX_HANDLER_ERROR, MYF(0));
      DBUG_RETURN(1);
    }
    if (!file->primary_key_is_clustered())
      m_pkey_is_clustered= FALSE;
390 391 392 393 394
    if (check_table_flags != file->ha_table_flags())
    {
      my_error(ER_MIX_HANDLER_ERROR, MYF(0));
      DBUG_RETURN(1);
    }
unknown's avatar
unknown committed
395
  } while (*(++file_array));
396
  m_handler_status= handler_initialized;
397 398 399 400 401 402 403
  DBUG_RETURN(0);
}

/****************************************************************************
                MODULE meta data changes
****************************************************************************/
/*
unknown's avatar
unknown committed
404
  Delete a table
405

unknown's avatar
unknown committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  SYNOPSIS
    delete_table()
    name                    Full path of table name

  RETURN VALUE
    >0                        Error
    0                         Success

  DESCRIPTION
    Used to delete a table. By the time delete_table() has been called all
    opened references to this table will have been closed (and your globally
    shared references released. The variable name will just be the name of
    the table. You will need to remove any files you have created at this
    point.

    If you do not implement this, the default delete_table() is called from
    handler.cc and it will delete all files with the file extentions returned
    by bas_ext().

    Called from handler.cc by delete_table and  ha_create_table(). Only used
    during create if the table_flag HA_DROP_BEFORE_CREATE was specified for
    the storage engine.
428 429 430 431 432
*/

int ha_partition::delete_table(const char *name)
{
  DBUG_ENTER("ha_partition::delete_table");
unknown's avatar
unknown committed
433

434
  DBUG_RETURN(del_ren_cre_table(name, NULL, NULL, NULL));
435 436 437 438
}


/*
unknown's avatar
unknown committed
439 440 441 442 443 444 445 446 447 448
  Rename a table

  SYNOPSIS
    rename_table()
    from                      Full path of old table name
    to                        Full path of new table name

  RETURN VALUE
    >0                        Error
    0                         Success
449

unknown's avatar
unknown committed
450 451
  DESCRIPTION
    Renames a table from one name to another from alter table call.
452

unknown's avatar
unknown committed
453 454 455 456 457
    If you do not implement this, the default rename_table() is called from
    handler.cc and it will rename all files with the file extentions returned
    by bas_ext().

    Called from sql_table.cc by mysql_rename_table().
458 459 460 461 462
*/

int ha_partition::rename_table(const char *from, const char *to)
{
  DBUG_ENTER("ha_partition::rename_table");
unknown's avatar
unknown committed
463

464
  DBUG_RETURN(del_ren_cre_table(from, to, NULL, NULL));
465 466 467 468
}


/*
unknown's avatar
unknown committed
469 470 471 472 473
  Create the handler file (.par-file)

  SYNOPSIS
    create_handler_files()
    name                              Full path of table name
474
    create_info                       Create info generated for CREATE TABLE
unknown's avatar
unknown committed
475 476 477 478 479 480 481 482 483 484 485

  RETURN VALUE
    >0                        Error
    0                         Success

  DESCRIPTION
    create_handler_files is called to create any handler specific files
    before opening the file with openfrm to later call ::create on the
    file object.
    In the partition handler this is used to store the names of partitions
    and types of engines in the partitions.
486 487
*/

488 489
int ha_partition::create_handler_files(const char *path,
                                       const char *old_path,
490
                                       int action_flag,
491
                                       HA_CREATE_INFO *create_info)
492 493
{
  DBUG_ENTER("ha_partition::create_handler_files()");
unknown's avatar
unknown committed
494 495 496 497 498

  /*
    We need to update total number of parts since we might write the handler
    file as part of a partition management command
  */
499 500
  if (action_flag == CHF_DELETE_FLAG ||
      action_flag == CHF_RENAME_FLAG)
501
  {
502 503 504
    char name[FN_REFLEN];
    char old_name[FN_REFLEN];

unknown's avatar
unknown committed
505 506
    strxmov(name, path, ha_par_ext, NullS);
    strxmov(old_name, old_path, ha_par_ext, NullS);
507
    if ((action_flag == CHF_DELETE_FLAG &&
Marc Alff's avatar
Marc Alff committed
508
         mysql_file_delete(key_file_partition, name, MYF(MY_WME))) ||
509
        (action_flag == CHF_RENAME_FLAG &&
Marc Alff's avatar
Marc Alff committed
510
         mysql_file_rename(key_file_partition, old_name, name, MYF(MY_WME))))
511 512 513 514
    {
      DBUG_RETURN(TRUE);
    }
  }
515
  else if (action_flag == CHF_CREATE_FLAG)
516 517 518 519 520 521
  {
    if (create_handler_file(path))
    {
      my_error(ER_CANT_CREATE_HANDLER_FILE, MYF(0));
      DBUG_RETURN(1);
    }
522 523 524 525 526 527
  }
  DBUG_RETURN(0);
}


/*
unknown's avatar
unknown committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
  Create a partitioned table

  SYNOPSIS
    create()
    name                              Full path of table name
    table_arg                         Table object
    create_info                       Create info generated for CREATE TABLE

  RETURN VALUE
    >0                        Error
    0                         Success

  DESCRIPTION
    create() is called to create a table. The variable name will have the name
    of the table. When create() is called you do not need to worry about
    opening the table. Also, the FRM file will have already been created so
    adjusting create_info will not do you any good. You can overwrite the frm
    file at this point if you wish to change the table definition, but there
    are no methods currently provided for doing that.

    Called from handler.cc by ha_create_table().
*/

int ha_partition::create(const char *name, TABLE *table_arg,
			 HA_CREATE_INFO *create_info)
{
  char t_name[FN_REFLEN];
  DBUG_ENTER("ha_partition::create");

  strmov(t_name, name);
  DBUG_ASSERT(*fn_rext((char*)name) == '\0');
  if (del_ren_cre_table(t_name, NULL, table_arg, create_info))
  {
    handler::delete_table(t_name);
    DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}


/*
  Drop partitions as part of ALTER TABLE of partitions

  SYNOPSIS
    drop_partitions()
    path                        Complete path of db and table name

  RETURN VALUE
    >0                          Failure
    0                           Success

  DESCRIPTION
    Use part_info object on handler object to deduce which partitions to
    drop (each partition has a state attached to it)
*/

int ha_partition::drop_partitions(const char *path)
{
  List_iterator<partition_element> part_it(m_part_info->partitions);
  char part_name_buff[FN_REFLEN];
588 589
  uint num_parts= m_part_info->partitions.elements;
  uint num_subparts= m_part_info->num_subparts;
unknown's avatar
unknown committed
590 591
  uint i= 0;
  uint name_variant;
592
  int  ret_error;
593
  int  error= 0;
unknown's avatar
unknown committed
594 595
  DBUG_ENTER("ha_partition::drop_partitions");

596 597 598 599 600 601
  /*
    Assert that it works without HA_FILE_BASED and lower_case_table_name = 2.
    We use m_file[0] as long as all partitions have the same storage engine.
  */
  DBUG_ASSERT(!strcmp(path, get_canonical_filename(m_file[0], path,
                                                   part_name_buff)));
unknown's avatar
unknown committed
602 603
  do
  {
604 605
    partition_element *part_elem= part_it++;
    if (part_elem->part_state == PART_TO_BE_DROPPED)
unknown's avatar
unknown committed
606 607 608 609 610 611 612 613 614 615 616 617 618
    {
      handler *file;
      /*
        This part is to be dropped, meaning the part or all its subparts.
      */
      name_variant= NORMAL_PART_NAME;
      if (m_is_sub_partitioned)
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
        uint j= 0, part;
        do
        {
          partition_element *sub_elem= sub_it++;
619
          part= i * num_subparts + j;
unknown's avatar
unknown committed
620 621 622
          create_subpartition_name(part_name_buff, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name, name_variant);
623
          file= m_file[part];
unknown's avatar
unknown committed
624
          DBUG_PRINT("info", ("Drop subpartition %s", part_name_buff));
625
          if ((ret_error= file->ha_delete_table(part_name_buff)))
626
            error= ret_error;
627 628
          if (deactivate_ddl_log_entry(sub_elem->log_entry->entry_pos))
            error= 1;
629
        } while (++j < num_subparts);
unknown's avatar
unknown committed
630 631 632 633 634 635
      }
      else
      {
        create_partition_name(part_name_buff, path,
                              part_elem->partition_name, name_variant,
                              TRUE);
636
        file= m_file[i];
unknown's avatar
unknown committed
637
        DBUG_PRINT("info", ("Drop partition %s", part_name_buff));
638
        if ((ret_error= file->ha_delete_table(part_name_buff)))
639
          error= ret_error;
640 641
        if (deactivate_ddl_log_entry(part_elem->log_entry->entry_pos))
          error= 1;
unknown's avatar
unknown committed
642 643 644 645 646 647
      }
      if (part_elem->part_state == PART_IS_CHANGED)
        part_elem->part_state= PART_NORMAL;
      else
        part_elem->part_state= PART_IS_DROPPED;
    }
648
  } while (++i < num_parts);
Konstantin Osipov's avatar
Konstantin Osipov committed
649
  (void) sync_ddl_log();
unknown's avatar
unknown committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
  DBUG_RETURN(error);
}


/*
  Rename partitions as part of ALTER TABLE of partitions

  SYNOPSIS
    rename_partitions()
    path                        Complete path of db and table name

  RETURN VALUE
    TRUE                        Failure
    FALSE                       Success

  DESCRIPTION
    When reorganising partitions, adding hash partitions and coalescing
    partitions it can be necessary to rename partitions while holding
    an exclusive lock on the table.
    Which partitions to rename is given by state of partitions found by the
    partition info struct referenced from the handler object
*/

int ha_partition::rename_partitions(const char *path)
{
  List_iterator<partition_element> part_it(m_part_info->partitions);
  List_iterator<partition_element> temp_it(m_part_info->temp_partitions);
  char part_name_buff[FN_REFLEN];
  char norm_name_buff[FN_REFLEN];
679
  uint num_parts= m_part_info->partitions.elements;
unknown's avatar
unknown committed
680
  uint part_count= 0;
681
  uint num_subparts= m_part_info->num_subparts;
unknown's avatar
unknown committed
682 683
  uint i= 0;
  uint j= 0;
684
  int error= 0;
685
  int ret_error;
unknown's avatar
unknown committed
686 687 688 689 690
  uint temp_partitions= m_part_info->temp_partitions.elements;
  handler *file;
  partition_element *part_elem, *sub_elem;
  DBUG_ENTER("ha_partition::rename_partitions");

691 692 693 694 695 696 697
  /*
    Assert that it works without HA_FILE_BASED and lower_case_table_name = 2.
    We use m_file[0] as long as all partitions have the same storage engine.
  */
  DBUG_ASSERT(!strcmp(path, get_canonical_filename(m_file[0], path,
                                                   norm_name_buff)));

698
  DEBUG_SYNC(ha_thd(), "before_rename_partitions");
unknown's avatar
unknown committed
699 700
  if (temp_partitions)
  {
701 702 703 704 705 706 707 708
    /*
      These are the reorganised partitions that have already been copied.
      We delete the partitions and log the delete by inactivating the
      delete log entry in the table log. We only need to synchronise
      these writes before moving to the next loop since there is no
      interaction among reorganised partitions, they cannot have the
      same name.
    */
unknown's avatar
unknown committed
709 710 711 712 713 714
    do
    {
      part_elem= temp_it++;
      if (m_is_sub_partitioned)
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
715
        j= 0;
unknown's avatar
unknown committed
716 717 718 719 720 721 722 723
        do
        {
          sub_elem= sub_it++;
          file= m_reorged_file[part_count++];
          create_subpartition_name(norm_name_buff, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name,
                                   NORMAL_PART_NAME);
724
          DBUG_PRINT("info", ("Delete subpartition %s", norm_name_buff));
725
          if ((ret_error= file->ha_delete_table(norm_name_buff)))
726
            error= ret_error;
727
          else if (deactivate_ddl_log_entry(sub_elem->log_entry->entry_pos))
728 729 730
            error= 1;
          else
            sub_elem->log_entry= NULL; /* Indicate success */
731
        } while (++j < num_subparts);
unknown's avatar
unknown committed
732 733 734 735 736 737 738
      }
      else
      {
        file= m_reorged_file[part_count++];
        create_partition_name(norm_name_buff, path,
                              part_elem->partition_name, NORMAL_PART_NAME,
                              TRUE);
739
        DBUG_PRINT("info", ("Delete partition %s", norm_name_buff));
740
        if ((ret_error= file->ha_delete_table(norm_name_buff)))
741
          error= ret_error;
742
        else if (deactivate_ddl_log_entry(part_elem->log_entry->entry_pos))
743 744
          error= 1;
        else
745
          part_elem->log_entry= NULL; /* Indicate success */
unknown's avatar
unknown committed
746 747
      }
    } while (++i < temp_partitions);
Konstantin Osipov's avatar
Konstantin Osipov committed
748
    (void) sync_ddl_log();
unknown's avatar
unknown committed
749 750 751 752
  }
  i= 0;
  do
  {
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    /*
       When state is PART_IS_CHANGED it means that we have created a new
       TEMP partition that is to be renamed to normal partition name and
       we are to delete the old partition with currently the normal name.
       
       We perform this operation by
       1) Delete old partition with normal partition name
       2) Signal this in table log entry
       3) Synch table log to ensure we have consistency in crashes
       4) Rename temporary partition name to normal partition name
       5) Signal this to table log entry
       It is not necessary to synch the last state since a new rename
       should not corrupt things if there was no temporary partition.

       The only other parts we need to cater for are new parts that
       replace reorganised parts. The reorganised parts were deleted
       by the code above that goes through the temp_partitions list.
       Thus the synch above makes it safe to simply perform step 4 and 5
       for those entries.
    */
unknown's avatar
unknown committed
773 774
    part_elem= part_it++;
    if (part_elem->part_state == PART_IS_CHANGED ||
775
        part_elem->part_state == PART_TO_BE_DROPPED ||
unknown's avatar
unknown committed
776 777 778 779 780 781 782 783 784 785 786
        (part_elem->part_state == PART_IS_ADDED && temp_partitions))
    {
      if (m_is_sub_partitioned)
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
        uint part;

        j= 0;
        do
        {
          sub_elem= sub_it++;
787
          part= i * num_subparts + j;
unknown's avatar
unknown committed
788 789 790 791 792 793 794
          create_subpartition_name(norm_name_buff, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name,
                                   NORMAL_PART_NAME);
          if (part_elem->part_state == PART_IS_CHANGED)
          {
            file= m_reorged_file[part_count++];
795
            DBUG_PRINT("info", ("Delete subpartition %s", norm_name_buff));
796
            if ((ret_error= file->ha_delete_table(norm_name_buff)))
797
              error= ret_error;
798
            else if (deactivate_ddl_log_entry(sub_elem->log_entry->entry_pos))
799
              error= 1;
Konstantin Osipov's avatar
Konstantin Osipov committed
800
            (void) sync_ddl_log();
unknown's avatar
unknown committed
801 802 803 804 805 806 807 808
          }
          file= m_new_file[part];
          create_subpartition_name(part_name_buff, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name,
                                   TEMP_PART_NAME);
          DBUG_PRINT("info", ("Rename subpartition from %s to %s",
                     part_name_buff, norm_name_buff));
809 810
          if ((ret_error= file->ha_rename_table(part_name_buff,
                                                norm_name_buff)))
811
            error= ret_error;
812
          else if (deactivate_ddl_log_entry(sub_elem->log_entry->entry_pos))
813 814 815
            error= 1;
          else
            sub_elem->log_entry= NULL;
816
        } while (++j < num_subparts);
unknown's avatar
unknown committed
817 818 819 820 821 822 823 824 825
      }
      else
      {
        create_partition_name(norm_name_buff, path,
                              part_elem->partition_name, NORMAL_PART_NAME,
                              TRUE);
        if (part_elem->part_state == PART_IS_CHANGED)
        {
          file= m_reorged_file[part_count++];
826
          DBUG_PRINT("info", ("Delete partition %s", norm_name_buff));
827
          if ((ret_error= file->ha_delete_table(norm_name_buff)))
828
            error= ret_error;
829
          else if (deactivate_ddl_log_entry(part_elem->log_entry->entry_pos))
830
            error= 1;
Konstantin Osipov's avatar
Konstantin Osipov committed
831
          (void) sync_ddl_log();
unknown's avatar
unknown committed
832 833 834 835 836 837 838
        }
        file= m_new_file[i];
        create_partition_name(part_name_buff, path,
                              part_elem->partition_name, TEMP_PART_NAME,
                              TRUE);
        DBUG_PRINT("info", ("Rename partition from %s to %s",
                   part_name_buff, norm_name_buff));
839 840
        if ((ret_error= file->ha_rename_table(part_name_buff,
                                              norm_name_buff)))
841
          error= ret_error;
842
        else if (deactivate_ddl_log_entry(part_elem->log_entry->entry_pos))
843 844 845
          error= 1;
        else
          part_elem->log_entry= NULL;
unknown's avatar
unknown committed
846 847
      }
    }
848
  } while (++i < num_parts);
Konstantin Osipov's avatar
Konstantin Osipov committed
849
  (void) sync_ddl_log();
unknown's avatar
unknown committed
850 851 852 853 854 855 856 857
  DBUG_RETURN(error);
}


#define OPTIMIZE_PARTS 1
#define ANALYZE_PARTS 2
#define CHECK_PARTS   3
#define REPAIR_PARTS 4
858 859
#define ASSIGN_KEYCACHE_PARTS 5
#define PRELOAD_KEYS_PARTS 6
unknown's avatar
unknown committed
860

861
static const char *opt_op_name[]= {NULL,
862 863
                                   "optimize", "analyze", "check", "repair",
                                   "assign_to_keycache", "preload_keys"};
864

unknown's avatar
unknown committed
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
/*
  Optimize table

  SYNOPSIS
    optimize()
    thd               Thread object
    check_opt         Check/analyze/repair/optimize options

  RETURN VALUES
    >0                Error
    0                 Success
*/

int ha_partition::optimize(THD *thd, HA_CHECK_OPT *check_opt)
{
  DBUG_ENTER("ha_partition::optimize");

882
  DBUG_RETURN(handle_opt_partitions(thd, check_opt, OPTIMIZE_PARTS));
unknown's avatar
unknown committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
}


/*
  Analyze table

  SYNOPSIS
    analyze()
    thd               Thread object
    check_opt         Check/analyze/repair/optimize options

  RETURN VALUES
    >0                Error
    0                 Success
*/

int ha_partition::analyze(THD *thd, HA_CHECK_OPT *check_opt)
{
  DBUG_ENTER("ha_partition::analyze");

903
  DBUG_RETURN(handle_opt_partitions(thd, check_opt, ANALYZE_PARTS));
unknown's avatar
unknown committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
}


/*
  Check table

  SYNOPSIS
    check()
    thd               Thread object
    check_opt         Check/analyze/repair/optimize options

  RETURN VALUES
    >0                Error
    0                 Success
*/

int ha_partition::check(THD *thd, HA_CHECK_OPT *check_opt)
{
  DBUG_ENTER("ha_partition::check");

924
  DBUG_RETURN(handle_opt_partitions(thd, check_opt, CHECK_PARTS));
unknown's avatar
unknown committed
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
}


/*
  Repair table

  SYNOPSIS
    repair()
    thd               Thread object
    check_opt         Check/analyze/repair/optimize options

  RETURN VALUES
    >0                Error
    0                 Success
*/

int ha_partition::repair(THD *thd, HA_CHECK_OPT *check_opt)
{
  DBUG_ENTER("ha_partition::repair");

945
  DBUG_RETURN(handle_opt_partitions(thd, check_opt, REPAIR_PARTS));
unknown's avatar
unknown committed
946 947
}

948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
/**
  Assign to keycache

  @param thd          Thread object
  @param check_opt    Check/analyze/repair/optimize options

  @return
    @retval >0        Error
    @retval 0         Success
*/

int ha_partition::assign_to_keycache(THD *thd, HA_CHECK_OPT *check_opt)
{
  DBUG_ENTER("ha_partition::assign_to_keycache");

  DBUG_RETURN(handle_opt_partitions(thd, check_opt, ASSIGN_KEYCACHE_PARTS));
}


/**
  Preload to keycache
969

970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
  @param thd          Thread object
  @param check_opt    Check/analyze/repair/optimize options

  @return
    @retval >0        Error
    @retval 0         Success
*/

int ha_partition::preload_keys(THD *thd, HA_CHECK_OPT *check_opt)
{
  DBUG_ENTER("ha_partition::preload_keys");

  DBUG_RETURN(handle_opt_partitions(thd, check_opt, PRELOAD_KEYS_PARTS));
}

 
unknown's avatar
unknown committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
/*
  Handle optimize/analyze/check/repair of one partition

  SYNOPSIS
    handle_opt_part()
    thd                      Thread object
    check_opt                Options
    file                     Handler object of partition
    flag                     Optimize/Analyze/Check/Repair flag

  RETURN VALUE
    >0                        Failure
    0                         Success
*/

static int handle_opt_part(THD *thd, HA_CHECK_OPT *check_opt,
                           handler *file, uint flag)
{
  int error;
  DBUG_ENTER("handle_opt_part");
  DBUG_PRINT("enter", ("flag = %u", flag));

  if (flag == OPTIMIZE_PARTS)
1009
    error= file->ha_optimize(thd, check_opt);
unknown's avatar
unknown committed
1010
  else if (flag == ANALYZE_PARTS)
1011
    error= file->ha_analyze(thd, check_opt);
unknown's avatar
unknown committed
1012
  else if (flag == CHECK_PARTS)
unknown's avatar
unknown committed
1013
    error= file->ha_check(thd, check_opt);
unknown's avatar
unknown committed
1014
  else if (flag == REPAIR_PARTS)
unknown's avatar
unknown committed
1015
    error= file->ha_repair(thd, check_opt);
1016 1017 1018 1019
  else if (flag == ASSIGN_KEYCACHE_PARTS)
    error= file->assign_to_keycache(thd, check_opt);
  else if (flag == PRELOAD_KEYS_PARTS)
    error= file->preload_keys(thd, check_opt);
unknown's avatar
unknown committed
1020 1021 1022 1023 1024 1025 1026 1027 1028
  else
  {
    DBUG_ASSERT(FALSE);
    error= 1;
  }
  if (error == HA_ADMIN_ALREADY_DONE)
    error= 0;
  DBUG_RETURN(error);
}
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053


/*
   print a message row formatted for ANALYZE/CHECK/OPTIMIZE/REPAIR TABLE 
   (modelled after mi_check_print_msg)
   TODO: move this into the handler, or rewrite mysql_admin_table.
*/
static bool print_admin_msg(THD* thd, const char* msg_type,
                            const char* db_name, const char* table_name,
                            const char* op_name, const char *fmt, ...)
{
  va_list args;
  Protocol *protocol= thd->protocol;
  uint length, msg_length;
  char msgbuf[MI_MAX_MSG_BUF];
  char name[NAME_LEN*2+2];

  va_start(args, fmt);
  msg_length= my_vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
  va_end(args);
  msgbuf[sizeof(msgbuf) - 1] = 0; // healthy paranoia


  if (!thd->vio_ok())
  {
1054
    sql_print_error("%s", msgbuf);
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    return TRUE;
  }

  length=(uint) (strxmov(name, db_name, ".", table_name,NullS) - name);
  /*
     TODO: switch from protocol to push_warning here. The main reason we didn't
     it yet is parallel repair. Due to following trace:
     mi_check_print_msg/push_warning/sql_alloc/my_pthread_getspecific_ptr.

     Also we likely need to lock mutex here (in both cases with protocol and
     push_warning).
  */
  DBUG_PRINT("info",("print_admin_msg:  %s, %s, %s, %s", name, op_name,
                     msg_type, msgbuf));
  protocol->prepare_for_resend();
  protocol->store(name, length, system_charset_info);
  protocol->store(op_name, system_charset_info);
  protocol->store(msg_type, system_charset_info);
  protocol->store(msgbuf, msg_length, system_charset_info);
  if (protocol->write())
  {
    sql_print_error("Failed on my_net_write, writing to stderr instead: %s\n",
                    msgbuf);
    return TRUE;
  }
  return FALSE;
}
unknown's avatar
unknown committed
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098


/*
  Handle optimize/analyze/check/repair of partitions

  SYNOPSIS
    handle_opt_partitions()
    thd                      Thread object
    check_opt                Options
    flag                     Optimize/Analyze/Check/Repair flag

  RETURN VALUE
    >0                        Failure
    0                         Success
*/

int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt,
1099
                                        uint flag)
unknown's avatar
unknown committed
1100 1101
{
  List_iterator<partition_element> part_it(m_part_info->partitions);
1102 1103
  uint num_parts= m_part_info->num_parts;
  uint num_subparts= m_part_info->num_subparts;
unknown's avatar
unknown committed
1104 1105 1106
  uint i= 0;
  int error;
  DBUG_ENTER("ha_partition::handle_opt_partitions");
1107
  DBUG_PRINT("enter", ("flag= %u", flag));
unknown's avatar
unknown committed
1108 1109 1110 1111

  do
  {
    partition_element *part_elem= part_it++;
1112 1113 1114 1115
    /*
      when ALTER TABLE <CMD> PARTITION ...
      it should only do named partitions, otherwise all partitions
    */
1116
    if (!(thd->lex->alter_info.flags & ALTER_ADMIN_PARTITION) ||
1117
        part_elem->part_state == PART_ADMIN)
unknown's avatar
unknown committed
1118 1119 1120
    {
      if (m_is_sub_partitioned)
      {
1121 1122
        List_iterator<partition_element> subpart_it(part_elem->subpartitions);
        partition_element *sub_elem;
unknown's avatar
unknown committed
1123 1124 1125
        uint j= 0, part;
        do
        {
1126
          sub_elem= subpart_it++;
1127
          part= i * num_subparts + j;
1128 1129
          DBUG_PRINT("info", ("Optimize subpartition %u (%s)",
                     part, sub_elem->partition_name));
unknown's avatar
unknown committed
1130 1131
          if ((error= handle_opt_part(thd, check_opt, m_file[part], flag)))
          {
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
            /* print a line which partition the error belongs to */
            if (error != HA_ADMIN_NOT_IMPLEMENTED &&
                error != HA_ADMIN_ALREADY_DONE &&
                error != HA_ADMIN_TRY_ALTER)
            {
              print_admin_msg(thd, "error", table_share->db.str, table->alias,
                              opt_op_name[flag],
                              "Subpartition %s returned error", 
                              sub_elem->partition_name);
            }
1142 1143 1144 1145 1146
            /* reset part_state for the remaining partitions */
            do
            {
              if (part_elem->part_state == PART_ADMIN)
                part_elem->part_state= PART_NORMAL;
1147
            } while ((part_elem= part_it++));
1148
            DBUG_RETURN(error);
unknown's avatar
unknown committed
1149
          }
1150
        } while (++j < num_subparts);
unknown's avatar
unknown committed
1151 1152 1153
      }
      else
      {
1154 1155
        DBUG_PRINT("info", ("Optimize partition %u (%s)", i,
                            part_elem->partition_name));
unknown's avatar
unknown committed
1156 1157
        if ((error= handle_opt_part(thd, check_opt, m_file[i], flag)))
        {
1158 1159 1160 1161 1162 1163 1164 1165 1166
          /* print a line which partition the error belongs to */
          if (error != HA_ADMIN_NOT_IMPLEMENTED &&
              error != HA_ADMIN_ALREADY_DONE &&
              error != HA_ADMIN_TRY_ALTER)
          {
            print_admin_msg(thd, "error", table_share->db.str, table->alias,
                            opt_op_name[flag], "Partition %s returned error", 
                            part_elem->partition_name);
          }
1167 1168 1169 1170 1171
          /* reset part_state for the remaining partitions */
          do
          {
            if (part_elem->part_state == PART_ADMIN)
              part_elem->part_state= PART_NORMAL;
1172
          } while ((part_elem= part_it++));
1173
          DBUG_RETURN(error);
unknown's avatar
unknown committed
1174 1175
        }
      }
1176
      part_elem->part_state= PART_NORMAL;
unknown's avatar
unknown committed
1177
    }
1178
  } while (++i < num_parts);
unknown's avatar
unknown committed
1179 1180 1181
  DBUG_RETURN(FALSE);
}

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245

/**
  @brief Check and repair the table if neccesary

  @param thd    Thread object

  @retval TRUE  Error/Not supported
  @retval FALSE Success
*/

bool ha_partition::check_and_repair(THD *thd)
{
  handler **file= m_file;
  DBUG_ENTER("ha_partition::check_and_repair");

  do
  {
    if ((*file)->ha_check_and_repair(thd))
      DBUG_RETURN(TRUE);
  } while (*(++file));
  DBUG_RETURN(FALSE);
}
 

/**
  @breif Check if the table can be automatically repaired

  @retval TRUE  Can be auto repaired
  @retval FALSE Cannot be auto repaired
*/

bool ha_partition::auto_repair() const
{
  DBUG_ENTER("ha_partition::auto_repair");

  /*
    As long as we only support one storage engine per table,
    we can use the first partition for this function.
  */
  DBUG_RETURN(m_file[0]->auto_repair());
}


/**
  @breif Check if the table is crashed

  @retval TRUE  Crashed
  @retval FALSE Not crashed
*/

bool ha_partition::is_crashed() const
{
  handler **file= m_file;
  DBUG_ENTER("ha_partition::is_crashed");

  do
  {
    if ((*file)->is_crashed())
      DBUG_RETURN(TRUE);
  } while (*(++file));
  DBUG_RETURN(FALSE);
}
 

unknown's avatar
unknown committed
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
/*
  Prepare by creating a new partition

  SYNOPSIS
    prepare_new_partition()
    table                      Table object
    create_info                Create info from CREATE TABLE
    file                       Handler object of new partition
    part_name                  partition name

  RETURN VALUE
    >0                         Error
    0                          Success
*/

1261
int ha_partition::prepare_new_partition(TABLE *tbl,
unknown's avatar
unknown committed
1262
                                        HA_CREATE_INFO *create_info,
1263 1264
                                        handler *file, const char *part_name,
                                        partition_element *p_elem)
unknown's avatar
unknown committed
1265 1266 1267 1268
{
  int error;
  DBUG_ENTER("prepare_new_partition");

1269
  if ((error= set_up_table_before_create(tbl, part_name, create_info,
1270
                                         0, p_elem)))
1271
    goto error_create;
1272
  if ((error= file->ha_create(part_name, tbl, create_info)))
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
  {
    /*
      Added for safety, InnoDB reports HA_ERR_FOUND_DUPP_KEY
      if the table/partition already exists.
      If we return that error code, then print_error would try to
      get_dup_key on a non-existing partition.
      So return a more reasonable error code.
    */
    if (error == HA_ERR_FOUND_DUPP_KEY)
      error= HA_ERR_TABLE_EXIST;
    goto error_create;
  }
  DBUG_PRINT("info", ("partition %s created", part_name));
1286
  if ((error= file->ha_open(tbl, part_name, m_mode, m_open_test_lock)))
1287 1288
    goto error_open;
  DBUG_PRINT("info", ("partition %s opened", part_name));
1289 1290 1291 1292 1293 1294
  /*
    Note: if you plan to add another call that may return failure,
    better to do it before external_lock() as cleanup_new_partition()
    assumes that external_lock() is last call that may fail here.
    Otherwise see description for cleanup_new_partition().
  */
1295
  if ((error= file->ha_external_lock(ha_thd(), F_WRLCK)))
1296 1297
    goto error_external_lock;
  DBUG_PRINT("info", ("partition %s external locked", part_name));
unknown's avatar
unknown committed
1298 1299

  DBUG_RETURN(0);
1300
error_external_lock:
1301
  (void) file->close();
1302
error_open:
1303
  (void) file->ha_delete_table(part_name);
1304
error_create:
unknown's avatar
unknown committed
1305 1306 1307 1308 1309 1310
  DBUG_RETURN(error);
}


/*
  Cleanup by removing all created partitions after error
1311

unknown's avatar
unknown committed
1312 1313 1314 1315 1316 1317 1318 1319
  SYNOPSIS
    cleanup_new_partition()
    part_count             Number of partitions to remove

  RETURN VALUE
    NONE

  DESCRIPTION
1320 1321 1322 1323 1324 1325 1326 1327
    This function is called immediately after prepare_new_partition() in
    case the latter fails.

    In prepare_new_partition() last call that may return failure is
    external_lock(). That means if prepare_new_partition() fails,
    partition does not have external lock. Thus no need to call
    external_lock(F_UNLCK) here.

unknown's avatar
unknown committed
1328 1329 1330 1331 1332 1333
  TODO:
    We must ensure that in the case that we get an error during the process
    that we call external_lock with F_UNLCK, close the table and delete the
    table in the case where we have been successful with prepare_handler.
    We solve this by keeping an array of successful calls to prepare_handler
    which can then be used to undo the call.
1334 1335
*/

unknown's avatar
unknown committed
1336
void ha_partition::cleanup_new_partition(uint part_count)
1337
{
unknown's avatar
unknown committed
1338
  DBUG_ENTER("ha_partition::cleanup_new_partition");
1339

1340
  if (m_added_file)
1341
  {
1342 1343 1344 1345 1346 1347
    THD *thd= ha_thd();
    handler **file= m_added_file;
    while ((part_count > 0) && (*file))
    {
      (*file)->ha_external_lock(thd, F_UNLCK);
      (*file)->close();
unknown's avatar
unknown committed
1348

1349
      /* Leave the (*file)->ha_delete_table(part_name) to the ddl-log */
unknown's avatar
unknown committed
1350

1351 1352 1353 1354
      file++;
      part_count--;
    }
    m_added_file= NULL;
1355
  }
unknown's avatar
unknown committed
1356
  DBUG_VOID_RETURN;
1357 1358
}

unknown's avatar
unknown committed
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
/*
  Implement the partition changes defined by ALTER TABLE of partitions

  SYNOPSIS
    change_partitions()
    create_info                 HA_CREATE_INFO object describing all
                                fields and indexes in table
    path                        Complete path of db and table name
    out: copied                 Output parameter where number of copied
                                records are added
    out: deleted                Output parameter where number of deleted
                                records are added
    pack_frm_data               Reference to packed frm file
    pack_frm_len                Length of packed frm file

  RETURN VALUE
    >0                        Failure
    0                         Success

  DESCRIPTION
    Add and copy if needed a number of partitions, during this operation
    no other operation is ongoing in the server. This is used by
    ADD PARTITION all types as well as by REORGANIZE PARTITION. For
    one-phased implementations it is used also by DROP and COALESCE
    PARTITIONs.
    One-phased implementation needs the new frm file, other handlers will
    get zero length and a NULL reference here.
*/

int ha_partition::change_partitions(HA_CREATE_INFO *create_info,
                                    const char *path,
1390 1391
                                    ulonglong * const copied,
                                    ulonglong * const deleted,
1392
                                    const uchar *pack_frm_data
unknown's avatar
unknown committed
1393
                                    __attribute__((unused)),
1394
                                    size_t pack_frm_len
unknown's avatar
unknown committed
1395
                                    __attribute__((unused)))
unknown's avatar
unknown committed
1396 1397
{
  List_iterator<partition_element> part_it(m_part_info->partitions);
unknown's avatar
unknown committed
1398
  List_iterator <partition_element> t_it(m_part_info->temp_partitions);
unknown's avatar
unknown committed
1399
  char part_name_buff[FN_REFLEN];
1400 1401
  uint num_parts= m_part_info->partitions.elements;
  uint num_subparts= m_part_info->num_subparts;
unknown's avatar
unknown committed
1402
  uint i= 0;
1403
  uint num_remain_partitions, part_count, orig_count;
unknown's avatar
unknown committed
1404
  handler **new_file_array;
unknown's avatar
unknown committed
1405
  int error= 1;
unknown's avatar
unknown committed
1406 1407
  bool first;
  uint temp_partitions= m_part_info->temp_partitions.elements;
1408
  THD *thd= ha_thd();
unknown's avatar
unknown committed
1409 1410
  DBUG_ENTER("ha_partition::change_partitions");

1411 1412 1413 1414 1415 1416
  /*
    Assert that it works without HA_FILE_BASED and lower_case_table_name = 2.
    We use m_file[0] as long as all partitions have the same storage engine.
  */
  DBUG_ASSERT(!strcmp(path, get_canonical_filename(m_file[0], path,
                                                   part_name_buff)));
unknown's avatar
unknown committed
1417
  m_reorged_parts= 0;
1418
  if (!m_part_info->is_sub_partitioned())
1419
    num_subparts= 1;
unknown's avatar
unknown committed
1420 1421 1422 1423 1424 1425 1426 1427

  /*
    Step 1:
      Calculate number of reorganised partitions and allocate space for
      their handler references.
  */
  if (temp_partitions)
  {
1428
    m_reorged_parts= temp_partitions * num_subparts;
unknown's avatar
unknown committed
1429 1430 1431 1432 1433 1434 1435 1436 1437
  }
  else
  {
    do
    {
      partition_element *part_elem= part_it++;
      if (part_elem->part_state == PART_CHANGED ||
          part_elem->part_state == PART_REORGED_DROPPED)
      {
1438
        m_reorged_parts+= num_subparts;
unknown's avatar
unknown committed
1439
      }
1440
    } while (++i < num_parts);
unknown's avatar
unknown committed
1441 1442
  }
  if (m_reorged_parts &&
unknown's avatar
unknown committed
1443
      !(m_reorged_file= (handler**)sql_calloc(sizeof(handler*)*
unknown's avatar
unknown committed
1444 1445
                                              (m_reorged_parts + 1))))
  {
unknown's avatar
unknown committed
1446
    mem_alloc_error(sizeof(handler*)*(m_reorged_parts+1));
1447
    DBUG_RETURN(ER_OUTOFMEMORY);
unknown's avatar
unknown committed
1448 1449 1450 1451 1452 1453 1454
  }

  /*
    Step 2:
      Calculate number of partitions after change and allocate space for
      their handler references.
  */
1455
  num_remain_partitions= 0;
unknown's avatar
unknown committed
1456 1457
  if (temp_partitions)
  {
1458
    num_remain_partitions= num_parts * num_subparts;
unknown's avatar
unknown committed
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
  }
  else
  {
    part_it.rewind();
    i= 0;
    do
    {
      partition_element *part_elem= part_it++;
      if (part_elem->part_state == PART_NORMAL ||
          part_elem->part_state == PART_TO_BE_ADDED ||
          part_elem->part_state == PART_CHANGED)
      {
1471
        num_remain_partitions+= num_subparts;
unknown's avatar
unknown committed
1472
      }
1473
    } while (++i < num_parts);
unknown's avatar
unknown committed
1474 1475
  }
  if (!(new_file_array= (handler**)sql_calloc(sizeof(handler*)*
1476
                                            (2*(num_remain_partitions + 1)))))
unknown's avatar
unknown committed
1477
  {
1478
    mem_alloc_error(sizeof(handler*)*2*(num_remain_partitions+1));
1479
    DBUG_RETURN(ER_OUTOFMEMORY);
unknown's avatar
unknown committed
1480
  }
1481
  m_added_file= &new_file_array[num_remain_partitions + 1];
unknown's avatar
unknown committed
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499

  /*
    Step 3:
      Fill m_reorged_file with handler references and NULL at the end
  */
  if (m_reorged_parts)
  {
    i= 0;
    part_count= 0;
    first= TRUE;
    part_it.rewind();
    do
    {
      partition_element *part_elem= part_it++;
      if (part_elem->part_state == PART_CHANGED ||
          part_elem->part_state == PART_REORGED_DROPPED)
      {
        memcpy((void*)&m_reorged_file[part_count],
1500 1501 1502
               (void*)&m_file[i*num_subparts],
               sizeof(handler*)*num_subparts);
        part_count+= num_subparts;
unknown's avatar
unknown committed
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
      }
      else if (first && temp_partitions &&
               part_elem->part_state == PART_TO_BE_ADDED)
      {
        /*
          When doing an ALTER TABLE REORGANIZE PARTITION a number of
          partitions is to be reorganised into a set of new partitions.
          The reorganised partitions are in this case in the temp_partitions
          list. We copy all of them in one batch and thus we only do this
          until we find the first partition with state PART_TO_BE_ADDED
          since this is where the new partitions go in and where the old
          ones used to be.
        */
        first= FALSE;
1517 1518
        DBUG_ASSERT(((i*num_subparts) + m_reorged_parts) <= m_file_tot_parts);
        memcpy((void*)m_reorged_file, &m_file[i*num_subparts],
1519
               sizeof(handler*)*m_reorged_parts);
unknown's avatar
unknown committed
1520
      }
1521
    } while (++i < num_parts);
unknown's avatar
unknown committed
1522 1523 1524 1525 1526 1527 1528 1529 1530
  }

  /*
    Step 4:
      Fill new_array_file with handler references. Create the handlers if
      needed.
  */
  i= 0;
  part_count= 0;
unknown's avatar
unknown committed
1531
  orig_count= 0;
1532
  first= TRUE;
unknown's avatar
unknown committed
1533 1534 1535 1536 1537 1538
  part_it.rewind();
  do
  {
    partition_element *part_elem= part_it++;
    if (part_elem->part_state == PART_NORMAL)
    {
1539
      DBUG_ASSERT(orig_count + num_subparts <= m_file_tot_parts);
unknown's avatar
unknown committed
1540
      memcpy((void*)&new_file_array[part_count], (void*)&m_file[orig_count],
1541 1542 1543
             sizeof(handler*)*num_subparts);
      part_count+= num_subparts;
      orig_count+= num_subparts;
unknown's avatar
unknown committed
1544 1545 1546 1547 1548 1549 1550
    }
    else if (part_elem->part_state == PART_CHANGED ||
             part_elem->part_state == PART_TO_BE_ADDED)
    {
      uint j= 0;
      do
      {
1551 1552 1553 1554
        if (!(new_file_array[part_count++]=
              get_new_handler(table->s,
                              thd->mem_root,
                              part_elem->engine_type)))
unknown's avatar
unknown committed
1555 1556
        {
          mem_alloc_error(sizeof(handler));
1557
          DBUG_RETURN(ER_OUTOFMEMORY);
unknown's avatar
unknown committed
1558
        }
1559
      } while (++j < num_subparts);
1560
      if (part_elem->part_state == PART_CHANGED)
1561
        orig_count+= num_subparts;
1562 1563
      else if (temp_partitions && first)
      {
1564
        orig_count+= (num_subparts * temp_partitions);
1565 1566
        first= FALSE;
      }
unknown's avatar
unknown committed
1567
    }
1568
  } while (++i < num_parts);
1569
  first= FALSE;
unknown's avatar
unknown committed
1570 1571 1572 1573 1574 1575 1576 1577 1578
  /*
    Step 5:
      Create the new partitions and also open, lock and call external_lock
      on them to prepare them for copy phase and also for later close
      calls
  */
  i= 0;
  part_count= 0;
  part_it.rewind();
unknown's avatar
unknown committed
1579 1580 1581
  do
  {
    partition_element *part_elem= part_it++;
unknown's avatar
unknown committed
1582 1583
    if (part_elem->part_state == PART_TO_BE_ADDED ||
        part_elem->part_state == PART_CHANGED)
unknown's avatar
unknown committed
1584 1585
    {
      /*
unknown's avatar
unknown committed
1586 1587 1588
        A new partition needs to be created PART_TO_BE_ADDED means an
        entirely new partition and PART_CHANGED means a changed partition
        that will still exist with either more or less data in it.
unknown's avatar
unknown committed
1589
      */
unknown's avatar
unknown committed
1590 1591 1592 1593
      uint name_variant= NORMAL_PART_NAME;
      if (part_elem->part_state == PART_CHANGED ||
          (part_elem->part_state == PART_TO_BE_ADDED && temp_partitions))
        name_variant= TEMP_PART_NAME;
1594
      if (m_part_info->is_sub_partitioned())
unknown's avatar
unknown committed
1595 1596 1597 1598 1599 1600 1601 1602
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
        uint j= 0, part;
        do
        {
          partition_element *sub_elem= sub_it++;
          create_subpartition_name(part_name_buff, path,
                                   part_elem->partition_name,
unknown's avatar
unknown committed
1603 1604
                                   sub_elem->partition_name,
                                   name_variant);
1605
          part= i * num_subparts + j;
unknown's avatar
unknown committed
1606 1607 1608
          DBUG_PRINT("info", ("Add subpartition %s", part_name_buff));
          if ((error= prepare_new_partition(table, create_info,
                                            new_file_array[part],
1609 1610
                                            (const char *)part_name_buff,
                                            sub_elem)))
unknown's avatar
unknown committed
1611 1612
          {
            cleanup_new_partition(part_count);
1613
            DBUG_RETURN(error);
unknown's avatar
unknown committed
1614 1615
          }
          m_added_file[part_count++]= new_file_array[part];
1616
        } while (++j < num_subparts);
unknown's avatar
unknown committed
1617 1618 1619 1620
      }
      else
      {
        create_partition_name(part_name_buff, path,
unknown's avatar
unknown committed
1621 1622 1623 1624 1625
                              part_elem->partition_name, name_variant,
                              TRUE);
        DBUG_PRINT("info", ("Add partition %s", part_name_buff));
        if ((error= prepare_new_partition(table, create_info,
                                          new_file_array[i],
1626 1627
                                          (const char *)part_name_buff,
                                          part_elem)))
unknown's avatar
unknown committed
1628 1629
        {
          cleanup_new_partition(part_count);
1630
          DBUG_RETURN(error);
unknown's avatar
unknown committed
1631 1632
        }
        m_added_file[part_count++]= new_file_array[i];
unknown's avatar
unknown committed
1633 1634
      }
    }
1635
  } while (++i < num_parts);
unknown's avatar
unknown committed
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651

  /*
    Step 6:
      State update to prepare for next write of the frm file.
  */
  i= 0;
  part_it.rewind();
  do
  {
    partition_element *part_elem= part_it++;
    if (part_elem->part_state == PART_TO_BE_ADDED)
      part_elem->part_state= PART_IS_ADDED;
    else if (part_elem->part_state == PART_CHANGED)
      part_elem->part_state= PART_IS_CHANGED;
    else if (part_elem->part_state == PART_REORGED_DROPPED)
      part_elem->part_state= PART_TO_BE_DROPPED;
1652
  } while (++i < num_parts);
unknown's avatar
unknown committed
1653 1654 1655 1656 1657 1658 1659
  for (i= 0; i < temp_partitions; i++)
  {
    partition_element *part_elem= t_it++;
    DBUG_ASSERT(part_elem->part_state == PART_TO_BE_REORGED);
    part_elem->part_state= PART_TO_BE_DROPPED;
  }
  m_new_file= new_file_array;
1660 1661 1662 1663 1664 1665 1666 1667 1668
  if ((error= copy_partitions(copied, deleted)))
  {
    /*
      Close and unlock the new temporary partitions.
      They will later be deleted through the ddl-log.
    */
    cleanup_new_partition(part_count);
  }
  DBUG_RETURN(error);
unknown's avatar
unknown committed
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
}


/*
  Copy partitions as part of ALTER TABLE of partitions

  SYNOPSIS
    copy_partitions()
    out:copied                 Number of records copied
    out:deleted                Number of records deleted

  RETURN VALUE
    >0                         Error code
    0                          Success

  DESCRIPTION
    change_partitions has done all the preparations, now it is time to
    actually copy the data from the reorganised partitions to the new
    partitions.
*/

1690 1691
int ha_partition::copy_partitions(ulonglong * const copied,
                                  ulonglong * const deleted)
unknown's avatar
unknown committed
1692 1693 1694
{
  uint reorg_part= 0;
  int result= 0;
1695
  longlong func_value;
unknown's avatar
unknown committed
1696 1697
  DBUG_ENTER("ha_partition::copy_partitions");

1698 1699 1700
  if (m_part_info->linear_hash_ind)
  {
    if (m_part_info->part_type == HASH_PARTITION)
1701
      set_linear_hash_mask(m_part_info, m_part_info->num_parts);
1702
    else
1703
      set_linear_hash_mask(m_part_info, m_part_info->num_subparts);
1704 1705
  }

unknown's avatar
unknown committed
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
  while (reorg_part < m_reorged_parts)
  {
    handler *file= m_reorged_file[reorg_part];
    uint32 new_part;

    late_extra_cache(reorg_part);
    if ((result= file->ha_rnd_init(1)))
      goto error;
    while (TRUE)
    {
      if ((result= file->rnd_next(m_rec0)))
      {
        if (result == HA_ERR_RECORD_DELETED)
          continue;                              //Probably MyISAM
        if (result != HA_ERR_END_OF_FILE)
          goto error;
        /*
          End-of-file reached, break out to continue with next partition or
          end the copy process.
        */
        break;
      }
      /* Found record to insert into new handler */
1729 1730
      if (m_part_info->get_partition_id(m_part_info, &new_part,
                                        &func_value))
unknown's avatar
unknown committed
1731 1732 1733 1734 1735 1736
      {
        /*
           This record is in the original table but will not be in the new
           table since it doesn't fit into any partition any longer due to
           changed partitioning ranges or list values.
        */
1737
        (*deleted)++;
unknown's avatar
unknown committed
1738 1739 1740
      }
      else
      {
1741
        THD *thd= ha_thd();
unknown's avatar
unknown committed
1742
        /* Copy record to new handler */
1743
        (*copied)++;
1744 1745 1746 1747
        tmp_disable_binlog(thd); /* Do not replicate the low-level changes. */
        result= m_new_file[new_part]->ha_write_row(m_rec0);
        reenable_binlog(thd);
        if (result)
unknown's avatar
unknown committed
1748 1749 1750 1751
          goto error;
      }
    }
    late_extra_no_cache(reorg_part);
1752
    file->ha_rnd_end();
unknown's avatar
unknown committed
1753 1754 1755 1756
    reorg_part++;
  }
  DBUG_RETURN(FALSE);
error:
1757
  m_reorged_file[reorg_part]->ha_rnd_end();
1758
  DBUG_RETURN(result);
unknown's avatar
unknown committed
1759
}
1760

unknown's avatar
unknown committed
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775

/*
  Update create info as part of ALTER TABLE

  SYNOPSIS
    update_create_info()
    create_info                   Create info from ALTER TABLE

  RETURN VALUE
    NONE

  DESCRIPTION
    Method empty so far
*/

1776 1777
void ha_partition::update_create_info(HA_CREATE_INFO *create_info)
{
1778 1779 1780 1781 1782 1783 1784 1785
  /*
    Fix for bug#38751, some engines needs info-calls in ALTER.
    Archive need this since it flushes in ::info.
    HA_STATUS_AUTO is optimized so it will not always be forwarded
    to all partitions, but HA_STATUS_VARIABLE will.
  */
  info(HA_STATUS_VARIABLE);

1786 1787 1788 1789 1790
  info(HA_STATUS_AUTO);

  if (!(create_info->used_fields & HA_CREATE_USED_AUTO))
    create_info->auto_increment_value= stats.auto_increment_value;

1791
  create_info->data_file_name= create_info->index_file_name = NULL;
1792 1793 1794 1795
  return;
}


1796 1797
void ha_partition::change_table_ptr(TABLE *table_arg, TABLE_SHARE *share)
{
1798
  handler **file_array;
1799 1800
  table= table_arg;
  table_share= share;
1801 1802 1803 1804 1805
  /*
    m_file can be NULL when using an old cached table in DROP TABLE, when the
    table just has REMOVED PARTITIONING, see Bug#42438
  */
  if (m_file)
1806
  {
1807 1808 1809 1810 1811 1812 1813 1814
    file_array= m_file;
    DBUG_ASSERT(*file_array);
    do
    {
      (*file_array)->change_table_ptr(table_arg, share);
    } while (*(++file_array));
  }

1815 1816 1817 1818 1819 1820 1821 1822 1823
  if (m_added_file && m_added_file[0])
  {
    /* if in middle of a drop/rename etc */
    file_array= m_added_file;
    do
    {
      (*file_array)->change_table_ptr(table_arg, share);
    } while (*(++file_array));
  }
1824 1825
}

unknown's avatar
unknown committed
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
/*
  Change comments specific to handler

  SYNOPSIS
    update_table_comment()
    comment                       Original comment

  RETURN VALUE
    new comment 

  DESCRIPTION
    No comment changes so far
*/

1840 1841
char *ha_partition::update_table_comment(const char *comment)
{
unknown's avatar
unknown committed
1842
  return (char*) comment;                       /* Nothing to change */
1843 1844 1845 1846 1847
}



/*
unknown's avatar
unknown committed
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
  Handle delete, rename and create table

  SYNOPSIS
    del_ren_cre_table()
    from                    Full path of old table
    to                      Full path of new table
    table_arg               Table object
    create_info             Create info

  RETURN VALUE
    >0                      Error
    0                       Success

  DESCRIPTION
    Common routine to handle delete_table and rename_table.
    The routine uses the partition handler file to get the
    names of the partition instances. Both these routines
    are called after creating the handler without table
    object and thus the file is needed to discover the
    names of the partitions and the underlying storage engines.
1868 1869 1870 1871 1872 1873 1874
*/

uint ha_partition::del_ren_cre_table(const char *from,
				     const char *to,
				     TABLE *table_arg,
				     HA_CREATE_INFO *create_info)
{
unknown's avatar
unknown committed
1875 1876
  int save_error= 0;
  int error;
1877 1878
  char from_buff[FN_REFLEN], to_buff[FN_REFLEN], from_lc_buff[FN_REFLEN],
       to_lc_buff[FN_REFLEN];
1879
  char *name_buffer_ptr;
1880 1881
  const char *from_path;
  const char *to_path= NULL;
1882
  uint i;
1883
  handler **file, **abort_file;
1884 1885
  DBUG_ENTER("del_ren_cre_table()");

1886 1887 1888 1889 1890 1891 1892
  /* Not allowed to create temporary partitioned tables */
  if (create_info && create_info->options & HA_LEX_CREATE_TMP_TABLE)
  {
    my_error(ER_PARTITION_NO_TEMPORARY, MYF(0));
    DBUG_RETURN(TRUE);
  }

1893
  if (get_from_handler_file(from, ha_thd()->mem_root))
1894 1895
    DBUG_RETURN(TRUE);
  DBUG_ASSERT(m_file_buffer);
1896
  DBUG_PRINT("enter", ("from: (%s) to: (%s)", from, to ? to : "(nil)"));
1897 1898
  name_buffer_ptr= m_name_buffer_ptr;
  file= m_file;
1899 1900 1901 1902 1903 1904 1905 1906 1907
  if (to == NULL && table_arg == NULL)
  {
    /*
      Delete table, start by delete the .par file. If error, break, otherwise
      delete as much as possible.
    */
    if ((error= handler::delete_table(from)))
      DBUG_RETURN(error);
  }
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
  /*
    Since ha_partition has HA_FILE_BASED, it must alter underlying table names
    if they do not have HA_FILE_BASED and lower_case_table_names == 2.
    See Bug#37402, for Mac OS X.
    The appended #P#<partname>[#SP#<subpartname>] will remain in current case.
    Using the first partitions handler, since mixing handlers is not allowed.
  */
  from_path= get_canonical_filename(*file, from, from_lc_buff);
  if (to != NULL)
    to_path= get_canonical_filename(*file, to, to_lc_buff);
1918 1919 1920
  i= 0;
  do
  {
1921 1922 1923
    create_partition_name(from_buff, from_path, name_buffer_ptr,
                          NORMAL_PART_NAME, FALSE);

1924 1925
    if (to != NULL)
    {						// Rename branch
1926 1927
      create_partition_name(to_buff, to_path, name_buffer_ptr,
                            NORMAL_PART_NAME, FALSE);
1928
      error= (*file)->ha_rename_table(from_buff, to_buff);
1929 1930
      if (error)
        goto rename_error;
1931 1932
    }
    else if (table_arg == NULL)			// delete branch
1933
      error= (*file)->ha_delete_table(from_buff);
1934 1935
    else
    {
1936 1937
      if ((error= set_up_table_before_create(table_arg, from_buff,
                                             create_info, i, NULL)) ||
1938
          ((error= (*file)->ha_create(from_buff, table_arg, create_info))))
1939
        goto create_error;
1940 1941 1942 1943 1944 1945
    }
    name_buffer_ptr= strend(name_buffer_ptr) + 1;
    if (error)
      save_error= error;
    i++;
  } while (*(++file));
1946 1947 1948 1949 1950 1951 1952 1953 1954
  if (to != NULL)
  {
    if ((error= handler::rename_table(from, to)))
    {
      /* Try to revert everything, ignore errors */
      (void) handler::rename_table(to, from);
      goto rename_error;
    }
  }
1955
  DBUG_RETURN(save_error);
1956 1957 1958 1959
create_error:
  name_buffer_ptr= m_name_buffer_ptr;
  for (abort_file= file, file= m_file; file < abort_file; file++)
  {
1960
    create_partition_name(from_buff, from_path, name_buffer_ptr, NORMAL_PART_NAME,
1961
                          FALSE);
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
    (void) (*file)->ha_delete_table((const char*) from_buff);
    name_buffer_ptr= strend(name_buffer_ptr) + 1;
  }
  DBUG_RETURN(error);
rename_error:
  name_buffer_ptr= m_name_buffer_ptr;
  for (abort_file= file, file= m_file; file < abort_file; file++)
  {
    /* Revert the rename, back from 'to' to the original 'from' */
    create_partition_name(from_buff, from_path, name_buffer_ptr,
                          NORMAL_PART_NAME, FALSE);
    create_partition_name(to_buff, to_path, name_buffer_ptr,
                          NORMAL_PART_NAME, FALSE);
    /* Ignore error here */
    (void) (*file)->ha_rename_table(to_buff, from_buff);
1977 1978 1979
    name_buffer_ptr= strend(name_buffer_ptr) + 1;
  }
  DBUG_RETURN(error);
1980 1981
}

unknown's avatar
unknown committed
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
/*
  Find partition based on partition id

  SYNOPSIS
    find_partition_element()
    part_id                   Partition id of partition looked for

  RETURN VALUE
    >0                        Reference to partition_element
    0                         Partition not found
*/
1993 1994 1995 1996 1997

partition_element *ha_partition::find_partition_element(uint part_id)
{
  uint i;
  uint curr_part_id= 0;
unknown's avatar
unknown committed
1998
  List_iterator_fast <partition_element> part_it(m_part_info->partitions);
1999

2000
  for (i= 0; i < m_part_info->num_parts; i++)
2001 2002 2003 2004 2005 2006 2007
  {
    partition_element *part_elem;
    part_elem= part_it++;
    if (m_is_sub_partitioned)
    {
      uint j;
      List_iterator_fast <partition_element> sub_it(part_elem->subpartitions);
2008
      for (j= 0; j < m_part_info->num_subparts; j++)
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
      {
	part_elem= sub_it++;
	if (part_id == curr_part_id++)
	  return part_elem;
      }
    }
    else if (part_id == curr_part_id++)
      return part_elem;
  }
  DBUG_ASSERT(0);
2019
  my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
2020 2021 2022 2023
  return NULL;
}


unknown's avatar
unknown committed
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
/*
   Set up table share object before calling create on underlying handler

   SYNOPSIS
     set_up_table_before_create()
     table                       Table object
     info                        Create info
     part_id                     Partition id of partition to set-up

   RETURN VALUE
2034 2035
     TRUE                        Error
     FALSE                       Success
unknown's avatar
unknown committed
2036 2037 2038 2039 2040 2041 2042 2043 2044

   DESCRIPTION
     Set up
     1) Comment on partition
     2) MAX_ROWS, MIN_ROWS on partition
     3) Index file name on partition
     4) Data file name on partition
*/

2045
int ha_partition::set_up_table_before_create(TABLE *tbl,
2046 2047 2048 2049
                    const char *partition_name_with_path, 
                    HA_CREATE_INFO *info,
                    uint part_id,
                    partition_element *part_elem)
2050
{
2051
  int error= 0;
2052
  const char *partition_name;
2053
  THD *thd= ha_thd();
2054 2055
  DBUG_ENTER("set_up_table_before_create");

2056
  if (!part_elem)
2057 2058 2059
  {
    part_elem= find_partition_element(part_id);
    if (!part_elem)
2060
      DBUG_RETURN(1);                             // Fatal error
2061
  }
2062 2063
  tbl->s->max_rows= part_elem->part_max_rows;
  tbl->s->min_rows= part_elem->part_min_rows;
2064
  partition_name= strrchr(partition_name_with_path, FN_LIBCHAR);
2065
  if ((part_elem->index_file_name &&
2066
      (error= append_file_to_dir(thd,
2067 2068 2069
                                 (const char**)&part_elem->index_file_name,
                                 partition_name+1))) ||
      (part_elem->data_file_name &&
2070
      (error= append_file_to_dir(thd,
2071 2072 2073 2074 2075
                                 (const char**)&part_elem->data_file_name,
                                 partition_name+1))))
  {
    DBUG_RETURN(error);
  }
2076 2077
  info->index_file_name= part_elem->index_file_name;
  info->data_file_name= part_elem->data_file_name;
2078
  DBUG_RETURN(0);
2079 2080 2081 2082
}


/*
unknown's avatar
unknown committed
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
  Add two names together

  SYNOPSIS
    name_add()
    out:dest                          Destination string
    first_name                        First name
    sec_name                          Second name

  RETURN VALUE
    >0                                Error
    0                                 Success

  DESCRIPTION
    Routine used to add two names with '_' in between then. Service routine
    to create_handler_file
    Include the NULL in the count of characters since it is needed as separator
    between the partition names.
2100 2101 2102 2103
*/

static uint name_add(char *dest, const char *first_name, const char *sec_name)
{
unknown's avatar
unknown committed
2104
  return (uint) (strxmov(dest, first_name, "#SP#", sec_name, NullS) -dest) + 1;
2105 2106 2107 2108
}


/*
unknown's avatar
unknown committed
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
  Create the special .par file

  SYNOPSIS
    create_handler_file()
    name                      Full path of table name

  RETURN VALUE
    >0                        Error code
    0                         Success

  DESCRIPTION
    Method used to create handler file with names of partitions, their
    engine types and the number of partitions.
2122 2123 2124 2125 2126 2127
*/

bool ha_partition::create_handler_file(const char *name)
{
  partition_element *part_elem, *subpart_elem;
  uint i, j, part_name_len, subpart_name_len;
2128
  uint tot_partition_words, tot_name_len, num_parts;
unknown's avatar
unknown committed
2129
  uint tot_parts= 0;
2130 2131 2132 2133 2134
  uint tot_len_words, tot_len_byte, chksum, tot_name_words;
  char *name_buffer_ptr;
  uchar *file_buffer, *engine_array;
  bool result= TRUE;
  char file_name[FN_REFLEN];
unknown's avatar
unknown committed
2135 2136
  char part_name[FN_REFLEN];
  char subpart_name[FN_REFLEN];
2137
  File file;
unknown's avatar
unknown committed
2138
  List_iterator_fast <partition_element> part_it(m_part_info->partitions);
2139 2140
  DBUG_ENTER("create_handler_file");

2141 2142 2143
  num_parts= m_part_info->partitions.elements;
  DBUG_PRINT("info", ("table name = %s, num_parts = %u", name,
                      num_parts));
2144
  tot_name_len= 0;
2145
  for (i= 0; i < num_parts; i++)
2146 2147
  {
    part_elem= part_it++;
unknown's avatar
unknown committed
2148
    if (part_elem->part_state != PART_NORMAL &&
2149 2150
        part_elem->part_state != PART_TO_BE_ADDED &&
        part_elem->part_state != PART_CHANGED)
unknown's avatar
unknown committed
2151 2152 2153 2154
      continue;
    tablename_to_filename(part_elem->partition_name, part_name,
                          FN_REFLEN);
    part_name_len= strlen(part_name);
2155
    if (!m_is_sub_partitioned)
unknown's avatar
unknown committed
2156
    {
2157
      tot_name_len+= part_name_len + 1;
unknown's avatar
unknown committed
2158 2159
      tot_parts++;
    }
2160 2161
    else
    {
unknown's avatar
unknown committed
2162
      List_iterator_fast <partition_element> sub_it(part_elem->subpartitions);
2163
      for (j= 0; j < m_part_info->num_subparts; j++)
2164 2165
      {
	subpart_elem= sub_it++;
unknown's avatar
unknown committed
2166 2167 2168 2169 2170 2171
        tablename_to_filename(subpart_elem->partition_name,
                              subpart_name,
                              FN_REFLEN);
	subpart_name_len= strlen(subpart_name);
	tot_name_len+= part_name_len + subpart_name_len + 5;
        tot_parts++;
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
      }
    }
  }
  /*
     File format:
     Length in words              4 byte
     Checksum                     4 byte
     Total number of partitions   4 byte
     Array of engine types        n * 4 bytes where
     n = (m_tot_parts + 3)/4
     Length of name part in bytes 4 bytes
     Name part                    m * 4 bytes where
     m = ((length_name_part + 3)/4)*4

     All padding bytes are zeroed
  */
unknown's avatar
unknown committed
2188
  tot_partition_words= (tot_parts + 3) / 4;
2189 2190 2191 2192 2193 2194 2195 2196
  tot_name_words= (tot_name_len + 3) / 4;
  tot_len_words= 4 + tot_partition_words + tot_name_words;
  tot_len_byte= 4 * tot_len_words;
  if (!(file_buffer= (uchar *) my_malloc(tot_len_byte, MYF(MY_ZEROFILL))))
    DBUG_RETURN(TRUE);
  engine_array= (file_buffer + 12);
  name_buffer_ptr= (char*) (file_buffer + ((4 + tot_partition_words) * 4));
  part_it.rewind();
2197
  for (i= 0; i < num_parts; i++)
2198 2199
  {
    part_elem= part_it++;
unknown's avatar
unknown committed
2200
    if (part_elem->part_state != PART_NORMAL &&
2201 2202
        part_elem->part_state != PART_TO_BE_ADDED &&
        part_elem->part_state != PART_CHANGED)
unknown's avatar
unknown committed
2203
      continue;
2204 2205
    if (!m_is_sub_partitioned)
    {
unknown's avatar
unknown committed
2206 2207
      tablename_to_filename(part_elem->partition_name, part_name, FN_REFLEN);
      name_buffer_ptr= strmov(name_buffer_ptr, part_name)+1;
2208
      *engine_array= (uchar) ha_legacy_type(part_elem->engine_type);
2209 2210 2211 2212 2213
      DBUG_PRINT("info", ("engine: %u", *engine_array));
      engine_array++;
    }
    else
    {
unknown's avatar
unknown committed
2214
      List_iterator_fast <partition_element> sub_it(part_elem->subpartitions);
2215
      for (j= 0; j < m_part_info->num_subparts; j++)
2216 2217
      {
	subpart_elem= sub_it++;
unknown's avatar
unknown committed
2218 2219 2220 2221
        tablename_to_filename(part_elem->partition_name, part_name,
                              FN_REFLEN);
        tablename_to_filename(subpart_elem->partition_name, subpart_name,
                              FN_REFLEN);
2222
	name_buffer_ptr+= name_add(name_buffer_ptr,
unknown's avatar
unknown committed
2223 2224
				   part_name,
				   subpart_name);
2225 2226
        *engine_array= (uchar) ha_legacy_type(subpart_elem->engine_type);
        DBUG_PRINT("info", ("engine: %u", *engine_array));
2227 2228 2229 2230 2231 2232
	engine_array++;
      }
    }
  }
  chksum= 0;
  int4store(file_buffer, tot_len_words);
unknown's avatar
unknown committed
2233
  int4store(file_buffer + 8, tot_parts);
2234 2235 2236 2237 2238 2239 2240 2241 2242
  int4store(file_buffer + 12 + (tot_partition_words * 4), tot_name_len);
  for (i= 0; i < tot_len_words; i++)
    chksum^= uint4korr(file_buffer + 4 * i);
  int4store(file_buffer + 4, chksum);
  /*
    Remove .frm extension and replace with .par
    Create and write and close file
    to be used at open, delete_table and rename_table
  */
2243
  fn_format(file_name, name, "", ha_par_ext, MY_APPEND_EXT);
Marc Alff's avatar
Marc Alff committed
2244 2245 2246
  if ((file= mysql_file_create(key_file_partition,
                               file_name, CREATE_MODE, O_RDWR | O_TRUNC,
                               MYF(MY_WME))) >= 0)
2247
  {
Marc Alff's avatar
Marc Alff committed
2248 2249 2250
    result= mysql_file_write(file, (uchar *) file_buffer, tot_len_byte,
                             MYF(MY_WME | MY_NABP)) != 0;
    (void) mysql_file_close(file, MYF(0));
2251 2252 2253
  }
  else
    result= TRUE;
2254
  my_free(file_buffer);
2255 2256 2257
  DBUG_RETURN(result);
}

unknown's avatar
unknown committed
2258 2259 2260 2261 2262 2263 2264 2265 2266
/*
  Clear handler variables and free some memory

  SYNOPSIS
    clear_handler_file()

  RETURN VALUE 
    NONE
*/
2267 2268 2269

void ha_partition::clear_handler_file()
{
unknown's avatar
unknown committed
2270 2271
  if (m_engine_array)
    plugin_unlock_list(NULL, m_engine_array, m_tot_parts);
2272 2273
  my_free(m_file_buffer);
  my_free(m_engine_array);
2274 2275 2276 2277
  m_file_buffer= NULL;
  m_engine_array= NULL;
}

unknown's avatar
unknown committed
2278 2279 2280 2281 2282
/*
  Create underlying handler objects

  SYNOPSIS
    create_handlers()
2283
    mem_root		Allocate memory through this
unknown's avatar
unknown committed
2284 2285 2286 2287 2288

  RETURN VALUE
    TRUE                  Error
    FALSE                 Success
*/
2289

2290
bool ha_partition::create_handlers(MEM_ROOT *mem_root)
2291 2292 2293
{
  uint i;
  uint alloc_len= (m_tot_parts + 1) * sizeof(handler*);
unknown's avatar
unknown committed
2294
  handlerton *hton0;
2295 2296
  DBUG_ENTER("create_handlers");

2297
  if (!(m_file= (handler **) alloc_root(mem_root, alloc_len)))
2298
    DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
2299
  m_file_tot_parts= m_tot_parts;
2300
  bzero((char*) m_file, alloc_len);
2301 2302
  for (i= 0; i < m_tot_parts; i++)
  {
unknown's avatar
unknown committed
2303
    handlerton *hton= plugin_data(m_engine_array[i], handlerton*);
2304
    if (!(m_file[i]= get_new_handler(table_share, mem_root,
unknown's avatar
unknown committed
2305
                                     hton)))
2306
      DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
2307
    DBUG_PRINT("info", ("engine_type: %u", hton->db_type));
2308 2309
  }
  /* For the moment we only support partition over the same table engine */
unknown's avatar
unknown committed
2310 2311
  hton0= plugin_data(m_engine_array[0], handlerton*);
  if (hton0 == myisam_hton)
2312 2313 2314 2315
  {
    DBUG_PRINT("info", ("MyISAM"));
    m_myisam= TRUE;
  }
unknown's avatar
unknown committed
2316
  /* INNODB may not be compiled in... */
unknown's avatar
unknown committed
2317
  else if (ha_legacy_type(hton0) == DB_TYPE_INNODB)
2318 2319 2320 2321 2322 2323 2324
  {
    DBUG_PRINT("info", ("InnoDB"));
    m_innodb= TRUE;
  }
  DBUG_RETURN(FALSE);
}

unknown's avatar
unknown committed
2325 2326 2327 2328 2329
/*
  Create underlying handler objects from partition info

  SYNOPSIS
    new_handlers_from_part_info()
2330
    mem_root		Allocate memory through this
unknown's avatar
unknown committed
2331 2332 2333 2334 2335

  RETURN VALUE
    TRUE                  Error
    FALSE                 Success
*/
2336

2337
bool ha_partition::new_handlers_from_part_info(MEM_ROOT *mem_root)
2338
{
unknown's avatar
unknown committed
2339
  uint i, j, part_count;
2340 2341 2342 2343 2344
  partition_element *part_elem;
  uint alloc_len= (m_tot_parts + 1) * sizeof(handler*);
  List_iterator_fast <partition_element> part_it(m_part_info->partitions);
  DBUG_ENTER("ha_partition::new_handlers_from_part_info");

2345
  if (!(m_file= (handler **) alloc_root(mem_root, alloc_len)))
unknown's avatar
unknown committed
2346 2347 2348 2349
  {
    mem_alloc_error(alloc_len);
    goto error_end;
  }
unknown's avatar
unknown committed
2350
  m_file_tot_parts= m_tot_parts;
2351
  bzero((char*) m_file, alloc_len);
2352
  DBUG_ASSERT(m_part_info->num_parts > 0);
2353 2354

  i= 0;
unknown's avatar
unknown committed
2355
  part_count= 0;
2356 2357 2358 2359 2360 2361 2362 2363 2364
  /*
    Don't know the size of the underlying storage engine, invent a number of
    bytes allocated for error message if allocation fails
  */
  do
  {
    part_elem= part_it++;
    if (m_is_sub_partitioned)
    {
2365
      for (j= 0; j < m_part_info->num_subparts; j++)
2366
      {
2367 2368
	if (!(m_file[part_count++]= get_new_handler(table_share, mem_root,
                                                    part_elem->engine_type)))
2369
          goto error;
unknown's avatar
unknown committed
2370 2371
	DBUG_PRINT("info", ("engine_type: %u",
                   (uint) ha_legacy_type(part_elem->engine_type)));
2372 2373
      }
    }
unknown's avatar
unknown committed
2374 2375
    else
    {
2376
      if (!(m_file[part_count++]= get_new_handler(table_share, mem_root,
unknown's avatar
unknown committed
2377 2378 2379 2380 2381
                                                  part_elem->engine_type)))
        goto error;
      DBUG_PRINT("info", ("engine_type: %u",
                 (uint) ha_legacy_type(part_elem->engine_type)));
    }
2382
  } while (++i < m_part_info->num_parts);
2383
  if (part_elem->engine_type == myisam_hton)
2384 2385 2386 2387 2388 2389
  {
    DBUG_PRINT("info", ("MyISAM"));
    m_myisam= TRUE;
  }
  DBUG_RETURN(FALSE);
error:
unknown's avatar
unknown committed
2390 2391
  mem_alloc_error(sizeof(handler));
error_end:
2392 2393 2394 2395 2396
  DBUG_RETURN(TRUE);
}


/*
unknown's avatar
unknown committed
2397 2398 2399 2400 2401
  Get info about partition engines and their names from the .par file

  SYNOPSIS
    get_from_handler_file()
    name                        Full path of table name
2402
    mem_root			Allocate memory through this
unknown's avatar
unknown committed
2403 2404 2405 2406 2407 2408 2409 2410

  RETURN VALUE
    TRUE                        Error
    FALSE                       Success

  DESCRIPTION
    Open handler file to get partition names, engine types and number of
    partitions.
2411 2412
*/

2413
bool ha_partition::get_from_handler_file(const char *name, MEM_ROOT *mem_root)
2414 2415 2416 2417
{
  char buff[FN_REFLEN], *address_tot_name_len;
  File file;
  char *file_buffer, *name_buffer_ptr;
unknown's avatar
unknown committed
2418
  handlerton **engine_array;
2419 2420 2421 2422 2423 2424
  uint i, len_bytes, len_words, tot_partition_words, tot_name_words, chksum;
  DBUG_ENTER("ha_partition::get_from_handler_file");
  DBUG_PRINT("enter", ("table name: '%s'", name));

  if (m_file_buffer)
    DBUG_RETURN(FALSE);
2425
  fn_format(buff, name, "", ha_par_ext, MY_APPEND_EXT);
2426

Marc Alff's avatar
Marc Alff committed
2427 2428 2429
  /* Following could be done with mysql_file_stat to read in whole file */
  if ((file= mysql_file_open(key_file_partition,
                             buff, O_RDONLY | O_SHARE, MYF(0))) < 0)
2430
    DBUG_RETURN(TRUE);
Marc Alff's avatar
Marc Alff committed
2431
  if (mysql_file_read(file, (uchar *) &buff[0], 8, MYF(MY_NABP)))
2432 2433 2434
    goto err1;
  len_words= uint4korr(buff);
  len_bytes= 4 * len_words;
2435
  if (!(file_buffer= (char*) my_malloc(len_bytes, MYF(0))))
2436
    goto err1;
Marc Alff's avatar
Marc Alff committed
2437 2438
  mysql_file_seek(file, 0, MY_SEEK_SET, MYF(0));
  if (mysql_file_read(file, (uchar *) file_buffer, len_bytes, MYF(MY_NABP)))
2439 2440 2441 2442 2443 2444 2445 2446
    goto err2;

  chksum= 0;
  for (i= 0; i < len_words; i++)
    chksum ^= uint4korr((file_buffer) + 4 * i);
  if (chksum)
    goto err2;
  m_tot_parts= uint4korr((file_buffer) + 8);
unknown's avatar
unknown committed
2447
  DBUG_PRINT("info", ("No of parts = %u", m_tot_parts));
2448
  tot_partition_words= (m_tot_parts + 3) / 4;
unknown's avatar
unknown committed
2449
  engine_array= (handlerton **) my_alloca(m_tot_parts * sizeof(handlerton*));
unknown's avatar
unknown committed
2450
  for (i= 0; i < m_tot_parts; i++)
2451
  {
2452
    engine_array[i]= ha_resolve_by_legacy_type(ha_thd(),
2453
                                               (enum legacy_db_type)
2454 2455 2456 2457 2458
                                               *(uchar *) ((file_buffer) +
                                                           12 + i));
    if (!engine_array[i])
      goto err3;
  }
2459 2460 2461
  address_tot_name_len= file_buffer + 12 + 4 * tot_partition_words;
  tot_name_words= (uint4korr(address_tot_name_len) + 3) / 4;
  if (len_words != (tot_partition_words + tot_name_words + 4))
unknown's avatar
unknown committed
2462
    goto err3;
2463
  name_buffer_ptr= file_buffer + 16 + 4 * tot_partition_words;
Marc Alff's avatar
Marc Alff committed
2464
  (void) mysql_file_close(file, MYF(0));
2465 2466
  m_file_buffer= file_buffer;          // Will be freed in clear_handler_file()
  m_name_buffer_ptr= name_buffer_ptr;
unknown's avatar
unknown committed
2467 2468 2469
  
  if (!(m_engine_array= (plugin_ref*)
                my_malloc(m_tot_parts * sizeof(plugin_ref), MYF(MY_WME))))
unknown's avatar
unknown committed
2470
    goto err3;
unknown's avatar
unknown committed
2471 2472 2473

  for (i= 0; i < m_tot_parts; i++)
    m_engine_array[i]= ha_lock_engine(NULL, engine_array[i]);
unknown's avatar
unknown committed
2474 2475

  my_afree((gptr) engine_array);
unknown's avatar
unknown committed
2476
    
2477
  if (!m_file && create_handlers(mem_root))
2478 2479 2480 2481 2482 2483
  {
    clear_handler_file();
    DBUG_RETURN(TRUE);
  }
  DBUG_RETURN(FALSE);

unknown's avatar
unknown committed
2484 2485
err3:
  my_afree((gptr) engine_array);
2486
err2:
2487
  my_free(file_buffer);
2488
err1:
Marc Alff's avatar
Marc Alff committed
2489
  (void) mysql_file_close(file, MYF(0));
2490 2491 2492
  DBUG_RETURN(TRUE);
}

unknown's avatar
unknown committed
2493

2494 2495 2496
/****************************************************************************
                MODULE open/close object
****************************************************************************/
2497 2498 2499 2500 2501 2502


/**
  A destructor for partition-specific TABLE_SHARE data.
*/

Mattias Jonsson's avatar
Mattias Jonsson committed
2503
void ha_data_partition_destroy(HA_DATA_PARTITION* ha_part_data)
2504
{
2505
  if (ha_part_data)
2506
  {
Mattias Jonsson's avatar
Mattias Jonsson committed
2507
    mysql_mutex_destroy(&ha_part_data->LOCK_auto_inc);
2508 2509 2510
  }
}

2511
/*
unknown's avatar
unknown committed
2512
  Open handler object
2513

unknown's avatar
unknown committed
2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
  SYNOPSIS
    open()
    name                  Full path of table name
    mode                  Open mode flags
    test_if_locked        ?

  RETURN VALUE
    >0                    Error
    0                     Success

  DESCRIPTION
    Used for opening tables. The name will be the name of the file.
    A table is opened when it needs to be opened. For instance
    when a request comes in for a select on the table (tables are not
    open and closed for each request, they are cached).

    Called from handler.cc by handler::ha_open(). The server opens all tables
    by calling ha_open() which then calls the handler specific open().
2532 2533 2534 2535 2536
*/

int ha_partition::open(const char *name, int mode, uint test_if_locked)
{
  char *name_buffer_ptr= m_name_buffer_ptr;
unknown's avatar
unknown committed
2537
  int error;
2538
  uint alloc_len;
unknown's avatar
unknown committed
2539 2540
  handler **file;
  char name_buff[FN_REFLEN];
2541
  bool is_not_tmp_table= (table_share->tmp_table == NO_TMP_TABLE);
2542
  ulonglong check_table_flags= 0;
2543 2544
  DBUG_ENTER("ha_partition::open");

2545
  DBUG_ASSERT(table->s == table_share);
2546
  ref_length= 0;
unknown's avatar
unknown committed
2547 2548
  m_mode= mode;
  m_open_test_lock= test_if_locked;
2549
  m_part_field_array= m_part_info->full_part_field_array;
2550
  if (get_from_handler_file(name, &table->mem_root))
2551 2552 2553
    DBUG_RETURN(1);
  m_start_key.length= 0;
  m_rec0= table->record[0];
2554
  m_rec_length= table_share->reclength;
unknown's avatar
unknown committed
2555
  alloc_len= m_tot_parts * (m_rec_length + PARTITION_BYTES_IN_POS);
2556
  alloc_len+= table_share->max_key_length;
2557 2558
  if (!m_ordered_rec_buffer)
  {
2559
    if (!(m_ordered_rec_buffer= (uchar*)my_malloc(alloc_len, MYF(MY_WME))))
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570
    {
      DBUG_RETURN(1);
    }
    {
      /*
        We set-up one record per partition and each record has 2 bytes in
        front where the partition id is written. This is used by ordered
        index_read.
        We also set-up a reference to the first record for temporary use in
        setting up the scan.
      */
2571
      char *ptr= (char*)m_ordered_rec_buffer;
2572 2573 2574 2575 2576 2577
      uint i= 0;
      do
      {
        int2store(ptr, i);
        ptr+= m_rec_length + PARTITION_BYTES_IN_POS;
      } while (++i < m_tot_parts);
2578
      m_start_key.key= (const uchar*)ptr;
2579 2580
    }
  }
unknown's avatar
unknown committed
2581

2582 2583 2584 2585
  /* Initialize the bitmap we use to minimize ha_start_bulk_insert calls */
  if (bitmap_init(&m_bulk_insert_started, NULL, m_tot_parts + 1, FALSE))
    DBUG_RETURN(1);
  bitmap_clear_all(&m_bulk_insert_started);
2586
  /* Initialize the bitmap we use to determine what partitions are used */
unknown's avatar
unknown committed
2587 2588 2589
  if (!is_clone)
  {
    if (bitmap_init(&(m_part_info->used_partitions), NULL, m_tot_parts, TRUE))
2590 2591
    {
      bitmap_free(&m_bulk_insert_started);
unknown's avatar
unknown committed
2592
      DBUG_RETURN(1);
2593
    }
unknown's avatar
unknown committed
2594 2595
    bitmap_set_all(&(m_part_info->used_partitions));
  }
unknown's avatar
unknown committed
2596

2597 2598 2599
  file= m_file;
  do
  {
unknown's avatar
unknown committed
2600 2601
    create_partition_name(name_buff, name, name_buffer_ptr, NORMAL_PART_NAME,
                          FALSE);
unknown's avatar
unknown committed
2602
    if ((error= (*file)->ha_open(table, (const char*) name_buff, mode,
2603 2604
                                 test_if_locked)))
      goto err_handler;
2605
    m_num_locks+= (*file)->lock_count();
2606 2607
    name_buffer_ptr+= strlen(name_buffer_ptr) + 1;
    set_if_bigger(ref_length, ((*file)->ref_length));
2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
    /*
      Verify that all partitions have the same set of table flags.
      Mask all flags that partitioning enables/disables.
    */
    if (!check_table_flags)
    {
      check_table_flags= (((*file)->ha_table_flags() &
                           ~(PARTITION_DISABLED_TABLE_FLAGS)) |
                          (PARTITION_ENABLED_TABLE_FLAGS));
    }
    else if (check_table_flags != (((*file)->ha_table_flags() &
                                    ~(PARTITION_DISABLED_TABLE_FLAGS)) |
                                   (PARTITION_ENABLED_TABLE_FLAGS)))
    {
      error= HA_ERR_INITIALIZATION;
      goto err_handler;
    }
2625
  } while (*(++file));
2626 2627
  key_used_on_scan= m_file[0]->key_used_on_scan;
  implicit_emptied= m_file[0]->implicit_emptied;
2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639
  /*
    Add 2 bytes for partition id in position ref length.
    ref_length=max_in_all_partitions(ref_length) + PARTITION_BYTES_IN_POS
  */
  ref_length+= PARTITION_BYTES_IN_POS;
  m_ref_length= ref_length;
  /*
    Release buffer read from .par file. It will not be reused again after
    being opened once.
  */
  clear_handler_file();
  /*
2640
    Initialize priority queue, initialized to reading forward.
2641
  */
unknown's avatar
unknown committed
2642
  if ((error= init_queue(&m_queue, m_tot_parts, (uint) PARTITION_BYTES_IN_POS,
2643 2644
                         0, key_rec_cmp, (void*)this)))
    goto err_handler;
unknown's avatar
unknown committed
2645

2646
  /*
2647 2648
    Use table_share->ha_part_data to share auto_increment_value among
    all handlers for the same table.
2649 2650
  */
  if (is_not_tmp_table)
2651
    mysql_mutex_lock(&table_share->LOCK_ha_data);
2652
  if (!table_share->ha_part_data)
2653 2654
  {
    /* currently only needed for auto_increment */
Mattias Jonsson's avatar
Mattias Jonsson committed
2655
    table_share->ha_part_data= (HA_DATA_PARTITION*)
2656 2657
                                   alloc_root(&table_share->mem_root,
                                              sizeof(HA_DATA_PARTITION));
Mattias Jonsson's avatar
Mattias Jonsson committed
2658
    if (!table_share->ha_part_data)
2659
    {
Mattias Jonsson's avatar
Mattias Jonsson committed
2660
      if (is_not_tmp_table)
2661
        mysql_mutex_unlock(&table_share->LOCK_ha_data);
2662 2663
      goto err_handler;
    }
Mattias Jonsson's avatar
Mattias Jonsson committed
2664 2665 2666
    DBUG_PRINT("info", ("table_share->ha_part_data 0x%p",
                        table_share->ha_part_data));
    bzero(table_share->ha_part_data, sizeof(HA_DATA_PARTITION));
2667
    table_share->ha_part_data_destroy= ha_data_partition_destroy;
Mattias Jonsson's avatar
Mattias Jonsson committed
2668 2669
    mysql_mutex_init(key_PARTITION_LOCK_auto_inc,
                     &table_share->ha_part_data->LOCK_auto_inc,
2670
                     MY_MUTEX_INIT_FAST);
2671 2672
  }
  if (is_not_tmp_table)
2673
    mysql_mutex_unlock(&table_share->LOCK_ha_data);
2674 2675 2676 2677 2678 2679
  /*
    Some handlers update statistics as part of the open call. This will in
    some cases corrupt the statistics of the partition handler and thus
    to ensure we have correct statistics we call info from open after
    calling open on all individual handlers.
  */
2680
  m_handler_status= handler_opened;
2681 2682 2683 2684 2685
  if (m_part_info->part_expr)
    m_part_func_monotonicity_info=
                            m_part_info->part_expr->get_monotonicity_info();
  else if (m_part_info->list_of_part_fields)
    m_part_func_monotonicity_info= MONOTONIC_STRICT_INCREASING;
2686 2687 2688 2689
  info(HA_STATUS_VARIABLE | HA_STATUS_CONST);
  DBUG_RETURN(0);

err_handler:
2690
  DEBUG_SYNC(ha_thd(), "partition_open_error");
2691 2692
  while (file-- != m_file)
    (*file)->close();
2693
  bitmap_free(&m_bulk_insert_started);
2694 2695
  if (!is_clone)
    bitmap_free(&(m_part_info->used_partitions));
unknown's avatar
unknown committed
2696

2697 2698 2699
  DBUG_RETURN(error);
}

2700 2701
handler *ha_partition::clone(MEM_ROOT *mem_root)
{
unknown's avatar
unknown committed
2702 2703
  handler *new_handler= get_new_handler(table->s, mem_root,
                                        table->s->db_type());
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713
  ((ha_partition*)new_handler)->m_part_info= m_part_info;
  ((ha_partition*)new_handler)->is_clone= TRUE;
  if (new_handler && !new_handler->ha_open(table,
                                           table->s->normalized_path.str,
                                           table->db_stat,
                                           HA_OPEN_IGNORE_IF_LOCKED))
    return new_handler;
  return NULL;
}

unknown's avatar
unknown committed
2714

2715
/*
unknown's avatar
unknown committed
2716
  Close handler object
2717

unknown's avatar
unknown committed
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
  SYNOPSIS
    close()

  RETURN VALUE
    >0                   Error code
    0                    Success

  DESCRIPTION
    Called from sql_base.cc, sql_select.cc, and table.cc.
    In sql_select.cc it is only used to close up temporary tables or during
    the process where a temporary table is converted over to being a
    myisam table.
    For sql_base.cc look at close_data_tables().
2731 2732 2733 2734
*/

int ha_partition::close(void)
{
unknown's avatar
unknown committed
2735
  bool first= TRUE;
unknown's avatar
unknown committed
2736
  handler **file;
2737
  DBUG_ENTER("ha_partition::close");
2738

2739
  DBUG_ASSERT(table->s == table_share);
unknown's avatar
unknown committed
2740
  delete_queue(&m_queue);
2741
  bitmap_free(&m_bulk_insert_started);
2742 2743
  if (!is_clone)
    bitmap_free(&(m_part_info->used_partitions));
2744
  file= m_file;
unknown's avatar
unknown committed
2745 2746

repeat:
2747 2748 2749 2750
  do
  {
    (*file)->close();
  } while (*(++file));
unknown's avatar
unknown committed
2751

unknown's avatar
unknown committed
2752 2753 2754 2755 2756 2757
  if (first && m_added_file && m_added_file[0])
  {
    file= m_added_file;
    first= FALSE;
    goto repeat;
  }
unknown's avatar
unknown committed
2758

2759
  m_handler_status= handler_closed;
2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772
  DBUG_RETURN(0);
}

/****************************************************************************
                MODULE start/end statement
****************************************************************************/
/*
  A number of methods to define various constants for the handler. In
  the case of the partition handler we need to use some max and min
  of the underlying handlers in most cases.
*/

/*
unknown's avatar
unknown committed
2773
  Set external locks on table
2774

unknown's avatar
unknown committed
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
  SYNOPSIS
    external_lock()
    thd                    Thread object
    lock_type              Type of external lock

  RETURN VALUE
    >0                   Error code
    0                    Success

  DESCRIPTION
    First you should go read the section "locking functions for mysql" in
    lock.cc to understand this.
    This create a lock on the table. If you are implementing a storage engine
    that can handle transactions look at ha_berkeley.cc to see how you will
    want to go about doing this. Otherwise you should consider calling
    flock() here.
    Originally this method was used to set locks on file level to enable
    several MySQL Servers to work on the same data. For transactional
    engines it has been "abused" to also mean start and end of statements
    to enable proper rollback of statements and transactions. When LOCK
    TABLES has been issued the start_stmt method takes over the role of
    indicating start of statement but in this case there is no end of
    statement indicator(?).

    Called from lock.cc by lock_external() and unlock_external(). Also called
    from sql_table.cc by copy_data_between_tables().
2801 2802 2803 2804
*/

int ha_partition::external_lock(THD *thd, int lock_type)
{
unknown's avatar
unknown committed
2805
  bool first= TRUE;
2806 2807 2808
  uint error;
  handler **file;
  DBUG_ENTER("ha_partition::external_lock");
unknown's avatar
unknown committed
2809

2810
  DBUG_ASSERT(!auto_increment_lock && !auto_increment_safe_stmt_log_lock);
2811
  file= m_file;
unknown's avatar
unknown committed
2812 2813 2814
  m_lock_type= lock_type;

repeat:
2815 2816
  do
  {
unknown's avatar
unknown committed
2817
    DBUG_PRINT("info", ("external_lock(thd, %d) iteration %d",
unknown's avatar
unknown committed
2818
                        lock_type, (int) (file - m_file)));
2819
    if ((error= (*file)->ha_external_lock(thd, lock_type)))
2820
    {
unknown's avatar
unknown committed
2821 2822
      if (F_UNLCK != lock_type)
        goto err_handler;
2823 2824
    }
  } while (*(++file));
unknown's avatar
unknown committed
2825

unknown's avatar
unknown committed
2826 2827 2828 2829 2830 2831 2832
  if (first && m_added_file && m_added_file[0])
  {
    DBUG_ASSERT(lock_type == F_UNLCK);
    file= m_added_file;
    first= FALSE;
    goto repeat;
  }
2833 2834 2835 2836
  DBUG_RETURN(0);

err_handler:
  while (file-- != m_file)
unknown's avatar
unknown committed
2837
  {
2838
    (*file)->ha_external_lock(thd, F_UNLCK);
unknown's avatar
unknown committed
2839
  }
2840 2841 2842 2843 2844
  DBUG_RETURN(error);
}


/*
unknown's avatar
unknown committed
2845
  Get the lock(s) for the table and perform conversion of locks if needed
2846

unknown's avatar
unknown committed
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
  SYNOPSIS
    store_lock()
    thd                   Thread object
    to                    Lock object array
    lock_type             Table lock type

  RETURN VALUE
    >0                   Error code
    0                    Success

  DESCRIPTION
    The idea with handler::store_lock() is the following:

    The statement decided which locks we should need for the table
    for updates/deletes/inserts we get WRITE locks, for SELECT... we get
    read locks.

    Before adding the lock into the table lock handler (see thr_lock.c)
    mysqld calls store lock with the requested locks.  Store lock can now
    modify a write lock to a read lock (or some other lock), ignore the
    lock (if we don't want to use MySQL table locks at all) or add locks
    for many tables (like we do when we are using a MERGE handler).

    Berkeley DB for partition  changes all WRITE locks to TL_WRITE_ALLOW_WRITE
    (which signals that we are doing WRITES, but we are still allowing other
    reader's and writer's.

    When releasing locks, store_lock() is also called. In this case one
    usually doesn't have to do anything.

    store_lock is called when holding a global mutex to ensure that only
    one thread at a time changes the locking information of tables.

    In some exceptional cases MySQL may send a request for a TL_IGNORE;
    This means that we are requesting the same lock as last time and this
    should also be ignored. (This may happen when someone does a flush
    table when we have opened a part of the tables, in which case mysqld
    closes and reopens the tables and tries to get the same locks as last
    time).  In the future we will probably try to remove this.

    Called from lock.cc by get_lock_data().
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898
*/

THR_LOCK_DATA **ha_partition::store_lock(THD *thd,
					 THR_LOCK_DATA **to,
					 enum thr_lock_type lock_type)
{
  handler **file;
  DBUG_ENTER("ha_partition::store_lock");
  file= m_file;
  do
  {
unknown's avatar
unknown committed
2899
    DBUG_PRINT("info", ("store lock %d iteration", (int) (file - m_file)));
2900 2901 2902 2903 2904
    to= (*file)->store_lock(thd, to, lock_type);
  } while (*(++file));
  DBUG_RETURN(to);
}

unknown's avatar
unknown committed
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920
/*
  Start a statement when table is locked

  SYNOPSIS
    start_stmt()
    thd                  Thread object
    lock_type            Type of external lock

  RETURN VALUE
    >0                   Error code
    0                    Success

  DESCRIPTION
    This method is called instead of external lock when the table is locked
    before the statement is executed.
*/
2921

unknown's avatar
merge  
unknown committed
2922
int ha_partition::start_stmt(THD *thd, thr_lock_type lock_type)
2923 2924 2925 2926
{
  int error= 0;
  handler **file;
  DBUG_ENTER("ha_partition::start_stmt");
unknown's avatar
unknown committed
2927

2928 2929 2930
  file= m_file;
  do
  {
unknown's avatar
merge  
unknown committed
2931
    if ((error= (*file)->start_stmt(thd, lock_type)))
2932 2933 2934 2935 2936 2937 2938
      break;
  } while (*(++file));
  DBUG_RETURN(error);
}


/*
unknown's avatar
unknown committed
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951
  Get number of lock objects returned in store_lock

  SYNOPSIS
    lock_count()

  RETURN VALUE
    Number of locks returned in call to store_lock

  DESCRIPTION
    Returns the number of store locks needed in call to store lock.
    We return number of partitions since we call store_lock on each
    underlying handler. Assists the above functions in allocating
    sufficient space for lock structures.
2952 2953 2954 2955 2956
*/

uint ha_partition::lock_count() const
{
  DBUG_ENTER("ha_partition::lock_count");
2957 2958
  DBUG_PRINT("info", ("m_num_locks %d", m_num_locks));
  DBUG_RETURN(m_num_locks);
2959 2960 2961 2962
}


/*
unknown's avatar
unknown committed
2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973
  Unlock last accessed row

  SYNOPSIS
    unlock_row()

  RETURN VALUE
    NONE

  DESCRIPTION
    Record currently processed was not in the result set of the statement
    and is thus unlocked. Used for UPDATE and DELETE queries.
2974 2975 2976 2977
*/

void ha_partition::unlock_row()
{
2978
  DBUG_ENTER("ha_partition::unlock_row");
2979
  m_file[m_last_part]->unlock_row();
2980 2981 2982
  DBUG_VOID_RETURN;
}

2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012
/**
  Check if semi consistent read was used

  SYNOPSIS
    was_semi_consistent_read()

  RETURN VALUE
    TRUE   Previous read was a semi consistent read
    FALSE  Previous read was not a semi consistent read

  DESCRIPTION
    See handler.h:
    In an UPDATE or DELETE, if the row under the cursor was locked by another
    transaction, and the engine used an optimistic read of the last
    committed row value under the cursor, then the engine returns 1 from this
    function. MySQL must NOT try to update this optimistic value. If the
    optimistic value does not match the WHERE condition, MySQL can decide to
    skip over this row. Currently only works for InnoDB. This can be used to
    avoid unnecessary lock waits.

    If this method returns nonzero, it will also signal the storage
    engine that the next read will be a locking re-read of the row.
*/
bool ha_partition::was_semi_consistent_read()
{
  DBUG_ENTER("ha_partition::was_semi_consistent_read");
  DBUG_ASSERT(m_last_part < m_tot_parts &&
              bitmap_is_set(&(m_part_info->used_partitions), m_last_part));
  DBUG_RETURN(m_file[m_last_part]->was_semi_consistent_read());
}
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043

/**
  Use semi consistent read if possible

  SYNOPSIS
    try_semi_consistent_read()
    yes   Turn on semi consistent read

  RETURN VALUE
    NONE

  DESCRIPTION
    See handler.h:
    Tell the engine whether it should avoid unnecessary lock waits.
    If yes, in an UPDATE or DELETE, if the row under the cursor was locked
    by another transaction, the engine may try an optimistic read of
    the last committed row value under the cursor.
    Note: prune_partitions are already called before this call, so using
    pruning is OK.
*/
void ha_partition::try_semi_consistent_read(bool yes)
{
  handler **file;
  DBUG_ENTER("ha_partition::try_semi_consistent_read");
  
  for (file= m_file; *file; file++)
  {
    if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file)))
      (*file)->try_semi_consistent_read(yes);
  }
  DBUG_VOID_RETURN;
3044 3045 3046 3047 3048 3049 3050 3051
}


/****************************************************************************
                MODULE change record
****************************************************************************/

/*
unknown's avatar
unknown committed
3052
  Insert a row to the table
3053

unknown's avatar
unknown committed
3054 3055 3056
  SYNOPSIS
    write_row()
    buf                        The row in MySQL Row Format
3057

unknown's avatar
unknown committed
3058 3059 3060 3061 3062 3063 3064
  RETURN VALUE
    >0                         Error code
    0                          Success

  DESCRIPTION
    write_row() inserts a row. buf() is a byte array of data, normally
    record[0].
3065

unknown's avatar
unknown committed
3066 3067
    You can use the field information to extract the data from the native byte
    array type.
3068

unknown's avatar
unknown committed
3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
    Example of this would be:
    for (Field **field=table->field ; *field ; field++)
    {
      ...
    }

    See ha_tina.cc for a variant of extracting all of the data as strings.
    ha_berkeley.cc has a variant of how to store it intact by "packing" it
    for ha_berkeley's own native storage type.

    Called from item_sum.cc, item_sum.cc, sql_acl.cc, sql_insert.cc,
    sql_insert.cc, sql_select.cc, sql_table.cc, sql_udf.cc, and sql_update.cc.
3081

unknown's avatar
unknown committed
3082
    ADDITIONAL INFO:
3083

3084 3085
    We have to set timestamp fields and auto_increment fields, because those
    may be used in determining which partition the row should be written to.
3086 3087
*/

3088
int ha_partition::write_row(uchar * buf)
3089 3090 3091
{
  uint32 part_id;
  int error;
3092
  longlong func_value;
3093
  bool have_auto_increment= table->next_number_field && buf == table->record[0];
3094
  my_bitmap_map *old_map;
3095
  THD *thd= ha_thd();
3096 3097 3098
  timestamp_auto_set_type saved_timestamp_type= table->timestamp_field_type;
  ulong saved_sql_mode= thd->variables.sql_mode;
  bool saved_auto_inc_field_not_null= table->auto_increment_field_not_null;
3099
#ifdef NOT_NEEDED
3100
  uchar *rec0= m_rec0;
3101 3102 3103 3104
#endif
  DBUG_ENTER("ha_partition::write_row");
  DBUG_ASSERT(buf == m_rec0);

3105 3106 3107
  /* If we have a timestamp column, update it to the current time */
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_INSERT)
    table->timestamp_field->set_time();
3108
  table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
3109 3110 3111 3112 3113

  /*
    If we have an auto_increment column and we are writing a changed row
    or a new row, then update the auto_increment value in the record.
  */
3114
  if (have_auto_increment)
3115
  {
Mattias Jonsson's avatar
Mattias Jonsson committed
3116 3117
    if (!table_share->ha_part_data->auto_inc_initialized &&
        !table_share->next_number_keypart)
3118 3119
    {
      /*
3120 3121
        If auto_increment in table_share is not initialized, start by
        initializing it.
3122
      */
3123
      info(HA_STATUS_AUTO);
3124
    }
unknown's avatar
unknown committed
3125 3126 3127 3128 3129 3130 3131 3132
    error= update_auto_increment();

    /*
      If we have failed to set the auto-increment value for this row,
      it is highly likely that we will not be able to insert it into
      the correct partition. We must check and fail if neccessary.
    */
    if (error)
3133
      goto exit;
3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149

    /*
      Don't allow generation of auto_increment value the partitions handler.
      If a partitions handler would change the value, then it might not
      match the partition any longer.
      This can occur if 'SET INSERT_ID = 0; INSERT (NULL)',
      So allow this by adding 'MODE_NO_AUTO_VALUE_ON_ZERO' to sql_mode.
      The partitions handler::next_insert_id must always be 0. Otherwise
      we need to forward release_auto_increment, or reset it for all
      partitions.
    */
    if (table->next_number_field->val_int() == 0)
    {
      table->auto_increment_field_not_null= TRUE;
      thd->variables.sql_mode|= MODE_NO_AUTO_VALUE_ON_ZERO;
    }
3150
  }
3151

3152
  old_map= dbug_tmp_use_all_columns(table, table->read_set);
3153 3154 3155
#ifdef NOT_NEEDED
  if (likely(buf == rec0))
#endif
3156 3157
    error= m_part_info->get_partition_id(m_part_info, &part_id,
                                         &func_value);
3158 3159 3160 3161
#ifdef NOT_NEEDED
  else
  {
    set_field_ptr(m_part_field_array, buf, rec0);
3162 3163
    error= m_part_info->get_partition_id(m_part_info, &part_id,
                                         &func_value);
3164 3165 3166
    set_field_ptr(m_part_field_array, rec0, buf);
  }
#endif
3167
  dbug_tmp_restore_column_map(table->read_set, old_map);
3168
  if (unlikely(error))
3169 3170
  {
    m_part_info->err_value= func_value;
3171
    goto exit;
3172
  }
3173 3174
  m_last_part= part_id;
  DBUG_PRINT("info", ("Insert in partition %d", part_id));
3175
  start_part_bulk_insert(thd, part_id);
3176

3177 3178
  tmp_disable_binlog(thd); /* Do not replicate the low-level changes. */
  error= m_file[part_id]->ha_write_row(buf);
3179
  if (have_auto_increment && !table->s->next_number_keypart)
3180
    set_auto_increment_if_higher(table->next_number_field);
3181
  reenable_binlog(thd);
3182
exit:
3183 3184 3185
  thd->variables.sql_mode= saved_sql_mode;
  table->auto_increment_field_not_null= saved_auto_inc_field_not_null;
  table->timestamp_field_type= saved_timestamp_type;
3186
  DBUG_RETURN(error);
3187 3188 3189 3190
}


/*
unknown's avatar
unknown committed
3191
  Update an existing row
3192

unknown's avatar
unknown committed
3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211
  SYNOPSIS
    update_row()
    old_data                 Old record in MySQL Row Format
    new_data                 New record in MySQL Row Format

  RETURN VALUE
    >0                         Error code
    0                          Success

  DESCRIPTION
    Yes, update_row() does what you expect, it updates a row. old_data will
    have the previous row record in it, while new_data will have the newest
    data in it.
    Keep in mind that the server can do updates based on ordering if an
    ORDER BY clause was used. Consecutive ordering is not guarenteed.

    Called from sql_select.cc, sql_acl.cc, sql_update.cc, and sql_insert.cc.
    new_data is always record[0]
    old_data is normally record[1] but may be anything
3212 3213
*/

3214
int ha_partition::update_row(const uchar *old_data, uchar *new_data)
3215
{
3216
  THD *thd= ha_thd();
3217
  uint32 new_part_id, old_part_id;
3218
  int error= 0;
3219
  longlong func_value;
3220
  timestamp_auto_set_type orig_timestamp_type= table->timestamp_field_type;
3221 3222
  DBUG_ENTER("ha_partition::update_row");

3223 3224 3225 3226 3227 3228 3229
  /*
    We need to set timestamp field once before we calculate
    the partition. Then we disable timestamp calculations
    inside m_file[*]->update_row() methods
  */
  if (orig_timestamp_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
    table->timestamp_field->set_time();
3230
  table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
3231

3232
  if ((error= get_parts_for_update(old_data, new_data, table->record[0],
3233 3234
                                   m_part_info, &old_part_id, &new_part_id,
                                   &func_value)))
3235
  {
3236
    m_part_info->err_value= func_value;
3237
    goto exit;
3238 3239 3240
  }

  m_last_part= new_part_id;
3241
  start_part_bulk_insert(thd, new_part_id);
3242 3243 3244
  if (new_part_id == old_part_id)
  {
    DBUG_PRINT("info", ("Update in partition %d", new_part_id));
3245 3246 3247
    tmp_disable_binlog(thd); /* Do not replicate the low-level changes. */
    error= m_file[new_part_id]->ha_update_row(old_data, new_data);
    reenable_binlog(thd);
3248
    goto exit;
3249 3250 3251
  }
  else
  {
3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263
    Field *saved_next_number_field= table->next_number_field;
    /*
      Don't allow generation of auto_increment value for update.
      table->next_number_field is never set on UPDATE.
      But is set for INSERT ... ON DUPLICATE KEY UPDATE,
      and since update_row() does not generate or update an auto_inc value,
      we cannot have next_number_field set when moving a row
      to another partition with write_row(), since that could
      generate/update the auto_inc value.
      This gives the same behavior for partitioned vs non partitioned tables.
    */
    table->next_number_field= NULL;
3264 3265
    DBUG_PRINT("info", ("Update from partition %d to partition %d",
			old_part_id, new_part_id));
3266 3267 3268
    tmp_disable_binlog(thd); /* Do not replicate the low-level changes. */
    error= m_file[new_part_id]->ha_write_row(new_data);
    reenable_binlog(thd);
3269
    table->next_number_field= saved_next_number_field;
3270
    if (error)
3271
      goto exit;
3272 3273 3274 3275 3276

    tmp_disable_binlog(thd); /* Do not replicate the low-level changes. */
    error= m_file[old_part_id]->ha_delete_row(old_data);
    reenable_binlog(thd);
    if (error)
3277 3278 3279 3280
    {
#ifdef IN_THE_FUTURE
      (void) m_file[new_part_id]->delete_last_inserted_row(new_data);
#endif
3281
      goto exit;
3282 3283
    }
  }
3284 3285

exit:
3286 3287
  /*
    if updating an auto_increment column, update
3288
    table_share->ha_part_data->next_auto_inc_val if needed.
3289 3290 3291 3292 3293 3294 3295 3296
    (not to be used if auto_increment on secondary field in a multi-column
    index)
    mysql_update does not set table->next_number_field, so we use
    table->found_next_number_field instead.
  */
  if (table->found_next_number_field && new_data == table->record[0] &&
      !table->s->next_number_keypart)
  {
Mattias Jonsson's avatar
Mattias Jonsson committed
3297
    if (!table_share->ha_part_data->auto_inc_initialized)
3298
      info(HA_STATUS_AUTO);
3299
    set_auto_increment_if_higher(table->found_next_number_field);
3300
  }
3301 3302
  table->timestamp_field_type= orig_timestamp_type;
  DBUG_RETURN(error);
3303 3304 3305 3306
}


/*
unknown's avatar
unknown committed
3307
  Remove an existing row
3308

unknown's avatar
unknown committed
3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331
  SYNOPSIS
    delete_row
    buf                      Deleted row in MySQL Row Format

  RETURN VALUE
    >0                       Error Code
    0                        Success

  DESCRIPTION
    This will delete a row. buf will contain a copy of the row to be deleted.
    The server will call this right after the current row has been read
    (from either a previous rnd_xxx() or index_xxx() call).
    If you keep a pointer to the last row or can access a primary key it will
    make doing the deletion quite a bit easier.
    Keep in mind that the server does no guarentee consecutive deletions.
    ORDER BY clauses can be used.

    Called in sql_acl.cc and sql_udf.cc to manage internal table information.
    Called in sql_delete.cc, sql_insert.cc, and sql_select.cc. In sql_select
    it is used for removing duplicates while in insert it is used for REPLACE
    calls.

    buf is either record[0] or record[1]
3332 3333
*/

3334
int ha_partition::delete_row(const uchar *buf)
3335 3336 3337
{
  uint32 part_id;
  int error;
3338
  THD *thd= ha_thd();
3339 3340 3341 3342 3343 3344 3345
  DBUG_ENTER("ha_partition::delete_row");

  if ((error= get_part_for_delete(buf, m_rec0, m_part_info, &part_id)))
  {
    DBUG_RETURN(error);
  }
  m_last_part= part_id;
3346 3347 3348 3349
  tmp_disable_binlog(thd);
  error= m_file[part_id]->ha_delete_row(buf);
  reenable_binlog(thd);
  DBUG_RETURN(error);
3350 3351 3352 3353
}


/*
unknown's avatar
unknown committed
3354
  Delete all rows in a table
3355

unknown's avatar
unknown committed
3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372
  SYNOPSIS
    delete_all_rows()

  RETURN VALUE
    >0                       Error Code
    0                        Success

  DESCRIPTION
    Used to delete all rows in a table. Both for cases of truncate and
    for cases where the optimizer realizes that all rows will be
    removed as a result of a SQL statement.

    Called from item_sum.cc by Item_func_group_concat::clear(),
    Item_sum_count_distinct::clear(), and Item_func_group_concat::clear().
    Called from sql_delete.cc by mysql_delete().
    Called from sql_select.cc by JOIN::reinit().
    Called from sql_union.cc by st_select_lex_unit::exec().
3373 3374 3375 3376 3377 3378 3379
*/

int ha_partition::delete_all_rows()
{
  int error;
  handler **file;
  DBUG_ENTER("ha_partition::delete_all_rows");
unknown's avatar
unknown committed
3380

3381 3382
  file= m_file;
  do
3383
  {
3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411
    if ((error= (*file)->ha_delete_all_rows()))
      DBUG_RETURN(error);
  } while (*(++file));
  DBUG_RETURN(0);
}


/**
  Manually truncate the table.

  @retval  0    Success.
  @retval  > 0  Error code.
*/

int ha_partition::truncate()
{
  int error;
  handler **file;
  DBUG_ENTER("ha_partition::truncate");

  /*
    TRUNCATE also means resetting auto_increment. Hence, reset
    it so that it will be initialized again at the next use.
  */
  lock_auto_increment();
  table_share->ha_part_data->next_auto_inc_val= 0;
  table_share->ha_part_data->auto_inc_initialized= FALSE;
  unlock_auto_increment();
3412

3413 3414 3415
  file= m_file;
  do
  {
3416
    if ((error= (*file)->ha_truncate()))
3417 3418 3419 3420 3421
      DBUG_RETURN(error);
  } while (*(++file));
  DBUG_RETURN(0);
}

unknown's avatar
unknown committed
3422

3423 3424 3425 3426 3427 3428 3429 3430
/**
  Truncate a set of specific partitions.

  @remark Auto increment value will be truncated in that partition as well!

  ALTER TABLE t TRUNCATE PARTITION ...
*/

3431
int ha_partition::truncate_partition(Alter_info *alter_info, bool *binlog_stmt)
3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442
{
  int error= 0;
  List_iterator<partition_element> part_it(m_part_info->partitions);
  uint num_parts= m_part_info->num_parts;
  uint num_subparts= m_part_info->num_subparts;
  uint i= 0;
  uint num_parts_set= alter_info->partition_names.elements;
  uint num_parts_found= set_part_state(alter_info, m_part_info,
                                        PART_ADMIN);
  DBUG_ENTER("ha_partition::truncate_partition");

3443
  /* Only binlog when it starts any call to the partitions handlers */
3444
  *binlog_stmt= false;
3445

3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458
  /*
    TRUNCATE also means resetting auto_increment. Hence, reset
    it so that it will be initialized again at the next use.
  */
  lock_auto_increment();
  table_share->ha_part_data->next_auto_inc_val= 0;
  table_share->ha_part_data->auto_inc_initialized= FALSE;
  unlock_auto_increment();

  if (num_parts_set != num_parts_found &&
      (!(alter_info->flags & ALTER_ALL_PARTITION)))
    DBUG_RETURN(HA_ERR_NO_PARTITION_FOUND);

3459
  *binlog_stmt= true;
3460

3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494
  do
  {
    partition_element *part_elem= part_it++;
    if (part_elem->part_state == PART_ADMIN)
    {
      if (m_is_sub_partitioned)
      {
        List_iterator<partition_element>
                                    subpart_it(part_elem->subpartitions);
        partition_element *sub_elem;
        uint j= 0, part;
        do
        {
          sub_elem= subpart_it++;
          part= i * num_subparts + j;
          DBUG_PRINT("info", ("truncate subpartition %u (%s)",
                              part, sub_elem->partition_name));
          if ((error= m_file[part]->ha_truncate()))
            break;
        } while (++j < num_subparts);
      }
      else
      {
        DBUG_PRINT("info", ("truncate partition %u (%s)", i,
                            part_elem->partition_name));
        error= m_file[i]->ha_truncate();
      }
      part_elem->part_state= PART_NORMAL;
    }
  } while (!error && (++i < num_parts));
  DBUG_RETURN(error);
}


3495
/*
unknown's avatar
unknown committed
3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506
  Start a large batch of insert rows

  SYNOPSIS
    start_bulk_insert()
    rows                  Number of rows to insert

  RETURN VALUE
    NONE

  DESCRIPTION
    rows == 0 means we will probably insert many rows
3507 3508 3509 3510
*/
void ha_partition::start_bulk_insert(ha_rows rows)
{
  DBUG_ENTER("ha_partition::start_bulk_insert");
unknown's avatar
unknown committed
3511

3512 3513 3514 3515
  m_bulk_inserted_rows= 0;
  bitmap_clear_all(&m_bulk_insert_started);
  /* use the last bit for marking if bulk_insert_started was called */
  bitmap_set_bit(&m_bulk_insert_started, m_tot_parts);
3516 3517 3518 3519
  DBUG_VOID_RETURN;
}


3520 3521 3522 3523
/*
  Check if start_bulk_insert has been called for this partition,
  if not, call it and mark it called
*/
3524
void ha_partition::start_part_bulk_insert(THD *thd, uint part_id)
3525
{
3526
  long old_buffer_size;
3527 3528 3529
  if (!bitmap_is_set(&m_bulk_insert_started, part_id) &&
      bitmap_is_set(&m_bulk_insert_started, m_tot_parts))
  {
3530 3531 3532
    old_buffer_size= thd->variables.read_buff_size;
    /* Update read_buffer_size for this partition */
    thd->variables.read_buff_size= estimate_read_buffer_size(old_buffer_size);
3533 3534
    m_file[part_id]->ha_start_bulk_insert(guess_bulk_insert_rows());
    bitmap_set_bit(&m_bulk_insert_started, part_id);
3535
    thd->variables.read_buff_size= old_buffer_size;
3536 3537 3538 3539
  }
  m_bulk_inserted_rows++;
}

3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580
/*
  Estimate the read buffer size for each partition.
  SYNOPSIS
    ha_partition::estimate_read_buffer_size()
    original_size  read buffer size originally set for the server
  RETURN VALUE
    estimated buffer size.
  DESCRIPTION
    If the estimated number of rows to insert is less than 10 (but not 0)
    the new buffer size is same as original buffer size.
    In case of first partition of when partition function is monotonic 
    new buffer size is same as the original buffer size.
    For rest of the partition total buffer of 10*original_size is divided 
    equally if number of partition is more than 10 other wise each partition
    will be allowed to use original buffer size.
*/
long ha_partition::estimate_read_buffer_size(long original_size)
{
  /*
    If number of rows to insert is less than 10, but not 0,
    return original buffer size.
  */
  if (estimation_rows_to_insert && (estimation_rows_to_insert < 10))
    return (original_size);
  /*
    If first insert/partition and monotonic partition function,
    allow using buffer size originally set.
   */
  if (!m_bulk_inserted_rows &&
      m_part_func_monotonicity_info != NON_MONOTONIC &&
      m_tot_parts > 1)
    return original_size;
  /*
    Allow total buffer used in all partition to go up to 10*read_buffer_size.
    11*read_buffer_size in case of monotonic partition function.
  */

  if (m_tot_parts < 10)
      return original_size;
  return (original_size * 10 / m_tot_parts);
}
3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612

/*
  Try to predict the number of inserts into this partition.

  If less than 10 rows (including 0 which means Unknown)
    just give that as a guess
  If monotonic partitioning function was used
    guess that 50 % of the inserts goes to the first partition
  For all other cases, guess on equal distribution between the partitions
*/ 
ha_rows ha_partition::guess_bulk_insert_rows()
{
  DBUG_ENTER("guess_bulk_insert_rows");

  if (estimation_rows_to_insert < 10)
    DBUG_RETURN(estimation_rows_to_insert);

  /* If first insert/partition and monotonic partition function, guess 50%.  */
  if (!m_bulk_inserted_rows && 
      m_part_func_monotonicity_info != NON_MONOTONIC &&
      m_tot_parts > 1)
    DBUG_RETURN(estimation_rows_to_insert / 2);

  /* Else guess on equal distribution (+1 is to avoid returning 0/Unknown) */
  if (m_bulk_inserted_rows < estimation_rows_to_insert)
    DBUG_RETURN(((estimation_rows_to_insert - m_bulk_inserted_rows)
                / m_tot_parts) + 1);
  /* The estimation was wrong, must say 'Unknown' */
  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
3613 3614 3615 3616 3617 3618 3619 3620 3621
/*
  Finish a large batch of insert rows

  SYNOPSIS
    end_bulk_insert()

  RETURN VALUE
    >0                      Error code
    0                       Success
3622 3623 3624 3625

  Note: end_bulk_insert can be called without start_bulk_insert
        being called, see bug¤44108.

unknown's avatar
unknown committed
3626 3627
*/

3628 3629 3630
int ha_partition::end_bulk_insert()
{
  int error= 0;
3631
  uint i;
3632 3633
  DBUG_ENTER("ha_partition::end_bulk_insert");

3634 3635 3636 3637
  if (!bitmap_is_set(&m_bulk_insert_started, m_tot_parts))
    DBUG_RETURN(error);

  for (i= 0; i < m_tot_parts; i++)
3638 3639
  {
    int tmp;
3640 3641
    if (bitmap_is_set(&m_bulk_insert_started, i) &&
        (tmp= m_file[i]->ha_end_bulk_insert()))
3642
      error= tmp;
3643 3644
  }
  bitmap_clear_all(&m_bulk_insert_started);
3645 3646 3647
  DBUG_RETURN(error);
}

unknown's avatar
unknown committed
3648

3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659
/****************************************************************************
                MODULE full table scan
****************************************************************************/
/*
  Initialize engine for random reads

  SYNOPSIS
    ha_partition::rnd_init()
    scan	0  Initialize for random reads through rnd_pos()
		1  Initialize for random scan through rnd_next()

unknown's avatar
unknown committed
3660 3661 3662
  RETURN VALUE
    >0          Error code
    0           Success
3663

unknown's avatar
unknown committed
3664 3665 3666
  DESCRIPTION 
    rnd_init() is called when the server wants the storage engine to do a
    table scan or when the server wants to access data through rnd_pos.
3667

unknown's avatar
unknown committed
3668 3669 3670 3671 3672 3673 3674 3675
    When scan is used we will scan one handler partition at a time.
    When preparing for rnd_pos we will init all handler partitions.
    No extra cache handling is needed when scannning is not performed.

    Before initialising we will call rnd_end to ensure that we clean up from
    any previous incarnation of a table scan.
    Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc,
    sql_table.cc, and sql_update.cc.
3676 3677 3678 3679 3680
*/

int ha_partition::rnd_init(bool scan)
{
  int error;
unknown's avatar
unknown committed
3681 3682
  uint i= 0;
  uint32 part_id;
3683 3684
  DBUG_ENTER("ha_partition::rnd_init");

3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712
  /*
    For operations that may need to change data, we may need to extend
    read_set.
  */
  if (m_lock_type == F_WRLCK)
  {
    /*
      If write_set contains any of the fields used in partition and
      subpartition expression, we need to set all bits in read_set because
      the row may need to be inserted in a different [sub]partition. In
      other words update_row() can be converted into write_row(), which
      requires a complete record.
    */
    if (bitmap_is_overlapping(&m_part_info->full_part_field_set,
                              table->write_set))
      bitmap_set_all(table->read_set);
    else
    {
      /*
        Some handlers only read fields as specified by the bitmap for the
        read set. For partitioned handlers we always require that the
        fields of the partition functions are read such that we can
        calculate the partition id to place updated and deleted records.
      */
      bitmap_union(table->read_set, &m_part_info->full_part_field_set);
    }
  }

unknown's avatar
unknown committed
3713
  /* Now we see what the index of our first important partition is */
unknown's avatar
unknown committed
3714 3715
  DBUG_PRINT("info", ("m_part_info->used_partitions: 0x%lx",
                      (long) m_part_info->used_partitions.bitmap));
unknown's avatar
unknown committed
3716 3717 3718 3719
  part_id= bitmap_get_first_set(&(m_part_info->used_partitions));
  DBUG_PRINT("info", ("m_part_spec.start_part %d", part_id));

  if (MY_BIT_NONE == part_id)
3720 3721
  {
    error= 0;
unknown's avatar
unknown committed
3722
    goto err1;
3723
  }
unknown's avatar
unknown committed
3724 3725 3726 3727 3728 3729

  /*
    We have a partition and we are scanning with rnd_next
    so we bump our cache
  */
  DBUG_PRINT("info", ("rnd_init on partition %d", part_id));
3730 3731 3732 3733 3734 3735 3736
  if (scan)
  {
    /*
      rnd_end() is needed for partitioning to reset internal data if scan
      is already in use
    */
    rnd_end();
unknown's avatar
unknown committed
3737 3738 3739 3740 3741 3742 3743
    late_extra_cache(part_id);
    if ((error= m_file[part_id]->ha_rnd_init(scan)))
      goto err;
  }
  else
  {
    for (i= part_id; i < m_tot_parts; i++)
3744
    {
unknown's avatar
unknown committed
3745 3746 3747 3748 3749
      if (bitmap_is_set(&(m_part_info->used_partitions), i))
      {
        if ((error= m_file[i]->ha_rnd_init(scan)))
          goto err;
      }
3750 3751
    }
  }
unknown's avatar
unknown committed
3752 3753 3754 3755
  m_scan_value= scan;
  m_part_spec.start_part= part_id;
  m_part_spec.end_part= m_tot_parts - 1;
  DBUG_PRINT("info", ("m_scan_value=%d", m_scan_value));
3756 3757 3758
  DBUG_RETURN(0);

err:
unknown's avatar
unknown committed
3759 3760 3761
  while ((int)--i >= (int)part_id)
  {
    if (bitmap_is_set(&(m_part_info->used_partitions), i))
3762
      m_file[i]->ha_rnd_end();
unknown's avatar
unknown committed
3763 3764 3765 3766
  }
err1:
  m_scan_value= 2;
  m_part_spec.start_part= NO_CURRENT_PART_ID;
3767 3768 3769 3770
  DBUG_RETURN(error);
}


unknown's avatar
unknown committed
3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781
/*
  End of a table scan

  SYNOPSIS
    rnd_end()

  RETURN VALUE
    >0          Error code
    0           Success
*/

3782 3783 3784 3785 3786 3787 3788
int ha_partition::rnd_end()
{
  handler **file;
  DBUG_ENTER("ha_partition::rnd_end");
  switch (m_scan_value) {
  case 2:                                       // Error
    break;
unknown's avatar
unknown committed
3789 3790
  case 1:
    if (NO_CURRENT_PART_ID != m_part_spec.start_part)         // Table scan
3791 3792 3793 3794 3795 3796 3797 3798 3799
    {
      late_extra_no_cache(m_part_spec.start_part);
      m_file[m_part_spec.start_part]->ha_rnd_end();
    }
    break;
  case 0:
    file= m_file;
    do
    {
unknown's avatar
unknown committed
3800 3801
      if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file)))
        (*file)->ha_rnd_end();
3802 3803 3804 3805
    } while (*(++file));
    break;
  }
  m_scan_value= 2;
unknown's avatar
unknown committed
3806
  m_part_spec.start_part= NO_CURRENT_PART_ID;
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816
  DBUG_RETURN(0);
}

/*
  read next row during full table scan (scan in random row order)

  SYNOPSIS
    rnd_next()
    buf		buffer that should be filled with data

unknown's avatar
unknown committed
3817 3818 3819
  RETURN VALUE
    >0          Error code
    0           Success
3820

unknown's avatar
unknown committed
3821 3822 3823 3824 3825 3826 3827 3828
  DESCRIPTION
    This is called for each row of the table scan. When you run out of records
    you should return HA_ERR_END_OF_FILE.
    The Field structure for the table is the key to getting data into buf
    in a manner that will allow the server to understand it.

    Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc,
    sql_table.cc, and sql_update.cc.
3829 3830
*/

3831
int ha_partition::rnd_next(uchar *buf)
3832
{
unknown's avatar
unknown committed
3833
  handler *file;
3834
  int result= HA_ERR_END_OF_FILE;
unknown's avatar
unknown committed
3835
  uint part_id= m_part_spec.start_part;
3836 3837
  DBUG_ENTER("ha_partition::rnd_next");

unknown's avatar
unknown committed
3838
  if (NO_CURRENT_PART_ID == part_id)
3839 3840 3841 3842 3843 3844 3845
  {
    /*
      The original set of partitions to scan was empty and thus we report
      the result here.
    */
    goto end;
  }
unknown's avatar
unknown committed
3846 3847 3848 3849
  
  DBUG_ASSERT(m_scan_value == 1);
  file= m_file[part_id];
  
3850 3851
  while (TRUE)
  {
3852
    result= file->rnd_next(buf);
unknown's avatar
unknown committed
3853
    if (!result)
3854 3855
    {
      m_last_part= part_id;
unknown's avatar
unknown committed
3856
      m_part_spec.start_part= part_id;
3857 3858 3859
      table->status= 0;
      DBUG_RETURN(0);
    }
unknown's avatar
unknown committed
3860 3861 3862 3863 3864 3865 3866 3867

    /*
      if we get here, then the current partition rnd_next returned failure
    */
    if (result == HA_ERR_RECORD_DELETED)
      continue;                               // Probably MyISAM

    if (result != HA_ERR_END_OF_FILE)
3868
      goto end_dont_reset_start_part;         // Return error
unknown's avatar
unknown committed
3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884

    /* End current partition */
    late_extra_no_cache(part_id);
    DBUG_PRINT("info", ("rnd_end on partition %d", part_id));
    if ((result= file->ha_rnd_end()))
      break;
    
    /* Shift to next partition */
    while (++part_id < m_tot_parts &&
           !bitmap_is_set(&(m_part_info->used_partitions), part_id))
      ;
    if (part_id >= m_tot_parts)
    {
      result= HA_ERR_END_OF_FILE;
      break;
    }
3885 3886
    m_last_part= part_id;
    m_part_spec.start_part= part_id;
unknown's avatar
unknown committed
3887 3888 3889 3890 3891
    file= m_file[part_id];
    DBUG_PRINT("info", ("rnd_init on partition %d", part_id));
    if ((result= file->ha_rnd_init(1)))
      break;
    late_extra_cache(part_id);
3892 3893 3894 3895
  }

end:
  m_part_spec.start_part= NO_CURRENT_PART_ID;
3896
end_dont_reset_start_part:
3897 3898 3899 3900 3901
  table->status= STATUS_NOT_FOUND;
  DBUG_RETURN(result);
}


unknown's avatar
unknown committed
3902 3903
/*
  Save position of current row
3904

unknown's avatar
unknown committed
3905 3906 3907
  SYNOPSIS
    position()
    record             Current record in MySQL Row Format
3908

unknown's avatar
unknown committed
3909 3910
  RETURN VALUE
    NONE
3911

unknown's avatar
unknown committed
3912 3913 3914 3915 3916
  DESCRIPTION
    position() is called after each call to rnd_next() if the data needs
    to be ordered. You can do something like the following to store
    the position:
    ha_store_ptr(ref, ref_length, current_position);
3917

unknown's avatar
unknown committed
3918 3919 3920 3921 3922 3923 3924
    The server uses ref to store data. ref_length in the above case is
    the size needed to store current_position. ref is just a byte array
    that the server will maintain. If you are using offsets to mark rows, then
    current_position should be the offset. If it is a primary key like in
    BDB, then it needs to be a primary key.

    Called from filesort.cc, sql_select.cc, sql_delete.cc and sql_update.cc.
3925 3926
*/

3927
void ha_partition::position(const uchar *record)
3928
{
3929
  handler *file= m_file[m_last_part];
3930
  DBUG_ENTER("ha_partition::position");
unknown's avatar
unknown committed
3931

3932
  file->position(record);
unknown's avatar
unknown committed
3933
  int2store(ref, m_last_part);
3934 3935 3936 3937 3938
  memcpy((ref + PARTITION_BYTES_IN_POS), file->ref,
	 (ref_length - PARTITION_BYTES_IN_POS));

#ifdef SUPPORTING_PARTITION_OVER_DIFFERENT_ENGINES
#ifdef HAVE_purify
unknown's avatar
unknown committed
3939 3940
  bzero(ref + PARTITION_BYTES_IN_POS + ref_length,
        max_ref_length-ref_length);
3941 3942 3943 3944 3945
#endif /* HAVE_purify */
#endif
  DBUG_VOID_RETURN;
}

unknown's avatar
unknown committed
3946 3947 3948 3949 3950 3951 3952 3953

void ha_partition::column_bitmaps_signal()
{
    handler::column_bitmaps_signal();
    bitmap_union(table->read_set, &m_part_info->full_part_field_set);
}
 

3954
/*
unknown's avatar
unknown committed
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972
  Read row using position

  SYNOPSIS
    rnd_pos()
    out:buf                     Row read in MySQL Row Format
    position                    Position of read row

  RETURN VALUE
    >0                          Error code
    0                           Success

  DESCRIPTION
    This is like rnd_next, but you are given a position to use
    to determine the row. The position will be of the type that you stored in
    ref. You can use ha_get_ptr(pos,ref_length) to retrieve whatever key
    or position you saved when position() was called.
    Called from filesort.cc records.cc sql_insert.cc sql_select.cc
    sql_update.cc.
3973 3974
*/

3975
int ha_partition::rnd_pos(uchar * buf, uchar *pos)
3976 3977 3978 3979 3980
{
  uint part_id;
  handler *file;
  DBUG_ENTER("ha_partition::rnd_pos");

3981
  part_id= uint2korr((const uchar *) pos);
3982 3983 3984 3985 3986 3987 3988
  DBUG_ASSERT(part_id < m_tot_parts);
  file= m_file[part_id];
  m_last_part= part_id;
  DBUG_RETURN(file->rnd_pos(buf, (pos + PARTITION_BYTES_IN_POS)));
}


3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018
/*
  Read row using position using given record to find

  SYNOPSIS
    rnd_pos_by_record()
    record             Current record in MySQL Row Format

  RETURN VALUE
    >0                 Error code
    0                  Success

  DESCRIPTION
    this works as position()+rnd_pos() functions, but does some extra work,
    calculating m_last_part - the partition to where the 'record'
    should go.

    called from replication (log_event.cc)
*/

int ha_partition::rnd_pos_by_record(uchar *record)
{
  DBUG_ENTER("ha_partition::rnd_pos_by_record");

  if (unlikely(get_part_for_delete(record, m_rec0, m_part_info, &m_last_part)))
    DBUG_RETURN(1);

  DBUG_RETURN(handler::rnd_pos_by_record(record));
}


4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038
/****************************************************************************
                MODULE index scan
****************************************************************************/
/*
  Positions an index cursor to the index specified in the handle. Fetches the
  row if available. If the key value is null, begin at the first key of the
  index.

  There are loads of optimisations possible here for the partition handler.
  The same optimisations can also be checked for full table scan although
  only through conditions and not from index ranges.
  Phase one optimisations:
    Check if the fields of the partition function are bound. If so only use
    the single partition it becomes bound to.
  Phase two optimisations:
    If it can be deducted through range or list partitioning that only a
    subset of the partitions are used, then only use those partitions.
*/

/*
4039
  Initialize handler before start of index scan
unknown's avatar
unknown committed
4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052

  SYNOPSIS
    index_init()
    inx                Index number
    sorted             Is rows to be returned in sorted order

  RETURN VALUE
    >0                 Error code
    0                  Success

  DESCRIPTION
    index_init is always called before starting index scans (except when
    starting through index_read_idx and using read_range variants).
4053 4054 4055 4056 4057 4058 4059 4060
*/

int ha_partition::index_init(uint inx, bool sorted)
{
  int error= 0;
  handler **file;
  DBUG_ENTER("ha_partition::index_init");

4061
  DBUG_PRINT("info", ("inx %u sorted %u", inx, sorted));
4062 4063 4064 4065
  active_index= inx;
  m_part_spec.start_part= NO_CURRENT_PART_ID;
  m_start_key.length= 0;
  m_ordered= sorted;
4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078
  m_curr_key_info[0]= table->key_info+inx;
  if (m_pkey_is_clustered && table->s->primary_key != MAX_KEY)
  {
    /*
      if PK is clustered, then the key cmp must use the pk to
      differentiate between equal key in given index.
    */
    DBUG_PRINT("info", ("Clustered pk, using pk as secondary cmp"));
    m_curr_key_info[1]= table->key_info+table->s->primary_key;
    m_curr_key_info[2]= NULL;
  }
  else
    m_curr_key_info[1]= NULL;
4079 4080 4081 4082 4083 4084 4085 4086 4087
  /*
    Some handlers only read fields as specified by the bitmap for the
    read set. For partitioned handlers we always require that the
    fields of the partition functions are read such that we can
    calculate the partition id to place updated and deleted records.
    But this is required for operations that may need to change data only.
  */
  if (m_lock_type == F_WRLCK)
    bitmap_union(table->read_set, &m_part_info->full_part_field_set);
4088
  if (sorted)
4089 4090
  {
    /*
4091 4092 4093 4094 4095 4096 4097 4098
      An ordered scan is requested. We must make sure all fields of the 
      used index are in the read set, as partitioning requires them for
      sorting (see ha_partition::handle_ordered_index_scan).

      The SQL layer may request an ordered index scan without having index
      fields in the read set when
       - it needs to do an ordered scan over an index prefix.
       - it evaluates ORDER BY with SELECT COUNT(*) FROM t1.
4099 4100 4101 4102

      TODO: handle COUNT(*) queries via unordered scan.
    */
    uint i;
4103 4104 4105 4106 4107 4108 4109
    KEY **key_info= m_curr_key_info;
    do
    {
      for (i= 0; i < (*key_info)->key_parts; i++)
        bitmap_set_bit(table->read_set,
                       (*key_info)->key_part[i].field->field_index);
    } while (*(++key_info));
4110
  }
4111 4112 4113 4114
  file= m_file;
  do
  {
    /* TODO RONM: Change to index_init() when code is stable */
unknown's avatar
unknown committed
4115 4116 4117 4118 4119 4120
    if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file)))
      if ((error= (*file)->ha_index_init(inx, sorted)))
      {
        DBUG_ASSERT(0);                           // Should never happen
        break;
      }
4121 4122 4123 4124 4125 4126
  } while (*(++file));
  DBUG_RETURN(error);
}


/*
unknown's avatar
unknown committed
4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138
  End of index scan

  SYNOPSIS
    index_end()

  RETURN VALUE
    >0                 Error code
    0                  Success

  DESCRIPTION
    index_end is called at the end of an index scan to clean up any
    things needed to clean up.
4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153
*/

int ha_partition::index_end()
{
  int error= 0;
  handler **file;
  DBUG_ENTER("ha_partition::index_end");

  active_index= MAX_KEY;
  m_part_spec.start_part= NO_CURRENT_PART_ID;
  file= m_file;
  do
  {
    int tmp;
    /* TODO RONM: Change to index_end() when code is stable */
unknown's avatar
unknown committed
4154 4155 4156
    if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file)))
      if ((tmp= (*file)->ha_index_end()))
        error= tmp;
4157 4158 4159 4160 4161 4162
  } while (*(++file));
  DBUG_RETURN(error);
}


/*
unknown's avatar
unknown committed
4163 4164 4165
  Read one record in an index scan and start an index scan

  SYNOPSIS
4166
    index_read_map()
unknown's avatar
unknown committed
4167 4168
    buf                    Read row in MySQL Row Format
    key                    Key parts in consecutive order
4169
    keypart_map            Which part of key is used
unknown's avatar
unknown committed
4170 4171 4172 4173 4174 4175 4176
    find_flag              What type of key condition is used

  RETURN VALUE
    >0                 Error code
    0                  Success

  DESCRIPTION
4177
    index_read_map starts a new index scan using a start key. The MySQL Server
unknown's avatar
unknown committed
4178 4179 4180
    will check the end key on its own. Thus to function properly the
    partitioned handler need to ensure that it delivers records in the sort
    order of the MySQL Server.
4181 4182
    index_read_map can be restarted without calling index_end on the previous
    index scan and without calling index_init. In this case the index_read_map
unknown's avatar
unknown committed
4183 4184
    is on the same index as the previous index_scan. This is particularly
    used in conjuntion with multi read ranges.
4185 4186
*/

4187 4188 4189
int ha_partition::index_read_map(uchar *buf, const uchar *key,
                                 key_part_map keypart_map,
                                 enum ha_rkey_function find_flag)
4190
{
4191
  DBUG_ENTER("ha_partition::index_read_map");
4192
  end_range= 0;
4193
  m_index_scan_type= partition_index_read;
4194 4195 4196 4197
  m_start_key.key= key;
  m_start_key.keypart_map= keypart_map;
  m_start_key.flag= find_flag;
  DBUG_RETURN(common_index_read(buf, TRUE));
4198 4199 4200
}


unknown's avatar
unknown committed
4201 4202 4203 4204
/*
  Common routine for a number of index_read variants

  SYNOPSIS
4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228
    ha_partition::common_index_read()
      buf             Buffer where the record should be returned
      have_start_key  TRUE <=> the left endpoint is available, i.e. 
                      we're in index_read call or in read_range_first
                      call and the range has left endpoint

                      FALSE <=> there is no left endpoint (we're in
                      read_range_first() call and the range has no left
                      endpoint)
 
  DESCRIPTION
    Start scanning the range (when invoked from read_range_first()) or doing 
    an index lookup (when invoked from index_read_XXX):
     - If possible, perform partition selection
     - Find the set of partitions we're going to use
     - Depending on whether we need ordering:
        NO:  Get the first record from first used partition (see 
             handle_unordered_scan_next_partition)
        YES: Fill the priority queue and get the record that is the first in
             the ordering

  RETURN
    0      OK 
    other  HA_ERR_END_OF_FILE or other error code.
unknown's avatar
unknown committed
4229 4230
*/

4231
int ha_partition::common_index_read(uchar *buf, bool have_start_key)
4232 4233
{
  int error;
4234
  uint UNINIT_VAR(key_len); /* used if have_start_key==TRUE */
4235
  bool reverse_order= FALSE;
4236 4237
  DBUG_ENTER("ha_partition::common_index_read");

4238 4239 4240
  DBUG_PRINT("info", ("m_ordered %u m_ordered_scan_ong %u have_start_key %u",
                      m_ordered, m_ordered_scan_ongoing, have_start_key));

4241 4242 4243 4244 4245
  if (have_start_key)
  {
    m_start_key.length= key_len= calculate_key_len(table, active_index, 
                                                   m_start_key.key,
                                                   m_start_key.keypart_map);
4246
    DBUG_ASSERT(key_len);
4247 4248
  }
  if ((error= partition_scan_set_up(buf, have_start_key)))
4249 4250 4251
  {
    DBUG_RETURN(error);
  }
4252 4253 4254 4255 4256

  if (have_start_key && 
      (m_start_key.flag == HA_READ_PREFIX_LAST ||
       m_start_key.flag == HA_READ_PREFIX_LAST_OR_PREV ||
       m_start_key.flag == HA_READ_BEFORE_KEY))
4257 4258 4259 4260
  {
    reverse_order= TRUE;
    m_ordered_scan_ongoing= TRUE;
  }
4261 4262
  DBUG_PRINT("info", ("m_ordered %u m_o_scan_ong %u have_start_key %u",
                      m_ordered, m_ordered_scan_ongoing, have_start_key));
4263
  if (!m_ordered_scan_ongoing ||
4264
      (have_start_key && m_start_key.flag == HA_READ_KEY_EXACT &&
4265 4266
       !m_pkey_is_clustered &&
       key_len >= m_curr_key_info[0]->key_length))
4267
   {
4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278
    /*
      We use unordered index scan either when read_range is used and flag
      is set to not use ordered or when an exact key is used and in this
      case all records will be sorted equal and thus the sort order of the
      resulting records doesn't matter.
      We also use an unordered index scan when the number of partitions to
      scan is only one.
      The unordered index scan will use the partition set created.
      Need to set unordered scan ongoing since we can come here even when
      it isn't set.
    */
4279
    DBUG_PRINT("info", ("doing unordered scan"));
4280 4281 4282 4283 4284 4285 4286 4287 4288
    m_ordered_scan_ongoing= FALSE;
    error= handle_unordered_scan_next_partition(buf);
  }
  else
  {
    /*
      In all other cases we will use the ordered index scan. This will use
      the partition set created by the get_partition_set method.
    */
4289
    error= handle_ordered_index_scan(buf, reverse_order);
4290 4291 4292 4293 4294 4295
  }
  DBUG_RETURN(error);
}


/*
unknown's avatar
unknown committed
4296 4297 4298 4299 4300
  Start an index scan from leftmost record and return first record

  SYNOPSIS
    index_first()
    buf                 Read row in MySQL Row Format
4301

unknown's avatar
unknown committed
4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313
  RETURN VALUE
    >0                  Error code
    0                   Success

  DESCRIPTION
    index_first() asks for the first key in the index.
    This is similar to index_read except that there is no start key since
    the scan starts from the leftmost entry and proceeds forward with
    index_next.

    Called from opt_range.cc, opt_sum.cc, sql_handler.cc,
    and sql_select.cc.
4314 4315
*/

4316
int ha_partition::index_first(uchar * buf)
4317 4318
{
  DBUG_ENTER("ha_partition::index_first");
unknown's avatar
unknown committed
4319

4320 4321 4322 4323 4324 4325 4326
  end_range= 0;
  m_index_scan_type= partition_index_first;
  DBUG_RETURN(common_first_last(buf));
}


/*
unknown's avatar
unknown committed
4327 4328 4329 4330 4331
  Start an index scan from rightmost record and return first record
  
  SYNOPSIS
    index_last()
    buf                 Read row in MySQL Row Format
4332

unknown's avatar
unknown committed
4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344
  RETURN VALUE
    >0                  Error code
    0                   Success

  DESCRIPTION
    index_last() asks for the last key in the index.
    This is similar to index_read except that there is no start key since
    the scan starts from the rightmost entry and proceeds forward with
    index_prev.

    Called from opt_range.cc, opt_sum.cc, sql_handler.cc,
    and sql_select.cc.
4345 4346
*/

4347
int ha_partition::index_last(uchar * buf)
4348 4349
{
  DBUG_ENTER("ha_partition::index_last");
unknown's avatar
unknown committed
4350

4351 4352 4353 4354
  m_index_scan_type= partition_index_last;
  DBUG_RETURN(common_first_last(buf));
}

unknown's avatar
unknown committed
4355 4356 4357 4358
/*
  Common routine for index_first/index_last

  SYNOPSIS
4359
    ha_partition::common_first_last()
unknown's avatar
unknown committed
4360 4361 4362 4363
  
  see index_first for rest
*/

4364
int ha_partition::common_first_last(uchar *buf)
4365 4366
{
  int error;
unknown's avatar
unknown committed
4367

4368 4369
  if ((error= partition_scan_set_up(buf, FALSE)))
    return error;
4370 4371
  if (!m_ordered_scan_ongoing &&
      m_index_scan_type != partition_index_last)
4372
    return handle_unordered_scan_next_partition(buf);
4373
  return handle_ordered_index_scan(buf, FALSE);
4374 4375
}

unknown's avatar
unknown committed
4376

4377
/*
unknown's avatar
unknown committed
4378 4379 4380
  Read last using key

  SYNOPSIS
4381
    index_read_last_map()
unknown's avatar
unknown committed
4382 4383
    buf                   Read row in MySQL Row Format
    key                   Key
4384
    keypart_map           Which part of key is used
unknown's avatar
unknown committed
4385 4386 4387 4388 4389 4390 4391 4392

  RETURN VALUE
    >0                    Error code
    0                     Success

  DESCRIPTION
    This is used in join_read_last_key to optimise away an ORDER BY.
    Can only be used on indexes supporting HA_READ_ORDER
4393 4394
*/

4395 4396
int ha_partition::index_read_last_map(uchar *buf, const uchar *key,
                                      key_part_map keypart_map)
4397 4398
{
  DBUG_ENTER("ha_partition::index_read_last");
unknown's avatar
unknown committed
4399

4400
  m_ordered= TRUE;				// Safety measure
4401 4402
  end_range= 0;
  m_index_scan_type= partition_index_read_last;
4403 4404 4405 4406
  m_start_key.key= key;
  m_start_key.keypart_map= keypart_map;
  m_start_key.flag= HA_READ_PREFIX_LAST;
  DBUG_RETURN(common_index_read(buf, TRUE));
4407 4408 4409
}


4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432
/*
  Optimization of the default implementation to take advantage of dynamic
  partition pruning.
*/
int ha_partition::index_read_idx_map(uchar *buf, uint index,
                                     const uchar *key,
                                     key_part_map keypart_map,
                                     enum ha_rkey_function find_flag)
{
  int error= HA_ERR_KEY_NOT_FOUND;
  DBUG_ENTER("ha_partition::index_read_idx_map");

  if (find_flag == HA_READ_KEY_EXACT)
  {
    uint part;
    m_start_key.key= key;
    m_start_key.keypart_map= keypart_map;
    m_start_key.flag= find_flag;
    m_start_key.length= calculate_key_len(table, index, m_start_key.key,
                                          m_start_key.keypart_map);

    get_partition_set(table, buf, index, &m_start_key, &m_part_spec);

4433 4434 4435 4436 4437 4438
    /* 
      We have either found exactly 1 partition
      (in which case start_part == end_part)
      or no matching partitions (start_part > end_part)
    */
    DBUG_ASSERT(m_part_spec.start_part >= m_part_spec.end_part);
4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450

    for (part= m_part_spec.start_part; part <= m_part_spec.end_part; part++)
    {
      if (bitmap_is_set(&(m_part_info->used_partitions), part))
      {
        error= m_file[part]->index_read_idx_map(buf, index, key,
                                                keypart_map, find_flag);
        if (error != HA_ERR_KEY_NOT_FOUND &&
            error != HA_ERR_END_OF_FILE)
          break;
      }
    }
4451 4452
    if (part <= m_part_spec.end_part)
      m_last_part= part;
4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467
  }
  else
  {
    /*
      If not only used with READ_EXACT, we should investigate if possible
      to optimize for other find_flag's as well.
    */
    DBUG_ASSERT(0);
    /* fall back on the default implementation */
    error= handler::index_read_idx_map(buf, index, key, keypart_map, find_flag);
  }
  DBUG_RETURN(error);
}


4468
/*
unknown's avatar
unknown committed
4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480
  Read next record in a forward index scan

  SYNOPSIS
    index_next()
    buf                   Read row in MySQL Row Format

  RETURN VALUE
    >0                    Error code
    0                     Success

  DESCRIPTION
    Used to read forward through the index.
4481 4482
*/

4483
int ha_partition::index_next(uchar * buf)
4484 4485
{
  DBUG_ENTER("ha_partition::index_next");
unknown's avatar
unknown committed
4486

4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501
  /*
    TODO(low priority):
    If we want partition to work with the HANDLER commands, we
    must be able to do index_last() -> index_prev() -> index_next()
  */
  DBUG_ASSERT(m_index_scan_type != partition_index_last);
  if (!m_ordered_scan_ongoing)
  {
    DBUG_RETURN(handle_unordered_next(buf, FALSE));
  }
  DBUG_RETURN(handle_ordered_next(buf, FALSE));
}


/*
unknown's avatar
unknown committed
4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516
  Read next record special

  SYNOPSIS
    index_next_same()
    buf                   Read row in MySQL Row Format
    key                   Key
    keylen                Length of key

  RETURN VALUE
    >0                    Error code
    0                     Success

  DESCRIPTION
    This routine is used to read the next but only if the key is the same
    as supplied in the call.
4517 4518
*/

4519
int ha_partition::index_next_same(uchar *buf, const uchar *key, uint keylen)
4520 4521
{
  DBUG_ENTER("ha_partition::index_next_same");
unknown's avatar
unknown committed
4522

4523 4524 4525 4526 4527 4528 4529
  DBUG_ASSERT(keylen == m_start_key.length);
  DBUG_ASSERT(m_index_scan_type != partition_index_last);
  if (!m_ordered_scan_ongoing)
    DBUG_RETURN(handle_unordered_next(buf, TRUE));
  DBUG_RETURN(handle_ordered_next(buf, TRUE));
}

unknown's avatar
unknown committed
4530

4531
/*
unknown's avatar
unknown committed
4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543
  Read next record when performing index scan backwards

  SYNOPSIS
    index_prev()
    buf                   Read row in MySQL Row Format

  RETURN VALUE
    >0                    Error code
    0                     Success

  DESCRIPTION
    Used to read backwards through the index.
4544 4545
*/

4546
int ha_partition::index_prev(uchar * buf)
4547 4548
{
  DBUG_ENTER("ha_partition::index_prev");
unknown's avatar
unknown committed
4549

4550 4551 4552 4553 4554 4555 4556
  /* TODO: read comment in index_next */
  DBUG_ASSERT(m_index_scan_type != partition_index_first);
  DBUG_RETURN(handle_ordered_prev(buf));
}


/*
unknown's avatar
unknown committed
4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574
  Start a read of one range with start and end key

  SYNOPSIS
    read_range_first()
    start_key           Specification of start key
    end_key             Specification of end key
    eq_range_arg        Is it equal range
    sorted              Should records be returned in sorted order

  RETURN VALUE
    >0                    Error code
    0                     Success

  DESCRIPTION
    We reimplement read_range_first since we don't want the compare_key
    check at the end. This is already performed in the partition handler.
    read_range_next is very much different due to that we need to scan
    all underlying handlers.
4575 4576 4577 4578 4579 4580 4581 4582
*/

int ha_partition::read_range_first(const key_range *start_key,
				   const key_range *end_key,
				   bool eq_range_arg, bool sorted)
{
  int error;
  DBUG_ENTER("ha_partition::read_range_first");
unknown's avatar
unknown committed
4583

4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595
  m_ordered= sorted;
  eq_range= eq_range_arg;
  end_range= 0;
  if (end_key)
  {
    end_range= &save_end_range;
    save_end_range= *end_key;
    key_compare_result_on_equal=
      ((end_key->flag == HA_READ_BEFORE_KEY) ? 1 :
       (end_key->flag == HA_READ_AFTER_KEY) ? -1 : 0);
  }

4596
  range_key_part= m_curr_key_info[0]->key_part;
4597 4598
  if (start_key)
    m_start_key= *start_key;
4599
  else
4600 4601 4602 4603
    m_start_key.key= NULL;

  m_index_scan_type= partition_read_range;
  error= common_index_read(m_rec0, test(start_key));
4604 4605 4606 4607
  DBUG_RETURN(error);
}


unknown's avatar
unknown committed
4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618
/*
  Read next record in read of a range with start and end key

  SYNOPSIS
    read_range_next()

  RETURN VALUE
    >0                    Error code
    0                     Success
*/

4619 4620 4621
int ha_partition::read_range_next()
{
  DBUG_ENTER("ha_partition::read_range_next");
unknown's avatar
unknown committed
4622

4623
  if (m_ordered_scan_ongoing)
4624
  {
4625
    DBUG_RETURN(handle_ordered_next(table->record[0], eq_range));
4626
  }
4627
  DBUG_RETURN(handle_unordered_next(table->record[0], eq_range));
4628 4629 4630
}


unknown's avatar
unknown committed
4631
/*
4632
  Common routine to set up index scans
unknown's avatar
unknown committed
4633 4634

  SYNOPSIS
4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650
    ha_partition::partition_scan_set_up()
      buf            Buffer to later return record in (this function
                     needs it to calculcate partitioning function
                     values)

      idx_read_flag  TRUE <=> m_start_key has range start endpoint which 
                     probably can be used to determine the set of partitions
                     to scan.
                     FALSE <=> there is no start endpoint.

  DESCRIPTION
    Find out which partitions we'll need to read when scanning the specified
    range.

    If we need to scan only one partition, set m_ordered_scan_ongoing=FALSE
    as we will not need to do merge ordering.
unknown's avatar
unknown committed
4651 4652 4653 4654 4655 4656

  RETURN VALUE
    >0                    Error code
    0                     Success
*/

4657
int ha_partition::partition_scan_set_up(uchar * buf, bool idx_read_flag)
4658 4659 4660 4661 4662 4663
{
  DBUG_ENTER("ha_partition::partition_scan_set_up");

  if (idx_read_flag)
    get_partition_set(table,buf,active_index,&m_start_key,&m_part_spec);
  else
unknown's avatar
unknown committed
4664 4665 4666 4667
  {
    m_part_spec.start_part= 0;
    m_part_spec.end_part= m_tot_parts - 1;
  }
4668 4669 4670 4671 4672 4673 4674
  if (m_part_spec.start_part > m_part_spec.end_part)
  {
    /*
      We discovered a partition set but the set was empty so we report
      key not found.
    */
    DBUG_PRINT("info", ("scan with no partition to scan"));
4675
    table->status= STATUS_NOT_FOUND;
4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691
    DBUG_RETURN(HA_ERR_END_OF_FILE);
  }
  if (m_part_spec.start_part == m_part_spec.end_part)
  {
    /*
      We discovered a single partition to scan, this never needs to be
      performed using the ordered index scan.
    */
    DBUG_PRINT("info", ("index scan using the single partition %d",
			m_part_spec.start_part));
    m_ordered_scan_ongoing= FALSE;
  }
  else
  {
    /*
      Set m_ordered_scan_ongoing according how the scan should be done
unknown's avatar
unknown committed
4692 4693 4694
      Only exact partitions are discovered atm by get_partition_set.
      Verify this, also bitmap must have at least one bit set otherwise
      the result from this table is the empty set.
4695
    */
unknown's avatar
unknown committed
4696 4697 4698 4699
    uint start_part= bitmap_get_first_set(&(m_part_info->used_partitions));
    if (start_part == MY_BIT_NONE)
    {
      DBUG_PRINT("info", ("scan with no partition to scan"));
4700
      table->status= STATUS_NOT_FOUND;
unknown's avatar
unknown committed
4701 4702 4703 4704 4705
      DBUG_RETURN(HA_ERR_END_OF_FILE);
    }
    if (start_part > m_part_spec.start_part)
      m_part_spec.start_part= start_part;
    DBUG_ASSERT(m_part_spec.start_part < m_tot_parts);
4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717
    m_ordered_scan_ongoing= m_ordered;
  }
  DBUG_ASSERT(m_part_spec.start_part < m_tot_parts &&
              m_part_spec.end_part < m_tot_parts);
  DBUG_RETURN(0);
}


/****************************************************************************
  Unordered Index Scan Routines
****************************************************************************/
/*
unknown's avatar
unknown committed
4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737
  Common routine to handle index_next with unordered results

  SYNOPSIS
    handle_unordered_next()
    out:buf                       Read row in MySQL Row Format
    next_same                     Called from index_next_same

  RETURN VALUE
    HA_ERR_END_OF_FILE            End of scan
    0                             Success
    other                         Error code

  DESCRIPTION
    These routines are used to scan partitions without considering order.
    This is performed in two situations.
    1) In read_multi_range this is the normal case
    2) When performing any type of index_read, index_first, index_last where
    all fields in the partition function is bound. In this case the index
    scan is performed on only one partition and thus it isn't necessary to
    perform any sort.
4738 4739
*/

4740
int ha_partition::handle_unordered_next(uchar *buf, bool is_next_same)
4741
{
4742
  handler *file= m_file[m_part_spec.start_part];
4743 4744 4745 4746
  int error;
  DBUG_ENTER("ha_partition::handle_unordered_next");

  /*
4747 4748
    We should consider if this should be split into three functions as
    partition_read_range is_next_same are always local constants
4749
  */
4750 4751 4752 4753 4754 4755 4756 4757 4758 4759

  if (m_index_scan_type == partition_read_range)
  {
    if (!(error= file->read_range_next()))
    {
      m_last_part= m_part_spec.start_part;
      DBUG_RETURN(0);
    }
  }
  else if (is_next_same)
4760 4761 4762 4763 4764 4765 4766 4767
  {
    if (!(error= file->index_next_same(buf, m_start_key.key,
                                       m_start_key.length)))
    {
      m_last_part= m_part_spec.start_part;
      DBUG_RETURN(0);
    }
  }
4768
  else 
4769
  {
4770
    if (!(error= file->index_next(buf)))
4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786
    {
      m_last_part= m_part_spec.start_part;
      DBUG_RETURN(0);                           // Row was in range
    }
  }

  if (error == HA_ERR_END_OF_FILE)
  {
    m_part_spec.start_part++;                    // Start using next part
    error= handle_unordered_scan_next_partition(buf);
  }
  DBUG_RETURN(error);
}


/*
unknown's avatar
unknown committed
4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800
  Handle index_next when changing to new partition

  SYNOPSIS
    handle_unordered_scan_next_partition()
    buf                       Read row in MySQL Row Format

  RETURN VALUE
    HA_ERR_END_OF_FILE            End of scan
    0                             Success
    other                         Error code

  DESCRIPTION
    This routine is used to start the index scan on the next partition.
    Both initial start and after completing scan on one partition.
4801 4802
*/

4803
int ha_partition::handle_unordered_scan_next_partition(uchar * buf)
4804 4805 4806 4807 4808 4809 4810
{
  uint i;
  DBUG_ENTER("ha_partition::handle_unordered_scan_next_partition");

  for (i= m_part_spec.start_part; i <= m_part_spec.end_part; i++)
  {
    int error;
unknown's avatar
unknown committed
4811
    handler *file;
4812

unknown's avatar
unknown committed
4813 4814 4815
    if (!(bitmap_is_set(&(m_part_info->used_partitions), i)))
      continue;
    file= m_file[i];
4816 4817
    m_part_spec.start_part= i;
    switch (m_index_scan_type) {
4818 4819 4820 4821 4822
    case partition_read_range:
      DBUG_PRINT("info", ("read_range_first on partition %d", i));
      error= file->read_range_first(m_start_key.key? &m_start_key: NULL,
                                    end_range, eq_range, FALSE);
      break;
4823 4824
    case partition_index_read:
      DBUG_PRINT("info", ("index_read on partition %d", i));
4825 4826 4827
      error= file->index_read_map(buf, m_start_key.key,
                                  m_start_key.keypart_map,
                                  m_start_key.flag);
4828 4829 4830 4831 4832
      break;
    case partition_index_first:
      DBUG_PRINT("info", ("index_first on partition %d", i));
      error= file->index_first(buf);
      break;
4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844
    case partition_index_first_unordered:
      /*
        We perform a scan without sorting and this means that we
        should not use the index_first since not all handlers
        support it and it is also unnecessary to restrict sort
        order.
      */
      DBUG_PRINT("info", ("read_range_first on partition %d", i));
      table->record[0]= buf;
      error= file->read_range_first(0, end_range, eq_range, 0);
      table->record[0]= m_rec0;
      break;
4845 4846 4847 4848 4849 4850
    default:
      DBUG_ASSERT(FALSE);
      DBUG_RETURN(1);
    }
    if (!error)
    {
4851 4852
      m_last_part= i;
      DBUG_RETURN(0);
4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863
    }
    if ((error != HA_ERR_END_OF_FILE) && (error != HA_ERR_KEY_NOT_FOUND))
      DBUG_RETURN(error);
    DBUG_PRINT("info", ("HA_ERR_END_OF_FILE on partition %d", i));
  }
  m_part_spec.start_part= NO_CURRENT_PART_ID;
  DBUG_RETURN(HA_ERR_END_OF_FILE);
}


/*
unknown's avatar
unknown committed
4864
  Common routine to start index scan with ordered results
4865

unknown's avatar
unknown committed
4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888
  SYNOPSIS
    handle_ordered_index_scan()
    out:buf                       Read row in MySQL Row Format

  RETURN VALUE
    HA_ERR_END_OF_FILE            End of scan
    0                             Success
    other                         Error code

  DESCRIPTION
    This part contains the logic to handle index scans that require ordered
    output. This includes all except those started by read_range_first with
    the flag ordered set to FALSE. Thus most direct index_read and all
    index_first and index_last.

    We implement ordering by keeping one record plus a key buffer for each
    partition. Every time a new entry is requested we will fetch a new
    entry from the partition that is currently not filled with an entry.
    Then the entry is put into its proper sort position.

    Returning a record is done by getting the top record, copying the
    record to the request buffer and setting the partition as empty on
    entries.
4889 4890
*/

4891
int ha_partition::handle_ordered_index_scan(uchar *buf, bool reverse_order)
4892
{
unknown's avatar
unknown committed
4893 4894
  uint i;
  uint j= 0;
4895 4896 4897 4898
  bool found= FALSE;
  DBUG_ENTER("ha_partition::handle_ordered_index_scan");

  m_top_entry= NO_CURRENT_PART_ID;
unknown's avatar
unknown committed
4899
  queue_remove_all(&m_queue);
unknown's avatar
unknown committed
4900 4901

  DBUG_PRINT("info", ("m_part_spec.start_part %d", m_part_spec.start_part));
4902 4903
  for (i= m_part_spec.start_part; i <= m_part_spec.end_part; i++)
  {
unknown's avatar
unknown committed
4904 4905
    if (!(bitmap_is_set(&(m_part_info->used_partitions), i)))
      continue;
4906
    uchar *rec_buf_ptr= rec_buf(i);
unknown's avatar
unknown committed
4907
    int error;
4908 4909 4910 4911
    handler *file= m_file[i];

    switch (m_index_scan_type) {
    case partition_index_read:
4912 4913 4914 4915
      error= file->index_read_map(rec_buf_ptr,
                                  m_start_key.key,
                                  m_start_key.keypart_map,
                                  m_start_key.flag);
4916 4917 4918 4919 4920 4921 4922 4923 4924
      break;
    case partition_index_first:
      error= file->index_first(rec_buf_ptr);
      reverse_order= FALSE;
      break;
    case partition_index_last:
      error= file->index_last(rec_buf_ptr);
      reverse_order= TRUE;
      break;
4925
    case partition_index_read_last:
4926 4927 4928
      error= file->index_read_last_map(rec_buf_ptr,
                                       m_start_key.key,
                                       m_start_key.keypart_map);
4929 4930
      reverse_order= TRUE;
      break;
4931 4932 4933 4934 4935 4936
    case partition_read_range:
    {
      /* 
        This can only read record to table->record[0], as it was set when
        the table was being opened. We have to memcpy data ourselves.
      */
4937 4938
      error= file->read_range_first(m_start_key.key? &m_start_key: NULL,
                                    end_range, eq_range, TRUE);
4939 4940 4941 4942
      memcpy(rec_buf_ptr, table->record[0], m_rec_length);
      reverse_order= FALSE;
      break;
    }
4943 4944 4945 4946 4947 4948 4949 4950
    default:
      DBUG_ASSERT(FALSE);
      DBUG_RETURN(HA_ERR_END_OF_FILE);
    }
    if (!error)
    {
      found= TRUE;
      /*
4951
        Initialize queue without order first, simply insert
4952
      */
4953
      queue_element(&m_queue, j++)= (uchar*)queue_buf(i);
4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965
    }
    else if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
    {
      DBUG_RETURN(error);
    }
  }
  if (found)
  {
    /*
      We found at least one partition with data, now sort all entries and
      after that read the first entry and copy it to the buffer to return in.
    */
unknown's avatar
unknown committed
4966 4967 4968 4969
    queue_set_max_at_top(&m_queue, reverse_order);
    queue_set_cmp_arg(&m_queue, (void*)m_curr_key_info);
    m_queue.elements= j;
    queue_fix(&m_queue);
4970
    return_top_record(buf);
4971
    table->status= 0;
4972 4973 4974 4975 4976 4977 4978
    DBUG_PRINT("info", ("Record returned from partition %d", m_top_entry));
    DBUG_RETURN(0);
  }
  DBUG_RETURN(HA_ERR_END_OF_FILE);
}


unknown's avatar
unknown committed
4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989
/*
  Return the top record in sort order

  SYNOPSIS
    return_top_record()
    out:buf                  Row returned in MySQL Row Format

  RETURN VALUE
    NONE
*/

4990
void ha_partition::return_top_record(uchar *buf)
4991 4992
{
  uint part_id;
4993 4994
  uchar *key_buffer= queue_top(&m_queue);
  uchar *rec_buffer= key_buffer + PARTITION_BYTES_IN_POS;
unknown's avatar
unknown committed
4995

4996 4997 4998 4999 5000 5001 5002
  part_id= uint2korr(key_buffer);
  memcpy(buf, rec_buffer, m_rec_length);
  m_last_part= part_id;
  m_top_entry= part_id;
}


unknown's avatar
unknown committed
5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016
/*
  Common routine to handle index_next with ordered results

  SYNOPSIS
    handle_ordered_next()
    out:buf                       Read row in MySQL Row Format
    next_same                     Called from index_next_same

  RETURN VALUE
    HA_ERR_END_OF_FILE            End of scan
    0                             Success
    other                         Error code
*/

5017
int ha_partition::handle_ordered_next(uchar *buf, bool is_next_same)
5018 5019 5020 5021 5022
{
  int error;
  uint part_id= m_top_entry;
  handler *file= m_file[part_id];
  DBUG_ENTER("ha_partition::handle_ordered_next");
5023 5024 5025 5026 5027 5028 5029
  
  if (m_index_scan_type == partition_read_range)
  {
    error= file->read_range_next();
    memcpy(rec_buf(part_id), table->record[0], m_rec_length);
  }
  else if (!is_next_same)
5030 5031 5032 5033 5034 5035 5036 5037 5038
    error= file->index_next(rec_buf(part_id));
  else
    error= file->index_next_same(rec_buf(part_id), m_start_key.key,
				 m_start_key.length);
  if (error)
  {
    if (error == HA_ERR_END_OF_FILE)
    {
      /* Return next buffered row */
unknown's avatar
unknown committed
5039 5040
      queue_remove(&m_queue, (uint) 0);
      if (m_queue.elements)
5041 5042 5043 5044
      {
         DBUG_PRINT("info", ("Record returned from partition %u (2)",
                     m_top_entry));
         return_top_record(buf);
5045
         table->status= 0;
5046 5047 5048 5049 5050
         error= 0;
      }
    }
    DBUG_RETURN(error);
  }
unknown's avatar
unknown committed
5051
  queue_replaced(&m_queue);
5052 5053 5054 5055 5056 5057
  return_top_record(buf);
  DBUG_PRINT("info", ("Record returned from partition %u", m_top_entry));
  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070
/*
  Common routine to handle index_prev with ordered results

  SYNOPSIS
    handle_ordered_prev()
    out:buf                       Read row in MySQL Row Format

  RETURN VALUE
    HA_ERR_END_OF_FILE            End of scan
    0                             Success
    other                         Error code
*/

5071
int ha_partition::handle_ordered_prev(uchar *buf)
5072 5073 5074 5075 5076
{
  int error;
  uint part_id= m_top_entry;
  handler *file= m_file[part_id];
  DBUG_ENTER("ha_partition::handle_ordered_prev");
unknown's avatar
unknown committed
5077

5078 5079 5080 5081
  if ((error= file->index_prev(rec_buf(part_id))))
  {
    if (error == HA_ERR_END_OF_FILE)
    {
unknown's avatar
unknown committed
5082 5083
      queue_remove(&m_queue, (uint) 0);
      if (m_queue.elements)
5084 5085 5086 5087 5088
      {
	return_top_record(buf);
	DBUG_PRINT("info", ("Record returned from partition %d (2)",
			    m_top_entry));
        error= 0;
5089
        table->status= 0;
5090 5091 5092 5093
      }
    }
    DBUG_RETURN(error);
  }
unknown's avatar
unknown committed
5094
  queue_replaced(&m_queue);
5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110
  return_top_record(buf);
  DBUG_PRINT("info", ("Record returned from partition %d", m_top_entry));
  DBUG_RETURN(0);
}


/****************************************************************************
                MODULE information calls
****************************************************************************/

/*
  These are all first approximations of the extra, info, scan_time
  and read_time calls
*/

/*
unknown's avatar
unknown committed
5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172
  General method to gather info from handler

  SYNOPSIS
    info()
    flag              Specifies what info is requested

  RETURN VALUE
    NONE

  DESCRIPTION
    ::info() is used to return information to the optimizer.
    Currently this table handler doesn't implement most of the fields
    really needed. SHOW also makes use of this data
    Another note, if your handler doesn't proved exact record count,
    you will probably want to have the following in your code:
    if (records < 2)
      records = 2;
    The reason is that the server will optimize for cases of only a single
    record. If in a table scan you don't know the number of records
    it will probably be better to set records to two so you can return
    as many records as you need.

    Along with records a few more variables you may wish to set are:
      records
      deleted
      data_file_length
      index_file_length
      delete_length
      check_time
    Take a look at the public variables in handler.h for more information.

    Called in:
      filesort.cc
      ha_heap.cc
      item_sum.cc
      opt_sum.cc
      sql_delete.cc
     sql_delete.cc
     sql_derived.cc
      sql_select.cc
      sql_select.cc
      sql_select.cc
      sql_select.cc
      sql_select.cc
      sql_show.cc
      sql_show.cc
      sql_show.cc
      sql_show.cc
      sql_table.cc
      sql_union.cc
      sql_update.cc

    Some flags that are not implemented
      HA_STATUS_POS:
        This parameter is never used from the MySQL Server. It is checked in a
        place in MyISAM so could potentially be used by MyISAM specific
        programs.
      HA_STATUS_NO_LOCK:
      This is declared and often used. It's only used by MyISAM.
      It means that MySQL doesn't need the absolute latest statistics
      information. This may save the handler from doing internal locks while
      retrieving statistics data.
5173 5174
*/

5175
int ha_partition::info(uint flag)
5176
{
5177 5178
  uint no_lock_flag= flag & HA_STATUS_NO_LOCK;
  uint extra_var_flag= flag & HA_STATUS_VARIABLE_EXTRA;
5179
  DBUG_ENTER("ha_partition::info");
5180 5181 5182

  if (flag & HA_STATUS_AUTO)
  {
5183
    bool auto_inc_is_first_in_idx= (table_share->next_number_keypart == 0);
5184
    DBUG_PRINT("info", ("HA_STATUS_AUTO"));
5185 5186
    if (!table->found_next_number_field)
      stats.auto_increment_value= 0;
Mattias Jonsson's avatar
Mattias Jonsson committed
5187
    else if (table_share->ha_part_data->auto_inc_initialized)
5188
    {
5189
      lock_auto_increment();
Mattias Jonsson's avatar
Mattias Jonsson committed
5190
      stats.auto_increment_value= table_share->ha_part_data->next_auto_inc_val;
5191 5192 5193 5194 5195 5196
      unlock_auto_increment();
    }
    else
    {
      lock_auto_increment();
      /* to avoid two concurrent initializations, check again when locked */
Mattias Jonsson's avatar
Mattias Jonsson committed
5197 5198 5199
      if (table_share->ha_part_data->auto_inc_initialized)
        stats.auto_increment_value=
                                 table_share->ha_part_data->next_auto_inc_val;
5200 5201 5202 5203 5204 5205 5206 5207 5208 5209
      else
      {
        handler *file, **file_array;
        ulonglong auto_increment_value= 0;
        file_array= m_file;
        DBUG_PRINT("info",
                   ("checking all partitions for auto_increment_value"));
        do
        {
          file= *file_array;
5210
          file->info(HA_STATUS_AUTO | no_lock_flag);
5211 5212 5213 5214 5215 5216 5217 5218
          set_if_bigger(auto_increment_value,
                        file->stats.auto_increment_value);
        } while (*(++file_array));

        DBUG_ASSERT(auto_increment_value);
        stats.auto_increment_value= auto_increment_value;
        if (auto_inc_is_first_in_idx)
        {
Mattias Jonsson's avatar
Mattias Jonsson committed
5219 5220 5221
          set_if_bigger(table_share->ha_part_data->next_auto_inc_val,
                        auto_increment_value);
          table_share->ha_part_data->auto_inc_initialized= TRUE;
5222
          DBUG_PRINT("info", ("initializing next_auto_inc_val to %lu",
Mattias Jonsson's avatar
Mattias Jonsson committed
5223
                       (ulong) table_share->ha_part_data->next_auto_inc_val));
5224 5225 5226 5227
        }
      }
      unlock_auto_increment();
    }
5228 5229 5230 5231 5232 5233 5234
  }
  if (flag & HA_STATUS_VARIABLE)
  {
    DBUG_PRINT("info", ("HA_STATUS_VARIABLE"));
    /*
      Calculates statistical variables
      records:           Estimate of number records in table
5235
      We report sum (always at least 2 if not empty)
5236 5237 5238 5239 5240 5241 5242 5243
      deleted:           Estimate of number holes in the table due to
      deletes
      We report sum
      data_file_length:  Length of data file, in principle bytes in table
      We report sum
      index_file_length: Length of index file, in principle bytes in
      indexes in the table
      We report sum
5244 5245
      delete_length: Length of free space easily used by new records in table
      We report sum
5246 5247 5248 5249 5250
      mean_record_length:Mean record length in the table
      We calculate this
      check_time:        Time of last check (only applicable to MyISAM)
      We report last time of all underlying handlers
    */
5251
    handler *file, **file_array;
5252 5253 5254 5255 5256
    stats.records= 0;
    stats.deleted= 0;
    stats.data_file_length= 0;
    stats.index_file_length= 0;
    stats.check_time= 0;
5257
    stats.delete_length= 0;
5258 5259 5260
    file_array= m_file;
    do
    {
unknown's avatar
unknown committed
5261 5262 5263
      if (bitmap_is_set(&(m_part_info->used_partitions), (file_array - m_file)))
      {
        file= *file_array;
5264
        file->info(HA_STATUS_VARIABLE | no_lock_flag | extra_var_flag);
5265 5266 5267 5268
        stats.records+= file->stats.records;
        stats.deleted+= file->stats.deleted;
        stats.data_file_length+= file->stats.data_file_length;
        stats.index_file_length+= file->stats.index_file_length;
unknown's avatar
unknown committed
5269
        stats.delete_length+= file->stats.delete_length;
5270 5271
        if (file->stats.check_time > stats.check_time)
          stats.check_time= file->stats.check_time;
unknown's avatar
unknown committed
5272
      }
5273
    } while (*(++file_array));
5274
    if (stats.records && stats.records < 2 &&
5275
        !(m_file[0]->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT))
5276 5277 5278
      stats.records= 2;
    if (stats.records > 0)
      stats.mean_rec_length= (ulong) (stats.data_file_length / stats.records);
unknown's avatar
unknown committed
5279
    else
5280
      stats.mean_rec_length= 0;
5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314
  }
  if (flag & HA_STATUS_CONST)
  {
    DBUG_PRINT("info", ("HA_STATUS_CONST"));
    /*
      Recalculate loads of constant variables. MyISAM also sets things
      directly on the table share object.

      Check whether this should be fixed since handlers should not
      change things directly on the table object.

      Monty comment: This should NOT be changed!  It's the handlers
      responsibility to correct table->s->keys_xxxx information if keys
      have been disabled.

      The most important parameters set here is records per key on
      all indexes. block_size and primar key ref_length.

      For each index there is an array of rec_per_key.
      As an example if we have an index with three attributes a,b and c
      we will have an array of 3 rec_per_key.
      rec_per_key[0] is an estimate of number of records divided by
      number of unique values of the field a.
      rec_per_key[1] is an estimate of the number of records divided
      by the number of unique combinations of the fields a and b.
      rec_per_key[2] is an estimate of the number of records divided
      by the number of unique combinations of the fields a,b and c.

      Many handlers only set the value of rec_per_key when all fields
      are bound (rec_per_key[2] in the example above).

      If the handler doesn't support statistics, it should set all of the
      above to 0.

5315 5316 5317
      We first scans through all partitions to get the one holding most rows.
      We will then allow the handler with the most rows to set
      the rec_per_key and use this as an estimate on the total table.
5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329

      max_data_file_length:     Maximum data file length
      We ignore it, is only used in
      SHOW TABLE STATUS
      max_index_file_length:    Maximum index file length
      We ignore it since it is never used
      block_size:               Block size used
      We set it to the value of the first handler
      ref_length:               We set this to the value calculated
      and stored in local object
      create_time:              Creation time of table

5330 5331
      So we calculate these constants by using the variables from the
      handler with most rows.
5332
    */
5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345
    handler *file, **file_array;
    ulonglong max_records= 0;
    uint32 i= 0;
    uint32 handler_instance= 0;

    file_array= m_file;
    do
    {
      file= *file_array;
      /* Get variables if not already done */
      if (!(flag & HA_STATUS_VARIABLE) ||
          !bitmap_is_set(&(m_part_info->used_partitions),
                         (file_array - m_file)))
5346
        file->info(HA_STATUS_VARIABLE | no_lock_flag | extra_var_flag);
5347 5348 5349 5350 5351 5352 5353
      if (file->stats.records > max_records)
      {
        max_records= file->stats.records;
        handler_instance= i;
      }
      i++;
    } while (*(++file_array));
5354

5355
    file= m_file[handler_instance];
5356
    file->info(HA_STATUS_CONST | no_lock_flag);
5357
    stats.block_size= file->stats.block_size;
5358
    stats.create_time= file->stats.create_time;
5359 5360 5361 5362 5363 5364 5365 5366 5367 5368
    ref_length= m_ref_length;
  }
  if (flag & HA_STATUS_ERRKEY)
  {
    handler *file= m_file[m_last_part];
    DBUG_PRINT("info", ("info: HA_STATUS_ERRKEY"));
    /*
      This flag is used to get index number of the unique index that
      reported duplicate key
      We will report the errkey on the last handler used and ignore the rest
5369
      Note: all engines does not support HA_STATUS_ERRKEY, so set errkey.
5370
    */
5371
    file->errkey= errkey;
5372
    file->info(HA_STATUS_ERRKEY | no_lock_flag);
5373
    errkey= file->errkey;
5374 5375 5376
  }
  if (flag & HA_STATUS_TIME)
  {
5377
    handler *file, **file_array;
5378 5379 5380 5381 5382 5383
    DBUG_PRINT("info", ("info: HA_STATUS_TIME"));
    /*
      This flag is used to set the latest update time of the table.
      Used by SHOW commands
      We will report the maximum of these times
    */
5384
    stats.update_time= 0;
5385 5386 5387 5388
    file_array= m_file;
    do
    {
      file= *file_array;
5389
      file->info(HA_STATUS_TIME | no_lock_flag);
5390 5391
      if (file->stats.update_time > stats.update_time)
	stats.update_time= file->stats.update_time;
5392 5393
    } while (*(++file_array));
  }
5394
  DBUG_RETURN(0);
5395 5396 5397
}


5398
void ha_partition::get_dynamic_partition_info(PARTITION_STATS *stat_info,
5399 5400 5401 5402
                                              uint part_id)
{
  handler *file= m_file[part_id];
  file->info(HA_STATUS_CONST | HA_STATUS_TIME | HA_STATUS_VARIABLE |
5403
             HA_STATUS_VARIABLE_EXTRA | HA_STATUS_NO_LOCK);
5404

5405 5406 5407 5408 5409 5410 5411 5412 5413
  stat_info->records=              file->stats.records;
  stat_info->mean_rec_length=      file->stats.mean_rec_length;
  stat_info->data_file_length=     file->stats.data_file_length;
  stat_info->max_data_file_length= file->stats.max_data_file_length;
  stat_info->index_file_length=    file->stats.index_file_length;
  stat_info->delete_length=        file->stats.delete_length;
  stat_info->create_time=          file->stats.create_time;
  stat_info->update_time=          file->stats.update_time;
  stat_info->check_time=           file->stats.check_time;
5414
  stat_info->check_sum= 0;
5415
  if (file->ha_table_flags() & HA_HAS_CHECKSUM)
5416 5417 5418 5419 5420
    stat_info->check_sum= file->checksum();
  return;
}


Konstantin Osipov's avatar
Konstantin Osipov committed
5421 5422
/**
  General function to prepare handler for certain behavior.
unknown's avatar
unknown committed
5423

Konstantin Osipov's avatar
Konstantin Osipov committed
5424
  @param[in]    operation       operation to execute
unknown's avatar
unknown committed
5425 5426
    operation              Operation type for extra call

Konstantin Osipov's avatar
Konstantin Osipov committed
5427 5428 5429 5430 5431
  @return       status
    @retval     0               success
    @retval     >0              error code

  @detail
unknown's avatar
unknown committed
5432

5433 5434 5435 5436
  extra() is called whenever the server wishes to send a hint to
  the storage engine. The MyISAM engine implements the most hints.

  We divide the parameters into the following categories:
Konstantin Osipov's avatar
Konstantin Osipov committed
5437 5438 5439 5440 5441 5442 5443 5444 5445
  1) Operations used by most handlers
  2) Operations used by some non-MyISAM handlers
  3) Operations used only by MyISAM
  4) Operations only used by temporary tables for query processing
  5) Operations only used by MyISAM internally
  6) Operations not used at all
  7) Operations only used by federated tables for query processing
  8) Operations only used by NDB
  9) Operations only used by MERGE
5446 5447 5448

  The partition handler need to handle category 1), 2) and 3).

Konstantin Osipov's avatar
Konstantin Osipov committed
5449
  1) Operations used by most handlers
5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462
  -----------------------------------
  HA_EXTRA_RESET:
    This option is used by most handlers and it resets the handler state
    to the same state as after an open call. This includes releasing
    any READ CACHE or WRITE CACHE or other internal buffer used.

    It is called from the reset method in the handler interface. There are
    three instances where this is called.
    1) After completing a INSERT ... SELECT ... query the handler for the
       table inserted into is reset
    2) It is called from close_thread_table which in turn is called from
       close_thread_tables except in the case where the tables are locked
       in which case ha_commit_stmt is called instead.
5463
       It is only called from here if refresh_version hasn't changed and the
5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483
       table is not an old table when calling close_thread_table.
       close_thread_tables is called from many places as a general clean up
       function after completing a query.
    3) It is called when deleting the QUICK_RANGE_SELECT object if the
       QUICK_RANGE_SELECT object had its own handler object. It is called
       immediatley before close of this local handler object.
  HA_EXTRA_KEYREAD:
  HA_EXTRA_NO_KEYREAD:
    These parameters are used to provide an optimisation hint to the handler.
    If HA_EXTRA_KEYREAD is set it is enough to read the index fields, for
    many handlers this means that the index-only scans can be used and it
    is not necessary to use the real records to satisfy this part of the
    query. Index-only scans is a very important optimisation for disk-based
    indexes. For main-memory indexes most indexes contain a reference to the
    record and thus KEYREAD only says that it is enough to read key fields.
    HA_EXTRA_NO_KEYREAD disables this for the handler, also HA_EXTRA_RESET
    will disable this option.
    The handler will set HA_KEYREAD_ONLY in its table flags to indicate this
    feature is supported.
  HA_EXTRA_FLUSH:
5484
    Indication to flush tables to disk, is supposed to be used to
5485
    ensure disk based tables are flushed at end of query execution.
5486
    Currently is never used.
5487

Konstantin Osipov's avatar
Konstantin Osipov committed
5488
  2) Operations used by some non-MyISAM handlers
5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506
  ----------------------------------------------
  HA_EXTRA_KEYREAD_PRESERVE_FIELDS:
    This is a strictly InnoDB feature that is more or less undocumented.
    When it is activated InnoDB copies field by field from its fetch
    cache instead of all fields in one memcpy. Have no idea what the
    purpose of this is.
    Cut from include/my_base.h:
    When using HA_EXTRA_KEYREAD, overwrite only key member fields and keep
    other fields intact. When this is off (by default) InnoDB will use memcpy
    to overwrite entire row.
  HA_EXTRA_IGNORE_DUP_KEY:
  HA_EXTRA_NO_IGNORE_DUP_KEY:
    Informs the handler to we will not stop the transaction if we get an
    duplicate key errors during insert/upate.
    Always called in pair, triggered by INSERT IGNORE and other similar
    SQL constructs.
    Not used by MyISAM.

Konstantin Osipov's avatar
Konstantin Osipov committed
5507
  3) Operations used only by MyISAM
5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588
  ---------------------------------
  HA_EXTRA_NORMAL:
    Only used in MyISAM to reset quick mode, not implemented by any other
    handler. Quick mode is also reset in MyISAM by HA_EXTRA_RESET.

    It is called after completing a successful DELETE query if the QUICK
    option is set.

  HA_EXTRA_QUICK:
    When the user does DELETE QUICK FROM table where-clause; this extra
    option is called before the delete query is performed and
    HA_EXTRA_NORMAL is called after the delete query is completed.
    Temporary tables used internally in MySQL always set this option

    The meaning of quick mode is that when deleting in a B-tree no merging
    of leafs is performed. This is a common method and many large DBMS's
    actually only support this quick mode since it is very difficult to
    merge leaves in a tree used by many threads concurrently.

  HA_EXTRA_CACHE:
    This flag is usually set with extra_opt along with a cache size.
    The size of this buffer is set by the user variable
    record_buffer_size. The value of this cache size is the amount of
    data read from disk in each fetch when performing a table scan.
    This means that before scanning a table it is normal to call
    extra with HA_EXTRA_CACHE and when the scan is completed to call
    HA_EXTRA_NO_CACHE to release the cache memory.

    Some special care is taken when using this extra parameter since there
    could be a write ongoing on the table in the same statement. In this
    one has to take special care since there might be a WRITE CACHE as
    well. HA_EXTRA_CACHE specifies using a READ CACHE and using
    READ CACHE and WRITE CACHE at the same time is not possible.

    Only MyISAM currently use this option.

    It is set when doing full table scans using rr_sequential and
    reset when completing such a scan with end_read_record
    (resetting means calling extra with HA_EXTRA_NO_CACHE).

    It is set in filesort.cc for MyISAM internal tables and it is set in
    a multi-update where HA_EXTRA_CACHE is called on a temporary result
    table and after that ha_rnd_init(0) on table to be updated
    and immediately after that HA_EXTRA_NO_CACHE on table to be updated.

    Apart from that it is always used from init_read_record but not when
    used from UPDATE statements. It is not used from DELETE statements
    with ORDER BY and LIMIT but it is used in normal scan loop in DELETE
    statements. The reason here is that DELETE's in MyISAM doesn't move
    existings data rows.

    It is also set in copy_data_between_tables when scanning the old table
    to copy over to the new table.
    And it is set in join_init_read_record where quick objects are used
    to perform a scan on the table. In this case the full table scan can
    even be performed multiple times as part of the nested loop join.

    For purposes of the partition handler it is obviously necessary to have
    special treatment of this extra call. If we would simply pass this
    extra call down to each handler we would allocate
    cache size * no of partitions amount of memory and this is not
    necessary since we will only scan one partition at a time when doing
    full table scans.

    Thus we treat it by first checking whether we have MyISAM handlers in
    the table, if not we simply ignore the call and if we have we will
    record the call but will not call any underlying handler yet. Then
    when performing the sequential scan we will check this recorded value
    and call extra_opt whenever we start scanning a new partition.

  HA_EXTRA_NO_CACHE:
    When performing a UNION SELECT HA_EXTRA_NO_CACHE is called from the
    flush method in the select_union class.
    It is used to some extent when insert delayed inserts.
    See HA_EXTRA_RESET_STATE for use in conjunction with delete_all_rows().

    It should be ok to call HA_EXTRA_NO_CACHE on all underlying handlers
    if they are MyISAM handlers. Other handlers we can ignore the call
    for. If no cache is in use they will quickly return after finding
    this out. And we also ensure that all caches are disabled and no one
    is left by mistake.
5589
    In the future this call will probably be deleted and we will instead call
5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600
    ::reset();

  HA_EXTRA_WRITE_CACHE:
    See above, called from various places. It is mostly used when we
    do INSERT ... SELECT
    No special handling to save cache space is developed currently.

  HA_EXTRA_PREPARE_FOR_UPDATE:
    This is called as part of a multi-table update. When the table to be
    updated is also scanned then this informs MyISAM handler to drop any
    caches if dynamic records are used (fixed size records do not care
5601 5602 5603
    about this call). We pass this along to the first partition to scan, and
    flag that it is to be called after HA_EXTRA_CACHE when moving to the next
    partition to scan.
5604

5605
  HA_EXTRA_PREPARE_FOR_DROP:
5606 5607 5608 5609
    Only used by MyISAM, called in preparation for a DROP TABLE.
    It's used mostly by Windows that cannot handle dropping an open file.
    On other platforms it has the same effect as HA_EXTRA_FORCE_REOPEN.

5610 5611 5612
  HA_EXTRA_PREPARE_FOR_RENAME:
    Informs the handler we are about to attempt a rename of the table.

5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635
  HA_EXTRA_READCHECK:
  HA_EXTRA_NO_READCHECK:
    Only one call to HA_EXTRA_NO_READCHECK from ha_open where it says that
    this is not needed in SQL. The reason for this call is that MyISAM sets
    the READ_CHECK_USED in the open call so the call is needed for MyISAM
    to reset this feature.
    The idea with this parameter was to inform of doing/not doing a read
    check before applying an update. Since SQL always performs a read before
    applying the update No Read Check is needed in MyISAM as well.

    This is a cut from Docs/myisam.txt
     Sometimes you might want to force an update without checking whether
     another user has changed the record since you last read it. This is
     somewhat dangerous, so it should ideally not be used. That can be
     accomplished by wrapping the mi_update() call in two calls to mi_extra(),
     using these functions:
     HA_EXTRA_NO_READCHECK=5                 No readcheck on update
     HA_EXTRA_READCHECK=6                    Use readcheck (def)

  HA_EXTRA_FORCE_REOPEN:
    Only used by MyISAM, called when altering table, closing tables to
    enforce a reopen of the table files.

Konstantin Osipov's avatar
Konstantin Osipov committed
5636
  4) Operations only used by temporary tables for query processing
5637 5638
  ----------------------------------------------------------------
  HA_EXTRA_RESET_STATE:
5639
    Same as reset() except that buffers are not released. If there is
5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666
    a READ CACHE it is reinit'ed. A cache is reinit'ed to restart reading
    or to change type of cache between READ CACHE and WRITE CACHE.

    This extra function is always called immediately before calling
    delete_all_rows on the handler for temporary tables.
    There are cases however when HA_EXTRA_RESET_STATE isn't called in
    a similar case for a temporary table in sql_union.cc and in two other
    cases HA_EXTRA_NO_CACHE is called before and HA_EXTRA_WRITE_CACHE
    called afterwards.
    The case with HA_EXTRA_NO_CACHE and HA_EXTRA_WRITE_CACHE means
    disable caching, delete all rows and enable WRITE CACHE. This is
    used for temporary tables containing distinct sums and a
    functional group.

    The only case that delete_all_rows is called on non-temporary tables
    is in sql_delete.cc when DELETE FROM table; is called by a user.
    In this case no special extra calls are performed before or after this
    call.

    The partition handler should not need to bother about this one. It
    should never be called.

  HA_EXTRA_NO_ROWS:
    Don't insert rows indication to HEAP and MyISAM, only used by temporary
    tables used in query processing.
    Not handled by partition handler.

Konstantin Osipov's avatar
Konstantin Osipov committed
5667
  5) Operations only used by MyISAM internally
5668 5669
  --------------------------------------------
  HA_EXTRA_REINIT_CACHE:
5670
    This call reinitializes the READ CACHE described above if there is one
5671 5672 5673 5674 5675 5676 5677
    and otherwise the call is ignored.

    We can thus safely call it on all underlying handlers if they are
    MyISAM handlers. It is however never called so we don't handle it at all.
  HA_EXTRA_FLUSH_CACHE:
    Flush WRITE CACHE in MyISAM. It is only from one place in the code.
    This is in sql_insert.cc where it is called if the table_flags doesn't
5678 5679 5680
    contain HA_DUPLICATE_POS. The only handler having the HA_DUPLICATE_POS
    set is the MyISAM handler and so the only handler not receiving this
    call is MyISAM.
5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701
    Thus in effect this call is called but never used. Could be removed
    from sql_insert.cc
  HA_EXTRA_NO_USER_CHANGE:
    Only used by MyISAM, never called.
    Simulates lock_type as locked.
  HA_EXTRA_WAIT_LOCK:
  HA_EXTRA_WAIT_NOLOCK:
    Only used by MyISAM, called from MyISAM handler but never from server
    code on top of the handler.
    Sets lock_wait on/off
  HA_EXTRA_NO_KEYS:
    Only used MyISAM, only used internally in MyISAM handler, never called
    from server level.
  HA_EXTRA_KEYREAD_CHANGE_POS:
  HA_EXTRA_REMEMBER_POS:
  HA_EXTRA_RESTORE_POS:
  HA_EXTRA_PRELOAD_BUFFER_SIZE:
  HA_EXTRA_CHANGE_KEY_TO_DUP:
  HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
    Only used by MyISAM, never called.

Konstantin Osipov's avatar
Konstantin Osipov committed
5702
  6) Operations not used at all
5703 5704 5705 5706
  -----------------------------
  HA_EXTRA_KEY_CACHE:
  HA_EXTRA_NO_KEY_CACHE:
    This parameters are no longer used and could be removed.
unknown's avatar
unknown committed
5707

Konstantin Osipov's avatar
Konstantin Osipov committed
5708
  7) Operations only used by federated tables for query processing
unknown's avatar
unknown committed
5709 5710 5711 5712
  ----------------------------------------------------------------
  HA_EXTRA_INSERT_WITH_UPDATE:
    Inform handler that an "INSERT...ON DUPLICATE KEY UPDATE" will be
    executed. This condition is unset by HA_EXTRA_NO_IGNORE_DUP_KEY.
5713

Konstantin Osipov's avatar
Konstantin Osipov committed
5714
  8) Operations only used by NDB
5715 5716 5717 5718 5719 5720 5721
  ------------------------------
  HA_EXTRA_DELETE_CANNOT_BATCH:
  HA_EXTRA_UPDATE_CANNOT_BATCH:
    Inform handler that delete_row()/update_row() cannot batch deletes/updates
    and should perform them immediately. This may be needed when table has 
    AFTER DELETE/UPDATE triggers which access to subject table.
    These flags are reset by the handler::extra(HA_EXTRA_RESET) call.
Konstantin Osipov's avatar
Konstantin Osipov committed
5722 5723 5724 5725 5726 5727 5728 5729

  9) Operations only used by MERGE
  ------------------------------
  HA_EXTRA_ADD_CHILDREN_LIST:
  HA_EXTRA_ATTACH_CHILDREN:
  HA_EXTRA_IS_ATTACHED_CHILDREN:
  HA_EXTRA_DETACH_CHILDREN:
    Special actions for MERGE tables. Ignore.
5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754
*/

int ha_partition::extra(enum ha_extra_function operation)
{
  DBUG_ENTER("ha_partition:extra");
  DBUG_PRINT("info", ("operation: %d", (int) operation));

  switch (operation) {
    /* Category 1), used by most handlers */
  case HA_EXTRA_KEYREAD:
  case HA_EXTRA_NO_KEYREAD:
  case HA_EXTRA_FLUSH:
    DBUG_RETURN(loop_extra(operation));

    /* Category 2), used by non-MyISAM handlers */
  case HA_EXTRA_IGNORE_DUP_KEY:
  case HA_EXTRA_NO_IGNORE_DUP_KEY:
  case HA_EXTRA_KEYREAD_PRESERVE_FIELDS:
  {
    if (!m_myisam)
      DBUG_RETURN(loop_extra(operation));
    break;
  }

  /* Category 3), used by MyISAM handlers */
5755 5756
  case HA_EXTRA_PREPARE_FOR_RENAME:
    DBUG_RETURN(prepare_for_rename());
5757
    break;
5758 5759 5760 5761 5762 5763 5764 5765
  case HA_EXTRA_PREPARE_FOR_UPDATE:
    /*
      Needs to be run on the first partition in the range now, and 
      later in late_extra_cache, when switching to a new partition to scan.
    */
    m_extra_prepare_for_update= TRUE;
    if (m_part_spec.start_part != NO_CURRENT_PART_ID)
    {
5766 5767
      if (!m_extra_cache)
        m_extra_cache_part_id= m_part_spec.start_part;
5768
      DBUG_ASSERT(m_extra_cache_part_id == m_part_spec.start_part);
Mattias Jonsson's avatar
merge  
Mattias Jonsson committed
5769
      (void) m_file[m_part_spec.start_part]->extra(HA_EXTRA_PREPARE_FOR_UPDATE);
5770 5771
    }
    break;
5772 5773 5774
  case HA_EXTRA_NORMAL:
  case HA_EXTRA_QUICK:
  case HA_EXTRA_FORCE_REOPEN:
5775
  case HA_EXTRA_PREPARE_FOR_DROP:
5776
  case HA_EXTRA_FLUSH_CACHE:
5777 5778 5779 5780 5781
  {
    if (m_myisam)
      DBUG_RETURN(loop_extra(operation));
    break;
  }
5782 5783 5784 5785 5786 5787 5788 5789
  case HA_EXTRA_NO_READCHECK:
  {
    /*
      This is only done as a part of ha_open, which is also used in
      ha_partition::open, so no need to do anything.
    */
    break;
  }
5790 5791 5792 5793 5794 5795
  case HA_EXTRA_CACHE:
  {
    prepare_extra_cache(0);
    break;
  }
  case HA_EXTRA_NO_CACHE:
5796 5797 5798 5799 5800 5801 5802 5803 5804 5805
  {
    int ret= 0;
    if (m_extra_cache_part_id != NO_CURRENT_PART_ID)
      ret= m_file[m_extra_cache_part_id]->extra(HA_EXTRA_NO_CACHE);
    m_extra_cache= FALSE;
    m_extra_cache_size= 0;
    m_extra_prepare_for_update= FALSE;
    m_extra_cache_part_id= NO_CURRENT_PART_ID;
    DBUG_RETURN(ret);
  }
5806
  case HA_EXTRA_WRITE_CACHE:
5807 5808 5809
  {
    m_extra_cache= FALSE;
    m_extra_cache_size= 0;
5810
    m_extra_prepare_for_update= FALSE;
5811
    m_extra_cache_part_id= NO_CURRENT_PART_ID;
5812 5813
    DBUG_RETURN(loop_extra(operation));
  }
5814 5815 5816 5817 5818 5819 5820 5821 5822
  case HA_EXTRA_IGNORE_NO_KEY:
  case HA_EXTRA_NO_IGNORE_NO_KEY:
  {
    /*
      Ignore as these are specific to NDB for handling
      idempotency
     */
    break;
  }
5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843
  case HA_EXTRA_WRITE_CAN_REPLACE:
  case HA_EXTRA_WRITE_CANNOT_REPLACE:
  {
    /*
      Informs handler that write_row() can replace rows which conflict
      with row being inserted by PK/unique key without reporting error
      to the SQL-layer.

      This optimization is not safe for partitioned table in general case
      since we may have to put new version of row into partition which is
      different from partition in which old version resides (for example
      when we partition by non-PK column or by some column which is not
      part of unique key which were violated).
      And since NDB which is the only engine at the moment that supports
      this optimization handles partitioning on its own we simple disable
      it here. (BTW for NDB this optimization is safe since it supports
      only KEY partitioning and won't use this optimization for tables
      which have additional unique constraints).
    */
    break;
  }
unknown's avatar
unknown committed
5844 5845 5846
    /* Category 7), used by federated handlers */
  case HA_EXTRA_INSERT_WITH_UPDATE:
    DBUG_RETURN(loop_extra(operation));
Konstantin Osipov's avatar
Konstantin Osipov committed
5847
    /* Category 8) Operations only used by NDB */
5848 5849 5850 5851 5852
  case HA_EXTRA_DELETE_CANNOT_BATCH:
  case HA_EXTRA_UPDATE_CANNOT_BATCH:
  {
    /* Currently only NDB use the *_CANNOT_BATCH */
    break;
Konstantin Osipov's avatar
Konstantin Osipov committed
5853 5854 5855 5856 5857 5858 5859 5860 5861
  }
    /* Category 9) Operations only used by MERGE */
  case HA_EXTRA_ADD_CHILDREN_LIST:
  case HA_EXTRA_ATTACH_CHILDREN:
  case HA_EXTRA_IS_ATTACHED_CHILDREN:
  case HA_EXTRA_DETACH_CHILDREN:
  {
    /* Special actions for MERGE tables. Ignore. */
    break;
5862
  }
5863 5864 5865 5866 5867 5868 5869
  /*
    http://dev.mysql.com/doc/refman/5.1/en/partitioning-limitations.html
    says we no longer support logging to partitioned tables, so we fail
    here.
  */
  case HA_EXTRA_MARK_AS_LOG_TABLE:
    DBUG_RETURN(ER_UNSUPORTED_LOG_ENGINE);
5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881
  default:
  {
    /* Temporary crash to discover what is wrong */
    DBUG_ASSERT(0);
    break;
  }
  }
  DBUG_RETURN(0);
}


/*
unknown's avatar
unknown committed
5882 5883 5884 5885 5886 5887 5888 5889 5890 5891
  Special extra call to reset extra parameters

  SYNOPSIS
    reset()

  RETURN VALUE
    >0                   Error code
    0                    Success

  DESCRIPTION
5892
    Called at end of each statement to reste buffers
5893 5894 5895 5896 5897 5898 5899
*/

int ha_partition::reset(void)
{
  int result= 0, tmp;
  handler **file;
  DBUG_ENTER("ha_partition::reset");
unknown's avatar
unknown committed
5900
  if (m_part_info)
unknown's avatar
unknown committed
5901 5902
    bitmap_set_all(&m_part_info->used_partitions);
  file= m_file;
5903 5904
  do
  {
5905
    if ((tmp= (*file)->ha_reset()))
5906 5907 5908 5909 5910
      result= tmp;
  } while (*(++file));
  DBUG_RETURN(result);
}

unknown's avatar
unknown committed
5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923
/*
  Special extra method for HA_EXTRA_CACHE with cachesize as extra parameter

  SYNOPSIS
    extra_opt()
    operation                      Must be HA_EXTRA_CACHE
    cachesize                      Size of cache in full table scan

  RETURN VALUE
    >0                   Error code
    0                    Success
*/

5924 5925 5926
int ha_partition::extra_opt(enum ha_extra_function operation, ulong cachesize)
{
  DBUG_ENTER("ha_partition::extra_opt()");
unknown's avatar
unknown committed
5927

5928 5929 5930 5931 5932 5933
  DBUG_ASSERT(HA_EXTRA_CACHE == operation);
  prepare_extra_cache(cachesize);
  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944
/*
  Call extra on handler with HA_EXTRA_CACHE and cachesize

  SYNOPSIS
    prepare_extra_cache()
    cachesize                Size of cache for full table scan

  RETURN VALUE
    NONE
*/

5945 5946 5947
void ha_partition::prepare_extra_cache(uint cachesize)
{
  DBUG_ENTER("ha_partition::prepare_extra_cache()");
5948
  DBUG_PRINT("info", ("cachesize %u", cachesize));
5949 5950 5951 5952 5953

  m_extra_cache= TRUE;
  m_extra_cache_size= cachesize;
  if (m_part_spec.start_part != NO_CURRENT_PART_ID)
  {
unknown's avatar
unknown committed
5954
    late_extra_cache(m_part_spec.start_part);
5955 5956 5957 5958 5959
  }
  DBUG_VOID_RETURN;
}


5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970
/*
  Prepares our new and reorged handlers for rename or delete

  SYNOPSIS
    prepare_for_delete()

  RETURN VALUE
    >0                    Error code
    0                     Success
*/

5971
int ha_partition::prepare_for_rename()
5972 5973 5974
{
  int result= 0, tmp;
  handler **file;
5975
  DBUG_ENTER("ha_partition::prepare_for_rename()");
5976 5977 5978 5979
  
  if (m_new_file != NULL)
  {
    for (file= m_new_file; *file; file++)
5980
      if ((tmp= (*file)->extra(HA_EXTRA_PREPARE_FOR_RENAME)))
5981 5982
        result= tmp;      
    for (file= m_reorged_file; *file; file++)
5983
      if ((tmp= (*file)->extra(HA_EXTRA_PREPARE_FOR_RENAME)))
unknown's avatar
unknown committed
5984 5985
        result= tmp;   
    DBUG_RETURN(result);   
5986
  }
unknown's avatar
unknown committed
5987
  
5988
  DBUG_RETURN(loop_extra(HA_EXTRA_PREPARE_FOR_RENAME));
5989 5990
}

unknown's avatar
unknown committed
5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002
/*
  Call extra on all partitions

  SYNOPSIS
    loop_extra()
    operation             extra operation type

  RETURN VALUE
    >0                    Error code
    0                     Success
*/

6003 6004 6005 6006
int ha_partition::loop_extra(enum ha_extra_function operation)
{
  int result= 0, tmp;
  handler **file;
6007
  bool is_select;
6008
  DBUG_ENTER("ha_partition::loop_extra()");
6009
  
6010
  is_select= (thd_sql_command(ha_thd()) == SQLCOM_SELECT);
6011 6012
  for (file= m_file; *file; file++)
  {
6013 6014 6015 6016 6017 6018
    if (!is_select ||
        bitmap_is_set(&(m_part_info->used_partitions), file - m_file))
    {
      if ((tmp= (*file)->extra(operation)))
        result= tmp;
    }
6019 6020 6021 6022 6023
  }
  DBUG_RETURN(result);
}


unknown's avatar
unknown committed
6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034
/*
  Call extra(HA_EXTRA_CACHE) on next partition_id

  SYNOPSIS
    late_extra_cache()
    partition_id               Partition id to call extra on

  RETURN VALUE
    NONE
*/

6035 6036 6037 6038
void ha_partition::late_extra_cache(uint partition_id)
{
  handler *file;
  DBUG_ENTER("ha_partition::late_extra_cache");
6039 6040
  DBUG_PRINT("info", ("extra_cache %u prepare %u partid %u size %u",
                      m_extra_cache, m_extra_prepare_for_update,
6041
                      partition_id, m_extra_cache_size));
unknown's avatar
unknown committed
6042

6043
  if (!m_extra_cache && !m_extra_prepare_for_update)
6044 6045
    DBUG_VOID_RETURN;
  file= m_file[partition_id];
6046 6047 6048
  if (m_extra_cache)
  {
    if (m_extra_cache_size == 0)
Mattias Jonsson's avatar
merge  
Mattias Jonsson committed
6049
      (void) file->extra(HA_EXTRA_CACHE);
6050
    else
Mattias Jonsson's avatar
merge  
Mattias Jonsson committed
6051
      (void) file->extra_opt(HA_EXTRA_CACHE, m_extra_cache_size);
6052
  }
6053 6054
  if (m_extra_prepare_for_update)
  {
Mattias Jonsson's avatar
merge  
Mattias Jonsson committed
6055
    (void) file->extra(HA_EXTRA_PREPARE_FOR_UPDATE);
6056
  }
6057
  m_extra_cache_part_id= partition_id;
6058 6059 6060 6061
  DBUG_VOID_RETURN;
}


unknown's avatar
unknown committed
6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072
/*
  Call extra(HA_EXTRA_NO_CACHE) on next partition_id

  SYNOPSIS
    late_extra_no_cache()
    partition_id               Partition id to call extra on

  RETURN VALUE
    NONE
*/

6073 6074 6075 6076
void ha_partition::late_extra_no_cache(uint partition_id)
{
  handler *file;
  DBUG_ENTER("ha_partition::late_extra_no_cache");
unknown's avatar
unknown committed
6077

6078
  if (!m_extra_cache && !m_extra_prepare_for_update)
6079 6080
    DBUG_VOID_RETURN;
  file= m_file[partition_id];
Konstantin Osipov's avatar
Konstantin Osipov committed
6081
  (void) file->extra(HA_EXTRA_NO_CACHE);
6082 6083
  DBUG_ASSERT(partition_id == m_extra_cache_part_id);
  m_extra_cache_part_id= NO_CURRENT_PART_ID;
6084 6085 6086 6087 6088 6089 6090 6091
  DBUG_VOID_RETURN;
}


/****************************************************************************
                MODULE optimiser support
****************************************************************************/

unknown's avatar
unknown committed
6092 6093 6094 6095 6096 6097 6098 6099 6100 6101
/*
  Get keys to use for scanning

  SYNOPSIS
    keys_to_use_for_scanning()

  RETURN VALUE
    key_map of keys usable for scanning
*/

6102 6103 6104
const key_map *ha_partition::keys_to_use_for_scanning()
{
  DBUG_ENTER("ha_partition::keys_to_use_for_scanning");
unknown's avatar
unknown committed
6105

6106 6107 6108
  DBUG_RETURN(m_file[0]->keys_to_use_for_scanning());
}

6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125
#define MAX_PARTS_FOR_OPTIMIZER_CALLS 10
/*
  Prepare start variables for estimating optimizer costs.

  @param[out] num_used_parts  Number of partitions after pruning.
  @param[out] check_min_num   Number of partitions to call.
  @param[out] first           first used partition.
*/
void ha_partition::partitions_optimizer_call_preparations(uint *first,
                                                          uint *num_used_parts,
                                                          uint *check_min_num)
{
  *first= bitmap_get_first_set(&(m_part_info->used_partitions));
  *num_used_parts= bitmap_bits_set(&(m_part_info->used_partitions));
  *check_min_num= min(MAX_PARTS_FOR_OPTIMIZER_CALLS, *num_used_parts);
}

unknown's avatar
unknown committed
6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136

/*
  Return time for a scan of the table

  SYNOPSIS
    scan_time()

  RETURN VALUE
    time for scan
*/

6137 6138
double ha_partition::scan_time()
{
6139 6140
  double scan_time= 0.0;
  uint first, part_id, num_used_parts, check_min_num, partitions_called= 0;
6141 6142
  DBUG_ENTER("ha_partition::scan_time");

6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155
  partitions_optimizer_call_preparations(&first, &num_used_parts, &check_min_num);
  for (part_id= first; partitions_called < num_used_parts ; part_id++)
  {
    if (!bitmap_is_set(&(m_part_info->used_partitions), part_id))
      continue;
    scan_time+= m_file[part_id]->scan_time();
    partitions_called++;
    if (partitions_called >= check_min_num && scan_time != 0.0)
    {
      DBUG_RETURN(scan_time *
                      (double) num_used_parts / (double) partitions_called);
    }
  }
6156 6157 6158 6159 6160
  DBUG_RETURN(scan_time);
}


/*
6161
  Estimate rows for records_in_range or estimate_rows_upper_bound.
unknown's avatar
unknown committed
6162

6163 6164 6165 6166 6167
  @param is_records_in_range  call records_in_range instead of
                              estimate_rows_upper_bound.
  @param inx                  (only for records_in_range) index to use.
  @param min_key              (only for records_in_range) start of range.
  @param max_key              (only for records_in_range) end of range.
unknown's avatar
unknown committed
6168

6169
  @return Number of rows or HA_POS_ERROR.
6170
*/
6171 6172
ha_rows ha_partition::estimate_rows(bool is_records_in_range, uint inx,
                                    key_range *min_key, key_range *max_key)
6173
{
6174 6175 6176
  ha_rows rows, estimated_rows= 0;
  uint first, part_id, num_used_parts, check_min_num, partitions_called= 0;
  DBUG_ENTER("ha_partition::records_in_range");
unknown's avatar
unknown committed
6177

6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196
  partitions_optimizer_call_preparations(&first, &num_used_parts, &check_min_num);
  for (part_id= first; partitions_called < num_used_parts ; part_id++)
  {
    if (!bitmap_is_set(&(m_part_info->used_partitions), part_id))
      continue;
    if (is_records_in_range)
      rows= m_file[part_id]->records_in_range(inx, min_key, max_key);
    else
      rows= m_file[part_id]->estimate_rows_upper_bound();
    if (rows == HA_POS_ERROR)
      DBUG_RETURN(HA_POS_ERROR);
    estimated_rows+= rows;
    partitions_called++;
    if (partitions_called >= check_min_num && estimated_rows)
    {
      DBUG_RETURN(estimated_rows * num_used_parts / partitions_called);
    }
  }
  DBUG_RETURN(estimated_rows);
6197 6198
}

6199

6200
/*
unknown's avatar
unknown committed
6201 6202 6203 6204 6205 6206 6207 6208 6209 6210
  Find number of records in a range

  SYNOPSIS
    records_in_range()
    inx                  Index number
    min_key              Start of range
    max_key              End of range

  RETURN VALUE
    Number of rows in range
6211

unknown's avatar
unknown committed
6212 6213 6214 6215
  DESCRIPTION
    Given a starting key, and an ending key estimate the number of rows that
    will exist between the two. end_key may be empty which in case determine
    if start_key matches any rows.
6216

unknown's avatar
unknown committed
6217 6218 6219 6220 6221
    Called from opt_range.cc by check_quick_keys().

    monty: MUST be called for each range and added.
          Note that MySQL will assume that if this returns 0 there is no
          matching rows for the range!
6222 6223 6224 6225 6226 6227 6228
*/

ha_rows ha_partition::records_in_range(uint inx, key_range *min_key,
				       key_range *max_key)
{
  DBUG_ENTER("ha_partition::records_in_range");

6229
  DBUG_RETURN(estimate_rows(TRUE, inx, min_key, max_key));
6230 6231 6232
}


unknown's avatar
unknown committed
6233 6234 6235 6236 6237 6238 6239 6240 6241 6242
/*
  Estimate upper bound of number of rows

  SYNOPSIS
    estimate_rows_upper_bound()

  RETURN VALUE
    Number of rows
*/

6243 6244 6245 6246
ha_rows ha_partition::estimate_rows_upper_bound()
{
  DBUG_ENTER("ha_partition::estimate_rows_upper_bound");

6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274
  DBUG_RETURN(estimate_rows(FALSE, 0, NULL, NULL));
}


/*
  Get time to read

  SYNOPSIS
    read_time()
    index                Index number used
    ranges               Number of ranges
    rows                 Number of rows

  RETURN VALUE
    time for read

  DESCRIPTION
    This will be optimised later to include whether or not the index can
    be used with partitioning. To achieve we need to add another parameter
    that specifies how many of the index fields that are bound in the ranges.
    Possibly added as a new call to handlers.
*/

double ha_partition::read_time(uint index, uint ranges, ha_rows rows)
{
  DBUG_ENTER("ha_partition::read_time");

  DBUG_RETURN(m_file[0]->read_time(index, ranges, rows));
6275 6276 6277
}


6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305
/**
  Number of rows in table. see handler.h

  SYNOPSIS
    records()

  RETURN VALUE
    Number of total rows in a partitioned table.
*/

ha_rows ha_partition::records()
{
  ha_rows rows, tot_rows= 0;
  handler **file;
  DBUG_ENTER("ha_partition::records");

  file= m_file;
  do
  {
    rows= (*file)->records();
    if (rows == HA_POS_ERROR)
      DBUG_RETURN(HA_POS_ERROR);
    tot_rows+= rows;
  } while (*(++file));
  DBUG_RETURN(tot_rows);
}


unknown's avatar
unknown committed
6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343
/*
  Is it ok to switch to a new engine for this table

  SYNOPSIS
    can_switch_engine()

  RETURN VALUE
    TRUE                  Ok
    FALSE                 Not ok

  DESCRIPTION
    Used to ensure that tables with foreign key constraints are not moved
    to engines without foreign key support.
*/

bool ha_partition::can_switch_engines()
{
  handler **file;
  DBUG_ENTER("ha_partition::can_switch_engines");
 
  file= m_file;
  do
  {
    if (!(*file)->can_switch_engines())
      DBUG_RETURN(FALSE);
  } while (*(++file));
  DBUG_RETURN(TRUE);
}


/*
  Is table cache supported

  SYNOPSIS
    table_cache_type()

*/

6344 6345 6346
uint8 ha_partition::table_cache_type()
{
  DBUG_ENTER("ha_partition::table_cache_type");
unknown's avatar
unknown committed
6347

6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358
  DBUG_RETURN(m_file[0]->table_cache_type());
}


/****************************************************************************
                MODULE print messages
****************************************************************************/

const char *ha_partition::index_type(uint inx)
{
  DBUG_ENTER("ha_partition::index_type");
unknown's avatar
unknown committed
6359

6360 6361 6362 6363
  DBUG_RETURN(m_file[0]->index_type(inx));
}


6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379
enum row_type ha_partition::get_row_type() const
{
  handler **file;
  enum row_type type= (*m_file)->get_row_type();

  for (file= m_file, file++; *file; file++)
  {
    enum row_type part_type= (*file)->get_row_type();
    if (part_type != type)
      return ROW_TYPE_NOT_USED;
  }

  return type;
}


6380 6381
void ha_partition::print_error(int error, myf errflag)
{
6382
  THD *thd= ha_thd();
6383
  DBUG_ENTER("ha_partition::print_error");
unknown's avatar
unknown committed
6384

6385
  /* Should probably look for my own errors first */
6386
  DBUG_PRINT("enter", ("error: %d", error));
unknown's avatar
unknown committed
6387

6388 6389
  if ((error == HA_ERR_NO_PARTITION_FOUND) &&
      ! (thd->lex->alter_info.flags & ALTER_TRUNCATE_PARTITION))
unknown's avatar
unknown committed
6390
    m_part_info->print_no_partition_found(table);
6391
  else
6392 6393 6394
  {
    /* In case m_file has not been initialized, like in bug#42438 */
    if (m_file)
6395 6396 6397 6398 6399 6400
    {
      if (m_last_part >= m_tot_parts)
      {
        DBUG_ASSERT(0);
        m_last_part= 0;
      }
6401
      m_file[m_last_part]->print_error(error, errflag);
6402
    }
6403 6404 6405
    else
      handler::print_error(error, errflag);
  }
6406 6407 6408 6409 6410 6411 6412
  DBUG_VOID_RETURN;
}


bool ha_partition::get_error_message(int error, String *buf)
{
  DBUG_ENTER("ha_partition::get_error_message");
unknown's avatar
unknown committed
6413

6414
  /* Should probably look for my own errors first */
6415 6416 6417 6418 6419 6420

  /* In case m_file has not been initialized, like in bug#42438 */
  if (m_file)
    DBUG_RETURN(m_file[m_last_part]->get_error_message(error, buf));
  DBUG_RETURN(handler::get_error_message(error, buf));

6421 6422 6423 6424 6425 6426
}


/****************************************************************************
                MODULE handler characteristics
****************************************************************************/
6427 6428 6429 6430 6431 6432
/**
  alter_table_flags must be on handler/table level, not on hton level
  due to the ha_partition hton does not know what the underlying hton is.
*/
uint ha_partition::alter_table_flags(uint flags)
{
6433
  uint flags_to_return, flags_to_check;
6434
  DBUG_ENTER("ha_partition::alter_table_flags");
6435 6436 6437 6438 6439 6440 6441 6442 6443

  flags_to_return= ht->alter_table_flags(flags);
  flags_to_return|= m_file[0]->alter_table_flags(flags); 

  /*
    If one partition fails we must be able to revert the change for the other,
    already altered, partitions. So both ADD and DROP can only be supported in
    pairs.
  */
6444 6445
  flags_to_check= HA_INPLACE_ADD_INDEX_NO_READ_WRITE;
  flags_to_check|= HA_INPLACE_DROP_INDEX_NO_READ_WRITE;
6446 6447
  if ((flags_to_return & flags_to_check) != flags_to_check)
    flags_to_return&= ~flags_to_check;
6448 6449
  flags_to_check= HA_INPLACE_ADD_UNIQUE_INDEX_NO_READ_WRITE;
  flags_to_check|= HA_INPLACE_DROP_UNIQUE_INDEX_NO_READ_WRITE;
6450 6451
  if ((flags_to_return & flags_to_check) != flags_to_check)
    flags_to_return&= ~flags_to_check;
6452 6453
  flags_to_check= HA_INPLACE_ADD_PK_INDEX_NO_READ_WRITE;
  flags_to_check|= HA_INPLACE_DROP_PK_INDEX_NO_READ_WRITE;
6454 6455
  if ((flags_to_return & flags_to_check) != flags_to_check)
    flags_to_return&= ~flags_to_check;
6456 6457
  flags_to_check= HA_INPLACE_ADD_INDEX_NO_WRITE;
  flags_to_check|= HA_INPLACE_DROP_INDEX_NO_WRITE;
6458 6459
  if ((flags_to_return & flags_to_check) != flags_to_check)
    flags_to_return&= ~flags_to_check;
6460 6461
  flags_to_check= HA_INPLACE_ADD_UNIQUE_INDEX_NO_WRITE;
  flags_to_check|= HA_INPLACE_DROP_UNIQUE_INDEX_NO_WRITE;
6462 6463
  if ((flags_to_return & flags_to_check) != flags_to_check)
    flags_to_return&= ~flags_to_check;
6464 6465
  flags_to_check= HA_INPLACE_ADD_PK_INDEX_NO_WRITE;
  flags_to_check|= HA_INPLACE_DROP_PK_INDEX_NO_WRITE;
6466 6467 6468
  if ((flags_to_return & flags_to_check) != flags_to_check)
    flags_to_return&= ~flags_to_check;
  DBUG_RETURN(flags_to_return);
6469 6470 6471 6472 6473 6474 6475 6476 6477 6478
}


/**
  check if copy of data is needed in alter table.
*/
bool ha_partition::check_if_incompatible_data(HA_CREATE_INFO *create_info,
                                              uint table_changes)
{
  handler **file;
6479
  bool ret= COMPATIBLE_DATA_YES;
6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500

  /*
    The check for any partitioning related changes have already been done
    in mysql_alter_table (by fix_partition_func), so it is only up to
    the underlying handlers.
  */
  for (file= m_file; *file; file++)
    if ((ret=  (*file)->check_if_incompatible_data(create_info,
                                                   table_changes)) !=
        COMPATIBLE_DATA_YES)
      break;
  return ret;
}


/**
  Support of fast or online add/drop index
*/
int ha_partition::add_index(TABLE *table_arg, KEY *key_info, uint num_of_keys)
{
  handler **file;
6501
  int ret= 0;
6502

6503
  DBUG_ENTER("ha_partition::add_index");
6504 6505 6506 6507 6508 6509 6510
  /*
    There has already been a check in fix_partition_func in mysql_alter_table
    before this call, which checks for unique/primary key violations of the
    partitioning function. So no need for extra check here.
  */
  for (file= m_file; *file; file++)
    if ((ret=  (*file)->add_index(table_arg, key_info, num_of_keys)))
6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532
      goto err;
  DBUG_RETURN(ret);
err:
  if (file > m_file)
  {
    uint *key_numbers= (uint*) ha_thd()->alloc(sizeof(uint) * num_of_keys);
    uint old_num_of_keys= table_arg->s->keys;
    uint i;
    /* The newly created keys have the last id's */
    for (i= 0; i < num_of_keys; i++)
      key_numbers[i]= i + old_num_of_keys;
    if (!table_arg->key_info)
      table_arg->key_info= key_info;
    while (--file >= m_file)
    {
      (void) (*file)->prepare_drop_index(table_arg, key_numbers, num_of_keys);
      (void) (*file)->final_drop_index(table_arg);
    }
    if (table_arg->key_info == key_info)
      table_arg->key_info= NULL;
  }
  DBUG_RETURN(ret);
6533 6534 6535 6536 6537 6538 6539
}


int ha_partition::prepare_drop_index(TABLE *table_arg, uint *key_num,
                                 uint num_of_keys)
{
  handler **file;
6540
  int ret= 0;
6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563

  /*
    DROP INDEX does not affect partitioning.
  */
  for (file= m_file; *file; file++)
    if ((ret=  (*file)->prepare_drop_index(table_arg, key_num, num_of_keys)))
      break;
  return ret;
}


int ha_partition::final_drop_index(TABLE *table_arg)
{
  handler **file;
  int ret= HA_ERR_WRONG_COMMAND;

  for (file= m_file; *file; file++)
    if ((ret=  (*file)->final_drop_index(table_arg)))
      break;
  return ret;
}


6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578
/*
  If frm_error() is called then we will use this to to find out what file
  extensions exist for the storage engine. This is also used by the default
  rename_table and delete_table method in handler.cc.
*/

static const char *ha_partition_ext[]=
{
  ha_par_ext, NullS
};

const char **ha_partition::bas_ext() const
{ return ha_partition_ext; }


unknown's avatar
unknown committed
6579 6580
uint ha_partition::min_of_the_max_uint(
                       uint (handler::*operator_func)(void) const) const
6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627
{
  handler **file;
  uint min_of_the_max= ((*m_file)->*operator_func)();

  for (file= m_file+1; *file; file++)
  {
    uint tmp= ((*file)->*operator_func)();
    set_if_smaller(min_of_the_max, tmp);
  }
  return min_of_the_max;
}


uint ha_partition::max_supported_key_parts() const
{
  return min_of_the_max_uint(&handler::max_supported_key_parts);
}


uint ha_partition::max_supported_key_length() const
{
  return min_of_the_max_uint(&handler::max_supported_key_length);
}


uint ha_partition::max_supported_key_part_length() const
{
  return min_of_the_max_uint(&handler::max_supported_key_part_length);
}


uint ha_partition::max_supported_record_length() const
{
  return min_of_the_max_uint(&handler::max_supported_record_length);
}


uint ha_partition::max_supported_keys() const
{
  return min_of_the_max_uint(&handler::max_supported_keys);
}


uint ha_partition::extra_rec_buf_length() const
{
  handler **file;
  uint max= (*m_file)->extra_rec_buf_length();
unknown's avatar
unknown committed
6628

6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639
  for (file= m_file, file++; *file; file++)
    if (max < (*file)->extra_rec_buf_length())
      max= (*file)->extra_rec_buf_length();
  return max;
}


uint ha_partition::min_record_length(uint options) const
{
  handler **file;
  uint max= (*m_file)->min_record_length(options);
unknown's avatar
unknown committed
6640

6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651
  for (file= m_file, file++; *file; file++)
    if (max < (*file)->min_record_length(options))
      max= (*file)->min_record_length(options);
  return max;
}


/****************************************************************************
                MODULE compare records
****************************************************************************/
/*
unknown's avatar
unknown committed
6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668
  Compare two positions

  SYNOPSIS
    cmp_ref()
    ref1                   First position
    ref2                   Second position

  RETURN VALUE
    <0                     ref1 < ref2
    0                      Equal
    >0                     ref1 > ref2

  DESCRIPTION
    We get two references and need to check if those records are the same.
    If they belong to different partitions we decide that they are not
    the same record. Otherwise we use the particular handler to decide if
    they are the same. Sort in partition id order if not equal.
6669 6670
*/

6671
int ha_partition::cmp_ref(const uchar *ref1, const uchar *ref2)
6672 6673 6674 6675 6676
{
  uint part_id;
  my_ptrdiff_t diff1, diff2;
  handler *file;
  DBUG_ENTER("ha_partition::cmp_ref");
unknown's avatar
unknown committed
6677

6678 6679
  if ((ref1[0] == ref2[0]) && (ref1[1] == ref2[1]))
  {
unknown's avatar
unknown committed
6680
    part_id= uint2korr(ref1);
6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707
    file= m_file[part_id];
    DBUG_ASSERT(part_id < m_tot_parts);
    DBUG_RETURN(file->cmp_ref((ref1 + PARTITION_BYTES_IN_POS),
			      (ref2 + PARTITION_BYTES_IN_POS)));
  }
  diff1= ref2[1] - ref1[1];
  diff2= ref2[0] - ref1[0];
  if (diff1 > 0)
  {
    DBUG_RETURN(-1);
  }
  if (diff1 < 0)
  {
    DBUG_RETURN(+1);
  }
  if (diff2 > 0)
  {
    DBUG_RETURN(-1);
  }
  DBUG_RETURN(+1);
}


/****************************************************************************
                MODULE auto increment
****************************************************************************/

unknown's avatar
unknown committed
6708

6709 6710 6711 6712 6713 6714
int ha_partition::reset_auto_increment(ulonglong value)
{
  handler **file= m_file;
  int res;
  DBUG_ENTER("ha_partition::reset_auto_increment");
  lock_auto_increment();
Mattias Jonsson's avatar
Mattias Jonsson committed
6715 6716
  table_share->ha_part_data->auto_inc_initialized= FALSE;
  table_share->ha_part_data->next_auto_inc_val= 0;
6717 6718 6719 6720 6721 6722 6723
  do
  {
    if ((res= (*file)->ha_reset_auto_increment(value)) != 0)
      break;
  } while (*(++file));
  unlock_auto_increment();
  DBUG_RETURN(res);
6724 6725 6726
}


6727
/**
6728
  This method is called by update_auto_increment which in turn is called
6729
  by the individual handlers as part of write_row. We use the
6730
  table_share->ha_part_data->next_auto_inc_val, or search all
6731 6732 6733
  partitions for the highest auto_increment_value if not initialized or
  if auto_increment field is a secondary part of a key, we must search
  every partition when holding a mutex to be sure of correctness.
6734 6735
*/

6736 6737 6738 6739
void ha_partition::get_auto_increment(ulonglong offset, ulonglong increment,
                                      ulonglong nb_desired_values,
                                      ulonglong *first_value,
                                      ulonglong *nb_reserved_values)
6740 6741
{
  DBUG_ENTER("ha_partition::get_auto_increment");
6742 6743 6744 6745 6746 6747
  DBUG_PRINT("info", ("offset: %lu inc: %lu desired_values: %lu "
                      "first_value: %lu", (ulong) offset, (ulong) increment,
                      (ulong) nb_desired_values, (ulong) *first_value));
  DBUG_ASSERT(increment && nb_desired_values);
  *first_value= 0;
  if (table->s->next_number_keypart)
6748
  {
6749
    /*
6750 6751
      next_number_keypart is != 0 if the auto_increment column is a secondary
      column in the index (it is allowed in MyISAM)
6752
    */
6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778
    DBUG_PRINT("info", ("next_number_keypart != 0"));
    ulonglong nb_reserved_values_part;
    ulonglong first_value_part, max_first_value;
    handler **file= m_file;
    first_value_part= max_first_value= *first_value;
    /* Must lock and find highest value among all partitions. */
    lock_auto_increment();
    do
    {
      /* Only nb_desired_values = 1 makes sense */
      (*file)->get_auto_increment(offset, increment, 1,
                                 &first_value_part, &nb_reserved_values_part);
      if (first_value_part == ~(ulonglong)(0)) // error in one partition
      {
        *first_value= first_value_part;
        /* log that the error was between table/partition handler */
        sql_print_error("Partition failed to reserve auto_increment value");
        unlock_auto_increment();
        DBUG_VOID_RETURN;
      }
      DBUG_PRINT("info", ("first_value_part: %lu", (ulong) first_value_part));
      set_if_bigger(max_first_value, first_value_part);
    } while (*(++file));
    *first_value= max_first_value;
    *nb_reserved_values= 1;
    unlock_auto_increment();
6779
  }
6780
  else
6781
  {
6782
    THD *thd= ha_thd();
unknown's avatar
unknown committed
6783
    /*
6784
      This is initialized in the beginning of the first write_row call.
unknown's avatar
unknown committed
6785
    */
Mattias Jonsson's avatar
Mattias Jonsson committed
6786
    DBUG_ASSERT(table_share->ha_part_data->auto_inc_initialized);
unknown's avatar
unknown committed
6787
    /*
6788
      Get a lock for handling the auto_increment in table_share->ha_part_data
6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802
      for avoiding two concurrent statements getting the same number.
    */ 

    lock_auto_increment();

    /*
      In a multi-row insert statement like INSERT SELECT and LOAD DATA
      where the number of candidate rows to insert is not known in advance
      we must hold a lock/mutex for the whole statement if we have statement
      based replication. Because the statement-based binary log contains
      only the first generated value used by the statement, and slaves assumes
      all other generated values used by this statement were consecutive to
      this first one, we must exclusively lock the generator until the statement
      is done.
unknown's avatar
unknown committed
6803
    */
6804 6805 6806
    if (!auto_increment_safe_stmt_log_lock &&
        thd->lex->sql_command != SQLCOM_INSERT &&
        mysql_bin_log.is_open() &&
6807
        !thd->is_current_stmt_binlog_format_row() &&
6808
        (thd->variables.option_bits & OPTION_BIN_LOG))
6809 6810 6811 6812 6813 6814
    {
      DBUG_PRINT("info", ("locking auto_increment_safe_stmt_log_lock"));
      auto_increment_safe_stmt_log_lock= TRUE;
    }

    /* this gets corrected (for offset/increment) in update_auto_increment */
Mattias Jonsson's avatar
Mattias Jonsson committed
6815 6816 6817
    *first_value= table_share->ha_part_data->next_auto_inc_val;
    table_share->ha_part_data->next_auto_inc_val+=
                                              nb_desired_values * increment;
6818 6819 6820 6821

    unlock_auto_increment();
    DBUG_PRINT("info", ("*first_value: %lu", (ulong) *first_value));
    *nb_reserved_values= nb_desired_values;
6822
  }
6823
  DBUG_VOID_RETURN;
6824 6825
}

6826 6827 6828 6829
void ha_partition::release_auto_increment()
{
  DBUG_ENTER("ha_partition::release_auto_increment");

6830
  if (table->s->next_number_keypart)
6831
  {
6832 6833 6834 6835 6836 6837 6838
    for (uint i= 0; i < m_tot_parts; i++)
      m_file[i]->ha_release_auto_increment();
  }
  else if (next_insert_id)
  {
    ulonglong next_auto_inc_val;
    lock_auto_increment();
Mattias Jonsson's avatar
Mattias Jonsson committed
6839
    next_auto_inc_val= table_share->ha_part_data->next_auto_inc_val;
6840 6841 6842 6843 6844
    /*
      If the current auto_increment values is lower than the reserved
      value, and the reserved value was reserved by this thread,
      we can lower the reserved value.
    */
6845 6846
    if (next_insert_id < next_auto_inc_val &&
        auto_inc_interval_for_cur_row.maximum() >= next_auto_inc_val)
6847 6848 6849 6850 6851 6852 6853
    {
      THD *thd= ha_thd();
      /*
        Check that we do not lower the value because of a failed insert
        with SET INSERT_ID, i.e. forced/non generated values.
      */
      if (thd->auto_inc_intervals_forced.maximum() < next_insert_id)
Mattias Jonsson's avatar
Mattias Jonsson committed
6854
        table_share->ha_part_data->next_auto_inc_val= next_insert_id;
6855
    }
Mattias Jonsson's avatar
Mattias Jonsson committed
6856 6857
    DBUG_PRINT("info", ("table_share->ha_part_data->next_auto_inc_val: %lu",
                        (ulong) table_share->ha_part_data->next_auto_inc_val));
6858 6859 6860 6861 6862 6863 6864 6865 6866

    /* Unlock the multi row statement lock taken in get_auto_increment */
    if (auto_increment_safe_stmt_log_lock)
    {
      auto_increment_safe_stmt_log_lock= FALSE;
      DBUG_PRINT("info", ("unlocking auto_increment_safe_stmt_log_lock"));
    }

    unlock_auto_increment();
6867 6868 6869
  }
  DBUG_VOID_RETURN;
}
6870 6871

/****************************************************************************
6872
                MODULE initialize handler for HANDLER call
6873 6874 6875 6876 6877 6878 6879 6880
****************************************************************************/

void ha_partition::init_table_handle_for_HANDLER()
{
  return;
}


6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901
/****************************************************************************
                MODULE enable/disable indexes
****************************************************************************/

/*
  Disable indexes for a while
  SYNOPSIS
    disable_indexes()
    mode                      Mode
  RETURN VALUES
    0                         Success
    != 0                      Error
*/

int ha_partition::disable_indexes(uint mode)
{
  handler **file;
  int error= 0;

  for (file= m_file; *file; file++)
  {
6902
    if ((error= (*file)->ha_disable_indexes(mode)))
6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925
      break;
  }
  return error;
}


/*
  Enable indexes again
  SYNOPSIS
    enable_indexes()
    mode                      Mode
  RETURN VALUES
    0                         Success
    != 0                      Error
*/

int ha_partition::enable_indexes(uint mode)
{
  handler **file;
  int error= 0;

  for (file= m_file; *file; file++)
  {
6926
    if ((error= (*file)->ha_enable_indexes(mode)))
6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956
      break;
  }
  return error;
}


/*
  Check if indexes are disabled
  SYNOPSIS
    indexes_are_disabled()

  RETURN VALUES
    0                      Indexes are enabled
    != 0                   Indexes are disabled
*/

int ha_partition::indexes_are_disabled(void)
{
  handler **file;
  int error= 0;

  for (file= m_file; *file; file++)
  {
    if ((error= (*file)->indexes_are_disabled()))
      break;
  }
  return error;
}


unknown's avatar
unknown committed
6957
struct st_mysql_storage_engine partition_storage_engine=
6958
{ MYSQL_HANDLERTON_INTERFACE_VERSION };
unknown's avatar
unknown committed
6959 6960 6961 6962

mysql_declare_plugin(partition)
{
  MYSQL_STORAGE_ENGINE_PLUGIN,
unknown's avatar
unknown committed
6963 6964
  &partition_storage_engine,
  "partition",
unknown's avatar
unknown committed
6965
  "Mikael Ronstrom, MySQL AB",
unknown's avatar
unknown committed
6966
  "Partition Storage Engine Helper",
6967
  PLUGIN_LICENSE_GPL,
unknown's avatar
unknown committed
6968
  partition_initialize, /* Plugin Init */
unknown's avatar
unknown committed
6969
  NULL, /* Plugin Deinit */
unknown's avatar
unknown committed
6970
  0x0100, /* 1.0 */
6971 6972 6973
  NULL,                       /* status variables                */
  NULL,                       /* system variables                */
  NULL                        /* config options                  */
unknown's avatar
unknown committed
6974 6975 6976 6977
}
mysql_declare_plugin_end;

#endif