sql_partition.cc 106 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Copyright (C) 2005 MySQL AB

   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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

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

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

/*
  This file was introduced as a container for general functionality related
  to partitioning introduced in MySQL version 5.1. It contains functionality
  used by all handlers that support partitioning, which in the first version
  is the partitioning handler itself and the NDB handler.

unknown's avatar
unknown committed
23
  The first version was written by Mikael Ronstrom.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

  This version supports RANGE partitioning, LIST partitioning, HASH
  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
  called KEY partitioning, where the MySQL server will use an internal
  hash function.
  There are quite a few defaults that can be used as well.
*/

/* Some general useful functions */

#include "mysql_priv.h"
#include <errno.h>
#include <m_ctype.h>
#include "md5.h"

41
#ifdef WITH_PARTITION_STORAGE_ENGINE
unknown's avatar
unknown committed
42
#include "ha_partition.h"
43 44 45
/*
  Partition related functions declarations and some static constants;
*/
46 47 48 49 50 51 52 53 54 55 56 57
static const char *hash_str= "HASH";
static const char *range_str= "RANGE";
static const char *list_str= "LIST";
static const char *part_str= "PARTITION";
static const char *sub_str= "SUB";
static const char *by_str= "BY";
static const char *key_str= "KEY";
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= ",";
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static char buff[22];

bool get_partition_id_list(partition_info *part_info,
                           uint32 *part_id);
bool get_partition_id_range(partition_info *part_info,
                            uint32 *part_id);
bool get_partition_id_hash_nosub(partition_info *part_info,
                                 uint32 *part_id);
bool get_partition_id_key_nosub(partition_info *part_info,
                                uint32 *part_id);
bool get_partition_id_linear_hash_nosub(partition_info *part_info,
                                        uint32 *part_id);
bool get_partition_id_linear_key_nosub(partition_info *part_info,
                                       uint32 *part_id);
bool get_partition_id_range_sub_hash(partition_info *part_info,
                                     uint32 *part_id);
bool get_partition_id_range_sub_key(partition_info *part_info,
                                    uint32 *part_id);
bool get_partition_id_range_sub_linear_hash(partition_info *part_info,
                                            uint32 *part_id);
bool get_partition_id_range_sub_linear_key(partition_info *part_info,
                                           uint32 *part_id);
bool get_partition_id_list_sub_hash(partition_info *part_info,
                                    uint32 *part_id);
bool get_partition_id_list_sub_key(partition_info *part_info,
                                   uint32 *part_id);
bool get_partition_id_list_sub_linear_hash(partition_info *part_info,
                                           uint32 *part_id);
bool get_partition_id_list_sub_linear_key(partition_info *part_info,
                                          uint32 *part_id);
uint32 get_partition_id_hash_sub(partition_info *part_info); 
uint32 get_partition_id_key_sub(partition_info *part_info); 
uint32 get_partition_id_linear_hash_sub(partition_info *part_info); 
uint32 get_partition_id_linear_key_sub(partition_info *part_info); 
unknown's avatar
unknown committed
92 93 94 95 96 97 98 99 100 101 102 103 104
#endif


/*
  A routine used by the parser to decide whether we are specifying a full
  partitioning or if only partitions to add or to split.
  SYNOPSIS
    is_partition_management()
    lex                    Reference to the lex object
  RETURN VALUE
    TRUE                   Yes, it is part of a management partition command
    FALSE                  No, not a management partition command
  DESCRIPTION
105 106
    This needs to be outside of WITH_PARTITION_STORAGE_ENGINE since it is
    used from the sql parser that doesn't have any #ifdef's
unknown's avatar
unknown committed
107 108 109 110 111 112 113 114 115
*/

my_bool is_partition_management(LEX *lex)
{
  return (lex->sql_command == SQLCOM_ALTER_TABLE &&
          (lex->alter_info.flags == ALTER_ADD_PARTITION ||
           lex->alter_info.flags == ALTER_REORGANISE_PARTITION));
}

116
#ifdef WITH_PARTITION_STORAGE_ENGINE
unknown's avatar
unknown committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/*
  A support function to check if a partition name is in a list of strings
  SYNOPSIS
    is_partition_in_list()
    part_name          String searched for
    list_part_names    A list of names searched in
  RETURN VALUES
    TRUE               String found
    FALSE              String not found
*/

bool is_partition_in_list(char *part_name,
                          List<char> list_part_names)
{
  List_iterator<char> part_names_it(list_part_names);
  uint no_names= list_part_names.elements;
  uint i= 0;
  do
  {
    char *list_name= part_names_it++;
    if (!(my_strcasecmp(system_charset_info, part_name, list_name)))
      return TRUE;
  } while (++i < no_names);
  return FALSE;
}


/*
  A support function to check partition names for duplication in a
  partitioned table
  SYNOPSIS
    is_partitions_in_table()
    new_part_info      New partition info
    old_part_info      Old partition info
  RETURN VALUES
    TRUE               Duplicate names found
    FALSE              Duplicate names not found
  DESCRIPTION
    Can handle that the new and old parts are the same in which case it
    checks that the list of names in the partitions doesn't contain any
    duplicated names.
*/

bool is_partitions_in_table(partition_info *new_part_info,
                            partition_info *old_part_info)
{
  uint no_new_parts= new_part_info->partitions.elements, new_count;
  uint no_old_parts= old_part_info->partitions.elements, old_count;
  List_iterator<partition_element> new_parts_it(new_part_info->partitions);
  bool same_part_info= (new_part_info == old_part_info);
  DBUG_ENTER("is_partitions_in_table");

  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)))
      {
        DBUG_RETURN(TRUE);
      }
    } while (old_count < no_old_parts);
  } while (new_count < no_new_parts);
  DBUG_RETURN(FALSE);
}


192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
/*
  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
    list_part_names    The list of partition names that will go away and can be reused in the
                       new table.

  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;
  uint no_new_parts= new_part_info->partitions.elements;
  uint no_old_parts= old_part_info->partitions.elements;
  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)))
      {
        if (!is_partition_in_list(old_name, list_part_names))
          DBUG_RETURN(TRUE);
      }
    } while (old_count < no_old_parts);
  } while (new_count < no_new_parts);
  DBUG_RETURN(FALSE);
}


247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
/*
  A useful routine used by update_row for partition handlers to calculate
  the partition ids of the old and the new record.
  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
    part_field_array        A NULL-terminated array of fields for partition
                            function
    old_part_id             The returned partition id of old record 
    new_part_id             The returned partition id of new record 
  RETURN VALUE
    0                       Success
    > 0                     Error code
  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 ids.
*/

