23
- add function from select_query that use JOIN* as parameter to JOIN
23
- add function from mysql_select that use JOIN* as parameter to JOIN
24
24
methods (sql_select.h/sql_select.cc)
29
29
#include <limits.h>
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>
229
223
engine->exclude();
232
session->setWhere("checking transformed subquery");
226
session->where= "checking transformed subquery";
233
227
if (! (*ref)->fixed)
235
229
ret= (*ref)->fix_fields(session, ref);
237
session->setWhere(save_where);
231
session->where= save_where;
241
234
// Is it one field subselect?
540
533
have_to_be_excluded= 1;
541
if (session->getLex()->describe)
534
if (session->lex->describe)
543
536
char warn_buff[DRIZZLE_ERRMSG_SIZE];
544
537
snprintf(warn_buff, sizeof(warn_buff), ER(ER_SELECT_REDUCED), select_lex->select_number);
836
int2_class_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value);
829
int2my_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value);
837
830
return decimal_value;
954
947
if (was_null && !value)
956
int2_class_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value);
949
int2my_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value);
957
950
return decimal_value;
1057
1050
upper_item->set_sum_test(item);
1058
1051
*select_lex->ref_pointer_array= item;
1060
List<Item>::iterator it(select_lex->item_list.begin());
1053
List_iterator<Item> it(select_lex->item_list);
1062
1055
it.replace(item);
1065
save_allow_sum_func= session->getLex()->allow_sum_func;
1066
session->getLex()->allow_sum_func|= 1 << session->getLex()->current_select->nest_level;
1058
save_allow_sum_func= session->lex->allow_sum_func;
1059
session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
1068
1061
Item_sum_(max|min) can't substitute other item => we can use 0 as
1069
1062
reference, also Item_sum_(max|min) can't be fixed after creation, so
1072
1065
if (item->fix_fields(session, 0))
1073
1066
return(RES_ERROR);
1074
session->getLex()->allow_sum_func= save_allow_sum_func;
1067
session->lex->allow_sum_func= save_allow_sum_func;
1075
1068
/* we added aggregate function => we have to change statistic */
1076
1069
count_field_types(select_lex, &join->tmp_table_param, join->all_fields,
1096
1089
Select_Lex_Unit *master_unit= select_lex->master_unit();
1097
1090
substitution= optimizer;
1099
Select_Lex *current= session->getLex()->current_select, *up;
1092
Select_Lex *current= session->lex->current_select, *up;
1101
session->getLex()->current_select= up= current->return_after_parsing();
1094
session->lex->current_select= up= current->return_after_parsing();
1102
1095
//optimizer never use Item **ref => we can pass 0 as parameter
1103
1096
if (!optimizer || optimizer->fix_left(session, 0))
1105
session->getLex()->current_select= current;
1098
session->lex->current_select= current;
1106
1099
return(RES_ERROR);
1108
session->getLex()->current_select= current;
1101
session->lex->current_select= current;
1111
1104
As far as Item_ref_in_optimizer do not substitute itself on fix_fields
1228
1221
Item *having= item, *orig_item= item;
1229
select_lex->item_list.clear();
1222
select_lex->item_list.empty();
1230
1223
select_lex->item_list.push_back(new Item_int("Not_used",
1232
1225
MY_INT64_NUM_DECIMAL_DIGITS));
1334
1327
// fix_field of item will be done in time of substituting
1335
1328
substitution= item;
1336
1329
have_to_be_excluded= 1;
1337
if (session->getLex()->describe)
1330
if (session->lex->describe)
1339
1332
char warn_buff[DRIZZLE_ERRMSG_SIZE];
1340
1333
snprintf(warn_buff, sizeof(warn_buff), ER(ER_SELECT_REDUCED), select_lex->select_number);
1372
1365
Select_Lex_Unit *master_unit= select_lex->master_unit();
1373
1366
substitution= optimizer;
1375
Select_Lex *current= session->getLex()->current_select, *up;
1376
session->getLex()->current_select= up= current->return_after_parsing();
1368
Select_Lex *current= session->lex->current_select, *up;
1369
session->lex->current_select= up= current->return_after_parsing();
1377
1370
//optimizer never use Item **ref => we can pass 0 as parameter
1378
1371
if (!optimizer || optimizer->fix_left(session, 0))
1380
session->getLex()->current_select= current;
1373
session->lex->current_select= current;
1381
1374
return(RES_ERROR);
1384
1377
// we will refer to upper level cache array => we have to save it in PS
1385
1378
optimizer->keep_top_level_cache();
1387
session->getLex()->current_select= current;
1380
session->lex->current_select= current;
1388
1381
master_unit->uncacheable.set(UNCACHEABLE_DEPENDENT);
1390
1383
if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
1392
if (!(pushed_cond_guards= (bool*)join->session->getMemRoot()->allocate(sizeof(bool) *
1385
if (!(pushed_cond_guards= (bool*)join->session->alloc(sizeof(bool) *
1393
1386
left_expr->cols())))
1394
1387
return(RES_ERROR);
1395
1388
for (uint32_t i= 0; i < cols_num; i++)
1656
1649
Item_subselect::trans_res
1657
1650
Item_in_subselect::select_in_like_transformer(Join *join, const Comp_creator *func)
1659
Select_Lex *current= session->getLex()->current_select, *up;
1660
const char *save_where= session->where();
1652
Select_Lex *current= session->lex->current_select, *up;
1653
const char *save_where= session->where;
1661
1654
Item_subselect::trans_res res= RES_ERROR;
1694
session->getLex()->current_select= up= current->return_after_parsing();
1687
session->lex->current_select= up= current->return_after_parsing();
1695
1688
result= (!left_expr->fixed &&
1696
1689
left_expr->fix_fields(session, optimizer->arguments()));
1697
1690
/* fix_fields can change reference to left_expr, we need reassign it */
1698
1691
left_expr= optimizer->arguments()[0];
1700
session->getLex()->current_select= current;
1693
session->lex->current_select= current;
2038
2031
if (!join || !result)
2039
2032
return 1; /* Fatal error is set already. */
2041
Select_Lex *save_select= session->getLex()->current_select;
2042
session->getLex()->current_select= select_lex;
2034
Select_Lex *save_select= session->lex->current_select;
2035
session->lex->current_select= select_lex;
2043
2036
if (join->prepare(&select_lex->ref_pointer_array,
2044
2037
(TableList*) select_lex->table_list.first,
2045
2038
select_lex->with_wild,
2096
2089
void subselect_engine::set_row(List<Item> &item_list, Item_cache **row)
2098
2091
Item *sel_item;
2099
List<Item>::iterator li(item_list.begin());
2092
List_iterator_fast<Item> li(item_list);
2100
2093
res_type= STRING_RESULT;
2101
2094
res_field_type= DRIZZLE_TYPE_VARCHAR;
2102
2095
for (uint32_t i= 0; (sel_item= li++); i++)
2143
int init_read_record_seq(JoinTable *tab);
2144
int join_read_always_key_or_null(JoinTable *tab);
2145
int join_read_next_same_or_null(ReadRecord *info);
2150
2147
int subselect_single_select_engine::exec()
2152
char const *save_where= session->where();
2153
Select_Lex *save_select= session->getLex()->current_select;
2154
session->getLex()->current_select= select_lex;
2149
char const *save_where= session->where;
2150
Select_Lex *save_select= session->lex->current_select;
2151
session->lex->current_select= select_lex;
2155
2152
if (!join->optimized)
2157
2154
Select_Lex_Unit *unit= select_lex->master_unit();
2159
2156
unit->set_limit(unit->global_parameters);
2160
2157
if (join->optimize())
2162
session->setWhere(save_where);
2159
session->where= save_where;
2164
session->getLex()->current_select= save_select;
2161
session->lex->current_select= save_select;
2165
2162
return(join->error ? join->error : 1);
2167
if (save_join_if_explain())
2164
if (select_lex->uncacheable.none() && session->lex->describe &&
2165
!(join->select_options & SELECT_DESCRIBE) &&
2166
join->need_tmp && item->const_item())
2169
Force join->join_tmp creation, because this subquery will be replaced
2170
by a simple select from the materialization temp table by optimize()
2171
called by EXPLAIN and we need to preserve the initial query structure
2172
so we can display it.
2174
select_lex->uncacheable.set(UNCACHEABLE_EXPLAIN);
2175
select_lex->master_unit()->uncacheable.set(UNCACHEABLE_EXPLAIN);
2176
if (join->init_save_join_tab())
2170
2179
if (item->engine_changed)
2237
2246
tab->read_record.read_record= tab->save_read_record;
2240
session->setWhere(save_where);
2241
session->getLex()->current_select= save_select;
2249
session->where= save_where;
2250
session->lex->current_select= save_select;
2242
2251
return(join->error||session->is_fatal_error);
2244
session->setWhere(save_where);
2245
session->getLex()->current_select= save_select;
2253
session->where= save_where;
2254
session->lex->current_select= save_select;
2250
subselect_single_select_engine::save_join_if_explain()
2253
Save this JOIN to join->tmp_join since the original layout will be
2254
replaced when JOIN::exec() calls make_simple_join() if:
2255
1) We are executing an EXPLAIN query
2256
2) An uncacheable flag has not been set for the select_lex. If
2257
set, JOIN::optimize() has already saved the JOIN
2258
3) Call does not come from select_describe()). If it does,
2259
JOIN::exec() will not call make_simple_join() and the JOIN we
2260
plan to save will not be replaced anyway.
2261
4) A temp table is needed. This is what triggers JOIN::exec() to
2262
make a replacement JOIN by calling make_simple_join().
2263
5) The Item_subselect is cacheable
2265
if (session->getLex()->describe && // 1
2266
select_lex->uncacheable.none() && // 2
2267
!(join->select_options & SELECT_DESCRIBE) && // 3
2268
join->need_tmp && // 4
2269
item->const_item()) // 5
2272
Save this JOIN to join->tmp_join since the original layout will
2273
be replaced when JOIN::exec() calls make_simple_join() due to
2274
need_tmp==TRUE. The original layout is needed so we can describe
2275
the query. No need to do this if uncacheable != 0 since in this
2276
case the JOIN has already been saved during JOIN::optimize()
2278
select_lex->uncacheable.set(UNCACHEABLE_EXPLAIN);
2279
select_lex->master_unit()->uncacheable.set(UNCACHEABLE_EXPLAIN);
2280
if (join->init_save_join_tab())
2287
2258
int subselect_union_engine::exec()
2289
char const *save_where= session->where();
2260
char const *save_where= session->where;
2290
2261
int res= unit->exec();
2291
session->setWhere(save_where);
2262
session->where= save_where;
2320
2290
if (table->cursor->inited)
2321
2291
table->cursor->endIndexScan();
2323
if ((error= table->cursor->startTableScan(1)))
2325
table->print_error(error, MYF(0));
2329
assert(table->getSession());
2293
table->cursor->startTableScan(1);
2330
2294
table->cursor->extra_opt(HA_EXTRA_CACHE,
2331
table->getSession()->variables.read_buff_size);
2295
current_session->variables.read_buff_size);
2332
2296
table->null_row= 0;
2503
2467
return(scan_table());
2505
2469
if (!table->cursor->inited)
2507
error= table->cursor->startIndexScan(tab->ref.key, 0);
2511
error= table->report_error(error);
2512
return (error != 0);
2470
table->cursor->startIndexScan(tab->ref.key, 0);
2516
2471
error= table->cursor->index_read_map(table->record[0],
2517
2472
tab->ref.key_buff,
2518
2473
make_prev_keypart_map(tab->ref.key_parts),
2625
2580
return(scan_table());
2627
2582
if (!table->cursor->inited)
2629
error= table->cursor->startIndexScan(tab->ref.key, 1);
2633
error= table->report_error(error);
2583
table->cursor->startIndexScan(tab->ref.key, 1);
2637
2584
error= table->cursor->index_read_map(table->record[0],
2638
2585
tab->ref.key_buff,
2639
2586
make_prev_keypart_map(tab->ref.key_parts),
2779
2726
void subselect_uniquesubquery_engine::print(String *str,
2780
2727
enum_query_type query_type)
2782
const char *table_name= tab->table->getShare()->getTableName();
2729
char *table_name= const_cast<char *>(tab->table->getShare()->getTableName());
2783
2730
str->append(STRING_WITH_LEN("<primary_index_lookup>("));
2784
2731
tab->ref.items[0]->print(str, query_type);
2785
2732
str->append(STRING_WITH_LEN(" in "));
3073
3020
- here we initialize only those members that are used by
3074
3021
subselect_uniquesubquery_engine, so these objects are incomplete.
3076
if (!(tab= (JoinTable*) session->getMemRoot()->allocate(sizeof(JoinTable))))
3023
if (!(tab= (JoinTable*) session->alloc(sizeof(JoinTable))))
3078
new (tab) JoinTable();
3079
3025
tab->table= tmp_table;
3080
3026
tab->ref.key= 0; /* The only temp table index. */
3081
3027
tab->ref.key_length= tmp_key->key_length;
3082
3028
if (!(tab->ref.key_buff=
3083
3029
(unsigned char*) session->calloc(ALIGN_SIZE(tmp_key->key_length) * 2)) ||
3084
3030
!(tab->ref.key_copy=
3085
(StoredKey**) session->getMemRoot()->allocate((sizeof(StoredKey*) *
3031
(StoredKey**) session->alloc((sizeof(StoredKey*) *
3086
3032
(tmp_key_parts + 1)))) ||
3087
3033
!(tab->ref.items=
3088
(Item**) session->getMemRoot()->allocate(sizeof(Item*) * tmp_key_parts)))
3034
(Item**) session->alloc(sizeof(Item*) * tmp_key_parts)))
3091
3037
KeyPartInfo *cur_key_part= tmp_key->key_part;
3186
3132
if (!is_materialized)
3189
Select_Lex *save_select= session->getLex()->current_select;
3190
session->getLex()->current_select= materialize_engine->select_lex;
3135
Select_Lex *save_select= session->lex->current_select;
3136
session->lex->current_select= materialize_engine->select_lex;
3191
3137
if ((res= materialize_join->optimize()))
3194
if (materialize_engine->save_join_if_explain())
3197
3139
materialize_join->exec();
3198
3140
if ((res= test(materialize_join->error || session->is_fatal_error)))