forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_subselect.cc
3859 lines (3329 loc) · 114 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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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_select.h"
#include "opt_trace.h"
#include "sql_parse.h" // check_stack_overrun
#include "sql_derived.h" // mysql_derived_create, ...
#include "debug_sync.h"
#include "sql_test.h"
#include "sql_join_buffer.h" // JOIN_CACHE
#include "sql_optimizer.h" // JOIN
#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), engine(NULL), old_engine(NULL),
used_tables_cache(0), have_to_be_excluded(0), const_item_cache(1),
engine_changed(false), changed(false)
{
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)
{
/*
Item can be changed in JOIN::prepare while engine in JOIN::optimize
=> we do not copy old_engine here
*/
engine= unit->item->engine;
parsing_place= unit->item->parsing_place;
unit->item->engine= 0;
unit->item= this;
engine->change_result(this, result);
}
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);
if (unit->is_union())
engine= new subselect_union_engine(unit, result, this);
else
engine= new subselect_single_select_engine(select_lex, result, this);
}
{
SELECT_LEX *upper= unit->outer_select();
if (upper->parsing_place == IN_HAVING)
upper->subquery_in_having= 1;
}
DBUG_VOID_RETURN;
}
void Item_subselect::cleanup()
{
DBUG_ENTER("Item_subselect::cleanup");
Item_result_field::cleanup();
if (old_engine)
{
if (engine)
{
engine->cleanup();
delete engine;
}
engine= old_engine;
old_engine= 0;
}
if (engine)
engine->cleanup();
reset();
value_assigned= 0;
traced_before= false;
in_cond_of_tab= INT_MIN;
DBUG_VOID_RETURN;
}
void Item_singlerow_subselect::cleanup()
{
DBUG_ENTER("Item_singlerow_subselect::cleanup");
value= 0; row= 0;
Item_subselect::cleanup();
DBUG_VOID_RETURN;
}
bool Item_in_subselect::finalize_exists_transform(SELECT_LEX *select_lex)
{
DBUG_ASSERT(exec_method == EXEC_EXISTS_OR_MAT ||
exec_method == EXEC_EXISTS);
/*
Change
SELECT expr1, expr2
to
SELECT 1,1
because EXISTS does not care about the selected expressions, only about
the existence of rows.
If UNION, we have to modify the SELECT list of each SELECT in the
UNION, fortunately this function is indeed called for each SELECT_LEX.
If this is a prepared statement, we must allow the next execution to use
materialization. So, we should back up the original SELECT list. If this
is a UNION, this means backing up the N original SELECT lists. To
avoid this constraint, we change the SELECT list only if this is not a
prepared statement.
*/
if (unit->thd->stmt_arena->is_conventional()) // not prepared stmt
{
uint cnt= select_lex->item_list.elements;
select_lex->item_list.empty();
for(; cnt > 0; cnt--)
select_lex->item_list.push_back(new Item_int(NAME_STRING("Not_used"),
(longlong) 1,
MY_INT64_NUM_DECIMAL_DIGITS));
Opt_trace_context * const trace= &unit->thd->opt_trace;
OPT_TRACE_TRANSFORM(trace, oto0, oto1,
select_lex->select_number,
"IN (SELECT)", "EXISTS (CORRELATED SELECT)");
oto1.add("put_1_in_SELECT_list", true);
}
/*
Note that if the subquery is "SELECT1 UNION SELECT2" then this is not
working optimally (Bug#14215895).
*/
unit->global_parameters->select_limit= new Item_int((int32) 1);
unit->set_limit(unit->global_parameters);
select_lex->join->allow_outer_refs= true; // for JOIN::set_prefix_tables()
exec_method= EXEC_EXISTS;
return false;
}
/*
Removes every predicate injected by IN->EXISTS.
This function is different from others:
- it wants to remove all traces of IN->EXISTS (for
materialization)
- remove_subq_pushed_predicates() and remove_additional_cond() want to
remove only the conditions of IN->EXISTS which index lookup already
satisfies (they are just an optimization).
Code reading suggests that remove_additional_cond() is equivalent to
"if in_subs->left_expr->cols()==1 then remove_in2exists_conds(where)"
but that would still not fix Bug#13915291 of remove_additional_cond().
@param conds condition
@returns new condition
*/
Item *Item_in_subselect::remove_in2exists_conds(Item* conds)
{
if (conds->created_by_in2exists())
return NULL;
if (conds->type() != Item::COND_ITEM)
return conds;
Item_cond *cnd= static_cast<Item_cond *>(conds);
/*
If IN->EXISTS has added something to 'conds', cnd must be AND list and we
must inspect each member.
*/
if (cnd->functype() != Item_func::COND_AND_FUNC)
return conds;
List_iterator<Item> li(*(cnd->argument_list()));
Item *item;
while ((item= li++))
{
// remove() does not invalidate iterator.
if (item->created_by_in2exists())
li.remove();
}
switch (cnd->argument_list()->elements)
{
case 0:
return NULL;
case 1: // AND(x) is the same as x, return x
return cnd->argument_list()->head();
default: // otherwise return AND
return conds;
}
}
bool Item_in_subselect::finalize_materialization_transform(JOIN *join)
{
DBUG_ASSERT(exec_method == EXEC_EXISTS_OR_MAT);
DBUG_ASSERT(engine->engine_type() == subselect_engine::SINGLE_SELECT_ENGINE);
THD * const thd= unit->thd;
subselect_single_select_engine *old_engine_derived=
static_cast<subselect_single_select_engine*>(engine);
DBUG_ASSERT(join == old_engine_derived->join);
// No UNION in materialized subquery so this holds:
DBUG_ASSERT(join->select_lex == unit->first_select());
DBUG_ASSERT(join->unit == unit);
DBUG_ASSERT(unit->global_parameters->select_limit == NULL);
exec_method= EXEC_MATERIALIZATION;
/*
We need to undo several changes which IN->EXISTS had done. But we first
back them up, so that the next execution of the statement is allowed to
choose IN->EXISTS.
*/
/*
Undo conditions injected by IN->EXISTS.
Condition guards, which those conditions maybe used, are not needed
anymore.
Subquery becomes 'not dependent' again, as before IN->EXISTS.
*/
if (join->conds)
join->conds= remove_in2exists_conds(join->conds);
if (join->having)
join->having= remove_in2exists_conds(join->having);
DBUG_ASSERT(!originally_dependent());
join->select_lex->uncacheable&= ~UNCACHEABLE_DEPENDENT;
/*
IN->EXISTS uses master_unit(); however, as we cannot have a UNION here,
'unit' must be correct too.
*/
DBUG_ASSERT(unit == join->select_lex->master_unit());
unit->uncacheable&= ~UNCACHEABLE_DEPENDENT;
OPT_TRACE_TRANSFORM(&thd->opt_trace, oto0, oto1,
old_engine_derived->join->select_lex->select_number,
"IN (SELECT)", "materialization");
oto1.add("chosen", true);
subselect_hash_sj_engine * const new_engine=
new subselect_hash_sj_engine(thd, this, old_engine_derived);
if (!new_engine)
return true;
if (new_engine->setup(unit->get_unit_column_types()))
{
/*
For some reason we cannot use materialization for this IN predicate.
Delete all materialization-related objects, and return error.
*/
delete new_engine;
return true;
}
if (change_engine(new_engine))
return true;
join->allow_outer_refs= false; // for JOIN::set_prefix_tables()
return false;
}
void Item_in_subselect::cleanup()
{
DBUG_ENTER("Item_in_subselect::cleanup");
if (left_expr_cache)
{
left_expr_cache->delete_elements();
delete left_expr_cache;
left_expr_cache= NULL;
}
left_expr_cache_filled= false;
need_expr_cache= TRUE;
switch(exec_method)
{
case EXEC_MATERIALIZATION:
unit->first_select()->uncacheable|= UNCACHEABLE_DEPENDENT;
unit->uncacheable|= UNCACHEABLE_DEPENDENT;
// fall through
case EXEC_EXISTS:
/*
Back to EXISTS_OR_MAT, so that next execution of this statement can
choose between the two.
*/
unit->global_parameters->select_limit= NULL;
exec_method= EXEC_EXISTS_OR_MAT;
break;
default:
break;
}
Item_subselect::cleanup();
DBUG_VOID_RETURN;
}
Item_subselect::~Item_subselect()
{
delete engine;
}
Item_subselect::trans_res
Item_subselect::select_transformer(JOIN *join)
{
DBUG_ENTER("Item_subselect::select_transformer");
DBUG_RETURN(RES_OK);
}
bool Item_subselect::fix_fields(THD *thd, Item **ref)
{
char const *save_where= thd->where;
uint8 uncacheable;
bool res;
DBUG_ASSERT(fixed == 0);
/*
Pointers to THD must match. unit::thd may vary over the lifetime of the
item (for example triggers, and thus their Item-s, are in a cache shared
by all connections), but reinit_stmt_before_use() keeps it up-to-date,
which we check here. subselect_union_engine functions also do sanity
checks.
*/
DBUG_ASSERT(thd == unit->thd);
#ifndef DBUG_OFF
// Engine accesses THD via its 'item' pointer, check it:
DBUG_ASSERT(engine->get_item() == this);
#endif
engine->set_thd_for_result();
if (check_stack_overrun(thd, STACK_MIN_SIZE, (uchar*)&res))
return TRUE;
if (!(res= engine->prepare()))
{
// all transformation is done (used by prepared statements)
changed= 1;
/*
Substitute the current item with an Item_in_optimizer that was
created by Item_in_subselect::select_in_like_transformer and
call fix_fields for the substituted item which in turn calls
engine->prepare for the subquery predicate.
*/
if (substitution)
{
int ret= 0;
// did we changed top item of WHERE condition
if (unit->outer_select()->where == (*ref))
unit->outer_select()->where= substitution; // correct WHERE for PS
else if (unit->outer_select()->having == (*ref))
unit->outer_select()->having= substitution; // correct HAVING for PS
(*ref)= substitution;
substitution->item_name= item_name;
if (have_to_be_excluded)
engine->exclude();
substitution= 0;
thd->where= "checking transformed subquery";
if (!(*ref)->fixed)
ret= (*ref)->fix_fields(thd, ref);
thd->where= save_where;
return ret;
}
// Is it one field subselect?
if (engine->cols() > max_columns)
{
my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
return TRUE;
}
fix_length_and_dec();
}
else
goto err;
if ((uncacheable= engine->uncacheable()))
{
const_item_cache= 0;
if (uncacheable & UNCACHEABLE_RAND)
used_tables_cache|= RAND_TABLE_BIT;
}
fixed= 1;
err:
thd->where= save_where;
return res;
}
/**
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_subselect::exec()
{
DBUG_ENTER("Item_subselect::exec");
/*
Do not execute subselect in case of a fatal error
or if the query has been killed.
*/
THD * const thd= unit->thd;
if (thd->is_error() || thd->killed)
DBUG_RETURN(true);
DBUG_ASSERT(!thd->lex->context_analysis_only);
/*
Simulate a failure in sub-query execution. Used to test e.g.
out of memory or query being killed conditions.
*/
DBUG_EXECUTE_IF("subselect_exec_fail", DBUG_RETURN(true););
/*
Disable tracing of subquery execution if
1) this is not the first time the subselect is executed, and
2) REPEATED_SUBSELECT is disabled
*/
#ifdef OPTIMIZER_TRACE
Opt_trace_context * const trace= &thd->opt_trace;
const bool disable_trace=
traced_before &&
!trace->feature_enabled(Opt_trace_context::REPEATED_SUBSELECT);
Opt_trace_disable_I_S disable_trace_wrapper(trace, disable_trace);
traced_before= true;
Opt_trace_object trace_wrapper(trace);
Opt_trace_object trace_exec(trace, "subselect_execution");
trace_exec.add_select_number(unit->first_select()->select_number);
Opt_trace_array trace_steps(trace, "steps");
#endif
bool res= engine->exec();
if (engine_changed)
{
engine_changed= 0;
res= exec();
DBUG_RETURN(res);
}
DBUG_RETURN(res);
}
/**
Fix used tables information for a subquery after query transformations.
Common actions for all predicates involving subqueries.
Most actions here involve re-resolving information for conditions
and items belonging to the subquery.
Notice that the usage information from underlying expressions is not
propagated to the subquery predicate, as it belongs to inner layers
of the query operator structure.
However, when underlying expressions contain outer references into
a select_lex on this level, the relevant information must be updated
when these expressions are resolved.
*/
void Item_subselect::fix_after_pullout(st_select_lex *parent_select,
st_select_lex *removed_select)
{
/* Clear usage information for this subquery predicate object */
used_tables_cache= 0;
/*
Go through all query specification objects of the subquery and re-resolve
all relevant expressions belonging to them.
*/
for (SELECT_LEX *sel= unit->first_select(); sel; sel= sel->next_select())
{
if (sel->where)
sel->where->fix_after_pullout(parent_select, removed_select);
if (sel->having)
sel->having->fix_after_pullout(parent_select, removed_select);
List_iterator<Item> li(sel->item_list);
Item *item;
while ((item=li++))
item->fix_after_pullout(parent_select, removed_select);
/*
No need to call fix_after_pullout() for outer-join conditions, as these
cannot have outer references.
*/
/* Re-resolve ORDER BY and GROUP BY fields */
for (ORDER *order= (ORDER*) sel->order_list.first;
order;
order= order->next)
(*order->item)->fix_after_pullout(parent_select, removed_select);
for (ORDER *group= (ORDER*) sel->group_list.first;
group;
group= group->next)
(*group->item)->fix_after_pullout(parent_select, removed_select);
}
}
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);
}
/*
Compute the IN predicate if the left operand's cache changed.
*/
bool Item_in_subselect::exec()
{
DBUG_ENTER("Item_in_subselect::exec");
DBUG_ASSERT(exec_method != EXEC_MATERIALIZATION ||
(exec_method == EXEC_MATERIALIZATION &&
engine->engine_type() == subselect_engine::HASH_SJ_ENGINE));
/*
Initialize the cache of the left predicate operand. This has to be done as
late as now, because Cached_item directly contains a resolved field (not
an item, and in some cases (when temp tables are created), these fields
end up pointing to the wrong field. One solution is to change Cached_item
to not resolve its field upon creation, but to resolve it dynamically
from a given Item_ref object.
Do not init the cache if a previous execution decided that it is not needed.
TODO: the cache should be applied conditionally based on:
- rules - e.g. only if the left operand is known to be ordered, and/or
- on a cost-based basis, that takes into account the cost of a cache
lookup, the cache hit rate, and the savings per cache hit.
*/
if (need_expr_cache && !left_expr_cache &&
exec_method == EXEC_MATERIALIZATION &&
init_left_expr_cache())
DBUG_RETURN(TRUE);
if (left_expr_cache != NULL)
{
const int result= test_if_item_cache_changed(*left_expr_cache);
if (left_expr_cache_filled && // cache was previously filled
result < 0) // new value is identical to previous cached value
{
/*
We needn't do a full execution, can just reuse "value", "was_null",
"null_value" of the previous execution.
*/
DBUG_RETURN(false);
}
left_expr_cache_filled= true;
}
null_value= was_null= false;
const bool retval= Item_subselect::exec();
DBUG_RETURN(retval);
}
Item::Type Item_subselect::type() const
{
return SUBSELECT_ITEM;
}
void Item_subselect::fix_length_and_dec()
{
engine->fix_length_and_dec(0);
}
table_map Item_subselect::used_tables() const
{
return (table_map) (engine->uncacheable() ? used_tables_cache : 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;
}
Item *Item_subselect::get_tmp_table_item(THD *thd_arg)
{
if (!with_sum_func && !const_item())
return new Item_field(result_field);
return copy_or_same(thd_arg);
}
void Item_subselect::update_used_tables()
{
if (!engine->uncacheable())
{
// did all used tables become static?
if (!(used_tables_cache & ~engine->upper_select_const_tables()))
const_item_cache= 1;
}
}
void Item_subselect::print(String *str, enum_query_type query_type)
{
if (engine)
{
str->append('(');
engine->print(str, query_type);
str->append(')');
}
else
str->append("(...)");
}
Item_singlerow_subselect::Item_singlerow_subselect(st_select_lex *select_lex)
:Item_subselect(), value(0), no_rows(false)
{
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;
/*
Following information was collected during performing fix_fields()
of Items belonged to subquery, which will be not repeated
*/
used_tables_cache= parent->get_used_tables_cache();
const_item_cache= parent->get_const_item_cache();
DBUG_VOID_RETURN;
}
void Item_maxmin_subselect::cleanup()
{
DBUG_ENTER("Item_maxmin_subselect::cleanup");
Item_singlerow_subselect::cleanup();
was_values= false;
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);
}
void Item_singlerow_subselect::reset()
{
null_value= TRUE;
if (value)
value->null_value= TRUE;
}
/**
@todo
- We cant change name of Item_field or Item_ref, because it will
prevent it's correct resolving, but we should save name of
removed item => we do not make optimization if top item of
list is field or reference.
- switch off this optimization for prepare statement,
because we do not rollback this changes.
Make rollback for it, or special name resolving mode in 5.0.
*/
Item_subselect::trans_res
Item_singlerow_subselect::select_transformer(JOIN *join)
{
DBUG_ENTER("Item_singlerow_subselect::select_transformer");
if (changed)
DBUG_RETURN(RES_OK);
SELECT_LEX *select_lex= join->select_lex;
THD * const thd= unit->thd;
Query_arena *arena= thd->stmt_arena;
if (!select_lex->master_unit()->is_union() &&
!select_lex->table_list.elements &&
select_lex->item_list.elements == 1 &&
!select_lex->item_list.head()->with_sum_func &&
/*
We cant change name of Item_field or Item_ref, because it will
prevent it's correct resolving, but we should save name of
removed item => we do not make optimization if top item of
list is field or reference.
TODO: solve above problem
*/
!(select_lex->item_list.head()->type() == FIELD_ITEM ||
select_lex->item_list.head()->type() == REF_ITEM) &&
!join->conds && !join->having &&
/*
switch off this optimization for prepare statement,
because we do not rollback this changes
TODO: make rollback for it, or special name resolving mode in 5.0.
*/
!arena->is_stmt_prepare_or_first_sp_execute()
)
{
have_to_be_excluded= 1;
if (thd->lex->describe)
{
char warn_buff[MYSQL_ERRMSG_SIZE];
sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number);
push_warning(thd, Sql_condition::WARN_LEVEL_NOTE,
ER_SELECT_REDUCED, warn_buff);
}
substitution= select_lex->item_list.head();
/*
as far as we moved content to upper level, field which depend of
'upper' select is not really dependent => we remove this dependence
*/
substitution->walk(&Item::remove_dependence_processor, 0,
(uchar *) select_lex->outer_select());
DBUG_RETURN(RES_REDUCE);
}
DBUG_RETURN(RES_OK);
}
void Item_singlerow_subselect::store(uint i, Item *item)
{
row[i]->store(item);
row[i]->cache_value();
}
enum Item_result Item_singlerow_subselect::result_type() const
{
return engine->type();
}
/*
Don't rely on the result type to calculate field type.
Ask the engine instead.
*/
enum_field_types Item_singlerow_subselect::field_type() const
{
return engine->field_type();
}
void Item_singlerow_subselect::fix_length_and_dec()
{
if ((max_columns= engine->cols()) == 1)
{
engine->fix_length_and_dec(row= &value);