~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/subselect.cc

  • Committer: Lee Bieber
  • Date: 2011-02-11 20:30:05 UTC
  • mfrom: (2157.1.3 build)
  • Revision ID: kalebral@gmail.com-20110211203005-757o1y2yf78dxzqr
Merge Stewart - 716848: drizzleimport displays wrong program_name
Merge Stewart - update README file
Merge Andrew and Joe - Exposes the InnoDB SYS_REPLICATION_LOG to data_dictionary so that it is fast and fixes many issues we have been having

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
    - add function from select_query that use JOIN* as parameter to JOIN
24
24
    methods (sql_select.h/sql_select.cc)
25
25
*/
26
 
#include <config.h>
 
26
#include "config.h"
27
27
 
28
28
#include <cstdio>
29
29
#include <limits.h>
30
30
 
31
 
#include <drizzled/session.h>
32
31
#include <drizzled/sql_select.h>
33
32
#include <drizzled/error.h>
34
33
#include <drizzled/item/cache.h>
40
39
#include <drizzled/item/ref_null_helper.h>
41
40
#include <drizzled/item/direct_ref.h>
42
41
#include <drizzled/join.h>
43
 
#include <drizzled/plugin/storage_engine.h>
44
 
#include <drizzled/select_singlerow_subselect.h>
45
 
#include <drizzled/select_max_min_finder_subselect.h>
46
 
#include <drizzled/select_exists_subselect.h>
47
 
#include <drizzled/select_union.h>
48
42
 
