~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/item/subselect.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
*/
26
26
#include "config.h"
27
27
 
 
28
#include <cstdio>
28
29
#include <limits.h>
29
30
 
30
31
#include <drizzled/sql_select.h>
89
90
  if (unit->item)
90
91
  {
91
92
    /*
92
 
      Item can be changed in JOIN::prepare while engine in JOIN::optimize
 
93
      Item can be changed in JOIN::prepare while engine in Join::optimize
93
94
      => we do not copy old_engine here
94
95
    */
95
96
    engine= unit->item->engine;
171
172
}
172
173
 
173
174
Item_subselect::trans_res
174
 
Item_subselect::select_transformer(JOIN *)
 
175
Item_subselect::select_transformer(Join *)
175
176
{
176
177
  return(RES_OK);
177
178
}
496
497
  Make rollback for it, or special name resolving mode in 5.0.
497
498
*/
498
499
Item_subselect::trans_res
499
 
Item_singlerow_subselect::select_transformer(JOIN *join)
 
500
Item_singlerow_subselect::select_transformer(Join *join)
500
501
{
501
502
  if (changed)
502
503
    return(RES_OK);
524
525
    if (session->lex->describe)
525
526
    {
526
527
      char warn_buff[DRIZZLE_ERRMSG_SIZE];
527
 
      sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number);
 
528
      snprintf(warn_buff, sizeof(warn_buff), ER(ER_SELECT_REDUCED), select_lex->select_number);
528
529
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
529
530
                   ER_SELECT_REDUCED, warn_buff);
530
531
    }
976
977
*/
977
978
 
978
979
Item_subselect::trans_res
979
 
