-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
item_subselect.cc
executable file
·496 lines (409 loc) · 13.3 KB
/
item_subselect.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights
reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
/**
@file
@brief
subselect Item
@todo
- add function from mysql_select that use JOIN* as parameter to JOIN
methods (sql_select.h/sql_select.cc)
*/
#include "sql_priv.h"
/*
It is necessary to include set_var.h instead of item.h because there
are dependencies on include order for set_var.h and item.h. This
will be resolved later.
*/
#include "sql_class.h" // set_var.h: THD
#include "set_var.h"
#include "sql_parse.h" // check_stack_overrun
#include "sql_derived.h" // mysql_derived_create, ...
#include "sql_test.h"
#include "opt_explain_format.h"
Item_subselect::Item_subselect():
Item_result_field(), value_assigned(0), traced_before(false),
substitution(NULL), in_cond_of_tab(INT_MIN),
used_tables_cache(0), have_to_be_excluded(0), const_item_cache(1)
{
with_subselect= 1;
reset();
/*
Item value is NULL if select_result_interceptor didn't change this value
(i.e. some rows will be found returned)
*/
null_value= TRUE;
}
void Item_subselect::init(st_select_lex *select_lex,
select_result_interceptor *result)
{
/*
Please see Item_singlerow_subselect::invalidate_and_restore_select_lex(),
which depends on alterations to the parse tree implemented here.
*/
DBUG_ENTER("Item_subselect::init");
DBUG_PRINT("enter", ("select_lex: 0x%lx", (long) select_lex));
unit= select_lex->master_unit();
if (unit->item)
{
parsing_place= unit->item->parsing_place;
unit->item= this;
}
else
{
SELECT_LEX *outer_select= unit->outer_select();
/*
do not take into account expression inside aggregate functions because
they can access original table fields
*/
parsing_place= (outer_select->in_sum_expr ?
NO_MATTER :
outer_select->parsing_place);
}
{
SELECT_LEX *upper= unit->outer_select();
if (upper->parsing_place == IN_HAVING)
upper->subquery_in_having= 1;
}
DBUG_VOID_RETURN;
}
Item_subselect::~Item_subselect()
{
}
/**
Apply walk() processor to join conditions.
JOINs may be nested. Walk nested joins recursively to apply the
processor.
*/
bool Item_subselect::walk_join_condition(List<TABLE_LIST> *tables,
Item_processor processor,
bool walk_subquery,
uchar *argument)
{
TABLE_LIST *table;
List_iterator<TABLE_LIST> li(*tables);
while ((table= li++))
{
if (table->join_cond() &&
table->join_cond()->walk(processor, walk_subquery, argument))
return true;
if (table->nested_join != NULL &&
walk_join_condition(&table->nested_join->join_list, processor,
walk_subquery, argument))
return true;
}
return false;
}
/**
Workaround for bug in gcc 4.1. @See Item_in_subselect::walk()
*/
bool Item_subselect::walk_body(Item_processor processor, bool walk_subquery,
uchar *argument)
{
if (walk_subquery)
{
for (SELECT_LEX *lex= unit->first_select(); lex; lex= lex->next_select())
{
List_iterator<Item> li(lex->item_list);
Item *item;
ORDER *order;
while ((item=li++))
{
if (item->walk(processor, walk_subquery, argument))
return true;
}
if (lex->join_list != NULL &&
walk_join_condition(lex->join_list, processor, walk_subquery, argument))
return true;
if (lex->where && (lex->where)->walk(processor, walk_subquery, argument))
return true;
for (order= lex->group_list.first ; order; order= order->next)
{
if ((*order->item)->walk(processor, walk_subquery, argument))
return true;
}
if (lex->having && (lex->having)->walk(processor, walk_subquery,
argument))
return true;
for (order= lex->order_list.first ; order; order= order->next)
{
if ((*order->item)->walk(processor, walk_subquery, argument))
return true;
}
}
}
return (this->*processor)(argument);
}
bool Item_subselect::walk(Item_processor processor, bool walk_subquery,
uchar *argument)
{
return walk_body(processor, walk_subquery, argument);
}
/**
Mark a subquery unit with information provided
A subquery may belong to WHERE, HAVING, ORDER BY or GROUP BY item trees.
This "processor" qualifies subqueries by outer clause type.
@note For the WHERE clause of the JOIN query this function also associates
a related table with the unit.
@param arg Explain_subquery_marker structure
@retval false
@note We always return "false" as far as we don't want to dive deeper because
we explain inner subqueries in their joins contexts.
*/
bool Item_subselect::explain_subquery_checker(uchar **arg)
{
Explain_subquery_marker *m=
*reinterpret_cast<Explain_subquery_marker **>(arg);
if (m->type == CTX_WHERE)
{
/*
A subquery in the WHERE clause may be associated with a few JOIN_TABs
simultaneously.
*/
if (unit->explain_marker == CTX_NONE)
unit->explain_marker= CTX_WHERE;
else
DBUG_ASSERT(unit->explain_marker == CTX_WHERE);
m->destination->register_where_subquery(unit);
return false;
}
if (m->type == CTX_HAVING && unit->explain_marker == CTX_WHERE)
{
/*
This subquery was in SELECT list of outer subquery transformed
with IN->EXISTS, so is referenced by WHERE and HAVING;
see Item_in_subselect::single_value_in_to_exists_transformer()
*/
return false;
}
if (unit->explain_marker == CTX_NONE)
goto overwrite;
if (unit->explain_marker == m->type)
return false;
/*
GROUP BY subqueries may be listed in different item trees simultaneously:
1) in GROUP BY items,
2) in ORDER BY items and/or
3) in SELECT list.
If such a subquery in the SELECT list, we mark the subquery as if it
belongs to SELECT list, otherwise we mark it as "GROUP BY" subquery.
ORDER BY subqueries may be listed twice in SELECT list and ORDER BY list.
In this case we mark such a subquery as "SELECT list" subquery.
*/
if (unit->explain_marker == CTX_GROUP_BY_SQ && m->type == CTX_ORDER_BY_SQ)
return false;
if (unit->explain_marker == CTX_ORDER_BY_SQ && m->type == CTX_GROUP_BY_SQ)
goto overwrite;
if (unit->explain_marker == CTX_SELECT_LIST &&
(m->type == CTX_ORDER_BY_SQ || m->type == CTX_GROUP_BY_SQ))
return false;
if ((unit->explain_marker == CTX_ORDER_BY_SQ ||
unit->explain_marker == CTX_GROUP_BY_SQ) && m->type == CTX_SELECT_LIST)
goto overwrite;
DBUG_ASSERT(!"Unexpected combination of item trees!");
return false;
overwrite:
unit->explain_marker= m->type;
return false;
}
bool Item_in_subselect::walk(Item_processor processor, bool walk_subquery,
uchar *argument)
{
if (left_expr->walk(processor, walk_subquery, argument))
return true;
/*
Cannot call "Item_subselect::walk(...)" because with gcc 4.1
Item_in_subselect::walk() was incorrectly called instead.
Using Item_subselect::walk_body() instead is a workaround.
*/
return walk_body(processor, walk_subquery, argument);
}
Item::Type Item_subselect::type() const
{
return SUBSELECT_ITEM;
}
table_map Item_subselect::used_tables() const
{
return 0L;
}
bool Item_subselect::const_item() const
{
if (unit->thd->lex->context_analysis_only)
return false;
/* Not constant until tables are locked. */
if (!unit->thd->lex->is_query_tables_locked())
return false;
return const_item_cache;
}
void Item_subselect::print(String *str, enum_query_type query_type)
{
str->append("(...)");
}
Item_singlerow_subselect::Item_singlerow_subselect(st_select_lex *select_lex)
:Item_subselect(), value(0)
{
DBUG_ENTER("Item_singlerow_subselect::Item_singlerow_subselect");
init(select_lex, new select_singlerow_subselect(this));
maybe_null= 1;
max_columns= UINT_MAX;
DBUG_VOID_RETURN;
}
st_select_lex *
Item_singlerow_subselect::invalidate_and_restore_select_lex()
{
DBUG_ENTER("Item_singlerow_subselect::invalidate_and_restore_select_lex");
st_select_lex *result= unit->first_select();
DBUG_ASSERT(result);
/*
This code restore the parse tree in it's state before the execution of
Item_singlerow_subselect::Item_singlerow_subselect(),
and in particular decouples this object from the SELECT_LEX,
so that the SELECT_LEX can be used with a different flavor
or Item_subselect instead, as part of query rewriting.
*/
unit->item= NULL;
DBUG_RETURN(result);
}
Item_maxmin_subselect::Item_maxmin_subselect(THD *thd_param,
Item_subselect *parent,
st_select_lex *select_lex,
bool max_arg,
bool ignore_nulls)
:Item_singlerow_subselect(), was_values(false)
{
DBUG_ENTER("Item_maxmin_subselect::Item_maxmin_subselect");
max= max_arg;
init(select_lex, new select_max_min_finder_subselect(this, max_arg,
ignore_nulls));
max_columns= 1;
maybe_null= 1;
max_columns= 1;
DBUG_VOID_RETURN;
}
void Item_maxmin_subselect::print(String *str, enum_query_type query_type)
{
str->append(max?"<max>":"<min>", 5);
Item_singlerow_subselect::print(str, query_type);
}
bool Item_singlerow_subselect::null_inside()
{
return 0;
}
double Item_singlerow_subselect::val_real()
{
reset();
return 0;
}
longlong Item_singlerow_subselect::val_int()
{
reset();
return 0;
}
String *Item_singlerow_subselect::val_str(String *str)
{
reset();
return 0;
}
my_decimal *Item_singlerow_subselect::val_decimal(my_decimal *decimal_value)
{
reset();
return 0;
}
bool Item_singlerow_subselect::val_bool()
{
reset();
return 0;
}
Item_exists_subselect::Item_exists_subselect(st_select_lex *select_lex):
Item_subselect(), value(FALSE), exec_method(EXEC_UNSPECIFIED),
sj_convert_priority(0), embedding_join_nest(NULL)
{
DBUG_ENTER("Item_exists_subselect::Item_exists_subselect");
init(select_lex, new select_exists_subselect(this));
max_columns= UINT_MAX;
null_value= FALSE; //can't be NULL
maybe_null= 0; //can't be NULL
DBUG_VOID_RETURN;
}
void Item_exists_subselect::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("exists"));
Item_subselect::print(str, query_type);
}
bool Item_in_subselect::test_limit(st_select_lex_unit *unit_arg)
{
if (unit_arg->fake_select_lex &&
unit_arg->fake_select_lex->test_limit())
return(1);
SELECT_LEX *sl= unit_arg->first_select();
for (; sl; sl= sl->next_select())
{
if (sl->test_limit())
return(1);
}
return(0);
}
Item_in_subselect::Item_in_subselect(Item * left_exp,
st_select_lex *select_lex):
Item_exists_subselect(), left_expr(left_exp), left_expr_cache(NULL),
left_expr_cache_filled(false), need_expr_cache(TRUE),
upper_item(NULL)
{
DBUG_ENTER("Item_in_subselect::Item_in_subselect");
init(select_lex, new select_exists_subselect(this));
max_columns= UINT_MAX;
maybe_null= 1;
reset();
//if test_limit will fail then error will be reported to client
test_limit(select_lex->master_unit());
DBUG_VOID_RETURN;
}
Item_allany_subselect::Item_allany_subselect(Item * left_exp,
chooser_compare_func_creator fc,
st_select_lex *select_lex,
bool all_arg)
:Item_in_subselect(), func_creator(fc), all(all_arg)
{
DBUG_ENTER("Item_allany_subselect::Item_allany_subselect");
left_expr= left_exp;
func= func_creator(all_arg);
init(select_lex, new select_exists_subselect(this));
max_columns= 1;
reset();
//if test_limit will fail then error will be reported to client
test_limit(select_lex->master_unit());
DBUG_VOID_RETURN;
}
void Item_in_subselect::print(String *str, enum_query_type query_type)
{
if (exec_method == EXEC_EXISTS_OR_MAT || exec_method == EXEC_EXISTS)
str->append(STRING_WITH_LEN("<exists>"));
else
{
left_expr->print(str, query_type);
str->append(STRING_WITH_LEN(" in "));
}
Item_subselect::print(str, query_type);
}
void Item_allany_subselect::print(String *str, enum_query_type query_type)
{
if (exec_method == EXEC_EXISTS_OR_MAT || exec_method == EXEC_EXISTS)
str->append(STRING_WITH_LEN("<exists>"));
else
{
left_expr->print(str, query_type);
str->append(' ');
str->append(func->symbol(all));
str->append(all ? " all " : " any ", 5);
}
Item_subselect::print(str, query_type);
}