26
26
#include <drizzled/server_includes.h>
27
27
#include <drizzled/sql_select.h>
28
#include <drizzled/error.h>
28
#include <drizzled/drizzled_error_messages.h>
30
30
inline Item * and_items(Item* cond, Item *item)
35
35
Item_subselect::Item_subselect():
36
Item_result_field(), value_assigned(0), session(0), substitution(0),
36
Item_result_field(), value_assigned(0), thd(0), substitution(0),
37
37
engine(0), old_engine(0), used_tables_cache(0), have_to_be_excluded(0),
38
38
const_item_cache(1), engine_changed(0), changed(0),
39
39
is_correlated(false)
152
bool Item_subselect::fix_fields(Session *session_param, Item **ref)
152
bool Item_subselect::fix_fields(THD *thd_param, Item **ref)
154
char const *save_where= session_param->where;
154
char const *save_where= thd_param->where;
155
155
uint8_t uncacheable;
158
158
assert(fixed == 0);
159
engine->set_session((session= session_param));
159
engine->set_thd((thd= thd_param));
161
if (check_stack_overrun(session, STACK_MIN_SIZE, (unsigned char*)&res))
161
if (check_stack_overrun(thd, STACK_MIN_SIZE, (uchar*)&res))
164
164
res= engine->prepare();
189
189
if (have_to_be_excluded)
190
190
engine->exclude();
192
session->where= "checking transformed subquery";
192
thd->where= "checking transformed subquery";
193
193
if (!(*ref)->fixed)
194
ret= (*ref)->fix_fields(session, ref);
195
session->where= save_where;
194
ret= (*ref)->fix_fields(thd, ref);
195
thd->where= save_where;
198
198
// Is it one field subselect?
343
343
return const_item_cache;
346
Item *Item_subselect::get_tmp_table_item(Session *session_arg)
346
Item *Item_subselect::get_tmp_table_item(THD *thd_arg)
348
348
if (!with_sum_func && !const_item())
349
349
return new Item_field(result_field);
350
return copy_or_same(session_arg);
350
return copy_or_same(thd_arg);
353
353
void Item_subselect::update_used_tables()
400
Item_maxmin_subselect::Item_maxmin_subselect(Session *session_param,
400
Item_maxmin_subselect::Item_maxmin_subselect(THD *thd_param,
401
401
Item_subselect *parent,
402
402
st_select_lex *select_lex,
495
495
have_to_be_excluded= 1;
496
if (session->lex->describe)
496
if (thd->lex->describe)
498
498
char warn_buff[DRIZZLE_ERRMSG_SIZE];
499
499
sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number);
500
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
500
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
501
501
ER_SELECT_REDUCED, warn_buff);
503
503
substitution= select_lex->item_list.head();
506
506
'upper' select is not really dependent => we remove this dependence
508
508
substitution->walk(&Item::remove_dependence_processor, 0,
509
(unsigned char *) select_lex->outer_select());
509
(uchar *) select_lex->outer_select());
510
510
return(RES_REDUCE);
516
void Item_singlerow_subselect::store(uint32_t i, Item *item)
516
void Item_singlerow_subselect::store(uint i, Item *item)
518
518
row[i]->store(item);
555
555
maybe_null= engine->may_be_null();
558
uint32_t Item_singlerow_subselect::cols()
558
uint Item_singlerow_subselect::cols()
560
560
return engine->cols();
563
bool Item_singlerow_subselect::check_cols(uint32_t c)
563
bool Item_singlerow_subselect::check_cols(uint c)
565
565
if (c != engine->cols())
1011
1011
it.replace(item);
1014
save_allow_sum_func= session->lex->allow_sum_func;
1015
session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
1014
save_allow_sum_func= thd->lex->allow_sum_func;
1015
thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
1017
1017
Item_sum_(max|min) can't substitute other item => we can use 0 as
1018
1018
reference, also Item_sum_(max|min) can't be fixed after creation, so
1019
1019
we do not check item->fixed
1021
if (item->fix_fields(session, 0))
1021
if (item->fix_fields(thd, 0))
1022
1022
return(RES_ERROR);
1023
session->lex->allow_sum_func= save_allow_sum_func;
1023
thd->lex->allow_sum_func= save_allow_sum_func;
1024
1024
/* we added aggregate function => we have to change statistic */
1025
1025
count_field_types(select_lex, &join->tmp_table_param, join->all_fields,
1032
1032
Item_maxmin_subselect *item;
1033
subs= item= new Item_maxmin_subselect(session, this, select_lex, func->l_op());
1033
subs= item= new Item_maxmin_subselect(thd, this, select_lex, func->l_op());
1034
1034
if (upper_item)
1035
1035
upper_item->set_sub_test(item);
1045
1045
SELECT_LEX_UNIT *master_unit= select_lex->master_unit();
1046
1046
substitution= optimizer;
1048
SELECT_LEX *current= session->lex->current_select, *up;
1048
SELECT_LEX *current= thd->lex->current_select, *up;
1050
session->lex->current_select= up= current->return_after_parsing();
1050
thd->lex->current_select= up= current->return_after_parsing();
1051
1051
//optimizer never use Item **ref => we can pass 0 as parameter
1052
if (!optimizer || optimizer->fix_left(session, 0))
1052
if (!optimizer || optimizer->fix_left(thd, 0))
1054
session->lex->current_select= current;
1054
thd->lex->current_select= current;
1055
1055
return(RES_ERROR);
1057
session->lex->current_select= current;
1057
thd->lex->current_select= current;
1060
1060
As far as Item_ref_in_optimizer do not substitute itself on fix_fields
1071
1071
if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
1073
if (!(pushed_cond_guards= (bool*)join->session->alloc(sizeof(bool))))
1073
if (!(pushed_cond_guards= (bool*)join->thd->alloc(sizeof(bool))))
1074
1074
return(RES_ERROR);
1075
1075
pushed_cond_guards[0]= true;
1162
1162
we do not check join->having->fixed, because Item_and (from and_items)
1163
1163
or comparison function (from func->create) can't be fixed after creation
1165
tmp= join->having->fix_fields(session, 0);
1165
tmp= join->having->fix_fields(thd, 0);
1166
1166
select_lex->having_fix_field= 0;
1168
1168
return(RES_ERROR);
1204
1204
and_items) or comparison function (from func->create) can't be
1205
1205
fixed after creation
1207
tmp= join->having->fix_fields(session, 0);
1207
tmp= join->having->fix_fields(thd, 0);
1208
1208
select_lex->having_fix_field= 0;
1210
1210
return(RES_ERROR);
1238
1238
we do not check join->conds->fixed, because Item_and can't be fixed
1241
if (join->conds->fix_fields(session, 0))
1241
if (join->conds->fix_fields(thd, 0))
1242
1242
return(RES_ERROR);
1271
1271
we do not check join->having->fixed, because comparison function
1272
1272
(from func->create) can't be fixed after creation
1274
tmp= join->having->fix_fields(session, 0);
1274
tmp= join->having->fix_fields(thd, 0);
1275
1275
select_lex->having_fix_field= 0;
1277
1277
return(RES_ERROR);
1283
1283
// fix_field of item will be done in time of substituting
1284
1284
substitution= item;
1285
1285
have_to_be_excluded= 1;
1286
if (session->lex->describe)
1286
if (thd->lex->describe)
1288
1288
char warn_buff[DRIZZLE_ERRMSG_SIZE];
1289
1289
sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number);
1290
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1290
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1291
1291
ER_SELECT_REDUCED, warn_buff);
1293
1293
return(RES_REDUCE);
1303
1303
Item_in_subselect::row_value_transformer(JOIN *join)
1305
1305
SELECT_LEX *select_lex= join->select_lex;
1306
uint32_t cols_num= left_expr->cols();
1306
uint cols_num= left_expr->cols();
1308
1308
if (select_lex->item_list.elements != left_expr->cols())
1321
1321
SELECT_LEX_UNIT *master_unit= select_lex->master_unit();
1322
1322
substitution= optimizer;
1324
SELECT_LEX *current= session->lex->current_select, *up;
1325
session->lex->current_select= up= current->return_after_parsing();
1324
SELECT_LEX *current= thd->lex->current_select, *up;
1325
thd->lex->current_select= up= current->return_after_parsing();
1326
1326
//optimizer never use Item **ref => we can pass 0 as parameter
1327
if (!optimizer || optimizer->fix_left(session, 0))
1327
if (!optimizer || optimizer->fix_left(thd, 0))
1329
session->lex->current_select= current;
1329
thd->lex->current_select= current;
1330
1330
return(RES_ERROR);
1333
1333
// we will refer to upper level cache array => we have to save it in PS
1334
1334
optimizer->keep_top_level_cache();
1336
session->lex->current_select= current;
1336
thd->lex->current_select= current;
1337
1337
master_unit->uncacheable|= UNCACHEABLE_DEPENDENT;
1339
1339
if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
1341
if (!(pushed_cond_guards= (bool*)join->session->alloc(sizeof(bool) *
1341
if (!(pushed_cond_guards= (bool*)join->thd->alloc(sizeof(bool) *
1342
1342
left_expr->cols())))
1343
1343
return(RES_ERROR);
1344
for (uint32_t i= 0; i < cols_num; i++)
1344
for (uint i= 0; i < cols_num; i++)
1345
1345
pushed_cond_guards[i]= true;
1382
1382
SELECT_LEX *select_lex= join->select_lex;
1383
1383
Item *having_item= 0;
1384
uint32_t cols_num= left_expr->cols();
1384
uint cols_num= left_expr->cols();
1385
1385
bool is_having_used= (join->having || select_lex->with_sum_func ||
1386
1386
select_lex->group_list.first ||
1387
1387
!select_lex->table_list.elements);
1403
1403
TODO: say here explicitly if the order of AND parts matters or not.
1405
1405
Item *item_having_part2= 0;
1406
for (uint32_t i= 0; i < cols_num; i++)
1406
for (uint i= 0; i < cols_num; i++)
1408
1408
assert((left_expr->fixed && select_lex->ref_pointer_array[i]->fixed) ||
1409
1409
(select_lex->ref_pointer_array[i]->type() == REF_ITEM &&
1481
1481
Item *where_item= 0;
1482
for (uint32_t i= 0; i < cols_num; i++)
1482
for (uint i= 0; i < cols_num; i++)
1484
1484
Item *item, *item_isnull;
1485
1485
assert((left_expr->fixed && select_lex->ref_pointer_array[i]->fixed) ||
1547
1547
select_lex->where= join->conds= and_items(join->conds, where_item);
1548
1548
select_lex->where->top_level_item();
1549
if (join->conds->fix_fields(session, 0))
1549
if (join->conds->fix_fields(thd, 0))
1550
1550
return(RES_ERROR);
1552
1552
if (having_item)
1562
1562
argument (reference) to fix_fields()
1564
1564
select_lex->having_fix_field= 1;
1565
res= join->having->fix_fields(session, 0);
1565
res= join->having->fix_fields(thd, 0);
1566
1566
select_lex->having_fix_field= 0;
1605
1605
Item_subselect::trans_res
1606
1606
Item_in_subselect::select_in_like_transformer(JOIN *join, Comp_creator *func)
1608
SELECT_LEX *current= session->lex->current_select, *up;
1609
const char *save_where= session->where;
1608
SELECT_LEX *current= thd->lex->current_select, *up;
1609
const char *save_where= thd->where;
1610
1610
Item_subselect::trans_res res= RES_ERROR;
1643
session->lex->current_select= up= current->return_after_parsing();
1643
thd->lex->current_select= up= current->return_after_parsing();
1644
1644
result= (!left_expr->fixed &&
1645
left_expr->fix_fields(session, optimizer->arguments()));
1645
left_expr->fix_fields(thd, optimizer->arguments()));
1646
1646
/* fix_fields can change reference to left_expr, we need reassign it */
1647
1647
left_expr= optimizer->arguments()[0];
1649
session->lex->current_select= current;
1649
thd->lex->current_select= current;
1698
bool Item_in_subselect::fix_fields(Session *session_arg, Item **ref)
1698
bool Item_in_subselect::fix_fields(THD *thd_arg, Item **ref)
1700
1700
bool result = 0;
1702
1702
if (exec_method == SEMI_JOIN)
1703
1703
return !( (*ref)= new Item_int(1));
1705
return result || Item_subselect::fix_fields(session_arg, ref);
1705
return result || Item_subselect::fix_fields(thd_arg, ref);
1743
1743
old_engine= (subselect_single_select_engine*) engine;
1745
if (!(new_engine= new subselect_hash_sj_engine(session, this,
1745
if (!(new_engine= new subselect_hash_sj_engine(thd, this,
1746
1746
old_engine)) ||
1747
1747
new_engine->init_permanent(unit->get_unit_column_types()))
1828
1828
if (!(left_expr_cache= new List<Cached_item>))
1831
for (uint32_t i= 0; i < left_expr->cols(); i++)
1831
for (uint i= 0; i < left_expr->cols(); i++)
1833
Cached_item *cur_item_cache= new_Cached_item(session,
1833
Cached_item *cur_item_cache= new_Cached_item(thd,
1834
1834
left_expr->element_index(i),
1835
1835
use_result_field);
1836
1836
if (!cur_item_cache || left_expr_cache->push_front(cur_item_cache))
2000
join= new JOIN(session, select_lex->item_list,
2000
join= new JOIN(thd, select_lex->item_list,
2001
2001
select_lex->options | SELECT_NO_UNLOCK, result);
2002
2002
if (!join || !result)
2003
2003
return 1; /* Fatal error is set already. */
2005
SELECT_LEX *save_select= session->lex->current_select;
2006
session->lex->current_select= select_lex;
2005
SELECT_LEX *save_select= thd->lex->current_select;
2006
thd->lex->current_select= select_lex;
2007
2007
if (join->prepare(&select_lex->ref_pointer_array,
2008
2008
(TableList*) select_lex->table_list.first,
2009
2009
select_lex->with_wild,
2016
2016
(order_st*) 0, select_lex,
2017
2017
select_lex->master_unit()))
2019
session->lex->current_select= save_select;
2019
thd->lex->current_select= save_select;
2023
2023
int subselect_union_engine::prepare()
2025
return unit->prepare(session, result, SELECT_NO_UNLOCK);
2025
return unit->prepare(thd, result, SELECT_NO_UNLOCK);
2028
2028
int subselect_uniquesubquery_engine::prepare()
2064
2064
List_iterator_fast<Item> li(item_list);
2065
2065
res_type= STRING_RESULT;
2066
2066
res_field_type= DRIZZLE_TYPE_VARCHAR;
2067
for (uint32_t i= 0; (sel_item= li++); i++)
2067
for (uint i= 0; (sel_item= li++); i++)
2069
2069
item->max_length= sel_item->max_length;
2070
2070
res_type= sel_item->result_type();
2119
2119
int subselect_single_select_engine::exec()
2121
char const *save_where= session->where;
2122
SELECT_LEX *save_select= session->lex->current_select;
2123
session->lex->current_select= select_lex;
2121
char const *save_where= thd->where;
2122
SELECT_LEX *save_select= thd->lex->current_select;
2123
thd->lex->current_select= select_lex;
2124
2124
if (!join->optimized)
2126
2126
SELECT_LEX_UNIT *unit= select_lex->master_unit();
2128
2128
unit->set_limit(unit->global_parameters);
2129
2129
if (join->flatten_subqueries())
2131
session->is_fatal_error= true;
2131
thd->is_fatal_error= true;
2134
2134
if (join->optimize())
2136
session->where= save_where;
2136
thd->where= save_where;
2138
session->lex->current_select= save_select;
2138
thd->lex->current_select= save_select;
2139
2139
return(join->error ? join->error : 1);
2141
if (!select_lex->uncacheable && session->lex->describe &&
2141
if (!select_lex->uncacheable && thd->lex->describe &&
2142
2142
!(join->select_options & SELECT_DESCRIBE) &&
2143
2143
join->need_tmp && item->const_item())
2184
2184
pushed down into the subquery. Those optimizations are ref[_or_null]
2185
2185
acceses. Change them to be full table scans.
2187
for (uint32_t i=join->const_tables ; i < join->tables ; i++)
2187
for (uint i=join->const_tables ; i < join->tables ; i++)
2189
2189
JOIN_TAB *tab=join->join_tab+i;
2190
2190
if (tab && tab->keyuse)
2192
for (uint32_t i= 0; i < tab->ref.key_parts; i++)
2192
for (uint i= 0; i < tab->ref.key_parts; i++)
2194
2194
bool *cond_guard= tab->ref.cond_guards[i];
2195
2195
if (cond_guard && !*cond_guard)
2199
2199
tab->save_read_record= tab->read_record.read_record;
2200
2200
tab->read_first_record= init_read_record_seq;
2201
2201
tab->read_record.record= tab->table->record[0];
2202
tab->read_record.session= join->session;
2202
tab->read_record.thd= join->thd;
2203
2203
tab->read_record.ref_length= tab->table->file->ref_length;
2204
2204
*(last_changed_tab++)= tab;
2221
2221
tab->read_record.read_record= tab->save_read_record;
2224
session->where= save_where;
2225
session->lex->current_select= save_select;
2226
return(join->error||session->is_fatal_error);
2224
thd->where= save_where;
2225
thd->lex->current_select= save_select;
2226
return(join->error||thd->is_fatal_error);
2228
session->where= save_where;
2229
session->lex->current_select= save_select;
2228
thd->where= save_where;
2229
thd->lex->current_select= save_select;
2233
2233
int subselect_union_engine::exec()
2235
char const *save_where= session->where;
2235
char const *save_where= thd->where;
2236
2236
int res= unit->exec();
2237
session->where= save_where;
2237
thd->where= save_where;
2268
2268
table->file->ha_rnd_init(1);
2269
2269
table->file->extra_opt(HA_EXTRA_CACHE,
2270
current_session->variables.read_buff_size);
2270
current_thd->variables.read_buff_size);
2271
2271
table->null_row= 0;
2609
uint32_t subselect_single_select_engine::cols()
2609
uint subselect_single_select_engine::cols()
2611
2611
return select_lex->item_list.elements;
2615
uint32_t subselect_union_engine::cols()
2615
uint subselect_union_engine::cols()
2617
2617
return unit->types.elements;
2677
2677
void subselect_single_select_engine::print(String *str,
2678
2678
enum_query_type query_type)
2680
select_lex->print(session, str, query_type);
2680
select_lex->print(thd, str, query_type);
2725
2725
KEY *key_info= tab->table->key_info + tab->ref.key;
2726
2726
str->append(STRING_WITH_LEN("<primary_index_lookup>("));
2727
for (uint32_t i= 0; i < key_info->key_parts; i++)
2727
for (uint i= 0; i < key_info->key_parts; i++)
2728
2728
tab->ref.items[i]->print(str);
2729
2729
str->append(STRING_WITH_LEN(" in "));
2730
2730
str->append(tab->table->s->table_name.str, tab->table->s->table_name.length);
2924
2924
/* The table into which the subquery is materialized. */
2925
2925
Table *tmp_table;
2926
2926
KEY *tmp_key; /* The only index on the temporary table. */
2927
uint32_t tmp_key_parts; /* Number of keyparts in tmp_key. */
2927
uint tmp_key_parts; /* Number of keyparts in tmp_key. */
2928
2928
Item_in_subselect *item_in= (Item_in_subselect *) item;
2930
2930
/* 1. Create/initialize materialization related objects. */
2937
2937
if (!(tmp_result_sink= new select_union))
2939
2939
if (tmp_result_sink->create_result_table(
2940
session, tmp_columns, true,
2941
session->options | TMP_TABLE_ALL_COLUMNS,
2940
thd, tmp_columns, true,
2941
thd->options | TMP_TABLE_ALL_COLUMNS,
2942
2942
"materialized subselect", true))
2960
2960
tmp_table->s->uniques ||
2961
2961
tmp_table->key_info->key_length >= tmp_table->file->max_key_length() ||
2962
2962
tmp_table->key_info->key_parts > tmp_table->file->max_key_parts());
2963
tmp_table->free_tmp_table(session);
2963
tmp_table->free_tmp_table(thd);
2983
2983
- here we initialize only those members that are used by
2984
2984
subselect_uniquesubquery_engine, so these objects are incomplete.
2986
if (!(tab= (JOIN_TAB*) session->alloc(sizeof(JOIN_TAB))))
2986
if (!(tab= (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB))))
2988
2988
tab->table= tmp_table;
2989
2989
tab->ref.key= 0; /* The only temp table index. */
2990
2990
tab->ref.key_length= tmp_key->key_length;
2991
2991
if (!(tab->ref.key_buff=
2992
(unsigned char*) session->calloc(ALIGN_SIZE(tmp_key->key_length) * 2)) ||
2992
(uchar*) thd->calloc(ALIGN_SIZE(tmp_key->key_length) * 2)) ||
2993
2993
!(tab->ref.key_copy=
2994
(store_key**) session->alloc((sizeof(store_key*) *
2994
(store_key**) thd->alloc((sizeof(store_key*) *
2995
2995
(tmp_key_parts + 1)))) ||
2996
2996
!(tab->ref.items=
2997
(Item**) session->alloc(sizeof(Item*) * tmp_key_parts)))
2997
(Item**) thd->alloc(sizeof(Item*) * tmp_key_parts)))
3000
3000
KEY_PART_INFO *cur_key_part= tmp_key->key_part;
3001
3001
store_key **ref_key= tab->ref.key_copy;
3002
unsigned char *cur_ref_buff= tab->ref.key_buff;
3002
uchar *cur_ref_buff= tab->ref.key_buff;
3004
for (uint32_t i= 0; i < tmp_key_parts; i++, cur_key_part++, ref_key++)
3004
for (uint i= 0; i < tmp_key_parts; i++, cur_key_part++, ref_key++)
3006
3006
tab->ref.items[i]= item_in->left_expr->element_index(i);
3007
3007
int null_count= test(cur_key_part->field->real_maybe_null());
3008
*ref_key= new store_key_item(session, cur_key_part->field,
3008
*ref_key= new store_key_item(thd, cur_key_part->field,
3010
3010
the NULL byte is taken into account in
3011
3011
cur_key_part->store_length, so instead of
3093
3093
if (!is_materialized)
3096
SELECT_LEX *save_select= session->lex->current_select;
3097
session->lex->current_select= materialize_engine->select_lex;
3096
SELECT_LEX *save_select= thd->lex->current_select;
3097
thd->lex->current_select= materialize_engine->select_lex;
3098
3098
if ((res= materialize_join->optimize()))
3100
3100
materialize_join->exec();
3101
if ((res= test(materialize_join->error || session->is_fatal_error)))
3101
if ((res= test(materialize_join->error || thd->is_fatal_error)))