int get_parts_for_update(const byte *old_data, byte *new_data,
                         const byte *rec0, partition_info *part_info,
                         uint32 *old_part_id, uint32 *new_part_id)
{
  Field **part_field_array= part_info->full_part_field_array;
  int error;
  DBUG_ENTER("get_parts_for_update");
  DBUG_ASSERT(new_data == rec0);

  set_field_ptr(part_field_array, old_data, rec0);
  error= part_info->get_partition_id(part_info, old_part_id);
  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
  {
    if (unlikely(error= part_info->get_partition_id(part_info,new_part_id)))
    {
      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);
    error= part_info->get_partition_id(part_info, new_part_id);
    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.
  SYNOPSIS
    get_part_for_delete()
    buf                     Buffer of old record
    rec0                    Reference to table->record[0]
    part_info               Reference to partition information
    part_field_array        A NULL-terminated array of fields for partition
                            function
    part_id                 The returned partition id to delete from
  RETURN VALUE
    0                       Success
    > 0                     Error code
  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.
*/

int get_part_for_delete(const byte *buf, const byte *rec0,
                        partition_info *part_info, uint32 *part_id)
{
  int error;
  DBUG_ENTER("get_part_for_delete");

  if (likely(buf == rec0))
  {
    if (unlikely((error= part_info->get_partition_id(part_info, part_id))))
    {
      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);
    error= part_info->get_partition_id(part_info, part_id);
    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);
}


/*
  This routine allocates an array for all range constants to achieve a fast
  check what partition a certain value belongs to. At the same time it does
  also check that the range constants are defined in increasing order and
  that the expressions are constant integer expressions.
  SYNOPSIS
    check_range_constants()
      part_info
  RETURN VALUE
    TRUE                An error occurred during creation of range constants
    FALSE               Successful creation of range constant mapping
  DESCRIPTION
    This routine is called from check_partition_info to get a quick error
    before we came too far into the CREATE TABLE process. It is also called
    from fix_partition_func every time we open the .frm file. It is only
    called for RANGE PARTITIONed tables.
*/

static bool check_range_constants(partition_info *part_info)
{
  partition_element* part_def;
  longlong current_largest_int= LONGLONG_MIN, part_range_value_int;
  uint no_parts= part_info->no_parts, i;
  List_iterator<partition_element> it(part_info->partitions);
  bool result= TRUE;
  DBUG_ENTER("check_range_constants");
  DBUG_PRINT("enter", ("INT_RESULT with %d parts", no_parts));

  part_info->part_result_type= INT_RESULT;
  part_info->range_int_array= 
                      (longlong*)sql_alloc(no_parts * sizeof(longlong));
  if (unlikely(part_info->range_int_array == NULL))
  {
    my_error(ER_OUTOFMEMORY, MYF(0), no_parts*sizeof(longlong));
    goto end;
  }
  i= 0;
  do
  {
    part_def= it++;
    if ((i != (no_parts - 1)) || !part_info->defined_max_value)
unknown's avatar
unknown committed
407
      part_range_value_int= part_def->range_value; 
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    else
      part_range_value_int= LONGLONG_MAX;
    if (likely(current_largest_int < part_range_value_int))
    {
      current_largest_int= part_range_value_int;
      part_info->range_int_array[i]= part_range_value_int;
    }
    else
    {
      my_error(ER_RANGE_NOT_INCREASING_ERROR, MYF(0));
      goto end;
    }
  } while (++i < no_parts);
  result= FALSE;
end:
  DBUG_RETURN(result);
}


/*
  A support routine for check_list_constants used by qsort to sort the
  constant list expressions.
  SYNOPSIS
    list_part_cmp()
      a                First list constant to compare with
      b                Second list constant to compare with
  RETURN VALUE
    +1                 a > b
    0                  a  == b
    -1                 a < b
*/

static int list_part_cmp(const void* a, const void* b)
{
  longlong a1, b1;
  a1= ((LIST_PART_ENTRY*)a)->list_value;
  b1= ((LIST_PART_ENTRY*)b)->list_value;
  if (a1 < b1)
    return -1;
  else if (a1 > b1)
    return +1;
  else
    return 0;
}


/*
  This routine allocates an array for all list constants to achieve a fast
  check what partition a certain value belongs to. At the same time it does
  also check that there are no duplicates among the list constants and that
  that the list expressions are constant integer expressions.
  SYNOPSIS
    check_list_constants()
      part_info
  RETURN VALUE
    TRUE                  An error occurred during creation of list constants
    FALSE                 Successful creation of list constant mapping
  DESCRIPTION
    This routine is called from check_partition_info to get a quick error
    before we came too far into the CREATE TABLE process. It is also called
    from fix_partition_func every time we open the .frm file. It is only
    called for LIST PARTITIONed tables.
*/

static bool check_list_constants(partition_info *part_info)
{
  uint i, no_list_values= 0, no_parts, list_index= 0;
unknown's avatar
unknown committed
475
  longlong *list_value;
476 477 478 479 480 481 482 483 484 485 486 487 488 489
  bool not_first, result= TRUE;
  longlong curr_value, prev_value;
  partition_element* part_def;
  List_iterator<partition_element> list_func_it(part_info->partitions);
  DBUG_ENTER("check_list_constants");

  part_info->part_result_type= INT_RESULT;

  /*
    We begin by calculating the number of list values that have been
    defined in the first step.

    We use this number to allocate a properly sized array of structs
    to keep the partition id and the value to use in that partition.
unknown's avatar
unknown committed
490
    In the second traversal we assign them values in the struct array.
491 492 493 494 495 496 497 498 499 500 501 502 503

    Finally we sort the array of structs in order of values to enable
    a quick binary search for the proper value to discover the
    partition id.
    After sorting the array we check that there are no duplicates in the
    list.
  */

  no_parts= part_info->no_parts;
  i= 0;
  do
  {
    part_def= list_func_it++;
unknown's avatar
unknown committed
504
    List_iterator<longlong> list_val_it1(part_def->list_val_list);
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
    while (list_val_it1++)
      no_list_values++;
  } while (++i < no_parts);
  list_func_it.rewind();
  part_info->no_list_values= no_list_values;
  part_info->list_array=
      (LIST_PART_ENTRY*)sql_alloc(no_list_values*sizeof(LIST_PART_ENTRY));
  if (unlikely(part_info->list_array == NULL))
  {
    my_error(ER_OUTOFMEMORY, MYF(0), no_list_values*sizeof(LIST_PART_ENTRY));
    goto end;
  }

  i= 0;
  do
  {
    part_def= list_func_it++;
unknown's avatar
unknown committed
522 523
    List_iterator<longlong> list_val_it2(part_def->list_val_list);
    while ((list_value= list_val_it2++))
524
    {
unknown's avatar
unknown committed
525 526
      part_info->list_array[list_index].list_value= *list_value;
      part_info->list_array[list_index++].partition_id= i;
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
    }
  } while (++i < no_parts);

  qsort((void*)part_info->list_array, no_list_values,
        sizeof(LIST_PART_ENTRY), &list_part_cmp);

  not_first= FALSE;
  i= prev_value= 0; //prev_value initialised to quiet compiler
  do
  {
    curr_value= part_info->list_array[i].list_value;
    if (likely(!not_first || prev_value != curr_value))
    {
      prev_value= curr_value;
      not_first= TRUE;
    }
    else
    {
      my_error(ER_MULTIPLE_DEF_CONST_IN_LIST_PART_ERROR, MYF(0));
      goto end;
    }
  } while (++i < no_list_values);
  result= FALSE;
end:
  DBUG_RETURN(result);
}


/*
  Create a memory area where default partition names are stored and fill it
  up with the names.
  SYNOPSIS
    create_default_partition_names()
    no_parts                        Number of partitions
    subpart                         Is it subpartitions
  RETURN VALUE
    A pointer to the memory area of the default partition names
  DESCRIPTION
    A support routine for the partition code where default values are
    generated.
    The external routine needing this code is check_partition_info
*/

#define MAX_PART_NAME_SIZE 8

unknown's avatar
unknown committed
572 573
static char *create_default_partition_names(uint no_parts, uint start_no,
                                            bool subpart)
574 575 576 577 578 579 580 581 582 583
{
  char *ptr= sql_calloc(no_parts*MAX_PART_NAME_SIZE);
  char *move_ptr= ptr;
  uint i= 0;
  DBUG_ENTER("create_default_partition_names");
  if (likely(ptr != 0))
  {
    do
    {
      if (subpart)
unknown's avatar
unknown committed
584
        my_sprintf(move_ptr, (move_ptr,"sp%u", (start_no + i)));
585
      else
unknown's avatar
unknown committed
586
        my_sprintf(move_ptr, (move_ptr,"p%u", (start_no + i)));
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
      move_ptr+=MAX_PART_NAME_SIZE;
    } while (++i < no_parts);
  }
  else
  {
    my_error(ER_OUTOFMEMORY, MYF(0), no_parts*MAX_PART_NAME_SIZE);
  }
  DBUG_RETURN(ptr);
}


/*
  Set up all the default partitions not set-up by the user in the SQL
  statement. Also perform a number of checks that the user hasn't tried
  to use default values where no defaults exists.
  SYNOPSIS
    set_up_default_partitions()
    part_info           The reference to all partition information
    file                A reference to a handler of the table
    max_rows            Maximum number of rows stored in the table
  RETURN VALUE
    TRUE                Error, attempted default values not possible
    FALSE               Ok, default partitions set-up
  DESCRIPTION
    The routine uses the underlying handler of the partitioning to define
    the default number of partitions. For some handlers this requires
    knowledge of the maximum number of rows to be stored in the table.
    This routine only accepts HASH and KEY partitioning and thus there is
    no subpartitioning if this routine is successful.
    The external routine needing this code is check_partition_info
*/

static bool set_up_default_partitions(partition_info *part_info,
unknown's avatar
unknown committed
620 621
                                      handler *file, ulonglong max_rows,
                                      uint start_no)
622 623 624 625 626 627 628 629
{
  uint no_parts, i;
  char *default_name;
  bool result= TRUE;
  DBUG_ENTER("set_up_default_partitions");

  if (part_info->part_type != HASH_PARTITION)
  {
630
    const char *error_string;
631 632 633 634 635 636 637 638 639 640
    if (part_info->part_type == RANGE_PARTITION)
      error_string= range_str;
    else
      error_string= list_str;
    my_error(ER_PARTITIONS_MUST_BE_DEFINED_ERROR, MYF(0), error_string);
    goto end;
  }
  if (part_info->no_parts == 0)
    part_info->no_parts= file->get_default_no_partitions(max_rows);
  no_parts= part_info->no_parts;
unknown's avatar
unknown committed
641
  part_info->use_default_partitions= FALSE;
642 643 644 645 646 647
  if (unlikely(no_parts > MAX_PARTITIONS))
  {
    my_error(ER_TOO_MANY_PARTITIONS_ERROR, MYF(0));
    goto end;
  }
  if (unlikely((!(default_name= create_default_partition_names(no_parts,
unknown's avatar
unknown committed
648
                                                               start_no,
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
                                                               FALSE)))))
    goto end;
  i= 0;
  do
  {
    partition_element *part_elem= new partition_element();
    if (likely(part_elem != 0))
    {
      part_elem->engine_type= DB_TYPE_UNKNOWN;
      part_elem->partition_name= default_name;
      default_name+=MAX_PART_NAME_SIZE;
      part_info->partitions.push_back(part_elem);
    }
    else
    {
      my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_element));
      goto end;
    }
  } while (++i < no_parts);
  result= FALSE;
end:
  DBUG_RETURN(result);
}


/*
  Set up all the default subpartitions not set-up by the user in the SQL
  statement. Also perform a number of checks that the default partitioning
  becomes an allowed partitioning scheme.
  SYNOPSIS
    set_up_default_subpartitions()
    part_info           The reference to all partition information
    file                A reference to a handler of the table
    max_rows            Maximum number of rows stored in the table
  RETURN VALUE
    TRUE                Error, attempted default values not possible
    FALSE               Ok, default partitions set-up
  DESCRIPTION
    The routine uses the underlying handler of the partitioning to define
    the default number of partitions. For some handlers this requires
    knowledge of the maximum number of rows to be stored in the table.
    This routine is only called for RANGE or LIST partitioning and those
    need to be specified so only subpartitions are specified.
    The external routine needing this code is check_partition_info
*/

static bool set_up_default_subpartitions(partition_info *part_info,
                                         handler *file, ulonglong max_rows)
{
unknown's avatar
unknown committed
698 699
  uint i, j, no_parts, no_subparts;
  char *default_name, *name_ptr;
700 701 702 703 704 705 706 707 708
  bool result= TRUE;
  partition_element *part_elem;
  List_iterator<partition_element> part_it(part_info->partitions);
  DBUG_ENTER("set_up_default_subpartitions");

  if (part_info->no_subparts == 0)
    part_info->no_subparts= file->get_default_no_partitions(max_rows);
  no_parts= part_info->no_parts;
  no_subparts= part_info->no_subparts;
unknown's avatar
unknown committed
709
  part_info->use_default_subpartitions= FALSE;
710 711 712 713 714 715
  if (unlikely((no_parts * no_subparts) > MAX_PARTITIONS))
  {
    my_error(ER_TOO_MANY_PARTITIONS_ERROR, MYF(0));
    goto end;
  }
  if (unlikely((!(default_name=
unknown's avatar
unknown committed
716
             create_default_partition_names(no_subparts, (uint)0, TRUE)))))
717 718 719 720 721
    goto end;
  i= 0;
  do
  {
    part_elem= part_it++;
unknown's avatar
unknown committed
722 723
    j= 0;
    name_ptr= default_name;
724 725 726 727 728 729
    do
    {
      partition_element *subpart_elem= new partition_element();
      if (likely(subpart_elem != 0))
      {
        subpart_elem->engine_type= DB_TYPE_UNKNOWN;
unknown's avatar
unknown committed
730 731
        subpart_elem->partition_name= name_ptr;
        name_ptr+= MAX_PART_NAME_SIZE;
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
        part_elem->subpartitions.push_back(subpart_elem);
      }
      else
      {
        my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_element));
        goto end;
      }
    } while (++j < no_subparts);
  } while (++i < no_parts);
  result= FALSE;
