Repository navigation
Expand file tree
/
Copy pathitem_subselect.h
More file actions
1470 lines (1308 loc) · 49.3 KB
/
Copy pathitem_subselect.h
File metadata and controls
1470 lines (1308 loc) · 49.3 KB
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
#ifndef ITEM_SUBSELECT_INCLUDED
#define ITEM_SUBSELECT_INCLUDED
/* Copyright (c) 2002, 2011, Oracle and/or its affiliates.
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, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* subselect Item */
#ifdef USE_PRAGMA_INTERFACE
#pragma interface /* gcc class implementation */
#endif
#include <queues.h>
class st_select_lex;
class st_select_lex_unit;
class JOIN;
class select_result_interceptor;
class subselect_engine;
class subselect_hash_sj_engine;
class Item_bool_func2;
class Comp_creator;
typedef class st_select_lex SELECT_LEX;
/**
Convenience typedef used in this file, and further used by any files
including this file.
*/
typedef Comp_creator* (*chooser_compare_func_creator)(bool invert);
class Cached_item;
/* base class for subselects */
class Item_subselect :public Item_result_field,
protected Used_tables_and_const_cache
{
bool value_assigned; /* value already assigned to subselect */
bool own_engine; /* the engine was not taken from other Item_subselect */
protected:
/* thread handler, will be assigned in fix_fields only */
THD *thd;
/* old engine if engine was changed */
subselect_engine *old_engine;
/* allowed number of columns (1 for single value subqueries) */
uint max_columns;
/* where subquery is placed */
enum_parsing_place parsing_place;
/* work with 'substitution' */
bool have_to_be_excluded;
bool inside_first_fix_fields;
bool done_first_fix_fields;
Item *expr_cache;
/*
Set to TRUE if at optimization or execution time we determine that this
item's value is a constant. We need this member because it is not possible
to substitute 'this' with a constant item.
*/
bool forced_const;
#ifndef DBUG_OFF
/* Count the number of times this subquery predicate has been executed. */
uint exec_counter;
#endif
public:
/*
Used inside Item_subselect::fix_fields() according to this scenario:
> Item_subselect::fix_fields
> engine->prepare
> child_join->prepare
(Here we realize we need to do the rewrite and set
substitution= some new Item, eg. Item_in_optimizer )
< child_join->prepare
< engine->prepare
*ref= substitution;
substitution= NULL;
< Item_subselect::fix_fields
*/
/* TODO make this protected member again. */
Item *substitution;
/* engine that perform execution of subselect (single select or union) */
/* TODO make this protected member again. */
subselect_engine *engine;
/* unit of subquery */
st_select_lex_unit *unit;
/* A reference from inside subquery predicate to somewhere outside of it */
class Ref_to_outside : public Sql_alloc
{
public:
st_select_lex *select; /* Select where the reference is pointing to */
/*
What is being referred. This may be NULL when we're referring to an
aggregate function.
*/
Item *item;
};
/*
References from within this subquery to somewhere outside of it (i.e. to
parent select, grandparent select, etc)
*/
List<Ref_to_outside> upper_refs;
st_select_lex *parent_select;
/*
TRUE<=>Table Elimination has made it redundant to evaluate this select
(and so it is not part of QEP, etc)
*/
bool eliminated;
/* subquery is transformed */
bool changed;
/* TRUE <=> The underlying SELECT is correlated w.r.t some ancestor select */
bool is_correlated;
enum subs_type {UNKNOWN_SUBS, SINGLEROW_SUBS,
EXISTS_SUBS, IN_SUBS, ALL_SUBS, ANY_SUBS};
Item_subselect(THD *thd);
virtual subs_type substype() { return UNKNOWN_SUBS; }
bool is_in_predicate()
{
return (substype() == Item_subselect::IN_SUBS ||
substype() == Item_subselect::ALL_SUBS ||
substype() == Item_subselect::ANY_SUBS);
}
/*
We need this method, because some compilers do not allow 'this'
pointer in constructor initialization list, but we need to pass a pointer
to subselect Item class to select_result_interceptor's constructor.
*/
virtual void init (st_select_lex *select_lex,
select_result_interceptor *result);
~Item_subselect();
void cleanup();
virtual void reset()
{
eliminated= FALSE;
null_value= 1;
}
/**
Set the subquery result to a default value consistent with the semantics of
the result row produced for queries with implicit grouping.
*/
void no_rows_in_result()= 0;
virtual bool select_transformer(JOIN *join);
bool assigned() { return value_assigned; }
void assigned(bool a) { value_assigned= a; }
enum Type type() const;
bool is_null()
{
update_null_value();
return null_value;
}
bool fix_fields(THD *thd, Item **ref);
bool mark_as_dependent(THD *thd, st_select_lex *select, Item *item);
void fix_after_pullout(st_select_lex *new_parent, Item **ref);
void recalc_used_tables(st_select_lex *new_parent, bool after_pullout);
virtual bool exec();
/*
If subquery optimization or execution determines that the subquery has
an empty result, mark the subquery predicate as a constant value.
*/
void make_const()
{
used_tables_cache= 0;
const_item_cache= 0;
forced_const= TRUE;
}
virtual void fix_length_and_dec();
table_map used_tables() const;
table_map not_null_tables() const { return 0; }
bool const_item() const;
inline table_map get_used_tables_cache() { return used_tables_cache; }
Item *get_tmp_table_item(THD *thd);
void update_used_tables();
virtual void print(String *str, enum_query_type query_type);
virtual bool have_guarded_conds() { return FALSE; }
bool change_engine(subselect_engine *eng)
{
old_engine= engine;
engine= eng;
return eng == 0;
}
bool engine_changed(subselect_engine *eng) { return engine != eng; }
/*
True if this subquery has been already evaluated. Implemented only for
single select and union subqueries only.
*/
bool is_evaluated() const;
bool is_uncacheable() const;
bool is_expensive();
/*
Used by max/min subquery to initialize value presence registration
mechanism. Engine call this method before rexecution query.
*/
virtual void reset_value_registration() {}
enum_parsing_place place() { return parsing_place; }
bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
bool mark_as_eliminated_processor(uchar *arg);
bool eliminate_subselect_processor(uchar *arg);
bool set_fake_select_as_master_processor(uchar *arg);
bool enumerate_field_refs_processor(uchar *arg);
bool check_vcol_func_processor(uchar *int_arg)
{
return trace_unsupported_by_check_vcol_func_processor("subselect");
}
/**
Callback to test if an IN predicate is expensive.
@notes
The return value affects the behavior of make_cond_for_table().
@retval TRUE if the predicate is expensive
@retval FALSE otherwise
*/
bool is_expensive_processor(uchar *arg) { return is_expensive(); }
/**
Get the SELECT_LEX structure associated with this Item.
@return the SELECT_LEX structure associated with this Item
*/
st_select_lex* get_select_lex();
virtual bool expr_cache_is_needed(THD *);
virtual void get_cache_parameters(List<Item> ¶meters);
virtual bool is_subquery_processor (uchar *opt_arg) { return 1; }
bool exists2in_processor(uchar *opt_arg) { return 0; }
bool limit_index_condition_pushdown_processor(uchar *opt_arg)
{
return TRUE;
}
void init_expr_cache_tracker(THD *thd);
friend class select_result_interceptor;
friend class Item_in_optimizer;
friend bool Item_field::fix_fields(THD *, Item **);
friend int Item_field::fix_outer_field(THD *, Field **, Item **);
friend bool Item_ref::fix_fields(THD *, Item **);
friend void mark_select_range_as_dependent(THD*,
st_select_lex*, st_select_lex*,
Field*, Item*, Item_ident*);
friend bool convert_join_subqueries_to_semijoins(JOIN *join);
};
/* single value subselect */
class Item_cache;
class Item_singlerow_subselect :public Item_subselect
{
protected:
Item_cache *value, **row;
public:
Item_singlerow_subselect(THD *thd_arg, st_select_lex *select_lex);
Item_singlerow_subselect(THD *thd_arg): Item_subselect(thd_arg), value(0), row (0)
{}
void cleanup();
subs_type substype() { return SINGLEROW_SUBS; }
void reset();
void no_rows_in_result();
bool select_transformer(JOIN *join);
void store(uint i, Item* item);
double val_real();
longlong val_int ();
String *val_str (String *);
my_decimal *val_decimal(my_decimal *);
bool val_bool();
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
enum Item_result result_type() const;
enum Item_result cmp_type() const;
enum_field_types field_type() const;
void fix_length_and_dec();
uint cols();
Item* element_index(uint i) { return reinterpret_cast<Item*>(row[i]); }
Item** addr(uint i) { return (Item**)row + i; }
bool check_cols(uint c);
bool null_inside();
void bring_value();
/**
This method is used to implement a special case of semantic tree
rewriting, mandated by a SQL:2003 exception in the specification.
The only caller of this method is handle_sql2003_note184_exception(),
see the code there for more details.
Note that this method breaks the object internal integrity, by
removing it's association with the corresponding SELECT_LEX,
making this object orphan from the parse tree.
No other method, beside the destructor, should be called on this
object, as it is now invalid.
@return the SELECT_LEX structure that was given in the constructor.
*/
st_select_lex* invalidate_and_restore_select_lex();
Item* expr_cache_insert_transformer(THD *thd, uchar *unused);
friend class select_singlerow_subselect;
};
/* used in static ALL/ANY optimization */
class select_max_min_finder_subselect;
class Item_maxmin_subselect :public Item_singlerow_subselect
{
protected:
bool max;
bool was_values; // Set if we have found at least one row
public:
Item_maxmin_subselect(THD *thd, Item_subselect *parent,
st_select_lex *select_lex, bool max);
virtual void print(String *str, enum_query_type query_type);
void cleanup();
bool any_value() { return was_values; }
void register_value() { was_values= TRUE; }
void reset_value_registration() { was_values= FALSE; }
void no_rows_in_result();
};
/* exists subselect */
class Item_exists_subselect :public Item_subselect
{
protected:
Item_func_not *upper_not;
bool value; /* value of this item (boolean: exists/not-exists) */
bool abort_on_null;
void init_length_and_dec();
bool select_prepare_to_be_in();
public:
/*
Used by subquery optimizations to keep track about in which clause this
subquery predicate is located:
NO_JOIN_NEST - the predicate is an AND-part of the WHERE
join nest pointer - the predicate is an AND-part of ON expression
of a join nest
NULL - for all other locations
*/
TABLE_LIST *emb_on_expr_nest;
/**
Reference on the Item_in_optimizer wrapper of this subquery
*/
Item_in_optimizer *optimizer;
/* true if we got this from EXISTS or to IN */
bool exists_transformed;
Item_exists_subselect(THD *thd_arg, st_select_lex *select_lex);
Item_exists_subselect(THD *thd_arg):
Item_subselect(thd_arg), upper_not(NULL), abort_on_null(0),
emb_on_expr_nest(NULL), optimizer(0), exists_transformed(0)
{}
subs_type substype() { return EXISTS_SUBS; }
void reset()
{
eliminated= FALSE;
value= 0;
}
void no_rows_in_result();
enum Item_result result_type() const { return INT_RESULT;}
longlong val_int();
double val_real();
String *val_str(String*);
my_decimal *val_decimal(my_decimal *);
bool val_bool();
bool fix_fields(THD *thd, Item **ref);
void fix_length_and_dec();
virtual void print(String *str, enum_query_type query_type);
bool select_transformer(JOIN *join);
void top_level_item() { abort_on_null=1; }
inline bool is_top_level_item() { return abort_on_null; }
bool exists2in_processor(uchar *opt_arg);
Item* expr_cache_insert_transformer(THD *thd, uchar *unused);
void mark_as_condition_AND_part(TABLE_LIST *embedding)
{
emb_on_expr_nest= embedding;
}
virtual void under_not(Item_func_not *upper) { upper_not= upper; };
void set_exists_transformed() { exists_transformed= TRUE; }
friend class select_exists_subselect;
friend class subselect_uniquesubquery_engine;
friend class subselect_indexsubquery_engine;
};
TABLE_LIST * const NO_JOIN_NEST=(TABLE_LIST*)0x1;
/*
Possible methods to execute an IN predicate. These are set by the optimizer
based on user-set optimizer switches, semantic analysis and cost comparison.
*/
#define SUBS_NOT_TRANSFORMED 0 /* No execution method was chosen for this IN. */
/* The Final decision about the strategy is made. */
#define SUBS_STRATEGY_CHOSEN 1
#define SUBS_SEMI_JOIN 2 /* IN was converted to semi-join. */
#define SUBS_IN_TO_EXISTS 4 /* IN was converted to correlated EXISTS. */
#define SUBS_MATERIALIZATION 8 /* Execute IN via subquery materialization. */
/* Partial matching substrategies of MATERIALIZATION. */
#define SUBS_PARTIAL_MATCH_ROWID_MERGE 16
#define SUBS_PARTIAL_MATCH_TABLE_SCAN 32
/* ALL/ANY will be transformed with max/min optimization */
/* The subquery has not aggregates, transform it into a MAX/MIN query. */
#define SUBS_MAXMIN_INJECTED 64
/* The subquery has aggregates, use a special max/min subselect engine. */
#define SUBS_MAXMIN_ENGINE 128
/**
Representation of IN subquery predicates of the form
"left_expr IN (SELECT ...)".
@details
This class has:
- A "subquery execution engine" (as a subclass of Item_subselect) that allows
it to evaluate subqueries. (and this class participates in execution by
having was_null variable where part of execution result is stored.
- Transformation methods (todo: more on this).
This class is not used directly, it is "wrapped" into Item_in_optimizer
which provides some small bits of subquery evaluation.
*/
class Item_in_subselect :public Item_exists_subselect
{
protected:
/*
Cache of the left operand of the subquery predicate. Allocated in the
runtime memory root, for each execution, thus need not be freed.
*/
List<Cached_item> *left_expr_cache;
bool first_execution;
/*
expr & optimizer used in subselect rewriting to store Item for
all JOIN in UNION
*/
Item *expr;
bool was_null;
/* A bitmap of possible execution strategies for an IN predicate. */
uchar in_strategy;
protected:
/* Used to trigger on/off conditions that were pushed down to subselect */
bool *pushed_cond_guards;
Comp_creator *func;
protected:
bool init_cond_guards();
bool select_in_like_transformer(JOIN *join);
bool single_value_transformer(JOIN *join);
bool row_value_transformer(JOIN * join);
bool fix_having(Item *having, st_select_lex *select_lex);
bool create_single_in_to_exists_cond(JOIN * join,
Item **where_item,
Item **having_item);
bool create_row_in_to_exists_cond(JOIN * join,
Item **where_item,
Item **having_item);
public:
Item *left_expr;
/*
Important for PS/SP: left_expr_orig is the item that left_expr originally
pointed at. That item is allocated on the statement arena, while
left_expr could later be changed to something on the execution arena.
*/
Item *left_expr_orig;
/* Priority of this predicate in the convert-to-semi-join-nest process. */
int sj_convert_priority;
/*
Types of left_expr and subquery's select list allow to perform subquery
materialization. Currently, we set this to FALSE when it as well could
be TRUE. This is to be properly addressed with fix for BUG#36752.
*/
bool types_allow_materialization;
/*
Same as above, but they also allow to scan the materialized table.
*/
bool sjm_scan_allowed;
/*
JoinTaB Materialization (JTBM) members
*/
/*
TRUE <=> This subselect has been converted into non-mergeable semi-join
table.
*/
bool is_jtbm_merged;
/* (Applicable if is_jtbm_merged==TRUE) Time required to run the materialized join */
double jtbm_read_time;
/* (Applicable if is_jtbm_merged==TRUE) Number of output rows in materialized join */
double jtbm_record_count;
/*
(Applicable if is_jtbm_merged==TRUE) TRUE <=> The materialized subselect is
a degenerate subselect which produces 0 or 1 rows, which we know at
optimization phase.
Examples:
1. subquery has "Impossible WHERE":
SELECT * FROM ot WHERE ot.column IN (SELECT it.col FROM it WHERE 2 > 3)
2. Subquery produces one row which opt_sum.cc is able to get with one lookup:
SELECT * FROM ot WHERE ot.column IN (SELECT MAX(it.key_col) FROM it)
*/
bool is_jtbm_const_tab;
/*
(Applicable if is_jtbm_const_tab==TRUE) Whether the subquery has produced
the row (or not)
*/
bool jtbm_const_row_found;
/*
TRUE<=>this is a flattenable semi-join, false overwise.
*/
bool is_flattenable_semijoin;
/*
TRUE<=>registered in the list of semijoins in outer select
*/
bool is_registered_semijoin;
/*
Used to determine how this subselect item is represented in the item tree,
in case there is a need to locate it there and replace with something else.
Two options are possible:
1. This item is there 'as-is'.
1. This item is wrapped within Item_in_optimizer.
*/
Item *original_item()
{
return (is_flattenable_semijoin && !exists_transformed ?
(Item*)this :
(Item*)optimizer);
}
bool *get_cond_guard(int i)
{
return pushed_cond_guards ? pushed_cond_guards + i : NULL;
}
void set_cond_guard_var(int i, bool v)
{
if ( pushed_cond_guards)
pushed_cond_guards[i]= v;
}
bool have_guarded_conds() { return MY_TEST(pushed_cond_guards); }
Item_func_not_all *upper_item; // point on NOT/NOP before ALL/SOME subquery
Item_in_subselect(THD *thd_arg, Item * left_expr, st_select_lex *select_lex);
Item_in_subselect(THD *thd_arg):
Item_exists_subselect(thd_arg), left_expr_cache(0), first_execution(TRUE),
in_strategy(SUBS_NOT_TRANSFORMED),
pushed_cond_guards(NULL), func(NULL), is_jtbm_merged(FALSE),
is_jtbm_const_tab(FALSE), upper_item(0) {}
void cleanup();
subs_type substype() { return IN_SUBS; }
void reset()
{
eliminated= FALSE;
value= 0;
null_value= 0;
was_null= 0;
}
bool select_transformer(JOIN *join);
bool create_in_to_exists_cond(JOIN *join_arg);
bool inject_in_to_exists_cond(JOIN *join_arg);
virtual bool exec();
longlong val_int();
double val_real();
String *val_str(String*);
my_decimal *val_decimal(my_decimal *);
void update_null_value () { (void) val_bool(); }
bool val_bool();
bool test_limit(st_select_lex_unit *unit);
virtual void print(String *str, enum_query_type query_type);
bool fix_fields(THD *thd, Item **ref);
void fix_length_and_dec();
void fix_after_pullout(st_select_lex *new_parent, Item **ref);
bool const_item() const
{
return Item_subselect::const_item() && left_expr->const_item();
}
void update_used_tables();
bool setup_mat_engine();
bool init_left_expr_cache();
/* Inform 'this' that it was computed, and contains a valid result. */
void set_first_execution() { if (first_execution) first_execution= FALSE; }
bool expr_cache_is_needed(THD *thd);
inline bool left_expr_has_null();
int optimize(double *out_rows, double *cost);
/*
Return the identifier that we could use to identify the subquery for the
user.
*/
int get_identifier();
bool test_strategy(uchar strategy)
{ return MY_TEST(in_strategy & strategy); }
/**
Test that the IN strategy was chosen for execution. This is so
when the CHOSEN flag is ON, and there is no other strategy.
*/
bool test_set_strategy(uchar strategy)
{
DBUG_ASSERT(strategy == SUBS_SEMI_JOIN ||
strategy == SUBS_IN_TO_EXISTS ||
strategy == SUBS_MATERIALIZATION ||
strategy == SUBS_PARTIAL_MATCH_ROWID_MERGE ||
strategy == SUBS_PARTIAL_MATCH_TABLE_SCAN ||
strategy == SUBS_MAXMIN_INJECTED ||
strategy == SUBS_MAXMIN_ENGINE);
return ((in_strategy & SUBS_STRATEGY_CHOSEN) &&
(in_strategy & ~SUBS_STRATEGY_CHOSEN) == strategy);
}
bool is_set_strategy()
{ return MY_TEST(in_strategy & SUBS_STRATEGY_CHOSEN); }
bool has_strategy()
{ return in_strategy != SUBS_NOT_TRANSFORMED; }
void add_strategy (uchar strategy)
{
DBUG_ENTER("Item_in_subselect::add_strategy");
DBUG_PRINT("enter", ("current: %u add: %u",
(uint) in_strategy, (uint) strategy));
DBUG_ASSERT(strategy != SUBS_NOT_TRANSFORMED);
DBUG_ASSERT(!(strategy & SUBS_STRATEGY_CHOSEN));
/*
TODO: PS re-execution breaks this condition, because
check_and_do_in_subquery_rewrites() is called for each reexecution
and re-adds the same strategies.
DBUG_ASSERT(!(in_strategy & SUBS_STRATEGY_CHOSEN));
*/
in_strategy|= strategy;
DBUG_VOID_RETURN;
}
void reset_strategy(uchar strategy)
{
DBUG_ENTER("Item_in_subselect::reset_strategy");
DBUG_PRINT("enter", ("current: %u new: %u",
(uint) in_strategy, (uint) strategy));
DBUG_ASSERT(strategy != SUBS_NOT_TRANSFORMED);
in_strategy= strategy;
DBUG_VOID_RETURN;
}
void set_strategy(uchar strategy)
{
DBUG_ENTER("Item_in_subselect::set_strategy");
DBUG_PRINT("enter", ("current: %u set: %u",
(uint) in_strategy,
(uint) (SUBS_STRATEGY_CHOSEN | strategy)));
/* Check that only one strategy is set for execution. */
DBUG_ASSERT(strategy == SUBS_SEMI_JOIN ||
strategy == SUBS_IN_TO_EXISTS ||
strategy == SUBS_MATERIALIZATION ||
strategy == SUBS_PARTIAL_MATCH_ROWID_MERGE ||
strategy == SUBS_PARTIAL_MATCH_TABLE_SCAN ||
strategy == SUBS_MAXMIN_INJECTED ||
strategy == SUBS_MAXMIN_ENGINE);
in_strategy= (SUBS_STRATEGY_CHOSEN | strategy);
DBUG_VOID_RETURN;
}
bool exists2in_processor(uchar *opt_arg __attribute__((unused)))
{
return 0;
};
friend class Item_ref_null_helper;
friend class Item_is_not_null_test;
friend class Item_in_optimizer;
friend class subselect_indexsubquery_engine;
friend class subselect_hash_sj_engine;
friend class subselect_partial_match_engine;
friend class Item_exists_subselect;
};
/* ALL/ANY/SOME subselect */
class Item_allany_subselect :public Item_in_subselect
{
public:
chooser_compare_func_creator func_creator;
bool all;
Item_allany_subselect(THD *thd_arg, Item * left_expr,
chooser_compare_func_creator fc,
st_select_lex *select_lex, bool all);
void cleanup();
// only ALL subquery has upper not
subs_type substype() { return all?ALL_SUBS:ANY_SUBS; }
bool select_transformer(JOIN *join);
void create_comp_func(bool invert) { func= func_creator(invert); }
virtual void print(String *str, enum_query_type query_type);
bool is_maxmin_applicable(JOIN *join);
bool transform_into_max_min(JOIN *join);
void no_rows_in_result();
};
class subselect_engine: public Sql_alloc
{
protected:
select_result_interceptor *result; /* results storage class */
THD *thd; /* pointer to current THD */
Item_subselect *item; /* item, that use this engine */
enum Item_result res_type; /* type of results */
enum Item_result cmp_type; /* how to compare the results */
enum_field_types res_field_type; /* column type of the results */
bool maybe_null; /* may be null (first item in select) */
public:
enum enum_engine_type {ABSTRACT_ENGINE, SINGLE_SELECT_ENGINE,
UNION_ENGINE, UNIQUESUBQUERY_ENGINE,
INDEXSUBQUERY_ENGINE, HASH_SJ_ENGINE,
ROWID_MERGE_ENGINE, TABLE_SCAN_ENGINE};
subselect_engine(Item_subselect *si,
select_result_interceptor *res):
thd(NULL)
{
result= res;
item= si;
cmp_type= res_type= STRING_RESULT;
res_field_type= MYSQL_TYPE_VAR_STRING;
maybe_null= 0;
}
virtual ~subselect_engine() {}; // to satisfy compiler
virtual void cleanup()= 0;
/*
Also sets "thd" for subselect_engine::result.
Should be called before prepare().
*/
void set_thd(THD *thd_arg);
THD * get_thd() { return thd ? thd : current_thd; }
virtual int prepare(THD *)= 0;
virtual void fix_length_and_dec(Item_cache** row)= 0;
/*
Execute the engine
SYNOPSIS
exec()
DESCRIPTION
Execute the engine. The result of execution is subquery value that is
either captured by previously set up select_result-based 'sink' or
stored somewhere by the exec() method itself.
A required side effect: If at least one pushed-down predicate is
disabled, subselect_engine->no_rows() must return correct result after
the exec() call.
RETURN
0 - OK
1 - Either an execution error, or the engine was "changed", and the
caller should call exec() again for the new engine.
*/
virtual int exec()= 0;
virtual uint cols()= 0; /* return number of columns in select */
virtual uint8 uncacheable()= 0; /* query is uncacheable */
enum Item_result type() { return res_type; }
enum Item_result cmptype() { return cmp_type; }
enum_field_types field_type() { return res_field_type; }
virtual void exclude()= 0;
virtual bool may_be_null() { return maybe_null; };
virtual table_map upper_select_const_tables()= 0;
static table_map calc_const_tables(TABLE_LIST *);
static table_map calc_const_tables(List<TABLE_LIST> &list);
virtual void print(String *str, enum_query_type query_type)= 0;
virtual bool change_result(Item_subselect *si,
select_result_interceptor *result,
bool temp= FALSE)= 0;
virtual bool no_tables()= 0;
virtual bool is_executed() const { return FALSE; }
/* Check if subquery produced any rows during last query execution */
virtual bool no_rows() = 0;
virtual enum_engine_type engine_type() { return ABSTRACT_ENGINE; }
virtual int get_identifier() { DBUG_ASSERT(0); return 0; }
protected:
void set_row(List<Item> &item_list, Item_cache **row);
};
class subselect_single_select_engine: public subselect_engine
{
bool prepared; /* simple subselect is prepared */
bool executed; /* simple subselect is executed */
st_select_lex *select_lex; /* corresponding select_lex */
JOIN * join; /* corresponding JOIN structure */
public:
subselect_single_select_engine(st_select_lex *select,
select_result_interceptor *result,
Item_subselect *item);
void cleanup();
int prepare(THD *thd);
void fix_length_and_dec(Item_cache** row);
int exec();
uint cols();
uint8 uncacheable();
void exclude();
table_map upper_select_const_tables();
virtual void print (String *str, enum_query_type query_type);
bool change_result(Item_subselect *si,
select_result_interceptor *result,
bool temp);
bool no_tables();
bool may_be_null();
bool is_executed() const { return executed; }
bool no_rows();
virtual enum_engine_type engine_type() { return SINGLE_SELECT_ENGINE; }
int get_identifier();
friend class subselect_hash_sj_engine;
friend class Item_in_subselect;
friend bool setup_jtbm_semi_joins(JOIN *join, List<TABLE_LIST> *join_list,
Item **join_where);
};
class subselect_union_engine: public subselect_engine
{
st_select_lex_unit *unit; /* corresponding unit structure */
public:
subselect_union_engine(st_select_lex_unit *u,
select_result_interceptor *result,
Item_subselect *item);
void cleanup();
int prepare(THD *);
void fix_length_and_dec(Item_cache** row);
int exec();
uint cols();
uint8 uncacheable();
void exclude();
table_map upper_select_const_tables();
virtual void print (String *str, enum_query_type query_type);
bool change_result(Item_subselect *si,
select_result_interceptor *result,
bool temp= FALSE);
bool no_tables();
bool is_executed() const;
bool no_rows();
virtual enum_engine_type engine_type() { return UNION_ENGINE; }
};
struct st_join_table;
/*
A subquery execution engine that evaluates the subquery by doing one index
lookup in a unique index.
This engine is used to resolve subqueries in forms
outer_expr IN (SELECT tbl.unique_key FROM tbl WHERE subq_where)
or, tuple-based:
(oe1, .. oeN) IN (SELECT uniq_key_part1, ... uniq_key_partK
FROM tbl WHERE subqwhere)
i.e. the subquery is a single table SELECT without GROUP BY, aggregate
functions, etc.
*/
class subselect_uniquesubquery_engine: public subselect_engine
{
protected:
st_join_table *tab;
Item *cond; /* The WHERE condition of subselect */
/*
TRUE<=> last execution produced empty set. Valid only when left
expression is NULL.
*/
bool empty_result_set;
public:
// constructor can assign THD because it will be called after JOIN::prepare
subselect_uniquesubquery_engine(THD *thd_arg, st_join_table *tab_arg,
Item_subselect *subs, Item *where)
:subselect_engine(subs, 0), tab(tab_arg), cond(where)
{}
~subselect_uniquesubquery_engine();
void cleanup();
int prepare(THD *);
void fix_length_and_dec(Item_cache** row);
int exec();
uint cols() { return 1; }
uint8 uncacheable() { return UNCACHEABLE_DEPENDENT_INJECTED; }
void exclude();
table_map upper_select_const_tables() { return 0; }
virtual void print (String *str, enum_query_type query_type);
bool change_result(Item_subselect *si,
select_result_interceptor *result,
bool temp= FALSE);
bool no_tables();
int index_lookup(); /* TIMOUR: this method needs refactoring. */
int scan_table();
bool copy_ref_key(bool skip_constants);
bool no_rows() { return empty_result_set; }
virtual enum_engine_type engine_type() { return UNIQUESUBQUERY_ENGINE; }
};
class subselect_indexsubquery_engine: public subselect_uniquesubquery_engine
{
/* FALSE for 'ref', TRUE for 'ref-or-null'. */
bool check_null;
/*
The "having" clause. This clause (further reffered to as "artificial
having") was inserted by subquery transformation code. It contains
Item(s) that have a side-effect: they record whether the subquery has
produced a row with NULL certain components. We need to use it for cases
like
(oe1, oe2) IN (SELECT t.key, t.no_key FROM t1)
where we do index lookup on t.key=oe1 but need also to check if there
was a row such that t.no_key IS NULL.
NOTE: This is currently here and not in the uniquesubquery_engine. Ideally
it should have been in uniquesubquery_engine in order to allow execution of
subqueries like
(oe1, oe2) IN (SELECT primary_key, non_key_maybe_null_field FROM tbl)
We could use uniquesubquery_engine for the first component and let
Item_is_not_null_test( non_key_maybe_null_field) to handle the second.
However, subqueries like the above are currently not handled by index
lookup-based subquery engines, the engine applicability check misses
them: it doesn't switch the engine for case of artificial having and
[eq_]ref access (only for artifical having + ref_or_null or no having).
The above example subquery is handled as a full-blown SELECT with eq_ref
access to one table.
Due to this limitation, the "artificial having" currently needs to be
checked by only in indexsubquery_engine.
*/
Item *having;
public:
// constructor can assign THD because it will be called after JOIN::prepare
subselect_indexsubquery_engine(THD *thd_arg, st_join_table *tab_arg,
Item_subselect *subs, Item *where,
Item *having_arg, bool chk_null)
:subselect_uniquesubquery_engine(thd_arg, tab_arg, subs, where),
check_null(chk_null),
having(having_arg)
{}
int exec();
virtual void print (String *str, enum_query_type query_type);
virtual enum_engine_type engine_type() { return INDEXSUBQUERY_ENGINE; }
};
/*
This function is actually defined in sql_parse.cc, but it depends on
chooser_compare_func_creator defined in this file.
*/
Item * all_any_subquery_creator(THD *thd, Item *left_expr,
chooser_compare_func_creator cmp,
bool all,
SELECT_LEX *select_lex);