49
43
namespace drizzled
50
44
{
273
267
  {
274
268
    for (Select_Lex *lex= unit->first_select(); lex; lex= lex->next_select())
275
269
    {
276
 
      List<Item>::iterator li(lex->item_list.begin());
 
270
      List_iterator<Item> li(lex->item_list);
277
271
      Item *item;
278
272
      Order *order;
279
273
 
538
532
  {
539
533
 
540
534
    have_to_be_excluded= 1;
541
 
    if (session->getLex()->describe)
 
535
    if (session->lex->describe)
542
536
    {
543
537
      char warn_buff[DRIZZLE_ERRMSG_SIZE];
544
538
      snprintf(warn_buff, sizeof(warn_buff), ER(ER_SELECT_REDUCED), select_lex->select_number);
1057
1051
        upper_item->set_sum_test(item);
1058
1052
      *select_lex->ref_pointer_array= item;
1059
1053
      {
1060
 
        List<Item>::iterator it(select_lex->item_list.begin());
 
1054
        List_iterator<Item> it(select_lex->item_list);
1061
1055
        it++;
1062
1056
        it.replace(item);
1063
1057
      }
1064
1058
 
1065
 
      save_allow_sum_func= session->getLex()->allow_sum_func;
1066
 
      session->getLex()->allow_sum_func|= 1 << session->getLex()->current_select->nest_level;
 
1059
      save_allow_sum_func= session->lex->allow_sum_func;
 
1060
      session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
1067
1061
      /*
1068
1062
        Item_sum_(max|min) can't substitute other item => we can use 0 as
1069
1063
        reference, also Item_sum_(max|min) can't be fixed after creation, so
1071
1065
      */
1072
1066
      if (item->fix_fields(session, 0))
1073
1067
        return(RES_ERROR);
1074
 
      session->getLex()->allow_sum_func= save_allow_sum_func;
 
1068
      session->lex->allow_sum_func= save_allow_sum_func;
1075
1069
      /* we added aggregate function => we have to change statistic */
1076
1070
      count_field_types(select_lex, &join->tmp_table_param, join->all_fields,
1077
1071
                        0);
1096
1090
    Select_Lex_Unit *master_unit= select_lex->master_unit();
1097
1091
    substitution= optimizer;
1098
1092
 
1099
 
    Select_Lex *current= session->getLex()->current_select, *up;
 
1093
    Select_Lex *current= session->lex->current_select, *up;
1100
1094
 
1101
 
    session->getLex()->current_select= up= current->return_after_parsing();
 
1095
    session->lex->current_select= up= current->return_after_parsing();
1102
1096
    //optimizer never use Item **ref => we can pass 0 as parameter
1103
1097
    if (!optimizer || optimizer->fix_left(session, 0))
1104
1098
    {
1105
 
      session->getLex()->current_select= current;
 
1099
      session->lex->current_select= current;
1106
1100
      return(RES_ERROR);
1107
1101
    }
1108
 
    session->getLex()->current_select= current;
 
1102
    session->lex->current_select= current;
1109
1103
 
1110
1104
    /*
1111
1105
      As far as  Item_ref_in_optimizer do not substitute itself on fix_fields
1121
1115
 
1122
1116
  if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
1123
1117
  {
1124
 
    if (!(pushed_cond_guards= (bool*)join->session->getMemRoot()->allocate(sizeof(bool))))
 
1118
    if (!(pushed_cond_guards= (bool*)join->session->alloc(sizeof(bool))))
1125
1119
      return(RES_ERROR);
1126
1120
    pushed_cond_guards[0]= true;
1127
1121
  }
1226
1220
    {
1227
1221
      bool tmp;
1228
1222
      Item *having= item, *orig_item= item;
1229
 
      select_lex->item_list.clear();
 
1223
      select_lex->item_list.empty();
1230
1224
      select_lex->item_list.push_back(new Item_int("Not_used",
1231
1225
                                                   (int64_t) 1,
1232
1226
                                                   MY_INT64_NUM_DECIMAL_DIGITS));
1334
1328
        // fix_field of item will be done in time of substituting
1335
1329
        substitution= item;
1336
1330
        have_to_be_excluded= 1;
1337
 
        if (session->getLex()->describe)
 
1331
        if (session->lex->describe)
1338
1332
        {
1339
1333
          char warn_buff[DRIZZLE_ERRMSG_SIZE];
1340
1334
          snprintf(warn_buff, sizeof(warn_buff), ER(ER_SELECT_REDUCED), select_lex->select_number);
1372
1366
    Select_Lex_Unit *master_unit= select_lex->master_unit();
1373
1367
    substitution= optimizer;
1374
1368
 
1375
 
    Select_Lex *current= session->getLex()->current_select, *up;
1376
 
    session->getLex()->current_select= up= current->return_after_parsing();
 
1369
    Select_Lex *current= session->lex->current_select, *up;
 
1370
    session->lex->current_select= up= current->return_after_parsing();
1377
1371
    //optimizer never use Item **ref => we can pass 0 as parameter
1378
1372
    if (!optimizer || optimizer->fix_left(session, 0))
1379
1373
    {
1380
 
      session->getLex()->current_select= current;
 
1374
      session->lex->current_select= current;
1381
1375
      return(RES_ERROR);
1382
1376
    }
1383
1377
 
1384
1378
    // we will refer to upper level cache array => we have to save it in PS
1385
1379
    optimizer->keep_top_level_cache();
1386
1380
 
1387
 
    session->getLex()->current_select= current;
 
1381
    session->lex->current_select= current;
1388
1382
    master_unit->uncacheable.set(UNCACHEABLE_DEPENDENT);
1389
1383
 
1390
1384
    if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
1391
1385
    {
1392
 
      if (!(pushed_cond_guards= (bool*)join->session->getMemRoot()->allocate(sizeof(bool) *
 
1386
      if (!(pushed_cond_guards= (bool*)join->session->alloc(sizeof(bool) *
1393
1387
                                                        left_expr->cols())))
1394
1388
        return(RES_ERROR);
1395
1389
      for (uint32_t i= 0; i < cols_num; i++)
1656
1650
Item_subselect::trans_res
1657
1651
Item_in_subselect::select_in_like_transformer(Join *join, const Comp_creator *func)
1658
1652
{
1659
 
  Select_Lex *current= session->getLex()->current_select, *up;
 
1653
  Select_Lex *current= session->lex->current_select, *up;
1660
1654
  const char *save_where= session->where();
1661
1655
  Item_subselect::trans_res res= RES_ERROR;
1662
1656
  bool result;
1691
1685
      goto err;
1692
1686
  }
1693
1687
 
1694
 
  session->getLex()->current_select= up= current->return_after_parsing();
 
1688
  session->lex->current_select= up= current->return_after_parsing();
1695
1689
  result= (!left_expr->fixed &&
1696
1690
           left_expr->fix_fields(session, optimizer->arguments()));
1697
1691
  /* fix_fields can change reference to left_expr, we need reassign it */
1698
1692
  left_expr= optimizer->arguments()[0];
1699
1693
 
1700
 
  session->getLex()->current_select= current;
 
1694
  session->lex->current_select= current;
1701
1695
  if (result)
1702
1696
    goto err;
1703
1697
 
2038
2032
  if (!join || !result)
2039
2033
    return 1; /* Fatal error is set already. */
2040
2034
  prepared= 1;
2041
 
  Select_Lex *save_select= session->getLex()->current_select;
2042
 
  session->getLex()->current_select= select_lex;
 
2035
  Select_Lex *save_select= session->lex->current_select;
 
2036
  session->lex->current_select= select_lex;
2043
2037
  if (join->prepare(&select_lex->ref_pointer_array,
2044
2038
                    (TableList*) select_lex->table_list.first,
2045
2039
                    select_lex->with_wild,
2051
2045
                    select_lex->having,
2052
2046
                    select_lex, select_lex->master_unit()))
2053
2047
    return 1;
2054
 
  session->getLex()->current_select= save_select;
 
2048
  session->lex->current_select= save_select;
2055
2049
  return 0;
2056
2050
}
2057
2051
 
2096
2090
void subselect_engine::set_row(List<Item> &item_list, Item_cache **row)
2097
2091
{
2098
2092
  Item *sel_item;
2099
 
  List<Item>::iterator li(item_list.begin());
 
2093
  List_iterator_fast<Item> li(item_list);
2100
2094
  res_type= STRING_RESULT;
2101
2095
  res_field_type= DRIZZLE_TYPE_VARCHAR;
2102
2096
  for (uint32_t i= 0; (sel_item= li++); i++)
2150
2144
int subselect_single_select_engine::exec()
2151
2145
{
2152
2146
  char const *save_where= session->where();
2153
 
  Select_Lex *save_select= session->getLex()->current_select;
2154
 
  session->getLex()->current_select= select_lex;
 
2147
  Select_Lex *save_select= session->lex->current_select;
 
2148
  session->lex->current_select= select_lex;
2155
2149
  if (!join->optimized)
2156
2150
  {
2157
2151
    Select_Lex_Unit *unit= select_lex->master_unit();
2161
2155
    {
2162
2156
      session->setWhere(save_where);
2163
2157
      executed= 1;
2164
 
      session->getLex()->current_select= save_select;
 
2158
      session->lex->current_select= save_select;
2165
2159
      return(join->error ? join->error : 1);
2166
2160
    }
2167
2161
    if (save_join_if_explain())
2179
2173
    if (join->reinit())
2180
2174
    {
2181
2175
      session->setWhere(save_where);
2182
 
      session->getLex()->current_select= save_select;
 
2176
      session->lex->current_select= save_select;
2183
2177
      return 1;
2184
2178
    }
2185
2179
    item->reset();
2238
2232
    }
2239
2233
    executed= 1;
2240
2234
    session->setWhere(save_where);
2241
 
    session->getLex()->current_select= save_select;
 
2235
    session->lex->current_select= save_select;
2242
2236
    return(join->error||session->is_fatal_error);
2243
2237
  }
2244
2238
  session->setWhere(save_where);
2245
 
  session->getLex()->current_select= save_select;
 
2239
  session->lex->current_select= save_select;
2246
2240
  return(0);
2247
2241
}
2248
2242
 
2262
2256
        make a replacement JOIN by calling make_simple_join(). 
2263
2257
     5) The Item_subselect is cacheable
2264
2258
  */
2265
 
  if (session->getLex()->describe &&                          // 1
 
2259
  if (session->lex->describe &&                          // 1
2266
2260
      select_lex->uncacheable.none() &&                  // 2
2267
2261
      !(join->select_options & SELECT_DESCRIBE) &&       // 3
2268
2262
      join->need_tmp &&                                  // 4
2779
2773
void subselect_uniquesubquery_engine::print(String *str,
2780
2774
                                            enum_query_type query_type)
2781
2775
{
2782
 
  const char *table_name= tab->table->getShare()->getTableName();
 
2776
  char *table_name= const_cast<char *>(tab->table->getShare()->getTableName());
2783
2777
  str->append(STRING_WITH_LEN("<primary_index_lookup>("));
2784
2778
  tab->ref.items[0]->print(str, query_type);
2785
2779
  str->append(STRING_WITH_LEN(" in "));
3073
3067
    - here we initialize only those members that are used by
3074
3068
      subselect_uniquesubquery_engine, so these objects are incomplete.
3075
3069
  */
3076
 
  if (!(tab= (JoinTable*) session->getMemRoot()->allocate(sizeof(JoinTable))))
 
3070
  if (!(tab= (JoinTable*) session->alloc(sizeof(JoinTable))))
3077
3071
    return(true);
3078
3072
  new (tab) JoinTable();
3079
3073
  tab->table= tmp_table;
3082
3076
  if (!(tab->ref.key_buff=
3083
3077
        (unsigned char*) session->calloc(ALIGN_SIZE(tmp_key->key_length) * 2)) ||
3084
3078
      !(tab->ref.key_copy=
3085
 
        (StoredKey**) session->getMemRoot()->allocate((sizeof(StoredKey*) *
 
3079
        (StoredKey**) session->alloc((sizeof(StoredKey*) *
3086
3080
                                  (tmp_key_parts + 1)))) ||
3087
3081
      !(tab->ref.items=
3088
 
        (Item**) session->getMemRoot()->allocate(sizeof(Item*) * tmp_key_parts)))
 
3082
        (Item**) session->alloc(sizeof(Item*) * tmp_key_parts)))
3089
3083
    return(true);
3090
3084
 
3091
3085
  KeyPartInfo *cur_key_part= tmp_key->key_part;
3186
3180
  if (!is_materialized)
3187
3181
  {
3188
3182
    int res= 0;
3189
 
    Select_Lex *save_select= session->getLex()->current_select;
3190
 
    session->getLex()->current_select= materialize_engine->select_lex;
 
3183
    Select_Lex *save_select= session->lex->current_select;
 
3184
    session->lex->current_select= materialize_engine->select_lex;
3191
3185
    if ((res= materialize_join->optimize()))
3192
3186
      goto err;
3193
3187
 
3228
3222
      tmp_param= NULL;
3229
3223
 
3230
3224
err:
3231
 
    session->getLex()->current_select= save_select;
 
3225
    session->lex->current_select= save_select;
3232
3226
    if (res)
3233
3227
      return(res);
3234
3228
  }