end:
  DBUG_RETURN(result);
}


/*
  Set up defaults for partition or subpartition (cannot set-up for both,
  this will return an error.
  SYNOPSIS
    set_up_defaults_for_partitioning()
    part_info           The reference to all partition information
    file                A reference to a handler of the table
    max_rows            Maximum number of rows stored in the table
  RETURN VALUE
    TRUE                Error, attempted default values not possible
    FALSE               Ok, default partitions set-up
  DESCRIPTION
    Support routine for check_partition_info
*/

unknown's avatar
unknown committed
762 763 764
bool set_up_defaults_for_partitioning(partition_info *part_info,
                                      handler *file,
                                      ulonglong max_rows, uint start_no)
765 766 767 768
{
  DBUG_ENTER("set_up_defaults_for_partitioning");

  if (part_info->use_default_partitions)
unknown's avatar
unknown committed
769 770
    DBUG_RETURN(set_up_default_partitions(part_info, file, max_rows,
                                          start_no));
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
  if (is_sub_partitioned(part_info) && part_info->use_default_subpartitions)
    DBUG_RETURN(set_up_default_subpartitions(part_info, file, max_rows));
  DBUG_RETURN(FALSE);
}


/*
  Check that all partitions use the same storage engine.
  This is currently a limitation in this version.
  SYNOPSIS
    check_engine_mix()
    engine_array           An array of engine identifiers
    no_parts               Total number of partitions
  RETURN VALUE
    TRUE                   Error, mixed engines
    FALSE                  Ok, no mixed engines
*/

static bool check_engine_mix(u_char *engine_array, uint no_parts)
{
  /*
    Current check verifies only that all handlers are the same.
    Later this check will be more sophisticated.
  */
  uint i= 0;
  bool result= FALSE;
  DBUG_ENTER("check_engine_mix");

  do
  {
    if (engine_array[i] != engine_array[0])
    {
      result= TRUE;
      break;
    }
  } while (++i < no_parts);
  DBUG_RETURN(result);
}


/*
  We will check that the partition info requested is possible to set-up in
  this version. This routine is an extension of the parser one could say.
  If defaults were used we will generate default data structures for all
  partitions.
  SYNOPSIS
    check_partition_info()
    part_info           The reference to all partition information
    db_type             Default storage engine if no engine specified per
                        partition.
    file                A reference to a handler of the table
    max_rows            Maximum number of rows stored in the table
  RETURN VALUE
    TRUE                 Error, something went wrong
    FALSE                Ok, full partition data structures are now generated
  DESCRIPTION
    This code is used early in the CREATE TABLE and ALTER TABLE process.
*/

bool check_partition_info(partition_info *part_info,enum db_type eng_type,
                          handler *file, ulonglong max_rows)
{
  u_char *engine_array= NULL;
  uint part_count= 0, i, no_parts, tot_partitions;
  bool result= TRUE;
  List_iterator<partition_element> part_it(part_info->partitions);
  DBUG_ENTER("check_partition_info");

  if (unlikely(is_sub_partitioned(part_info) &&
              (!(part_info->part_type == RANGE_PARTITION ||
                 part_info->part_type == LIST_PARTITION))))
  {
    /* Only RANGE and LIST partitioning can be subpartitioned */
    my_error(ER_SUBPARTITION_ERROR, MYF(0));
    goto end;
  }
unknown's avatar
unknown committed
847 848
  if (unlikely(set_up_defaults_for_partitioning(part_info, file,
                                                max_rows, (uint)0)))
849 850 851 852 853 854 855
    goto end;
  tot_partitions= get_tot_partitions(part_info);
  if (unlikely(tot_partitions > MAX_PARTITIONS))
  {
    my_error(ER_TOO_MANY_PARTITIONS_ERROR, MYF(0));
    goto end;
  }
unknown's avatar
unknown committed
856 857 858 859 860
  if (unlikely(is_partitions_in_table(part_info, part_info)))
  {
    my_error(ER_SAME_NAME_PARTITION, MYF(0));
    goto end;
  }
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 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 949 950 951 952 953 954 955 956 957 958 959
  engine_array= (u_char*)my_malloc(tot_partitions, MYF(MY_WME));
  if (unlikely(!engine_array))
    goto end;
  i= 0;
  no_parts= part_info->no_parts;
  do
  {
    partition_element *part_elem= part_it++;
    if (!is_sub_partitioned(part_info))
    {
      if (part_elem->engine_type == DB_TYPE_UNKNOWN)
        part_elem->engine_type= eng_type;
      DBUG_PRINT("info", ("engine = %u",(uint)part_elem->engine_type));
      engine_array[part_count++]= (u_char)part_elem->engine_type;
    }
    else
    {
      uint j= 0, no_subparts= part_info->no_subparts;;
      List_iterator<partition_element> sub_it(part_elem->subpartitions);
      do
      {
        part_elem= sub_it++;
        if (part_elem->engine_type == DB_TYPE_UNKNOWN)
          part_elem->engine_type= eng_type;
        DBUG_PRINT("info", ("engine = %u",(uint)part_elem->engine_type));
        engine_array[part_count++]= (u_char)part_elem->engine_type;
      } while (++j < no_subparts);
    }
  } while (++i < part_info->no_parts);
  if (unlikely(check_engine_mix(engine_array, part_count)))
  {
    my_error(ER_MIX_HANDLER_ERROR, MYF(0));
    goto end;
  }

  /*
    We need to check all constant expressions that they are of the correct
    type and that they are increasing for ranges and not overlapping for
    list constants.
  */

  if (unlikely((part_info->part_type == RANGE_PARTITION &&
                check_range_constants(part_info)) ||
               (part_info->part_type == LIST_PARTITION &&
                check_list_constants(part_info))))
    goto end;
  result= FALSE;
end:
  my_free((char*)engine_array,MYF(MY_ALLOW_ZERO_PTR));
  DBUG_RETURN(result);
}


/*
  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) No NULLable fields in partition function
  2) All fields of partition function in Primary keys and unique indexes
     (if not supported)
  3) No fields in partition function that are BLOB's or VARCHAR with a
     collation other than the binary collation.



  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.
  SYNOPSIS
    set_up_field_array()
    table                TABLE object for which partition fields are set-up
    sub_part             Is the table subpartitioned as well
  RETURN VALUE
    TRUE                 Error, some field didn't meet requirements
    FALSE                Ok, partition field array set-up
  DESCRIPTION
    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.
*/
static bool set_up_field_array(TABLE *table,
                              bool sub_part)
{
  Field **ptr, *field, **field_array;
  uint no_fields= 0, size_field_array, i= 0;
unknown's avatar
unknown committed
960
  partition_info *part_info= table->part_info;
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 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 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
  int result= FALSE;
  DBUG_ENTER("set_up_field_array");

  ptr= table->field;
  while ((field= *(ptr++))) 
  {
    if (field->flags & GET_FIXED_FIELDS_FLAG)
      no_fields++;
  }
  size_field_array= (no_fields+1)*sizeof(Field*);
  field_array= (Field**)sql_alloc(size_field_array);
  if (unlikely(!field_array))
  {
    my_error(ER_OUTOFMEMORY, MYF(0), size_field_array);
    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))
      {
        field_array[i++]= field;

        /*
          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.
          2) Not be a VARCHAR other than VARCHAR with a binary collation
            A VARCHAR with character sets can have several values being
            equal with different number of spaces or NULL's. This is not a
            good ground for a safe and exact partition function. Thus it is
            not allowed in partition functions.
        */

        if (unlikely(field->flags & BLOB_FLAG))
        {
          my_error(ER_BLOB_FIELD_IN_PART_FUNC_ERROR, MYF(0));
          result= TRUE;
        }
        else if (unlikely((!field->flags & BINARY_FLAG) &&
                          field->real_type() == MYSQL_TYPE_VARCHAR))
        {
          my_error(ER_CHAR_SET_IN_PART_FIELD_ERROR, MYF(0));
          result= TRUE;
        }
      }
    }
  }
  field_array[no_fields]= 0;
  if (!sub_part)
  {
    part_info->part_field_array= field_array;
    part_info->no_part_fields= no_fields;
  }
  else
  {
    part_info->subpart_field_array= field_array;
    part_info->no_subpart_fields= no_fields;
  }
  DBUG_RETURN(result);
}


/*
  Create a field array including all fields of both the partitioning and the
  subpartitioning functions.
  SYNOPSIS
    create_full_part_field_array()
    table                TABLE object for which partition fields are set-up
    part_info            Reference to partitioning data structure
  RETURN VALUE
    TRUE                 Memory allocation of field array failed
    FALSE                Ok
  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
*/

