~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.cc

  • Committer: Brian Aker
  • Date: 2009-06-09 01:59:10 UTC
  • mfrom: (1054.1.11 merge)
  • Revision ID: brian@gaz-20090609015910-0sfsovp2x8wfbr2c
Merge Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1924
1924
}
1925
1925
 
1926
1926
 
1927
 
/*
1928
 
  Create Item_field for each column in the table.
1929
 
 
1930
 
  SYNPOSIS
1931
 
    Table::fill_item_list()
1932
 
      item_list          a pointer to an empty list used to store items
1933
 
 
1934
 
  DESCRIPTION
1935
 
    Create Item_field object for each column in the table and
1936
 
    initialize it with the corresponding Field. New items are
1937
 
    created in the current Session memory root.
1938
 
 
1939
 
  RETURN VALUE
1940
 
    0                    success
1941
 
    1                    out of memory
1942
 
*/
1943
 
 
1944
 
bool Table::fill_item_list(List<Item> *item_list) const
1945
 
{
1946
 
  /*
1947
 
    All Item_field's created using a direct pointer to a field
1948
 
    are fixed in Item_field constructor.
1949
 
  */
1950
 
  for (Field **ptr= field; *ptr; ptr++)
1951
 
  {
1952
 
    Item_field *item= new Item_field(*ptr);
1953
 
    if (!item || item_list->push_back(item))
1954
 
      return true;
1955
 
  }
1956
 
  return false;
1957
 
}
1958
 
 
1959
 
 
1960
 
/*
1961
 
  Find underlying base tables (TableList) which represent given
1962
 
  table_to_find (Table)
1963
 
 
1964
 
  SYNOPSIS
1965
 
    TableList::find_underlying_table()
1966
 
    table_to_find table to find
1967
 
 
1968
 
  RETURN
1969
 
    0  table is not found
1970
 
    found table reference
1971
 
*/
1972
 
 
1973
 
TableList *TableList::find_underlying_table(Table *table_to_find)
1974
 
{
1975
 
  /* is this real table and table which we are looking for? */
1976
 
  if (table == table_to_find)
1977
 
    return this;
1978
 
 
1979
 
  return NULL;
1980
 
}
1981
 
 
1982
 
 
1983
 
bool TableList::placeholder()
1984
 
{
1985
 
  return derived || schema_table || (create && !table->getDBStat()) || !table;
1986
 
}
1987
 
 
1988
 
 
1989
 
/*
1990
 
  Set insert_values buffer
1991
 
 
1992
 
  SYNOPSIS
1993
 
    set_insert_values()
1994
 
    mem_root   memory pool for allocating
1995
 
 
1996
 
  RETURN
1997
 
    false - OK
1998
 
    TRUE  - out of memory
1999
 
*/
2000
 
 
2001
 
bool TableList::set_insert_values(MEM_ROOT *mem_root)
2002
 
{
2003
 
  if (table)
2004
 
  {
2005
 
    if (!table->insert_values &&
2006
 
        !(table->insert_values= (unsigned char *)alloc_root(mem_root,
2007
 
                                                   table->s->rec_buff_length)))
2008
 
      return true;
2009
 
  }
2010
 
 
2011
 
  return false;
2012
 
}
2013
 
 
2014
 
 
2015
 
/*
2016
 
  Test if this is a leaf with respect to name resolution.
2017
 
 
2018
 
  SYNOPSIS
2019
 
    TableList::is_leaf_for_name_resolution()
2020
 
 
2021
 
  DESCRIPTION
2022
 
    A table reference is a leaf with respect to name resolution if
2023
 
    it is either a leaf node in a nested join tree (table, view,
2024
 
    schema table, subquery), or an inner node that represents a
2025
 
    NATURAL/USING join, or a nested join with materialized join
2026
 
    columns.
2027
 
 
2028
 
  RETURN
2029
 
    TRUE if a leaf, false otherwise.
2030
 
*/
2031
 
bool TableList::is_leaf_for_name_resolution()
2032
 
{
2033
 
  return (is_natural_join || is_join_columns_complete || !nested_join);
2034
 
}
2035
 
 
2036
 
 
2037
 