Item_in_subselect::single_value_transformer(JOIN *join,
 
980
Item_in_subselect::single_value_transformer(Join *join,
980
981
                                            const Comp_creator *func)
981
982
{
982
983
  Select_Lex *select_lex= join->select_lex;
1158
1159
*/
1159
1160
 
1160
1161
Item_subselect::trans_res
1161
 
Item_in_subselect::single_value_in_to_exists_transformer(JOIN * join, const Comp_creator *func)
 
1162
Item_in_subselect::single_value_in_to_exists_transformer(Join * join, const Comp_creator *func)
1162
1163
{
1163
1164
  Select_Lex *select_lex= join->select_lex;
1164
1165
 
1320
1321
        if (session->lex->describe)
1321
1322
        {
1322
1323
          char warn_buff[DRIZZLE_ERRMSG_SIZE];
1323
 
          sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number);
 
1324
          snprintf(warn_buff, sizeof(warn_buff), ER(ER_SELECT_REDUCED), select_lex->select_number);
1324
1325
          push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1325
1326
                       ER_SELECT_REDUCED, warn_buff);
1326
1327
        }
1334
1335
 
1335
1336
 
1336
1337
Item_subselect::trans_res
1337
 
Item_in_subselect::row_value_transformer(JOIN *join)
 
1338
Item_in_subselect::row_value_transformer(Join *join)
1338
1339
{
1339
1340
  Select_Lex *select_lex= join->select_lex;
1340
1341
  uint32_t cols_num= left_expr->cols();
1411
1412
*/
1412
1413
 
1413
1414
Item_subselect::trans_res
1414
 
Item_in_subselect::row_value_in_to_exists_transformer(JOIN * join)
 
1415
Item_in_subselect::row_value_in_to_exists_transformer(Join * join)
1415
1416
{
1416
1417
  Select_Lex *select_lex= join->select_lex;
1417
1418
  Item *having_item= 0;
1609
1610
 
1610
1611
 
1611
1612
Item_subselect::trans_res
1612
 
Item_in_subselect::select_transformer(JOIN *join)
 
1613
Item_in_subselect::select_transformer(Join *join)
1613
1614
{
1614
1615
  return select_in_like_transformer(join, Eq_creator::instance());
1615
1616
}
1637
1638
*/
1638
1639
 
1639
1640
Item_subselect::trans_res
1640
 
Item_in_subselect::select_in_like_transformer(JOIN *join, const Comp_creator *func)
 
1641
Item_in_subselect::select_in_like_transformer(Join *join, const Comp_creator *func)
1641
1642
{
1642
1643
  Select_Lex *current= session->lex->current_select, *up;
1643
1644
  const char *save_where= session->where;
1840
1841
 
1841
1842
bool Item_in_subselect::init_left_expr_cache()
1842
1843
{
1843
 
  JOIN *outer_join= NULL;
 
1844
  Join *outer_join= NULL;
1844
1845
 
1845
1846
  outer_join= unit->outer_select()->join;
1846
1847
  if (! outer_join || ! outer_join->tables || ! outer_join->join_tab)
1879
1880
 
1880
1881
 
1881
1882
Item_subselect::trans_res
1882
 
Item_allany_subselect::select_transformer(JOIN *join)
 
1883
Item_allany_subselect::select_transformer(Join *join)
1883
1884
{
1884
1885
  exec_method= IN_TO_EXISTS;
1885
1886
  if (upper_item)
1971
1972
{
1972
1973
  /* Tell handler we don't need the index anymore */
1973
1974
  if (tab->table->cursor->inited)
1974
 
    tab->table->cursor->ha_index_end();
 
1975
    tab->table->cursor->endIndexScan();
1975
1976
  return;
1976
1977
}
1977
1978
 
2016
2017
{
2017
2018
  if (prepared)
2018
2019
    return 0;
2019
 
  join= new JOIN(session, select_lex->item_list,
 
2020
  join= new Join(session, select_lex->item_list,
2020
2021
                 select_lex->options | SELECT_NO_UNLOCK, result);
2021
2022
  if (!join || !result)
2022
2023
    return 1; /* Fatal error is set already. */
2132
2133
 
2133
2134
int  init_read_record_seq(JoinTable *tab);
2134
2135
int join_read_always_key_or_null(JoinTable *tab);
2135
 
int join_read_next_same_or_null(READ_RECORD *info);
 
2136
int join_read_next_same_or_null(ReadRecord *info);
2136
2137
 
2137
2138
int subselect_single_select_engine::exec()
2138
2139
{
2278
2279
  Table *table= tab->table;
2279
2280
 
2280
2281
  if (table->cursor->inited)
2281
 
    table->cursor->ha_index_end();
 
2282
    table->cursor->endIndexScan();
2282
2283
 
2283
 
  table->cursor->ha_rnd_init(1);
 
2284
  table->cursor->startTableScan(1);
2284
2285
  table->cursor->extra_opt(HA_EXTRA_CACHE,
2285
 
                         current_session->variables.read_buff_size);
 
2286
                           current_session->variables.read_buff_size);
2286
2287
  table->null_row= 0;
2287
2288
  for (;;)
2288
2289
  {
2303
2304
    }
2304
2305
  }
2305
2306
 
2306
 
  table->cursor->ha_rnd_end();
 
2307
  table->cursor->endTableScan();
2307
2308
  return(error != 0);
2308
2309
}
2309
2310
 
2457
2458
    return(scan_table());
2458
2459
 
2459
2460
  if (!table->cursor->inited)
2460
 
    table->cursor->ha_index_init(tab->ref.key, 0);
 
2461
    table->cursor->startIndexScan(tab->ref.key, 0);
2461
2462
  error= table->cursor->index_read_map(table->record[0],
2462
2463
                                     tab->ref.key_buff,
2463
2464
                                     make_prev_keypart_map(tab->ref.key_parts),
2570
2571
    return(scan_table());
2571
2572
 
2572
2573
  if (!table->cursor->inited)
2573
 
    table->cursor->ha_index_init(tab->ref.key, 1);
 
2574
    table->cursor->startIndexScan(tab->ref.key, 1);
2574
2575
  error= table->cursor->index_read_map(table->record[0],
2575
2576
                                     tab->ref.key_buff,
2576
2577
                                     make_prev_keypart_map(tab->ref.key_parts),
2704
2705
void subselect_uniquesubquery_engine::print(String *str,
2705
2706
                                            enum_query_type query_type)
2706
2707
{
2707
 
  char *table_name= tab->table->s->table_name.str;
 
2708
  char *table_name= const_cast<char *>(tab->table->getShare()->getTableName());
2708
2709
  str->append(STRING_WITH_LEN("<primary_index_lookup>("));
2709
2710
  tab->ref.items[0]->print(str, query_type);
2710
2711
  str->append(STRING_WITH_LEN(" in "));
2711
 
  if (tab->table->s->table_category == TABLE_CATEGORY_TEMPORARY)
 
2712
  if (tab->table->getShare()->isTemporaryCategory())
2712
2713
  {
2713
2714
    /*
2714
2715
      Temporary tables' names change across runs, so they can't be used for
2717
2718
    str->append(STRING_WITH_LEN("<temporary table>"));
2718
2719
  }
2719
2720
  else
2720
 
    str->append(table_name, tab->table->s->table_name.length);
2721
 
  KEY *key_info= tab->table->key_info+ tab->ref.key;
 
2721
    str->append(table_name, tab->table->getShare()->getTableNameSize());
 
2722
  KeyInfo *key_info= tab->table->key_info+ tab->ref.key;
2722
2723
  str->append(STRING_WITH_LEN(" on "));
2723
2724
  str->append(key_info->name);
2724
2725
  if (cond)
2741
2742
  for (uint32_t i= 0; i < key_info->key_parts; i++)
2742
2743
    tab->ref.items[i]->print(str);
2743
2744
  str->append(STRING_WITH_LEN(" in "));
2744
 
  str->append(tab->table->s->table_name.str, tab->table->s->table_name.length);
 
2745
  str->append(tab->table->getShare()->getTableName(), tab->table->getShare()->getTableNameSize());
2745
2746
  str->append(STRING_WITH_LEN(" on "));
2746
2747
  str->append(key_info->name);
2747
2748
  if (cond)
2759
2760
  str->append(STRING_WITH_LEN("<index_lookup>("));
2760
2761
  tab->ref.items[0]->print(str, query_type);
2761
2762
  str->append(STRING_WITH_LEN(" in "));
2762
 
  str->append(tab->table->s->table_name.str, tab->table->s->table_name.length);
2763
 
  KEY *key_info= tab->table->key_info+ tab->ref.key;
 
2763
  str->append(tab->table->getShare()->getTableName(), tab->table->getShare()->getTableNameSize());
 
2764
  KeyInfo *key_info= tab->table->key_info+ tab->ref.key;
2764
2765
  str->append(STRING_WITH_LEN(" on "));
2765
2766
  str->append(key_info->name);
2766
2767
  if (check_null)
2937
2938
  select_union  *tmp_result_sink;
2938
2939
  /* The table into which the subquery is materialized. */
2939
2940
  Table         *tmp_table;
2940
 
  KEY           *tmp_key; /* The only index on the temporary table. */
 
2941
  KeyInfo           *tmp_key; /* The only index on the temporary table. */
2941
2942
  uint32_t          tmp_key_parts; /* Number of keyparts in tmp_key. */
2942
2943
  Item_in_subselect *item_in= (Item_in_subselect *) item;
2943
2944
 
2950
2951
  */
2951
2952
  if (!(tmp_result_sink= new select_union))
2952
2953
    return(true);
 
2954
 
2953
2955
  if (tmp_result_sink->create_result_table(
2954
2956
                         session, tmp_columns, true,
2955
2957
                         session->options | TMP_TABLE_ALL_COLUMNS,
2967
2969
     table since it will not be used, and tell the caller we failed to
2968
2970
     initialize the engine.
2969
2971
  */
2970
 
  if (tmp_table->s->keys == 0)
 
2972
  if (tmp_table->getShare()->sizeKeys() == 0)
2971
2973
  {
2972
 
    assert(tmp_table->s->db_type() == myisam_engine);
 
2974
    assert(tmp_table->getShare()->db_type() == myisam_engine);
2973
2975
    assert(
2974
 
      tmp_table->s->uniques ||
 
2976
      tmp_table->getShare()->uniques ||
2975
2977
      tmp_table->key_info->key_length >= tmp_table->cursor->getEngine()->max_key_length() ||
2976
2978
      tmp_table->key_info->key_parts > tmp_table->cursor->getEngine()->max_key_parts());
2977
 
    tmp_table->free_tmp_table(session);
 
2979
    tmp_table= NULL;
2978
2980
    delete result;
2979
2981
    result= NULL;
2980
2982
    return(true);
2985
2987
    Make sure there is only one index on the temp table, and it doesn't have
2986
2988
    the extra key part created when s->uniques > 0.
2987
2989
  */
2988
 
  assert(tmp_table->s->keys == 1 && tmp_columns->elements == tmp_key_parts);
 
2990
  assert(tmp_table->getShare()->sizeKeys() == 1 && tmp_columns->elements == tmp_key_parts);
2989
2991
 
2990
2992
 
2991
2993
  /* 2. Create/initialize execution related objects. */
3011
3013
        (Item**) session->alloc(sizeof(Item*) * tmp_key_parts)))
3012
3014
    return(true);
3013
3015
 
3014
 
  KEY_PART_INFO *cur_key_part= tmp_key->key_part;
 
3016
  KeyPartInfo *cur_key_part= tmp_key->key_part;
3015
3017
  StoredKey **ref_key= tab->ref.key_copy;
3016
3018
  unsigned char *cur_ref_buff= tab->ref.key_buff;
3017
3019
 
3065
3067
{
3066
3068
  delete result;
3067
3069
  if (tab)
3068
 
    tab->table->free_tmp_table(session);
 
3070
  {
 
3071
    tab->table= NULL;
 
3072
  }
3069
3073
}
3070
3074
 
3071
3075
 
3073
3077
  Cleanup performed after each PS execution.
3074
3078
 
3075
3079
  @detail
3076
 
  Called in the end of JOIN::prepare for PS from Item_subselect::cleanup.
 
3080
  Called in the end of Join::prepare for PS from Item_subselect::cleanup.
3077
3081
*/
3078
3082
 
3079
3083
void subselect_hash_sj_engine::cleanup()
3118
3122
    /*
3119
3123
      TODO:
3120
3124
      - Unlock all subquery tables as we don't need them. To implement this
3121
 
        we need to add new functionality to JOIN::join_free that can unlock
 
3125
        we need to add new functionality to Join::join_free that can unlock
3122
3126
        all tables in a subquery (and all its subqueries).
3123
3127
      - The temp table used for grouping in the subquery can be freed
3124
3128
        immediately after materialization (yet it's done together with