static bool create_full_part_field_array(TABLE *table,
                                         partition_info *part_info)
{
  bool result= FALSE;
  DBUG_ENTER("create_full_part_field_array");

  if (!is_sub_partitioned(part_info))
  {
    part_info->full_part_field_array= part_info->part_field_array;
    part_info->no_full_part_fields= part_info->no_part_fields;
  }
  else
  {
    Field **ptr, *field, **field_array;
    uint no_part_fields=0, size_field_array;
    ptr= table->field;
    while ((field= *(ptr++)))
    {
      if (field->flags & FIELD_IN_PART_FUNC_FLAG)
        no_part_fields++;
    }
    size_field_array= (no_part_fields+1)*sizeof(Field*);
    field_array= (Field**)sql_alloc(size_field_array);
    if (unlikely(!field_array))
    {
      my_error(ER_OUTOFMEMORY, MYF(0), size_field_array);
      result= TRUE;
      goto end;
    }
    no_part_fields= 0;
    ptr= table->field;
    while ((field= *(ptr++)))
    {
      if (field->flags & FIELD_IN_PART_FUNC_FLAG)
        field_array[no_part_fields++]= field;
    }
    field_array[no_part_fields]=0;
    part_info->full_part_field_array= field_array;
    part_info->no_full_part_fields= no_part_fields;
  }
end:
  DBUG_RETURN(result);
}


/*
  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.



  Clear flag GET_FIXED_FIELDS_FLAG in all fields of a key previously set by
  set_indicator_in_key_fields (always used in pairs).
  SYNOPSIS
    clear_indicator_in_key_fields()
    key_info                  Reference to find the key fields
*/

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.
  SYNOPSIS
    set_indicator_in_key_fields
    key_info                  Reference to find the key fields
*/

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.
  SYNOPSIS
    check_fields_in_PF()
    ptr                  Partition field array
    all_fields           Is all fields of partition field array used in key
    some_fields          Is some fields of partition field array used in key
  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");
  *all_fields= TRUE;
  *some_fields= FALSE;
  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.
  SYNOPSIS
    clear_field_flag()
    table                TABLE object for which partition fields are set-up
*/

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;
}


/*
  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.
  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
  RETURN VALUE
    TRUE                 Fields in list of fields not part of table
    FALSE                All fields ok and array created
  DESCRIPTION
    find_field_in_table_sef finds the field given its name. All fields get
    GET_FIXED_FIELDS_FLAG set.
*/

static bool handle_list_of_fields(List_iterator<char> it,
                                  TABLE *table,
                                  partition_info *part_info,
                                  bool sub_part)
{
  Field *field;
  bool result;
  char *field_name;
  DBUG_ENTER("handle_list_of_fields");

  while ((field_name= it++))
  {
    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;
    }
  }
  result= set_up_field_array(table, sub_part);
end:
  DBUG_RETURN(result);
}


/*
  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.
  SYNOPSIS
    fix_fields_part_func()
    thd                  The thread object
    tables               A list of one table, the partitioned table
    func_expr            The item tree reference of the partition function
    part_info            Reference to partitioning data structure
    sub_part             Is the table subpartitioned as well
  RETURN VALUE
    TRUE                 An error occurred, something was wrong with the
                         partition function.
    FALSE                Ok, a partition field array was created
  DESCRIPTION
    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.
*/

static bool fix_fields_part_func(THD *thd, TABLE_LIST *tables,
                                 Item* func_expr, partition_info *part_info,
                                 bool sub_part)
{
  /*
    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.
  */

  bool result= TRUE;
  TABLE *table= tables->table;
unknown's avatar
unknown committed
1271
  TABLE_LIST *save_table_list, *save_first_table, *save_last_table;
1272
  int error;
unknown's avatar
unknown committed
1273
  Name_resolution_context *context;
1274 1275
  DBUG_ENTER("fix_fields_part_func");

unknown's avatar
unknown committed
1276
  context= thd->lex->current_context();
1277 1278
  table->map= 1; //To ensure correct calculation of const item
  table->get_fields_in_item_tree= TRUE;
unknown's avatar
unknown committed
1279 1280 1281
  save_table_list= context->table_list;
  save_first_table= context->first_name_resolution_table;
  save_last_table= context->last_name_resolution_table;
1282
  context->table_list= tables;
unknown's avatar
unknown committed
1283 1284 1285
  context->first_name_resolution_table= tables;
  context->last_name_resolution_table= NULL;
  func_expr->walk(&Item::change_context_processor, (byte*) context);
1286 1287
  thd->where= "partition function";
  error= func_expr->fix_fields(thd, (Item**)0);
unknown's avatar
unknown committed
1288 1289 1290
  context->table_list= save_table_list;
  context->first_name_resolution_table= save_first_table;
  context->last_name_resolution_table= save_last_table;
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
  if (unlikely(error))
  {
    DBUG_PRINT("info", ("Field in partition function not part of table"));
    clear_field_flag(table);
    goto end;
  }
  if (unlikely(func_expr->const_item()))
  {
    my_error(ER_CONST_EXPR_IN_PARTITION_FUNC_ERROR, MYF(0));
    clear_field_flag(table);
    goto end;
  }
  result= set_up_field_array(table, sub_part);
end:
  table->get_fields_in_item_tree= FALSE;
  table->map= 0; //Restore old value
  DBUG_RETURN(result);
}


/*
  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.
  SYNOPSIS
    check_primary_key()
    table                TABLE object for which partition fields are set-up
  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
*/

static bool check_primary_key(TABLE *table)
{
  uint primary_key= table->s->primary_key;
  bool all_fields, some_fields, result= FALSE;
  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
1335
    check_fields_in_PF(table->part_info->full_part_field_array,
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
                        &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);
}


/*
  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.
  SYNOPSIS
    check_unique_keys()
    table                TABLE object for which partition fields are set-up
  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
*/

