sql_partition.cc 273 KB
Newer Older
Sergei Golubchik's avatar
Sergei Golubchik committed
1 2
/* Copyright (c) 2005, 2014, Oracle and/or its affiliates.
   Copyright (c) 2009, 2014, SkySQL Ab.
3 4 5

   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
6
   the Free Software Foundation; version 2 of the License.
7 8 9 10 11 12 13 14

   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
15
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
16 17

/*
18
  This file is a container for general functionality related
19
  to partitioning introduced in MySQL version 5.1. It contains functionality
20 21
  used by all handlers that support partitioning, such as
  the partitioning handler itself and the NDB handler.
22 23
  (Much of the code in this file has been split into partition_info.cc and
   the header files partition_info.h + partition_element.h + sql_partition.h)
24

25 26 27 28 29 30
  The first version was written by Mikael Ronstrom 2004-2006.
  Various parts of the optimizer code was written by Sergey Petrunia.
  Code have been maintained by Mattias Jonsson.
  The second version was written by Mikael Ronstrom 2006-2007 with some
  final fixes for partition pruning in 2008-2009 with assistance from Sergey
  Petrunia and Mattias Jonsson.
31

32
  The first version supports RANGE partitioning, LIST partitioning, HASH
33 34 35
  partitioning and composite partitioning (hereafter called subpartitioning)
  where each RANGE/LIST partitioning is HASH partitioned. The hash function
  can either be supplied by the user or by only a list of fields (also
36
  called KEY partitioning), where the MySQL server will use an internal
37 38
  hash function.
  There are quite a few defaults that can be used as well.
39 40 41 42 43 44

  The second version introduces a new variant of RANGE and LIST partitioning
  which is often referred to as column lists in the code variables. This
  enables a user to specify a set of columns and their concatenated value
  as the partition value. By comparing the concatenation of these values
  the proper partition can be choosen.
45 46 47 48
*/

/* Some general useful functions */

49
#define MYSQL_LEX 1
50 51 52 53 54 55 56 57
#include "sql_priv.h"
#include "unireg.h"                    // REQUIRED: for other includes
#include "sql_partition.h"
#include "key.h"                            // key_restore
#include "sql_parse.h"                      // parse_sql
#include "sql_cache.h"                      // query_cache_invalidate3
#include "lock.h"                           // mysql_lock_remove
#include "sql_show.h"                       // append_identifier
58 59
#include <errno.h>
#include <m_ctype.h>
unknown's avatar
unknown committed
60
#include "my_md5.h"
Konstantin Osipov's avatar
Konstantin Osipov committed
61
#include "transaction.h"
62

63
#include "sql_base.h"                   // close_all_tables_for_name
64 65 66
#include "sql_table.h"                  // build_table_filename,
                                        // build_table_shadow_filename,
                                        // table_to_filename
67
                                        // mysql_*_alter_copy_data
68 69 70
#include "opt_range.h"                  // store_key_image_to_rec
#include "sql_analyse.h"                // append_escaped

71
#ifdef WITH_PARTITION_STORAGE_ENGINE
unknown's avatar
unknown committed
72
#include "ha_partition.h"
Alfranio Correia's avatar
Alfranio Correia committed
73 74

#define ERROR_INJECT_CRASH(code) \
75
  DBUG_EVALUATE_IF(code, (DBUG_SUICIDE(), 0), 0)
76 77
#define ERROR_INJECT_ERROR(code) \
  DBUG_EVALUATE_IF(code, (my_error(ER_UNKNOWN_ERROR, MYF(0)), TRUE), 0)
Alfranio Correia's avatar
Alfranio Correia committed
78

79 80 81
/*
  Partition related functions declarations and some static constants;
*/
82 83
const LEX_STRING partition_keywords[]=
{
unknown's avatar
unknown committed
84 85 86 87 88
  { C_STRING_WITH_LEN("HASH") },
  { C_STRING_WITH_LEN("RANGE") },
  { C_STRING_WITH_LEN("LIST") }, 
  { C_STRING_WITH_LEN("KEY") },
  { C_STRING_WITH_LEN("MAXVALUE") },
89
  { C_STRING_WITH_LEN("LINEAR ") },
90 91 92
  { C_STRING_WITH_LEN(" COLUMNS") },
  { C_STRING_WITH_LEN("ALGORITHM") }

93
};
94 95 96 97 98 99 100 101
static const char *part_str= "PARTITION";
static const char *sub_str= "SUB";
static const char *by_str= "BY";
static const char *space_str= " ";
static const char *equal_str= "=";
static const char *end_paren_str= ")";
static const char *begin_paren_str= "(";
static const char *comma_str= ",";
102

103 104 105
int get_partition_id_list_col(partition_info *part_info,
                              uint32 *part_id,
                              longlong *func_value);
unknown's avatar
unknown committed
106
int get_partition_id_list(partition_info *part_info,
107 108
                          uint32 *part_id,
                          longlong *func_value);
109 110 111
int get_partition_id_range_col(partition_info *part_info,
                               uint32 *part_id,
                               longlong *func_value);
112
int get_partition_id_range(partition_info *part_info,
113 114
                           uint32 *part_id,
                           longlong *func_value);
115 116 117 118 119
static int get_part_id_charset_func_part(partition_info *part_info,
                                         uint32 *part_id,
                                         longlong *func_value);
static int get_part_id_charset_func_subpart(partition_info *part_info,
                                            uint32 *part_id);
unknown's avatar
unknown committed
120
int get_partition_id_hash_nosub(partition_info *part_info,
121 122
                                uint32 *part_id,
                                longlong *func_value);
123 124 125
int get_partition_id_key_nosub(partition_info *part_info,
                               uint32 *part_id,
                               longlong *func_value);
unknown's avatar
unknown committed
126
int get_partition_id_linear_hash_nosub(partition_info *part_info,
127 128
                                       uint32 *part_id,
                                       longlong *func_value);
129 130 131
int get_partition_id_linear_key_nosub(partition_info *part_info,
                                      uint32 *part_id,
                                      longlong *func_value);
132 133 134
int get_partition_id_with_sub(partition_info *part_info,
                              uint32 *part_id,
                              longlong *func_value);
135 136 137 138 139 140 141 142
int get_partition_id_hash_sub(partition_info *part_info,
                              uint32 *part_id); 
int get_partition_id_key_sub(partition_info *part_info,
                             uint32 *part_id); 
int get_partition_id_linear_hash_sub(partition_info *part_info,
                                     uint32 *part_id); 
int get_partition_id_linear_key_sub(partition_info *part_info,
                                    uint32 *part_id); 
unknown's avatar
unknown committed
143
static uint32 get_next_partition_via_walking(PARTITION_ITERATOR*);
144
static void set_up_range_analysis_info(partition_info *part_info);
unknown's avatar
unknown committed
145
static uint32 get_next_subpartition_via_walking(PARTITION_ITERATOR*);
146 147
#endif

unknown's avatar
unknown committed
148 149 150 151
uint32 get_next_partition_id_range(PARTITION_ITERATOR* part_iter);
uint32 get_next_partition_id_list(PARTITION_ITERATOR* part_iter);
int get_part_iter_for_interval_via_mapping(partition_info *part_info,
                                           bool is_subpart,
152
                                           uint32 *store_length_array,
153
                                           uchar *min_value, uchar *max_value,
154
                                           uint min_len, uint max_len,
unknown's avatar
unknown committed
155 156
                                           uint flags,
                                           PARTITION_ITERATOR *part_iter);
157 158 159 160 161 162 163
int get_part_iter_for_interval_cols_via_map(partition_info *part_info,
                                            bool is_subpart,
                                            uint32 *store_length_array,
                                            uchar *min_value, uchar *max_value,
                                            uint min_len, uint max_len,
                                            uint flags,
                                            PARTITION_ITERATOR *part_iter);
unknown's avatar
unknown committed
164 165
int get_part_iter_for_interval_via_walking(partition_info *part_info,
                                           bool is_subpart,
166
                                           uint32 *store_length_array,
167
                                           uchar *min_value, uchar *max_value,
168
                                           uint min_len, uint max_len,
unknown's avatar
unknown committed
169 170
                                           uint flags,
                                           PARTITION_ITERATOR *part_iter);
Georgi Kodinov's avatar
merge  
Georgi Kodinov committed
171 172

#ifdef WITH_PARTITION_STORAGE_ENGINE
173 174 175
static int cmp_rec_and_tuple(part_column_list_val *val, uint32 nvals_in_rec);
static int cmp_rec_and_tuple_prune(part_column_list_val *val,
                                   uint32 n_vals_in_rec,
176 177
                                   bool is_left_endpoint,
                                   bool include_endpoint);
unknown's avatar
unknown committed
178

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/*
  Convert constants in VALUES definition to the character set the
  corresponding field uses.

  SYNOPSIS
    convert_charset_partition_constant()
    item                                Item to convert
    cs                                  Character set to convert to

  RETURN VALUE
    NULL                                Error
    item                                New converted item
*/

Item* convert_charset_partition_constant(Item *item, CHARSET_INFO *cs)
{
  THD *thd= current_thd;
  Name_resolution_context *context= &thd->lex->current_select->context;
  TABLE_LIST *save_list= context->table_list;
  const char *save_where= thd->where;

  item= item->safe_charset_converter(cs);
  context->table_list= NULL;
  thd->where= "convert character set partition constant";
  if (!item || item->fix_fields(thd, (Item**)NULL))
    item= NULL;
  thd->where= save_where;
  context->table_list= save_list;
  return item;
}


unknown's avatar
unknown committed
211
/*
unknown's avatar
unknown committed
212 213
  A support function to check if a name is in a list of strings

unknown's avatar
unknown committed
214
  SYNOPSIS
unknown's avatar
unknown committed
215 216 217 218
    is_name_in_list()
    name               String searched for
    list_names         A list of names searched in

unknown's avatar
unknown committed
219 220 221 222 223
  RETURN VALUES
    TRUE               String found
    FALSE              String not found
*/

unknown's avatar
unknown committed
224 225
bool is_name_in_list(char *name,
                          List<char> list_names)
unknown's avatar
unknown committed
226
{
unknown's avatar
unknown committed
227
  List_iterator<char> names_it(list_names);
228
  uint num_names= list_names.elements;
unknown's avatar
unknown committed
229
  uint i= 0;
unknown's avatar
unknown committed
230

unknown's avatar
unknown committed
231 232
  do
  {
unknown's avatar
unknown committed
233 234
    char *list_name= names_it++;
    if (!(my_strcasecmp(system_charset_info, name, list_name)))
unknown's avatar
unknown committed
235
      return TRUE;
236
  } while (++i < num_names);
unknown's avatar
unknown committed
237 238 239 240
  return FALSE;
}


unknown's avatar
unknown committed
241 242 243 244 245 246 247 248

/*
  Set-up defaults for partitions. 

  SYNOPSIS
    partition_default_handling()
    table                         Table object
    part_info                     Partition info to set up
249 250
    is_create_table_ind           Is this part of a table creation
    normalized_path               Normalized path name of table and database
unknown's avatar
unknown committed
251 252 253 254 255 256

  RETURN VALUES
    TRUE                          Error
    FALSE                         Success
*/

257
bool partition_default_handling(TABLE *table, partition_info *part_info,
258
                                bool is_create_table_ind,
259
                                const char *normalized_path)
unknown's avatar
unknown committed
260 261 262
{
  DBUG_ENTER("partition_default_handling");

263
  if (!is_create_table_ind)
unknown's avatar
unknown committed
264
  {
265
    if (part_info->use_default_num_partitions)
unknown's avatar
unknown committed
266
    {
267
      if (table->file->get_no_parts(normalized_path, &part_info->num_parts))
268 269 270
      {
        DBUG_RETURN(TRUE);
      }
unknown's avatar
unknown committed
271
    }
272
    else if (part_info->is_sub_partitioned() &&
273
             part_info->use_default_num_subpartitions)
unknown's avatar
unknown committed
274
    {
275 276
      uint num_parts;
      if (table->file->get_no_parts(normalized_path, &num_parts))
277 278 279
      {
        DBUG_RETURN(TRUE);
      }
280 281 282
      DBUG_ASSERT(part_info->num_parts > 0);
      DBUG_ASSERT((num_parts % part_info->num_parts) == 0);
      part_info->num_subparts= num_parts / part_info->num_parts;
unknown's avatar
unknown committed
283 284
    }
  }
285 286
  part_info->set_up_defaults_for_partitioning(table->file,
                                              (ulonglong)0, (uint)0);
unknown's avatar
unknown committed
287 288 289 290
  DBUG_RETURN(FALSE);
}


291 292 293 294 295 296 297
/*
  Check that the reorganized table will not have duplicate partitions.

  SYNOPSIS
    check_reorganise_list()
    new_part_info      New partition info
    old_part_info      Old partition info
298 299
    list_part_names    The list of partition names that will go away and
                       can be reused in the new table.
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

  RETURN VALUES
    TRUE               Inacceptable name conflict detected.
    FALSE              New names are OK.

  DESCRIPTION
    Can handle that the 'new_part_info' and 'old_part_info' the same
    in which case it checks that the list of names in the partitions
    doesn't contain any duplicated names.
*/

bool check_reorganise_list(partition_info *new_part_info,
                           partition_info *old_part_info,
                           List<char> list_part_names)
{
  uint new_count, old_count;
316 317
  uint num_new_parts= new_part_info->partitions.elements;
  uint num_old_parts= old_part_info->partitions.elements;
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  List_iterator<partition_element> new_parts_it(new_part_info->partitions);
  bool same_part_info= (new_part_info == old_part_info);
  DBUG_ENTER("check_reorganise_list");

  new_count= 0;
  do
  {
    List_iterator<partition_element> old_parts_it(old_part_info->partitions);
    char *new_name= (new_parts_it++)->partition_name;
    new_count++;
    old_count= 0;
    do
    {
      char *old_name= (old_parts_it++)->partition_name;
      old_count++;
      if (same_part_info && old_count == new_count)
        break;
      if (!(my_strcasecmp(system_charset_info, old_name, new_name)))
      {
unknown's avatar
unknown committed
337
        if (!is_name_in_list(old_name, list_part_names))
338 339
          DBUG_RETURN(TRUE);
      }
340 341
    } while (old_count < num_old_parts);
  } while (new_count < num_new_parts);
342 343 344 345
  DBUG_RETURN(FALSE);
}


346 347 348
/*
  A useful routine used by update_row for partition handlers to calculate
  the partition ids of the old and the new record.
unknown's avatar
unknown committed
349

350 351 352 353 354 355
  SYNOPSIS
    get_part_for_update()
    old_data                Buffer of old record
    new_data                Buffer of new record
    rec0                    Reference to table->record[0]
    part_info               Reference to partition information
unknown's avatar
unknown committed
356 357 358
    out:old_part_id         The returned partition id of old record 
    out:new_part_id         The returned partition id of new record

359 360 361 362 363
  RETURN VALUE
    0                       Success
    > 0                     Error code
*/

364 365
int get_parts_for_update(const uchar *old_data, uchar *new_data,
                         const uchar *rec0, partition_info *part_info,
366 367
                         uint32 *old_part_id, uint32 *new_part_id,
                         longlong *new_func_value)
368 369 370
{
  Field **part_field_array= part_info->full_part_field_array;
  int error;
371
  longlong old_func_value;
372 373
  DBUG_ENTER("get_parts_for_update");

374
  DBUG_ASSERT(new_data == rec0);             // table->record[0]
375
  set_field_ptr(part_field_array, old_data, rec0);
376 377
  error= part_info->get_partition_id(part_info, old_part_id,
                                     &old_func_value);
378 379 380 381 382 383 384 385 386 387
  set_field_ptr(part_field_array, rec0, old_data);
  if (unlikely(error))                             // Should never happen
  {
    DBUG_ASSERT(0);
    DBUG_RETURN(error);
  }
#ifdef NOT_NEEDED
  if (new_data == rec0)
#endif
  {
388 389 390
    if (unlikely(error= part_info->get_partition_id(part_info,
                                                    new_part_id,
                                                    new_func_value)))
391 392 393 394 395 396 397 398 399 400 401 402 403
    {
      DBUG_RETURN(error);
    }
  }
#ifdef NOT_NEEDED
  else
  {
    /*
      This branch should never execute but it is written anyways for
      future use. It will be tested by ensuring that the above
      condition is false in one test situation before pushing the code.
    */
    set_field_ptr(part_field_array, new_data, rec0);
404 405
    error= part_info->get_partition_id(part_info, new_part_id,
                                       new_func_value);
406 407 408 409 410 411 412 413 414 415 416 417 418 419
    set_field_ptr(part_field_array, rec0, new_data);
    if (unlikely(error))
    {
      DBUG_RETURN(error);
    }
  }
#endif
  DBUG_RETURN(0);
}


/*
  A useful routine used by delete_row for partition handlers to calculate
  the partition id.
unknown's avatar
unknown committed
420

421 422 423 424 425
  SYNOPSIS
    get_part_for_delete()
    buf                     Buffer of old record
    rec0                    Reference to table->record[0]
    part_info               Reference to partition information
unknown's avatar
unknown committed
426 427
    out:part_id             The returned partition id to delete from

428 429 430
  RETURN VALUE
    0                       Success
    > 0                     Error code
unknown's avatar
unknown committed
431

432 433 434 435 436 437
  DESCRIPTION
    Dependent on whether buf is not record[0] we need to prepare the
    fields. Then we call the function pointer get_partition_id to
    calculate the partition id.
*/

438
int get_part_for_delete(const uchar *buf, const uchar *rec0,
439 440 441
                        partition_info *part_info, uint32 *part_id)
{
  int error;
442
  longlong func_value;
443 444 445 446
  DBUG_ENTER("get_part_for_delete");

  if (likely(buf == rec0))
  {
447 448
    if (unlikely((error= part_info->get_partition_id(part_info, part_id,
                                                     &func_value))))
449 450 451 452 453 454 455 456 457
    {
      DBUG_RETURN(error);
    }
    DBUG_PRINT("info", ("Delete from partition %d", *part_id));
  }
  else
  {
    Field **part_field_array= part_info->full_part_field_array;
    set_field_ptr(part_field_array, buf, rec0);
458
    error= part_info->get_partition_id(part_info, part_id, &func_value);
459 460 461 462 463 464 465 466 467 468 469 470
    set_field_ptr(part_field_array, rec0, buf);
    if (unlikely(error))
    {
      DBUG_RETURN(error);
    }
    DBUG_PRINT("info", ("Delete from partition %d (path2)", *part_id));
  }
  DBUG_RETURN(0);
}


/*
unknown's avatar
unknown committed
471 472 473
  This method is used to set-up both partition and subpartitioning
  field array and used for all types of partitioning.
  It is part of the logic around fix_partition_func.
474 475 476 477 478

  SYNOPSIS
    set_up_field_array()
    table                TABLE object for which partition fields are set-up
    sub_part             Is the table subpartitioned as well
unknown's avatar
unknown committed
479

480 481 482
  RETURN VALUE
    TRUE                 Error, some field didn't meet requirements
    FALSE                Ok, partition field array set-up
unknown's avatar
unknown committed
483

484
  DESCRIPTION
unknown's avatar
unknown committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

    A great number of functions below here is part of the fix_partition_func
    method. It is used to set up the partition structures for execution from
    openfrm. It is called at the end of the openfrm when the table struct has
    been set-up apart from the partition information.
    It involves:
    1) Setting arrays of fields for the partition functions.
    2) Setting up binary search array for LIST partitioning
    3) Setting up array for binary search for RANGE partitioning
    4) Setting up key_map's to assist in quick evaluation whether one
       can deduce anything from a given index of what partition to use
    5) Checking whether a set of partitions can be derived from a range on
       a field in the partition function.
    As part of doing this there is also a great number of error controls.
    This is actually the place where most of the things are checked for
    partition information when creating a table.
    Things that are checked includes
    1) All fields of partition function in Primary keys and unique indexes
       (if not supported)


    Create an array of partition fields (NULL terminated). Before this method
    is called fix_fields or find_table_in_sef has been called to set
    GET_FIXED_FIELDS_FLAG on all fields that are part of the partition
    function.
510
*/
unknown's avatar
unknown committed
511

512
static bool set_up_field_array(TABLE *table,
unknown's avatar
unknown committed
513
                              bool is_sub_part)
514 515
{
  Field **ptr, *field, **field_array;
516
  uint num_fields= 0;
unknown's avatar
unknown committed
517 518
  uint size_field_array;
  uint i= 0;
519
  uint inx;
unknown's avatar
unknown committed
520
  partition_info *part_info= table->part_info;
521 522 523 524 525 526 527
  int result= FALSE;
  DBUG_ENTER("set_up_field_array");

  ptr= table->field;
  while ((field= *(ptr++))) 
  {
    if (field->flags & GET_FIXED_FIELDS_FLAG)
528
      num_fields++;
529
  }
530
  if (num_fields > MAX_REF_PARTS)
531
  {
532
    char *err_str;
533
    if (is_sub_part)
534
      err_str= (char*)"subpartition function";
535
    else
536 537
      err_str= (char*)"partition function";
    my_error(ER_TOO_MANY_PARTITION_FUNC_FIELDS_ERROR, MYF(0), err_str);
538
    DBUG_RETURN(TRUE);
539
  }
540
  if (num_fields == 0)
unknown's avatar
unknown committed
541 542 543 544 545 546 547
  {
    /*
      We are using hidden key as partitioning field
    */
    DBUG_ASSERT(!is_sub_part);
    DBUG_RETURN(result);
  }
548
  size_field_array= (num_fields+1)*sizeof(Field*);
549
  field_array= (Field**)sql_calloc(size_field_array);
550 551
  if (unlikely(!field_array))
  {
unknown's avatar
unknown committed
552
    mem_alloc_error(size_field_array);
553 554 555 556 557 558 559 560 561 562 563
    result= TRUE;
  }
  ptr= table->field;
  while ((field= *(ptr++))) 
  {
    if (field->flags & GET_FIXED_FIELDS_FLAG)
    {
      field->flags&= ~GET_FIXED_FIELDS_FLAG;
      field->flags|= FIELD_IN_PART_FUNC_FLAG;
      if (likely(!result))
      {
564 565 566 567 568
        if (!is_sub_part && part_info->column_list)
        {
          List_iterator<char> it(part_info->part_field_list);
          char *field_name;

569
          DBUG_ASSERT(num_fields == part_info->part_field_list.elements);
570 571 572 573
          inx= 0;
          do
          {
            field_name= it++;
574 575 576
            if (!my_strcasecmp(system_charset_info,
                               field_name,
                               field->field_name))
577
              break;
578 579
          } while (++inx < num_fields);
          if (inx == num_fields)
580 581 582 583 584 585 586 587 588 589
          {
            mem_alloc_error(1);
            result= TRUE;
            continue;
          }
        }
        else
          inx= i;
        field_array[inx]= field;
        i++;
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

        /*
          We check that the fields are proper. It is required for each
          field in a partition function to:
          1) Not be a BLOB of any type
            A BLOB takes too long time to evaluate so we don't want it for
            performance reasons.
        */

        if (unlikely(field->flags & BLOB_FLAG))
        {
          my_error(ER_BLOB_FIELD_IN_PART_FUNC_ERROR, MYF(0));
          result= TRUE;
        }
      }
    }
  }
607
  field_array[num_fields]= 0;
unknown's avatar
unknown committed
608
  if (!is_sub_part)
609 610
  {
    part_info->part_field_array= field_array;
611
    part_info->num_part_fields= num_fields;
612 613 614 615
  }
  else
  {
    part_info->subpart_field_array= field_array;
616
    part_info->num_subpart_fields= num_fields;
617 618 619 620 621
  }
  DBUG_RETURN(result);
}


622

623 624 625
/*
  Create a field array including all fields of both the partitioning and the
  subpartitioning functions.
unknown's avatar
unknown committed
626

627 628
  SYNOPSIS
    create_full_part_field_array()
629
    thd                  Thread handle
630 631
    table                TABLE object for which partition fields are set-up
    part_info            Reference to partitioning data structure
unknown's avatar
unknown committed
632

633 634 635
  RETURN VALUE
    TRUE                 Memory allocation of field array failed
    FALSE                Ok
unknown's avatar
unknown committed
636

637 638 639 640 641 642 643
  DESCRIPTION
    If there is no subpartitioning then the same array is used as for the
    partitioning. Otherwise a new array is built up using the flag
    FIELD_IN_PART_FUNC in the field object.
    This function is called from fix_partition_func
*/

644
static bool create_full_part_field_array(THD *thd, TABLE *table,
645 646 647
                                         partition_info *part_info)
{
  bool result= FALSE;
648
  Field **ptr;
649
  my_bitmap_map *bitmap_buf;
650 651
  DBUG_ENTER("create_full_part_field_array");

652
  if (!part_info->is_sub_partitioned())
653 654
  {
    part_info->full_part_field_array= part_info->part_field_array;
655
    part_info->num_full_part_fields= part_info->num_part_fields;
656 657 658
  }
  else
  {
659
    Field *field, **field_array;
660
    uint num_part_fields=0, size_field_array;
661 662 663 664
    ptr= table->field;
    while ((field= *(ptr++)))
    {
      if (field->flags & FIELD_IN_PART_FUNC_FLAG)
665
        num_part_fields++;
666
    }
667
    size_field_array= (num_part_fields+1)*sizeof(Field*);
668
    field_array= (Field**)sql_calloc(size_field_array);
669 670
    if (unlikely(!field_array))
    {
unknown's avatar
unknown committed
671
      mem_alloc_error(size_field_array);
672 673 674
      result= TRUE;
      goto end;
    }
675
    num_part_fields= 0;
676 677 678 679
    ptr= table->field;
    while ((field= *(ptr++)))
    {
      if (field->flags & FIELD_IN_PART_FUNC_FLAG)
680
        field_array[num_part_fields++]= field;
681
    }
682
    field_array[num_part_fields]=0;
683
    part_info->full_part_field_array= field_array;
684
    part_info->num_full_part_fields= num_part_fields;
685
  }
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

  /*
    Initialize the set of all fields used in partition and subpartition
    expression. Required for testing of partition fields in write_set
    when updating. We need to set all bits in read_set because the row
    may need to be inserted in a different [sub]partition.
  */
  if (!(bitmap_buf= (my_bitmap_map*)
        thd->alloc(bitmap_buffer_size(table->s->fields))))
  {
    mem_alloc_error(bitmap_buffer_size(table->s->fields));
    result= TRUE;
    goto end;
  }
  if (bitmap_init(&part_info->full_part_field_set, bitmap_buf,
                  table->s->fields, FALSE))
  {
    mem_alloc_error(table->s->fields);
    result= TRUE;
    goto end;
  }
  /*
    full_part_field_array may be NULL if storage engine supports native
    partitioning.
  */
  if ((ptr= part_info->full_part_field_array))
712 713
    for (; *ptr; ptr++)
      bitmap_set_bit(&part_info->full_part_field_set, (*ptr)->field_index);
714

715 716 717 718 719 720 721 722 723
end:
  DBUG_RETURN(result);
}


/*

  Clear flag GET_FIXED_FIELDS_FLAG in all fields of a key previously set by
  set_indicator_in_key_fields (always used in pairs).
unknown's avatar
unknown committed
724

725 726 727
  SYNOPSIS
    clear_indicator_in_key_fields()
    key_info                  Reference to find the key fields
unknown's avatar
unknown committed
728 729 730 731 732 733 734 735 736 737 738 739

  RETURN VALUE
    NONE

  DESCRIPTION
    These support routines is used to set/reset an indicator of all fields
    in a certain key. It is used in conjunction with another support routine
    that traverse all fields in the PF to find if all or some fields in the
    PF is part of the key. This is used to check primary keys and unique
    keys involve all fields in PF (unless supported) and to derive the
    key_map's used to quickly decide whether the index can be used to
    derive which partitions are needed to scan.
740 741 742 743 744 745 746 747 748 749 750 751 752
*/

static void clear_indicator_in_key_fields(KEY *key_info)
{
  KEY_PART_INFO *key_part;
  uint key_parts= key_info->key_parts, i;
  for (i= 0, key_part=key_info->key_part; i < key_parts; i++, key_part++)
    key_part->field->flags&= (~GET_FIXED_FIELDS_FLAG);
}


/*
  Set flag GET_FIXED_FIELDS_FLAG in all fields of a key.
unknown's avatar
unknown committed
753

754 755 756
  SYNOPSIS
    set_indicator_in_key_fields
    key_info                  Reference to find the key fields
unknown's avatar
unknown committed
757 758 759

  RETURN VALUE
    NONE
760 761 762 763 764 765 766 767 768 769 770 771 772 773
*/

static void set_indicator_in_key_fields(KEY *key_info)
{
  KEY_PART_INFO *key_part;
  uint key_parts= key_info->key_parts, i;
  for (i= 0, key_part=key_info->key_part; i < key_parts; i++, key_part++)
    key_part->field->flags|= GET_FIXED_FIELDS_FLAG;
}


/*
  Check if all or some fields in partition field array is part of a key
  previously used to tag key fields.
unknown's avatar
unknown committed
774

775 776 777
  SYNOPSIS
    check_fields_in_PF()
    ptr                  Partition field array
unknown's avatar
unknown committed
778 779 780
    out:all_fields       Is all fields of partition field array used in key
    out:some_fields      Is some fields of partition field array used in key

781 782 783 784 785 786 787 788
  RETURN VALUE
    all_fields, some_fields
*/

static void check_fields_in_PF(Field **ptr, bool *all_fields,
                               bool *some_fields)
{
  DBUG_ENTER("check_fields_in_PF");
unknown's avatar
unknown committed
789

790 791
  *all_fields= TRUE;
  *some_fields= FALSE;
792 793 794 795 796
  if ((!ptr) || !(*ptr))
  {
    *all_fields= FALSE;
    DBUG_VOID_RETURN;
  }
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
  do
  {
  /* Check if the field of the PF is part of the current key investigated */
    if ((*ptr)->flags & GET_FIXED_FIELDS_FLAG)
      *some_fields= TRUE; 
    else
      *all_fields= FALSE;
  } while (*(++ptr));
  DBUG_VOID_RETURN;
}


/*
  Clear flag GET_FIXED_FIELDS_FLAG in all fields of the table.
  This routine is used for error handling purposes.
unknown's avatar
unknown committed
812

813 814 815
  SYNOPSIS
    clear_field_flag()
    table                TABLE object for which partition fields are set-up
unknown's avatar
unknown committed
816 817 818

  RETURN VALUE
    NONE
819 820 821 822 823 824 825 826 827 828 829 830 831 832
*/

static void clear_field_flag(TABLE *table)
{
  Field **ptr;
  DBUG_ENTER("clear_field_flag");

  for (ptr= table->field; *ptr; ptr++)
    (*ptr)->flags&= (~GET_FIXED_FIELDS_FLAG);
  DBUG_VOID_RETURN;
}


/*
unknown's avatar
unknown committed
833 834 835
  find_field_in_table_sef finds the field given its name. All fields get
  GET_FIXED_FIELDS_FLAG set.

836 837 838 839 840 841
  SYNOPSIS
    handle_list_of_fields()
    it                   A list of field names for the partition function
    table                TABLE object for which partition fields are set-up
    part_info            Reference to partitioning data structure
    sub_part             Is the table subpartitioned as well
unknown's avatar
unknown committed
842

843 844 845
  RETURN VALUE
    TRUE                 Fields in list of fields not part of table
    FALSE                All fields ok and array created
unknown's avatar
unknown committed
846

847
  DESCRIPTION
unknown's avatar
unknown committed
848 849 850 851
    This routine sets-up the partition field array for KEY partitioning, it
    also verifies that all fields in the list of fields is actually a part of
    the table.

852 853
*/

unknown's avatar
unknown committed
854

855 856 857
static bool handle_list_of_fields(List_iterator<char> it,
                                  TABLE *table,
                                  partition_info *part_info,
unknown's avatar
unknown committed
858
                                  bool is_sub_part)
859 860 861 862
{
  Field *field;
  bool result;
  char *field_name;
unknown's avatar
unknown committed
863
  bool is_list_empty= TRUE;
864 865 866 867
  DBUG_ENTER("handle_list_of_fields");

  while ((field_name= it++))
  {
unknown's avatar
unknown committed
868
    is_list_empty= FALSE;
869 870 871 872 873 874 875 876 877 878 879
    field= find_field_in_table_sef(table, field_name);
    if (likely(field != 0))
      field->flags|= GET_FIXED_FIELDS_FLAG;
    else
    {
      my_error(ER_FIELD_NOT_FOUND_PART_ERROR, MYF(0));
      clear_field_flag(table);
      result= TRUE;
      goto end;
    }
  }
880
  if (is_list_empty && part_info->part_type == HASH_PARTITION)
unknown's avatar
unknown committed
881 882 883 884
  {
    uint primary_key= table->s->primary_key;
    if (primary_key != MAX_KEY)
    {
885
      uint num_key_parts= table->key_info[primary_key].key_parts, i;
unknown's avatar
unknown committed
886 887 888
      /*
        In the case of an empty list we use primary key as partition key.
      */
889
      for (i= 0; i < num_key_parts; i++)
unknown's avatar
unknown committed
890 891 892 893 894 895 896
      {
        Field *field= table->key_info[primary_key].key_part[i].field;
        field->flags|= GET_FIXED_FIELDS_FLAG;
      }
    }
    else
    {
unknown's avatar
WL#2936  
unknown committed
897 898 899
      if (table->s->db_type()->partition_flags &&
          (table->s->db_type()->partition_flags() & HA_USE_AUTO_PARTITION) &&
          (table->s->db_type()->partition_flags() & HA_CAN_PARTITION))
unknown's avatar
unknown committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
      {
        /*
          This engine can handle automatic partitioning and there is no
          primary key. In this case we rely on that the engine handles
          partitioning based on a hidden key. Thus we allocate no
          array for partitioning fields.
        */
        DBUG_RETURN(FALSE);
      }
      else
      {
        my_error(ER_FIELD_NOT_FOUND_PART_ERROR, MYF(0));
        DBUG_RETURN(TRUE);
      }
    }
  }
  result= set_up_field_array(table, is_sub_part);
917 918 919 920 921
end:
  DBUG_RETURN(result);
}


922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
/*
  Support function to check if all VALUES * (expression) is of the
  right sign (no signed constants when unsigned partition function)

  SYNOPSIS
    check_signed_flag()
    part_info                Partition info object

  RETURN VALUES
    0                        No errors due to sign errors
    >0                       Sign error
*/

int check_signed_flag(partition_info *part_info)
{
  int error= 0;
  uint i= 0;
  if (part_info->part_type != HASH_PARTITION &&
      part_info->part_expr->unsigned_flag)
  {
    List_iterator<partition_element> part_it(part_info->partitions);
    do
    {
      partition_element *part_elem= part_it++;

      if (part_elem->signed_flag)
      {
949 950
        my_error(ER_PARTITION_CONST_DOMAIN_ERROR, MYF(0));
        error= ER_PARTITION_CONST_DOMAIN_ERROR;
951 952
        break;
      }
953
    } while (++i < part_info->num_parts);
954 955 956 957
  }
  return error;
}

Sergei Golubchik's avatar
Sergei Golubchik committed
958 959 960
/*
  init_lex_with_single_table and end_lex_with_single_table
  are now in sql_lex.cc
961 962
*/

963
/*
unknown's avatar
unknown committed
964 965 966 967 968
  The function uses a new feature in fix_fields where the flag 
  GET_FIXED_FIELDS_FLAG is set for all fields in the item tree.
  This field must always be reset before returning from the function
  since it is used for other purposes as well.

969 970 971 972
  SYNOPSIS
    fix_fields_part_func()
    thd                  The thread object
    func_expr            The item tree reference of the partition function
973
    table                The table object
974
    part_info            Reference to partitioning data structure
975
    is_sub_part          Is the table subpartitioned as well
976 977
    is_create_table_ind  Indicator of whether openfrm was called as part of
                         CREATE or ALTER TABLE
unknown's avatar
unknown committed
978

979 980 981 982
  RETURN VALUE
    TRUE                 An error occurred, something was wrong with the
                         partition function.
    FALSE                Ok, a partition field array was created
unknown's avatar
unknown committed
983

984
  DESCRIPTION
unknown's avatar
unknown committed
985 986 987 988 989 990
    This function is used to build an array of partition fields for the
    partitioning function and subpartitioning function. The partitioning
    function is an item tree that must reference at least one field in the
    table. This is checked first in the parser that the function doesn't
    contain non-cacheable parts (like a random function) and by checking
    here that the function isn't a constant function.
991 992 993 994 995 996 997

    Calculate the number of fields in the partition function.
    Use it allocate memory for array of Field pointers.
    Initialise array of field pointers. Use information set when
    calling fix_fields and reset it immediately after.
    The get_fields_in_item_tree activates setting of bit in flags
    on the field object.
unknown's avatar
unknown committed
998
*/
999

1000
static bool fix_fields_part_func(THD *thd, Item* func_expr, TABLE *table,
1001
                          bool is_sub_part, bool is_create_table_ind)
unknown's avatar
unknown committed
1002
{
1003
  partition_info *part_info= table->part_info;
1004 1005
  bool result= TRUE;
  int error;
1006 1007
  LEX *old_lex= thd->lex;
  LEX lex;
1008 1009
  DBUG_ENTER("fix_fields_part_func");

1010 1011
  if (init_lex_with_single_table(thd, table, &lex))
    goto end;
1012

1013 1014
  func_expr->walk(&Item::change_context_processor, 0,
                  (uchar*) &lex.select_lex.context);
1015
  thd->where= "partition function";
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
  /*
    In execution we must avoid the use of thd->change_item_tree since
    we might release memory before statement is completed. We do this
    by temporarily setting the stmt_arena->mem_root to be the mem_root
    of the table object, this also ensures that any memory allocated
    during fix_fields will not be released at end of execution of this
    statement. Thus the item tree will remain valid also in subsequent
    executions of this table object. We do however not at the moment
    support allocations during execution of val_int so any item class
    that does this during val_int must be disallowed as partition
    function.
    SEE Bug #21658
1028

1029 1030 1031
    This is a tricky call to prepare for since it can have a large number
    of interesting side effects, both desirable and undesirable.
  */
1032 1033 1034 1035 1036
  {
    const bool save_agg_field= thd->lex->current_select->non_agg_field_used();
    const bool save_agg_func=  thd->lex->current_select->agg_func_used();
    const nesting_map saved_allow_sum_func= thd->lex->allow_sum_func;
    thd->lex->allow_sum_func= 0;
1037

Sergei Golubchik's avatar
Sergei Golubchik committed
1038 1039
    if (!(error= func_expr->fix_fields(thd, (Item**)&func_expr)))
      func_expr->walk(&Item::vcol_in_partition_func_processor, 0, NULL);
1040

1041
    /*
1042
      Restore agg_field/agg_func  and allow_sum_func,
1043 1044 1045 1046 1047 1048
      fix_fields should not affect mysql_select later, see Bug#46923.
    */
    thd->lex->current_select->set_non_agg_field_used(save_agg_field);
    thd->lex->current_select->set_agg_func_used(save_agg_func);
    thd->lex->allow_sum_func= saved_allow_sum_func;
  }
1049 1050 1051
  if (unlikely(error))
  {
    DBUG_PRINT("info", ("Field in partition function not part of table"));
1052
    clear_field_flag(table);
1053 1054 1055 1056
    goto end;
  }
  if (unlikely(func_expr->const_item()))
  {
1057
    my_error(ER_WRONG_EXPR_IN_PARTITION_FUNC_ERROR, MYF(0));
1058 1059 1060
    clear_field_flag(table);
    goto end;
  }
1061 1062

  /*
1063 1064 1065 1066 1067
    We don't allow creating partitions with expressions with non matching
    arguments as a (sub)partitioning function,
    but we want to allow such expressions when opening existing tables for
    easier maintenance. This exception should be deprecated at some point
    in future so that we always throw an error.
1068
  */
1069
  if (func_expr->walk(&Item::check_valid_arguments_processor,
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
                      0, NULL))
  {
    if (is_create_table_ind)
    {
      my_error(ER_WRONG_EXPR_IN_PARTITION_FUNC_ERROR, MYF(0));
      goto end;
    }
    else
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                   ER_WRONG_EXPR_IN_PARTITION_FUNC_ERROR,
                   ER(ER_WRONG_EXPR_IN_PARTITION_FUNC_ERROR));
  }

1083 1084
  if ((!is_sub_part) && (error= check_signed_flag(part_info)))
    goto end;
1085
  result= set_up_field_array(table, is_sub_part);
1086
end:
1087 1088 1089 1090 1091
  end_lex_with_single_table(thd, table, old_lex);
#if !defined(DBUG_OFF)
  func_expr->walk(&Item::change_context_processor, 0,
                  (uchar*) 0);
#endif
1092 1093 1094 1095 1096
  DBUG_RETURN(result);
}


/*
unknown's avatar
unknown committed
1097 1098
  Check that the primary key contains all partition fields if defined

1099 1100 1101
  SYNOPSIS
    check_primary_key()
    table                TABLE object for which partition fields are set-up
unknown's avatar
unknown committed
1102

1103 1104 1105 1106 1107
  RETURN VALUES
    TRUE                 Not all fields in partitioning function was part
                         of primary key
    FALSE                Ok, all fields of partitioning function were part
                         of primary key
unknown's avatar
unknown committed
1108 1109 1110 1111 1112 1113

  DESCRIPTION
    This function verifies that if there is a primary key that it contains
    all the fields of the partition function.
    This is a temporary limitation that will hopefully be removed after a
    while.
1114 1115 1116 1117 1118
*/

static bool check_primary_key(TABLE *table)
{
  uint primary_key= table->s->primary_key;
unknown's avatar
unknown committed
1119 1120
  bool all_fields, some_fields;
  bool result= FALSE;
1121 1122 1123 1124 1125
  DBUG_ENTER("check_primary_key");

  if (primary_key < MAX_KEY)
  {
    set_indicator_in_key_fields(table->key_info+primary_key);
unknown's avatar
unknown committed
1126
    check_fields_in_PF(table->part_info->full_part_field_array,
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
                        &all_fields, &some_fields);
    clear_indicator_in_key_fields(table->key_info+primary_key);
    if (unlikely(!all_fields))
    {
      my_error(ER_UNIQUE_KEY_NEED_ALL_FIELDS_IN_PF,MYF(0),"PRIMARY KEY");
      result= TRUE;
    }
  }
  DBUG_RETURN(result);
}


/*
unknown's avatar
unknown committed
1140 1141
  Check that unique keys contains all partition fields

1142 1143 1144
  SYNOPSIS
    check_unique_keys()
    table                TABLE object for which partition fields are set-up
unknown's avatar
unknown committed
1145

1146 1147 1148 1149 1150
  RETURN VALUES
    TRUE                 Not all fields in partitioning function was part
                         of all unique keys
    FALSE                Ok, all fields of partitioning function were part
                         of unique keys
unknown's avatar
unknown committed
1151 1152 1153 1154 1155 1156

  DESCRIPTION
    This function verifies that if there is a unique index that it contains
    all the fields of the partition function.
    This is a temporary limitation that will hopefully be removed after a
    while.
1157 1158 1159 1160
*/

static bool check_unique_keys(TABLE *table)
{
unknown's avatar
unknown committed
1161 1162 1163 1164
  bool all_fields, some_fields;
  bool result= FALSE;
  uint keys= table->s->keys;
  uint i;
1165
  DBUG_ENTER("check_unique_keys");
unknown's avatar
unknown committed
1166

1167 1168 1169 1170 1171
  for (i= 0; i < keys; i++)
  {
    if (table->key_info[i].flags & HA_NOSAME) //Unique index
    {
      set_indicator_in_key_fields(table->key_info+i);
unknown's avatar
unknown committed
1172
      check_fields_in_PF(table->part_info->full_part_field_array,
1173 1174 1175 1176 1177 1178 1179 1180 1181 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
                         &all_fields, &some_fields);
      clear_indicator_in_key_fields(table->key_info+i);
      if (unlikely(!all_fields))
      {
        my_error(ER_UNIQUE_KEY_NEED_ALL_FIELDS_IN_PF,MYF(0),"UNIQUE INDEX");
        result= TRUE;
        break;
      }
    }
  }
  DBUG_RETURN(result);
}


/*
  An important optimisation is whether a range on a field can select a subset
  of the partitions.
  A prerequisite for this to happen is that the PF is a growing function OR
  a shrinking function.
  This can never happen for a multi-dimensional PF. Thus this can only happen
  with PF with at most one field involved in the PF.
  The idea is that if the function is a growing function and you know that
  the field of the PF is 4 <= A <= 6 then we can convert this to a range
  in the PF instead by setting the range to PF(4) <= PF(A) <= PF(6). In the
  case of RANGE PARTITIONING and LIST PARTITIONING this can be used to
  calculate a set of partitions rather than scanning all of them.
  Thus the following prerequisites are there to check if sets of partitions
  can be found.
  1) Only possible for RANGE and LIST partitioning (not for subpartitioning)
  2) Only possible if PF only contains 1 field
  3) Possible if PF is a growing function of the field
  4) Possible if PF is a shrinking function of the field
  OBSERVATION:
  1) IF f1(A) is a growing function AND f2(A) is a growing function THEN
     f1(A) + f2(A) is a growing function
     f1(A) * f2(A) is a growing function if f1(A) >= 0 and f2(A) >= 0
  2) IF f1(A) is a growing function and f2(A) is a shrinking function THEN
     f1(A) / f2(A) is a growing function if f1(A) >= 0 and f2(A) > 0
  3) IF A is a growing function then a function f(A) that removes the
     least significant portion of A is a growing function
     E.g. DATE(datetime) is a growing function
     MONTH(datetime) is not a growing/shrinking function
  4) IF f1(A) is a growing function and f2(A) is a growing function THEN
     f1(f2(A)) and f2(f1(A)) are also growing functions
  5) IF f1(A) is a shrinking function and f2(A) is a growing function THEN
     f1(f2(A)) is a shrinking function and f2(f1(A)) is a shrinking function
  6) f1(A) = A is a growing function
  7) f1(A) = A*a + b (where a and b are constants) is a growing function

  By analysing the item tree of the PF we can use these deducements and
  derive whether the PF is a growing function or a shrinking function or
  neither of it.

  If the PF is range capable then a flag is set on the table object
  indicating this to notify that we can use also ranges on the field
  of the PF to deduce a set of partitions if the fields of the PF were
  not all fully bound.
unknown's avatar
unknown committed
1230

1231 1232 1233
  SYNOPSIS
    check_range_capable_PF()
    table                TABLE object for which partition fields are set-up
unknown's avatar
unknown committed
1234

1235 1236 1237 1238 1239 1240 1241
  DESCRIPTION
    Support for this is not implemented yet.
*/

void check_range_capable_PF(TABLE *table)
{
  DBUG_ENTER("check_range_capable_PF");
unknown's avatar
unknown committed
1242

1243 1244 1245 1246
  DBUG_VOID_RETURN;
}


unknown's avatar
unknown committed
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
/*
  Set up partition bitmap

  SYNOPSIS
    set_up_partition_bitmap()
    thd                  Thread object
    part_info            Reference to partitioning data structure

  RETURN VALUE
    TRUE                 Memory allocation failure
    FALSE                Success

  DESCRIPTION
    Allocate memory for bitmap of the partitioned table
    and initialise it.
*/

static bool set_up_partition_bitmap(THD *thd, partition_info *part_info)
{
  uint32 *bitmap_buf;
1267 1268 1269
  uint bitmap_bits= part_info->num_subparts? 
                     (part_info->num_subparts* part_info->num_parts):
                      part_info->num_parts;
unknown's avatar
unknown committed
1270 1271 1272 1273 1274 1275 1276 1277 1278
  uint bitmap_bytes= bitmap_buffer_size(bitmap_bits);
  DBUG_ENTER("set_up_partition_bitmap");

  if (!(bitmap_buf= (uint32*)thd->alloc(bitmap_bytes)))
  {
    mem_alloc_error(bitmap_bytes);
    DBUG_RETURN(TRUE);
  }
  bitmap_init(&part_info->used_partitions, bitmap_buf, bitmap_bytes*8, FALSE);
unknown's avatar
unknown committed
1279
  bitmap_set_all(&part_info->used_partitions);
unknown's avatar
unknown committed
1280 1281 1282 1283
  DBUG_RETURN(FALSE);
}


1284 1285
/*
  Set up partition key maps
unknown's avatar
unknown committed
1286

1287 1288 1289 1290
  SYNOPSIS
    set_up_partition_key_maps()
    table                TABLE object for which partition fields are set-up
    part_info            Reference to partitioning data structure
unknown's avatar
unknown committed
1291

1292 1293
  RETURN VALUES
    None
unknown's avatar
unknown committed
1294

1295
  DESCRIPTION
unknown's avatar
unknown committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
    This function sets up a couple of key maps to be able to quickly check
    if an index ever can be used to deduce the partition fields or even
    a part of the fields of the  partition function.
    We set up the following key_map's.
    PF = Partition Function
    1) All fields of the PF is set even by equal on the first fields in the
       key
    2) All fields of the PF is set if all fields of the key is set
    3) At least one field in the PF is set if all fields is set
    4) At least one field in the PF is part of the key
1306 1307 1308 1309 1310
*/

static void set_up_partition_key_maps(TABLE *table,
                                      partition_info *part_info)
{
unknown's avatar
unknown committed
1311 1312
  uint keys= table->s->keys;
  uint i;
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
  bool all_fields, some_fields;
  DBUG_ENTER("set_up_partition_key_maps");

  part_info->all_fields_in_PF.clear_all();
  part_info->all_fields_in_PPF.clear_all();
  part_info->all_fields_in_SPF.clear_all();
  part_info->some_fields_in_PF.clear_all();
  for (i= 0; i < keys; i++)
  {
    set_indicator_in_key_fields(table->key_info+i);
    check_fields_in_PF(part_info->full_part_field_array,
                       &all_fields, &some_fields);
    if (all_fields)
      part_info->all_fields_in_PF.set_bit(i);
    if (some_fields)
      part_info->some_fields_in_PF.set_bit(i);
1329
    if (part_info->is_sub_partitioned())
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
    {
      check_fields_in_PF(part_info->part_field_array,
                         &all_fields, &some_fields);
      if (all_fields)
        part_info->all_fields_in_PPF.set_bit(i);
      check_fields_in_PF(part_info->subpart_field_array,
                         &all_fields, &some_fields);
      if (all_fields)
        part_info->all_fields_in_SPF.set_bit(i);
    }
    clear_indicator_in_key_fields(table->key_info+i);
  }
  DBUG_VOID_RETURN;
}


/*
unknown's avatar
unknown committed
1347 1348
  Set up function pointers for partition function

1349
  SYNOPSIS
unknown's avatar
unknown committed
1350
    set_up_partition_func_pointers()
1351
    part_info            Reference to partitioning data structure
unknown's avatar
unknown committed
1352 1353 1354 1355 1356 1357 1358 1359 1360

  RETURN VALUE
    NONE

  DESCRIPTION
    Set-up all function pointers for calculation of partition id,
    subpartition id and the upper part in subpartitioning. This is to speed up
    execution of get_partition_id which is executed once every record to be
    written and deleted and twice for updates.
1361 1362 1363 1364
*/

static void set_up_partition_func_pointers(partition_info *part_info)
{
unknown's avatar
unknown committed
1365 1366
  DBUG_ENTER("set_up_partition_func_pointers");

1367
  if (part_info->is_sub_partitioned())
1368
  {
1369
    part_info->get_partition_id= get_partition_id_with_sub;
1370 1371
    if (part_info->part_type == RANGE_PARTITION)
    {
1372 1373 1374 1375
      if (part_info->column_list)
        part_info->get_part_partition_id= get_partition_id_range_col;
      else
        part_info->get_part_partition_id= get_partition_id_range;
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
      if (part_info->list_of_subpart_fields)
      {
        if (part_info->linear_hash_ind)
          part_info->get_subpartition_id= get_partition_id_linear_key_sub;
        else
          part_info->get_subpartition_id= get_partition_id_key_sub;
      }
      else
      {
        if (part_info->linear_hash_ind)
          part_info->get_subpartition_id= get_partition_id_linear_hash_sub;
        else
          part_info->get_subpartition_id= get_partition_id_hash_sub;
      }
    }
unknown's avatar
unknown committed
1391
    else /* LIST Partitioning */
1392
    {
1393 1394 1395 1396
      if (part_info->column_list)
        part_info->get_part_partition_id= get_partition_id_list_col;
      else
        part_info->get_part_partition_id= get_partition_id_list;
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
      if (part_info->list_of_subpart_fields)
      {
        if (part_info->linear_hash_ind)
          part_info->get_subpartition_id= get_partition_id_linear_key_sub;
        else
          part_info->get_subpartition_id= get_partition_id_key_sub;
      }
      else
      {
        if (part_info->linear_hash_ind)
          part_info->get_subpartition_id= get_partition_id_linear_hash_sub;
        else
          part_info->get_subpartition_id= get_partition_id_hash_sub;
      }
    }
  }
unknown's avatar
unknown committed
1413
  else /* No subpartitioning */
1414 1415 1416 1417
  {
    part_info->get_part_partition_id= NULL;
    part_info->get_subpartition_id= NULL;
    if (part_info->part_type == RANGE_PARTITION)
1418 1419 1420 1421 1422 1423
    {
      if (part_info->column_list)
        part_info->get_partition_id= get_partition_id_range_col;
      else
        part_info->get_partition_id= get_partition_id_range;
    }
1424
    else if (part_info->part_type == LIST_PARTITION)
1425 1426 1427 1428 1429 1430
    {
      if (part_info->column_list)
        part_info->get_partition_id= get_partition_id_list_col;
      else
        part_info->get_partition_id= get_partition_id_list;
    }
unknown's avatar
unknown committed
1431
    else /* HASH partitioning */
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    {
      if (part_info->list_of_part_fields)
      {
        if (part_info->linear_hash_ind)
          part_info->get_partition_id= get_partition_id_linear_key_nosub;
        else
          part_info->get_partition_id= get_partition_id_key_nosub;
      }
      else
      {
        if (part_info->linear_hash_ind)
          part_info->get_partition_id= get_partition_id_linear_hash_nosub;
        else
          part_info->get_partition_id= get_partition_id_hash_nosub;
      }
    }
  }
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
  /*
    We need special functions to handle character sets since they require copy
    of field pointers and restore afterwards. For subpartitioned tables we do
    the copy and restore individually on the part and subpart parts. For non-
    subpartitioned tables we use the same functions as used for the parts part
    of subpartioning.
    Thus for subpartitioned tables the get_partition_id is always
    get_partition_id_with_sub, even when character sets exists.
  */
  if (part_info->part_charset_field_array)
1459
  {
1460 1461 1462
    if (part_info->is_sub_partitioned())
    {
      DBUG_ASSERT(part_info->get_part_partition_id);
1463 1464
      if (!part_info->column_list)
      {
Mikael Ronstrom's avatar
Mikael Ronstrom committed
1465
        part_info->get_part_partition_id_charset=
1466
          part_info->get_part_partition_id;
1467 1468
        part_info->get_part_partition_id= get_part_id_charset_func_part;
      }
1469
    }
1470
    else
1471 1472
    {
      DBUG_ASSERT(part_info->get_partition_id);
1473 1474 1475 1476 1477
      if (!part_info->column_list)
      {
        part_info->get_part_partition_id_charset= part_info->get_partition_id;
        part_info->get_part_partition_id= get_part_id_charset_func_part;
      }
1478
    }
1479
  }
1480
  if (part_info->subpart_charset_field_array)
1481 1482 1483 1484
  {
    DBUG_ASSERT(part_info->get_subpartition_id);
    part_info->get_subpartition_id_charset=
          part_info->get_subpartition_id;
1485
    part_info->get_subpartition_id= get_part_id_charset_func_subpart;
1486
  }
unknown's avatar
unknown committed
1487
  DBUG_VOID_RETURN;
1488
}
unknown's avatar
unknown committed
1489 1490


1491 1492
/*
  For linear hashing we need a mask which is on the form 2**n - 1 where
1493
  2**n >= num_parts. Thus if num_parts is 6 then mask is 2**3 - 1 = 8 - 1 = 7.
unknown's avatar
unknown committed
1494

1495 1496 1497
  SYNOPSIS
    set_linear_hash_mask()
    part_info            Reference to partitioning data structure
1498
    num_parts            Number of parts in linear hash partitioning
unknown's avatar
unknown committed
1499 1500 1501

  RETURN VALUE
    NONE
1502 1503
*/

1504
void set_linear_hash_mask(partition_info *part_info, uint num_parts)
1505 1506
{
  uint mask;
unknown's avatar
unknown committed
1507

1508
  for (mask= 1; mask < num_parts; mask<<=1)
1509 1510 1511 1512 1513 1514 1515 1516
    ;
  part_info->linear_hash_mask= mask - 1;
}


/*
  This function calculates the partition id provided the result of the hash
  function using linear hashing parameters, mask and number of partitions.
unknown's avatar
unknown committed
1517

1518 1519 1520 1521
  SYNOPSIS
    get_part_id_from_linear_hash()
    hash_value          Hash value calculated by HASH function or KEY function
    mask                Mask calculated previously by set_linear_hash_mask
1522
    num_parts           Number of partitions in HASH partitioned part
unknown's avatar
unknown committed
1523

1524 1525
  RETURN VALUE
    part_id             The calculated partition identity (starting at 0)
unknown's avatar
unknown committed
1526

1527 1528 1529 1530 1531 1532 1533 1534
  DESCRIPTION
    The partition is calculated according to the theory of linear hashing.
    See e.g. Linear hashing: a new tool for file and table addressing,
    Reprinted from VLDB-80 in Readings Database Systems, 2nd ed, M. Stonebraker
    (ed.), Morgan Kaufmann 1994.
*/

static uint32 get_part_id_from_linear_hash(longlong hash_value, uint mask,
1535
                                           uint num_parts)
1536 1537
{
  uint32 part_id= (uint32)(hash_value & mask);
unknown's avatar
unknown committed
1538

1539
  if (part_id >= num_parts)
1540 1541
  {
    uint new_mask= ((mask + 1) >> 1) - 1;
1542
    part_id= (uint32)(hash_value & new_mask);
1543 1544 1545 1546
  }
  return part_id;
}

1547

1548 1549 1550
/*
  Check if a particular field is in need of character set
  handling for partition functions.
1551

1552 1553 1554
  SYNOPSIS
    field_is_partition_charset()
    field                         The field to check
1555

1556 1557 1558 1559 1560 1561 1562
  RETURN VALUES
    FALSE                        Not in need of character set handling
    TRUE                         In need of character set handling
*/

bool field_is_partition_charset(Field *field)
{
unknown's avatar
Bug fix  
unknown committed
1563 1564
  if (!(field->type() == MYSQL_TYPE_STRING) &&
      !(field->type() == MYSQL_TYPE_VARCHAR))
1565 1566 1567
    return FALSE;
  {
    CHARSET_INFO *cs= ((Field_str*)field)->charset();
unknown's avatar
Bug fix  
unknown committed
1568
    if (!(field->type() == MYSQL_TYPE_STRING) ||
1569 1570 1571 1572 1573 1574 1575
        !(cs->state & MY_CS_BINSORT))
      return TRUE;
    return FALSE;
  }
}


1576
/*
1577
  Check that partition function doesn't contain any forbidden
1578
  character sets and collations.
1579

1580
  SYNOPSIS
1581
    check_part_func_fields()
1582
    ptr                                 Array of Field pointers
1583 1584
    ok_with_charsets                    Will we report allowed charset
                                        fields as ok
1585 1586 1587
  RETURN VALUES
    FALSE                               Success
    TRUE                                Error
1588

1589 1590 1591 1592 1593
  DESCRIPTION
    We will check in this routine that the fields of the partition functions
    do not contain unallowed parts. It can also be used to check if there
    are fields that require special care by calling my_strnxfrm before
    calling the functions to calculate partition id.
1594 1595
*/

1596
bool check_part_func_fields(Field **ptr, bool ok_with_charsets)
1597 1598
{
  Field *field;
unknown's avatar
Bug fix  
unknown committed
1599
  DBUG_ENTER("check_part_func_fields");
1600

1601 1602
  while ((field= *(ptr++)))
  {
1603 1604 1605 1606 1607
    /*
      For CHAR/VARCHAR fields we need to take special precautions.
      Binary collation with CHAR is automatically supported. Other
      types need some kind of standardisation function handling
    */
1608
    if (field_is_partition_charset(field))
1609 1610
    {
      CHARSET_INFO *cs= ((Field_str*)field)->charset();
1611 1612 1613 1614 1615 1616
      if (!ok_with_charsets ||
          cs->mbmaxlen > 1 ||
          cs->strxfrm_multiply > 1)
      {
        DBUG_RETURN(TRUE);
      }
1617 1618
    }
  }
1619
  DBUG_RETURN(FALSE);
1620 1621 1622
}


1623
/*
unknown's avatar
unknown committed
1624 1625
  fix partition functions

1626 1627 1628 1629
  SYNOPSIS
    fix_partition_func()
    thd                  The thread object
    table                TABLE object for which partition fields are set-up
1630
    is_create_table_ind  Indicator of whether openfrm was called as part of
unknown's avatar
unknown committed
1631
                         CREATE or ALTER TABLE
unknown's avatar
unknown committed
1632

1633
  RETURN VALUE
unknown's avatar
unknown committed
1634 1635
    TRUE                 Error
    FALSE                Success
unknown's avatar
unknown committed
1636

1637 1638 1639 1640
  DESCRIPTION
    The name parameter contains the full table name and is used to get the
    database name of the table which is used to set-up a correct
    TABLE_LIST object for use in fix_fields.
unknown's avatar
unknown committed
1641 1642 1643 1644 1645 1646 1647

NOTES
    This function is called as part of opening the table by opening the .frm
    file. It is a part of CREATE TABLE to do this so it is quite permissible
    that errors due to erroneus syntax isn't found until we come here.
    If the user has used a non-existing field in the table is one such example
    of an error that is not discovered until here.
1648 1649
*/

1650
bool fix_partition_func(THD *thd, TABLE *table,
unknown's avatar
unknown committed
1651
                        bool is_create_table_ind)
1652 1653
{
  bool result= TRUE;
unknown's avatar
unknown committed
1654
  partition_info *part_info= table->part_info;
1655
  enum_mark_columns save_mark_used_columns= thd->mark_used_columns;
1656 1657
  DBUG_ENTER("fix_partition_func");

unknown's avatar
unknown committed
1658 1659 1660 1661
  if (part_info->fixed)
  {
    DBUG_RETURN(FALSE);
  }
1662 1663
  thd->mark_used_columns= MARK_COLUMNS_NONE;
  DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
1664

1665
  if (!is_create_table_ind ||
1666
       thd->lex->sql_command != SQLCOM_CREATE_TABLE)
unknown's avatar
unknown committed
1667
  {
1668
    if (partition_default_handling(table, part_info,
1669
                                   is_create_table_ind,
1670
                                   table->s->normalized_path.str))
unknown's avatar
unknown committed
1671 1672 1673 1674
    {
      DBUG_RETURN(TRUE);
    }
  }
1675
  if (part_info->is_sub_partitioned())
1676 1677 1678
  {
    DBUG_ASSERT(part_info->subpart_type == HASH_PARTITION);
    /*
unknown's avatar
unknown committed
1679 1680
      Subpartition is defined. We need to verify that subpartitioning
      function is correct.
1681 1682
    */
    if (part_info->linear_hash_ind)
1683
      set_linear_hash_mask(part_info, part_info->num_subparts);
1684 1685 1686 1687 1688 1689 1690 1691
    if (part_info->list_of_subpart_fields)
    {
      List_iterator<char> it(part_info->subpart_field_list);
      if (unlikely(handle_list_of_fields(it, table, part_info, TRUE)))
        goto end;
    }
    else
    {
1692
      if (unlikely(fix_fields_part_func(thd, part_info->subpart_expr,
1693
                                        table, TRUE, is_create_table_ind)))
1694 1695 1696
        goto end;
      if (unlikely(part_info->subpart_expr->result_type() != INT_RESULT))
      {
1697
        part_info->report_part_expr_error(TRUE);
1698 1699 1700 1701 1702 1703
        goto end;
      }
    }
  }
  DBUG_ASSERT(part_info->part_type != NOT_A_PARTITION);
  /*
unknown's avatar
unknown committed
1704 1705
    Partition is defined. We need to verify that partitioning
    function is correct.
1706 1707 1708 1709
  */
  if (part_info->part_type == HASH_PARTITION)
  {
    if (part_info->linear_hash_ind)
1710
      set_linear_hash_mask(part_info, part_info->num_parts);
1711 1712 1713 1714 1715 1716 1717 1718
    if (part_info->list_of_part_fields)
    {
      List_iterator<char> it(part_info->part_field_list);
      if (unlikely(handle_list_of_fields(it, table, part_info, FALSE)))
        goto end;
    }
    else
    {
1719
      if (unlikely(fix_fields_part_func(thd, part_info->part_expr,
1720
                                        table, FALSE, is_create_table_ind)))
1721 1722 1723
        goto end;
      if (unlikely(part_info->part_expr->result_type() != INT_RESULT))
      {
1724
        part_info->report_part_expr_error(FALSE);
1725 1726 1727
        goto end;
      }
    }
1728
    part_info->fixed= TRUE;
1729 1730 1731
  }
  else
  {
1732
    const char *error_str;
1733 1734 1735 1736 1737 1738 1739 1740 1741
    if (part_info->column_list)
    {
      List_iterator<char> it(part_info->part_field_list);
      if (unlikely(handle_list_of_fields(it, table, part_info, FALSE)))
        goto end;
    }
    else
    {
      if (unlikely(fix_fields_part_func(thd, part_info->part_expr,
1742
                                        table, FALSE, is_create_table_ind)))
1743 1744
        goto end;
    }
1745
    part_info->fixed= TRUE;
1746 1747
    if (part_info->part_type == RANGE_PARTITION)
    {
1748
      error_str= partition_keywords[PKW_RANGE].str; 
1749
      if (unlikely(part_info->check_range_constants(thd)))
1750 1751 1752 1753
        goto end;
    }
    else if (part_info->part_type == LIST_PARTITION)
    {
1754
      error_str= partition_keywords[PKW_LIST].str; 
1755
      if (unlikely(part_info->check_list_constants(thd)))
1756 1757 1758 1759 1760 1761 1762 1763
        goto end;
    }
    else
    {
      DBUG_ASSERT(0);
      my_error(ER_INCONSISTENT_PARTITION_INFO_ERROR, MYF(0));
      goto end;
    }
1764
    if (unlikely(part_info->num_parts < 1))
1765 1766 1767 1768
    {
      my_error(ER_PARTITIONS_MUST_BE_DEFINED_ERROR, MYF(0), error_str);
      goto end;
    }
1769 1770
    if (unlikely(!part_info->column_list &&
                  part_info->part_expr->result_type() != INT_RESULT))
1771
    {
1772
      part_info->report_part_expr_error(FALSE);
1773 1774 1775
      goto end;
    }
  }
unknown's avatar
unknown committed
1776
  if (((part_info->part_type != HASH_PARTITION ||
1777 1778 1779
        part_info->list_of_part_fields == FALSE) &&
       !part_info->column_list &&
       check_part_func_fields(part_info->part_field_array, TRUE)) ||
1780
      (part_info->list_of_subpart_fields == FALSE &&
unknown's avatar
unknown committed
1781
       part_info->is_sub_partitioned() &&
1782
       check_part_func_fields(part_info->subpart_field_array, TRUE)))
1783
  {
1784 1785 1786 1787
    /*
      Range/List/HASH (but not KEY) and not COLUMNS or HASH subpartitioning
      with columns in the partitioning expression using unallowed charset.
    */
1788 1789 1790
    my_error(ER_PARTITION_FUNCTION_IS_NOT_ALLOWED, MYF(0));
    goto end;
  }
1791
  if (unlikely(create_full_part_field_array(thd, table, part_info)))
1792 1793 1794
    goto end;
  if (unlikely(check_primary_key(table)))
    goto end;
unknown's avatar
WL#2936  
unknown committed
1795 1796
  if (unlikely((!(table->s->db_type()->partition_flags &&
      (table->s->db_type()->partition_flags() & HA_CAN_PARTITION_UNIQUE))) &&
1797 1798
               check_unique_keys(table)))
    goto end;
unknown's avatar
unknown committed
1799 1800
  if (unlikely(set_up_partition_bitmap(thd, part_info)))
    goto end;
1801
  if (unlikely(part_info->set_up_charset_field_preps()))
1802 1803 1804 1805
  {
    my_error(ER_PARTITION_FUNCTION_IS_NOT_ALLOWED, MYF(0));
    goto end;
  }
1806 1807 1808 1809 1810
  if (unlikely(part_info->check_partition_field_length()))
  {
    my_error(ER_PARTITION_FIELDS_TOO_LONG, MYF(0));
    goto end;
  }
1811 1812 1813
  check_range_capable_PF(table);
  set_up_partition_key_maps(table, part_info);
  set_up_partition_func_pointers(part_info);
unknown's avatar
unknown committed
1814
  set_up_range_analysis_info(part_info);
1815 1816
  result= FALSE;
end:
1817 1818
  thd->mark_used_columns= save_mark_used_columns;
  DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
  DBUG_RETURN(result);
}


/*
  The code below is support routines for the reverse parsing of the 
  partitioning syntax. This feature is very useful to generate syntax for
  all default values to avoid all default checking when opening the frm
  file. It is also used when altering the partitioning by use of various
  ALTER TABLE commands. Finally it is used for SHOW CREATE TABLES.
*/

static int add_write(File fptr, const char *buf, uint len)
{
Marc Alff's avatar
Marc Alff committed
1833
  uint ret_code= mysql_file_write(fptr, (const uchar*)buf, len, MYF(MY_FNABP));
unknown's avatar
unknown committed
1834

1835
  if (likely(ret_code == 0))
1836 1837 1838 1839 1840
    return 0;
  else
    return 1;
}

1841 1842 1843 1844 1845
static int add_string_object(File fptr, String *string)
{
  return add_write(fptr, string->ptr(), string->length());
}

1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
static int add_string(File fptr, const char *string)
{
  return add_write(fptr, string, strlen(string));
}

static int add_string_len(File fptr, const char *string, uint len)
{
  return add_write(fptr, string, len);
}

static int add_space(File fptr)
{
  return add_string(fptr, space_str);
}

static int add_comma(File fptr)
{
  return add_string(fptr, comma_str);
}

static int add_equal(File fptr)
{
  return add_string(fptr, equal_str);
}

static int add_end_parenthesis(File fptr)
{
  return add_string(fptr, end_paren_str);
}

static int add_begin_parenthesis(File fptr)
{
  return add_string(fptr, begin_paren_str);
}

static int add_part_key_word(File fptr, const char *key_string)
{
  int err= add_string(fptr, key_string);
  err+= add_space(fptr);
1885
  return err;
1886 1887 1888 1889
}

static int add_partition(File fptr)
{
1890
  char buff[22];
1891 1892 1893 1894 1895 1896 1897
  strxmov(buff, part_str, space_str, NullS);
  return add_string(fptr, buff);
}

static int add_subpartition(File fptr)
{
  int err= add_string(fptr, sub_str);
unknown's avatar
unknown committed
1898

1899 1900 1901 1902 1903
  return err + add_partition(fptr);
}

static int add_partition_by(File fptr)
{
1904
  char buff[22];
1905 1906 1907 1908 1909 1910 1911
  strxmov(buff, part_str, space_str, by_str, space_str, NullS);
  return add_string(fptr, buff);
}

static int add_subpartition_by(File fptr)
{
  int err= add_string(fptr, sub_str);
unknown's avatar
unknown committed
1912

1913 1914 1915
  return err + add_partition_by(fptr);
}

1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
static int add_name_string(File fptr, const char *name)
{
  int err;
  String name_string("", 0, system_charset_info);
  THD *thd= current_thd;
  ulonglong save_options= thd->variables.option_bits;
  thd->variables.option_bits&= ~OPTION_QUOTE_SHOW_CREATE;
  append_identifier(thd, &name_string, name, strlen(name));
  thd->variables.option_bits= save_options;
  err= add_string_object(fptr, &name_string);
  return err;
}

1929
static int add_part_field_list(File fptr, List<char> field_list)
1930
{
1931
  uint i, num_fields;
1932
  int err= 0;
unknown's avatar
unknown committed
1933

1934
  List_iterator<char> part_it(field_list);
1935
  num_fields= field_list.elements;
1936
  i= 0;
1937
  err+= add_begin_parenthesis(fptr);
1938
  while (i < num_fields)
1939
  {
1940
    err+= add_name_string(fptr, part_it++);
1941
    if (i != (num_fields-1))
1942
      err+= add_comma(fptr);
unknown's avatar
unknown committed
1943 1944
    i++;
  }
1945
  err+= add_end_parenthesis(fptr);
1946 1947 1948 1949 1950
  return err;
}

static int add_int(File fptr, longlong number)
{
1951
  char buff[32];
1952 1953 1954 1955
  llstr(number, buff);
  return add_string(fptr, buff);
}

1956 1957 1958
static int add_uint(File fptr, ulonglong number)
{
  char buff[32];
Sergei Golubchik's avatar
Sergei Golubchik committed
1959
  longlong2str(number, buff, 10);
1960 1961 1962
  return add_string(fptr, buff);
}

1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
/*
   Must escape strings in partitioned tables frm-files,
   parsing it later with mysql_unpack_partition will fail otherwise.
*/
static int add_quoted_string(File fptr, const char *quotestr)
{
  String orgstr(quotestr, system_charset_info);
  String escapedstr;
  int err= add_string(fptr, "'");
  err+= append_escaped(&escapedstr, &orgstr);
1973
  err+= add_string(fptr, escapedstr.c_ptr_safe());
1974 1975 1976
  return err + add_string(fptr, "'");
}

1977
static int add_keyword_string(File fptr, const char *keyword,
1978
                              bool should_use_quotes, 
1979 1980 1981
                              const char *keystr)
{
  int err= add_string(fptr, keyword);
unknown's avatar
unknown committed
1982

1983 1984 1985
  err+= add_space(fptr);
  err+= add_equal(fptr);
  err+= add_space(fptr);
1986
  if (should_use_quotes)
1987 1988 1989
    err+= add_quoted_string(fptr, keystr);
  else
    err+= add_string(fptr, keystr);
1990 1991 1992 1993 1994 1995
  return err + add_space(fptr);
}

static int add_keyword_int(File fptr, const char *keyword, longlong num)
{
  int err= add_string(fptr, keyword);
unknown's avatar
unknown committed
1996

1997 1998 1999 2000 2001 2002 2003
  err+= add_space(fptr);
  err+= add_equal(fptr);
  err+= add_space(fptr);
  err+= add_int(fptr, num);
  return err + add_space(fptr);
}

unknown's avatar
unknown committed
2004
static int add_engine(File fptr, handlerton *engine_type)
2005
{
unknown's avatar
WL#2936  
unknown committed
2006
  const char *engine_str= ha_resolve_storage_engine_name(engine_type);
unknown's avatar
unknown committed
2007
  DBUG_PRINT("info", ("ENGINE: %s", engine_str));
2008 2009 2010 2011 2012 2013 2014
  int err= add_string(fptr, "ENGINE = ");
  return err + add_string(fptr, engine_str);
}

static int add_partition_options(File fptr, partition_element *p_elem)
{
  int err= 0;
unknown's avatar
unknown committed
2015

2016
  err+= add_space(fptr);
2017
  if (p_elem->tablespace_name)
unknown's avatar
unknown committed
2018
    err+= add_keyword_string(fptr,"TABLESPACE", FALSE,
2019
                             p_elem->tablespace_name);
2020 2021 2022 2023 2024 2025
  if (p_elem->nodegroup_id != UNDEF_NODEGROUP)
    err+= add_keyword_int(fptr,"NODEGROUP",(longlong)p_elem->nodegroup_id);
  if (p_elem->part_max_rows)
    err+= add_keyword_int(fptr,"MAX_ROWS",(longlong)p_elem->part_max_rows);
  if (p_elem->part_min_rows)
    err+= add_keyword_int(fptr,"MIN_ROWS",(longlong)p_elem->part_min_rows);
2026 2027 2028 2029 2030 2031 2032 2033 2034
  if (!(current_thd->variables.sql_mode & MODE_NO_DIR_IN_CREATE))
  {
    if (p_elem->data_file_name)
      err+= add_keyword_string(fptr, "DATA DIRECTORY", TRUE, 
                               p_elem->data_file_name);
    if (p_elem->index_file_name)
      err+= add_keyword_string(fptr, "INDEX DIRECTORY", TRUE, 
                               p_elem->index_file_name);
  }
2035
  if (p_elem->part_comment)
2036
    err+= add_keyword_string(fptr, "COMMENT", TRUE, p_elem->part_comment);
2037 2038 2039
  if (p_elem->connect_string.length)
    err+= add_keyword_string(fptr, "CONNECTION", TRUE,
                             p_elem->connect_string.str);
2040 2041 2042
  return err + add_engine(fptr,p_elem->engine_type);
}

2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059

/*
  Check partition fields for result type and if they need
  to check the character set.

  SYNOPSIS
    check_part_field()
    sql_type              Type provided by user
    field_name            Name of field, used for error handling
    result_type           Out value: Result type of field
    need_cs_check         Out value: Do we need character set check

  RETURN VALUES
    TRUE                  Error
    FALSE                 Ok
*/

2060 2061 2062
static int check_part_field(enum_field_types sql_type,
                            const char *field_name,
                            Item_result *result_type,
2063 2064
                            bool *need_cs_check)
{
2065 2066
  if (sql_type >= MYSQL_TYPE_TINY_BLOB &&
      sql_type <= MYSQL_TYPE_BLOB)
2067 2068 2069 2070
  {
    my_error(ER_BLOB_FIELD_IN_PART_FUNC_ERROR, MYF(0));
    return TRUE;
  }
2071
  switch (sql_type)
2072
  {
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
    case MYSQL_TYPE_TINY:
    case MYSQL_TYPE_SHORT:
    case MYSQL_TYPE_LONG:
    case MYSQL_TYPE_LONGLONG:
    case MYSQL_TYPE_INT24:
      *result_type= INT_RESULT;
      *need_cs_check= FALSE;
      return FALSE;
    case MYSQL_TYPE_NEWDATE:
    case MYSQL_TYPE_DATE:
    case MYSQL_TYPE_TIME:
    case MYSQL_TYPE_DATETIME:
      *result_type= STRING_RESULT;
2086
      *need_cs_check= TRUE;
2087
      return FALSE;
2088 2089 2090
    case MYSQL_TYPE_VARCHAR:
    case MYSQL_TYPE_STRING:
    case MYSQL_TYPE_VAR_STRING:
2091
      *result_type= STRING_RESULT;
2092 2093
      *need_cs_check= TRUE;
      return FALSE;
2094 2095
    case MYSQL_TYPE_NEWDECIMAL:
    case MYSQL_TYPE_DECIMAL:
2096 2097 2098 2099 2100 2101 2102 2103 2104
    case MYSQL_TYPE_TIMESTAMP:
    case MYSQL_TYPE_NULL:
    case MYSQL_TYPE_FLOAT:
    case MYSQL_TYPE_DOUBLE:
    case MYSQL_TYPE_BIT:
    case MYSQL_TYPE_ENUM:
    case MYSQL_TYPE_SET:
    case MYSQL_TYPE_GEOMETRY:
      goto error;
2105 2106 2107 2108 2109
    default:
      goto error;
  }
error:
  my_error(ER_FIELD_TYPE_NOT_ALLOWED_AS_PARTITION_FIELD, MYF(0),
2110
           field_name);
2111 2112 2113
  return TRUE;
}

2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127

/*
  Find the given field's Create_field object using name of field

  SYNOPSIS
    get_sql_field()
    field_name                   Field name
    alter_info                   Info from ALTER TABLE/CREATE TABLE

  RETURN VALUE
    sql_field                    Object filled in by parser about field
    NULL                         No field found
*/

2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
static Create_field* get_sql_field(char *field_name,
                                   Alter_info *alter_info)
{
  List_iterator<Create_field> it(alter_info->create_list);
  Create_field *sql_field;
  DBUG_ENTER("get_sql_field");

  while ((sql_field= it++))
  {
    if (!(my_strcasecmp(system_charset_info,
                        sql_field->field_name,
                        field_name)))
    {
      DBUG_RETURN(sql_field);
    }
  }
  DBUG_RETURN(NULL);
}

2147

2148
static int add_column_list_values(File fptr, partition_info *part_info,
2149 2150 2151
                                  part_elem_value *list_value,
                                  HA_CREATE_INFO *create_info,
                                  Alter_info *alter_info)
2152 2153 2154
{
  int err= 0;
  uint i;
2155
  List_iterator<char> it(part_info->part_field_list);
2156
  uint num_elements= part_info->part_field_list.elements;
2157 2158 2159 2160 2161
  bool use_parenthesis= (part_info->part_type == LIST_PARTITION &&
                         part_info->num_columns > 1U);

  if (use_parenthesis)
    err+= add_begin_parenthesis(fptr);
2162
  for (i= 0; i < num_elements; i++)
2163 2164
  {
    part_column_list_val *col_val= &list_value->col_val_array[i];
2165
    char *field_name= it++;
2166 2167 2168 2169 2170 2171
    if (col_val->max_value)
      err+= add_string(fptr, partition_keywords[PKW_MAXVALUE].str);
    else if (col_val->null_value)
      err+= add_string(fptr, "NULL");
    else
    {
2172
      char buffer[MAX_KEY_LENGTH];
2173 2174 2175 2176 2177 2178
      String str(buffer, sizeof(buffer), &my_charset_bin);
      Item *item_expr= col_val->item_expression;
      if (item_expr->null_value)
        err+= add_string(fptr, "NULL");
      else
      {
2179 2180
        String *res;
        CHARSET_INFO *field_cs;
2181 2182
        bool need_cs_check= FALSE;
        Item_result result_type= STRING_RESULT;
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199

        /*
          This function is called at a very early stage, even before
          we have prepared the sql_field objects. Thus we have to
          find the proper sql_field object and get the character set
          from that object.
        */
        if (create_info)
        {
          Create_field *sql_field;

          if (!(sql_field= get_sql_field(field_name,
                                         alter_info)))
          {
            my_error(ER_FIELD_NOT_FOUND_PART_ERROR, MYF(0));
            return 1;
          }
2200 2201 2202 2203
          if (check_part_field(sql_field->sql_type,
                               sql_field->field_name,
                               &result_type,
                               &need_cs_check))
2204 2205 2206 2207 2208 2209 2210
            return 1;
          if (need_cs_check)
            field_cs= get_sql_field_charset(sql_field, create_info);
          else
            field_cs= NULL;
        }
        else
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
        {
          Field *field= part_info->part_field_array[i];
          result_type= field->result_type();
          if (check_part_field(field->real_type(),
                               field->field_name,
                               &result_type,
                               &need_cs_check))
            return 1;
          DBUG_ASSERT(result_type == field->result_type());
          if (need_cs_check)
            field_cs= field->charset();
          else
            field_cs= NULL;
        }
2225 2226 2227 2228 2229
        if (result_type != item_expr->result_type())
        {
          my_error(ER_WRONG_TYPE_COLUMN_VALUE_ERROR, MYF(0));
          return 1;
        }
2230 2231 2232 2233 2234 2235 2236 2237 2238
        if (field_cs && field_cs != item_expr->collation.collation)
        {
          if (!(item_expr= convert_charset_partition_constant(item_expr,
                                                              field_cs)))
          {
            my_error(ER_PARTITION_FUNCTION_IS_NOT_ALLOWED, MYF(0));
            return 1;
          }
        }
2239
        {
2240
          String val_conv;
2241
          val_conv.set_charset(system_charset_info);
2242
          res= item_expr->val_str(&str);
2243 2244 2245
          if (get_cs_converted_part_value_from_string(current_thd,
                                                      item_expr, res,
                                                      &val_conv, field_cs,
2246
                                                   (bool)(alter_info != NULL)))
2247
            return 1;
2248
          err+= add_string_object(fptr, &val_conv);
2249
        }
2250 2251
      }
    }
2252
    if (i != (num_elements - 1))
2253 2254
      err+= add_string(fptr, comma_str);
  }
2255 2256
  if (use_parenthesis)
    err+= add_end_parenthesis(fptr);
2257 2258 2259 2260
  return err;
}

static int add_partition_values(File fptr, partition_info *part_info,
2261 2262 2263
                                partition_element *p_elem,
                                HA_CREATE_INFO *create_info,
                                Alter_info *alter_info)
2264 2265
{
  int err= 0;
unknown's avatar
unknown committed
2266

2267 2268
  if (part_info->part_type == RANGE_PARTITION)
  {
2269
    err+= add_string(fptr, " VALUES LESS THAN ");
2270
    if (part_info->column_list)
2271
    {
2272 2273
      List_iterator<part_elem_value> list_val_it(p_elem->list_val_list);
      part_elem_value *list_value= list_val_it++;
2274
      err+= add_begin_parenthesis(fptr);
2275 2276
      err+= add_column_list_values(fptr, part_info, list_value,
                                   create_info, alter_info);
2277 2278 2279
      err+= add_end_parenthesis(fptr);
    }
    else
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
    {
      if (!p_elem->max_value)
      {
        err+= add_begin_parenthesis(fptr);
        if (p_elem->signed_flag)
          err+= add_int(fptr, p_elem->range_value);
        else
          err+= add_uint(fptr, p_elem->range_value);
        err+= add_end_parenthesis(fptr);
      }
      else
        err+= add_string(fptr, partition_keywords[PKW_MAXVALUE].str);
    }
2293 2294 2295 2296
  }
  else if (part_info->part_type == LIST_PARTITION)
  {
    uint i;
2297
    List_iterator<part_elem_value> list_val_it(p_elem->list_val_list);
2298
    err+= add_string(fptr, " VALUES IN ");
2299
    uint num_items= p_elem->list_val_list.elements;
2300

2301
    err+= add_begin_parenthesis(fptr);
2302 2303 2304
    if (p_elem->has_null_value)
    {
      err+= add_string(fptr, "NULL");
2305
      if (num_items == 0)
2306 2307 2308 2309 2310 2311
      {
        err+= add_end_parenthesis(fptr);
        goto end;
      }
      err+= add_comma(fptr);
    }
2312 2313 2314
    i= 0;
    do
    {
2315 2316
      part_elem_value *list_value= list_val_it++;

2317
      if (part_info->column_list)
2318 2319
        err+= add_column_list_values(fptr, part_info, list_value,
                                     create_info, alter_info);
2320
      else
2321 2322 2323 2324 2325 2326
      {
        if (!list_value->unsigned_flag)
          err+= add_int(fptr, list_value->value);
        else
          err+= add_uint(fptr, list_value->value);
      }
2327
      if (i != (num_items-1))
2328
        err+= add_comma(fptr);
2329
    } while (++i < num_items);
2330 2331
    err+= add_end_parenthesis(fptr);
  }
2332
end:
2333
  return err;
2334 2335
}

2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387

/**
  Add 'KEY' word, with optional 'ALGORTIHM = N'.

  @param fptr                   File to write to.
  @param part_info              partition_info holding the used key_algorithm
  @param current_comment_start  NULL, or comment string encapsulating the
                                PARTITION BY clause.

  @return Operation status.
    @retval 0    Success
    @retval != 0 Failure
*/

static int add_key_with_algorithm(File fptr, partition_info *part_info,
                                  const char *current_comment_start)
{
  int err= 0;
  err+= add_part_key_word(fptr, partition_keywords[PKW_KEY].str);

  /*
    current_comment_start is given when called from SHOW CREATE TABLE,
    Then only add ALGORITHM = 1, not the default 2 or non-set 0!
    For .frm current_comment_start is NULL, then add ALGORITHM if != 0.
  */
  if (part_info->key_algorithm == partition_info::KEY_ALGORITHM_51 || // SHOW
      (!current_comment_start &&                                      // .frm
       (part_info->key_algorithm != partition_info::KEY_ALGORITHM_NONE)))
  {
    /* If we already are within a comment, end that comment first. */
    if (current_comment_start)
      err+= add_string(fptr, "*/ ");
    err+= add_string(fptr, "/*!50531 ");
    err+= add_part_key_word(fptr, partition_keywords[PKW_ALGORITHM].str);
    err+= add_equal(fptr);
    err+= add_space(fptr);
    err+= add_int(fptr, part_info->key_algorithm);
    err+= add_space(fptr);
    err+= add_string(fptr, "*/ ");
    if (current_comment_start)
    {
      /* Skip new line. */
      if (current_comment_start[0] == '\n')
        current_comment_start++;
      err+= add_string(fptr, current_comment_start);
      err+= add_space(fptr);
    }
  }
  return err;
}


2388 2389 2390 2391
/*
  Generate the partition syntax from the partition data structure.
  Useful for support of generating defaults, SHOW CREATE TABLES
  and easy partition management.
unknown's avatar
unknown committed
2392

2393 2394 2395 2396 2397 2398
  SYNOPSIS
    generate_partition_syntax()
    part_info                  The partitioning data structure
    buf_length                 A pointer to the returned buffer length
    use_sql_alloc              Allocate buffer from sql_alloc if true
                               otherwise use my_malloc
2399
    show_partition_options     Should we display partition options
2400 2401
    create_info                Info generated by parser
    alter_info                 Info generated by parser
unknown's avatar
unknown committed
2402

2403 2404 2405
  RETURN VALUES
    NULL error
    buf, buf_length            Buffer and its length
unknown's avatar
unknown committed
2406

2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
  DESCRIPTION
  Here we will generate the full syntax for the given command where all
  defaults have been expanded. By so doing the it is also possible to
  make lots of checks of correctness while at it.
  This could will also be reused for SHOW CREATE TABLES and also for all
  type ALTER TABLE commands focusing on changing the PARTITION structure
  in any fashion.

  The implementation writes the syntax to a temporary file (essentially
  an abstraction of a dynamic array) and if all writes goes well it
  allocates a buffer and writes the syntax into this one and returns it.

  As a security precaution the file is deleted before writing into it. This
  means that no other processes on the machine can open and read the file
  while this processing is ongoing.

  The code is optimised for minimal code size since it is not used in any
  common queries.
*/

char *generate_partition_syntax(partition_info *part_info,
                                uint *buf_length,
2429
                                bool use_sql_alloc,
2430 2431
                                bool show_partition_options,
                                HA_CREATE_INFO *create_info,
2432 2433
                                Alter_info *alter_info,
                                const char *current_comment_start)
2434
{
2435
  uint i,j, tot_num_parts, num_subparts;
2436 2437 2438 2439
  partition_element *part_elem;
  ulonglong buffer_length;
  char path[FN_REFLEN];
  int err= 0;
unknown's avatar
unknown committed
2440
  List_iterator<partition_element> part_it(part_info->partitions);
2441 2442
  File fptr;
  char *buf= NULL; //Return buffer
unknown's avatar
unknown committed
2443 2444
  DBUG_ENTER("generate_partition_syntax");

2445 2446 2447
  if (unlikely(((fptr= create_temp_file(path,mysql_tmpdir,"psy", 
                                        O_RDWR | O_BINARY | O_TRUNC |  
                                        O_TEMPORARY, MYF(MY_WME)))) < 0))
2448
    DBUG_RETURN(NULL);
unknown's avatar
unknown committed
2449 2450
#ifndef __WIN__
  unlink(path);
2451 2452 2453 2454 2455 2456
#endif
  err+= add_space(fptr);
  err+= add_partition_by(fptr);
  switch (part_info->part_type)
  {
    case RANGE_PARTITION:
2457
      err+= add_part_key_word(fptr, partition_keywords[PKW_RANGE].str);
2458 2459
      break;
    case LIST_PARTITION:
2460
      err+= add_part_key_word(fptr, partition_keywords[PKW_LIST].str);
2461 2462 2463
      break;
    case HASH_PARTITION:
      if (part_info->linear_hash_ind)
2464
        err+= add_string(fptr, partition_keywords[PKW_LINEAR].str);
2465
      if (part_info->list_of_part_fields)
2466
      {
2467 2468
        err+= add_key_with_algorithm(fptr, part_info,
                                     current_comment_start);
2469 2470
        err+= add_part_field_list(fptr, part_info->part_field_list);
      }
2471
      else
2472
        err+= add_part_key_word(fptr, partition_keywords[PKW_HASH].str);
2473 2474 2475 2476
      break;
    default:
      DBUG_ASSERT(0);
      /* We really shouldn't get here, no use in continuing from here */
2477
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
2478 2479 2480
      DBUG_RETURN(NULL);
  }
  if (part_info->part_expr)
2481 2482
  {
    err+= add_begin_parenthesis(fptr);
2483 2484
    err+= add_string_len(fptr, part_info->part_func_string,
                         part_info->part_func_len);
2485 2486 2487 2488 2489 2490 2491
    err+= add_end_parenthesis(fptr);
  }
  else if (part_info->column_list)
  {
    err+= add_string(fptr, partition_keywords[PKW_COLUMNS].str);
    err+= add_part_field_list(fptr, part_info->part_field_list);
  }
2492
  if ((!part_info->use_default_num_partitions) &&
unknown's avatar
unknown committed
2493 2494
       part_info->use_default_partitions)
  {
2495
    err+= add_string(fptr, "\n");
unknown's avatar
unknown committed
2496
    err+= add_string(fptr, "PARTITIONS ");
2497
    err+= add_int(fptr, part_info->num_parts);
unknown's avatar
unknown committed
2498
  }
2499
  if (part_info->is_sub_partitioned())
2500
  {
2501
    err+= add_string(fptr, "\n");
2502 2503
    err+= add_subpartition_by(fptr);
    /* Must be hash partitioning for subpartitioning */
2504 2505
    if (part_info->linear_hash_ind)
      err+= add_string(fptr, partition_keywords[PKW_LINEAR].str);
2506
    if (part_info->list_of_subpart_fields)
2507
    {
2508 2509 2510
      err+= add_key_with_algorithm(fptr, part_info,
                                   current_comment_start);
      err+= add_part_field_list(fptr, part_info->subpart_field_list);
2511
    }
2512
    else
2513
      err+= add_part_key_word(fptr, partition_keywords[PKW_HASH].str);
2514
    if (part_info->subpart_expr)
2515 2516
    {
      err+= add_begin_parenthesis(fptr);
2517 2518
      err+= add_string_len(fptr, part_info->subpart_func_string,
                           part_info->subpart_func_len);
2519 2520
      err+= add_end_parenthesis(fptr);
    }
2521
    if ((!part_info->use_default_num_subpartitions) && 
unknown's avatar
unknown committed
2522 2523
          part_info->use_default_subpartitions)
    {
2524
      err+= add_string(fptr, "\n");
unknown's avatar
unknown committed
2525
      err+= add_string(fptr, "SUBPARTITIONS ");
2526
      err+= add_int(fptr, part_info->num_subparts);
unknown's avatar
unknown committed
2527 2528
    }
  }
2529 2530
  tot_num_parts= part_info->partitions.elements;
  num_subparts= part_info->num_subparts;
unknown's avatar
unknown committed
2531

2532
  if (!part_info->use_default_partitions)
2533
  {
2534
    bool first= TRUE;
2535
    err+= add_string(fptr, "\n");
unknown's avatar
unknown committed
2536 2537 2538
    err+= add_begin_parenthesis(fptr);
    i= 0;
    do
2539
    {
2540 2541 2542
      part_elem= part_it++;
      if (part_elem->part_state != PART_TO_BE_DROPPED &&
          part_elem->part_state != PART_REORGED_DROPPED)
unknown's avatar
unknown committed
2543
      {
2544
        if (!first)
unknown's avatar
unknown committed
2545
        {
2546
          err+= add_comma(fptr);
2547
          err+= add_string(fptr, "\n");
2548
          err+= add_space(fptr);
unknown's avatar
unknown committed
2549
        }
2550
        first= FALSE;
unknown's avatar
unknown committed
2551
        err+= add_partition(fptr);
2552
        err+= add_name_string(fptr, part_elem->partition_name);
2553 2554
        err+= add_partition_values(fptr, part_info, part_elem,
                                   create_info, alter_info);
2555 2556
        if (!part_info->is_sub_partitioned() ||
            part_info->use_default_subpartitions)
2557
        {
2558 2559
          if (show_partition_options)
            err+= add_partition_options(fptr, part_elem);
2560 2561
        }
        else
unknown's avatar
unknown committed
2562
        {
2563
          err+= add_string(fptr, "\n");
unknown's avatar
unknown committed
2564 2565 2566 2567 2568 2569 2570 2571
          err+= add_space(fptr);
          err+= add_begin_parenthesis(fptr);
          List_iterator<partition_element> sub_it(part_elem->subpartitions);
          j= 0;
          do
          {
            part_elem= sub_it++;
            err+= add_subpartition(fptr);
2572
            err+= add_name_string(fptr, part_elem->partition_name);
2573 2574
            if (show_partition_options)
              err+= add_partition_options(fptr, part_elem);
2575
            if (j != (num_subparts-1))
unknown's avatar
unknown committed
2576 2577
            {
              err+= add_comma(fptr);
2578 2579
              err+= add_string(fptr, "\n");
              err+= add_space(fptr);
unknown's avatar
unknown committed
2580 2581 2582 2583
              err+= add_space(fptr);
            }
            else
              err+= add_end_parenthesis(fptr);
2584
          } while (++j < num_subparts);
unknown's avatar
unknown committed
2585 2586
        }
      }
2587
      if (i == (tot_num_parts-1))
unknown's avatar
unknown committed
2588
        err+= add_end_parenthesis(fptr);
2589
    } while (++i < tot_num_parts);
2590
  }
2591 2592
  if (err)
    goto close_file;
Marc Alff's avatar
Marc Alff committed
2593
  buffer_length= mysql_file_seek(fptr, 0L, MY_SEEK_END, MYF(0));
2594 2595
  if (unlikely(buffer_length == MY_FILEPOS_ERROR))
    goto close_file;
Marc Alff's avatar
Marc Alff committed
2596 2597
  if (unlikely(mysql_file_seek(fptr, 0L, MY_SEEK_SET, MYF(0))
               == MY_FILEPOS_ERROR))
2598 2599 2600
    goto close_file;
  *buf_length= (uint)buffer_length;
  if (use_sql_alloc)
2601
    buf= (char*) sql_alloc(*buf_length+1);
2602
  else
2603
    buf= (char*) my_malloc(*buf_length+1, MYF(MY_WME));
2604 2605 2606
  if (!buf)
    goto close_file;

Marc Alff's avatar
Marc Alff committed
2607
  if (unlikely(mysql_file_read(fptr, (uchar*)buf, *buf_length, MYF(MY_FNABP))))
2608 2609
  {
    if (!use_sql_alloc)
2610
      my_free(buf);
2611 2612 2613 2614 2615 2616 2617
    else
      buf= NULL;
  }
  else
    buf[*buf_length]= 0;

close_file:
Marc Alff's avatar
Marc Alff committed
2618
  mysql_file_close(fptr, MYF(0));
2619 2620 2621 2622 2623 2624 2625
  DBUG_RETURN(buf);
}


/*
  Check if partition key fields are modified and if it can be handled by the
  underlying storage engine.
unknown's avatar
unknown committed
2626

2627 2628 2629
  SYNOPSIS
    partition_key_modified
    table                TABLE object for which partition fields are set-up
2630
    fields               Bitmap representing fields to be modified
unknown's avatar
unknown committed
2631

2632 2633 2634 2635 2636
  RETURN VALUES
    TRUE                 Need special handling of UPDATE
    FALSE                Normal UPDATE handling is ok
*/

2637
bool partition_key_modified(TABLE *table, const MY_BITMAP *fields)
2638
{
2639
  Field **fld;
unknown's avatar
unknown committed
2640
  partition_info *part_info= table->part_info;
2641
  DBUG_ENTER("partition_key_modified");
unknown's avatar
unknown committed
2642

2643 2644
  if (!part_info)
    DBUG_RETURN(FALSE);
unknown's avatar
WL#2936  
unknown committed
2645 2646
  if (table->s->db_type()->partition_flags &&
      (table->s->db_type()->partition_flags() & HA_CAN_UPDATE_PARTITION_KEY))
2647
    DBUG_RETURN(FALSE);
2648 2649
  for (fld= part_info->full_part_field_array; *fld; fld++)
    if (bitmap_is_set(fields, (*fld)->field_index))
2650 2651 2652 2653 2654
      DBUG_RETURN(TRUE);
  DBUG_RETURN(FALSE);
}


2655 2656 2657 2658 2659 2660
/*
  A function to handle correct handling of NULL values in partition
  functions.
  SYNOPSIS
    part_val_int()
    item_expr                 The item expression to evaluate
2661 2662
    out:result                The value of the partition function,
                                LONGLONG_MIN if any null value in function
2663
  RETURN VALUES
2664 2665
    TRUE      Error in val_int()
    FALSE     ok
2666 2667
*/

2668
static inline int part_val_int(Item *item_expr, longlong *result)
2669
{
2670
  *result= item_expr->val_int();
2671
  if (item_expr->null_value)
2672 2673 2674 2675 2676 2677 2678
  {
    if (current_thd->is_error())
      return TRUE;
    else
      *result= LONGLONG_MIN;
  }
  return FALSE;
2679 2680 2681
}


2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701
/*
  The next set of functions are used to calculate the partition identity.
  A handler sets up a variable that corresponds to one of these functions
  to be able to quickly call it whenever the partition id needs to calculated
  based on the record in table->record[0] (or set up to fake that).
  There are 4 functions for hash partitioning and 2 for RANGE/LIST partitions.
  In addition there are 4 variants for RANGE subpartitioning and 4 variants
  for LIST subpartitioning thus in total there are 14 variants of this
  function.

  We have a set of support functions for these 14 variants. There are 4
  variants of hash functions and there is a function for each. The KEY
  partitioning uses the function calculate_key_value to calculate the hash
  value based on an array of fields. The linear hash variants uses the
  method get_part_id_from_linear_hash to get the partition id using the
  hash value and some parameters calculated from the number of partitions.
*/

/*
  Calculate hash value for KEY partitioning using an array of fields.
unknown's avatar
unknown committed
2702

2703 2704 2705
  SYNOPSIS
    calculate_key_value()
    field_array             An array of the fields in KEY partitioning
unknown's avatar
unknown committed
2706

2707 2708
  RETURN VALUE
    hash_value calculated
unknown's avatar
unknown committed
2709

2710 2711 2712 2713 2714 2715 2716
  DESCRIPTION
    Uses the hash function on the character set of the field. Integer and
    floating point fields use the binary character set by default.
*/

static uint32 calculate_key_value(Field **field_array)
{
2717
  ulong nr1= 1;
2718
  ulong nr2= 4;
2719 2720 2721
  bool use_51_hash;
  use_51_hash= test((*field_array)->table->part_info->key_algorithm ==
                    partition_info::KEY_ALGORITHM_51);
unknown's avatar
unknown committed
2722

2723 2724 2725
  do
  {
    Field *field= *field_array;
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794
    if (use_51_hash)
    {
      switch (field->real_type()) {
      case MYSQL_TYPE_TINY:
      case MYSQL_TYPE_SHORT:
      case MYSQL_TYPE_LONG:
      case MYSQL_TYPE_FLOAT:
      case MYSQL_TYPE_DOUBLE:
      case MYSQL_TYPE_NEWDECIMAL:
      case MYSQL_TYPE_TIMESTAMP:
      case MYSQL_TYPE_LONGLONG:
      case MYSQL_TYPE_INT24:
      case MYSQL_TYPE_TIME:
      case MYSQL_TYPE_DATETIME:
      case MYSQL_TYPE_YEAR:
      case MYSQL_TYPE_NEWDATE:
        {
          if (field->is_null())
          {
            nr1^= (nr1 << 1) | 1;
            continue;
          }
          /* Force this to my_hash_sort_bin, which was used in 5.1! */
          uint len= field->pack_length();
          my_charset_bin.coll->hash_sort(&my_charset_bin, field->ptr, len,
                                         &nr1, &nr2);
          /* Done with this field, continue with next one. */
          continue;
        }
      case MYSQL_TYPE_STRING:
      case MYSQL_TYPE_VARCHAR:
      case MYSQL_TYPE_BIT:
        /* Not affected, same in 5.1 and 5.5 */
        break;
      /*
        ENUM/SET uses my_hash_sort_simple in 5.1 (i.e. my_charset_latin1)
        and my_hash_sort_bin in 5.5!
      */
      case MYSQL_TYPE_ENUM:
      case MYSQL_TYPE_SET:
        {
          if (field->is_null())
          {
            nr1^= (nr1 << 1) | 1;
            continue;
          }
          /* Force this to my_hash_sort_bin, which was used in 5.1! */
          uint len= field->pack_length();
          my_charset_latin1.coll->hash_sort(&my_charset_latin1, field->ptr,
                                            len, &nr1, &nr2);
          continue;
        }
      /* These types should not be allowed for partitioning! */
      case MYSQL_TYPE_NULL:
      case MYSQL_TYPE_DECIMAL:
      case MYSQL_TYPE_DATE:
      case MYSQL_TYPE_TINY_BLOB:
      case MYSQL_TYPE_MEDIUM_BLOB:
      case MYSQL_TYPE_LONG_BLOB:
      case MYSQL_TYPE_BLOB:
      case MYSQL_TYPE_VAR_STRING:
      case MYSQL_TYPE_GEOMETRY:
        /* fall through. */
      default:
        DBUG_ASSERT(0);                    // New type?
        /* Fall through for default hashing (5.5). */
      }
      /* fall through, use collation based hashing. */
    }
2795
    field->hash(&nr1, &nr2);
2796
  } while (*(++field_array));
2797
  return (uint32) nr1;
2798 2799 2800 2801 2802 2803
}


/*
  A simple support function to calculate part_id given local part and
  sub part.
unknown's avatar
unknown committed
2804

2805 2806 2807 2808
  SYNOPSIS
    get_part_id_for_sub()
    loc_part_id             Local partition id
    sub_part_id             Subpartition id
2809
    num_subparts            Number of subparts
2810 2811 2812 2813
*/

inline
static uint32 get_part_id_for_sub(uint32 loc_part_id, uint32 sub_part_id,
2814
                                  uint num_subparts)
2815
{
2816
  return (uint32)((loc_part_id * num_subparts) + sub_part_id);
2817 2818 2819 2820 2821
}


/*
  Calculate part_id for (SUB)PARTITION BY HASH
unknown's avatar
unknown committed
2822

2823 2824
  SYNOPSIS
    get_part_id_hash()
2825
    num_parts                Number of hash partitions
2826
    part_expr                Item tree of hash function
2827 2828
    out:part_id              The returned partition id
    out:func_value           Value of hash function
unknown's avatar
unknown committed
2829

2830
  RETURN VALUE
2831 2832
    != 0                          Error code
    FALSE                         Success
2833 2834
*/

2835
static int get_part_id_hash(uint num_parts,
2836 2837 2838
                            Item *part_expr,
                            uint32 *part_id,
                            longlong *func_value)
2839
{
2840
  longlong int_hash_id;
2841
  DBUG_ENTER("get_part_id_hash");
2842

2843 2844 2845
  if (part_val_int(part_expr, func_value))
    DBUG_RETURN(HA_ERR_NO_PARTITION_FOUND);

2846
  int_hash_id= *func_value % num_parts;
2847

2848 2849
  *part_id= int_hash_id < 0 ? (uint32) -int_hash_id : (uint32) int_hash_id;
  DBUG_RETURN(FALSE);
2850 2851 2852 2853 2854
}


/*
  Calculate part_id for (SUB)PARTITION BY LINEAR HASH
unknown's avatar
unknown committed
2855

2856 2857 2858 2859
  SYNOPSIS
    get_part_id_linear_hash()
    part_info           A reference to the partition_info struct where all the
                        desired information is given
2860
    num_parts           Number of hash partitions
2861
    part_expr           Item tree of hash function
2862
    out:part_id         The returned partition id
2863
    out:func_value      Value of hash function
unknown's avatar
unknown committed
2864

2865
  RETURN VALUE
2866 2867
    != 0     Error code
    0        OK
2868 2869
*/

2870
static int get_part_id_linear_hash(partition_info *part_info,
2871
                                   uint num_parts,
2872 2873 2874
                                   Item *part_expr,
                                   uint32 *part_id,
                                   longlong *func_value)
2875 2876
{
  DBUG_ENTER("get_part_id_linear_hash");
unknown's avatar
unknown committed
2877

2878 2879 2880 2881 2882
  if (part_val_int(part_expr, func_value))
    DBUG_RETURN(HA_ERR_NO_PARTITION_FOUND);

  *part_id= get_part_id_from_linear_hash(*func_value,
                                         part_info->linear_hash_mask,
2883
                                         num_parts);
2884
  DBUG_RETURN(FALSE);
2885 2886 2887 2888 2889
}


/*
  Calculate part_id for (SUB)PARTITION BY KEY
unknown's avatar
unknown committed
2890

2891 2892 2893
  SYNOPSIS
    get_part_id_key()
    field_array         Array of fields for PARTTION KEY
2894
    num_parts           Number of KEY partitions
unknown's avatar
unknown committed
2895

2896 2897 2898 2899 2900 2901
  RETURN VALUE
    Calculated partition id
*/

inline
static uint32 get_part_id_key(Field **field_array,
2902
                              uint num_parts,
2903
                              longlong *func_value)
2904 2905
{
  DBUG_ENTER("get_part_id_key");
2906
  *func_value= calculate_key_value(field_array);
2907
  DBUG_RETURN((uint32) (*func_value % num_parts));
2908 2909 2910 2911 2912
}


/*
  Calculate part_id for (SUB)PARTITION BY LINEAR KEY
unknown's avatar
unknown committed
2913

2914 2915 2916 2917 2918
  SYNOPSIS
    get_part_id_linear_key()
    part_info           A reference to the partition_info struct where all the
                        desired information is given
    field_array         Array of fields for PARTTION KEY
2919
    num_parts            Number of KEY partitions
unknown's avatar
unknown committed
2920

2921 2922 2923 2924 2925 2926 2927
  RETURN VALUE
    Calculated partition id
*/

inline
static uint32 get_part_id_linear_key(partition_info *part_info,
                                     Field **field_array,
2928
                                     uint num_parts,
2929
                                     longlong *func_value)
2930
{
2931
  DBUG_ENTER("get_part_id_linear_key");
unknown's avatar
unknown committed
2932

2933 2934
  *func_value= calculate_key_value(field_array);
  DBUG_RETURN(get_part_id_from_linear_hash(*func_value,
2935
                                           part_info->linear_hash_mask,
2936
                                           num_parts));
2937 2938
}

2939 2940
/*
  Copy to field buffers and set up field pointers
2941

2942 2943 2944
  SYNOPSIS
    copy_to_part_field_buffers()
    ptr                          Array of fields to copy
2945 2946 2947
    field_bufs                   Array of field buffers to copy to
    restore_ptr                  Array of pointers to restore to

2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
  RETURN VALUES
    NONE
  DESCRIPTION
    This routine is used to take the data from field pointer, convert
    it to a standard format and store this format in a field buffer
    allocated for this purpose. Next the field pointers are moved to
    point to the field buffers. There is a separate to restore the
    field pointers after this call.
*/

static void copy_to_part_field_buffers(Field **ptr,
2959 2960
                                       uchar **field_bufs,
                                       uchar **restore_ptr)
2961 2962 2963 2964 2965 2966
{
  Field *field;
  while ((field= *(ptr++)))
  {
    *restore_ptr= field->ptr;
    restore_ptr++;
2967
    if (!field->maybe_null() || !field->is_null())
2968 2969
    {
      CHARSET_INFO *cs= ((Field_str*)field)->charset();
2970 2971
      uint max_len= field->pack_length();
      uint data_len= field->data_length();
2972
      uchar *field_buf= *field_bufs;
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982
      /*
         We only use the field buffer for VARCHAR and CHAR strings
         which isn't of a binary collation. We also only use the
         field buffer for fields which are not currently NULL.
         The field buffer will store a normalised string. We use
         the strnxfrm method to normalise the string.
       */
      if (field->type() == MYSQL_TYPE_VARCHAR)
      {
        uint len_bytes= ((Field_varstring*)field)->length_bytes;
2983 2984
        my_strnxfrm(cs, field_buf + len_bytes, max_len,
                    field->ptr + len_bytes, data_len);
2985
        if (len_bytes == 1)
2986
          *field_buf= (uchar) data_len;
2987
        else
2988
          int2store(field_buf, data_len);
2989 2990 2991
      }
      else
      {
2992 2993
        my_strnxfrm(cs, field_buf, max_len,
                    field->ptr, max_len);
2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006
      }
      field->ptr= field_buf;
    }
    field_bufs++;
  }
  return;
}

/*
  Restore field pointers
  SYNOPSIS
    restore_part_field_pointers()
    ptr                            Array of fields to restore
3007 3008
    restore_ptr                    Array of field pointers to restore to

3009 3010 3011
  RETURN VALUES
*/

3012
static void restore_part_field_pointers(Field **ptr, uchar **restore_ptr)
3013 3014 3015 3016 3017 3018 3019 3020 3021
{
  Field *field;
  while ((field= *(ptr++)))
  {
    field->ptr= *restore_ptr;
    restore_ptr++;
  }
  return;
}
unknown's avatar
unknown committed
3022

3023 3024 3025 3026 3027 3028 3029 3030 3031 3032
/*
  This function is used to calculate the partition id where all partition
  fields have been prepared to point to a record where the partition field
  values are bound.

  SYNOPSIS
    get_partition_id()
    part_info           A reference to the partition_info struct where all the
                        desired information is given
    out:part_id         The partition id is returned through this pointer
Mikael Ronstrom's avatar
Mikael Ronstrom committed
3033
    out:func_value      Value of partition function (longlong)
3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060

  RETURN VALUE
    part_id                     Partition id of partition that would contain
                                row with given values of PF-fields
    HA_ERR_NO_PARTITION_FOUND   The fields of the partition function didn't
                                fit into any partition and thus the values of 
                                the PF-fields are not allowed.

  DESCRIPTION
    A routine used from write_row, update_row and delete_row from any
    handler supporting partitioning. It is also a support routine for
    get_partition_set used to find the set of partitions needed to scan
    for a certain index scan or full table scan.
    
    It is actually 9 different variants of this function which are called
    through a function pointer.

    get_partition_id_list
    get_partition_id_list_col
    get_partition_id_range
    get_partition_id_range_col
    get_partition_id_hash_nosub
    get_partition_id_key_nosub
    get_partition_id_linear_hash_nosub
    get_partition_id_linear_key_nosub
    get_partition_id_with_sub
*/
3061 3062 3063 3064 3065

/*
  This function is used to calculate the main partition to use in the case of
  subpartitioning and we don't know enough to get the partition identity in
  total.
unknown's avatar
unknown committed
3066

3067 3068 3069 3070
  SYNOPSIS
    get_part_partition_id()
    part_info           A reference to the partition_info struct where all the
                        desired information is given
unknown's avatar
unknown committed
3071
    out:part_id         The partition id is returned through this pointer
3072
    out:func_value      The value calculated by partition function
unknown's avatar
unknown committed
3073

3074
  RETURN VALUE
3075 3076 3077
    HA_ERR_NO_PARTITION_FOUND   The fields of the partition function didn't
                                fit into any partition and thus the values of 
                                the PF-fields are not allowed.
3078
    0                           OK
unknown's avatar
unknown committed
3079

3080 3081
  DESCRIPTION
    
3082
    It is actually 8 different variants of this function which are called
3083 3084 3085
    through a function pointer.

    get_partition_id_list
3086
    get_partition_id_list_col
3087
    get_partition_id_range
3088
    get_partition_id_range_col
3089 3090 3091 3092 3093 3094
    get_partition_id_hash_nosub
    get_partition_id_key_nosub
    get_partition_id_linear_hash_nosub
    get_partition_id_linear_key_nosub
*/

3095 3096 3097 3098 3099
static int get_part_id_charset_func_part(partition_info *part_info,
                                         uint32 *part_id,
                                         longlong *func_value)
{
  int res;
3100
  DBUG_ENTER("get_part_id_charset_func_part");
3101 3102

  copy_to_part_field_buffers(part_info->part_charset_field_array,
3103 3104 3105 3106
                             part_info->part_field_buffers,
                             part_info->restore_part_field_ptrs);
  res= part_info->get_part_partition_id_charset(part_info,
                                                part_id, func_value);
3107
  restore_part_field_pointers(part_info->part_charset_field_array,
3108
                              part_info->restore_part_field_ptrs);
3109
  DBUG_RETURN(res);
3110 3111
}

3112

3113 3114
static int get_part_id_charset_func_subpart(partition_info *part_info,
                                            uint32 *part_id)
3115 3116
{
  int res;
3117 3118
  DBUG_ENTER("get_part_id_charset_func_subpart");

3119
  copy_to_part_field_buffers(part_info->subpart_charset_field_array,
3120 3121
                             part_info->subpart_field_buffers,
                             part_info->restore_subpart_field_ptrs);
3122
  res= part_info->get_subpartition_id_charset(part_info, part_id);
3123
  restore_part_field_pointers(part_info->subpart_charset_field_array,
3124
                              part_info->restore_subpart_field_ptrs);
3125 3126 3127 3128 3129 3130 3131 3132
  DBUG_RETURN(res);
}

int get_partition_id_list_col(partition_info *part_info,
                              uint32 *part_id,
                              longlong *func_value)
{
  part_column_list_val *list_col_array= part_info->list_col_array;
3133
  uint num_columns= part_info->part_field_list.elements;
3134 3135
  int list_index, cmp;
  int min_list_index= 0;
3136
  int max_list_index= part_info->num_list_values - 1;
3137 3138 3139 3140 3141
  DBUG_ENTER("get_partition_id_list_col");

  while (max_list_index >= min_list_index)
  {
    list_index= (max_list_index + min_list_index) >> 1;
3142 3143
    cmp= cmp_rec_and_tuple(list_col_array + list_index*num_columns,
                          num_columns);
3144 3145 3146 3147 3148 3149 3150 3151 3152 3153
    if (cmp > 0)
      min_list_index= list_index + 1;
    else if (cmp < 0)
    {
      if (!list_index)
        goto notfound;
      max_list_index= list_index - 1;
    }
    else
    {
3154
      *part_id= (uint32)list_col_array[list_index*num_columns].partition_id;
3155 3156 3157 3158 3159 3160
      DBUG_RETURN(0);
    }
  }
notfound:
  *part_id= 0;
  DBUG_RETURN(HA_ERR_NO_PARTITION_FOUND);
3161
}
3162 3163


unknown's avatar
unknown committed
3164
int get_partition_id_list(partition_info *part_info,
3165 3166
                          uint32 *part_id,
                          longlong *func_value)
3167 3168
{
  LIST_PART_ENTRY *list_array= part_info->list_array;
unknown's avatar
unknown committed
3169 3170
  int list_index;
  int min_list_index= 0;
3171
  int max_list_index= part_info->num_list_values - 1;
3172 3173
  longlong part_func_value;
  int error= part_val_int(part_info->part_expr, &part_func_value);
unknown's avatar
unknown committed
3174
  longlong list_value;
3175
  bool unsigned_flag= part_info->part_expr->unsigned_flag;
unknown's avatar
unknown committed
3176 3177
  DBUG_ENTER("get_partition_id_list");

3178 3179 3180
  if (error)
    goto notfound;

3181 3182 3183 3184 3185 3186 3187 3188 3189
  if (part_info->part_expr->null_value)
  {
    if (part_info->has_null_value)
    {
      *part_id= part_info->has_null_part_id;
      DBUG_RETURN(0);
    }
    goto notfound;
  }
3190
  *func_value= part_func_value;
unknown's avatar
unknown committed
3191 3192
  if (unsigned_flag)
    part_func_value-= 0x8000000000000000ULL;
3193 3194 3195 3196 3197 3198 3199
  while (max_list_index >= min_list_index)
  {
    list_index= (max_list_index + min_list_index) >> 1;
    list_value= list_array[list_index].list_value;
    if (list_value < part_func_value)
      min_list_index= list_index + 1;
    else if (list_value > part_func_value)
unknown's avatar
unknown committed
3200 3201 3202
    {
      if (!list_index)
        goto notfound;
3203
      max_list_index= list_index - 1;
unknown's avatar
unknown committed
3204 3205 3206
    }
    else
    {
3207
      *part_id= (uint32)list_array[list_index].partition_id;
unknown's avatar
unknown committed
3208
      DBUG_RETURN(0);
3209 3210
    }
  }
unknown's avatar
unknown committed
3211
notfound:
3212
  *part_id= 0;
unknown's avatar
unknown committed
3213
  DBUG_RETURN(HA_ERR_NO_PARTITION_FOUND);
3214 3215 3216
}


3217 3218 3219 3220 3221 3222
uint32 get_partition_id_cols_list_for_endpoint(partition_info *part_info,
                                               bool left_endpoint,
                                               bool include_endpoint,
                                               uint32 nparts)
{
  part_column_list_val *list_col_array= part_info->list_col_array;
3223
  uint num_columns= part_info->part_field_list.elements;
3224
  uint list_index;
3225
  uint min_list_index= 0;
3226 3227
  int cmp;
  /* Notice that max_list_index = last_index + 1 here! */
3228
  uint max_list_index= part_info->num_list_values;
3229 3230
  DBUG_ENTER("get_partition_id_cols_list_for_endpoint");

3231
  /* Find the matching partition (including taking endpoint into account). */
3232 3233
  do
  {
3234
    /* Midpoint, adjusted down, so it can never be >= max_list_index. */
3235
    list_index= (max_list_index + min_list_index) >> 1;
3236 3237 3238 3239
    cmp= cmp_rec_and_tuple_prune(list_col_array + list_index*num_columns,
                                 nparts, left_endpoint, include_endpoint);
    if (cmp > 0)
    {
3240
      min_list_index= list_index + 1;
3241
    }
3242
    else
3243
    {
3244
      max_list_index= list_index;
3245 3246 3247
      if (cmp == 0)
        break;
    }
3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263
  } while (max_list_index > min_list_index);
  list_index= max_list_index;

  /* Given value must be LESS THAN or EQUAL to the found partition. */
  DBUG_ASSERT(list_index == part_info->num_list_values ||
              (0 >= cmp_rec_and_tuple_prune(list_col_array +
                                              list_index*num_columns,
                                            nparts, left_endpoint,
                                            include_endpoint)));
  /* Given value must be GREATER THAN the previous partition. */
  DBUG_ASSERT(list_index == 0 ||
              (0 < cmp_rec_and_tuple_prune(list_col_array +
                                            (list_index - 1)*num_columns,
                                           nparts, left_endpoint,
                                           include_endpoint)));

3264 3265 3266 3267
  /* Include the right endpoint if not already passed end of array. */
  if (!left_endpoint && include_endpoint && cmp == 0 &&
      list_index < part_info->num_list_values)
    list_index++;
3268

3269 3270 3271 3272
  DBUG_RETURN(list_index);
}


3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305
/**
  Find the sub-array part_info->list_array that corresponds to given interval.

  @param part_info         Partitioning info (partitioning type must be LIST)
  @param left_endpoint     TRUE  - the interval is [a; +inf) or (a; +inf)
                           FALSE - the interval is (-inf; a] or (-inf; a)
  @param include_endpoint  TRUE iff the interval includes the endpoint

  This function finds the sub-array of part_info->list_array where values of
  list_array[idx].list_value are contained within the specifed interval.
  list_array is ordered by list_value, so
  1. For [a; +inf) or (a; +inf)-type intervals (left_endpoint==TRUE), the
     sought sub-array starts at some index idx and continues till array end.
     The function returns first number idx, such that
     list_array[idx].list_value is contained within the passed interval.

  2. For (-inf; a] or (-inf; a)-type intervals (left_endpoint==FALSE), the
     sought sub-array starts at array start and continues till some last
     index idx.
     The function returns first number idx, such that
     list_array[idx].list_value is NOT contained within the passed interval.
     If all array elements are contained, part_info->num_list_values is
     returned.

  @note The caller will call this function and then will run along the
  sub-array of list_array to collect partition ids. If the number of list
  values is significantly higher then number of partitions, this could be slow
  and we could invent some other approach. The "run over list array" part is
  already wrapped in a get_next()-like function.

  @return The index of corresponding sub-array of part_info->list_array.
*/

3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320
uint32 get_list_array_idx_for_endpoint_charset(partition_info *part_info,
                                               bool left_endpoint,
                                               bool include_endpoint)
{
  uint32 res;
  copy_to_part_field_buffers(part_info->part_field_array,
                             part_info->part_field_buffers,
                             part_info->restore_part_field_ptrs);
  res= get_list_array_idx_for_endpoint(part_info, left_endpoint,
                                       include_endpoint);
  restore_part_field_pointers(part_info->part_field_array,
                              part_info->restore_part_field_ptrs);
  return res;
}

unknown's avatar
unknown committed
3321 3322 3323 3324 3325 3326
uint32 get_list_array_idx_for_endpoint(partition_info *part_info,
                                       bool left_endpoint,
                                       bool include_endpoint)
{
  LIST_PART_ENTRY *list_array= part_info->list_array;
  uint list_index;
3327
  uint min_list_index= 0, max_list_index= part_info->num_list_values - 1;
3328
  longlong list_value;
3329
  /* Get the partitioning function value for the endpoint */
3330 3331
  longlong part_func_value= 
    part_info->part_expr->val_int_endpoint(left_endpoint, &include_endpoint);
3332 3333 3334
  bool unsigned_flag= part_info->part_expr->unsigned_flag;
  DBUG_ENTER("get_list_array_idx_for_endpoint");

3335 3336
  if (part_info->part_expr->null_value)
  {
3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352
    /*
      Special handling for MONOTONIC functions that can return NULL for
      values that are comparable. I.e.
      '2000-00-00' can be compared to '2000-01-01' but TO_DAYS('2000-00-00')
      returns NULL which cannot be compared used <, >, <=, >= etc.

      Otherwise, just return the the first index (lowest value).
    */
    enum_monotonicity_info monotonic;
    monotonic= part_info->part_expr->get_monotonicity_info();
    if (monotonic != MONOTONIC_INCREASING_NOT_NULL && 
        monotonic != MONOTONIC_STRICT_INCREASING_NOT_NULL)
    {
      /* F(col) can not return NULL, return index with lowest value */
      DBUG_RETURN(0);
    }
3353
  }
3354

unknown's avatar
unknown committed
3355 3356
  if (unsigned_flag)
    part_func_value-= 0x8000000000000000ULL;
3357
  DBUG_ASSERT(part_info->num_list_values);
3358
  do
unknown's avatar
unknown committed
3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373
  {
    list_index= (max_list_index + min_list_index) >> 1;
    list_value= list_array[list_index].list_value;
    if (list_value < part_func_value)
      min_list_index= list_index + 1;
    else if (list_value > part_func_value)
    {
      if (!list_index)
        goto notfound;
      max_list_index= list_index - 1;
    }
    else 
    {
      DBUG_RETURN(list_index + test(left_endpoint ^ include_endpoint));
    }
3374
  } while (max_list_index >= min_list_index);
unknown's avatar
unknown committed
3375 3376 3377 3378 3379 3380
notfound:
  if (list_value < part_func_value)
    list_index++;
  DBUG_RETURN(list_index);
}

3381

3382 3383 3384 3385 3386
int get_partition_id_range_col(partition_info *part_info,
                               uint32 *part_id,
                               longlong *func_value)
{
  part_column_list_val *range_col_array= part_info->range_col_array;
3387 3388
  uint num_columns= part_info->part_field_list.elements;
  uint max_partition= part_info->num_parts - 1;
3389 3390 3391 3392 3393 3394 3395 3396
  uint min_part_id= 0;
  uint max_part_id= max_partition;
  uint loc_part_id;
  DBUG_ENTER("get_partition_id_range_col");

  while (max_part_id > min_part_id)
  {
    loc_part_id= (max_part_id + min_part_id + 1) >> 1;
3397
    if (cmp_rec_and_tuple(range_col_array + loc_part_id*num_columns,
3398
                          num_columns) >= 0)
3399 3400 3401 3402 3403 3404
      min_part_id= loc_part_id + 1;
    else
      max_part_id= loc_part_id - 1;
  }
  loc_part_id= max_part_id;
  if (loc_part_id != max_partition)
3405 3406
    if (cmp_rec_and_tuple(range_col_array + loc_part_id*num_columns,
                          num_columns) >= 0)
3407 3408 3409
      loc_part_id++;
  *part_id= (uint32)loc_part_id;
  if (loc_part_id == max_partition &&
3410 3411
      (cmp_rec_and_tuple(range_col_array + loc_part_id*num_columns,
                         num_columns) >= 0))
3412 3413 3414 3415 3416 3417 3418
    DBUG_RETURN(HA_ERR_NO_PARTITION_FOUND);

  DBUG_PRINT("exit",("partition: %d", *part_id));
  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
3419
int get_partition_id_range(partition_info *part_info,
3420 3421
                           uint32 *part_id,
                           longlong *func_value)
3422 3423
{
  longlong *range_array= part_info->range_int_array;
3424
  uint max_partition= part_info->num_parts - 1;
unknown's avatar
unknown committed
3425 3426 3427
  uint min_part_id= 0;
  uint max_part_id= max_partition;
  uint loc_part_id;
3428 3429
  longlong part_func_value;
  int error= part_val_int(part_info->part_expr, &part_func_value);
3430
  bool unsigned_flag= part_info->part_expr->unsigned_flag;
unknown's avatar
unknown committed
3431
  DBUG_ENTER("get_partition_id_range");
unknown's avatar
unknown committed
3432

3433 3434 3435
  if (error)
    DBUG_RETURN(HA_ERR_NO_PARTITION_FOUND);

3436 3437 3438 3439 3440
  if (part_info->part_expr->null_value)
  {
    *part_id= 0;
    DBUG_RETURN(0);
  }
unknown's avatar
unknown committed
3441
  *func_value= part_func_value;
unknown's avatar
unknown committed
3442 3443
  if (unsigned_flag)
    part_func_value-= 0x8000000000000000ULL;
3444
  /* Search for the partition containing part_func_value */
3445 3446
  while (max_part_id > min_part_id)
  {
3447
    loc_part_id= (max_part_id + min_part_id) / 2;
unknown's avatar
unknown committed
3448
    if (range_array[loc_part_id] <= part_func_value)
3449 3450
      min_part_id= loc_part_id + 1;
    else
3451
      max_part_id= loc_part_id;
3452 3453 3454
  }
  loc_part_id= max_part_id;
  *part_id= (uint32)loc_part_id;
3455
  if (loc_part_id == max_partition &&
3456 3457
      part_func_value >= range_array[loc_part_id] &&
      !part_info->defined_max_value)
3458 3459 3460
    DBUG_RETURN(HA_ERR_NO_PARTITION_FOUND);

  DBUG_PRINT("exit",("partition: %d", *part_id));
unknown's avatar
unknown committed
3461
  DBUG_RETURN(0);
3462 3463
}

unknown's avatar
unknown committed
3464 3465

/*
3466 3467
  Find the sub-array of part_info->range_int_array that covers given interval
 
unknown's avatar
unknown committed
3468 3469 3470 3471 3472 3473 3474 3475 3476
  SYNOPSIS 
    get_partition_id_range_for_endpoint()
      part_info         Partitioning info (partitioning type must be RANGE)
      left_endpoint     TRUE  - the interval is [a; +inf) or (a; +inf)
                        FALSE - the interval is (-inf; a] or (-inf; a).
      include_endpoint  TRUE <=> the endpoint itself is included in the
                        interval

  DESCRIPTION
3477
    This function finds the sub-array of part_info->range_int_array where the
unknown's avatar
unknown committed
3478
    elements have non-empty intersections with the given interval.
3479
 
unknown's avatar
unknown committed
3480 3481 3482 3483 3484 3485 3486
    A range_int_array element at index idx represents the interval
      
      [range_int_array[idx-1], range_int_array[idx]),

    intervals are disjoint and ordered by their right bound, so
    
    1. For [a; +inf) or (a; +inf)-type intervals (left_endpoint==TRUE), the
3487
       sought sub-array starts at some index idx and continues till array end.
unknown's avatar
unknown committed
3488 3489 3490 3491 3492
       The function returns first number idx, such that the interval
       represented by range_int_array[idx] has non empty intersection with 
       the passed interval.
       
    2. For (-inf; a] or (-inf; a)-type intervals (left_endpoint==FALSE), the
3493
       sought sub-array starts at array start and continues till some last
unknown's avatar
unknown committed
3494 3495 3496 3497 3498
       index idx.
       The function returns first number idx, such that the interval
       represented by range_int_array[idx] has EMPTY intersection with the
       passed interval.
       If the interval represented by the last array element has non-empty 
3499
       intersection with the passed interval, part_info->num_parts is
unknown's avatar
unknown committed
3500 3501 3502
       returned.
       
  RETURN
3503
    The edge of corresponding part_info->range_int_array sub-array.
unknown's avatar
unknown committed
3504 3505
*/

3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521
static uint32
get_partition_id_range_for_endpoint_charset(partition_info *part_info,
                                            bool left_endpoint,
                                            bool include_endpoint)
{
  uint32 res;
  copy_to_part_field_buffers(part_info->part_field_array,
                             part_info->part_field_buffers,
                             part_info->restore_part_field_ptrs);
  res= get_partition_id_range_for_endpoint(part_info, left_endpoint,
                                           include_endpoint);
  restore_part_field_pointers(part_info->part_field_array,
                              part_info->restore_part_field_ptrs);
  return res;
}

unknown's avatar
unknown committed
3522 3523 3524 3525 3526
uint32 get_partition_id_range_for_endpoint(partition_info *part_info,
                                           bool left_endpoint,
                                           bool include_endpoint)
{
  longlong *range_array= part_info->range_int_array;
3527
  longlong part_end_val;
3528
  uint max_partition= part_info->num_parts - 1;
unknown's avatar
unknown committed
3529
  uint min_part_id= 0, max_part_id= max_partition, loc_part_id;
3530
  /* Get the partitioning function value for the endpoint */
3531 3532 3533
  longlong part_func_value= 
    part_info->part_expr->val_int_endpoint(left_endpoint, &include_endpoint);

3534 3535
  bool unsigned_flag= part_info->part_expr->unsigned_flag;
  DBUG_ENTER("get_partition_id_range_for_endpoint");
3536

3537 3538
  if (part_info->part_expr->null_value)
  {
3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558
    /*
      Special handling for MONOTONIC functions that can return NULL for
      values that are comparable. I.e.
      '2000-00-00' can be compared to '2000-01-01' but TO_DAYS('2000-00-00')
      returns NULL which cannot be compared used <, >, <=, >= etc.

      Otherwise, just return the first partition
      (may be included if not left endpoint)
    */
    enum_monotonicity_info monotonic;
    monotonic= part_info->part_expr->get_monotonicity_info();
    if (monotonic != MONOTONIC_INCREASING_NOT_NULL &&
        monotonic != MONOTONIC_STRICT_INCREASING_NOT_NULL)
    {
      /* F(col) can not return NULL, return partition with lowest value */
      if (!left_endpoint && include_endpoint)
        DBUG_RETURN(1);
      DBUG_RETURN(0);               

    }
3559
  }
3560

unknown's avatar
unknown committed
3561 3562
  if (unsigned_flag)
    part_func_value-= 0x8000000000000000ULL;
unknown's avatar
unknown committed
3563 3564
  if (left_endpoint && !include_endpoint)
    part_func_value++;
3565 3566 3567 3568 3569

  /*
    Search for the partition containing part_func_value
    (including the right endpoint).
  */
unknown's avatar
unknown committed
3570 3571
  while (max_part_id > min_part_id)
  {
3572 3573
    loc_part_id= (max_part_id + min_part_id) / 2;
    if (range_array[loc_part_id] < part_func_value)
unknown's avatar
unknown committed
3574 3575
      min_part_id= loc_part_id + 1;
    else
3576
      max_part_id= loc_part_id;
unknown's avatar
unknown committed
3577 3578
  }
  loc_part_id= max_part_id;
3579 3580 3581

  /* Adjust for endpoints */
  part_end_val= range_array[loc_part_id];
unknown's avatar
unknown committed
3582 3583
  if (left_endpoint)
  {
3584 3585 3586 3587
    DBUG_ASSERT(part_func_value > part_end_val ?
                (loc_part_id == max_partition &&
                 !part_info->defined_max_value) :
                1);
3588 3589
    /*
      In case of PARTITION p VALUES LESS THAN MAXVALUE
3590 3591 3592
      the maximum value is in the current (last) partition.
      If value is equal or greater than the endpoint,
      the range starts from the next partition.
3593
    */
3594
    if (part_func_value >= part_end_val &&
3595
        (loc_part_id < max_partition || !part_info->defined_max_value))
unknown's avatar
unknown committed
3596 3597 3598 3599
      loc_part_id++;
  }
  else 
  {
3600 3601 3602
    /* if 'WHERE <= X' and partition is LESS THAN (X) include next partition */
    if (include_endpoint && loc_part_id < max_partition &&
        part_func_value == part_end_val)
3603
      loc_part_id++;
3604

3605
    /* Right endpoint, set end after correct partition */
unknown's avatar
unknown committed
3606 3607 3608 3609 3610 3611
    loc_part_id++;
  }
  DBUG_RETURN(loc_part_id);
}


3612 3613 3614
int get_partition_id_hash_nosub(partition_info *part_info,
                                 uint32 *part_id,
                                 longlong *func_value)
3615
{
3616
  return get_part_id_hash(part_info->num_parts, part_info->part_expr,
3617
                          part_id, func_value);
3618 3619 3620
}


3621 3622 3623
int get_partition_id_linear_hash_nosub(partition_info *part_info,
                                        uint32 *part_id,
                                        longlong *func_value)
3624
{
3625
  return get_part_id_linear_hash(part_info, part_info->num_parts,
3626
                                 part_info->part_expr, part_id, func_value);
3627 3628 3629
}


3630 3631 3632
int get_partition_id_key_nosub(partition_info *part_info,
                                uint32 *part_id,
                                longlong *func_value)
3633
{
3634
  *part_id= get_part_id_key(part_info->part_field_array,
3635
                            part_info->num_parts, func_value);
3636
  return 0;
3637 3638 3639
}


3640 3641 3642
int get_partition_id_linear_key_nosub(partition_info *part_info,
                                      uint32 *part_id,
                                      longlong *func_value)
3643
{
3644 3645
  *part_id= get_part_id_linear_key(part_info,
                                   part_info->part_field_array,
3646
                                   part_info->num_parts, func_value);
3647
  return 0;
3648 3649 3650
}


3651 3652 3653
int get_partition_id_with_sub(partition_info *part_info,
                              uint32 *part_id,
                              longlong *func_value)
3654 3655
{
  uint32 loc_part_id, sub_part_id;
3656
  uint num_subparts;
unknown's avatar
unknown committed
3657
  int error;
3658
  DBUG_ENTER("get_partition_id_with_sub");
unknown's avatar
unknown committed
3659

3660 3661 3662
  if (unlikely((error= part_info->get_part_partition_id(part_info,
                                                        &loc_part_id,
                                                        func_value))))
3663
  {
unknown's avatar
unknown committed
3664
    DBUG_RETURN(error);
3665
  }
3666
  num_subparts= part_info->num_subparts;
3667 3668
  if (unlikely((error= part_info->get_subpartition_id(part_info,
                                                      &sub_part_id))))
3669
  {
unknown's avatar
unknown committed
3670
    DBUG_RETURN(error);
3671
  } 
3672
  *part_id= get_part_id_for_sub(loc_part_id, sub_part_id, num_subparts);
unknown's avatar
unknown committed
3673
  DBUG_RETURN(0);
3674 3675 3676 3677 3678
}


/*
  This function is used to calculate the subpartition id
unknown's avatar
unknown committed
3679

3680 3681 3682 3683
  SYNOPSIS
    get_subpartition_id()
    part_info           A reference to the partition_info struct where all the
                        desired information is given
unknown's avatar
unknown committed
3684

3685
  RETURN VALUE
unknown's avatar
unknown committed
3686 3687
    part_id             The subpartition identity

3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700
  DESCRIPTION
    A routine used in some SELECT's when only partial knowledge of the
    partitions is known.
    
    It is actually 4 different variants of this function which are called
    through a function pointer.

    get_partition_id_hash_sub
    get_partition_id_key_sub
    get_partition_id_linear_hash_sub
    get_partition_id_linear_key_sub
*/

3701 3702
int get_partition_id_hash_sub(partition_info *part_info,
                              uint32 *part_id)
3703
{
3704
  longlong func_value;
3705
  return get_part_id_hash(part_info->num_subparts, part_info->subpart_expr,
3706
                          part_id, &func_value);
3707 3708 3709
}


3710 3711
int get_partition_id_linear_hash_sub(partition_info *part_info,
                                     uint32 *part_id)
3712
{
3713
  longlong func_value;
3714
  return get_part_id_linear_hash(part_info, part_info->num_subparts,
3715 3716
                                 part_info->subpart_expr, part_id,
                                 &func_value);
3717 3718 3719
}


3720 3721
int get_partition_id_key_sub(partition_info *part_info,
                             uint32 *part_id)
3722
{
3723
  longlong func_value;
3724
  *part_id= get_part_id_key(part_info->subpart_field_array,
3725
                            part_info->num_subparts, &func_value);
3726
  return FALSE;
3727 3728 3729
}


3730 3731
int get_partition_id_linear_key_sub(partition_info *part_info,
                                       uint32 *part_id)
3732
{
3733
  longlong func_value;
3734 3735
  *part_id= get_part_id_linear_key(part_info,
                                   part_info->subpart_field_array,
3736
                                   part_info->num_subparts, &func_value);
3737
  return FALSE;
3738 3739 3740 3741
}


/*
unknown's avatar
unknown committed
3742 3743
  Set an indicator on all partition fields that are set by the key

3744 3745 3746 3747
  SYNOPSIS
    set_PF_fields_in_key()
    key_info                   Information about the index
    key_length                 Length of key
unknown's avatar
unknown committed
3748

3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788
  RETURN VALUE
    TRUE                       Found partition field set by key
    FALSE                      No partition field set by key
*/

static bool set_PF_fields_in_key(KEY *key_info, uint key_length)
{
  KEY_PART_INFO *key_part;
  bool found_part_field= FALSE;
  DBUG_ENTER("set_PF_fields_in_key");

  for (key_part= key_info->key_part; (int)key_length > 0; key_part++)
  {
    if (key_part->null_bit)
      key_length--;
    if (key_part->type == HA_KEYTYPE_BIT)
    {
      if (((Field_bit*)key_part->field)->bit_len)
        key_length--;
    }
    if (key_part->key_part_flag & (HA_BLOB_PART + HA_VAR_LENGTH_PART))
    {
      key_length-= HA_KEY_BLOB_LENGTH;
    }
    if (key_length < key_part->length)
      break;
    key_length-= key_part->length;
    if (key_part->field->flags & FIELD_IN_PART_FUNC_FLAG)
    {
      found_part_field= TRUE;
      key_part->field->flags|= GET_FIXED_FIELDS_FLAG;
    }
  }
  DBUG_RETURN(found_part_field);
}


/*
  We have found that at least one partition field was set by a key, now
  check if a partition function has all its fields bound or not.
unknown's avatar
unknown committed
3789

3790 3791 3792
  SYNOPSIS
    check_part_func_bound()
    ptr                     Array of fields NULL terminated (partition fields)
unknown's avatar
unknown committed
3793

3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818
  RETURN VALUE
    TRUE                    All fields in partition function are set
    FALSE                   Not all fields in partition function are set
*/

static bool check_part_func_bound(Field **ptr)
{
  bool result= TRUE;
  DBUG_ENTER("check_part_func_bound");

  for (; *ptr; ptr++)
  {
    if (!((*ptr)->flags & GET_FIXED_FIELDS_FLAG))
    {
      result= FALSE;
      break;
    }
  }
  DBUG_RETURN(result);
}


/*
  Get the id of the subpartitioning part by using the key buffer of the
  index scan.
unknown's avatar
unknown committed
3819

3820 3821 3822 3823 3824 3825
  SYNOPSIS
    get_sub_part_id_from_key()
    table         The table object
    buf           A buffer that can be used to evaluate the partition function
    key_info      The index object
    key_spec      A key_range containing key and key length
3826
    out:part_id   The returned partition id
unknown's avatar
unknown committed
3827

3828
  RETURN VALUES
3829 3830
    TRUE                    All fields in partition function are set
    FALSE                   Not all fields in partition function are set
unknown's avatar
unknown committed
3831

3832 3833 3834 3835 3836
  DESCRIPTION
    Use key buffer to set-up record in buf, move field pointers and
    get the partition identity and restore field pointers afterwards.
*/

3837 3838 3839 3840
static int get_sub_part_id_from_key(const TABLE *table,uchar *buf,
                                    KEY *key_info,
                                    const key_range *key_spec,
                                    uint32 *part_id)
3841
{
3842
  uchar *rec0= table->record[0];
unknown's avatar
unknown committed
3843
  partition_info *part_info= table->part_info;
3844
  int res;
3845 3846
  DBUG_ENTER("get_sub_part_id_from_key");

3847
  key_restore(buf, (uchar*)key_spec->key, key_info, key_spec->length);
3848
  if (likely(rec0 == buf))
unknown's avatar
unknown committed
3849
  {
3850
    res= part_info->get_subpartition_id(part_info, part_id);
unknown's avatar
unknown committed
3851
  }
3852 3853 3854 3855
  else
  {
    Field **part_field_array= part_info->subpart_field_array;
    set_field_ptr(part_field_array, buf, rec0);
3856
    res= part_info->get_subpartition_id(part_info, part_id);
3857 3858
    set_field_ptr(part_field_array, rec0, buf);
  }
3859
  DBUG_RETURN(res);
3860 3861 3862 3863 3864
}

/*
  Get the id of the partitioning part by using the key buffer of the
  index scan.
unknown's avatar
unknown committed
3865

3866 3867 3868 3869 3870 3871
  SYNOPSIS
    get_part_id_from_key()
    table         The table object
    buf           A buffer that can be used to evaluate the partition function
    key_info      The index object
    key_spec      A key_range containing key and key length
unknown's avatar
unknown committed
3872 3873
    out:part_id   Partition to use

3874 3875 3876
  RETURN VALUES
    TRUE          Partition to use not found
    FALSE         Ok, part_id indicates partition to use
unknown's avatar
unknown committed
3877

3878 3879 3880 3881
  DESCRIPTION
    Use key buffer to set-up record in buf, move field pointers and
    get the partition identity and restore field pointers afterwards.
*/
unknown's avatar
unknown committed
3882

3883
bool get_part_id_from_key(const TABLE *table, uchar *buf, KEY *key_info,
3884 3885 3886
                          const key_range *key_spec, uint32 *part_id)
{
  bool result;
3887
  uchar *rec0= table->record[0];
unknown's avatar
unknown committed
3888
  partition_info *part_info= table->part_info;
3889
  longlong func_value;
3890 3891
  DBUG_ENTER("get_part_id_from_key");

3892
  key_restore(buf, (uchar*)key_spec->key, key_info, key_spec->length);
3893
  if (likely(rec0 == buf))
unknown's avatar
unknown committed
3894
  {
3895 3896
    result= part_info->get_part_partition_id(part_info, part_id,
                                             &func_value);
unknown's avatar
unknown committed
3897
  }
3898 3899 3900 3901
  else
  {
    Field **part_field_array= part_info->part_field_array;
    set_field_ptr(part_field_array, buf, rec0);
3902 3903
    result= part_info->get_part_partition_id(part_info, part_id,
                                             &func_value);
3904 3905 3906 3907 3908 3909 3910 3911
    set_field_ptr(part_field_array, rec0, buf);
  }
  DBUG_RETURN(result);
}

/*
  Get the partitioning id of the full PF by using the key buffer of the
  index scan.
unknown's avatar
unknown committed
3912

3913 3914 3915 3916 3917 3918
  SYNOPSIS
    get_full_part_id_from_key()
    table         The table object
    buf           A buffer that is used to evaluate the partition function
    key_info      The index object
    key_spec      A key_range containing key and key length
unknown's avatar
unknown committed
3919 3920
    out:part_spec A partition id containing start part and end part

3921 3922 3923
  RETURN VALUES
    part_spec
    No partitions to scan is indicated by end_part > start_part when returning
unknown's avatar
unknown committed
3924

3925 3926 3927 3928 3929
  DESCRIPTION
    Use key buffer to set-up record in buf, move field pointers if needed and
    get the partition identity and restore field pointers afterwards.
*/

3930
void get_full_part_id_from_key(const TABLE *table, uchar *buf,
3931 3932 3933 3934 3935
                               KEY *key_info,
                               const key_range *key_spec,
                               part_id_range *part_spec)
{
  bool result;
unknown's avatar
unknown committed
3936
  partition_info *part_info= table->part_info;
3937
  uchar *rec0= table->record[0];
3938
  longlong func_value;
3939 3940
  DBUG_ENTER("get_full_part_id_from_key");

3941
  key_restore(buf, (uchar*)key_spec->key, key_info, key_spec->length);
3942
  if (likely(rec0 == buf))
unknown's avatar
unknown committed
3943
  {
3944 3945
    result= part_info->get_partition_id(part_info, &part_spec->start_part,
                                        &func_value);
unknown's avatar
unknown committed
3946
  }
3947 3948 3949 3950
  else
  {
    Field **part_field_array= part_info->full_part_field_array;
    set_field_ptr(part_field_array, buf, rec0);
3951 3952
    result= part_info->get_partition_id(part_info, &part_spec->start_part,
                                        &func_value);
3953 3954 3955 3956 3957 3958 3959
    set_field_ptr(part_field_array, rec0, buf);
  }
  part_spec->end_part= part_spec->start_part;
  if (unlikely(result))
    part_spec->start_part++;
  DBUG_VOID_RETURN;
}
3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996

/*
  Prune the set of partitions to use in query 

  SYNOPSIS
    prune_partition_set()
    table         The table object
    out:part_spec Contains start part, end part 

  DESCRIPTION
    This function is called to prune the range of partitions to scan by
    checking the used_partitions bitmap.
    If start_part > end_part at return it means no partition needs to be
    scanned. If start_part == end_part it always means a single partition
    needs to be scanned.

  RETURN VALUE
    part_spec
*/
void prune_partition_set(const TABLE *table, part_id_range *part_spec)
{
  int last_partition= -1;
  uint i;
  partition_info *part_info= table->part_info;

  DBUG_ENTER("prune_partition_set");
  for (i= part_spec->start_part; i <= part_spec->end_part; i++)
  {
    if (bitmap_is_set(&(part_info->used_partitions), i))
    {
      DBUG_PRINT("info", ("Partition %d is set", i));
      if (last_partition == -1)
        /* First partition found in set and pruned bitmap */
        part_spec->start_part= i;
      last_partition= i;
    }
  }
unknown's avatar
unknown committed
3997 3998 3999 4000
  if (last_partition == -1)
    /* No partition found in pruned bitmap */
    part_spec->start_part= part_spec->end_part + 1;  
  else //if (last_partition != -1)
4001 4002 4003 4004 4005
    part_spec->end_part= last_partition;

  DBUG_VOID_RETURN;
}

4006 4007
/*
  Get the set of partitions to use in query.
unknown's avatar
unknown committed
4008

4009 4010 4011 4012 4013 4014
  SYNOPSIS
    get_partition_set()
    table         The table object
    buf           A buffer that can be used to evaluate the partition function
    index         The index of the key used, if MAX_KEY no index used
    key_spec      A key_range containing key and key length
unknown's avatar
unknown committed
4015
    out:part_spec Contains start part, end part and indicator if bitmap is
4016
                  used for which partitions to scan
unknown's avatar
unknown committed
4017

4018 4019 4020 4021 4022 4023 4024 4025 4026
  DESCRIPTION
    This function is called to discover which partitions to use in an index
    scan or a full table scan.
    It returns a range of partitions to scan. If there are holes in this
    range with partitions that are not needed to scan a bit array is used
    to signal which partitions to use and which not to use.
    If start_part > end_part at return it means no partition needs to be
    scanned. If start_part == end_part it always means a single partition
    needs to be scanned.
unknown's avatar
unknown committed
4027

4028 4029 4030
  RETURN VALUE
    part_spec
*/
4031
void get_partition_set(const TABLE *table, uchar *buf, const uint index,
4032 4033
                       const key_range *key_spec, part_id_range *part_spec)
{
unknown's avatar
unknown committed
4034
  partition_info *part_info= table->part_info;
4035
  uint num_parts= part_info->get_tot_partitions();
unknown's avatar
unknown committed
4036
  uint i, part_id;
4037 4038
  uint sub_part= num_parts;
  uint32 part_part= num_parts;
4039 4040 4041 4042 4043
  KEY *key_info= NULL;
  bool found_part_field= FALSE;
  DBUG_ENTER("get_partition_set");

  part_spec->start_part= 0;
4044
  part_spec->end_part= num_parts - 1;
4045
  if ((index < MAX_KEY) && 
4046
       key_spec && key_spec->flag == (uint)HA_READ_KEY_EXACT &&
4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068
       part_info->some_fields_in_PF.is_set(index))
  {
    key_info= table->key_info+index;
    /*
      The index can potentially provide at least one PF-field (field in the
      partition function). Thus it is interesting to continue our probe.
    */
    if (key_spec->length == key_info->key_length)
    {
      /*
        The entire key is set so we can check whether we can immediately
        derive either the complete PF or if we can derive either
        the top PF or the subpartitioning PF. This can be established by
        checking precalculated bits on each index.
      */
      if (part_info->all_fields_in_PF.is_set(index))
      {
        /*
          We can derive the exact partition to use, no more than this one
          is needed.
        */
        get_full_part_id_from_key(table,buf,key_info,key_spec,part_spec);
4069 4070 4071 4072
        /*
          Check if range can be adjusted by looking in used_partitions
        */
        prune_partition_set(table, part_spec);
4073 4074
        DBUG_VOID_RETURN;
      }
4075
      else if (part_info->is_sub_partitioned())
4076 4077
      {
        if (part_info->all_fields_in_SPF.is_set(index))
4078 4079 4080
        {
          if (get_sub_part_id_from_key(table, buf, key_info, key_spec, &sub_part))
          {
4081
            part_spec->start_part= num_parts;
4082 4083 4084
            DBUG_VOID_RETURN;
          }
        }
4085 4086
        else if (part_info->all_fields_in_PPF.is_set(index))
        {
unknown's avatar
unknown committed
4087 4088
          if (get_part_id_from_key(table,buf,key_info,
                                   key_spec,(uint32*)&part_part))
4089 4090 4091 4092 4093 4094
          {
            /*
              The value of the RANGE or LIST partitioning was outside of
              allowed values. Thus it is certain that the result of this
              scan will be empty.
            */
4095
            part_spec->start_part= num_parts;
4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120
            DBUG_VOID_RETURN;
          }
        }
      }
    }
    else
    {
      /*
        Set an indicator on all partition fields that are bound.
        If at least one PF-field was bound it pays off to check whether
        the PF or PPF or SPF has been bound.
        (PF = Partition Function, SPF = Subpartition Function and
         PPF = Partition Function part of subpartitioning)
      */
      if ((found_part_field= set_PF_fields_in_key(key_info,
                                                  key_spec->length)))
      {
        if (check_part_func_bound(part_info->full_part_field_array))
        {
          /*
            We were able to bind all fields in the partition function even
            by using only a part of the key. Calculate the partition to use.
          */
          get_full_part_id_from_key(table,buf,key_info,key_spec,part_spec);
          clear_indicator_in_key_fields(key_info);
4121 4122 4123 4124
          /*
            Check if range can be adjusted by looking in used_partitions
          */
          prune_partition_set(table, part_spec);
4125 4126
          DBUG_VOID_RETURN; 
        }
4127
        else if (part_info->is_sub_partitioned())
4128
        {
unknown's avatar
unknown committed
4129
          if (check_part_func_bound(part_info->subpart_field_array))
4130 4131 4132
          {
            if (get_sub_part_id_from_key(table, buf, key_info, key_spec, &sub_part))
            {
4133
              part_spec->start_part= num_parts;
4134 4135 4136 4137
              clear_indicator_in_key_fields(key_info);
              DBUG_VOID_RETURN;
            }
          }
unknown's avatar
unknown committed
4138
          else if (check_part_func_bound(part_info->part_field_array))
4139
          {
unknown's avatar
unknown committed
4140 4141
            if (get_part_id_from_key(table,buf,key_info,key_spec,&part_part))
            {
4142
              part_spec->start_part= num_parts;
unknown's avatar
unknown committed
4143 4144 4145
              clear_indicator_in_key_fields(key_info);
              DBUG_VOID_RETURN;
            }
4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162
          }
        }
      }
    }
  }
  {
    /*
      The next step is to analyse the table condition to see whether any
      information about which partitions to scan can be derived from there.
      Currently not implemented.
    */
  }
  /*
    If we come here we have found a range of sorts we have either discovered
    nothing or we have discovered a range of partitions with possible holes
    in it. We need a bitvector to further the work here.
  */
4163
  if (!(part_part == num_parts && sub_part == num_parts))
4164 4165 4166 4167
  {
    /*
      We can only arrive here if we are using subpartitioning.
    */
4168
    if (part_part != num_parts)
4169 4170 4171 4172 4173
    {
      /*
        We know the top partition and need to scan all underlying
        subpartitions. This is a range without holes.
      */
4174 4175 4176
      DBUG_ASSERT(sub_part == num_parts);
      part_spec->start_part= part_part * part_info->num_subparts;
      part_spec->end_part= part_spec->start_part+part_info->num_subparts - 1;
4177 4178 4179
    }
    else
    {
4180
      DBUG_ASSERT(sub_part != num_parts);
4181 4182
      part_spec->start_part= sub_part;
      part_spec->end_part=sub_part+
4183 4184 4185
                           (part_info->num_subparts*(part_info->num_parts-1));
      for (i= 0, part_id= sub_part; i < part_info->num_parts;
           i++, part_id+= part_info->num_subparts)
4186 4187 4188 4189 4190
        ; //Set bit part_id in bit array
    }
  }
  if (found_part_field)
    clear_indicator_in_key_fields(key_info);
4191 4192 4193 4194
  /*
    Check if range can be adjusted by looking in used_partitions
  */
  prune_partition_set(table, part_spec);
4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215
  DBUG_VOID_RETURN;
}

/*
   If the table is partitioned we will read the partition info into the
   .frm file here.
   -------------------------------
   |  Fileinfo     64 bytes      |
   -------------------------------
   | Formnames     7 bytes       |
   -------------------------------
   | Not used    4021 bytes      |
   -------------------------------
   | Keyinfo + record            |
   -------------------------------
   | Padded to next multiple     |
   | of IO_SIZE                  |
   -------------------------------
   | Forminfo     288 bytes      |
   -------------------------------
   | Screen buffer, to make      |
unknown's avatar
unknown committed
4216
   |field names readable        |
4217 4218
   -------------------------------
   | Packed field info           |
unknown's avatar
unknown committed
4219
   |17 + 1 + strlen(field_name) |
4220 4221 4222 4223 4224 4225 4226 4227
   | + 1 end of file character   |
   -------------------------------
   | Partition info              |
   -------------------------------
   We provide the length of partition length in Fileinfo[55-58].

   Read the partition syntax from the frm file and parse it to get the
   data structures of the partitioning.
unknown's avatar
unknown committed
4228

4229 4230 4231
   SYNOPSIS
     mysql_unpack_partition()
     thd                           Thread object
unknown's avatar
unknown committed
4232
     part_buf                      Partition info from frm file
4233 4234
     part_info_len                 Length of partition syntax
     table                         Table object of partitioned table
unknown's avatar
unknown committed
4235 4236
     create_table_ind              Is it called from CREATE TABLE
     default_db_type               What is the default engine of the table
4237 4238
     work_part_info_used           Flag is raised if we don't create new
                                   part_info, but used thd->work_part_info
unknown's avatar
unknown committed
4239

4240 4241 4242
   RETURN VALUE
     TRUE                          Error
     FALSE                         Sucess
unknown's avatar
unknown committed
4243

4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254
   DESCRIPTION
     Read the partition syntax from the current position in the frm file.
     Initiate a LEX object, save the list of item tree objects to free after
     the query is done. Set-up partition info object such that parser knows
     it is called from internally. Call parser to create data structures
     (best possible recreation of item trees and so forth since there is no
     serialisation of these objects other than in parseable text format).
     We need to save the text of the partition functions since it is not
     possible to retrace this given an item tree.
*/

4255
bool mysql_unpack_partition(THD *thd,
4256
                            char *part_buf, uint part_info_len,
unknown's avatar
unknown committed
4257
                            TABLE* table, bool is_create_table_ind,
4258 4259
                            handlerton *default_db_type,
                            bool *work_part_info_used)
4260 4261 4262
{
  bool result= TRUE;
  partition_info *part_info;
4263
  CHARSET_INFO *old_character_set_client= thd->variables.character_set_client;
unknown's avatar
unknown committed
4264 4265
  LEX *old_lex= thd->lex;
  LEX lex;
4266
  DBUG_ENTER("mysql_unpack_partition");
unknown's avatar
unknown committed
4267

4268
  thd->variables.character_set_client= system_charset_info;
4269

4270 4271 4272
  Parser_state parser_state;
  if (parser_state.init(thd, part_buf, part_info_len))
    goto end;
4273

4274
  if (init_lex_with_single_table(thd, table, &lex))
4275
    goto end;
4276

4277 4278 4279 4280 4281 4282 4283 4284 4285
  /*
    All Items created is put into a free list on the THD object. This list
    is used to free all Item objects after completing a query. We don't
    want that to happen with the Item tree created as part of the partition
    info. This should be attached to the table object and remain so until
    the table object is released.
    Thus we move away the current list temporarily and start a new list that
    we then save in the partition info structure.
  */
4286
  *work_part_info_used= FALSE;
4287
  lex.part_info= new partition_info();/* Indicates MYSQLparse from this place */
unknown's avatar
unknown committed
4288 4289 4290 4291 4292
  if (!lex.part_info)
  {
    mem_alloc_error(sizeof(partition_info));
    goto end;
  }
4293
  part_info= lex.part_info;
unknown's avatar
unknown committed
4294
  DBUG_PRINT("info", ("Parse: %s", part_buf));
4295 4296
  if (parse_sql(thd, & parser_state, NULL) ||
      part_info->fix_parser_data(thd))
4297
  {
4298
    thd->free_items();
4299 4300
    goto end;
  }
unknown's avatar
unknown committed
4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316
  /*
    The parsed syntax residing in the frm file can still contain defaults.
    The reason is that the frm file is sometimes saved outside of this
    MySQL Server and used in backup and restore of clusters or partitioned
    tables. It is not certain that the restore will restore exactly the
    same default partitioning.
    
    The easiest manner of handling this is to simply continue using the
    part_info we already built up during mysql_create_table if we are
    in the process of creating a table. If the table already exists we
    need to discover the number of partitions for the default parts. Since
    the handler object hasn't been created here yet we need to postpone this
    to the fix_partition_func method.
  */

  DBUG_PRINT("info", ("Successful parse"));
unknown's avatar
unknown committed
4317 4318 4319
  DBUG_PRINT("info", ("default engine = %s, default_db_type = %s",
             ha_resolve_storage_engine_name(part_info->default_engine_type),
             ha_resolve_storage_engine_name(default_db_type)));
4320
  if (is_create_table_ind && old_lex->sql_command == SQLCOM_CREATE_TABLE)
unknown's avatar
unknown committed
4321
  {
4322 4323 4324 4325 4326 4327 4328 4329
    /*
      When we come here we are doing a create table. In this case we
      have already done some preparatory work on the old part_info
      object. We don't really need this new partition_info object.
      Thus we go back to the old partition info object.
      We need to free any memory objects allocated on item_free_list
      by the parser since we are keeping the old info from the first
      parser call in CREATE TABLE.
4330 4331 4332 4333

      This table object can not be used any more. However, since
      this is CREATE TABLE, we know that it will be destroyed by the
      caller, and rely on that.
4334 4335 4336 4337
    */
    thd->free_items();
    part_info= thd->work_part_info;
    *work_part_info_used= true;
unknown's avatar
unknown committed
4338
  }
unknown's avatar
unknown committed
4339
  table->part_info= part_info;
4340
  table->file->set_part_info(part_info);
4341
  if (!part_info->default_engine_type)
4342
    part_info->default_engine_type= default_db_type;
4343
  DBUG_ASSERT(part_info->default_engine_type == default_db_type);
4344 4345
  DBUG_ASSERT(part_info->default_engine_type->db_type != DB_TYPE_UNKNOWN);
  DBUG_ASSERT(part_info->default_engine_type != partition_hton);
4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358

  {
  /*
    This code part allocates memory for the serialised item information for
    the partition functions. In most cases this is not needed but if the
    table is used for SHOW CREATE TABLES or ALTER TABLE that modifies
    partition information it is needed and the info is lost if we don't
    save it here so unfortunately we have to do it here even if in most
    cases it is not needed. This is a consequence of that item trees are
    not serialisable.
  */
    uint part_func_len= part_info->part_func_len;
    uint subpart_func_len= part_info->subpart_func_len; 
unknown's avatar
unknown committed
4359 4360 4361
    char *part_func_string= NULL;
    char *subpart_func_string= NULL;
    if ((part_func_len &&
4362
         !((part_func_string= (char*) thd->alloc(part_func_len)))) ||
4363
        (subpart_func_len &&
4364
         !((subpart_func_string= (char*) thd->alloc(subpart_func_len)))))
4365
    {
unknown's avatar
unknown committed
4366
      mem_alloc_error(part_func_len);
4367
      thd->free_items();
4368 4369
      goto end;
    }
unknown's avatar
unknown committed
4370 4371
    if (part_func_len)
      memcpy(part_func_string, part_info->part_func_string, part_func_len);
4372 4373 4374 4375 4376 4377 4378 4379 4380
    if (subpart_func_len)
      memcpy(subpart_func_string, part_info->subpart_func_string,
             subpart_func_len);
    part_info->part_func_string= part_func_string;
    part_info->subpart_func_string= subpart_func_string;
  }

  result= FALSE;
end:
4381
  end_lex_with_single_table(thd, table, old_lex);
4382
  thd->variables.character_set_client= old_character_set_client;
4383 4384
  DBUG_RETURN(result);
}
unknown's avatar
unknown committed
4385

4386

unknown's avatar
unknown committed
4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418
/*
  Set engine type on all partition element objects
  SYNOPSIS
    set_engine_all_partitions()
    part_info                  Partition info
    engine_type                Handlerton reference of engine
  RETURN VALUES
    NONE
*/

static
void
set_engine_all_partitions(partition_info *part_info,
                          handlerton *engine_type)
{
  uint i= 0;
  List_iterator<partition_element> part_it(part_info->partitions);
  do
  {
    partition_element *part_elem= part_it++;

    part_elem->engine_type= engine_type;
    if (part_info->is_sub_partitioned())
    {
      List_iterator<partition_element> sub_it(part_elem->subpartitions);
      uint j= 0;

      do
      {
        partition_element *sub_elem= sub_it++;

        sub_elem->engine_type= engine_type;
4419
      } while (++j < part_info->num_subparts);
unknown's avatar
unknown committed
4420
    }
4421
  } while (++i < part_info->num_parts);
unknown's avatar
unknown committed
4422
}
unknown's avatar
unknown committed
4423 4424


4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435
/**
  Support routine to handle the successful cases for partition management.

  @param thd               Thread object
  @param copied            Number of records copied
  @param deleted           Number of records deleted
  @param table_list        Table list with the one table in it

  @return Operation status
    @retval FALSE          Success
    @retval TRUE           Failure
4436 4437
*/

unknown's avatar
unknown committed
4438 4439
static int fast_end_partition(THD *thd, ulonglong copied,
                              ulonglong deleted,
4440
                              TABLE_LIST *table_list)
4441
{
4442
  char tmp_name[80];
unknown's avatar
unknown committed
4443 4444 4445
  DBUG_ENTER("fast_end_partition");

  thd->proc_info="end";
4446

4447
  query_cache_invalidate3(thd, table_list, 0);
4448 4449 4450 4451 4452

  my_snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
              (ulong) (copied + deleted),
              (ulong) deleted,
              (ulong) 0);
4453
  my_ok(thd, (ha_rows) (copied+deleted),0L, tmp_name);
4454
  DBUG_RETURN(FALSE);
unknown's avatar
unknown committed
4455 4456 4457 4458 4459 4460
}


/*
  We need to check if engine used by all partitions can handle
  partitioning natively.
4461

unknown's avatar
unknown committed
4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481
  SYNOPSIS
    check_native_partitioned()
    create_info            Create info in CREATE TABLE
    out:ret_val            Return value
    part_info              Partition info
    thd                    Thread object

  RETURN VALUES
  Value returned in bool ret_value
    TRUE                   Native partitioning supported by engine
    FALSE                  Need to use partition handler

  Return value from function
    TRUE                   Error
    FALSE                  Success
*/

static bool check_native_partitioned(HA_CREATE_INFO *create_info,bool *ret_val,
                                     partition_info *part_info, THD *thd)
{
4482 4483
  bool table_engine_set;
  handlerton *engine_type= part_info->default_engine_type;
4484
  handlerton *old_engine_type= engine_type;
unknown's avatar
unknown committed
4485 4486
  DBUG_ENTER("check_native_partitioned");

4487
  if (create_info->used_fields & HA_CREATE_USED_ENGINE)
4488
  {
4489 4490 4491 4492 4493 4494 4495
    table_engine_set= TRUE;
    engine_type= create_info->db_type;
  }
  else
  {
    table_engine_set= FALSE;
    if (thd->lex->sql_command != SQLCOM_CREATE_TABLE)
unknown's avatar
unknown committed
4496
    {
4497 4498 4499
      table_engine_set= TRUE;
      DBUG_ASSERT(engine_type && engine_type != partition_hton);
    }
4500
  }
unknown's avatar
unknown committed
4501 4502
  DBUG_PRINT("info", ("engine_type = %s, table_engine_set = %u",
                       ha_resolve_storage_engine_name(engine_type),
4503 4504 4505
                       table_engine_set));
  if (part_info->check_engine_mix(engine_type, table_engine_set))
    goto error;
4506

unknown's avatar
unknown committed
4507 4508 4509 4510
  /*
    All engines are of the same type. Check if this engine supports
    native partitioning.
  */
4511 4512 4513 4514 4515

  if (!engine_type)
    engine_type= old_engine_type;
  DBUG_PRINT("info", ("engine_type = %s",
              ha_resolve_storage_engine_name(engine_type)));
unknown's avatar
unknown committed
4516 4517 4518 4519 4520 4521 4522 4523
  if (engine_type->partition_flags &&
      (engine_type->partition_flags() & HA_CAN_PARTITION))
  {
    create_info->db_type= engine_type;
    DBUG_PRINT("info", ("Changed to native partitioning"));
    *ret_val= TRUE;
  }
  DBUG_RETURN(FALSE);
4524 4525 4526 4527 4528
error:
  /*
    Mixed engines not yet supported but when supported it will need
    the partition handler
  */
unknown's avatar
unknown committed
4529
  my_error(ER_MIX_HANDLER_ERROR, MYF(0));
4530 4531
  *ret_val= FALSE;
  DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
4532 4533 4534
}


4535 4536 4537 4538 4539 4540 4541 4542 4543 4544
/**
  Sets which partitions to be used in the command.

  @param alter_info     Alter_info pointer holding partition names and flags.
  @param tab_part_info  partition_info holding all partitions.
  @param part_state     Which state to set for the named partitions.

  @return Operation status
    @retval false  Success
    @retval true   Failure
4545
*/
4546 4547 4548

bool set_part_state(Alter_info *alter_info, partition_info *tab_part_info,
                    enum partition_state part_state)
4549 4550
{
  uint part_count= 0;
4551
  uint num_parts_found= 0;
4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563
  List_iterator<partition_element> part_it(tab_part_info->partitions);

  do
  {
    partition_element *part_elem= part_it++;
    if ((alter_info->flags & ALTER_ALL_PARTITION) ||
         (is_name_in_list(part_elem->partition_name,
          alter_info->partition_names)))
    {
      /*
        Mark the partition.
        I.e mark the partition as a partition to be "changed" by
4564
        analyzing/optimizing/rebuilding/checking/repairing/...
4565
      */
4566
      num_parts_found++;
4567 4568 4569 4570
      part_elem->part_state= part_state;
      DBUG_PRINT("info", ("Setting part_state to %u for partition %s",
                          part_state, part_elem->partition_name));
    }
4571 4572
    else
      part_elem->part_state= PART_NORMAL;
4573
  } while (++part_count < tab_part_info->num_parts);
4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588

  if (num_parts_found != alter_info->partition_names.elements &&
      !(alter_info->flags & ALTER_ALL_PARTITION))
  {
    /* Not all given partitions found, revert and return failure */
    part_it.rewind();
    part_count= 0;
    do
    {
      partition_element *part_elem= part_it++;
      part_elem->part_state= PART_NORMAL;
    } while (++part_count < tab_part_info->num_parts);
    return true;
  }
  return false;
4589 4590 4591
}


4592
/**
unknown's avatar
unknown committed
4593 4594
  Prepare for ALTER TABLE of partition structure

4595 4596 4597 4598 4599 4600 4601 4602
  @param[in] thd                 Thread object
  @param[in] table               Table object
  @param[in,out] alter_info      Alter information
  @param[in,out] create_info     Create info for CREATE TABLE
  @param[in] old_db_type         Old engine type
  @param[out] partition_changed  Boolean indicating whether partition changed
  @param[out] fast_alter_table   Internal temporary table allowing fast
                                 partition change or NULL if not possible
unknown's avatar
unknown committed
4603

4604 4605 4606
  @return Operation status
    @retval TRUE                 Error
    @retval FALSE                Success
unknown's avatar
unknown committed
4607

4608
  @note 
unknown's avatar
unknown committed
4609
    This method handles all preparations for ALTER TABLE for partitioned
4610
    tables.
unknown's avatar
unknown committed
4611 4612 4613 4614 4615 4616 4617
    We need to handle both partition management command such as Add Partition
    and others here as well as an ALTER TABLE that completely changes the
    partitioning and yet others that don't change anything at all. We start
    by checking the partition management variants and then check the general
    change patterns.
*/

4618
uint prep_alter_part_table(THD *thd, TABLE *table, Alter_info *alter_info,
unknown's avatar
unknown committed
4619 4620 4621
                           HA_CREATE_INFO *create_info,
                           handlerton *old_db_type,
                           bool *partition_changed,
4622 4623 4624 4625
                           char *db,
                           const char *table_name,
                           const char *path,
                           TABLE **fast_alter_table)
unknown's avatar
unknown committed
4626
{
4627
  TABLE *new_table= NULL;
unknown's avatar
unknown committed
4628 4629
  DBUG_ENTER("prep_alter_part_table");

4630 4631 4632 4633 4634 4635
  /* Foreign keys on partitioned tables are not supported, waits for WL#148 */
  if (table->part_info && (alter_info->flags & ALTER_FOREIGN_KEY))
  {
    my_error(ER_FOREIGN_KEY_ON_PARTITIONED, MYF(0));
    DBUG_RETURN(TRUE);
  }
4636

4637
  thd->work_part_info= thd->lex->part_info;
4638

4639 4640
  if (thd->work_part_info &&
      !(thd->work_part_info= thd->lex->part_info->get_clone()))
unknown's avatar
unknown committed
4641 4642
    DBUG_RETURN(TRUE);

4643 4644 4645
  /* ALTER_ADMIN_PARTITION is handled in mysql_admin_table */
  DBUG_ASSERT(!(alter_info->flags & ALTER_ADMIN_PARTITION));

unknown's avatar
unknown committed
4646 4647 4648
  if (alter_info->flags &
      (ALTER_ADD_PARTITION | ALTER_DROP_PARTITION |
       ALTER_COALESCE_PARTITION | ALTER_REORGANIZE_PARTITION |
4649
       ALTER_TABLE_REORG | ALTER_REBUILD_PARTITION))
unknown's avatar
unknown committed
4650
  {
4651
    partition_info *tab_part_info;
unknown's avatar
unknown committed
4652
    partition_info *alt_part_info= thd->work_part_info;
4653
    uint flags= 0;
4654
    bool is_last_partition_reorged= FALSE;
4655 4656 4657 4658
    part_elem_value *tab_max_elem_val= NULL;
    part_elem_value *alt_max_elem_val= NULL;
    longlong tab_max_range= 0, alt_max_range= 0;

4659
    if (!table->part_info)
unknown's avatar
unknown committed
4660 4661 4662 4663
    {
      my_error(ER_PARTITION_MGMT_ON_NONPARTITIONED, MYF(0));
      DBUG_RETURN(TRUE);
    }
4664 4665 4666 4667 4668 4669 4670 4671 4672

    /*
      Open our intermediate table, we will operate on a temporary instance
      of the original table, to be able to skip copying all partitions.
      Open it as a copy of the original table, and modify its partition_info
      object to allow fast_alter_partition_table to perform the changes.
    */
    DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE, db, table_name,
                                               MDL_INTENTION_EXCLUSIVE));
4673
    new_table= open_table_uncached(thd, path, db, table_name, 0);
4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684
    if (!new_table)
      DBUG_RETURN(TRUE);

    /*
      This table may be used for copy rows between partitions
      and also read/write columns when fixing the partition_info struct.
    */
    new_table->use_all_columns();
 
    tab_part_info= new_table->part_info;

4685
    if (alter_info->flags & ALTER_TABLE_REORG)
unknown's avatar
unknown committed
4686 4687 4688
    {
      uint new_part_no, curr_part_no;
      if (tab_part_info->part_type != HASH_PARTITION ||
4689
          tab_part_info->use_default_num_partitions)
unknown's avatar
unknown committed
4690 4691
      {
        my_error(ER_REORG_NO_PARAM_ERROR, MYF(0));
4692
        goto err;
unknown's avatar
unknown committed
4693
      }
4694
      new_part_no= new_table->file->get_default_no_partitions(create_info);
4695
      curr_part_no= tab_part_info->num_parts;
unknown's avatar
unknown committed
4696 4697 4698 4699 4700 4701 4702
      if (new_part_no == curr_part_no)
      {
        /*
          No change is needed, we will have the same number of partitions
          after the change as before. Thus we can reply ok immediately
          without any changes at all.
        */
4703 4704
        *fast_alter_table= new_table;
        thd->work_part_info= tab_part_info;
4705
        DBUG_RETURN(FALSE);
unknown's avatar
unknown committed
4706 4707 4708 4709 4710 4711 4712 4713
      }
      else if (new_part_no > curr_part_no)
      {
        /*
          We will add more partitions, we use the ADD PARTITION without
          setting the flag for no default number of partitions
        */
        alter_info->flags|= ALTER_ADD_PARTITION;
4714
        thd->work_part_info->num_parts= new_part_no - curr_part_no;
unknown's avatar
unknown committed
4715 4716 4717 4718 4719 4720 4721 4722
      }
      else
      {
        /*
          We will remove hash partitions, we use the COALESCE PARTITION
          without setting the flag for no default number of partitions
        */
        alter_info->flags|= ALTER_COALESCE_PARTITION;
4723
        alter_info->num_parts= curr_part_no - new_part_no;
unknown's avatar
unknown committed
4724 4725
      }
    }
4726
    if (!(flags= new_table->file->alter_table_flags(alter_info->flags)))
unknown's avatar
unknown committed
4727 4728
    {
      my_error(ER_PARTITION_FUNCTION_FAILURE, MYF(0));
4729
      goto err;
unknown's avatar
unknown committed
4730
    }
4731 4732 4733 4734
    if ((flags & (HA_FAST_CHANGE_PARTITION | HA_PARTITION_ONE_PHASE)) != 0)
      *fast_alter_table= new_table;
    DBUG_PRINT("info", ("*fast_alter_table: %p  flags: 0x%x",
                        *fast_alter_table, flags));
4735 4736
    if ((alter_info->flags & ALTER_ADD_PARTITION) ||
         (alter_info->flags & ALTER_REORGANIZE_PARTITION))
4737
    {
4738
      if (thd->work_part_info->part_type != tab_part_info->part_type)
4739
      {
4740
        if (thd->work_part_info->part_type == NOT_A_PARTITION)
4741
        {
4742 4743 4744
          if (tab_part_info->part_type == RANGE_PARTITION)
          {
            my_error(ER_PARTITIONS_MUST_BE_DEFINED_ERROR, MYF(0), "RANGE");
4745
            goto err;
4746 4747 4748 4749
          }
          else if (tab_part_info->part_type == LIST_PARTITION)
          {
            my_error(ER_PARTITIONS_MUST_BE_DEFINED_ERROR, MYF(0), "LIST");
4750
            goto err;
4751 4752 4753 4754 4755
          }
          /*
            Hash partitions can be altered without parser finds out about
            that it is HASH partitioned. So no error here.
          */
4756 4757 4758
        }
        else
        {
4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780
          if (thd->work_part_info->part_type == RANGE_PARTITION)
          {
            my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
                     "RANGE", "LESS THAN");
          }
          else if (thd->work_part_info->part_type == LIST_PARTITION)
          {
            DBUG_ASSERT(thd->work_part_info->part_type == LIST_PARTITION);
            my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
                     "LIST", "IN");
          }
          else if (tab_part_info->part_type == RANGE_PARTITION)
          {
            my_error(ER_PARTITION_REQUIRES_VALUES_ERROR, MYF(0),
                     "RANGE", "LESS THAN");
          }
          else
          {
            DBUG_ASSERT(tab_part_info->part_type == LIST_PARTITION);
            my_error(ER_PARTITION_REQUIRES_VALUES_ERROR, MYF(0),
                     "LIST", "IN");
          }
4781
          goto err;
4782
        }
4783
      }
4784 4785 4786 4787 4788 4789 4790 4791 4792
      if ((tab_part_info->column_list &&
          alt_part_info->num_columns != tab_part_info->num_columns) ||
          (!tab_part_info->column_list &&
            (tab_part_info->part_type == RANGE_PARTITION ||
             tab_part_info->part_type == LIST_PARTITION) &&
            alt_part_info->num_columns != 1U) ||
          (!tab_part_info->column_list &&
            tab_part_info->part_type == HASH_PARTITION &&
            alt_part_info->num_columns != 0))
4793
      {
4794
        my_error(ER_PARTITION_COLUMN_LIST_ERROR, MYF(0));
4795
        goto err;
4796
      }
4797 4798
      alt_part_info->column_list= tab_part_info->column_list;
      if (alt_part_info->fix_parser_data(thd))
4799
      {
4800
        goto err;
4801 4802
      }
    }
unknown's avatar
unknown committed
4803 4804 4805 4806 4807 4808 4809 4810
    if (alter_info->flags & ALTER_ADD_PARTITION)
    {
      /*
        We start by moving the new partitions to the list of temporary
        partitions. We will then check that the new partitions fit in the
        partitioning scheme as currently set-up.
        Partitions are always added at the end in ADD PARTITION.
      */
4811 4812 4813
      uint num_new_partitions= alt_part_info->num_parts;
      uint num_orig_partitions= tab_part_info->num_parts;
      uint check_total_partitions= num_new_partitions + num_orig_partitions;
unknown's avatar
unknown committed
4814 4815 4816 4817 4818 4819 4820 4821 4822
      uint new_total_partitions= check_total_partitions;
      /*
        We allow quite a lot of values to be supplied by defaults, however we
        must know the number of new partitions in this case.
      */
      if (thd->lex->no_write_to_binlog &&
          tab_part_info->part_type != HASH_PARTITION)
      {
        my_error(ER_NO_BINLOG_ERROR, MYF(0));
4823
        goto err;
4824 4825 4826 4827
      }
      if (tab_part_info->defined_max_value)
      {
        my_error(ER_PARTITION_MAXVALUE_ERROR, MYF(0));
4828
        goto err;
4829
      }
4830
      if (num_new_partitions == 0)
unknown's avatar
unknown committed
4831 4832
      {
        my_error(ER_ADD_PARTITION_NO_NEW_PARTITION, MYF(0));
4833
        goto err;
unknown's avatar
unknown committed
4834
      }
4835
      if (tab_part_info->is_sub_partitioned())
unknown's avatar
unknown committed
4836
      {
4837 4838 4839
        if (alt_part_info->num_subparts == 0)
          alt_part_info->num_subparts= tab_part_info->num_subparts;
        else if (alt_part_info->num_subparts != tab_part_info->num_subparts)
unknown's avatar
unknown committed
4840 4841
        {
          my_error(ER_ADD_PARTITION_SUBPART_ERROR, MYF(0));
4842
          goto err;
unknown's avatar
unknown committed
4843 4844
        }
        check_total_partitions= new_total_partitions*
4845
                                alt_part_info->num_subparts;
unknown's avatar
unknown committed
4846 4847 4848 4849
      }
      if (check_total_partitions > MAX_PARTITIONS)
      {
        my_error(ER_TOO_MANY_PARTITIONS_ERROR, MYF(0));
4850
        goto err;
unknown's avatar
unknown committed
4851 4852
      }
      alt_part_info->part_type= tab_part_info->part_type;
4853
      alt_part_info->subpart_type= tab_part_info->subpart_type;
4854
      if (alt_part_info->set_up_defaults_for_partitioning(new_table->file,
4855 4856
                                                    ULL(0), 
                                                    tab_part_info->num_parts))
unknown's avatar
unknown committed
4857
      {
4858
        goto err;
unknown's avatar
unknown committed
4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921
      }
/*
Handling of on-line cases:

ADD PARTITION for RANGE/LIST PARTITIONING:
------------------------------------------
For range and list partitions add partition is simply adding a
new empty partition to the table. If the handler support this we
will use the simple method of doing this. The figure below shows
an example of this and the states involved in making this change.
            
Existing partitions                                     New added partitions
------       ------        ------        ------      |  ------    ------
|    |       |    |        |    |        |    |      |  |    |    |    |
| p0 |       | p1 |        | p2 |        | p3 |      |  | p4 |    | p5 |
------       ------        ------        ------      |  ------    ------
PART_NORMAL  PART_NORMAL   PART_NORMAL   PART_NORMAL    PART_TO_BE_ADDED*2
PART_NORMAL  PART_NORMAL   PART_NORMAL   PART_NORMAL    PART_IS_ADDED*2

The first line is the states before adding the new partitions and the 
second line is after the new partitions are added. All the partitions are
in the partitions list, no partitions are placed in the temp_partitions
list.

ADD PARTITION for HASH PARTITIONING
-----------------------------------
This little figure tries to show the various partitions involved when
adding two new partitions to a linear hash based partitioned table with
four partitions to start with, which lists are used and the states they
pass through. Adding partitions to a normal hash based is similar except
that it is always all the existing partitions that are reorganised not
only a subset of them.

Existing partitions                                     New added partitions
------       ------        ------        ------      |  ------    ------
|    |       |    |        |    |        |    |      |  |    |    |    |
| p0 |       | p1 |        | p2 |        | p3 |      |  | p4 |    | p5 |
------       ------        ------        ------      |  ------    ------
PART_CHANGED PART_CHANGED  PART_NORMAL   PART_NORMAL    PART_TO_BE_ADDED
PART_IS_CHANGED*2          PART_NORMAL   PART_NORMAL    PART_IS_ADDED
PART_NORMAL  PART_NORMAL   PART_NORMAL   PART_NORMAL    PART_IS_ADDED

Reorganised existing partitions
------      ------
|    |      |    |
| p0'|      | p1'|
------      ------

p0 - p5 will be in the partitions list of partitions.
p0' and p1' will actually not exist as separate objects, there presence can
be deduced from the state of the partition and also the names of those
partitions can be deduced this way.

After adding the partitions and copying the partition data to p0', p1',
p4 and p5 from p0 and p1 the states change to adapt for the new situation
where p0 and p1 is dropped and replaced by p0' and p1' and the new p4 and
p5 are in the table again.

The first line above shows the states of the partitions before we start
adding and copying partitions, the second after completing the adding
and copying and finally the third line after also dropping the partitions
that are reorganised.
*/
4922
      if (*fast_alter_table &&
unknown's avatar
unknown committed
4923 4924 4925 4926 4927 4928 4929 4930
          tab_part_info->part_type == HASH_PARTITION)
      {
        uint part_no= 0, start_part= 1, start_sec_part= 1;
        uint end_part= 0, end_sec_part= 0;
        uint upper_2n= tab_part_info->linear_hash_mask + 1;
        uint lower_2n= upper_2n >> 1;
        bool all_parts= TRUE;
        if (tab_part_info->linear_hash_ind &&
4931
            num_new_partitions < upper_2n)
unknown's avatar
unknown committed
4932 4933 4934 4935 4936 4937 4938 4939
        {
          /*
            An analysis of which parts needs reorganisation shows that it is
            divided into two intervals. The first interval is those parts
            that are reorganised up until upper_2n - 1. From upper_2n and
            onwards it starts again from partition 0 and goes on until
            it reaches p(upper_2n - 1). If the last new partition reaches
            beyond upper_2n - 1 then the first interval will end with
4940
            p(lower_2n - 1) and start with p(num_orig_partitions - lower_2n).
unknown's avatar
unknown committed
4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967
            If lower_2n partitions are added then p0 to p(lower_2n - 1) will
            be reorganised which means that the two interval becomes one
            interval at this point. Thus only when adding less than
            lower_2n partitions and going beyond a total of upper_2n we
            actually get two intervals.

            To exemplify this assume we have 6 partitions to start with and
            add 1, 2, 3, 5, 6, 7, 8, 9 partitions.
            The first to add after p5 is p6 = 110 in bit numbers. Thus we
            can see that 10 = p2 will be partition to reorganise if only one
            partition.
            If 2 partitions are added we reorganise [p2, p3]. Those two
            cases are covered by the second if part below.
            If 3 partitions are added we reorganise [p2, p3] U [p0,p0]. This
            part is covered by the else part below.
            If 5 partitions are added we get [p2,p3] U [p0, p2] = [p0, p3].
            This is covered by the first if part where we need the max check
            to here use lower_2n - 1.
            If 7 partitions are added we get [p2,p3] U [p0, p4] = [p0, p4].
            This is covered by the first if part but here we use the first
            calculated end_part.
            Finally with 9 new partitions we would also reorganise p6 if we
            used the method below but we cannot reorganise more partitions
            than what we had from the start and thus we simply set all_parts
            to TRUE. In this case we don't get into this if-part at all.
          */
          all_parts= FALSE;
4968
          if (num_new_partitions >= lower_2n)
unknown's avatar
unknown committed
4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983
          {
            /*
              In this case there is only one interval since the two intervals
              overlap and this starts from zero to last_part_no - upper_2n
            */
            start_part= 0;
            end_part= new_total_partitions - (upper_2n + 1);
            end_part= max(lower_2n - 1, end_part);
          }
          else if (new_total_partitions <= upper_2n)
          {
            /*
              Also in this case there is only one interval since we are not
              going over a 2**n boundary
            */
4984 4985
            start_part= num_orig_partitions - lower_2n;
            end_part= start_part + (num_new_partitions - 1);
unknown's avatar
unknown committed
4986 4987 4988 4989 4990 4991 4992 4993
          }
          else
          {
            /* We have two non-overlapping intervals since we are not
               passing a 2**n border and we have not at least lower_2n
               new parts that would ensure that the intervals become
               overlapping.
            */
4994
            start_part= num_orig_partitions - lower_2n;
unknown's avatar
unknown committed
4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010
            end_part= upper_2n - 1;
            start_sec_part= 0;
            end_sec_part= new_total_partitions - (upper_2n + 1);
          }
        }
        List_iterator<partition_element> tab_it(tab_part_info->partitions);
        part_no= 0;
        do
        {
          partition_element *p_elem= tab_it++;
          if (all_parts ||
              (part_no >= start_part && part_no <= end_part) ||
              (part_no >= start_sec_part && part_no <= end_sec_part))
          {
            p_elem->part_state= PART_CHANGED;
          }
5011
        } while (++part_no < num_orig_partitions);
unknown's avatar
unknown committed
5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026
      }
      /*
        Need to concatenate the lists here to make it possible to check the
        partition info for correctness using check_partition_info.
        For on-line add partition we set the state of this partition to
        PART_TO_BE_ADDED to ensure that it is known that it is not yet
        usable (becomes usable when partition is created and the switch of
        partition configuration is made.
      */
      {
        List_iterator<partition_element> alt_it(alt_part_info->partitions);
        uint part_count= 0;
        do
        {
          partition_element *part_elem= alt_it++;
5027
          if (*fast_alter_table)
unknown's avatar
unknown committed
5028 5029 5030 5031
            part_elem->part_state= PART_TO_BE_ADDED;
          if (tab_part_info->partitions.push_back(part_elem))
          {
            mem_alloc_error(1);
5032
            goto err;
unknown's avatar
unknown committed
5033
          }
5034 5035
        } while (++part_count < num_new_partitions);
        tab_part_info->num_parts+= num_new_partitions;
unknown's avatar
unknown committed
5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046
      }
      /*
        If we specify partitions explicitly we don't use defaults anymore.
        Using ADD PARTITION also means that we don't have the default number
        of partitions anymore. We use this code also for Table reorganisations
        and here we don't set any default flags to FALSE.
      */
      if (!(alter_info->flags & ALTER_TABLE_REORG))
      {
        if (!alt_part_info->use_default_partitions)
        {
unknown's avatar
unknown committed
5047
          DBUG_PRINT("info", ("part_info: 0x%lx", (long) tab_part_info));
unknown's avatar
unknown committed
5048 5049
          tab_part_info->use_default_partitions= FALSE;
        }
5050
        tab_part_info->use_default_num_partitions= FALSE;
5051
        tab_part_info->is_auto_partitioned= FALSE;
unknown's avatar
unknown committed
5052 5053
      }
    }
5054
    else if (alter_info->flags & ALTER_DROP_PARTITION)
unknown's avatar
unknown committed
5055 5056 5057 5058 5059 5060 5061 5062 5063
    {
      /*
        Drop a partition from a range partition and list partitioning is
        always safe and can be made more or less immediate. It is necessary
        however to ensure that the partition to be removed is safely removed
        and that REPAIR TABLE can remove the partition if for some reason the
        command to drop the partition failed in the middle.
      */
      uint part_count= 0;
5064 5065
      uint num_parts_dropped= alter_info->partition_names.elements;
      uint num_parts_found= 0;
unknown's avatar
unknown committed
5066
      List_iterator<partition_element> part_it(tab_part_info->partitions);
5067 5068

      tab_part_info->is_auto_partitioned= FALSE;
unknown's avatar
unknown committed
5069 5070 5071 5072
      if (!(tab_part_info->part_type == RANGE_PARTITION ||
            tab_part_info->part_type == LIST_PARTITION))
      {
        my_error(ER_ONLY_ON_RANGE_LIST_PARTITION, MYF(0), "DROP");
5073
        goto err;
unknown's avatar
unknown committed
5074
      }
5075
      if (num_parts_dropped >= tab_part_info->num_parts)
unknown's avatar
unknown committed
5076 5077
      {
        my_error(ER_DROP_LAST_PARTITION, MYF(0));
5078
        goto err;
unknown's avatar
unknown committed
5079 5080 5081 5082 5083 5084 5085 5086 5087 5088
      }
      do
      {
        partition_element *part_elem= part_it++;
        if (is_name_in_list(part_elem->partition_name,
                            alter_info->partition_names))
        {
          /*
            Set state to indicate that the partition is to be dropped.
          */
5089
          num_parts_found++;
unknown's avatar
unknown committed
5090 5091
          part_elem->part_state= PART_TO_BE_DROPPED;
        }
5092 5093
      } while (++part_count < tab_part_info->num_parts);
      if (num_parts_found != num_parts_dropped)
unknown's avatar
unknown committed
5094 5095
      {
        my_error(ER_DROP_PARTITION_NON_EXISTENT, MYF(0), "DROP");
5096
        goto err;
unknown's avatar
unknown committed
5097
      }
5098
      if (new_table->file->is_fk_defined_on_table_or_index(MAX_KEY))
unknown's avatar
unknown committed
5099 5100
      {
        my_error(ER_ROW_IS_REFERENCED, MYF(0));
5101
        goto err;
unknown's avatar
unknown committed
5102
      }
5103
      tab_part_info->num_parts-= num_parts_dropped;
unknown's avatar
unknown committed
5104
    }
5105
    else if (alter_info->flags & ALTER_REBUILD_PARTITION)
unknown's avatar
unknown committed
5106
    {
5107 5108
      set_engine_all_partitions(tab_part_info,
                                tab_part_info->default_engine_type);
5109
      if (set_part_state(alter_info, tab_part_info, PART_CHANGED))
unknown's avatar
unknown committed
5110
      {
5111
        my_error(ER_DROP_PARTITION_NON_EXISTENT, MYF(0), "REBUILD");
5112
        goto err;
unknown's avatar
unknown committed
5113
      }
5114
      if (!(*fast_alter_table))
5115
      {
5116 5117
        new_table->file->print_error(HA_ERR_WRONG_COMMAND, MYF(0));
        goto err;
5118
      }
unknown's avatar
unknown committed
5119 5120 5121
    }
    else if (alter_info->flags & ALTER_COALESCE_PARTITION)
    {
5122 5123
      uint num_parts_coalesced= alter_info->num_parts;
      uint num_parts_remain= tab_part_info->num_parts - num_parts_coalesced;
unknown's avatar
unknown committed
5124 5125 5126 5127
      List_iterator<partition_element> part_it(tab_part_info->partitions);
      if (tab_part_info->part_type != HASH_PARTITION)
      {
        my_error(ER_COALESCE_ONLY_ON_HASH_PARTITION, MYF(0));
5128
        goto err;
unknown's avatar
unknown committed
5129
      }
5130
      if (num_parts_coalesced == 0)
unknown's avatar
unknown committed
5131 5132
      {
        my_error(ER_COALESCE_PARTITION_NO_PARTITION, MYF(0));
5133
        goto err;
unknown's avatar
unknown committed
5134
      }
5135
      if (num_parts_coalesced >= tab_part_info->num_parts)
unknown's avatar
unknown committed
5136 5137
      {
        my_error(ER_DROP_LAST_PARTITION, MYF(0));
5138
        goto err;
unknown's avatar
unknown committed
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 5173 5174 5175 5176
      }
/*
Online handling:
COALESCE PARTITION:
-------------------
The figure below shows the manner in which partitions are handled when
performing an on-line coalesce partition and which states they go through
at start, after adding and copying partitions and finally after dropping
the partitions to drop. The figure shows an example using four partitions
to start with, using linear hash and coalescing one partition (always the
last partition).

Using linear hash then all remaining partitions will have a new reorganised
part.

Existing partitions                     Coalesced partition 
------       ------              ------   |      ------
|    |       |    |              |    |   |      |    |
| p0 |       | p1 |              | p2 |   |      | p3 |
------       ------              ------   |      ------
PART_NORMAL  PART_CHANGED        PART_NORMAL     PART_REORGED_DROPPED
PART_NORMAL  PART_IS_CHANGED     PART_NORMAL     PART_TO_BE_DROPPED
PART_NORMAL  PART_NORMAL         PART_NORMAL     PART_IS_DROPPED

Reorganised existing partitions
            ------
            |    |
            | p1'|
            ------

p0 - p3 is in the partitions list.
The p1' partition will actually not be in any list it is deduced from the
state of p1.
*/
      {
        uint part_count= 0, start_part= 1, start_sec_part= 1;
        uint end_part= 0, end_sec_part= 0;
        bool all_parts= TRUE;
5177
        if (*fast_alter_table &&
unknown's avatar
unknown committed
5178 5179 5180 5181 5182
            tab_part_info->linear_hash_ind)
        {
          uint upper_2n= tab_part_info->linear_hash_mask + 1;
          uint lower_2n= upper_2n >> 1;
          all_parts= FALSE;
5183
          if (num_parts_coalesced >= lower_2n)
unknown's avatar
unknown committed
5184 5185 5186
          {
            all_parts= TRUE;
          }
5187
          else if (num_parts_remain >= lower_2n)
unknown's avatar
unknown committed
5188
          {
5189 5190
            end_part= tab_part_info->num_parts - (lower_2n + 1);
            start_part= num_parts_remain - lower_2n;
unknown's avatar
unknown committed
5191 5192 5193 5194
          }
          else
          {
            start_part= 0;
5195
            end_part= tab_part_info->num_parts - (lower_2n + 1);
unknown's avatar
unknown committed
5196
            end_sec_part= (lower_2n >> 1) - 1;
5197
            start_sec_part= end_sec_part - (lower_2n - (num_parts_remain + 1));
unknown's avatar
unknown committed
5198 5199 5200 5201 5202
          }
        }
        do
        {
          partition_element *p_elem= part_it++;
5203
          if (*fast_alter_table &&
unknown's avatar
unknown committed
5204 5205 5206 5207
              (all_parts ||
              (part_count >= start_part && part_count <= end_part) ||
              (part_count >= start_sec_part && part_count <= end_sec_part)))
            p_elem->part_state= PART_CHANGED;
5208
          if (++part_count > num_parts_remain)
unknown's avatar
unknown committed
5209
          {
5210
            if (*fast_alter_table)
unknown's avatar
unknown committed
5211 5212 5213 5214
              p_elem->part_state= PART_REORGED_DROPPED;
            else
              part_it.remove();
          }
5215 5216
        } while (part_count < tab_part_info->num_parts);
        tab_part_info->num_parts= num_parts_remain;
unknown's avatar
unknown committed
5217 5218
      }
      if (!(alter_info->flags & ALTER_TABLE_REORG))
5219
      {
5220
        tab_part_info->use_default_num_partitions= FALSE;
5221 5222
        tab_part_info->is_auto_partitioned= FALSE;
      }
unknown's avatar
unknown committed
5223
    }
5224
    else if (alter_info->flags & ALTER_REORGANIZE_PARTITION)
unknown's avatar
unknown committed
5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236
    {
      /*
        Reorganise partitions takes a number of partitions that are next
        to each other (at least for RANGE PARTITIONS) and then uses those
        to create a set of new partitions. So data is copied from those
        partitions into the new set of partitions. Those new partitions
        can have more values in the LIST value specifications or less both
        are allowed. The ranges can be different but since they are 
        changing a set of consecutive partitions they must cover the same
        range as those changed from.
        This command can be used on RANGE and LIST partitions.
      */
5237 5238
      uint num_parts_reorged= alter_info->partition_names.elements;
      uint num_parts_new= thd->work_part_info->partitions.elements;
unknown's avatar
unknown committed
5239
      uint check_total_partitions;
5240 5241

      tab_part_info->is_auto_partitioned= FALSE;
5242
      if (num_parts_reorged > tab_part_info->num_parts)
unknown's avatar
unknown committed
5243 5244
      {
        my_error(ER_REORG_PARTITION_NOT_EXIST, MYF(0));
5245
        goto err;
unknown's avatar
unknown committed
5246 5247 5248
      }
      if (!(tab_part_info->part_type == RANGE_PARTITION ||
            tab_part_info->part_type == LIST_PARTITION) &&
5249
           (num_parts_new != num_parts_reorged))
unknown's avatar
unknown committed
5250 5251
      {
        my_error(ER_REORG_HASH_ONLY_ON_SAME_NO, MYF(0));
5252
        goto err;
unknown's avatar
unknown committed
5253
      }
5254
      if (tab_part_info->is_sub_partitioned() &&
5255 5256
          alt_part_info->num_subparts &&
          alt_part_info->num_subparts != tab_part_info->num_subparts)
5257 5258
      {
        my_error(ER_PARTITION_WRONG_NO_SUBPART_ERROR, MYF(0));
5259
        goto err;
5260
      }
5261 5262
      check_total_partitions= tab_part_info->num_parts + num_parts_new;
      check_total_partitions-= num_parts_reorged;
unknown's avatar
unknown committed
5263 5264 5265
      if (check_total_partitions > MAX_PARTITIONS)
      {
        my_error(ER_TOO_MANY_PARTITIONS_ERROR, MYF(0));
5266
        goto err;
unknown's avatar
unknown committed
5267
      }
5268 5269
      alt_part_info->part_type= tab_part_info->part_type;
      alt_part_info->subpart_type= tab_part_info->subpart_type;
5270
      alt_part_info->num_subparts= tab_part_info->num_subparts;
5271
      DBUG_ASSERT(!alt_part_info->use_default_partitions);
5272
      if (alt_part_info->set_up_defaults_for_partitioning(new_table->file,
5273 5274 5275
                                                          ULL(0), 
                                                          0))
      {
5276
        goto err;
5277
      }
unknown's avatar
unknown committed
5278 5279 5280 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 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327
/*
Online handling:
REORGANIZE PARTITION:
---------------------
The figure exemplifies the handling of partitions, their state changes and
how they are organised. It exemplifies four partitions where two of the
partitions are reorganised (p1 and p2) into two new partitions (p4 and p5).
The reason of this change could be to change range limits, change list
values or for hash partitions simply reorganise the partition which could
also involve moving them to new disks or new node groups (MySQL Cluster).

Existing partitions                                  
------       ------        ------        ------
|    |       |    |        |    |        |    |
| p0 |       | p1 |        | p2 |        | p3 |
------       ------        ------        ------
PART_NORMAL  PART_TO_BE_REORGED          PART_NORMAL
PART_NORMAL  PART_TO_BE_DROPPED          PART_NORMAL
PART_NORMAL  PART_IS_DROPPED             PART_NORMAL

Reorganised new partitions (replacing p1 and p2)
------      ------
|    |      |    |
| p4 |      | p5 |
------      ------
PART_TO_BE_ADDED
PART_IS_ADDED
PART_IS_ADDED

All unchanged partitions and the new partitions are in the partitions list
in the order they will have when the change is completed. The reorganised
partitions are placed in the temp_partitions list. PART_IS_ADDED is only a
temporary state not written in the frm file. It is used to ensure we write
the generated partition syntax in a correct manner.
*/
      {
        List_iterator<partition_element> tab_it(tab_part_info->partitions);
        uint part_count= 0;
        bool found_first= FALSE;
        bool found_last= FALSE;
        uint drop_count= 0;
        do
        {
          partition_element *part_elem= tab_it++;
          is_last_partition_reorged= FALSE;
          if (is_name_in_list(part_elem->partition_name,
                              alter_info->partition_names))
          {
            is_last_partition_reorged= TRUE;
            drop_count++;
5328 5329 5330 5331 5332 5333 5334
            if (tab_part_info->column_list)
            {
              List_iterator<part_elem_value> p(part_elem->list_val_list);
              tab_max_elem_val= p++;
            }
            else
              tab_max_range= part_elem->range_value;
5335
            if (*fast_alter_table &&
unknown's avatar
unknown committed
5336 5337 5338
                tab_part_info->temp_partitions.push_back(part_elem))
            {
              mem_alloc_error(1);
5339
              goto err;
unknown's avatar
unknown committed
5340
            }
5341
            if (*fast_alter_table)
unknown's avatar
unknown committed
5342 5343 5344 5345
              part_elem->part_state= PART_TO_BE_REORGED;
            if (!found_first)
            {
              uint alt_part_count= 0;
5346
              partition_element *alt_part_elem;
unknown's avatar
unknown committed
5347 5348
              List_iterator<partition_element>
                                 alt_it(alt_part_info->partitions);
5349
              found_first= TRUE;
unknown's avatar
unknown committed
5350 5351
              do
              {
5352 5353 5354 5355 5356 5357 5358 5359 5360
                alt_part_elem= alt_it++;
                if (tab_part_info->column_list)
                {
                  List_iterator<part_elem_value> p(alt_part_elem->list_val_list);
                  alt_max_elem_val= p++;
                }
                else
                  alt_max_range= alt_part_elem->range_value;

5361
                if (*fast_alter_table)
unknown's avatar
unknown committed
5362 5363 5364 5365 5366
                  alt_part_elem->part_state= PART_TO_BE_ADDED;
                if (alt_part_count == 0)
                  tab_it.replace(alt_part_elem);
                else
                  tab_it.after(alt_part_elem);
5367
              } while (++alt_part_count < num_parts_new);
unknown's avatar
unknown committed
5368 5369 5370 5371
            }
            else if (found_last)
            {
              my_error(ER_CONSECUTIVE_REORG_PARTITIONS, MYF(0));
5372
              goto err;
unknown's avatar
unknown committed
5373 5374 5375 5376 5377 5378 5379 5380 5381
            }
            else
              tab_it.remove();
          }
          else
          {
            if (found_first)
              found_last= TRUE;
          }
5382 5383
        } while (++part_count < tab_part_info->num_parts);
        if (drop_count != num_parts_reorged)
unknown's avatar
unknown committed
5384 5385
        {
          my_error(ER_DROP_PARTITION_NON_EXISTENT, MYF(0), "REORGANIZE");
5386
          goto err;
unknown's avatar
unknown committed
5387
        }
5388
        tab_part_info->num_parts= check_total_partitions;
unknown's avatar
unknown committed
5389 5390 5391 5392 5393 5394 5395
      }
    }
    else
    {
      DBUG_ASSERT(FALSE);
    }
    *partition_changed= TRUE;
unknown's avatar
unknown committed
5396
    thd->work_part_info= tab_part_info;
5397 5398
    if (alter_info->flags & ALTER_ADD_PARTITION ||
        alter_info->flags & ALTER_REORGANIZE_PARTITION)
unknown's avatar
unknown committed
5399
    {
5400
      if (tab_part_info->use_default_subpartitions &&
5401 5402 5403
          !alt_part_info->use_default_subpartitions)
      {
        tab_part_info->use_default_subpartitions= FALSE;
5404
        tab_part_info->use_default_num_subpartitions= FALSE;
5405
      }
5406
      if (tab_part_info->check_partition_info(thd, (handlerton**)NULL,
5407
                                              new_table->file, ULL(0), TRUE))
unknown's avatar
unknown committed
5408
      {
5409
        goto err;
unknown's avatar
unknown committed
5410
      }
5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429
      /*
        The check below needs to be performed after check_partition_info
        since this function "fixes" the item trees of the new partitions
        to reorganize into
      */
      if (alter_info->flags == ALTER_REORGANIZE_PARTITION &&
          tab_part_info->part_type == RANGE_PARTITION &&
          ((is_last_partition_reorged &&
            (tab_part_info->column_list ?
             (tab_part_info->compare_column_values(
                              alt_max_elem_val->col_val_array,
                              tab_max_elem_val->col_val_array) < 0) :
             alt_max_range < tab_max_range)) ||
            (!is_last_partition_reorged &&
             (tab_part_info->column_list ?
              (tab_part_info->compare_column_values(
                              alt_max_elem_val->col_val_array,
                              tab_max_elem_val->col_val_array) != 0) :
              alt_max_range != tab_max_range))))
unknown's avatar
unknown committed
5430
      {
5431 5432 5433 5434 5435 5436 5437 5438 5439 5440
        /*
          For range partitioning the total resulting range before and
          after the change must be the same except in one case. This is
          when the last partition is reorganised, in this case it is
          acceptable to increase the total range.
          The reason is that it is not allowed to have "holes" in the
          middle of the ranges and thus we should not allow to reorganise
          to create "holes".
        */
        my_error(ER_REORG_OUTSIDE_RANGE, MYF(0));
5441
        goto err;
unknown's avatar
unknown committed
5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460
      }
    }
  }
  else
  {
    /*
     When thd->lex->part_info has a reference to a partition_info the
     ALTER TABLE contained a definition of a partitioning.

     Case I:
       If there was a partition before and there is a new one defined.
       We use the new partitioning. The new partitioning is already
       defined in the correct variable so no work is needed to
       accomplish this.
       We do however need to update partition_changed to ensure that not
       only the frm file is changed in the ALTER TABLE command.

     Case IIa:
       There was a partitioning before and there is no new one defined.
unknown's avatar
unknown committed
5461
       Also the user has not specified to remove partitioning explicitly.
unknown's avatar
unknown committed
5462 5463 5464

       We use the old partitioning also for the new table. We do this
       by assigning the partition_info from the table loaded in
5465
       open_table to the partition_info struct used by mysql_create_table
unknown's avatar
unknown committed
5466 5467 5468 5469
       later in this method.

     Case IIb:
       There was a partitioning before and there is no new one defined.
unknown's avatar
unknown committed
5470
       The user has specified explicitly to remove partitioning
unknown's avatar
unknown committed
5471

unknown's avatar
unknown committed
5472 5473 5474
       Since the user has specified explicitly to remove partitioning
       we override the old partitioning info and create a new table using
       the specified engine.
unknown's avatar
unknown committed
5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494
       In this case the partition also is changed.

     Case III:
       There was no partitioning before altering the table, there is
       partitioning defined in the altered table. Use the new partitioning.
       No work needed since the partitioning info is already in the
       correct variable.

       In this case we discover one case where the new partitioning is using
       the same partition function as the default (PARTITION BY KEY or
       PARTITION BY LINEAR KEY with the list of fields equal to the primary
       key fields OR PARTITION BY [LINEAR] KEY() for tables without primary
       key)
       Also here partition has changed and thus a new table must be
       created.

     Case IV:
       There was no partitioning before and no partitioning defined.
       Obviously no work needed.
    */
5495 5496 5497
    partition_info *tab_part_info= table->part_info;

    if (tab_part_info)
unknown's avatar
unknown committed
5498
    {
unknown's avatar
unknown committed
5499
      if (alter_info->flags & ALTER_REMOVE_PARTITIONING)
unknown's avatar
unknown committed
5500 5501
      {
        DBUG_PRINT("info", ("Remove partitioning"));
unknown's avatar
unknown committed
5502
        if (!(create_info->used_fields & HA_CREATE_USED_ENGINE))
unknown's avatar
unknown committed
5503 5504
        {
          DBUG_PRINT("info", ("No explicit engine used"));
5505
          create_info->db_type= tab_part_info->default_engine_type;
unknown's avatar
unknown committed
5506
        }
unknown's avatar
unknown committed
5507
        DBUG_PRINT("info", ("New engine type: %s",
unknown's avatar
WL#2936  
unknown committed
5508
                   ha_resolve_storage_engine_name(create_info->db_type)));
5509
        thd->work_part_info= NULL;
unknown's avatar
unknown committed
5510 5511
        *partition_changed= TRUE;
      }
5512
      else if (!thd->work_part_info)
unknown's avatar
unknown committed
5513 5514 5515 5516
      {
        /*
          Retain partitioning but possibly with a new storage engine
          beneath.
5517 5518

          Create a copy of TABLE::part_info to be able to modify it freely.
unknown's avatar
unknown committed
5519
        */
5520 5521 5522
        if (!(tab_part_info= tab_part_info->get_clone()))
          DBUG_RETURN(TRUE);
        thd->work_part_info= tab_part_info;
unknown's avatar
unknown committed
5523
        if (create_info->used_fields & HA_CREATE_USED_ENGINE &&
5524
            create_info->db_type != tab_part_info->default_engine_type)
unknown's avatar
unknown committed
5525 5526 5527 5528
        {
          /*
            Make sure change of engine happens to all partitions.
          */
5529
          DBUG_PRINT("info", ("partition changed"));
5530
          if (tab_part_info->is_auto_partitioned)
5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545
          {
            /*
              If the user originally didn't specify partitioning to be
              used we can remove it now.
            */
            thd->work_part_info= NULL;
          }
          else
          {
            /*
              Ensure that all partitions have the proper engine set-up
            */
            set_engine_all_partitions(thd->work_part_info,
                                      create_info->db_type);
          }
unknown's avatar
unknown committed
5546 5547 5548
          *partition_changed= TRUE;
        }
      }
unknown's avatar
unknown committed
5549
    }
unknown's avatar
unknown committed
5550
    if (thd->work_part_info)
unknown's avatar
unknown committed
5551
    {
unknown's avatar
unknown committed
5552
      partition_info *part_info= thd->work_part_info;
unknown's avatar
unknown committed
5553
      bool is_native_partitioned= FALSE;
unknown's avatar
unknown committed
5554 5555 5556 5557
      /*
        Need to cater for engine types that can handle partition without
        using the partition handler.
      */
5558
      if (part_info != tab_part_info)
5559
      {
5560
        if (part_info->fix_parser_data(thd))
5561
        {
5562
          goto err;
5563
        }
5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576
        /*
          Compare the old and new part_info. If only key_algorithm
          change is done, don't consider it as changed partitioning (to avoid
          rebuild). This is to handle KEY (numeric_cols) partitioned tables
          created in 5.1. For more info, see bug#14521864.
        */
        if (alter_info->flags != ALTER_PARTITION ||
            !table->part_info ||
            !table->part_info->has_same_partitioning(part_info))
        {
          DBUG_PRINT("info", ("partition changed"));
          *partition_changed= true;
        }
5577
      }
5578 5579 5580 5581 5582 5583 5584
      /*
        Set up partition default_engine_type either from the create_info
        or from the previus table
      */
      if (create_info->used_fields & HA_CREATE_USED_ENGINE)
        part_info->default_engine_type= create_info->db_type;
      else
5585
      {
5586 5587
        if (tab_part_info)
          part_info->default_engine_type= tab_part_info->default_engine_type;
5588 5589
        else
          part_info->default_engine_type= create_info->db_type;
5590
      }
5591 5592
      DBUG_ASSERT(part_info->default_engine_type &&
                  part_info->default_engine_type != partition_hton);
unknown's avatar
unknown committed
5593 5594
      if (check_native_partitioned(create_info, &is_native_partitioned,
                                   part_info, thd))
unknown's avatar
unknown committed
5595
      {
5596
        goto err;
unknown's avatar
unknown committed
5597
      }
unknown's avatar
unknown committed
5598
      if (!is_native_partitioned)
unknown's avatar
unknown committed
5599
      {
unknown's avatar
unknown committed
5600
        DBUG_ASSERT(create_info->db_type);
5601
        create_info->db_type= partition_hton;
unknown's avatar
unknown committed
5602 5603 5604 5605
      }
    }
  }
  DBUG_RETURN(FALSE);
5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616
err:
  if (new_table)
  {
    /*
      Only remove the intermediate table object and its share object,
      do not remove the .frm file, since it is the original one.
    */
    close_temporary(new_table, 1, 0);
  }
  *fast_alter_table= NULL;
  DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648
}


/*
  Change partitions, used to implement ALTER TABLE ADD/REORGANIZE/COALESCE
  partitions. This method is used to implement both single-phase and multi-
  phase implementations of ADD/REORGANIZE/COALESCE partitions.

  SYNOPSIS
    mysql_change_partitions()
    lpt                        Struct containing parameters

  RETURN VALUES
    TRUE                          Failure
    FALSE                         Success

  DESCRIPTION
    Request handler to add partitions as set in states of the partition

    Elements of the lpt parameters used:
    create_info                Create information used to create partitions
    db                         Database name
    table_name                 Table name
    copied                     Output parameter where number of copied
                               records are added
    deleted                    Output parameter where number of deleted
                               records are added
*/

static bool mysql_change_partitions(ALTER_PARTITION_PARAM_TYPE *lpt)
{
  char path[FN_REFLEN+1];
5649 5650
  int error;
  handler *file= lpt->table->file;
5651
  THD *thd= lpt->thd;
unknown's avatar
unknown committed
5652 5653
  DBUG_ENTER("mysql_change_partitions");

5654
  build_table_filename(path, sizeof(path) - 1, lpt->db, lpt->table_name, "", 0);
5655

5656 5657
  /* First lock the original tables */
  if (file->ha_external_lock(thd, F_WRLCK))
5658 5659
    DBUG_RETURN(TRUE);

5660 5661
  /* Disable transactions for all new tables */
  if (mysql_trans_prepare_alter_copy_data(thd))
5662 5663 5664 5665
    DBUG_RETURN(TRUE);

  /* TODO: test if bulk_insert would increase the performance */

5666 5667 5668
  if ((error= file->ha_change_partitions(lpt->create_info, path, &lpt->copied,
                                         &lpt->deleted, lpt->pack_frm_data,
                                         lpt->pack_frm_len)))
5669
  {
5670
    file->print_error(error, MYF(error != ER_OUTOFMEMORY ? 0 : ME_FATALERROR));
5671
  }
5672 5673

  if (mysql_trans_commit_alter_copy_data(thd))
5674 5675 5676 5677
    error= 1;                                /* The error has been reported */

  if (file->ha_external_lock(thd, F_UNLCK))
    error= 1;
5678 5679

  DBUG_RETURN(test(error));
unknown's avatar
unknown committed
5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704
}


/*
  Rename partitions in an ALTER TABLE of partitions

  SYNOPSIS
    mysql_rename_partitions()
    lpt                        Struct containing parameters

  RETURN VALUES
    TRUE                          Failure
    FALSE                         Success

  DESCRIPTION
    Request handler to rename partitions as set in states of the partition

    Parameters used:
    db                         Database name
    table_name                 Table name
*/

static bool mysql_rename_partitions(ALTER_PARTITION_PARAM_TYPE *lpt)
{
  char path[FN_REFLEN+1];
5705
  int error;
unknown's avatar
unknown committed
5706 5707
  DBUG_ENTER("mysql_rename_partitions");

5708
  build_table_filename(path, sizeof(path) - 1, lpt->db, lpt->table_name, "", 0);
5709
  if ((error= lpt->table->file->ha_rename_partitions(path)))
5710 5711 5712 5713 5714 5715
  {
    if (error != 1)
      lpt->table->file->print_error(error, MYF(0));
    DBUG_RETURN(TRUE);
  }
  DBUG_RETURN(FALSE);
unknown's avatar
unknown committed
5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745
}


/*
  Drop partitions in an ALTER TABLE of partitions

  SYNOPSIS
    mysql_drop_partitions()
    lpt                        Struct containing parameters

  RETURN VALUES
    TRUE                          Failure
    FALSE                         Success
  DESCRIPTION
    Drop the partitions marked with PART_TO_BE_DROPPED state and remove
    those partitions from the list.

    Parameters used:
    table                       Table object
    db                          Database name
    table_name                  Table name
*/

static bool mysql_drop_partitions(ALTER_PARTITION_PARAM_TYPE *lpt)
{
  char path[FN_REFLEN+1];
  partition_info *part_info= lpt->table->part_info;
  List_iterator<partition_element> part_it(part_info->partitions);
  uint i= 0;
  uint remove_count= 0;
5746
  int error;
unknown's avatar
unknown committed
5747 5748
  DBUG_ENTER("mysql_drop_partitions");

5749
  build_table_filename(path, sizeof(path) - 1, lpt->db, lpt->table_name, "", 0);
5750
  if ((error= lpt->table->file->ha_drop_partitions(path)))
unknown's avatar
unknown committed
5751
  {
5752
    lpt->table->file->print_error(error, MYF(0));
unknown's avatar
unknown committed
5753 5754 5755 5756 5757 5758 5759 5760 5761 5762
    DBUG_RETURN(TRUE);
  }
  do
  {
    partition_element *part_elem= part_it++;
    if (part_elem->part_state == PART_IS_DROPPED)
    {
      part_it.remove();
      remove_count++;
    }
5763 5764
  } while (++i < part_info->num_parts);
  part_info->num_parts-= remove_count;
unknown's avatar
unknown committed
5765 5766 5767 5768
  DBUG_RETURN(FALSE);
}


5769 5770 5771 5772 5773 5774 5775 5776 5777
/*
  Insert log entry into list
  SYNOPSIS
    insert_part_info_log_entry_list()
    log_entry
  RETURN VALUES
    NONE
*/

5778 5779
static void insert_part_info_log_entry_list(partition_info *part_info,
                                            DDL_LOG_MEMORY_ENTRY *log_entry)
5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794
{
  log_entry->next_active_log_entry= part_info->first_log_entry;
  part_info->first_log_entry= log_entry;
}


/*
  Release all log entries for this partition info struct
  SYNOPSIS
    release_part_info_log_entries()
    first_log_entry                 First log entry in list to release
  RETURN VALUES
    NONE
*/

5795
static void release_part_info_log_entries(DDL_LOG_MEMORY_ENTRY *log_entry)
5796 5797 5798 5799 5800
{
  DBUG_ENTER("release_part_info_log_entries");

  while (log_entry)
  {
5801
    release_ddl_log_memory_entry(log_entry);
5802
    log_entry= log_entry->next_active_log_entry;
5803 5804 5805 5806 5807
  }
  DBUG_VOID_RETURN;
}


unknown's avatar
unknown committed
5808
/*
5809
  Log an delete/rename frm file
unknown's avatar
unknown committed
5810
  SYNOPSIS
5811
    write_log_replace_delete_frm()
5812 5813
    lpt                            Struct for parameters
    next_entry                     Next reference to use in log record
5814 5815 5816
    from_path                      Name to rename from
    to_path                        Name to rename to
    replace_flag                   TRUE if replace, else delete
unknown's avatar
unknown committed
5817
  RETURN VALUES
5818 5819
    TRUE                           Error
    FALSE                          Success
unknown's avatar
unknown committed
5820
  DESCRIPTION
5821
    Support routine that writes a replace or delete of an frm file into the
5822
    ddl log. It also inserts an entry that keeps track of used space into
5823
    the partition info object
unknown's avatar
unknown committed
5824 5825
*/

5826 5827 5828 5829 5830
static bool write_log_replace_delete_frm(ALTER_PARTITION_PARAM_TYPE *lpt,
                                         uint next_entry,
                                         const char *from_path,
                                         const char *to_path,
                                         bool replace_flag)
unknown's avatar
unknown committed
5831
{
5832 5833
  DDL_LOG_ENTRY ddl_log_entry;
  DDL_LOG_MEMORY_ENTRY *log_entry;
5834
  DBUG_ENTER("write_log_replace_delete_frm");
unknown's avatar
unknown committed
5835

5836
  if (replace_flag)
5837
    ddl_log_entry.action_type= DDL_LOG_REPLACE_ACTION;
5838
  else
5839 5840
    ddl_log_entry.action_type= DDL_LOG_DELETE_ACTION;
  ddl_log_entry.next_entry= next_entry;
5841
  ddl_log_entry.handler_name= reg_ext;
5842
  ddl_log_entry.name= to_path;
5843
  if (replace_flag)
5844 5845
    ddl_log_entry.from_name= from_path;
  if (write_ddl_log_entry(&ddl_log_entry, &log_entry))
5846 5847 5848
  {
    DBUG_RETURN(TRUE);
  }
5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861
  insert_part_info_log_entry_list(lpt->part_info, log_entry);
  DBUG_RETURN(FALSE);
}


/*
  Log final partition changes in change partition
  SYNOPSIS
    write_log_changed_partitions()
    lpt                      Struct containing parameters
  RETURN VALUES
    TRUE                     Error
    FALSE                    Success
5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873
  DESCRIPTION
    This code is used to perform safe ADD PARTITION for HASH partitions
    and COALESCE for HASH partitions and REORGANIZE for any type of
    partitions.
    We prepare entries for all partitions except the reorganised partitions
    in REORGANIZE partition, those are handled by
    write_log_dropped_partitions. For those partitions that are replaced
    special care is needed to ensure that this is performed correctly and
    this requires a two-phased approach with this log as a helper for this.

    This code is closely intertwined with the code in rename_partitions in
    the partition handler.
5874 5875
*/

5876 5877
static bool write_log_changed_partitions(ALTER_PARTITION_PARAM_TYPE *lpt,
                                         uint *next_entry, const char *path)
5878
{
5879
  DDL_LOG_ENTRY ddl_log_entry;
5880
  partition_info *part_info= lpt->part_info;
5881
  DDL_LOG_MEMORY_ENTRY *log_entry;
5882 5883
  char tmp_path[FN_REFLEN];
  char normal_path[FN_REFLEN];
5884 5885
  List_iterator<partition_element> part_it(part_info->partitions);
  uint temp_partitions= part_info->temp_partitions.elements;
5886
  uint num_elements= part_info->partitions.elements;
5887
  uint i= 0;
5888
  DBUG_ENTER("write_log_changed_partitions");
5889 5890 5891 5892 5893 5894 5895

  do
  {
    partition_element *part_elem= part_it++;
    if (part_elem->part_state == PART_IS_CHANGED ||
        (part_elem->part_state == PART_IS_ADDED && temp_partitions))
    {
unknown's avatar
unknown committed
5896
      if (part_info->is_sub_partitioned())
5897 5898
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
5899
        uint num_subparts= part_info->num_subparts;
5900 5901 5902 5903
        uint j= 0;
        do
        {
          partition_element *sub_elem= sub_it++;
5904 5905
          ddl_log_entry.next_entry= *next_entry;
          ddl_log_entry.handler_name=
5906 5907 5908 5909 5910 5911 5912 5913 5914
               ha_resolve_storage_engine_name(sub_elem->engine_type);
          create_subpartition_name(tmp_path, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name,
                                   TEMP_PART_NAME);
          create_subpartition_name(normal_path, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name,
                                   NORMAL_PART_NAME);
5915 5916
          ddl_log_entry.name= normal_path;
          ddl_log_entry.from_name= tmp_path;
5917
          if (part_elem->part_state == PART_IS_CHANGED)
5918
            ddl_log_entry.action_type= DDL_LOG_REPLACE_ACTION;
5919
          else
5920 5921
            ddl_log_entry.action_type= DDL_LOG_RENAME_ACTION;
          if (write_ddl_log_entry(&ddl_log_entry, &log_entry))
5922 5923 5924 5925 5926 5927
          {
            DBUG_RETURN(TRUE);
          }
          *next_entry= log_entry->entry_pos;
          sub_elem->log_entry= log_entry;
          insert_part_info_log_entry_list(part_info, log_entry);
5928
        } while (++j < num_subparts);
5929 5930 5931
      }
      else
      {
5932 5933
        ddl_log_entry.next_entry= *next_entry;
        ddl_log_entry.handler_name=
5934 5935 5936 5937 5938 5939 5940
               ha_resolve_storage_engine_name(part_elem->engine_type);
        create_partition_name(tmp_path, path,
                              part_elem->partition_name,
                              TEMP_PART_NAME, TRUE);
        create_partition_name(normal_path, path,
                              part_elem->partition_name,
                              NORMAL_PART_NAME, TRUE);
5941 5942
        ddl_log_entry.name= normal_path;
        ddl_log_entry.from_name= tmp_path;
5943
        if (part_elem->part_state == PART_IS_CHANGED)
5944
          ddl_log_entry.action_type= DDL_LOG_REPLACE_ACTION;
5945
        else
5946 5947
          ddl_log_entry.action_type= DDL_LOG_RENAME_ACTION;
        if (write_ddl_log_entry(&ddl_log_entry, &log_entry))
5948 5949 5950 5951
        {
          DBUG_RETURN(TRUE);
        }
        *next_entry= log_entry->entry_pos;
unknown's avatar
unknown committed
5952
        part_elem->log_entry= log_entry;
5953 5954 5955
        insert_part_info_log_entry_list(part_info, log_entry);
      }
    }
5956
  } while (++i < num_elements);
5957
  DBUG_RETURN(FALSE);
5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970
}


/*
  Log dropped partitions
  SYNOPSIS
    write_log_dropped_partitions()
    lpt                      Struct containing parameters
  RETURN VALUES
    TRUE                     Error
    FALSE                    Success
*/

5971 5972 5973 5974
static bool write_log_dropped_partitions(ALTER_PARTITION_PARAM_TYPE *lpt,
                                         uint *next_entry,
                                         const char *path,
                                         bool temp_list)
5975
{
5976
  DDL_LOG_ENTRY ddl_log_entry;
5977
  partition_info *part_info= lpt->part_info;
5978
  DDL_LOG_MEMORY_ENTRY *log_entry;
5979 5980
  char tmp_path[FN_LEN];
  List_iterator<partition_element> part_it(part_info->partitions);
5981
  List_iterator<partition_element> temp_it(part_info->temp_partitions);
5982 5983
  uint num_temp_partitions= part_info->temp_partitions.elements;
  uint num_elements= part_info->partitions.elements;
5984 5985
  DBUG_ENTER("write_log_dropped_partitions");

5986
  ddl_log_entry.action_type= DDL_LOG_DELETE_ACTION;
5987
  if (temp_list)
5988 5989
    num_elements= num_temp_partitions;
  while (num_elements--)
5990
  {
5991 5992 5993 5994 5995
    partition_element *part_elem;
    if (temp_list)
      part_elem= temp_it++;
    else
      part_elem= part_it++;
5996
    if (part_elem->part_state == PART_TO_BE_DROPPED ||
5997 5998
        part_elem->part_state == PART_TO_BE_ADDED ||
        part_elem->part_state == PART_CHANGED)
5999
    {
6000 6001 6002
      uint name_variant;
      if (part_elem->part_state == PART_CHANGED ||
          (part_elem->part_state == PART_TO_BE_ADDED &&
6003
           num_temp_partitions))
6004 6005 6006
        name_variant= TEMP_PART_NAME;
      else
        name_variant= NORMAL_PART_NAME;
unknown's avatar
unknown committed
6007
      if (part_info->is_sub_partitioned())
6008 6009
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
6010
        uint num_subparts= part_info->num_subparts;
6011
        uint j= 0;
6012 6013 6014
        do
        {
          partition_element *sub_elem= sub_it++;
6015 6016
          ddl_log_entry.next_entry= *next_entry;
          ddl_log_entry.handler_name=
6017
               ha_resolve_storage_engine_name(sub_elem->engine_type);
6018 6019 6020
          create_subpartition_name(tmp_path, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name,
6021
                                   name_variant);
6022 6023
          ddl_log_entry.name= tmp_path;
          if (write_ddl_log_entry(&ddl_log_entry, &log_entry))
6024 6025 6026 6027
          {
            DBUG_RETURN(TRUE);
          }
          *next_entry= log_entry->entry_pos;
6028
          sub_elem->log_entry= log_entry;
6029
          insert_part_info_log_entry_list(part_info, log_entry);
6030
        } while (++j < num_subparts);
6031 6032 6033
      }
      else
      {
6034 6035
        ddl_log_entry.next_entry= *next_entry;
        ddl_log_entry.handler_name=
6036 6037 6038
               ha_resolve_storage_engine_name(part_elem->engine_type);
        create_partition_name(tmp_path, path,
                              part_elem->partition_name,
6039
                              name_variant, TRUE);
6040 6041
        ddl_log_entry.name= tmp_path;
        if (write_ddl_log_entry(&ddl_log_entry, &log_entry))
6042 6043 6044 6045
        {
          DBUG_RETURN(TRUE);
        }
        *next_entry= log_entry->entry_pos;
6046
        part_elem->log_entry= log_entry;
6047 6048 6049
        insert_part_info_log_entry_list(part_info, log_entry);
      }
    }
6050
  }
unknown's avatar
unknown committed
6051 6052 6053 6054
  DBUG_RETURN(FALSE);
}


6055
/*
6056
  Set execute log entry in ddl log for this partitioned table
6057 6058 6059 6060 6061 6062 6063 6064
  SYNOPSIS
    set_part_info_exec_log_entry()
    part_info                      Partition info object
    exec_log_entry                 Log entry
  RETURN VALUES
    NONE
*/

6065 6066
static void set_part_info_exec_log_entry(partition_info *part_info,
                                         DDL_LOG_MEMORY_ENTRY *exec_log_entry)
6067 6068 6069 6070 6071 6072
{
  part_info->exec_log_entry= exec_log_entry;
  exec_log_entry->next_active_log_entry= NULL;
}


unknown's avatar
unknown committed
6073
/*
6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084
  Write the log entry to ensure that the shadow frm file is removed at
  crash.
  SYNOPSIS
    write_log_drop_shadow_frm()
    lpt                      Struct containing parameters
    install_frm              Should we log action to install shadow frm or should
                             the action be to remove the shadow frm file.
  RETURN VALUES
    TRUE                     Error
    FALSE                    Success
  DESCRIPTION
6085
    Prepare an entry to the ddl log indicating a drop/install of the shadow frm
6086 6087 6088
    file and its corresponding handler file.
*/

6089
static bool write_log_drop_shadow_frm(ALTER_PARTITION_PARAM_TYPE *lpt)
6090 6091
{
  partition_info *part_info= lpt->part_info;
6092 6093
  DDL_LOG_MEMORY_ENTRY *log_entry;
  DDL_LOG_MEMORY_ENTRY *exec_log_entry= NULL;
6094
  char shadow_path[FN_REFLEN + 1];
6095
  DBUG_ENTER("write_log_drop_shadow_frm");
unknown's avatar
unknown committed
6096

6097
  build_table_shadow_filename(shadow_path, sizeof(shadow_path) - 1, lpt);
Marc Alff's avatar
Marc Alff committed
6098
  mysql_mutex_lock(&LOCK_gdl);
6099 6100 6101 6102 6103 6104 6105
  if (write_log_replace_delete_frm(lpt, 0UL, NULL,
                                  (const char*)shadow_path, FALSE))
    goto error;
  log_entry= part_info->first_log_entry;
  if (write_execute_ddl_log_entry(log_entry->entry_pos,
                                    FALSE, &exec_log_entry))
    goto error;
Marc Alff's avatar
Marc Alff committed
6106
  mysql_mutex_unlock(&LOCK_gdl);
6107 6108 6109 6110
  set_part_info_exec_log_entry(part_info, exec_log_entry);
  DBUG_RETURN(FALSE);

error:
6111
  release_part_info_log_entries(part_info->first_log_entry);
Marc Alff's avatar
Marc Alff committed
6112
  mysql_mutex_unlock(&LOCK_gdl);
6113
  part_info->first_log_entry= NULL;
6114
  my_error(ER_DDL_LOG_ERROR, MYF(0));
6115 6116 6117 6118 6119 6120
  DBUG_RETURN(TRUE);
}


/*
  Log renaming of shadow frm to real frm name and dropping of old frm
unknown's avatar
unknown committed
6121
  SYNOPSIS
6122
    write_log_rename_frm()
unknown's avatar
unknown committed
6123 6124 6125 6126 6127
    lpt                      Struct containing parameters
  RETURN VALUES
    TRUE                     Error
    FALSE                    Success
  DESCRIPTION
6128 6129
    Prepare an entry to ensure that we complete the renaming of the frm
    file if failure occurs in the middle of the rename process.
unknown's avatar
unknown committed
6130 6131
*/

6132
static bool write_log_rename_frm(ALTER_PARTITION_PARAM_TYPE *lpt)
unknown's avatar
unknown committed
6133
{
6134
  partition_info *part_info= lpt->part_info;
6135 6136
  DDL_LOG_MEMORY_ENTRY *log_entry;
  DDL_LOG_MEMORY_ENTRY *exec_log_entry= part_info->exec_log_entry;
6137 6138
  char path[FN_REFLEN + 1];
  char shadow_path[FN_REFLEN + 1];
6139
  DDL_LOG_MEMORY_ENTRY *old_first_log_entry= part_info->first_log_entry;
6140
  DBUG_ENTER("write_log_rename_frm");
unknown's avatar
unknown committed
6141

6142
  part_info->first_log_entry= NULL;
6143
  build_table_filename(path, sizeof(path) - 1, lpt->db,
6144
                       lpt->table_name, "", 0);
6145
  build_table_shadow_filename(shadow_path, sizeof(shadow_path) - 1, lpt);
Marc Alff's avatar
Marc Alff committed
6146
  mysql_mutex_lock(&LOCK_gdl);
6147
  if (write_log_replace_delete_frm(lpt, 0UL, shadow_path, path, TRUE))
6148 6149 6150 6151 6152 6153 6154
    goto error;
  log_entry= part_info->first_log_entry;
  part_info->frm_log_entry= log_entry;
  if (write_execute_ddl_log_entry(log_entry->entry_pos,
                                    FALSE, &exec_log_entry))
    goto error;
  release_part_info_log_entries(old_first_log_entry);
Marc Alff's avatar
Marc Alff committed
6155
  mysql_mutex_unlock(&LOCK_gdl);
6156 6157 6158
  DBUG_RETURN(FALSE);

error:
6159
  release_part_info_log_entries(part_info->first_log_entry);
Marc Alff's avatar
Marc Alff committed
6160
  mysql_mutex_unlock(&LOCK_gdl);
6161
  part_info->first_log_entry= old_first_log_entry;
6162
  part_info->frm_log_entry= NULL;
6163
  my_error(ER_DDL_LOG_ERROR, MYF(0));
6164
  DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
6165 6166 6167 6168
}


/*
6169 6170
  Write the log entries to ensure that the drop partition command is completed
  even in the presence of a crash.
unknown's avatar
unknown committed
6171 6172

  SYNOPSIS
6173
    write_log_drop_partition()
unknown's avatar
unknown committed
6174 6175 6176 6177 6178
    lpt                      Struct containing parameters
  RETURN VALUES
    TRUE                     Error
    FALSE                    Success
  DESCRIPTION
6179
    Prepare entries to the ddl log indicating all partitions to drop and to
6180
    install the shadow frm file and remove the old frm file.
unknown's avatar
unknown committed
6181 6182
*/

6183
static bool write_log_drop_partition(ALTER_PARTITION_PARAM_TYPE *lpt)
unknown's avatar
unknown committed
6184
{
6185
  partition_info *part_info= lpt->part_info;
6186 6187
  DDL_LOG_MEMORY_ENTRY *log_entry;
  DDL_LOG_MEMORY_ENTRY *exec_log_entry= part_info->exec_log_entry;
6188 6189
  char tmp_path[FN_REFLEN + 1];
  char path[FN_REFLEN + 1];
6190
  uint next_entry= 0;
6191
  DDL_LOG_MEMORY_ENTRY *old_first_log_entry= part_info->first_log_entry;
6192
  DBUG_ENTER("write_log_drop_partition");
unknown's avatar
unknown committed
6193

6194
  part_info->first_log_entry= NULL;
6195
  build_table_filename(path, sizeof(path) - 1, lpt->db,
6196
                       lpt->table_name, "", 0);
6197
  build_table_shadow_filename(tmp_path, sizeof(tmp_path) - 1, lpt);
Marc Alff's avatar
Marc Alff committed
6198
  mysql_mutex_lock(&LOCK_gdl);
6199 6200 6201
  if (write_log_dropped_partitions(lpt, &next_entry, (const char*)path,
                                   FALSE))
    goto error;
6202 6203
  if (write_log_replace_delete_frm(lpt, next_entry, (const char*)tmp_path,
                                  (const char*)path, TRUE))
6204 6205 6206 6207 6208 6209 6210
    goto error;
  log_entry= part_info->first_log_entry;
  part_info->frm_log_entry= log_entry;
  if (write_execute_ddl_log_entry(log_entry->entry_pos,
                                    FALSE, &exec_log_entry))
    goto error;
  release_part_info_log_entries(old_first_log_entry);
Marc Alff's avatar
Marc Alff committed
6211
  mysql_mutex_unlock(&LOCK_gdl);
6212 6213 6214
  DBUG_RETURN(FALSE);

error:
6215
  release_part_info_log_entries(part_info->first_log_entry);
Marc Alff's avatar
Marc Alff committed
6216
  mysql_mutex_unlock(&LOCK_gdl);
6217
  part_info->first_log_entry= old_first_log_entry;
6218
  part_info->frm_log_entry= NULL;
6219
  my_error(ER_DDL_LOG_ERROR, MYF(0));
6220
  DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
6221 6222 6223 6224
}


/*
6225 6226 6227
  Write the log entries to ensure that the add partition command is not
  executed at all if a crash before it has completed

unknown's avatar
unknown committed
6228
  SYNOPSIS
6229
    write_log_add_change_partition()
unknown's avatar
unknown committed
6230 6231 6232 6233 6234
    lpt                      Struct containing parameters
  RETURN VALUES
    TRUE                     Error
    FALSE                    Success
  DESCRIPTION
6235
    Prepare entries to the ddl log indicating all partitions to drop and to
6236
    remove the shadow frm file.
6237
    We always inject entries backwards in the list in the ddl log since we
6238
    don't know the entry position until we have written it.
unknown's avatar
unknown committed
6239 6240
*/

6241
static bool write_log_add_change_partition(ALTER_PARTITION_PARAM_TYPE *lpt)
unknown's avatar
unknown committed
6242
{
6243
  partition_info *part_info= lpt->part_info;
6244
  DDL_LOG_MEMORY_ENTRY *log_entry;
6245
  DDL_LOG_MEMORY_ENTRY *exec_log_entry= part_info->exec_log_entry;
6246 6247
  char tmp_path[FN_REFLEN + 1];
  char path[FN_REFLEN + 1];
6248
  uint next_entry= 0;
6249 6250 6251
  DDL_LOG_MEMORY_ENTRY *old_first_log_entry= part_info->first_log_entry;
  /* write_log_drop_shadow_frm(lpt) must have been run first */
  DBUG_ASSERT(old_first_log_entry);
6252
  DBUG_ENTER("write_log_add_change_partition");
unknown's avatar
unknown committed
6253

6254
  build_table_filename(path, sizeof(path) - 1, lpt->db,
6255
                       lpt->table_name, "", 0);
6256
  build_table_shadow_filename(tmp_path, sizeof(tmp_path) - 1, lpt);
Marc Alff's avatar
Marc Alff committed
6257
  mysql_mutex_lock(&LOCK_gdl);
6258 6259 6260 6261

  /* Relink the previous drop shadow frm entry */
  if (old_first_log_entry)
    next_entry= old_first_log_entry->entry_pos;
6262 6263 6264 6265
  if (write_log_dropped_partitions(lpt, &next_entry, (const char*)path,
                                   FALSE))
    goto error;
  log_entry= part_info->first_log_entry;
6266

6267
  if (write_execute_ddl_log_entry(log_entry->entry_pos,
6268 6269 6270
                                  FALSE,
                                  /* Reuse the old execute ddl_log_entry */
                                  &exec_log_entry))
6271
    goto error;
Marc Alff's avatar
Marc Alff committed
6272
  mysql_mutex_unlock(&LOCK_gdl);
6273 6274 6275 6276
  set_part_info_exec_log_entry(part_info, exec_log_entry);
  DBUG_RETURN(FALSE);

error:
6277
  release_part_info_log_entries(part_info->first_log_entry);
Marc Alff's avatar
Marc Alff committed
6278
  mysql_mutex_unlock(&LOCK_gdl);
6279
  part_info->first_log_entry= old_first_log_entry;
6280
  my_error(ER_DDL_LOG_ERROR, MYF(0));
6281
  DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
6282 6283 6284 6285 6286 6287 6288 6289
}


/*
  Write description of how to complete the operation after first phase of
  change partitions.

  SYNOPSIS
6290
    write_log_final_change_partition()
unknown's avatar
unknown committed
6291 6292 6293 6294 6295
    lpt                      Struct containing parameters
  RETURN VALUES
    TRUE                     Error
    FALSE                    Success
  DESCRIPTION
6296 6297 6298 6299 6300 6301 6302 6303 6304
    We will write log entries that specify to
    1) Install the shadow frm file.
    2) Remove all partitions reorganized. (To be able to reorganize a partition
       to the same name. Like in REORGANIZE p0 INTO (p0, p1),
       so that the later rename from the new p0-temporary name to p0 don't
       fail because the partition already exists.
    3) Rename others to reflect the new naming scheme.

    Note that it is written in the ddl log in reverse.
unknown's avatar
unknown committed
6305 6306
*/

6307
static bool write_log_final_change_partition(ALTER_PARTITION_PARAM_TYPE *lpt)
unknown's avatar
unknown committed
6308
{
6309
  partition_info *part_info= lpt->part_info;
6310 6311
  DDL_LOG_MEMORY_ENTRY *log_entry;
  DDL_LOG_MEMORY_ENTRY *exec_log_entry= part_info->exec_log_entry;
6312 6313
  char path[FN_REFLEN + 1];
  char shadow_path[FN_REFLEN + 1];
6314
  DDL_LOG_MEMORY_ENTRY *old_first_log_entry= part_info->first_log_entry;
6315 6316
  uint next_entry= 0;
  DBUG_ENTER("write_log_final_change_partition");
unknown's avatar
unknown committed
6317

6318 6319 6320 6321
  /*
    Do not link any previous log entry.
    Replace the revert operations with forced retry operations.
  */
6322
  part_info->first_log_entry= NULL;
6323
  build_table_filename(path, sizeof(path) - 1, lpt->db,
6324
                       lpt->table_name, "", 0);
6325
  build_table_shadow_filename(shadow_path, sizeof(shadow_path) - 1, lpt);
Marc Alff's avatar
Marc Alff committed
6326
  mysql_mutex_lock(&LOCK_gdl);
6327 6328
  if (write_log_changed_partitions(lpt, &next_entry, (const char*)path))
    goto error;
6329
  if (write_log_dropped_partitions(lpt, &next_entry, (const char*)path,
6330
                      lpt->alter_info->flags & ALTER_REORGANIZE_PARTITION))
6331
    goto error;
6332
  if (write_log_replace_delete_frm(lpt, next_entry, shadow_path, path, TRUE))
6333 6334 6335
    goto error;
  log_entry= part_info->first_log_entry;
  part_info->frm_log_entry= log_entry;
6336
  /* Overwrite the revert execute log entry with this retry execute entry */
6337 6338 6339 6340
  if (write_execute_ddl_log_entry(log_entry->entry_pos,
                                    FALSE, &exec_log_entry))
    goto error;
  release_part_info_log_entries(old_first_log_entry);
Marc Alff's avatar
Marc Alff committed
6341
  mysql_mutex_unlock(&LOCK_gdl);
6342 6343 6344
  DBUG_RETURN(FALSE);

error:
6345
  release_part_info_log_entries(part_info->first_log_entry);
Marc Alff's avatar
Marc Alff committed
6346
  mysql_mutex_unlock(&LOCK_gdl);
6347
  part_info->first_log_entry= old_first_log_entry;
6348
  part_info->frm_log_entry= NULL;
6349
  my_error(ER_DDL_LOG_ERROR, MYF(0));
6350
  DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
6351 6352 6353
}


6354
/*
6355
  Remove entry from ddl log and release resources for others to use
6356 6357 6358 6359 6360 6361 6362 6363

  SYNOPSIS
    write_log_completed()
    lpt                      Struct containing parameters
  RETURN VALUES
    TRUE                     Error
    FALSE                    Success
*/
6364

6365 6366
static void write_log_completed(ALTER_PARTITION_PARAM_TYPE *lpt,
                                bool dont_crash)
6367
{
6368
  partition_info *part_info= lpt->part_info;
6369
  DDL_LOG_MEMORY_ENTRY *log_entry= part_info->exec_log_entry;
6370
  DBUG_ENTER("write_log_completed");
unknown's avatar
unknown committed
6371

6372
  DBUG_ASSERT(log_entry);
Marc Alff's avatar
Marc Alff committed
6373
  mysql_mutex_lock(&LOCK_gdl);
6374
  if (write_execute_ddl_log_entry(0UL, TRUE, &log_entry))
6375 6376
  {
    /*
6377
      Failed to write, Bad...
6378 6379
      We have completed the operation but have log records to REMOVE
      stuff that shouldn't be removed. What clever things could one do
6380 6381
      here? An error output was written to the error output by the
      above method so we don't do anything here.
6382
    */
6383
    ;
6384 6385 6386
  }
  release_part_info_log_entries(part_info->first_log_entry);
  release_part_info_log_entries(part_info->exec_log_entry);
Marc Alff's avatar
Marc Alff committed
6387
  mysql_mutex_unlock(&LOCK_gdl);
6388 6389
  part_info->exec_log_entry= NULL;
  part_info->first_log_entry= NULL;
6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402
  DBUG_VOID_RETURN;
}


/*
   Release all log entries
   SYNOPSIS
     release_log_entries()
     part_info                  Partition info struct
   RETURN VALUES
     NONE
*/

6403
static void release_log_entries(partition_info *part_info)
6404
{
Marc Alff's avatar
Marc Alff committed
6405
  mysql_mutex_lock(&LOCK_gdl);
6406 6407
  release_part_info_log_entries(part_info->first_log_entry);
  release_part_info_log_entries(part_info->exec_log_entry);
Marc Alff's avatar
Marc Alff committed
6408
  mysql_mutex_unlock(&LOCK_gdl);
6409 6410
  part_info->first_log_entry= NULL;
  part_info->exec_log_entry= NULL;
6411 6412 6413
}


6414
/*
6415 6416 6417 6418
  Final part of partition changes to handle things when under
  LOCK TABLES.
  SYNPOSIS
    alter_partition_lock_handling()
6419 6420
    lpt                        Struct carrying parameters
  RETURN VALUES
6421
    NONE
6422
*/
unknown's avatar
unknown committed
6423
static void alter_partition_lock_handling(ALTER_PARTITION_PARAM_TYPE *lpt)
6424
{
Konstantin Osipov's avatar
Konstantin Osipov committed
6425 6426
  THD *thd= lpt->thd;

6427
  if (lpt->old_table)
6428
    close_all_tables_for_name(thd, lpt->old_table->s, HA_EXTRA_NOT_USED);
6429 6430 6431 6432 6433 6434 6435 6436
  if (lpt->table)
  {
    /*
      Only remove the intermediate table object and its share object,
      do not remove the .frm file, since it is the original one.
    */
    close_temporary(lpt->table, 1, 0);
  }
Konstantin Osipov's avatar
Konstantin Osipov committed
6437
  lpt->table= 0;
6438
  lpt->old_table= 0;
Konstantin Osipov's avatar
Konstantin Osipov committed
6439 6440 6441
  lpt->table_list->table= 0;
  if (thd->locked_tables_list.reopen_tables(thd))
    sql_print_warning("We failed to reacquire LOCKs in ALTER TABLE");
6442 6443 6444
}


6445 6446 6447 6448 6449
/*
  Unlock and close table before renaming and dropping partitions
  SYNOPSIS
    alter_close_tables()
    lpt                        Struct carrying parameters
6450
    close_old                  Close original table too
6451 6452 6453 6454
  RETURN VALUES
    0
*/

6455
static int alter_close_tables(ALTER_PARTITION_PARAM_TYPE *lpt, bool close_old)
6456 6457
{
  DBUG_ENTER("alter_close_tables");
6458
  if (lpt->table->db_stat)
6459
  {
Sergei Golubchik's avatar
Sergei Golubchik committed
6460
    lpt->table->file->ha_close();
6461 6462 6463 6464
    lpt->table->db_stat= 0;                        // Mark file closed
  }
  if (close_old && lpt->old_table)
  {
6465
    close_all_tables_for_name(lpt->thd, lpt->old_table->s, HA_EXTRA_NOT_USED);
6466
    lpt->old_table= 0;
6467 6468 6469 6470 6471
  }
  DBUG_RETURN(0);
}


6472 6473 6474 6475 6476 6477 6478 6479
/**
  Handle errors for ALTER TABLE for partitioning.

  @param lpt                Struct carrying parameters
  @param action_completed   The action must be completed, NOT reverted
  @param drop_partition     Partitions has not been dropped yet
  @param frm_install        The shadow frm-file has not yet been installed
  @param close_table        Table is still open, close it before reverting
6480 6481
*/

6482
void handle_alter_part_error(ALTER_PARTITION_PARAM_TYPE *lpt,
6483
                             bool action_completed,
6484
                             bool drop_partition,
6485 6486
                             bool frm_install,
                             bool close_table)
6487 6488 6489 6490
{
  partition_info *part_info= lpt->part_info;
  DBUG_ENTER("handle_alter_part_error");

6491 6492 6493 6494 6495 6496 6497 6498 6499
  if (close_table)
  {
    /*
      Since the error handling (ddl_log) needs to drop newly created
      partitions they must be closed first to not issue errors.
      But we still need some information from the part_info object,
      so we clone it first to have a copy.
    */
    part_info= lpt->part_info->get_clone();
6500
    alter_close_tables(lpt, action_completed);
6501 6502
  }

6503
  if (part_info->first_log_entry &&
6504
      execute_ddl_log_entry(lpt->thd,
unknown's avatar
Fixes  
unknown committed
6505
                            part_info->first_log_entry->entry_pos))
6506 6507
  {
    /*
6508 6509
      We couldn't recover from error, most likely manual interaction
      is required.
6510
    */
6511 6512
    write_log_completed(lpt, FALSE);
    release_log_entries(part_info);
6513
    if (!action_completed)
6514 6515 6516 6517
    {
      if (drop_partition)
      {
        /* Table is still ok, but we left a shadow frm file behind. */
6518
        push_warning_printf(lpt->thd, MYSQL_ERROR::WARN_LEVEL_WARN, 1,
6519 6520 6521
                            "%s %s",
           "Operation was unsuccessful, table is still intact,",
           "but it is possible that a shadow frm file was left behind");
6522 6523 6524 6525
      }
      else
      {
        push_warning_printf(lpt->thd, MYSQL_ERROR::WARN_LEVEL_WARN, 1,
6526 6527 6528 6529 6530
                            "%s %s %s %s",
           "Operation was unsuccessful, table is still intact,",
           "but it is possible that a shadow frm file was left behind.",
           "It is also possible that temporary partitions are left behind,",
           "these could be empty or more or less filled with records");
6531 6532 6533 6534
      }
    }
    else
    {
6535
      if (frm_install)
6536 6537 6538 6539 6540
      {
        /*
           Failed during install of shadow frm file, table isn't intact
           and dropped partitions are still there
        */
6541
        push_warning_printf(lpt->thd, MYSQL_ERROR::WARN_LEVEL_WARN, 1,
6542 6543 6544 6545
                            "%s %s %s",
          "Failed during alter of partitions, table is no longer intact.",
          "The frm file is in an unknown state, and a backup",
          "is required.");
6546 6547 6548 6549
      }
      else if (drop_partition)
      {
        /*
6550 6551 6552 6553
          Table is ok, we have switched to new table but left dropped
          partitions still in their places. We remove the log records and
          ask the user to perform the action manually. We remove the log
          records and ask the user to perform the action manually.
6554
        */
6555
        push_warning_printf(lpt->thd, MYSQL_ERROR::WARN_LEVEL_WARN, 1,
6556 6557 6558
                            "%s %s",
              "Failed during drop of partitions, table is intact.",
              "Manual drop of remaining partitions is required");
6559
      }
6560
      else
6561
      {
6562
        /*
6563 6564 6565
          We failed during renaming of partitions. The table is most
          certainly in a very bad state so we give user warning and disable
          the table by writing an ancient frm version into it.
6566
        */
6567
        push_warning_printf(lpt->thd, MYSQL_ERROR::WARN_LEVEL_WARN, 1,
6568 6569 6570 6571
                            "%s %s %s",
           "Failed during renaming of partitions. We are now in a position",
           "where table is not reusable",
           "Table is disabled by writing ancient frm file version into it");
6572 6573
      }
    }
6574 6575 6576
  }
  else
  {
6577
    release_log_entries(part_info);
6578
    if (!action_completed)
6579 6580 6581
    {
      /*
        We hit an error before things were completed but managed
6582 6583
        to recover from the error. An error occurred and we have
        restored things to original so no need for further action.
6584
      */
6585
      ;
6586 6587 6588 6589 6590 6591
    }
    else
    {
      /*
        We hit an error after we had completed most of the operation
        and were successful in a second attempt so the operation
6592 6593 6594
        actually is successful now. We need to issue a warning that
        even though we reported an error the operation was successfully
        completed.
6595
      */
6596 6597 6598
      push_warning_printf(lpt->thd, MYSQL_ERROR::WARN_LEVEL_WARN, 1,"%s %s",
         "Operation was successfully completed by failure handling,",
         "after failure of normal operation");
6599 6600 6601 6602 6603 6604
    }
  }
  DBUG_VOID_RETURN;
}


6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620
/**
  Downgrade an exclusive MDL lock if under LOCK TABLE.

  If we don't downgrade the lock, it will not be downgraded or released
  until the table is unlocked, resulting in blocking other threads using
  the table.
*/

static void downgrade_mdl_if_lock_tables_mode(THD *thd, MDL_ticket *ticket,
                                              enum_mdl_type type)
{
  if (thd->locked_tables_mode)
    ticket->downgrade_exclusive_lock(type);
}


6621
/**
unknown's avatar
unknown committed
6622 6623 6624
  Actually perform the change requested by ALTER TABLE of partitions
  previously prepared.

6625 6626 6627 6628 6629 6630 6631 6632
  @param thd                           Thread object
  @param table                         Original table object
  @param alter_info                    ALTER TABLE info
  @param create_info                   Create info for CREATE TABLE
  @param table_list                    List of the table involved
  @param db                            Database name of new table
  @param table_name                    Table name of new table
  @param fast_alter_table              Prepared table object
unknown's avatar
unknown committed
6633

6634 6635 6636
  @return Operation status
    @retval TRUE                          Error
    @retval FALSE                         Success
unknown's avatar
unknown committed
6637

6638
  @note
unknown's avatar
unknown committed
6639 6640 6641 6642 6643
    Perform all ALTER TABLE operations for partitioned tables that can be
    performed fast without a full copy of the original table.
*/

uint fast_alter_partition_table(THD *thd, TABLE *table,
6644
                                Alter_info *alter_info,
unknown's avatar
unknown committed
6645 6646
                                HA_CREATE_INFO *create_info,
                                TABLE_LIST *table_list,
6647
                                char *db,
unknown's avatar
unknown committed
6648
                                const char *table_name,
6649
                                TABLE *fast_alter_table)
unknown's avatar
unknown committed
6650 6651
{
  /* Set-up struct used to write frm files */
6652
  partition_info *part_info;
unknown's avatar
unknown committed
6653 6654
  ALTER_PARTITION_PARAM_TYPE lpt_obj;
  ALTER_PARTITION_PARAM_TYPE *lpt= &lpt_obj;
6655
  bool action_completed= FALSE;
6656
  bool close_table_on_failure= FALSE;
6657
  bool frm_install= FALSE;
6658
  MDL_ticket *mdl_ticket= table->mdl_ticket;
6659
  DBUG_ASSERT(fast_alter_table);
unknown's avatar
unknown committed
6660 6661
  DBUG_ENTER("fast_alter_partition_table");

6662
  part_info= fast_alter_table->part_info;
unknown's avatar
unknown committed
6663
  lpt->thd= thd;
Konstantin Osipov's avatar
Konstantin Osipov committed
6664
  lpt->table_list= table_list;
6665
  lpt->part_info= part_info;
6666
  lpt->alter_info= alter_info;
unknown's avatar
unknown committed
6667 6668 6669 6670
  lpt->create_info= create_info;
  lpt->db_options= create_info->table_options;
  if (create_info->row_type == ROW_TYPE_DYNAMIC)
    lpt->db_options|= HA_OPTION_PACK_RECORD;
6671 6672
  lpt->table= fast_alter_table;
  lpt->old_table= table;
unknown's avatar
unknown committed
6673 6674 6675 6676 6677 6678 6679 6680 6681
  lpt->key_info_buffer= 0;
  lpt->key_count= 0;
  lpt->db= db;
  lpt->table_name= table_name;
  lpt->copied= 0;
  lpt->deleted= 0;
  lpt->pack_frm_data= NULL;
  lpt->pack_frm_len= 0;

6682
  /* Never update timestamp columns when alter */
6683
  lpt->table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
6684

6685 6686
  if (table->file->alter_table_flags(alter_info->flags) &
        HA_PARTITION_ONE_PHASE)
unknown's avatar
unknown committed
6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719
  {
    /*
      In the case where the engine supports one phase online partition
      changes it is not necessary to have any exclusive locks. The
      correctness is upheld instead by transactions being aborted if they
      access the table after its partition definition has changed (if they
      are still using the old partition definition).

      The handler is in this case responsible to ensure that all users
      start using the new frm file after it has changed. To implement
      one phase it is necessary for the handler to have the master copy
      of the frm file and use discovery mechanisms to renew it. Thus
      write frm will write the frm, pack the new frm and finally
      the frm is deleted and the discovery mechanisms will either restore
      back to the old or installing the new after the change is activated.

      Thus all open tables will be discovered that they are old, if not
      earlier as soon as they try an operation using the old table. One
      should ensure that this is checked already when opening a table,
      even if it is found in the cache of open tables.

      change_partitions will perform all operations and it is the duty of
      the handler to ensure that the frm files in the system gets updated
      in synch with the changes made and if an error occurs that a proper
      error handling is done.

      If the MySQL Server crashes at this moment but the handler succeeds
      in performing the change then the binlog is not written for the
      change. There is no way to solve this as long as the binlog is not
      transactional and even then it is hard to solve it completely.
 
      The first approach here was to downgrade locks. Now a different approach
      is decided upon. The idea is that the handler will have access to the
6720
      Alter_info when store_lock arrives with TL_WRITE_ALLOW_READ. So if the
unknown's avatar
unknown committed
6721 6722 6723 6724 6725 6726
      handler knows that this functionality can be handled with a lower lock
      level it will set the lock level to TL_WRITE_ALLOW_WRITE immediately.
      Thus the need to downgrade the lock disappears.
      1) Write the new frm, pack it and then delete it
      2) Perform the change within the handler
    */
6727 6728
    if (mysql_write_frm(lpt, WFRM_WRITE_SHADOW | WFRM_PACK_FRM) ||
        mysql_change_partitions(lpt))
unknown's avatar
unknown committed
6729
    {
6730
      goto err;
unknown's avatar
unknown committed
6731 6732
    }
  }
6733
  else if (alter_info->flags & ALTER_DROP_PARTITION)
unknown's avatar
unknown committed
6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756
  {
    /*
      Now after all checks and setting state on dropped partitions we can
      start the actual dropping of the partitions.

      Drop partition is actually two things happening. The first is that
      a lot of records are deleted. The second is that the behaviour of
      subsequent updates and writes and deletes will change. The delete
      part can be handled without any particular high lock level by
      transactional engines whereas non-transactional engines need to
      ensure that this change is done with an exclusive lock on the table.
      The second part, the change of partitioning does however require
      an exclusive lock to install the new partitioning as one atomic
      operation. If this is not the case, it is possible for two
      transactions to see the change in a different order than their
      serialisation order. Thus we need an exclusive lock for both
      transactional and non-transactional engines.

      For LIST partitions it could be possible to avoid the exclusive lock
      (and for RANGE partitions if they didn't rearrange range definitions
      after a DROP PARTITION) if one ensured that failed accesses to the
      dropped partitions was aborted for sure (thus only possible for
      transactional engines).
6757 6758 6759

      0) Write an entry that removes the shadow frm file if crash occurs 
      1) Write the new frm file as a shadow frm
6760 6761
      2) Get an exclusive metadata lock on the table (waits for all active
         transactions using this table). This ensures that we
Konstantin Osipov's avatar
Konstantin Osipov committed
6762 6763 6764
         can release all other locks on the table and since no one can open
         the table, there can be no new threads accessing the table. They
         will be hanging on this exclusive lock.
6765 6766 6767 6768
      3) Write the ddl log to ensure that the operation is completed
         even in the presence of a MySQL Server crash (the log is executed
         before any other threads are started, so there are no locking issues).
      4) Close all tables that have already been opened but didn't stumble on
6769
         the abort locked previously. This is done as part of the
6770 6771
         alter_close_tables call.
      5) Write the bin log
6772 6773 6774 6775 6776 6777
         Unfortunately the writing of the binlog is not synchronised with
         other logging activities. So no matter in which order the binlog
         is written compared to other activities there will always be cases
         where crashes make strange things occur. In this placement it can
         happen that the ALTER TABLE DROP PARTITION gets performed in the
         master but not in the slaves if we have a crash, after writing the
6778 6779
         ddl log but before writing the binlog. A solution to this would
         require writing the statement first in the ddl log and then
6780 6781
         when recovering from the crash read the binlog and insert it into
         the binlog if not written already.
6782 6783 6784 6785 6786 6787
      6) Install the previously written shadow frm file
      7) Prepare handlers for drop of partitions
      8) Drop the partitions
      9) Remove entries from ddl log
      10) Reopen table if under lock tables
      11) Complete query
6788 6789 6790

      We insert Error injections at all places where it could be interesting
      to test if recovery is properly done.
unknown's avatar
unknown committed
6791
    */
6792
    if (write_log_drop_shadow_frm(lpt) ||
6793
        ERROR_INJECT_CRASH("crash_drop_partition_1") ||
6794
        ERROR_INJECT_ERROR("fail_drop_partition_1") ||
6795
        mysql_write_frm(lpt, WFRM_WRITE_SHADOW) ||
6796
        ERROR_INJECT_CRASH("crash_drop_partition_2") ||
6797
        ERROR_INJECT_ERROR("fail_drop_partition_2") ||
6798
        wait_while_table_is_used(thd, table, HA_EXTRA_NOT_USED) ||
6799
        ERROR_INJECT_CRASH("crash_drop_partition_3") ||
6800 6801 6802
        ERROR_INJECT_ERROR("fail_drop_partition_3") ||
        (close_table_on_failure= TRUE, FALSE) ||
        write_log_drop_partition(lpt) ||
6803
        (action_completed= TRUE, FALSE) ||
6804 6805
        ERROR_INJECT_CRASH("crash_drop_partition_4") ||
        ERROR_INJECT_ERROR("fail_drop_partition_4") ||
6806
        alter_close_tables(lpt, action_completed) ||
6807
        (close_table_on_failure= FALSE, FALSE) ||
6808
        ERROR_INJECT_CRASH("crash_drop_partition_5") ||
6809
        ERROR_INJECT_ERROR("fail_drop_partition_5") ||
unknown's avatar
unknown committed
6810 6811
        ((!thd->lex->no_write_to_binlog) &&
         (write_bin_log(thd, FALSE,
6812
                        thd->query(), thd->query_length()), FALSE)) ||
6813
        ERROR_INJECT_CRASH("crash_drop_partition_6") ||
6814 6815
        ERROR_INJECT_ERROR("fail_drop_partition_6") ||
        (frm_install= TRUE, FALSE) ||
6816
        mysql_write_frm(lpt, WFRM_INSTALL_SHADOW) ||
6817
        (frm_install= FALSE, FALSE) ||
6818
        ERROR_INJECT_CRASH("crash_drop_partition_7") ||
6819
        ERROR_INJECT_ERROR("fail_drop_partition_7") ||
6820
        mysql_drop_partitions(lpt) ||
6821
        ERROR_INJECT_CRASH("crash_drop_partition_8") ||
6822
        ERROR_INJECT_ERROR("fail_drop_partition_8") ||
6823
        (write_log_completed(lpt, FALSE), FALSE) ||
6824
        ERROR_INJECT_CRASH("crash_drop_partition_9") ||
6825
        ERROR_INJECT_ERROR("fail_drop_partition_9") ||
6826
        (alter_partition_lock_handling(lpt), FALSE)) 
unknown's avatar
unknown committed
6827
    {
6828
      handle_alter_part_error(lpt, action_completed, TRUE, frm_install,
6829
                              close_table_on_failure);
6830
      goto err;
unknown's avatar
unknown committed
6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845
    }
  }
  else if ((alter_info->flags & ALTER_ADD_PARTITION) &&
           (part_info->part_type == RANGE_PARTITION ||
            part_info->part_type == LIST_PARTITION))
  {
    /*
      ADD RANGE/LIST PARTITIONS
      In this case there are no tuples removed and no tuples are added.
      Thus the operation is merely adding a new partition. Thus it is
      necessary to perform the change as an atomic operation. Otherwise
      someone reading without seeing the new partition could potentially
      miss updates made by a transaction serialised before it that are
      inserted into the new partition.

6846 6847
      0) Write an entry that removes the shadow frm file if crash occurs 
      1) Write the new frm file as a shadow frm file
6848 6849
      2) Get an exclusive metadata lock on the table (waits for all active
         transactions using this table). This ensures that we
Konstantin Osipov's avatar
Konstantin Osipov committed
6850 6851 6852
         can release all other locks on the table and since no one can open
         the table, there can be no new threads accessing the table. They
         will be hanging on this exclusive lock.
6853 6854 6855 6856 6857
      3) Write an entry to remove the new parttions if crash occurs
      4) Add the new partitions.
      5) Close all instances of the table and remove them from the table cache.
      6) Write binlog
      7) Now the change is completed except for the installation of the
unknown's avatar
unknown committed
6858 6859
         new frm file. We thus write an action in the log to change to
         the shadow frm file
6860
      8) Install the new frm file of the table where the partitions are
6861
         added to the table.
6862 6863 6864
      9) Remove entries from ddl log
      10)Reopen tables if under lock tables
      11)Complete query
unknown's avatar
unknown committed
6865
    */
6866
    if (write_log_drop_shadow_frm(lpt) ||
6867
        ERROR_INJECT_CRASH("crash_add_partition_1") ||
6868
        ERROR_INJECT_ERROR("fail_add_partition_1") ||
6869
        mysql_write_frm(lpt, WFRM_WRITE_SHADOW) ||
6870
        ERROR_INJECT_CRASH("crash_add_partition_2") ||
6871
        ERROR_INJECT_ERROR("fail_add_partition_2") ||
6872
        wait_while_table_is_used(thd, table, HA_EXTRA_NOT_USED) ||
6873
        ERROR_INJECT_CRASH("crash_add_partition_3") ||
6874 6875 6876 6877 6878 6879
        ERROR_INJECT_ERROR("fail_add_partition_3") ||
        (close_table_on_failure= TRUE, FALSE) ||
        write_log_add_change_partition(lpt) ||
        ERROR_INJECT_CRASH("crash_add_partition_4") ||
        ERROR_INJECT_ERROR("fail_add_partition_4") ||
        mysql_change_partitions(lpt) ||
6880
        ERROR_INJECT_CRASH("crash_add_partition_5") ||
6881 6882
        ERROR_INJECT_ERROR("fail_add_partition_5") ||
        (close_table_on_failure= FALSE, FALSE) ||
6883
        alter_close_tables(lpt, action_completed) ||
6884 6885
        ERROR_INJECT_CRASH("crash_add_partition_6") ||
        ERROR_INJECT_ERROR("fail_add_partition_6") ||
unknown's avatar
unknown committed
6886 6887
        ((!thd->lex->no_write_to_binlog) &&
         (write_bin_log(thd, FALSE,
6888
                        thd->query(), thd->query_length()), FALSE)) ||
6889
        ERROR_INJECT_CRASH("crash_add_partition_7") ||
6890 6891
        ERROR_INJECT_ERROR("fail_add_partition_7") ||
        write_log_rename_frm(lpt) ||
6892
        (action_completed= TRUE, FALSE) ||
6893
        ERROR_INJECT_CRASH("crash_add_partition_8") ||
6894
        ERROR_INJECT_ERROR("fail_add_partition_8") ||
6895
        (frm_install= TRUE, FALSE) ||
6896
        mysql_write_frm(lpt, WFRM_INSTALL_SHADOW) ||
6897
        (frm_install= FALSE, FALSE) ||
6898
        ERROR_INJECT_CRASH("crash_add_partition_9") ||
6899 6900 6901 6902
        ERROR_INJECT_ERROR("fail_add_partition_9") ||
        (write_log_completed(lpt, FALSE), FALSE) ||
        ERROR_INJECT_CRASH("crash_add_partition_10") ||
        ERROR_INJECT_ERROR("fail_add_partition_10") ||
6903
        (alter_partition_lock_handling(lpt), FALSE))
unknown's avatar
unknown committed
6904
    {
6905
      handle_alter_part_error(lpt, action_completed, FALSE, frm_install,
6906
                              close_table_on_failure);
6907
      goto err;
unknown's avatar
unknown committed
6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942
    }
  }
  else
  {
    /*
      ADD HASH PARTITION/
      COALESCE PARTITION/
      REBUILD PARTITION/
      REORGANIZE PARTITION
 
      In this case all records are still around after the change although
      possibly organised into new partitions, thus by ensuring that all
      updates go to both the old and the new partitioning scheme we can
      actually perform this operation lock-free. The only exception to
      this is when REORGANIZE PARTITION adds/drops ranges. In this case
      there needs to be an exclusive lock during the time when the range
      changes occur.
      This is only possible if the handler can ensure double-write for a
      period. The double write will ensure that it doesn't matter where the
      data is read from since both places are updated for writes. If such
      double writing is not performed then it is necessary to perform the
      change with the usual exclusive lock. With double writes it is even
      possible to perform writes in parallel with the reorganisation of
      partitions.

      Without double write procedure we get the following procedure.
      The only difference with using double write is that we can downgrade
      the lock to TL_WRITE_ALLOW_WRITE. Double write in this case only
      double writes from old to new. If we had double writing in both
      directions we could perform the change completely without exclusive
      lock for HASH partitions.
      Handlers that perform double writing during the copy phase can actually
      use a lower lock level. This can be handled inside store_lock in the
      respective handler.

6943 6944 6945 6946 6947
      0) Write an entry that removes the shadow frm file if crash occurs 
      1) Write the shadow frm file of new partitioning
      2) Log such that temporary partitions added in change phase are
         removed in a crash situation
      3) Add the new partitions
unknown's avatar
unknown committed
6948
         Copy from the reorganised partitions to the new partitions
6949 6950 6951 6952 6953
      4) Get an exclusive metadata lock on the table (waits for all active
         transactions using this table). This ensures that we
         can release all other locks on the table and since no one can open
         the table, there can be no new threads accessing the table. They
         will be hanging on this exclusive lock.
6954
      5) Log that operation is completed and log all complete actions
6955
         needed to complete operation from here
6956 6957
      6) Write bin log
      7) Close all instances of the table and remove them from the table cache.
Konstantin Osipov's avatar
Konstantin Osipov committed
6958 6959 6960 6961 6962 6963
      8) Prepare handlers for rename and delete of partitions
      9) Rename and drop the reorged partitions such that they are no
         longer used and rename those added to their real new names.
      10) Install the shadow frm file
      11) Reopen the table if under lock tables
      12) Complete query
unknown's avatar
unknown committed
6964
    */
6965
    if (write_log_drop_shadow_frm(lpt) ||
6966
        ERROR_INJECT_CRASH("crash_change_partition_1") ||
6967
        ERROR_INJECT_ERROR("fail_change_partition_1") ||
6968
        mysql_write_frm(lpt, WFRM_WRITE_SHADOW) ||
6969
        ERROR_INJECT_CRASH("crash_change_partition_2") ||
6970 6971 6972
        ERROR_INJECT_ERROR("fail_change_partition_2") ||
        (close_table_on_failure= TRUE, FALSE) ||
        write_log_add_change_partition(lpt) ||
6973
        ERROR_INJECT_CRASH("crash_change_partition_3") ||
6974 6975
        ERROR_INJECT_ERROR("fail_change_partition_3") ||
        mysql_change_partitions(lpt) ||
6976
        ERROR_INJECT_CRASH("crash_change_partition_4") ||
6977
        ERROR_INJECT_ERROR("fail_change_partition_4") ||
6978
        wait_while_table_is_used(thd, table, HA_EXTRA_NOT_USED) ||
6979 6980
        ERROR_INJECT_CRASH("crash_change_partition_5") ||
        ERROR_INJECT_ERROR("fail_change_partition_5") ||
6981 6982
        write_log_final_change_partition(lpt) ||
        (action_completed= TRUE, FALSE) ||
6983
        ERROR_INJECT_CRASH("crash_change_partition_6") ||
6984
        ERROR_INJECT_ERROR("fail_change_partition_6") ||
unknown's avatar
unknown committed
6985 6986
        ((!thd->lex->no_write_to_binlog) &&
         (write_bin_log(thd, FALSE,
6987
                        thd->query(), thd->query_length()), FALSE)) ||
6988 6989 6990 6991 6992
        ERROR_INJECT_CRASH("crash_change_partition_7") ||
        ERROR_INJECT_ERROR("fail_change_partition_7") ||
        ((frm_install= TRUE), FALSE) ||
        mysql_write_frm(lpt, WFRM_INSTALL_SHADOW) ||
        (frm_install= FALSE, FALSE) ||
6993
        ERROR_INJECT_CRASH("crash_change_partition_8") ||
6994
        ERROR_INJECT_ERROR("fail_change_partition_8") ||
6995 6996
        alter_close_tables(lpt, action_completed) ||
        (close_table_on_failure= FALSE, FALSE) ||
6997
        ERROR_INJECT_CRASH("crash_change_partition_9") ||
6998 6999 7000 7001
        ERROR_INJECT_ERROR("fail_change_partition_9") ||
        mysql_drop_partitions(lpt) ||
        ERROR_INJECT_CRASH("crash_change_partition_10") ||
        ERROR_INJECT_ERROR("fail_change_partition_10") ||
7002
        mysql_rename_partitions(lpt) ||
7003
        ERROR_INJECT_CRASH("crash_change_partition_11") ||
7004 7005 7006 7007
        ERROR_INJECT_ERROR("fail_change_partition_11") ||
        (write_log_completed(lpt, FALSE), FALSE) ||
        ERROR_INJECT_CRASH("crash_change_partition_12") ||
        ERROR_INJECT_ERROR("fail_change_partition_12") ||
7008
        (alter_partition_lock_handling(lpt), FALSE))
unknown's avatar
unknown committed
7009
    {
7010
      handle_alter_part_error(lpt, action_completed, FALSE, frm_install,
7011
                              close_table_on_failure);
7012
      goto err;
unknown's avatar
unknown committed
7013 7014
    }
  }
7015
  downgrade_mdl_if_lock_tables_mode(thd, mdl_ticket, MDL_SHARED_NO_READ_WRITE);
unknown's avatar
unknown committed
7016 7017 7018 7019
  /*
    A final step is to write the query to the binlog and send ok to the
    user
  */
7020
  DBUG_RETURN(fast_end_partition(thd, lpt->copied, lpt->deleted, table_list));
7021
err:
7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037
  if (action_completed)
  {
    /*
      Although error occurred, the action was forced to retry for completion.
      Therefore we must close+reopen all instances of the table.
    */
    (void) alter_partition_lock_handling(lpt);
  }
  else
  {
    /*
      The failed action was reverted, leave the original table as is and
      close/destroy the intermediate table object and its share.
    */
    close_temporary(lpt->table, 1, 0);
  }
7038
  downgrade_mdl_if_lock_tables_mode(thd, mdl_ticket, MDL_SHARED_NO_READ_WRITE);
7039
  DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061
}
#endif


/*
  Prepare for calling val_int on partition function by setting fields to
  point to the record where the values of the PF-fields are stored.

  SYNOPSIS
    set_field_ptr()
    ptr                 Array of fields to change ptr
    new_buf             New record pointer
    old_buf             Old record pointer

  DESCRIPTION
    Set ptr in field objects of field array to refer to new_buf record
    instead of previously old_buf. Used before calling val_int and after
    it is used to restore pointers to table->record[0].
    This routine is placed outside of partition code since it can be useful
    also for other programs.
*/

7062 7063
void set_field_ptr(Field **ptr, const uchar *new_buf,
                   const uchar *old_buf)
unknown's avatar
unknown committed
7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095
{
  my_ptrdiff_t diff= (new_buf - old_buf);
  DBUG_ENTER("set_field_ptr");

  do
  {
    (*ptr)->move_field_offset(diff);
  } while (*(++ptr));
  DBUG_VOID_RETURN;
}


/*
  Prepare for calling val_int on partition function by setting fields to
  point to the record where the values of the PF-fields are stored.
  This variant works on a key_part reference.
  It is not required that all fields are NOT NULL fields.

  SYNOPSIS
    set_key_field_ptr()
    key_info            key info with a set of fields to change ptr
    new_buf             New record pointer
    old_buf             Old record pointer

  DESCRIPTION
    Set ptr in field objects of field array to refer to new_buf record
    instead of previously old_buf. Used before calling val_int and after
    it is used to restore pointers to table->record[0].
    This routine is placed outside of partition code since it can be useful
    also for other programs.
*/

7096 7097
void set_key_field_ptr(KEY *key_info, const uchar *new_buf,
                       const uchar *old_buf)
unknown's avatar
unknown committed
7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130
{
  KEY_PART_INFO *key_part= key_info->key_part;
  uint key_parts= key_info->key_parts;
  uint i= 0;
  my_ptrdiff_t diff= (new_buf - old_buf);
  DBUG_ENTER("set_key_field_ptr");

  do
  {
    key_part->field->move_field_offset(diff);
    key_part++;
  } while (++i < key_parts);
  DBUG_VOID_RETURN;
}


/*
  SYNOPSIS
    mem_alloc_error()
    size                Size of memory attempted to allocate
    None

  RETURN VALUES
    None

  DESCRIPTION
    A routine to use for all the many places in the code where memory
    allocation error can happen, a tremendous amount of them, needs
    simple routine that signals this error.
*/

void mem_alloc_error(size_t size)
{
7131 7132
  my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), 
           static_cast<int>(size));
7133
}
unknown's avatar
unknown committed
7134

7135
#ifdef WITH_PARTITION_STORAGE_ENGINE
unknown's avatar
unknown committed
7136
/*
7137 7138
  Return comma-separated list of used partitions in the provided given string

unknown's avatar
unknown committed
7139 7140 7141 7142
  SYNOPSIS
    make_used_partitions_str()
      part_info  IN  Partitioning info
      parts_str  OUT The string to fill
7143 7144 7145 7146 7147 7148 7149

  DESCRIPTION
    Generate a list of used partitions (from bits in part_info->used_partitions
    bitmap), asd store it into the provided String object.
    
  NOTE
    The produced string must not be longer then MAX_PARTITIONS * (1 + FN_LEN).
unknown's avatar
unknown committed
7150 7151 7152 7153 7154 7155 7156 7157 7158
*/

void make_used_partitions_str(partition_info *part_info, String *parts_str)
{
  parts_str->length(0);
  partition_element *pe;
  uint partition_id= 0;
  List_iterator<partition_element> it(part_info->partitions);
  
7159
  if (part_info->is_sub_partitioned())
unknown's avatar
unknown committed
7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197
  {
    partition_element *head_pe;
    while ((head_pe= it++))
    {
      List_iterator<partition_element> it2(head_pe->subpartitions);
      while ((pe= it2++))
      {
        if (bitmap_is_set(&part_info->used_partitions, partition_id))
        {
          if (parts_str->length())
            parts_str->append(',');
          parts_str->append(head_pe->partition_name,
                           strlen(head_pe->partition_name),
                           system_charset_info);
          parts_str->append('_');
          parts_str->append(pe->partition_name,
                           strlen(pe->partition_name),
                           system_charset_info);
        }
        partition_id++;
      }
    }
  }
  else
  {
    while ((pe= it++))
    {
      if (bitmap_is_set(&part_info->used_partitions, partition_id))
      {
        if (parts_str->length())
          parts_str->append(',');
        parts_str->append(pe->partition_name, strlen(pe->partition_name),
                         system_charset_info);
      }
      partition_id++;
    }
  }
}
7198
#endif
unknown's avatar
unknown committed
7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218

/****************************************************************************
 * Partition interval analysis support
 ***************************************************************************/

/*
  Setup partition_info::* members related to partitioning range analysis

  SYNOPSIS
    set_up_partition_func_pointers()
      part_info  Partitioning info structure

  DESCRIPTION
    Assuming that passed partition_info structure already has correct values
    for members that specify [sub]partitioning type, table fields, and
    functions, set up partition_info::* members that are related to
    Partitioning Interval Analysis (see get_partitions_in_range_iter for its
    definition)

  IMPLEMENTATION
7219 7220
    There are three available interval analyzer functions:
    (1) get_part_iter_for_interval_via_mapping
7221 7222
    (2) get_part_iter_for_interval_cols_via_map 
    (3) get_part_iter_for_interval_via_walking
unknown's avatar
unknown committed
7223

7224
    They all have limited applicability:
unknown's avatar
unknown committed
7225 7226
    (1) is applicable for "PARTITION BY <RANGE|LIST>(func(t.field))", where
    func is a monotonic function.
7227

7228
    (2) is applicable for "PARTITION BY <RANGE|LIST> COLUMNS (field_list)
7229 7230

    (3) is applicable for 
unknown's avatar
unknown committed
7231 7232
      "[SUB]PARTITION BY <any-partitioning-type>(any_func(t.integer_field))"
      
7233
    If both (1) and (3) are applicable, (1) is preferred over (3).
unknown's avatar
unknown committed
7234 7235 7236 7237 7238
    
    This function sets part_info::get_part_iter_for_interval according to
    this criteria, and also sets some auxilary fields that the function
    uses.
*/
7239
#ifdef WITH_PARTITION_STORAGE_ENGINE
unknown's avatar
unknown committed
7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252
static void set_up_range_analysis_info(partition_info *part_info)
{
  /* Set the catch-all default */
  part_info->get_part_iter_for_interval= NULL;
  part_info->get_subpart_iter_for_interval= NULL;

  /* 
    Check if get_part_iter_for_interval_via_mapping() can be used for 
    partitioning
  */
  switch (part_info->part_type) {
  case RANGE_PARTITION:
  case LIST_PARTITION:
7253 7254 7255 7256 7257 7258 7259 7260 7261 7262
    if (!part_info->column_list)
    {
      if (part_info->part_expr->get_monotonicity_info() != NON_MONOTONIC)
      {
        part_info->get_part_iter_for_interval=
          get_part_iter_for_interval_via_mapping;
        goto setup_subparts;
      }
    }
    else
unknown's avatar
unknown committed
7263 7264
    {
      part_info->get_part_iter_for_interval=
7265
        get_part_iter_for_interval_cols_via_map;
unknown's avatar
unknown committed
7266 7267 7268 7269 7270 7271 7272
      goto setup_subparts;
    }
  default:
    ;
  }
   
  /*
7273
    Check if get_part_iter_for_interval_via_walking() can be used for
unknown's avatar
unknown committed
7274 7275
    partitioning
  */
7276
  if (part_info->num_part_fields == 1)
unknown's avatar
unknown committed
7277 7278 7279 7280 7281
  {
    Field *field= part_info->part_field_array[0];
    switch (field->type()) {
    case MYSQL_TYPE_TINY:
    case MYSQL_TYPE_SHORT:
7282
    case MYSQL_TYPE_INT24:
unknown's avatar
unknown committed
7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294
    case MYSQL_TYPE_LONG:
    case MYSQL_TYPE_LONGLONG:
      part_info->get_part_iter_for_interval=
        get_part_iter_for_interval_via_walking;
      break;
    default:
      ;
    }
  }

setup_subparts:
  /*
7295
    Check if get_part_iter_for_interval_via_walking() can be used for
unknown's avatar
unknown committed
7296 7297
    subpartitioning
  */
7298
  if (part_info->num_subpart_fields == 1)
unknown's avatar
unknown committed
7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315
  {
    Field *field= part_info->subpart_field_array[0];
    switch (field->type()) {
    case MYSQL_TYPE_TINY:
    case MYSQL_TYPE_SHORT:
    case MYSQL_TYPE_LONG:
    case MYSQL_TYPE_LONGLONG:
      part_info->get_subpart_iter_for_interval=
        get_part_iter_for_interval_via_walking;
      break;
    default:
      ;
    }
  }
}


Mikael Ronstrom's avatar
Mikael Ronstrom committed
7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331
/*
  This function takes a memory of packed fields in opt-range format
  and stores it in record format. To avoid having to worry about how
  the length of fields are calculated in opt-range format we send
  an array of lengths used for each field in store_length_array.

  SYNOPSIS
  store_tuple_to_record()
  pfield                         Field array
  store_length_array             Array of field lengths
  value                          Memory where fields are stored
  value_end                      End of memory

  RETURN VALUE
  nparts                         Number of fields assigned
*/
7332 7333 7334 7335 7336
uint32 store_tuple_to_record(Field **pfield,
                             uint32 *store_length_array,
                             uchar *value,
                             uchar *value_end)
{
Mikael Ronstrom's avatar
Mikael Ronstrom committed
7337
  /* This function is inspired by store_key_image_rec. */
7338 7339 7340 7341 7342 7343 7344 7345 7346
  uint32 nparts= 0;
  uchar *loc_value;
  while (value < value_end)
  {
    loc_value= value;
    if ((*pfield)->real_maybe_null())
    {
      if (*loc_value)
        (*pfield)->set_null();
7347 7348
      else
        (*pfield)->set_notnull();
7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360
      loc_value++;
    }
    uint len= (*pfield)->pack_length();
    (*pfield)->set_key_image(loc_value, len);
    value+= *store_length_array;
    store_length_array++;
    nparts++;
    pfield++;
  }
  return nparts;
}

7361 7362 7363 7364 7365
/**
  RANGE(columns) partitioning: compare partition value bound and probe tuple.

  @param val           Partition column values.
  @param nvals_in_rec  Number of (prefix) fields to compare.
7366

7367
  @return Less than/Equal to/Greater than 0 if the record is L/E/G than val.
7368

7369 7370 7371
  @note The partition value bound is always a full tuple (but may include the
  MAXVALUE special value). The probe tuple may be a prefix of partitioning
  tuple.
7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400
*/

static int cmp_rec_and_tuple(part_column_list_val *val, uint32 nvals_in_rec)
{
  partition_info *part_info= val->part_info;
  Field **field= part_info->part_field_array;
  Field **fields_end= field + nvals_in_rec;
  int res;

  for (; field != fields_end; field++, val++)
  {
    if (val->max_value)
      return -1;
    if ((*field)->is_null())
    {
      if (val->null_value)
        continue;
      return -1;
    }
    if (val->null_value)
      return +1;
    res= (*field)->cmp((const uchar*)val->column_value);
    if (res)
      return res;
  }
  return 0;
}


7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417
/**
  Compare record and columns partition tuple including endpoint handling.

  @param  val               Columns partition tuple
  @param  n_vals_in_rec     Number of columns to compare
  @param  is_left_endpoint  True if left endpoint (part_tuple < rec or
                            part_tuple <= rec)
  @param  include_endpoint  If endpoint is included (part_tuple <= rec or
                            rec <= part_tuple)

  @return Less than/Equal to/Greater than 0 if the record is L/E/G than
  the partition tuple.

  @see get_list_array_idx_for_endpoint() and
  get_partition_id_range_for_endpoint().
*/

7418 7419
static int cmp_rec_and_tuple_prune(part_column_list_val *val,
                                   uint32 n_vals_in_rec,
7420 7421
                                   bool is_left_endpoint,
                                   bool include_endpoint)
7422 7423 7424 7425 7426
{
  int cmp;
  Field **field;
  if ((cmp= cmp_rec_and_tuple(val, n_vals_in_rec)))
    return cmp;
7427 7428
  field= val->part_info->part_field_array + n_vals_in_rec;
  if (!(*field))
7429
  {
7430 7431 7432
    /* Full match. Only equal if including endpoint. */
    if (include_endpoint)
      return 0;
7433

7434 7435 7436
    if (is_left_endpoint)
      return +4;     /* Start of range, part_tuple < rec, return higher. */
    return -4;     /* End of range, rec < part_tupe, return lesser. */
7437
  }
7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465
  /*
    The prefix is equal and there are more partition columns to compare.

    If including left endpoint or not including right endpoint
    then the record is considered lesser compared to the partition.

    i.e:
    part(10, x) <= rec(10, unknown) and rec(10, unknown) < part(10, x)
    part <= rec -> lesser (i.e. this or previous partitions)
    rec < part -> lesser (i.e. this or previous partitions)
  */
  if (is_left_endpoint == include_endpoint)
    return -2;

  /*
    If right endpoint and the first additional partition value
    is MAXVALUE, then the record is lesser.
  */
  if (!is_left_endpoint && (val + n_vals_in_rec)->max_value)
    return -3;

  /*
    Otherwise the record is considered greater.

    rec <= part -> greater (i.e. does not match this partition, seek higher).
    part < rec -> greater (i.e. does not match this partition, seek higher).
  */
  return 2;
7466 7467 7468
}


unknown's avatar
unknown committed
7469 7470 7471
typedef uint32 (*get_endpoint_func)(partition_info*, bool left_endpoint,
                                    bool include_endpoint);

7472 7473
typedef uint32 (*get_col_endpoint_func)(partition_info*, bool left_endpoint,
                                        bool include_endpoint,
7474
                                        uint32 num_parts);
7475

7476 7477
/**
  Get partition for RANGE COLUMNS endpoint.
unknown's avatar
unknown committed
7478

7479 7480 7481 7482
  @param part_info         Partitioning metadata.
  @param is_left_endpoint     True if left endpoint (const <=/< cols)
  @param include_endpoint  True if range includes the endpoint (<=/>=)
  @param nparts            Total number of partitions
unknown's avatar
unknown committed
7483

7484
  @return Partition id of matching partition.
unknown's avatar
unknown committed
7485

7486 7487
  @see get_partition_id_cols_list_for_endpoint and
  get_partition_id_range_for_endpoint.
unknown's avatar
unknown committed
7488 7489
*/

7490
uint32 get_partition_id_cols_range_for_endpoint(partition_info *part_info,
7491
                                                bool is_left_endpoint,
7492 7493 7494
                                                bool include_endpoint,
                                                uint32 nparts)
{
7495
  uint min_part_id= 0, max_part_id= part_info->num_parts, loc_part_id;
7496
  part_column_list_val *range_col_array= part_info->range_col_array;
7497
  uint num_columns= part_info->part_field_list.elements;
7498 7499
  DBUG_ENTER("get_partition_id_cols_range_for_endpoint");

7500 7501
  /* Find the matching partition (including taking endpoint into account). */
  do
7502
  {
7503 7504 7505 7506 7507 7508 7509
    /* Midpoint, adjusted down, so it can never be > last partition. */
    loc_part_id= (max_part_id + min_part_id) >> 1;
    if (0 <= cmp_rec_and_tuple_prune(range_col_array +
                                       loc_part_id * num_columns,
                                     nparts,
                                     is_left_endpoint,
                                     include_endpoint))
7510 7511
      min_part_id= loc_part_id + 1;
    else
7512 7513
      max_part_id= loc_part_id;
  } while (max_part_id > min_part_id);
7514
  loc_part_id= max_part_id;
7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532

  /* Given value must be LESS THAN the found partition. */
  DBUG_ASSERT(loc_part_id == part_info->num_parts ||
              (0 > cmp_rec_and_tuple_prune(range_col_array +
                                             loc_part_id * num_columns,
                                           nparts, is_left_endpoint,
                                           include_endpoint)));
  /* Given value must be GREATER THAN or EQUAL to the previous partition. */
  DBUG_ASSERT(loc_part_id == 0 ||
              (0 <= cmp_rec_and_tuple_prune(range_col_array +
                                              (loc_part_id - 1) * num_columns,
                                            nparts, is_left_endpoint,
                                            include_endpoint)));

  if (!is_left_endpoint)
  {
    /* Set the end after this partition if not already after the last. */
    if (loc_part_id < part_info->num_parts)
7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547
      loc_part_id++;
  }
  DBUG_RETURN(loc_part_id);
}


int get_part_iter_for_interval_cols_via_map(partition_info *part_info,
                                            bool is_subpart,
                                            uint32 *store_length_array,
                                            uchar *min_value, uchar *max_value,
                                            uint min_len, uint max_len, 
                                            uint flags,
                                            PARTITION_ITERATOR *part_iter)
{
  uint32 nparts;
7548
  get_col_endpoint_func  UNINIT_VAR(get_col_endpoint);
7549 7550 7551 7552 7553 7554 7555
  DBUG_ENTER("get_part_iter_for_interval_cols_via_map");

  if (part_info->part_type == RANGE_PARTITION)
  {
    get_col_endpoint= get_partition_id_cols_range_for_endpoint;
    part_iter->get_next= get_next_partition_id_range;
  }
7556
  else if (part_info->part_type == LIST_PARTITION)
7557 7558 7559 7560
  {
    get_col_endpoint= get_partition_id_cols_list_for_endpoint;
    part_iter->get_next= get_next_partition_id_list;
    part_iter->part_info= part_info;
7561
    DBUG_ASSERT(part_info->num_list_values);
7562
  }
7563 7564 7565
  else
    assert(0);

7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579
  if (flags & NO_MIN_RANGE)
    part_iter->part_nums.start= part_iter->part_nums.cur= 0;
  else
  {
    // Copy from min_value to record
    nparts= store_tuple_to_record(part_info->part_field_array,
                                  store_length_array,
                                  min_value,
                                  min_value + min_len);
    part_iter->part_nums.start= part_iter->part_nums.cur=
      get_col_endpoint(part_info, TRUE, !(flags & NEAR_MIN),
                       nparts);
  }
  if (flags & NO_MAX_RANGE)
7580 7581 7582 7583 7584 7585 7586 7587 7588
  {
    if (part_info->part_type == RANGE_PARTITION)
      part_iter->part_nums.end= part_info->num_parts;
    else /* LIST_PARTITION */
    {
      DBUG_ASSERT(part_info->part_type == LIST_PARTITION);
      part_iter->part_nums.end= part_info->num_list_values;
    }
  }
7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605
  else
  {
    // Copy from max_value to record
    nparts= store_tuple_to_record(part_info->part_field_array,
                                  store_length_array,
                                  max_value,
                                  max_value + max_len);
    part_iter->part_nums.end= get_col_endpoint(part_info, FALSE,
                                               !(flags & NEAR_MAX),
                                               nparts);
  }
  if (part_iter->part_nums.start == part_iter->part_nums.end)
    DBUG_RETURN(0);
  DBUG_RETURN(1);
}


7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639
/**
  Partitioning Interval Analysis: Initialize the iterator for "mapping" case

  @param part_info   Partition info
  @param is_subpart  TRUE  - act for subpartitioning
                     FALSE - act for partitioning
  @param store_length_array  Ignored.
  @param min_value   minimum field value, in opt_range key format.
  @param max_value   minimum field value, in opt_range key format.
  @param min_len     Ignored.
  @param max_len     Ignored.
  @param flags       Some combination of NEAR_MIN, NEAR_MAX, NO_MIN_RANGE,
                     NO_MAX_RANGE.
  @param part_iter   Iterator structure to be initialized

  @details Initialize partition set iterator to walk over the interval in
  ordered-array-of-partitions (for RANGE partitioning) or
  ordered-array-of-list-constants (for LIST partitioning) space.

  This function is used when partitioning is done by
  <RANGE|LIST>(ascending_func(t.field)), and we can map an interval in
  t.field space into a sub-array of partition_info::range_int_array or
  partition_info::list_array (see get_partition_id_range_for_endpoint,
  get_list_array_idx_for_endpoint for details).

  The function performs this interval mapping, and sets the iterator to
  traverse the sub-array and return appropriate partitions.

  @return Status of iterator
    @retval 0   No matching partitions (iterator not initialized)
    @retval 1   Ok, iterator intialized for traversal of matching partitions.
    @retval -1  All partitions would match (iterator not initialized)
*/

unknown's avatar
unknown committed
7640 7641
int get_part_iter_for_interval_via_mapping(partition_info *part_info,
                                           bool is_subpart,
7642
                                           uint32 *store_length_array, /* ignored */
7643
                                           uchar *min_value, uchar *max_value,
7644
                                           uint min_len, uint max_len, /* ignored */
unknown's avatar
unknown committed
7645 7646 7647 7648
                                           uint flags,
                                           PARTITION_ITERATOR *part_iter)
{
  Field *field= part_info->part_field_array[0];
7649 7650
  uint32             UNINIT_VAR(max_endpoint_val);
  get_endpoint_func  UNINIT_VAR(get_endpoint);
7651
  bool               can_match_multiple_values;  /* is not '=' */
unknown's avatar
unknown committed
7652
  uint field_len= field->pack_length_in_rec();
7653 7654 7655
  MYSQL_TIME start_date;
  bool check_zero_dates= false;
  bool zero_in_start_date= true;
7656 7657 7658 7659 7660
  DBUG_ENTER("get_part_iter_for_interval_via_mapping");
  DBUG_ASSERT(!is_subpart);
  (void) store_length_array;
  (void)min_len;
  (void)max_len;
7661
  part_iter->ret_null_part= part_iter->ret_null_part_orig= FALSE;
unknown's avatar
unknown committed
7662 7663 7664

  if (part_info->part_type == RANGE_PARTITION)
  {
7665
    if (part_info->part_charset_field_array)
7666 7667 7668
      get_endpoint=        get_partition_id_range_for_endpoint_charset;
    else
      get_endpoint=        get_partition_id_range_for_endpoint;
7669
    max_endpoint_val=    part_info->num_parts;
unknown's avatar
unknown committed
7670 7671 7672 7673
    part_iter->get_next= get_next_partition_id_range;
  }
  else if (part_info->part_type == LIST_PARTITION)
  {
7674

7675
    if (part_info->part_charset_field_array)
7676 7677 7678
      get_endpoint=        get_list_array_idx_for_endpoint_charset;
    else
      get_endpoint=        get_list_array_idx_for_endpoint;
7679
    max_endpoint_val=    part_info->num_list_values;
unknown's avatar
unknown committed
7680 7681
    part_iter->get_next= get_next_partition_id_list;
    part_iter->part_info= part_info;
7682 7683 7684 7685 7686 7687 7688 7689 7690 7691
    if (max_endpoint_val == 0)
    {
      /*
        We handle this special case without optimisations since it is
        of little practical value but causes a great number of complex
        checks later in the code.
      */
      part_iter->part_nums.start= part_iter->part_nums.end= 0;
      part_iter->part_nums.cur= 0;
      part_iter->ret_null_part= part_iter->ret_null_part_orig= TRUE;
7692
      DBUG_RETURN(-1);
7693
    }
unknown's avatar
unknown committed
7694 7695
  }
  else
7696 7697
    MY_ASSERT_UNREACHABLE();

7698 7699 7700 7701 7702
  can_match_multiple_values= (flags || !min_value || !max_value ||
                              memcmp(min_value, max_value, field_len));
  if (can_match_multiple_values &&
      (part_info->part_type == RANGE_PARTITION ||
       part_info->has_null_value))
7703
  {
7704
    /* Range scan on RANGE or LIST partitioned table */
7705 7706 7707 7708 7709 7710 7711
    enum_monotonicity_info monotonic;
    monotonic= part_info->part_expr->get_monotonicity_info();
    if (monotonic == MONOTONIC_INCREASING_NOT_NULL ||
        monotonic == MONOTONIC_STRICT_INCREASING_NOT_NULL)
    {
      /* col is NOT NULL, but F(col) can return NULL, add NULL partition */
      part_iter->ret_null_part= part_iter->ret_null_part_orig= TRUE;
7712
      check_zero_dates= true;
7713 7714
    }
  }
unknown's avatar
unknown committed
7715

7716 7717 7718 7719 7720 7721
  /* 
    Find minimum: Do special handling if the interval has left bound in form
     " NULL <= X ":
  */
  if (field->real_maybe_null() && part_info->has_null_value && 
      !(flags & (NO_MIN_RANGE | NEAR_MIN)) && *min_value)
unknown's avatar
unknown committed
7722
  {
7723 7724
    part_iter->ret_null_part= part_iter->ret_null_part_orig= TRUE;
    part_iter->part_nums.start= part_iter->part_nums.cur= 0;
7725
    if (!(flags & NO_MAX_RANGE) && *max_value)
unknown's avatar
unknown committed
7726
    {
7727 7728
      /* The right bound is X <= NULL, i.e. it is a "X IS NULL" interval */
      part_iter->part_nums.end= 0;
7729
      DBUG_RETURN(1);
unknown's avatar
unknown committed
7730 7731
    }
  }
unknown's avatar
unknown committed
7732 7733
  else
  {
7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744
    if (flags & NO_MIN_RANGE)
      part_iter->part_nums.start= part_iter->part_nums.cur= 0;
    else
    {
      /*
        Store the interval edge in the record buffer, and call the
        function that maps the edge in table-field space to an edge
        in ordered-set-of-partitions (for RANGE partitioning) or 
        index-in-ordered-array-of-list-constants (for LIST) space.
      */
      store_key_image_to_rec(field, min_value, field_len);
7745
      bool include_endp= !test(flags & NEAR_MIN);
7746
      part_iter->part_nums.start= get_endpoint(part_info, 1, include_endp);
7747 7748 7749 7750 7751 7752
      if (!can_match_multiple_values && part_info->part_expr->null_value)
      {
        /* col = x and F(x) = NULL -> only search NULL partition */
        part_iter->part_nums.cur= part_iter->part_nums.start= 0;
        part_iter->part_nums.end= 0;
        part_iter->ret_null_part= part_iter->ret_null_part_orig= TRUE;
7753
        DBUG_RETURN(1);
7754
      }
7755
      part_iter->part_nums.cur= part_iter->part_nums.start;
7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768
      if (check_zero_dates && !part_info->part_expr->null_value)
      {
        if (!(flags & NO_MAX_RANGE) &&
            (field->type() == MYSQL_TYPE_DATE ||
             field->type() == MYSQL_TYPE_DATETIME))
        {
          /* Monotonic, but return NULL for dates with zeros in month/day. */
          zero_in_start_date= field->get_date(&start_date, 0);
          DBUG_PRINT("info", ("zero start %u %04d-%02d-%02d",
                              zero_in_start_date, start_date.year,
                              start_date.month, start_date.day));
        }
      }
7769
      if (part_iter->part_nums.start == max_endpoint_val)
7770
        DBUG_RETURN(0); /* No partitions */
7771
    }
unknown's avatar
unknown committed
7772 7773 7774 7775
  }

  /* Find maximum, do the same as above but for right interval bound */
  if (flags & NO_MAX_RANGE)
7776
    part_iter->part_nums.end= max_endpoint_val;
unknown's avatar
unknown committed
7777 7778 7779
  else
  {
    store_key_image_to_rec(field, max_value, field_len);
7780
    bool include_endp= !test(flags & NEAR_MAX);
7781
    part_iter->part_nums.end= get_endpoint(part_info, 0, include_endp);
7782 7783 7784 7785 7786 7787 7788
    if (check_zero_dates &&
        !zero_in_start_date &&
        !part_info->part_expr->null_value)
    {
      MYSQL_TIME end_date;
      bool zero_in_end_date= field->get_date(&end_date, 0);
      /*
7789 7790
        This is an optimization for TO_DAYS()/TO_SECONDS() to avoid scanning
        the NULL partition for ranges that cannot include a date with 0 as
7791 7792 7793 7794 7795 7796
        month/day.
      */
      DBUG_PRINT("info", ("zero end %u %04d-%02d-%02d",
                          zero_in_end_date,
                          end_date.year, end_date.month, end_date.day));
      DBUG_ASSERT(!memcmp(((Item_func*) part_info->part_expr)->func_name(),
7797 7798 7799
                          "to_days", 7) ||
                  !memcmp(((Item_func*) part_info->part_expr)->func_name(),
                          "to_seconds", 10));
7800 7801 7802 7803 7804
      if (!zero_in_end_date &&
          start_date.month == end_date.month &&
          start_date.year == end_date.year)
        part_iter->ret_null_part= part_iter->ret_null_part_orig= false;
    }
7805
    if (part_iter->part_nums.start >= part_iter->part_nums.end &&
7806
        !part_iter->ret_null_part)
7807
      DBUG_RETURN(0); /* No partitions */
unknown's avatar
unknown committed
7808
  }
7809
  DBUG_RETURN(1); /* Ok, iterator initialized */
unknown's avatar
unknown committed
7810 7811 7812 7813
}


/* See get_part_iter_for_interval_via_walking for definition of what this is */
7814
#define MAX_RANGE_TO_WALK 32
unknown's avatar
unknown committed
7815 7816 7817


/*
7818
  Partitioning Interval Analysis: Initialize iterator to walk field interval
unknown's avatar
unknown committed
7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833

  SYNOPSIS
    get_part_iter_for_interval_via_walking()
      part_info   Partition info
      is_subpart  TRUE  - act for subpartitioning
                  FALSE - act for partitioning
      min_value   minimum field value, in opt_range key format.
      max_value   minimum field value, in opt_range key format.
      flags       Some combination of NEAR_MIN, NEAR_MAX, NO_MIN_RANGE,
                  NO_MAX_RANGE.
      part_iter   Iterator structure to be initialized

  DESCRIPTION
    Initialize partition set iterator to walk over interval in integer field
    space. That is, for "const1 <=? t.field <=? const2" interval, initialize 
7834 7835
    the iterator to return a set of [sub]partitions obtained with the
    following procedure:
unknown's avatar
unknown committed
7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848
      get partition id for t.field = const1,   return it
      get partition id for t.field = const1+1, return it
       ...                 t.field = const1+2, ...
       ...                           ...       ...
       ...                 t.field = const2    ...

  IMPLEMENTATION
    See get_partitions_in_range_iter for general description of interval
    analysis. We support walking over the following intervals: 
      "t.field IS NULL" 
      "c1 <=? t.field <=? c2", where c1 and c2 are finite. 
    Intervals with +inf/-inf, and [NULL, c1] interval can be processed but
    that is more tricky and I don't have time to do it right now.
7849

unknown's avatar
unknown committed
7850 7851 7852 7853 7854 7855 7856
  RETURN
    0 - No matching partitions, iterator not initialized
    1 - Some partitions would match, iterator intialized for traversing them
   -1 - All partitions would match, iterator not initialized
*/

int get_part_iter_for_interval_via_walking(partition_info *part_info,
7857 7858 7859 7860 7861 7862
                                      bool is_subpart,
                                      uint32 *store_length_array, /* ignored */
                                      uchar *min_value, uchar *max_value,
                                      uint min_len, uint max_len, /* ignored */
                                      uint flags,
                                      PARTITION_ITERATOR *part_iter)
unknown's avatar
unknown committed
7863 7864 7865 7866
{
  Field *field;
  uint total_parts;
  partition_iter_func get_next_func;
7867 7868 7869 7870 7871
  DBUG_ENTER("get_part_iter_for_interval_via_walking");
  (void)store_length_array;
  (void)min_len;
  (void)max_len;

7872
  part_iter->ret_null_part= part_iter->ret_null_part_orig= FALSE;
unknown's avatar
unknown committed
7873 7874 7875
  if (is_subpart)
  {
    field= part_info->subpart_field_array[0];
7876
    total_parts= part_info->num_subparts;
unknown's avatar
unknown committed
7877 7878 7879 7880 7881
    get_next_func=  get_next_subpartition_via_walking;
  }
  else
  {
    field= part_info->part_field_array[0];
7882
    total_parts= part_info->num_parts;
unknown's avatar
unknown committed
7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898
    get_next_func=  get_next_partition_via_walking;
  }

  /* Handle the "t.field IS NULL" interval, it is a special case */
  if (field->real_maybe_null() && !(flags & (NO_MIN_RANGE | NO_MAX_RANGE)) &&
      *min_value && *max_value)
  {
    /* 
      We don't have a part_iter->get_next() function that would find which
      partition "t.field IS NULL" belongs to, so find partition that contains 
      NULL right here, and return an iterator over singleton set.
    */
    uint32 part_id;
    field->set_null();
    if (is_subpart)
    {
7899 7900 7901
      if (!part_info->get_subpartition_id(part_info, &part_id))
      {
        init_single_partition_iterator(part_id, part_iter);
7902
        DBUG_RETURN(1); /* Ok, iterator initialized */
7903
      }
unknown's avatar
unknown committed
7904 7905 7906
    }
    else
    {
unknown's avatar
unknown committed
7907
      longlong dummy;
7908 7909 7910 7911 7912
      int res= part_info->is_sub_partitioned() ?
                  part_info->get_part_partition_id(part_info, &part_id,
                                                   &dummy):
                  part_info->get_partition_id(part_info, &part_id, &dummy);
      if (!res)
unknown's avatar
unknown committed
7913 7914
      {
        init_single_partition_iterator(part_id, part_iter);
7915
        DBUG_RETURN(1); /* Ok, iterator initialized */
unknown's avatar
unknown committed
7916 7917
      }
    }
7918
    DBUG_RETURN(0); /* No partitions match */
unknown's avatar
unknown committed
7919 7920
  }

7921 7922 7923 7924 7925
  if ((field->real_maybe_null() && 
       ((!(flags & NO_MIN_RANGE) && *min_value) ||  // NULL <? X
        (!(flags & NO_MAX_RANGE) && *max_value))) ||  // X <? NULL
      (flags & (NO_MIN_RANGE | NO_MAX_RANGE)))    // -inf at any bound
  {
7926
    DBUG_RETURN(-1); /* Can't handle this interval, have to use all partitions */
7927
  }
unknown's avatar
unknown committed
7928 7929 7930 7931 7932 7933 7934 7935 7936
  
  /* Get integers for left and right interval bound */
  longlong a, b;
  uint len= field->pack_length_in_rec();
  store_key_image_to_rec(field, min_value, len);
  a= field->val_int();
  
  store_key_image_to_rec(field, max_value, len);
  b= field->val_int();
7937 7938 7939 7940 7941 7942 7943 7944
  
  /* 
    Handle a special case where the distance between interval bounds is 
    exactly 4G-1. This interval is too big for range walking, and if it is an
    (x,y]-type interval then the following "b +=..." code will convert it to 
    an empty interval by "wrapping around" a + 4G-1 + 1 = a. 
  */
  if ((ulonglong)b - (ulonglong)a == ~0ULL)
7945
    DBUG_RETURN(-1);
unknown's avatar
unknown committed
7946 7947 7948

  a += test(flags & NEAR_MIN);
  b += test(!(flags & NEAR_MAX));
7949
  ulonglong n_values= b - a;
7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967

  /*
    Will it pay off to enumerate all values in the [a..b] range and evaluate
    the partitioning function for every value? It depends on 
     1. whether we'll be able to infer that some partitions are not used 
     2. if time savings from not scanning these partitions will be greater
        than time spent in enumeration.
    We will assume that the cost of accessing one extra partition is greater
    than the cost of evaluating the partitioning function O(#partitions).
    This means we should jump at any chance to eliminate a partition, which
    gives us this logic:

    Do the enumeration if
     - the number of values to enumerate is comparable to the number of
       partitions, or
     - there are not many values to enumerate.
  */
  if ((n_values > 2*total_parts) && n_values > MAX_RANGE_TO_WALK)
7968
    DBUG_RETURN(-1);
unknown's avatar
unknown committed
7969

7970
  part_iter->field_vals.start= part_iter->field_vals.cur= a;
7971
  part_iter->field_vals.end=   b;
unknown's avatar
unknown committed
7972 7973
  part_iter->part_info= part_info;
  part_iter->get_next=  get_next_func;
7974
  DBUG_RETURN(1);
unknown's avatar
unknown committed
7975 7976 7977 7978 7979 7980 7981
}


/*
  PARTITION_ITERATOR::get_next implementation: enumerate partitions in range

  SYNOPSIS
7982
    get_next_partition_id_range()
unknown's avatar
unknown committed
7983 7984 7985 7986 7987
      part_iter  Partition set iterator structure

  DESCRIPTION
    This is implementation of PARTITION_ITERATOR::get_next() that returns
    [sub]partition ids in [min_partition_id, max_partition_id] range.
7988
    The function conforms to partition_iter_func type.
unknown's avatar
unknown committed
7989 7990 7991 7992 7993 7994 7995 7996

  RETURN
    partition id
    NOT_A_PARTITION_ID if there are no more partitions
*/

uint32 get_next_partition_id_range(PARTITION_ITERATOR* part_iter)
{
7997
  if (part_iter->part_nums.cur >= part_iter->part_nums.end)
7998
  {
7999 8000 8001 8002 8003
    if (part_iter->ret_null_part)
    {
      part_iter->ret_null_part= FALSE;
      return 0;                    /* NULL always in first range partition */
    }
8004
    part_iter->part_nums.cur= part_iter->part_nums.start;
8005
    part_iter->ret_null_part= part_iter->ret_null_part_orig;
unknown's avatar
unknown committed
8006
    return NOT_A_PARTITION_ID;
8007
  }
unknown's avatar
unknown committed
8008
  else
8009
    return part_iter->part_nums.cur++;
unknown's avatar
unknown committed
8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020
}


/*
  PARTITION_ITERATOR::get_next implementation for LIST partitioning

  SYNOPSIS
    get_next_partition_id_list()
      part_iter  Partition set iterator structure

  DESCRIPTION
8021
    This implementation of PARTITION_ITERATOR::get_next() is special for 
8022
    LIST partitioning: it enumerates partition ids in
8023
    part_info->list_array[i] (list_col_array[i*cols] for COLUMNS LIST
8024
    partitioning) where i runs over [min_idx, max_idx] interval.
8025
    The function conforms to partition_iter_func type.
unknown's avatar
unknown committed
8026 8027 8028 8029 8030 8031 8032 8033

  RETURN 
    partition id
    NOT_A_PARTITION_ID if there are no more partitions
*/

uint32 get_next_partition_id_list(PARTITION_ITERATOR *part_iter)
{
8034
  if (part_iter->part_nums.cur >= part_iter->part_nums.end)
unknown's avatar
unknown committed
8035
  {
8036
    if (part_iter->ret_null_part)
unknown's avatar
unknown committed
8037
    {
8038
      part_iter->ret_null_part= FALSE;
unknown's avatar
unknown committed
8039 8040
      return part_iter->part_info->has_null_part_id;
    }
8041 8042
    part_iter->part_nums.cur= part_iter->part_nums.start;
    part_iter->ret_null_part= part_iter->ret_null_part_orig;
unknown's avatar
unknown committed
8043
    return NOT_A_PARTITION_ID;
unknown's avatar
unknown committed
8044
  }
unknown's avatar
unknown committed
8045
  else
8046 8047 8048
  {
    partition_info *part_info= part_iter->part_info;
    uint32 num_part= part_iter->part_nums.cur++;
8049 8050 8051 8052 8053 8054
    if (part_info->column_list)
    {
      uint num_columns= part_info->part_field_list.elements;
      return part_info->list_col_array[num_part*num_columns].partition_id;
    }
    return part_info->list_array[num_part].partition_id;
8055
  }
unknown's avatar
unknown committed
8056 8057 8058 8059
}


/*
8060
  PARTITION_ITERATOR::get_next implementation: walk over field-space interval
unknown's avatar
unknown committed
8061 8062 8063 8064 8065 8066

  SYNOPSIS
    get_next_partition_via_walking()
      part_iter  Partitioning iterator

  DESCRIPTION
8067 8068 8069
    This implementation of PARTITION_ITERATOR::get_next() returns ids of
    partitions that contain records with partitioning field value within
    [start_val, end_val] interval.
8070
    The function conforms to partition_iter_func type.
unknown's avatar
unknown committed
8071 8072 8073 8074 8075 8076 8077 8078 8079 8080

  RETURN 
    partition id
    NOT_A_PARTITION_ID if there are no more partitioning.
*/

static uint32 get_next_partition_via_walking(PARTITION_ITERATOR *part_iter)
{
  uint32 part_id;
  Field *field= part_iter->part_info->part_field_array[0];
8081
  while (part_iter->field_vals.cur != part_iter->field_vals.end)
unknown's avatar
unknown committed
8082
  {
unknown's avatar
unknown committed
8083
    longlong dummy;
8084 8085
    field->store(part_iter->field_vals.cur++,
                 ((Field_num*)field)->unsigned_flag);
8086
    if ((part_iter->part_info->is_sub_partitioned() &&
8087 8088
         !part_iter->part_info->get_part_partition_id(part_iter->part_info,
                                                      &part_id, &dummy)) ||
8089
        !part_iter->part_info->get_partition_id(part_iter->part_info,
unknown's avatar
unknown committed
8090
                                                &part_id, &dummy))
unknown's avatar
unknown committed
8091 8092
      return part_id;
  }
8093
  part_iter->field_vals.cur= part_iter->field_vals.start;
unknown's avatar
unknown committed
8094 8095 8096 8097 8098 8099 8100 8101 8102
  return NOT_A_PARTITION_ID;
}


/* Same as get_next_partition_via_walking, but for subpartitions */

static uint32 get_next_subpartition_via_walking(PARTITION_ITERATOR *part_iter)
{
  Field *field= part_iter->part_info->subpart_field_array[0];
8103
  uint32 res;
8104 8105 8106
  if (part_iter->field_vals.cur == part_iter->field_vals.end)
  {
    part_iter->field_vals.cur= part_iter->field_vals.start;
unknown's avatar
unknown committed
8107
    return NOT_A_PARTITION_ID;
8108 8109
  }
  field->store(part_iter->field_vals.cur++, FALSE);
8110 8111 8112 8113 8114
  if (part_iter->part_info->get_subpartition_id(part_iter->part_info,
                                                &res))
    return NOT_A_PARTITION_ID;
  return res;

unknown's avatar
unknown committed
8115
}
8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135


/*
  Create partition names

  SYNOPSIS
    create_partition_name()
    out:out                   Created partition name string
    in1                       First part
    in2                       Second part
    name_variant              Normal, temporary or renamed partition name

  RETURN VALUE
    NONE

  DESCRIPTION
    This method is used to calculate the partition name, service routine to
    the del_ren_cre_table method.
*/

8136 8137 8138
void create_partition_name(char *out, const char *in1,
                           const char *in2, uint name_variant,
                           bool translate)
8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177
{
  char transl_part_name[FN_REFLEN];
  const char *transl_part;

  if (translate)
  {
    tablename_to_filename(in2, transl_part_name, FN_REFLEN);
    transl_part= transl_part_name;
  }
  else
    transl_part= in2;
  if (name_variant == NORMAL_PART_NAME)
    strxmov(out, in1, "#P#", transl_part, NullS);
  else if (name_variant == TEMP_PART_NAME)
    strxmov(out, in1, "#P#", transl_part, "#TMP#", NullS);
  else if (name_variant == RENAMED_PART_NAME)
    strxmov(out, in1, "#P#", transl_part, "#REN#", NullS);
}


/*
  Create subpartition name

  SYNOPSIS
    create_subpartition_name()
    out:out                   Created partition name string
    in1                       First part
    in2                       Second part
    in3                       Third part
    name_variant              Normal, temporary or renamed partition name

  RETURN VALUE
    NONE

  DESCRIPTION
  This method is used to calculate the subpartition name, service routine to
  the del_ren_cre_table method.
*/

8178 8179 8180
void create_subpartition_name(char *out, const char *in1,
                              const char *in2, const char *in3,
                              uint name_variant)
8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195
{
  char transl_part_name[FN_REFLEN], transl_subpart_name[FN_REFLEN];

  tablename_to_filename(in2, transl_part_name, FN_REFLEN);
  tablename_to_filename(in3, transl_subpart_name, FN_REFLEN);
  if (name_variant == NORMAL_PART_NAME)
    strxmov(out, in1, "#P#", transl_part_name,
            "#SP#", transl_subpart_name, NullS);
  else if (name_variant == TEMP_PART_NAME)
    strxmov(out, in1, "#P#", transl_part_name,
            "#SP#", transl_subpart_name, "#TMP#", NullS);
  else if (name_variant == RENAMED_PART_NAME)
    strxmov(out, in1, "#P#", transl_part_name,
            "#SP#", transl_subpart_name, "#REN#", NullS);
}
8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207

uint get_partition_field_store_length(Field *field)
{
  uint store_length;

  store_length= field->key_length();
  if (field->real_maybe_null())
    store_length+= HA_KEY_NULL_LENGTH;
  if (field->real_type() == MYSQL_TYPE_VARCHAR)
    store_length+= HA_KEY_BLOB_LENGTH;
  return store_length;
}
8208
#endif