~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/subselect.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

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