/*
2038
 
  Retrieve the first (left-most) leaf in a nested join tree with
2039
 
  respect to name resolution.
2040
 
 
2041
 
  SYNOPSIS
2042
 
    TableList::first_leaf_for_name_resolution()
2043
 
 
2044
 
  DESCRIPTION
2045
 
    Given that 'this' is a nested table reference, recursively walk
2046
 
    down the left-most children of 'this' until we reach a leaf
2047
 
    table reference with respect to name resolution.
2048
 
 
2049
 
  IMPLEMENTATION
2050
 
    The left-most child of a nested table reference is the last element
2051
 
    in the list of children because the children are inserted in
2052
 
    reverse order.
2053
 
 
2054
 
  RETURN
2055
 
    If 'this' is a nested table reference - the left-most child of
2056
 
      the tree rooted in 'this',
2057
 
    else return 'this'
2058
 
*/
2059
 
 
2060
 
TableList *TableList::first_leaf_for_name_resolution()
2061
 
{
2062
 
  TableList *cur_table_ref= NULL;
2063
 
  nested_join_st *cur_nested_join;
2064
 
 
2065
 
  if (is_leaf_for_name_resolution())
2066
 
    return this;
2067
 
  assert(nested_join);
2068
 
 
2069
 
  for (cur_nested_join= nested_join;
2070
 
       cur_nested_join;
2071
 
       cur_nested_join= cur_table_ref->nested_join)
2072
 
  {
2073
 
    List_iterator_fast<TableList> it(cur_nested_join->join_list);
2074
 
    cur_table_ref= it++;
2075
 
    /*
2076
 
      If the current nested join is a RIGHT JOIN, the operands in
2077
 
      'join_list' are in reverse order, thus the first operand is
2078
 
      already at the front of the list. Otherwise the first operand
2079
 
      is in the end of the list of join operands.
2080
 
    */
2081
 
    if (!(cur_table_ref->outer_join & JOIN_TYPE_RIGHT))
2082
 
    {
2083
 
      TableList *next;
2084
 
      while ((next= it++))
2085
 
        cur_table_ref= next;
2086
 
    }
2087
 
    if (cur_table_ref->is_leaf_for_name_resolution())
2088
 
      break;
2089
 
  }
2090
 
  return cur_table_ref;
2091
 
}
2092
 
 
2093
 
 
2094
 
/*
2095
 
  Retrieve the last (right-most) leaf in a nested join tree with
2096
 
  respect to name resolution.
2097
 
 
2098
 
  SYNOPSIS
2099
 
    TableList::last_leaf_for_name_resolution()
2100
 
 
2101
 
  DESCRIPTION
2102
 
    Given that 'this' is a nested table reference, recursively walk
2103
 
    down the right-most children of 'this' until we reach a leaf
2104
 
    table reference with respect to name resolution.
2105
 
 
2106
 
  IMPLEMENTATION
2107
 
    The right-most child of a nested table reference is the first
2108
 
    element in the list of children because the children are inserted
2109
 
    in reverse order.
2110
 
 
2111
 
  RETURN
2112
 
    - If 'this' is a nested table reference - the right-most child of
2113
 
      the tree rooted in 'this',
2114
 
    - else - 'this'
2115
 
*/
2116
 
 
2117
 
TableList *TableList::last_leaf_for_name_resolution()
2118
 