static bool check_unique_keys(TABLE *table)
{
  bool all_fields, some_fields, result= FALSE;
  uint keys= table->s->keys, i;
  DBUG_ENTER("check_unique_keys");
  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
1373
      check_fields_in_PF(table->part_info->full_part_field_array,
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
                         &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.
  SYNOPSIS
    check_range_capable_PF()
    table                TABLE object for which partition fields are set-up
  DESCRIPTION
    Support for this is not implemented yet.
*/

void check_range_capable_PF(TABLE *table)
{
  DBUG_ENTER("check_range_capable_PF");
  DBUG_VOID_RETURN;
}


/*
  Set up partition key maps
  SYNOPSIS
    set_up_partition_key_maps()
    table                TABLE object for which partition fields are set-up
    part_info            Reference to partitioning data structure
  RETURN VALUES
    None
  DESCRIPTION
  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
*/

static void set_up_partition_key_maps(TABLE *table,
                                      partition_info *part_info)
{
  uint keys= table->s->keys, i;
  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);
    if (is_sub_partitioned(part_info))
    {
      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;
}


/*
  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.
  SYNOPSIS
    set_up_partition_function_pointers()
    part_info            Reference to partitioning data structure
*/

static void set_up_partition_func_pointers(partition_info *part_info)
{
  if (is_sub_partitioned(part_info))
  {
    if (part_info->part_type == RANGE_PARTITION)
    {
      part_info->get_part_partition_id= get_partition_id_range;
      if (part_info->list_of_subpart_fields)
      {
        if (part_info->linear_hash_ind)
        {
          part_info->get_partition_id= get_partition_id_range_sub_linear_key;
          part_info->get_subpartition_id= get_partition_id_linear_key_sub;
        }
        else
        {
          part_info->get_partition_id= get_partition_id_range_sub_key;
          part_info->get_subpartition_id= get_partition_id_key_sub;
        }
      }
      else
      {
        if (part_info->linear_hash_ind)
        {
          part_info->get_partition_id= get_partition_id_range_sub_linear_hash;
          part_info->get_subpartition_id= get_partition_id_linear_hash_sub;
        }
        else
        {
          part_info->get_partition_id= get_partition_id_range_sub_hash;
          part_info->get_subpartition_id= get_partition_id_hash_sub;
        }
      }
    }
    else //LIST Partitioning
    {
      part_info->get_part_partition_id= get_partition_id_list;
      if (part_info->list_of_subpart_fields)
      {
        if (part_info->linear_hash_ind)
        {
          part_info->get_partition_id= get_partition_id_list_sub_linear_key;
          part_info->get_subpartition_id= get_partition_id_linear_key_sub;
        }
        else
        {
          part_info->get_partition_id= get_partition_id_list_sub_key;
          part_info->get_subpartition_id= get_partition_id_key_sub;
        }
      }
      else
      {
        if (part_info->linear_hash_ind)
        {
          part_info->get_partition_id= get_partition_id_list_sub_linear_hash;
          part_info->get_subpartition_id= get_partition_id_linear_hash_sub;
        }
        else
        {
          part_info->get_partition_id= get_partition_id_list_sub_hash;
          part_info->get_subpartition_id= get_partition_id_hash_sub;
        }
      }
    }
  }
  else //No subpartitioning
  {
    part_info->get_part_partition_id= NULL;
    part_info->get_subpartition_id= NULL;
    if (part_info->part_type == RANGE_PARTITION)
      part_info->get_partition_id= get_partition_id_range;
    else if (part_info->part_type == LIST_PARTITION)
      part_info->get_partition_id= get_partition_id_list;
    else //HASH partitioning
    {
      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;
      }
    }
  }
}
          
        
/*
  For linear hashing we need a mask which is on the form 2**n - 1 where
  2**n >= no_parts. Thus if no_parts is 6 then mask is 2**3 - 1 = 8 - 1 = 7.
  SYNOPSIS
    set_linear_hash_mask()
    part_info            Reference to partitioning data structure
    no_parts             Number of parts in linear hash partitioning
*/

static void set_linear_hash_mask(partition_info *part_info, uint no_parts)
{
  uint mask;
  for (mask= 1; mask < no_parts; mask<<=1)
    ;
  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.
  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
    no_parts            Number of partitions in HASH partitioned part
  RETURN VALUE
    part_id             The calculated partition identity (starting at 0)
  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,
                                           uint no_parts)
{
  uint32 part_id= (uint32)(hash_value & mask);
  if (part_id >= no_parts)
  {
    uint new_mask= ((mask + 1) >> 1) - 1;
    part_id= hash_value & new_mask;
  }
  return part_id;
}

/*
unknown's avatar
unknown committed
1655 1656
  fix partition functions

1657 1658 1659 1660 1661
  SYNOPSIS
    fix_partition_func()
    thd                  The thread object
    name                 The name of the partitioned table
    table                TABLE object for which partition fields are set-up
unknown's avatar
unknown committed
1662

1663 1664 1665
  RETURN VALUE
    TRUE
    FALSE
unknown's avatar
unknown committed
1666

1667 1668 1669 1670
  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
1671 1672 1673 1674 1675 1676 1677

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.
1678 1679
*/

unknown's avatar
unknown committed
1680
bool fix_partition_func(THD *thd, const char *name, TABLE *table)
1681 1682 1683 1684 1685 1686 1687
{
  bool result= TRUE;
  uint dir_length, home_dir_length;
  TABLE_LIST tables;
  TABLE_SHARE *share= table->s;
  char db_name_string[FN_REFLEN];
  char* db_name;
unknown's avatar
unknown committed
1688
  partition_info *part_info= table->part_info;
1689 1690 1691 1692 1693
  ulong save_set_query_id= thd->set_query_id;
  DBUG_ENTER("fix_partition_func");

  thd->set_query_id= 0;
  /*
unknown's avatar
unknown committed
1694 1695 1696
    Set-up the TABLE_LIST object to be a list with a single table
    Set the object to zero to create NULL pointers and set alias
    and real name to table name and get database name from file name.
1697 1698 1699
  */

  bzero((void*)&tables, sizeof(TABLE_LIST));
unknown's avatar
unknown committed
1700
  tables.alias= tables.table_name= (char*) share->table_name.str;
1701
  tables.table= table;
unknown's avatar
unknown committed
1702 1703
  tables.next_local= 0;
  tables.next_name_resolution_table= 0;
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
  strmov(db_name_string, name);
  dir_length= dirname_length(db_name_string);
  db_name_string[dir_length - 1]= 0;
  home_dir_length= dirname_length(db_name_string);
  db_name= &db_name_string[home_dir_length];
  tables.db= db_name;

  if (is_sub_partitioned(part_info))
  {
    DBUG_ASSERT(part_info->subpart_type == HASH_PARTITION);
    /*
unknown's avatar
unknown committed
1715 1716
      Subpartition is defined. We need to verify that subpartitioning
      function is correct.
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
    */
    if (part_info->linear_hash_ind)
      set_linear_hash_mask(part_info, part_info->no_subparts);
    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
    {
      if (unlikely(fix_fields_part_func(thd, &tables,
unknown's avatar
unknown committed
1729 1730
                                        part_info->subpart_expr, part_info,
                                        TRUE)))
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
        goto end;
      if (unlikely(part_info->subpart_expr->result_type() != INT_RESULT))
      {
        my_error(ER_PARTITION_FUNC_NOT_ALLOWED_ERROR, MYF(0),
                 "SUBPARTITION");
        goto end;
      }
    }
  }
  DBUG_ASSERT(part_info->part_type != NOT_A_PARTITION);
  /*
unknown's avatar
unknown committed
1742 1743
    Partition is defined. We need to verify that partitioning
    function is correct.
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
  */
  if (part_info->part_type == HASH_PARTITION)
  {
    if (part_info->linear_hash_ind)
      set_linear_hash_mask(part_info, part_info->no_parts);
    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
    {
      if (unlikely(fix_fields_part_func(thd, &tables, part_info->part_expr,
                                        part_info, FALSE)))
        goto end;
      if (unlikely(part_info->part_expr->result_type() != INT_RESULT))
      {
        my_error(ER_PARTITION_FUNC_NOT_ALLOWED_ERROR, MYF(0), part_str);
        goto end;
      }
      part_info->part_result_type= INT_RESULT;
    }
  }
  else
  {
1770
    const char *error_str;
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
    if (part_info->part_type == RANGE_PARTITION)
    {
      error_str= range_str; 
      if (unlikely(check_range_constants(part_info)))
        goto end;
    }
    else if (part_info->part_type == LIST_PARTITION)
    {
      error_str= list_str; 
      if (unlikely(check_list_constants(part_info)))
        goto end;
    }
    else
    {
      DBUG_ASSERT(0);
      my_error(ER_INCONSISTENT_PARTITION_INFO_ERROR, MYF(0));
      goto end;
    }
    if (unlikely(part_info->no_parts < 1))
    {
      my_error(ER_PARTITIONS_MUST_BE_DEFINED_ERROR, MYF(0), error_str);
      goto end;
    }
    if (unlikely(fix_fields_part_func(thd, &tables, part_info->part_expr,
                                      part_info, FALSE)))
      goto end;
    if (unlikely(part_info->part_expr->result_type() != INT_RESULT))
    {
      my_error(ER_PARTITION_FUNC_NOT_ALLOWED_ERROR, MYF(0), part_str);
      goto end;
    }
  }
  if (unlikely(create_full_part_field_array(table, part_info)))
    goto end;
  if (unlikely(check_primary_key(table)))
    goto end;
  if (unlikely((!table->file->partition_flags() & HA_CAN_PARTITION_UNIQUE) &&
               check_unique_keys(table)))
    goto end;
  check_range_capable_PF(table);
  set_up_partition_key_maps(table, part_info);
  set_up_partition_func_pointers(part_info);
  result= FALSE;
end:
  thd->set_query_id= save_set_query_id;
  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)
{
1830
  uint len_written= my_write(fptr, (const byte*)buf, len, MYF(0));
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 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 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
  if (likely(len == len_written))
    return 0;
  else
    return 1;
}

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);
  return err + add_begin_parenthesis(fptr);
}

static int add_hash(File fptr)
{
  return add_part_key_word(fptr, hash_str);
}

static int add_partition(File fptr)
{
  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);
  return err + add_partition(fptr);
}

static int add_partition_by(File fptr)
{
  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);
  return err + add_partition_by(fptr);
}

static int add_key_partition(File fptr, List<char> field_list)
{
  uint i, no_fields;
  int err;
  List_iterator<char> part_it(field_list);
  err= add_part_key_word(fptr, key_str);
  no_fields= field_list.elements;
  i= 0;
  do
  {
    const char *field_str= part_it++;
    err+= add_string(fptr, field_str);
    if (i != (no_fields-1))
      err+= add_comma(fptr);
  } while (++i < no_fields);
  return err;
}

static int add_int(File fptr, longlong number)
{
  llstr(number, buff);
  return add_string(fptr, buff);
}

static int add_keyword_string(File fptr, const char *keyword,
                              const char *keystr)
{
  int err= add_string(fptr, keyword);
  err+= add_space(fptr);
  err+= add_equal(fptr);
  err+= add_space(fptr);
  err+= add_string(fptr, keystr);
  return err + add_space(fptr);
}

static int add_keyword_int(File fptr, const char *keyword, longlong num)
{
  int err= add_string(fptr, keyword);
  err+= add_space(fptr);
  err+= add_equal(fptr);
  err+= add_space(fptr);
  err+= add_int(fptr, num);
  return err + add_space(fptr);
}

static int add_engine(File fptr, enum db_type engine_type)
{
  const char *engine_str= ha_get_storage_engine(engine_type);
  int err= add_string(fptr, "ENGINE = ");
  return err + add_string(fptr, engine_str);
  return err;
}

static int add_partition_options(File fptr, partition_element *p_elem)
{
  int err= 0;
  if (p_elem->tablespace_name)
    err+= add_keyword_string(fptr,"TABLESPACE",p_elem->tablespace_name);
  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);
  if (p_elem->data_file_name)
    err+= add_keyword_string(fptr,"DATA DIRECTORY",p_elem->data_file_name);
  if (p_elem->index_file_name)
    err+= add_keyword_string(fptr,"INDEX DIRECTORY",p_elem->index_file_name);
  if (p_elem->part_comment)
    err+= add_keyword_string(fptr, "COMMENT",p_elem->part_comment);
  return err + add_engine(fptr,p_elem->engine_type);
}

static int add_partition_values(File fptr, partition_info *part_info,
                         partition_element *p_elem)
{
  int err= 0;
  if (part_info->part_type == RANGE_PARTITION)
  {
    err+= add_string(fptr, "VALUES LESS THAN ");
unknown's avatar
unknown committed
1988
    if (p_elem->range_value != LONGLONG_MAX)
1989 1990
    {
      err+= add_begin_parenthesis(fptr);
unknown's avatar
unknown committed
1991
      err+= add_int(fptr, p_elem->range_value);
1992 1993 1994 1995 1996 1997 1998 1999
      err+= add_end_parenthesis(fptr);
    }
    else
      err+= add_string(fptr, "MAXVALUE");
  }
  else if (part_info->part_type == LIST_PARTITION)
  {
    uint i;
unknown's avatar
unknown committed
2000
    List_iterator<longlong> list_val_it(p_elem->list_val_list);
2001
    err+= add_string(fptr, "VALUES IN ");
unknown's avatar
unknown committed
2002
    uint no_items= p_elem->list_val_list.elements;
2003 2004 2005 2006
    err+= add_begin_parenthesis(fptr);
    i= 0;
    do
    {
unknown's avatar
unknown committed
2007 2008
      longlong *list_value= list_val_it++;
      err+= add_int(fptr, *list_value);
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
      if (i != (no_items-1))
        err+= add_comma(fptr);
    } while (++i < no_items);
    err+= add_end_parenthesis(fptr);
  }
  return err + add_space(fptr);
}

/*
  Generate the partition syntax from the partition data structure.
  Useful for support of generating defaults, SHOW CREATE TABLES
  and easy partition management.
  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
2027
    add_default_info           Add info generated by default
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
  RETURN VALUES
    NULL error
    buf, buf_length            Buffer and its length
  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,
2053 2054
                                bool use_sql_alloc,
				bool add_default_info)
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
{
  uint i,j, no_parts, no_subparts;
  partition_element *part_elem;
  ulonglong buffer_length;
  char path[FN_REFLEN];
  int err= 0;
  DBUG_ENTER("generate_partition_syntax");
  File fptr;
  char *buf= NULL; //Return buffer
  const char *file_name;
unknown's avatar
unknown committed
2065

2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
  sprintf(path, "%s_%lx_%lx", "part_syntax", current_pid,
          current_thd->thread_id);
  fn_format(path,path,mysql_tmpdir,".psy", MY_REPLACE_EXT);
  file_name= &path[0];
  DBUG_PRINT("info", ("File name = %s", file_name));
  if (unlikely(((fptr= my_open(file_name,O_CREAT|O_RDWR, MYF(MY_WME))) == -1)))
    DBUG_RETURN(NULL);
#if defined(MSDOS) || defined(__WIN__) || defined(__EMX__) || defined(OS2)
#else
  my_delete(file_name, MYF(0));
#endif
  err+= add_space(fptr);
  err+= add_partition_by(fptr);
  switch (part_info->part_type)
  {
    case RANGE_PARTITION:
2082
      add_default_info= TRUE;
2083 2084 2085
      err+= add_part_key_word(fptr, range_str);
      break;
    case LIST_PARTITION:
2086
      add_default_info= TRUE;
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
      err+= add_part_key_word(fptr, list_str);
      break;
    case HASH_PARTITION:
      if (part_info->linear_hash_ind)
        err+= add_string(fptr, "LINEAR ");
      if (part_info->list_of_part_fields)
        err+= add_key_partition(fptr, part_info->part_field_list);
      else
        err+= add_hash(fptr);
      break;
    default:
      DBUG_ASSERT(0);
      /* We really shouldn't get here, no use in continuing from here */
      current_thd->fatal_error();
      DBUG_RETURN(NULL);
  }
  if (part_info->part_expr)
    err+= add_string_len(fptr, part_info->part_func_string,
                         part_info->part_func_len);
  err+= add_end_parenthesis(fptr);
  err+= add_space(fptr);
  if (is_sub_partitioned(part_info))
  {
    err+= add_subpartition_by(fptr);
    /* Must be hash partitioning for subpartitioning */
    if (part_info->list_of_subpart_fields)
      err+= add_key_partition(fptr, part_info->subpart_field_list);
    else
      err+= add_hash(fptr);
    if (part_info->subpart_expr)
      err+= add_string_len(fptr, part_info->subpart_func_string,
                           part_info->subpart_func_len);
    err+= add_end_parenthesis(fptr);
    err+= add_space(fptr);
  }
2122 2123
  if (add_default_info)
  {
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
  err+= add_begin_parenthesis(fptr);
  List_iterator<partition_element> part_it(part_info->partitions);
  no_parts= part_info->no_parts;
  no_subparts= part_info->no_subparts;
  i= 0;
  do
  {
    part_elem= part_it++;
    err+= add_partition(fptr);
    err+= add_string(fptr, part_elem->partition_name);
    err+= add_space(fptr);
    err+= add_partition_values(fptr, part_info, part_elem);
    if (!is_sub_partitioned(part_info))
      err+= add_partition_options(fptr, part_elem);
    if (is_sub_partitioned(part_info))
    {
      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);
        err+= add_string(fptr, part_elem->partition_name);
        err+= add_space(fptr);
        err+= add_partition_options(fptr, part_elem);
        if (j != (no_subparts-1))
        {
          err+= add_comma(fptr);
          err+= add_space(fptr);
        }
        else
          err+= add_end_parenthesis(fptr);
      } while (++j < no_subparts);
    }
    if (i != (no_parts-1))
    {
      err+= add_comma(fptr);
      err+= add_space(fptr);
    }
    else
      err+= add_end_parenthesis(fptr);
  } while (++i < no_parts);
2168
  }
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
  if (err)
    goto close_file;
  buffer_length= my_seek(fptr, 0L,MY_SEEK_END,MYF(0));
  if (unlikely(buffer_length == MY_FILEPOS_ERROR))
    goto close_file;
  if (unlikely(my_seek(fptr, 0L, MY_SEEK_SET, MYF(0)) == MY_FILEPOS_ERROR))
    goto close_file;
  *buf_length= (uint)buffer_length;
  if (use_sql_alloc)
    buf= sql_alloc(*buf_length+1);
  else
    buf= my_malloc(*buf_length+1, MYF(MY_WME));
  if (!buf)
    goto close_file;

2184
  if (unlikely(my_read(fptr, (byte*)buf, *buf_length, MYF(MY_FNABP))))
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
  {
    if (!use_sql_alloc)
      my_free(buf, MYF(0));
    else
      buf= NULL;
  }
  else
    buf[*buf_length]= 0;

close_file:
  /*
    Delete the file before closing to ensure the file doesn't get synched
    to disk unnecessary. We only used the file system as a dynamic array
    implementation so we are not really interested in getting the file
    present on disk.
    This is not possible on Windows so here it has to be done after closing
    the file. Also on Unix we delete immediately after opening to ensure no
    other process can read the information written into the file.
  */
  my_close(fptr, MYF(0));
#if defined(MSDOS) || defined(__WIN__) || defined(__EMX__) || defined(OS2)
  my_delete(file_name, MYF(0));
#endif
  DBUG_RETURN(buf);
}


/*
  Check if partition key fields are modified and if it can be handled by the
  underlying storage engine.
  SYNOPSIS
    partition_key_modified
    table                TABLE object for which partition fields are set-up
    fields               A list of the to be modifed
  RETURN VALUES
    TRUE                 Need special handling of UPDATE
    FALSE                Normal UPDATE handling is ok
*/

bool partition_key_modified(TABLE *table, List<Item> &fields)
{
  List_iterator_fast<Item> f(fields);
unknown's avatar
unknown committed
2227
  partition_info *part_info= table->part_info;
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 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
  Item_field *item_field;
  DBUG_ENTER("partition_key_modified");
  if (!part_info)
    DBUG_RETURN(FALSE);
  if (table->file->partition_flags() & HA_CAN_UPDATE_PARTITION_KEY)
    DBUG_RETURN(FALSE);
  f.rewind();
  while ((item_field=(Item_field*) f++))
    if (item_field->field->flags & FIELD_IN_PART_FUNC_FLAG)
      DBUG_RETURN(TRUE);
  DBUG_RETURN(FALSE);
}


/*
  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.
  SYNOPSIS
    calculate_key_value()
    field_array             An array of the fields in KEY partitioning
  RETURN VALUE
    hash_value calculated
  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)
{
  uint32 hashnr= 0;
  ulong nr2= 4;
  do
  {
    Field *field= *field_array;
    if (field->is_null())
    {
      hashnr^= (hashnr << 1) | 1;
    }
    else
    {
      uint len= field->pack_length();
      ulong nr1= 1;
      CHARSET_INFO *cs= field->charset();
      cs->coll->hash_sort(cs, (uchar*)field->ptr, len, &nr1, &nr2);
      hashnr^= (uint32)nr1;
    }
  } while (*(++field_array));
  return hashnr;
}


/*
  A simple support function to calculate part_id given local part and
  sub part.
  SYNOPSIS
    get_part_id_for_sub()
    loc_part_id             Local partition id
    sub_part_id             Subpartition id
    no_subparts             Number of subparts
*/

inline
static uint32 get_part_id_for_sub(uint32 loc_part_id, uint32 sub_part_id,
                                  uint no_subparts)
{
  return (uint32)((loc_part_id * no_subparts) + sub_part_id);
}


/*
  Calculate part_id for (SUB)PARTITION BY HASH
  SYNOPSIS
    get_part_id_hash()
    no_parts                 Number of hash partitions
    part_expr                Item tree of hash function
  RETURN VALUE
    Calculated partition id
*/

inline
static uint32 get_part_id_hash(uint no_parts,
                               Item *part_expr)
{
  DBUG_ENTER("get_part_id_hash");
  DBUG_RETURN((uint32)(part_expr->val_int() % no_parts));
}


/*
  Calculate part_id for (SUB)PARTITION BY LINEAR HASH
  SYNOPSIS
    get_part_id_linear_hash()
    part_info           A reference to the partition_info struct where all the
                        desired information is given
    no_parts            Number of hash partitions
    part_expr           Item tree of hash function
  RETURN VALUE
    Calculated partition id
*/

inline
static uint32 get_part_id_linear_hash(partition_info *part_info,
                                      uint no_parts,
                                      Item *part_expr)
{
  DBUG_ENTER("get_part_id_linear_hash");
  DBUG_RETURN(get_part_id_from_linear_hash(part_expr->val_int(),
                                           part_info->linear_hash_mask,
                                           no_parts));
}


/*
  Calculate part_id for (SUB)PARTITION BY KEY
  SYNOPSIS
    get_part_id_key()
    field_array         Array of fields for PARTTION KEY
    no_parts            Number of KEY partitions
  RETURN VALUE
    Calculated partition id
*/

inline
static uint32 get_part_id_key(Field **field_array,
                              uint no_parts)
{
  DBUG_ENTER("get_part_id_key");
unknown's avatar
unknown committed
2372
  DBUG_RETURN(calculate_key_value(field_array) % no_parts);
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 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 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836
}


/*
  Calculate part_id for (SUB)PARTITION BY LINEAR KEY
  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
    no_parts            Number of KEY partitions
  RETURN VALUE
    Calculated partition id
*/

inline
static uint32 get_part_id_linear_key(partition_info *part_info,
                                     Field **field_array,
                                     uint no_parts)
{
  DBUG_ENTER("get_partition_id_linear_key");
  DBUG_RETURN(get_part_id_from_linear_hash(calculate_key_value(field_array),
                                           part_info->linear_hash_mask,
                                           no_parts));
}

/*
  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
    part_id             The partition id is returned through this pointer
  RETURN VALUE
    part_id
    return TRUE means that 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 14 different variants of this function which are called
    through a function pointer.

    get_partition_id_list
    get_partition_id_range
    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_range_sub_hash
    get_partition_id_range_sub_key
    get_partition_id_range_sub_linear_hash
    get_partition_id_range_sub_linear_key
    get_partition_id_list_sub_hash
    get_partition_id_list_sub_key
    get_partition_id_list_sub_linear_hash
    get_partition_id_list_sub_linear_key
*/