{
2119
 
  TableList *cur_table_ref= this;
2120
 
  nested_join_st *cur_nested_join;
2121
 
 
2122
 
  if (is_leaf_for_name_resolution())
2123
 
    return this;
2124
 
  assert(nested_join);
2125
 
 
2126
 
  for (cur_nested_join= nested_join;
2127
 
       cur_nested_join;
2128
 
       cur_nested_join= cur_table_ref->nested_join)
2129
 
  {
2130
 
    cur_table_ref= cur_nested_join->join_list.head();
2131
 
    /*
2132
 
      If the current nested is a RIGHT JOIN, the operands in
2133
 
      'join_list' are in reverse order, thus the last operand is in the
2134
 
      end of the list.
2135
 
    */
2136
 
    if ((cur_table_ref->outer_join & JOIN_TYPE_RIGHT))
2137
 
    {
2138
 
      List_iterator_fast<TableList> it(cur_nested_join->join_list);
2139
 
      TableList *next;
2140
 
      cur_table_ref= it++;
2141
 
      while ((next= it++))
2142
 
        cur_table_ref= next;
2143
 
    }
2144
 
    if (cur_table_ref->is_leaf_for_name_resolution())
2145
 
      break;
2146
 
  }
2147
 
  return cur_table_ref;
2148
 
}
2149
 
 
2150
 
 
2151
1927
/*****************************************************************************
2152
1928
  Functions to handle column usage bitmaps (read_set, write_set etc...)
2153
1929
*****************************************************************************/
2384
2160
    mark_auto_increment_column();
2385
2161
}
2386
2162
 
2387
 
/*
2388
 
  Return subselect that contains the FROM list this table is taken from
2389
 
 
2390
 
  SYNOPSIS
2391
 
    TableList::containing_subselect()
2392
 
 
2393
 
  RETURN
2394
 
    Subselect item for the subquery that contains the FROM list
2395
 
    this table is taken from if there is any
2396
 
    0 - otherwise
2397
 
 
2398
 
*/
2399
 
 
2400
 
Item_subselect *TableList::containing_subselect()
2401
 
{
2402
 
  return (select_lex ? select_lex->master_unit()->item : 0);
2403
 
}
2404
 
 
2405
 
/*
2406
 
  Compiles the tagged hints list and fills up the bitmasks.
2407
 
 
2408
 
  SYNOPSIS
2409
 
    process_index_hints()
2410
 
      table         the Table to operate on.
2411
 
 
2412
 
  DESCRIPTION
2413
 
    The parser collects the index hints for each table in a "tagged list"
2414
 
    (TableList::index_hints). Using the information in this tagged list
2415
 
    this function sets the members Table::keys_in_use_for_query,
2416
 
    Table::keys_in_use_for_group_by, Table::keys_in_use_for_order_by,
2417
 
    Table::force_index and Table::covering_keys.
2418
 
 
2419
 
    Current implementation of the runtime does not allow mixing FORCE INDEX
2420
 
    and USE INDEX, so this is checked here. Then the FORCE INDEX list
2421
 
    (if non-empty) is appended to the USE INDEX list and a flag is set.
2422
 
 
2423
 
    Multiple hints of the same kind are processed so that each clause
2424
 
    is applied to what is computed in the previous clause.
2425
 
    For example:
2426
 
        USE INDEX (i1) USE INDEX (i2)
2427
 
    is equivalent to
2428
 
        USE INDEX (i1,i2)
2429
 
    and means "consider only i1 and i2".
2430
 
 
2431
 
    Similarly
2432
 
        USE INDEX () USE INDEX (i1)
2433
 
    is equivalent to
2434
 
        USE INDEX (i1)
2435
 
    and means "consider only the index i1"
2436
 
 
2437
 
    It is OK to have the same index several times, e.g. "USE INDEX (i1,i1)" is
2438
 
    not an error.
2439
 
 
2440
 
    Different kind of hints (USE/FORCE/IGNORE) are processed in the following
2441
 
    order:
2442
 
      1. All indexes in USE (or FORCE) INDEX are added to the mask.
2443
 
      2. All IGNORE INDEX
2444
 
 
2445
 
    e.g. "USE INDEX i1, IGNORE INDEX i1, USE INDEX i1" will not use i1 at all
2446
 
    as if we had "USE INDEX i1, USE INDEX i1, IGNORE INDEX i1".
2447
 
 
2448
 
    As an optimization if there is a covering index, and we have
2449
 
    IGNORE INDEX FOR GROUP/order_st, and this index is used for the JOIN part,
2450
 
    then we have to ignore the IGNORE INDEX FROM GROUP/order_st.
2451
 
 
2452
 
  RETURN VALUE
2453
 
    false                no errors found
2454
 
    TRUE                 found and reported an error.
2455
 
*/
2456
 