/*
  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.
  SYNOPSIS
    get_part_partition_id()
    part_info           A reference to the partition_info struct where all the
                        desired information is given
    part_id             The partition id is returned through this pointer
  RETURN VALUE
    part_id
    return TRUE means that the fields of the partition function didn't fit
    into any partition and thus the values of the PF-fields are not allowed.
  DESCRIPTION
    
    It is actually 6 different variants of this function which are called
    through a function pointer.

    get_partition_id_list
    get_partition_id_range
    get_partition_id_hash_nosub
    get_partition_id_key_nosub
    get_partition_id_linear_hash_nosub
    get_partition_id_linear_key_nosub
*/


bool get_partition_id_list(partition_info *part_info,
                           uint32 *part_id)
{
  DBUG_ENTER("get_partition_id_list");
  LIST_PART_ENTRY *list_array= part_info->list_array;
  uint list_index;
  longlong list_value;
  uint min_list_index= 0, max_list_index= part_info->no_list_values - 1;
  longlong part_func_value= part_info->part_expr->val_int();
  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)
      max_list_index= list_index - 1;
    else {
      *part_id= (uint32)list_array[list_index].partition_id;
      DBUG_RETURN(FALSE);
    }
  }
  *part_id= 0;
  DBUG_RETURN(TRUE);
}


bool get_partition_id_range(partition_info *part_info,
                            uint32 *part_id)
{
  DBUG_ENTER("get_partition_id_int_range");
  longlong *range_array= part_info->range_int_array;
  uint max_partition= part_info->no_parts - 1;
  uint min_part_id= 0, max_part_id= max_partition, loc_part_id;
  longlong part_func_value= part_info->part_expr->val_int();
  while (max_part_id > min_part_id)
  {
    loc_part_id= (max_part_id + min_part_id + 1) >> 1;
    if (range_array[loc_part_id] < part_func_value)
      min_part_id= loc_part_id + 1;
    else
      max_part_id= loc_part_id - 1;
  }
  loc_part_id= max_part_id;
  if (part_func_value >= range_array[loc_part_id])
    if (loc_part_id != max_partition)
      loc_part_id++;
  *part_id= (uint32)loc_part_id;
  if (loc_part_id == max_partition)
    if (range_array[loc_part_id] != LONGLONG_MAX)
      if (part_func_value >= range_array[loc_part_id])
        DBUG_RETURN(TRUE);
  DBUG_RETURN(FALSE);
}

bool get_partition_id_hash_nosub(partition_info *part_info,
                                 uint32 *part_id)
{
  *part_id= get_part_id_hash(part_info->no_parts, part_info->part_expr);
  return FALSE;
}


bool get_partition_id_linear_hash_nosub(partition_info *part_info,
                                        uint32 *part_id)
{
  *part_id= get_part_id_linear_hash(part_info, part_info->no_parts,
                                    part_info->part_expr);
  return FALSE;
}


bool get_partition_id_key_nosub(partition_info *part_info,
                                uint32 *part_id)
{
  *part_id= get_part_id_key(part_info->part_field_array, part_info->no_parts);
  return FALSE;
}


bool get_partition_id_linear_key_nosub(partition_info *part_info,
                                       uint32 *part_id)
{
  *part_id= get_part_id_linear_key(part_info,
                                   part_info->part_field_array,
                                   part_info->no_parts);
  return FALSE;
}


bool get_partition_id_range_sub_hash(partition_info *part_info,
                                     uint32 *part_id)
{
  uint32 loc_part_id, sub_part_id;
  uint no_subparts;
  DBUG_ENTER("get_partition_id_range_sub_hash");
  if (unlikely(get_partition_id_range(part_info, &loc_part_id)))
  {
    DBUG_RETURN(TRUE);
  }
  no_subparts= part_info->no_subparts;
  sub_part_id= get_part_id_hash(no_subparts, part_info->subpart_expr);
  *part_id= get_part_id_for_sub(loc_part_id, sub_part_id, no_subparts);
  DBUG_RETURN(FALSE);
}


bool get_partition_id_range_sub_linear_hash(partition_info *part_info,
                                            uint32 *part_id)
{
  uint32 loc_part_id, sub_part_id;
  uint no_subparts;
  DBUG_ENTER("get_partition_id_range_sub_linear_hash");
  if (unlikely(get_partition_id_range(part_info, &loc_part_id)))
  {
    DBUG_RETURN(TRUE);
  }
  no_subparts= part_info->no_subparts;
  sub_part_id= get_part_id_linear_hash(part_info, no_subparts,
                                       part_info->subpart_expr);
  *part_id= get_part_id_for_sub(loc_part_id, sub_part_id, no_subparts);
  DBUG_RETURN(FALSE);
}


bool get_partition_id_range_sub_key(partition_info *part_info,
                                    uint32 *part_id)
{
  uint32 loc_part_id, sub_part_id;
  uint no_subparts;
  DBUG_ENTER("get_partition_id_range_sub_key");
  if (unlikely(get_partition_id_range(part_info, &loc_part_id)))
  {
    DBUG_RETURN(TRUE);
  }
  no_subparts= part_info->no_subparts;
  sub_part_id= get_part_id_key(part_info->subpart_field_array, no_subparts);
  *part_id= get_part_id_for_sub(loc_part_id, sub_part_id, no_subparts);
  DBUG_RETURN(FALSE);
}


bool get_partition_id_range_sub_linear_key(partition_info *part_info,
                                           uint32 *part_id)
{
  uint32 loc_part_id, sub_part_id;
  uint no_subparts;
  DBUG_ENTER("get_partition_id_range_sub_linear_key");
  if (unlikely(get_partition_id_range(part_info, &loc_part_id)))
  {
    DBUG_RETURN(TRUE);
  }
  no_subparts= part_info->no_subparts;
  sub_part_id= get_part_id_linear_key(part_info,
                                      part_info->subpart_field_array,
                                      no_subparts);
  *part_id= get_part_id_for_sub(loc_part_id, sub_part_id, no_subparts);
  DBUG_RETURN(FALSE);
}


bool get_partition_id_list_sub_hash(partition_info *part_info,
                                    uint32 *part_id)
{
  uint32 loc_part_id, sub_part_id;
  uint no_subparts;
  DBUG_ENTER("get_partition_id_list_sub_hash");
  if (unlikely(get_partition_id_list(part_info, &loc_part_id)))
  {
    DBUG_RETURN(TRUE);
  }
  no_subparts= part_info->no_subparts;
  sub_part_id= get_part_id_hash(no_subparts, part_info->subpart_expr);
  *part_id= get_part_id_for_sub(loc_part_id, sub_part_id, no_subparts);
  DBUG_RETURN(FALSE);
}


bool get_partition_id_list_sub_linear_hash(partition_info *part_info,
                                           uint32 *part_id)
{
  uint32 loc_part_id, sub_part_id;
  uint no_subparts;
  DBUG_ENTER("get_partition_id_list_sub_linear_hash");
  if (unlikely(get_partition_id_list(part_info, &loc_part_id)))
  {
    DBUG_RETURN(TRUE);
  }
  no_subparts= part_info->no_subparts;
  sub_part_id= get_part_id_hash(no_subparts, part_info->subpart_expr);
  *part_id= get_part_id_for_sub(loc_part_id, sub_part_id, no_subparts);
  DBUG_RETURN(FALSE);
}


bool get_partition_id_list_sub_key(partition_info *part_info,
                                   uint32 *part_id)
{
  uint32 loc_part_id, sub_part_id;
  uint no_subparts;
  DBUG_ENTER("get_partition_id_range_sub_key");
  if (unlikely(get_partition_id_list(part_info, &loc_part_id)))
  {
    DBUG_RETURN(TRUE);
  }
  no_subparts= part_info->no_subparts;
  sub_part_id= get_part_id_key(part_info->subpart_field_array, no_subparts);
  *part_id= get_part_id_for_sub(loc_part_id, sub_part_id, no_subparts);
  DBUG_RETURN(FALSE);
}


bool get_partition_id_list_sub_linear_key(partition_info *part_info,
                                          uint32 *part_id)
{
  uint32 loc_part_id, sub_part_id;
  uint no_subparts;
  DBUG_ENTER("get_partition_id_list_sub_linear_key");
  if (unlikely(get_partition_id_list(part_info, &loc_part_id)))
  {
    DBUG_RETURN(TRUE);
  }
  no_subparts= part_info->no_subparts;
  sub_part_id= get_part_id_linear_key(part_info,
                                      part_info->subpart_field_array,
                                      no_subparts);
  *part_id= get_part_id_for_sub(loc_part_id, sub_part_id, no_subparts);
  DBUG_RETURN(FALSE);
}


/*
  This function is used to calculate the subpartition id
  SYNOPSIS
    get_subpartition_id()
    part_info           A reference to the partition_info struct where all the
                        desired information is given
  RETURN VALUE
    part_id
    The subpartition identity
  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
*/

uint32 get_partition_id_hash_sub(partition_info *part_info)
{
  return get_part_id_hash(part_info->no_subparts, part_info->subpart_expr);
}


uint32 get_partition_id_linear_hash_sub(partition_info *part_info)
{
  return get_part_id_linear_hash(part_info, part_info->no_subparts,
                                 part_info->subpart_expr);
}


uint32 get_partition_id_key_sub(partition_info *part_info)
{
  return get_part_id_key(part_info->subpart_field_array,
                         part_info->no_subparts);
}


uint32 get_partition_id_linear_key_sub(partition_info *part_info)
{
  return get_part_id_linear_key(part_info,
                                part_info->subpart_field_array,
                                part_info->no_subparts);
}