bool TableList::process_index_hints(Table *tbl)
2457
 
{
2458
 
  /* initialize the result variables */
2459
 
  tbl->keys_in_use_for_query= tbl->keys_in_use_for_group_by=
2460
 
    tbl->keys_in_use_for_order_by= tbl->s->keys_in_use;
2461
 
 
2462
 
  /* index hint list processing */
2463
 
  if (index_hints)
2464
 
  {
2465
 
    key_map index_join[INDEX_HINT_FORCE + 1];
2466
 
    key_map index_order[INDEX_HINT_FORCE + 1];
2467
 
    key_map index_group[INDEX_HINT_FORCE + 1];
2468
 
    Index_hint *hint;
2469
 
    int type;
2470
 
    bool have_empty_use_join= false, have_empty_use_order= false,
2471
 
         have_empty_use_group= false;
2472
 
    List_iterator <Index_hint> iter(*index_hints);
2473
 
 
2474
 
    /* initialize temporary variables used to collect hints of each kind */
2475
 
    for (type= INDEX_HINT_IGNORE; type <= INDEX_HINT_FORCE; type++)
2476
 
    {
2477
 
      index_join[type].reset();
2478
 
      index_order[type].reset();
2479
 
      index_group[type].reset();
2480
 
    }
2481
 
 
2482
 
    /* iterate over the hints list */
2483
 
    while ((hint= iter++))
2484
 
    {
2485
 
      uint32_t pos;
2486
 
 
2487
 
      /* process empty USE INDEX () */
2488
 
      if (hint->type == INDEX_HINT_USE && !hint->key_name.str)
2489
 
      {
2490
 
        if (hint->clause & INDEX_HINT_MASK_JOIN)
2491
 
        {
2492
 
          index_join[hint->type].reset();
2493
 
          have_empty_use_join= true;
2494
 
        }
2495
 
        if (hint->clause & INDEX_HINT_MASK_ORDER)
2496
 
        {
2497
 
          index_order[hint->type].reset();
2498
 
          have_empty_use_order= true;
2499
 
        }
2500
 
        if (hint->clause & INDEX_HINT_MASK_GROUP)
2501
 
        {
2502
 
          index_group[hint->type].reset();
2503
 
          have_empty_use_group= true;
2504
 
        }
2505
 
        continue;
2506
 
      }
2507
 
 
2508
 
      /*
2509
 
        Check if an index with the given name exists and get his offset in
2510
 
        the keys bitmask for the table
2511
 
      */
2512
 
      if (tbl->s->keynames.type_names == 0 ||
2513
 
          (pos= find_type(&tbl->s->keynames, hint->key_name.str,
2514
 
                          hint->key_name.length, 1)) <= 0)
2515
 
      {
2516
 
        my_error(ER_KEY_DOES_NOT_EXITS, MYF(0), hint->key_name.str, alias);
2517
 
        return 1;
2518
 
      }
2519
 
 
2520
 
      pos--;
2521
 
 
2522
 
      /* add to the appropriate clause mask */
2523
 
      if (hint->clause & INDEX_HINT_MASK_JOIN)
2524
 
        index_join[hint->type].set(pos);
2525
 
      if (hint->clause & INDEX_HINT_MASK_ORDER)
2526
 
        index_order[hint->type].set(pos);
2527
 
      if (hint->clause & INDEX_HINT_MASK_GROUP)
2528
 
        index_group[hint->type].set(pos);
2529
 
    }
2530
 
 
2531
 
    /* cannot mix USE INDEX and FORCE INDEX */
2532
 
    if ((index_join[INDEX_HINT_FORCE].any() ||
2533
 
         index_order[INDEX_HINT_FORCE].any() ||
2534
 
         index_group[INDEX_HINT_FORCE].any()) &&
2535
 
        (index_join[INDEX_HINT_USE].any() ||  have_empty_use_join ||
2536
 
         index_order[INDEX_HINT_USE].any() || have_empty_use_order ||
2537
 
         index_group[INDEX_HINT_USE].any() || have_empty_use_group))
2538
 
    {
2539
 
      my_error(ER_WRONG_USAGE, MYF(0), index_hint_type_name[INDEX_HINT_USE],
2540
 
               index_hint_type_name[INDEX_HINT_FORCE]);
2541
 
      return 1;
2542
 
    }
2543
 
 
2544
 
    /* process FORCE INDEX as USE INDEX with a flag */
2545
 
    if (index_join[INDEX_HINT_FORCE].any() ||
2546
 
        index_order[INDEX_HINT_FORCE].any() ||
2547
 
        index_group[INDEX_HINT_FORCE].any())
2548
 
    {
2549
 
      tbl->force_index= true;
2550
 
      index_join[INDEX_HINT_USE]|= index_join[INDEX_HINT_FORCE];
2551
 
      index_order[INDEX_HINT_USE]|= index_order[INDEX_HINT_FORCE];
2552
 
      index_group[INDEX_HINT_USE]|= index_group[INDEX_HINT_FORCE];
2553
 
    }
2554
 
 
2555
 
    /* apply USE INDEX */
2556
 
    if (index_join[INDEX_HINT_USE].any() || have_empty_use_join)
2557
 
      tbl->keys_in_use_for_query&= index_join[INDEX_HINT_USE];
2558
 
    if (index_order[INDEX_HINT_USE].any() || have_empty_use_order)
2559
 
      tbl->keys_in_use_for_order_by&= index_order[INDEX_HINT_USE];
2560
 
    if (index_group[INDEX_HINT_USE].any() || have_empty_use_group)
2561
 
      tbl->keys_in_use_for_group_by&= index_group[INDEX_HINT_USE];
2562
 
 
2563
 
    /* apply IGNORE INDEX */
2564
 
    key_map_subtract(tbl->keys_in_use_for_query, index_join[INDEX_HINT_IGNORE]);
2565
 
    key_map_subtract(tbl->keys_in_use_for_order_by, index_order[INDEX_HINT_IGNORE]);
2566
 
    key_map_subtract(tbl->keys_in_use_for_group_by, index_group[INDEX_HINT_IGNORE]);
2567
 
  }
2568
 
 
2569
 
  /* make sure covering_keys don't include indexes disabled with a hint */
2570
 
  tbl->covering_keys&= tbl->keys_in_use_for_query;
2571
 
  return 0;
2572
 
}
2573
2163
 