/*
  Set an indicator on all partition fields that are set by the key 
  SYNOPSIS
    set_PF_fields_in_key()
    key_info                   Information about the index
    key_length                 Length of key
  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.
  SYNOPSIS
    check_part_func_bound()
    ptr                     Array of fields NULL terminated (partition fields)
  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.
  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
  RETURN VALUES
    part_id       Subpartition id to use
  DESCRIPTION
    Use key buffer to set-up record in buf, move field pointers and
    get the partition identity and restore field pointers afterwards.
*/

static uint32 get_sub_part_id_from_key(const TABLE *table,byte *buf,
                                       KEY *key_info,
                                       const key_range *key_spec)
{
  byte *rec0= table->record[0];
unknown's avatar
unknown committed
2837
  partition_info *part_info= table->part_info;
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
  uint32 part_id;
  DBUG_ENTER("get_sub_part_id_from_key");

  key_restore(buf, (byte*)key_spec->key, key_info, key_spec->length);
  if (likely(rec0 == buf))
    part_id= part_info->get_subpartition_id(part_info);
  else
  {
    Field **part_field_array= part_info->subpart_field_array;
    set_field_ptr(part_field_array, buf, rec0);
    part_id= part_info->get_subpartition_id(part_info);
    set_field_ptr(part_field_array, rec0, buf);
  }
  DBUG_RETURN(part_id);
}

/*
  Get the id of the partitioning part by using the key buffer of the
  index scan.
  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
    part_id       Partition to use
  RETURN VALUES
    TRUE          Partition to use not found
    FALSE         Ok, part_id indicates partition to use
  DESCRIPTION
    Use key buffer to set-up record in buf, move field pointers and
    get the partition identity and restore field pointers afterwards.
*/
bool get_part_id_from_key(const TABLE *table, byte *buf, KEY *key_info,
                          const key_range *key_spec, uint32 *part_id)
{
  bool result;
  byte *rec0= table->record[0];
unknown's avatar
unknown committed
2876
  partition_info *part_info= table->part_info;
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915
  DBUG_ENTER("get_part_id_from_key");

  key_restore(buf, (byte*)key_spec->key, key_info, key_spec->length);
  if (likely(rec0 == buf))
    result= part_info->get_part_partition_id(part_info, part_id);
  else
  {
    Field **part_field_array= part_info->part_field_array;
    set_field_ptr(part_field_array, buf, rec0);
    result= part_info->get_part_partition_id(part_info, part_id);
    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.
  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
    part_spec     A partition id containing start part and end part
  RETURN VALUES
    part_spec
    No partitions to scan is indicated by end_part > start_part when returning
  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.
*/

void get_full_part_id_from_key(const TABLE *table, byte *buf,
                               KEY *key_info,
                               const key_range *key_spec,
                               part_id_range *part_spec)
{
  bool result;
unknown's avatar
unknown committed
2916
  partition_info *part_info= table->part_info;
2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960
  byte *rec0= table->record[0];
  DBUG_ENTER("get_full_part_id_from_key");

  key_restore(buf, (byte*)key_spec->key, key_info, key_spec->length);
  if (likely(rec0 == buf))
    result= part_info->get_partition_id(part_info, &part_spec->start_part);
  else
  {
    Field **part_field_array= part_info->full_part_field_array;
    set_field_ptr(part_field_array, buf, rec0);
    result= part_info->get_partition_id(part_info, &part_spec->start_part);
    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;
}
    
/*
  Get the set of partitions to use in query.
  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
    part_spec     Contains start part, end part and indicator if bitmap is
                  used for which partitions to scan
  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.
  RETURN VALUE
    part_spec
*/
void get_partition_set(const TABLE *table, byte *buf, const uint index,
                       const key_range *key_spec, part_id_range *part_spec)
{
unknown's avatar
unknown committed
2961
  partition_info *part_info= table->part_info;
unknown's avatar
unknown committed
2962
  uint no_parts= get_tot_partitions(part_info), i, part_id;
2963 2964
  uint sub_part= no_parts;
  uint32 part_part= no_parts;
2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003
  KEY *key_info= NULL;
  bool found_part_field= FALSE;
  DBUG_ENTER("get_partition_set");

  part_spec->use_bit_array= FALSE;
  part_spec->start_part= 0;
  part_spec->end_part= no_parts - 1;
  if ((index < MAX_KEY) && 
       key_spec->flag == (uint)HA_READ_KEY_EXACT &&
       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);
        DBUG_VOID_RETURN;
      }
      else if (is_sub_partitioned(part_info))
      {
        if (part_info->all_fields_in_SPF.is_set(index))
          sub_part= get_sub_part_id_from_key(table, buf, key_info, key_spec);
        else if (part_info->all_fields_in_PPF.is_set(index))
        {
3004
          if (get_part_id_from_key(table,buf,key_info,key_spec,(uint32*)&part_part))
3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
          {
            /*
              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.
            */
            part_spec->start_part= no_parts;
            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);
          DBUG_VOID_RETURN; 
        }
        else if (check_part_func_bound(part_info->part_field_array))
          sub_part= get_sub_part_id_from_key(table, buf, key_info, key_spec);
        else if (check_part_func_bound(part_info->subpart_field_array))
        {
3043
          if (get_part_id_from_key(table,buf,key_info,key_spec,(uint32*)&part_part))
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147
          {
            part_spec->start_part= no_parts;
            clear_indicator_in_key_fields(key_info);
            DBUG_VOID_RETURN;
          }
        }
      }
    }
  }
  {
    /*
      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.
  */
  if (!(part_part == no_parts && sub_part == no_parts))
  {
    /*
      We can only arrive here if we are using subpartitioning.
    */
    if (part_part != no_parts)
    {
      /*
        We know the top partition and need to scan all underlying
        subpartitions. This is a range without holes.
      */
      DBUG_ASSERT(sub_part == no_parts);
      part_spec->start_part= part_part * part_info->no_parts;
      part_spec->end_part= part_spec->start_part+part_info->no_subparts - 1;
    }
    else
    {
      DBUG_ASSERT(sub_part != no_parts);
      part_spec->use_bit_array= TRUE;
      part_spec->start_part= sub_part;
      part_spec->end_part=sub_part+
                           (part_info->no_subparts*(part_info->no_parts-1));
      for (i= 0, part_id= sub_part; i < part_info->no_parts;
           i++, part_id+= part_info->no_subparts)
        ; //Set bit part_id in bit array
    }
  }
  if (found_part_field)
    clear_indicator_in_key_fields(key_info);
  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      |
   | field names readable        |
   -------------------------------
   | Packed field info           |
   | 17 + 1 + strlen(field_name) |
   | + 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.
   SYNOPSIS
     mysql_unpack_partition()
     file                          File reference of frm file
     thd                           Thread object
     part_info_len                 Length of partition syntax
     table                         Table object of partitioned table
   RETURN VALUE
     TRUE                          Error
     FALSE                         Sucess
   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.
*/

unknown's avatar
unknown committed
3148 3149 3150
bool mysql_unpack_partition(THD *thd, const uchar *part_buf,
                            uint part_info_len, TABLE* table,
                            enum db_type default_db_type)
3151 3152 3153 3154 3155 3156
{
  Item *thd_free_list= thd->free_list;
  bool result= TRUE;
  partition_info *part_info;
  LEX *old_lex= thd->lex, lex;
  DBUG_ENTER("mysql_unpack_partition");
unknown's avatar
unknown committed
3157

3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184
  thd->lex= &lex;
  lex_start(thd, part_buf, part_info_len);
  /*
    We need to use the current SELECT_LEX since I need to keep the
    Name_resolution_context object which is referenced from the
    Item_field objects.
    This is not a nice solution since if the parser uses current_select
    for anything else it will corrupt the current LEX object.
  */
  thd->lex->current_select= old_lex->current_select; 
  /*
    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.
  */
  thd->free_list= NULL;
  lex.part_info= (partition_info*)1; //Indicate yyparse from this place
  if (yyparse((void*)thd) || thd->is_fatal_error)
  {
    free_items(thd->free_list);
    goto end;
  }
  part_info= lex.part_info;
unknown's avatar
unknown committed
3185
  table->part_info= part_info;
3186
  table->file->set_part_info(part_info);
3187 3188 3189 3190 3191 3192
  if (part_info->default_engine_type == DB_TYPE_UNKNOWN)
    part_info->default_engine_type= default_db_type;
  else
  {
    DBUG_ASSERT(part_info->default_engine_type == default_db_type);
  }
3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207
  part_info->item_free_list= thd->free_list;

  {
  /*
    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; 
    char *part_func_string, *subpart_func_string= NULL;
unknown's avatar
unknown committed
3208
    if (!((part_func_string= thd->alloc(part_func_len))) ||
3209
        (subpart_func_len &&
unknown's avatar
unknown committed
3210
        !((subpart_func_string= thd->alloc(subpart_func_len)))))
3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256
    {
      my_error(ER_OUTOFMEMORY, MYF(0), part_func_len);
      free_items(thd->free_list);
      part_info->item_free_list= 0;
      goto end;
    }
    memcpy(part_func_string, part_info->part_func_string, part_func_len);
    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:
  thd->free_list= thd_free_list;
  thd->lex= old_lex;
  DBUG_RETURN(result);
}
#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.
*/

void set_field_ptr(Field **ptr, const byte *new_buf,
                            const byte *old_buf)
{
  my_ptrdiff_t diff= (new_buf - old_buf);
  DBUG_ENTER("set_nullable_field_ptr");

  do
  {
unknown's avatar
unknown committed
3257
    (*ptr)->move_field_offset(diff);
3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290
  } 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_part            key part 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.
*/

void set_key_field_ptr(KEY *key_info, const byte *new_buf,
                       const byte *old_buf)
{
  KEY_PART_INFO *key_part= key_info->key_part;
  uint key_parts= key_info->key_parts, i= 0;
  my_ptrdiff_t diff= (new_buf - old_buf);
  DBUG_ENTER("set_key_field_ptr");

  do
  {
unknown's avatar
unknown committed
3291
    key_part->field->move_field_offset(diff);
3292 3293 3294 3295
    key_part++;
  } while (++i < key_parts);
  DBUG_VOID_RETURN;
}