2574
2164
 
2575
2165
size_t Table::max_row_length(const unsigned char *data)
3967
3557
}
3968
3558
 
3969
3559
 
 
3560
/*
 
3561
  Find field in table, no side effects, only purpose is to check for field
 
3562
  in table object and get reference to the field if found.
 
3563
 
 
3564
  SYNOPSIS
 
3565
  find_field_in_table_sef()
 
3566
 
 
3567
  table                         table where to find
 
3568
  name                          Name of field searched for
 
3569
 
 
3570
  RETURN
 
3571
  0                   field is not found
 
3572
#                   pointer to field
 
3573
*/
 
3574
 
 
3575
Field *Table::find_field_in_table_sef(const char *name)
 
3576
{
 
3577
  Field **field_ptr;
 
3578
  if (s->name_hash.records)
 
3579
  {
 
3580
    field_ptr= (Field**)hash_search(&s->name_hash,(unsigned char*) name,
 
3581
                                    strlen(name));
 
3582
    if (field_ptr)
 
3583
    {
 
3584
      /*
 
3585
        field_ptr points to field in TableShare. Convert it to the matching
 
3586
        field in table
 
3587
      */
 
3588
      field_ptr= (field + (field_ptr - s->field));
 
3589
    }
 
3590
  }
 
3591
  else
 
3592
  {
 
3593
    if (!(field_ptr= field))
 
3594
      return (Field *)0;
 
3595
    for (; *field_ptr; ++field_ptr)
 
3596
      if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
 
3597
        break;
 
3598
  }
 
3599
  if (field_ptr)
 
3600
    return *field_ptr;
 
3601
  else
 
3602
    return (Field *)0;
 
3603
}
 
3604
 
 
3605
 
3970
3606
/*****************************************************************************
3971
3607
** Instansiate templates
3972
3608
*****************************************************************************/