MySQL 9.0.0
Source Code Documentation
item_subselect.h
Go to the documentation of this file.
1#ifndef ITEM_SUBSELECT_INCLUDED
2#define ITEM_SUBSELECT_INCLUDED
3
4/* Copyright (c) 2002, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27/// Implements classes that represent subqueries and predicates containing
28/// table subqueries.
29
30#include <assert.h>
31#include <sys/types.h>
32
33#include <cstddef>
34#include <memory> // unique_ptr
35#include <vector>
36
37#include "field_types.h" // enum_field_types
38#include "my_alloc.h" // Destroy_only
39
40#include "my_inttypes.h"
41#include "my_table_map.h"
42#include "my_time.h"
44#include "mysql_time.h"
45#include "sql/comp_creator.h"
46#include "sql/enum_query_type.h"
47#include "sql/item.h" // Item_result_field
48#include "sql/iterators/row_iterator.h" // IWYU pragma: keep
49#include "sql/parse_location.h" // POS
51#include "sql/sql_const.h"
53#include "template_utils.h"
54
55class Comp_creator;
56class Field;
59class JOIN;
60class Json_wrapper;
61class PT_subquery;
65class Query_block;
67class String;
68class THD;
70class my_decimal;
72struct AccessPath;
73class Table_ref;
74
75template <class T>
76class List;
77
78/// Base class that is common to all subqueries and subquery predicates
79
82
83 public:
85
86 /**
87 If !=NO_PLAN_IDX: this Item is in the condition attached to the JOIN_TAB
88 having this index in the parent JOIN.
89 */
91
92 // For EXPLAIN.
95
96 // For EXPLAIN. Only valid if engine_type() == HASH_SJ_ENGINE.
97 const TABLE *get_table() const;
98 const Index_lookup &index_lookup() const;
100
101 void create_iterators(THD *thd);
102 virtual AccessPath *root_access_path() const { return nullptr; }
103
105 SCALAR_SUBQUERY, ///< Scalar or row subquery
106 EXISTS_SUBQUERY, ///< [NOT] EXISTS subquery predicate
107 IN_SUBQUERY, ///< [NOT] IN subquery predicate
108 ALL_SUBQUERY, ///< comp-op ALL quantified comparison predicate
109 ANY_SUBQUERY ///< comp-op ANY quantified comparison predicate
110 };
111
112 /// Accumulate used tables
116 }
117
118 virtual Subquery_type subquery_type() const = 0;
119
120 /**
121 @returns whether this subquery is a single column scalar subquery.
122 (Note that scalar and row subqueries are both represented as
123 scalar subqueries, this function can be used to distinguish them)
124 */
125 virtual bool is_single_column_scalar_subquery() const { return false; }
126
127 void cleanup() override;
128 /**
129 Reset state after a single execution of a subquery, useful when a
130 dependent subquery must be evaluated multiple times for varying values
131 of the outer references.
132 This is a lighter cleanup procedure than the one provided by cleanup().
133 */
134 virtual void reset() {
135 m_value_assigned = false;
136 null_value = true;
137 }
138 bool is_value_assigned() const { return m_value_assigned; }
141 enum Type type() const override;
142 bool is_null() override { return update_null_value() || null_value; }
143 bool fix_fields(THD *thd, Item **ref) override;
144 void fix_after_pullout(Query_block *parent_query_block,
145 Query_block *removed_query_block) override;
146 virtual bool exec(THD *thd);
147 table_map used_tables() const override { return m_used_tables_cache; }
148 table_map not_null_tables() const override { return 0; }
149 /// @returns used tables for subquery (excluding any left-hand expression)
151 Item *get_tmp_table_item(THD *thd) override;
152 void update_used_tables() override;
153 void print(const THD *thd, String *str,
154 enum_query_type query_type) const override;
155
158 }
159
160 /*
161 True if this subquery has been already evaluated. Implemented only for
162 single select and union subqueries only.
163 */
164 bool is_evaluated() const;
165 bool is_uncacheable() const;
166
167 /**
168 Used by max/min subquery to initialize value presence registration
169 mechanism. Engine calls this function before re-execution of subquery.
170 */
171 virtual void reset_has_values() {}
172 /**
173 @returns the "place" where this subquery is located in the simply
174 containing query block.
175 */
177
178 bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
179 bool explain_subquery_checker(uchar **arg) override;
180 bool inform_item_in_cond_of_tab(uchar *arg) override;
181 bool clean_up_after_removal(uchar *arg) override;
182
185 pointer_cast<Check_function_as_value_generator_parameters *>(args);
186 func_arg->err_code = func_arg->get_unnamed_function_error_code();
187 return true;
188 }
189
190 /// argument used by walk method collect_scalar_subqueries ("css")
192 ///< accumulated all subq (or aggregates) found
193 std::vector<Item_subselect *> list;
196 bool contains(Query_expression *candidate) {
197 for (auto sq : list) {
198 if (sq->m_query_expr == candidate) return true;
199 }
200 return false;
201 }
202 };
203
204 bool collect_subqueries(uchar *) override;
205 Item *replace_item_field(uchar *arg) override;
206 Item *replace_item_view_ref(uchar *arg) override;
207
208 protected:
210 explicit Item_subselect(const POS &pos) : super(pos) { set_subquery(); }
211 /**
212 Bind this subquery object with the supplied query expression.
213 As part of transformations, this function may re-bind the subquery to
214 a different query expression.
215
216 @param qe supplied query expression
217 */
218 void bind(Query_expression *qe);
219
220 /// The query expression of the subquery
221 Query_expression *m_query_expr{nullptr}; // Actual value set in construction
222
223 /// The query result object associated with the query expression
225
226 /// Only relevant for Item_in_subselect; optimized structure used for
227 /// execution in place of running the entire subquery.
229
230 /// cache of used tables
232 /// cache of used tables from subquery only (not including LHS of IN subquery)
234
235 /// allowed number of columns (1 for scalar subqueries)
236 uint m_max_columns{0}; // Irrelevant value, actually set during construction
237 /// where subquery is placed
239
240 private:
241 bool subq_opt_away_processor(uchar *arg) override;
242
243 /// Accumulate properties from underlying query expression
245 /// Accumulate properties from underlying query block
247 /// Accumulate properties from a selected expression within a query block.
248 void accumulate_expression(Item *item);
249 /// Accumulate properties from a condition or GROUP/ORDER within a query
250 /// block.
251 void accumulate_condition(Item *item);
252
253 /// True if value has been assigned to subquery
254 bool m_value_assigned{false};
255 /**
256 Whether or not execution of this subquery has been traced by
257 optimizer tracing already. If optimizer trace option
258 REPEATED_SUBSELECT is disabled, this is used to disable tracing
259 after the first one.
260 */
261 bool m_traced_before{false};
262};
263
264/// Class that represents scalar subquery and row subquery
265
267 public:
270
271 bool fix_fields(THD *thd, Item **ref) override;
272 void cleanup() override;
273 Subquery_type subquery_type() const override { return SCALAR_SUBQUERY; }
274 bool create_row(const mem_root_deque<Item *> &item_list, Item_cache **row,
275 bool possibly_empty);
276
277 bool is_single_column_scalar_subquery() const override;
278
279 void reset() override;
280 void store(uint i, Item *item);
281 double val_real() override;
282 longlong val_int() override;
283 String *val_str(String *) override;
284 my_decimal *val_decimal(my_decimal *) override;
285 bool val_json(Json_wrapper *result) override;
286 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
287 bool get_time(MYSQL_TIME *ltime) override;
288 bool val_bool() override;
289 enum Item_result result_type() const override;
290 bool resolve_type(THD *) override;
291
292 /*
293 Mark the subquery as having no rows.
294 If there are aggregate functions (in the outer query),
295 we need to generate a NULL row. @c return_zero_rows().
296 */
297 void no_rows_in_result() override;
298
299 uint cols() const override;
300
301 /**
302 @note that this returns the i-th element of the SELECT list.
303 To check for nullability, look at this->is_nullable() and not
304 element_index[i]->is_nullable(), since the selected expressions are
305 always NULL if the subquery is empty.
306 */
307 Item *element_index(uint i) override { return m_row[i]; }
308 Item **addr(uint i) override { return pointer_cast<Item **>(m_row) + i; }
309 bool check_cols(uint c) override;
310 bool null_inside() override;
311 void bring_value() override;
312
313 bool collect_scalar_subqueries(uchar *) override;
314 virtual bool is_maxmin() const { return false; }
315
316 /**
317 Argument for walk method replace_scalar_subquery
318 */
320 ///< subquery to be replaced with ␓field from derived table
322 ///< The derived table of the transform
324 ///< the replacement field
326 ///< The transformed query block.
328 ///< The immediately surrounding query block. This will be the transformed
329 ///< block or a subquery of it
331 ///< True if subquery's selected item contains a COUNT aggregate
332 bool m_add_coalesce{false};
333 ///< Presence of HAVING clause in subquery: Only relevant if
334 ///< \c m_add_coalesce is true
336 ///< Index of field holding value of having clause in derived table's list
337 ///< of fields. Only relevant if \c m_add_coalesce is true
339
341 TABLE *derived, Field *field,
342 Query_block *select, bool add_coalesce,
343 bool add_having_compensation, uint having_idx)
344 : m_target(target),
345 m_derived(derived),
346 m_field(field),
347 m_outer_query_block(select),
348 m_inner_query_block(select),
349 m_add_coalesce(add_coalesce),
350 m_add_having_compensation(add_having_compensation),
351 m_having_idx(having_idx) {}
352 };
353
354 Item *replace_scalar_subquery(uchar *arge) override;
355 /**
356 This method is used to implement a special case of semantic tree
357 rewriting, mandated by a SQL:2003 exception in the specification.
358 The only caller of this method is handle_sql2003_note184_exception(),
359 see the code there for more details.
360 Note that this method breaks the object internal integrity, by
361 removing it's association with the corresponding Query_block,
362 making this object orphan from the parse tree.
363 No other method, beside the destructor, should be called on this
364 object, as it is now invalid.
365 @return the Query_block structure that was given in the constructor.
366 */
368 std::optional<ContainedSubquery> get_contained_subquery(
369 const Query_block *outer_query_block) override;
371
372 private:
373 /// Value cache of a scalar subquery
375 /**
376 Value cache for a row or scalar subquery. In case of a scalar subquery,
377 m_row points directly at m_value. In case of a row subquery, m_row
378 is an array of pointers to individual Item_cache objects.
379 */
380 Item_cache **m_row{nullptr};
381 bool m_no_rows{false}; ///< @c no_rows_in_result
382};
383
384/* used in static ALL/ANY optimization */
386 public:
387 /**
388 Create an Item for use in transformation of quantified comparison
389 predicates that can be evaluated using MAX or MIN aggregation.
390
391 @param parent The quantified comparison predicate that is transformed.
392 @param query_block The query block representing the inner part of the subq.
393 @param max_arg Whether the aggregation is for MAX or MIN.
394 */
396 bool max_arg);
397 void print(const THD *thd, String *str,
398 enum_query_type query_type) const override;
399 void cleanup() override;
400 bool has_values() const { return m_has_values; }
401 void register_value() { m_has_values = true; }
402 void reset_has_values() override { m_has_values = false; }
403 bool is_maxmin() const override { return true; }
404
405 private:
406 bool m_max; ///< True if performing MAX, false if MIN
407 bool m_has_values{false}; ///< Set if we have found at least one row
408};
409
410/// Classes that represent predicates over table subqueries:
411/// [NOT] EXISTS, [NOT] IN, ANY/SOME and ALL
412
413/**
414 Strategy which will be used to handle this subquery: flattening to a
415 semi-join, conversion to a derived table, rewrite of IN to EXISTS...
416 Sometimes the strategy is first only a candidate, then the real decision
417 happens in a second phase. Other times the first decision is final.
418 */
419enum class Subquery_strategy : int {
420 /// Nothing decided yet
422 /// Candidate for rewriting IN(subquery) to EXISTS, or subquery
423 /// materialization
425 /// Candidate for semi-join flattening
427 /// Candidate for rewriting to joined derived table
429 /// Semi-join flattening
430 SEMIJOIN,
431 /// Rewrite to joined derived table
433 /// Evaluate as EXISTS subquery (possibly after rewriting from another type)
435 /// Subquery materialization (HASH_SJ_ENGINE)
437 /// Subquery has been deleted, probably because it was always false
438 DELETED,
439};
440
443
444 public:
445 /**
446 Create an Item that represents an EXISTS subquery predicate, or any
447 quantified comparison predicate that uses the same base class.
448
449 @param query_block First query block of query expression representing
450 the contained subquery.
451 */
452 explicit Item_exists_subselect(Query_block *query_block);
453
455
456 explicit Item_exists_subselect(const POS &pos) : super(pos) {}
457
458 // The left-hand expression of the subquery predicate. Unused with EXISTS
459 Item *left_expr{nullptr};
460
461 /// Priority of this predicate in the convert-to-semi-join-nest process.
463 /// Execution strategy chosen for this Item
465 /// Used by the transformation to derived table
467
468 /**
469 Used by subquery optimizations to keep track about where this subquery
470 predicate is located, and whether it is a candidate for transformation.
471 (Table_ref*) 1 - the predicate is an AND-part of the WHERE
472 join nest pointer - the predicate is an AND-part of ON expression
473 of a join nest
474 NULL - for all other locations. It also means that the
475 predicate is not a candidate for transformation.
476 See also THD::emb_on_expr_nest.
477
478 As for the second case above (the join nest pointer), note that this value
479 may change if scalar subqueries are transformed to derived tables,
480 cf. transform_scalar_subqueries_to_join_with_derived, due to the need to
481 build new join nests. The change is performed in Query_block::nest_derived.
482 */
484
486
487 /*
488 Transform subquery predicate to a form that can be executed, e.g.
489 as an EXISTS subquery, or a MAX or MIN aggregation on the subquery.
490
491 This is a virtual function that has implementations for EXISTS, IN
492 and quantified comparison subquery predicates.
493
494 @param thd Thread handle
495 @param[out] transformed Points to transformed representation of the object
496 if unchanged: No transformation was performed
497
498 @returns false if success, true if error
499 */
500 virtual bool transformer(THD *thd, Item **transformed);
501
502 Subquery_type subquery_type() const override { return EXISTS_SUBQUERY; }
503 bool is_bool_func() const override { return true; }
504 void reset() override {
506 m_value = false;
507 }
508
509 enum Item_result result_type() const override { return INT_RESULT; }
510 /*
511 The item is
512 ([NOT] IN/EXISTS) [ IS [NOT] TRUE|FALSE ]
513 */
515 bool with_is_op() const {
516 switch (value_transform) {
517 case BOOL_IS_TRUE:
518 case BOOL_IS_FALSE:
519 case BOOL_NOT_TRUE:
520 case BOOL_NOT_FALSE:
521 return true;
522 default:
523 return false;
524 }
525 }
526 /// True if the IS TRUE/FALSE wasn't explicit in the query
527 bool implicit_is_op = false;
528 Item *truth_transformer(THD *, enum Bool_test test) override;
529 bool translate(bool &null_v, bool v);
530 void apply_is_true() override {
531 const bool had_is = with_is_op();
533 if (!had_is && value_transform == BOOL_IS_TRUE)
534 implicit_is_op = true; // needn't be written by EXPLAIN
535 }
536 /// True if the Item has decided that it can do antijoin
537 bool can_do_aj = false;
539 bool fix_fields(THD *thd, Item **ref) override;
540 longlong val_int() override;
541 double val_real() override;
542 String *val_str(String *) override;
543 my_decimal *val_decimal(my_decimal *) override;
544 bool val_bool() override;
545 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
546 return get_date_from_int(ltime, fuzzydate);
547 }
548 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
549 bool resolve_type(THD *thd) override;
550 void print(const THD *thd, String *str,
551 enum_query_type query_type) const override;
552
554
555 protected:
556 bool is_semijoin_candidate(THD *thd);
557 bool is_derived_candidate(THD *thd);
558
559 /// value of this item (boolean: exists/not-exists)
560 bool m_value{false};
561
562 /**
563 True if naked IN is allowed to exchange FALSE for UNKNOWN.
564 Because this is about the naked IN, there is no public ignore_unknown(),
565 intentionally, so that callers don't get it wrong.
566 */
567 bool abort_on_null{false};
568};
569
570/**
571 Representation of IN subquery predicates of the form
572 "left_expr IN (SELECT ...)".
573
574 @details
575 This class has:
576 - A "subquery execution engine" (as a subclass of Item_subselect) that allows
577 it to evaluate subqueries. (and this class participates in execution by
578 having m_was_null variable where part of execution result is stored.
579 - Transformation methods (todo: more on this).
580
581 This class is not used directly, it is "wrapped" into Item_in_optimizer
582 which provides some small bits of subquery evaluation.
583*/
584
587
588 public:
590 Item_in_subselect(const POS &pos, Item *left_expr,
591 PT_subquery *pt_subquery_arg);
592
594
595 bool do_itemize(Parse_context *pc, Item **res) override;
596
597 void cleanup() override;
598 Subquery_type subquery_type() const override { return IN_SUBQUERY; }
599
600 void reset() override {
601 m_value = false;
602 null_value = false;
603 m_was_null = false;
604 }
605 bool transformer(THD *thd, Item **transformed) override;
607 Item **transformed);
609 Item **transformed);
610 bool row_value_transformer(THD *thd, Item **transformed);
612 Comp_creator *func);
614 Item_in_optimizer *optimizer);
615 bool subquery_allows_materialization(THD *thd, Query_block *query_block,
616 const Query_block *outer);
617 bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
619 Item *compile(Item_analyzer analyzer, uchar **arg_p,
620 Item_transformer transformer, uchar *arg_t) override;
621
622 bool exec(THD *thd) override;
623 longlong val_int() override;
624 double val_real() override;
625 String *val_str(String *) override;
626 my_decimal *val_decimal(my_decimal *) override;
627 bool val_bool() override;
628 bool test_limit();
629 void print(const THD *thd, String *str,
630 enum_query_type query_type) const override;
631 void fix_after_pullout(Query_block *parent_query_block,
632 Query_block *removed_query_block) override;
633 void update_used_tables() override;
634 bool init_left_expr_cache(THD *thd);
635
636 /**
637 Once the decision to use IN->EXISTS has been taken, performs some last
638 steps of this transformation.
639 */
640 bool finalize_exists_transform(THD *thd, Query_block *select);
641 /**
642 Once the decision to use materialization has been taken, performs some
643 last steps of this transformation.
644 */
646 AccessPath *root_access_path() const override;
647 std::optional<ContainedSubquery> get_contained_subquery(
648 const Query_block *outer_query_block) override;
649
651 return m_in2exists_info != nullptr && m_in2exists_info->added_to_where;
652 }
653
654 /// Is reliable only if IN->EXISTS has been done.
656 return m_in2exists_info->dependent_before;
657 }
658
659 bool *get_cond_guard(int i) const {
660 return m_pushed_cond_guards != nullptr ? m_pushed_cond_guards + i : nullptr;
661 }
662 void set_cond_guard_var(int i, bool v) const {
663 if (m_pushed_cond_guards != nullptr) m_pushed_cond_guards[i] = v;
664 }
665
668 friend class Item_in_optimizer;
671
672 /// Used to trigger on/off conditions that were pushed down to subquery
673 bool *m_pushed_cond_guards{nullptr};
674
675 /// Point on NOT/NOP before ALL/SOME subquery
677
678 protected:
679 /**
680 Cache of the left operand of the subquery predicate. Allocated in the
681 runtime memory root, for each execution, thus need not be freed.
682 */
684
685 /// Whether m_left_expr_cache holds a value
687
688 /// The need for expr cache may be optimized away, @sa init_left_expr_cache.
689 bool need_expr_cache{true};
690
691 private:
692 bool mark_as_outer(Item *left_row, size_t col);
693
694 /**
695 In the case of
696
697 x COMP_OP (SELECT1 UNION SELECT2 ...)
698
699 - the subquery transformation is done on SELECT1; this requires wrapping
700 'x' with more Item layers, and injecting that in a condition in SELECT1.
701
702 - the same transformation is done on SELECT2; but the wrapped 'x' doesn't
703 need to be created again, the one created for SELECT1 could be reused
704
705 - to achieve this, the wrapped 'x' is stored in member
706 'm_injected_left_expr' when it is created for SELECT1, and is later
707 reused for SELECT2.
708
709 This will refer to a cached value which is reevaluated once for each
710 candidate row, cf. setup in #single_value_transformer.
711 */
713
714 bool m_was_null{false};
715
716 /**
717 This bundles several pieces of information useful when doing the
718 IN->EXISTS transform. If this transform has not been done, pointer is
719 NULL.
720 */
722 /**
723 True: if IN->EXISTS has been done and has added a condition to the
724 subquery's WHERE clause.
725 */
727 /**
728 True: if subquery was dependent (correlated) before IN->EXISTS
729 was done.
730 */
732 /**
733 True: if subquery was dependent (correlated) after IN->EXISTS
734 was done.
735 */
737 } * m_in2exists_info{nullptr};
738
740
741 bool val_bool_naked();
742};
743
744/**
745 Class that represents a quantified comparison predicate.
746
747 expression comp-op ANY ( subquery )
748 expression comp-op ALL ( subquery )
749
750 Note that the special cases = ANY (aka IN) and <> ALL (aka NOT IN)
751 are handled by the base class Item_in_subselect.
752*/
754 public:
756 Query_block *select, bool all);
757
758 Subquery_type subquery_type() const override {
760 }
761 bool transformer(THD *thd, Item **transformed) override;
762 void print(const THD *thd, String *str,
763 enum_query_type query_type) const override;
764
767 bool m_all;
768};
769
770/**
771 A subquery execution engine that evaluates the subquery by doing index
772 lookups in a single table's index.
773
774 This engine is used to resolve subqueries in forms
775
776 outer_expr IN (SELECT tbl.key FROM tbl WHERE subq_where)
777
778 or, row-based:
779
780 (oe1, .. oeN) IN (SELECT key_part1, ... key_partK
781 FROM tbl WHERE subqwhere)
782
783 i.e. the subquery is a single table SELECT without GROUP BY, aggregate
784 functions, etc.
785*/
787 protected:
788 Query_result_union *result = nullptr; /* results storage class */
789 /// Table which is read, using one of eq_ref, ref, ref_or_null.
790 TABLE *table{nullptr};
794 Item *m_cond; ///< The WHERE condition of the subquery
795 ulonglong m_hash; ///< Hash value calculated by RefIterator, when needed.
796 /*
797 The "having" clause. This clause (further referred to as "artificial
798 having") was inserted by subquery transformation code. It contains
799 Item(s) that have a side-effect: they record whether the subquery has
800 produced a row with NULL certain components. We need to use it for cases
801 like
802 (oe1, oe2) IN (SELECT t.key, t.no_key FROM t1)
803 where we do index lookup on t.key=oe1 but need also to check if there
804 was a row such that t.no_key IS NULL.
805 */
807
808 Item_in_subselect *item; /* item that uses this engine */
809
810 public:
812
814 const Index_lookup &ref,
815 enum join_type join_type,
817 Item *having)
818 : table(table),
820 ref(ref),
822 m_cond(where),
823 m_having(having),
824 item(subs) {}
826 virtual bool exec(THD *thd);
827 virtual void print(const THD *thd, String *str, enum_query_type query_type);
829 virtual void cleanup() {}
830 virtual void create_iterators(THD *) {}
831};
832
833/*
834 This function is actually defined in sql_parse.cc, but it depends on
835 chooser_compare_func_creator defined in this file.
836 */
839 Query_block *select);
840
841/**
842 Compute an IN predicate via a hash semi-join. The subquery is materialized
843 during the first evaluation of the IN predicate. The IN predicate is executed
844 via the functionality inherited from subselect_indexsubquery_engine.
845*/
846
848 private:
849 /* true if the subquery was materialized into a temp table. */
851 // true if we know for sure that there are zero rows in the table.
852 // Set only after is_materialized is true.
853 bool has_zero_rows = false;
854 /**
855 Existence of inner NULLs in materialized table:
856 By design, other values than IRRELEVANT_OR_FALSE are possible only if the
857 subquery has only one inner expression.
858 */
860 /// none, or they don't matter
862 /// they matter, and we don't know yet if they exists
864 /// they matter, and we know there exists at least one.
865 NEX_TRUE = 2
866 };
871
872 /// Saved result object, must be restored after use
874
875 public:
877 Query_expression *query_expr)
879 in_predicate, nullptr, nullptr),
880 is_materialized(false),
881 m_query_expr(query_expr) {}
882 ~subselect_hash_sj_engine() override;
883
884 bool setup(THD *thd, const mem_root_deque<Item *> &tmp_columns);
885 void cleanup() override;
886 bool exec(THD *thd) override;
887 void print(const THD *thd, String *str, enum_query_type query_type) override;
888 enum_engine_type engine_type() const override { return HASH_SJ_ENGINE; }
889
890 TABLE *get_table() const { return table; }
891 const Index_lookup &index_lookup() const { return ref; }
892 enum join_type get_join_type() const { return type; }
894 void create_iterators(THD *thd) override;
895};
896
897/**
898 Removes every predicate injected by IN->EXISTS.
899
900 This function is different from others:
901 - it wants to remove all traces of IN->EXISTS (for
902 materialization)
903 - remove_subq_pushed_predicates() and remove_additional_cond() want to
904 remove only the conditions of IN->EXISTS which index lookup already
905 satisfies (they are just an optimization).
906
907 If there are no in2exists conditions, it will return the exact same
908 pointer. If it returns a new Item, the old Item is left alone, so it
909 can be reused in other settings.
910
911 @param conds Condition; may be nullptr.
912 @returns new condition
913 */
915
916/// @returns whether the item is a quantified comparison predicate
918
919#endif /* ITEM_SUBSELECT_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Abstract factory interface for creating comparison predicates.
Definition: item_cmpfunc.h:524
Definition: field.h:577
Class that represents a quantified comparison predicate.
Definition: item_subselect.h:753
Item_allany_subselect(Item *left_expr, chooser_compare_func_creator fc, Query_block *select, bool all)
Definition: item_subselect.cc:1451
bool transformer(THD *thd, Item **transformed) override
Definition: item_subselect.cc:2917
bool m_all
Definition: item_subselect.h:767
Subquery_type subquery_type() const override
Definition: item_subselect.h:758
Comp_creator * m_func
Definition: item_subselect.h:766
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_subselect.cc:2927
chooser_compare_func_creator m_func_creator
Definition: item_subselect.h:765
Definition: item.h:6841
Definition: item_subselect.h:441
enum_condition_context outer_condition_context
Used by the transformation to derived table.
Definition: item_subselect.h:466
bool is_semijoin_candidate(THD *thd)
Definition: item_subselect.cc:1507
void apply_is_true() override
Apply the IS TRUE truth property, meaning that an UNKNOWN result and a FALSE result are treated the s...
Definition: item_subselect.h:530
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_subselect.cc:1468
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_subselect.h:545
String * val_str(String *) override
Return the result of EXISTS as a string value.
Definition: item_subselect.cc:1716
bool m_value
value of this item (boolean: exists/not-exists)
Definition: item_subselect.h:560
Item_exists_subselect()
Definition: item_subselect.h:454
double val_real() override
Definition: item_subselect.cc:1702
bool get_time(MYSQL_TIME *ltime) override
Definition: item_subselect.h:548
bool implicit_is_op
True if the IS TRUE/FALSE wasn't explicit in the query.
Definition: item_subselect.h:527
Item * truth_transformer(THD *, enum Bool_test test) override
Informs an item that it is wrapped in a truth test, in case it wants to transforms itself to implemen...
Definition: item_subselect.cc:1379
longlong val_int() override
Definition: item_subselect.cc:1704
Subquery_strategy strategy
Execution strategy chosen for this Item.
Definition: item_subselect.h:464
Item * left_expr
Definition: item_subselect.h:459
bool val_bool() override
Definition: item_subselect.cc:1740
int sj_convert_priority
Priority of this predicate in the convert-to-semi-join-nest process.
Definition: item_subselect.h:462
void reset() override
Reset state after a single execution of a subquery, useful when a dependent subquery must be evaluate...
Definition: item_subselect.h:504
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_subselect.cc:1321
bool abort_on_null
True if naked IN is allowed to exchange FALSE for UNKNOWN.
Definition: item_subselect.h:567
virtual bool transformer(THD *thd, Item **transformed)
Definition: item_subselect.cc:1490
bool is_derived_candidate(THD *thd)
Definition: item_subselect.cc:1598
bool can_do_aj
True if the Item has decided that it can do antijoin.
Definition: item_subselect.h:537
bool with_is_op() const
Definition: item_subselect.h:515
bool fix_fields(THD *thd, Item **ref) override
An object of class Item_in_subselect is always substituted with another object of class Item_in_optim...
Definition: item_subselect.cc:2656
enum Bool_test value_transform
Definition: item_subselect.h:514
void notify_removal() override
Called when an item has been removed, can be used to notify external objects about the removal,...
Definition: item_subselect.h:485
Subquery_type subquery_type() const override
Definition: item_subselect.h:502
Item_subselect super
Definition: item_subselect.h:442
bool translate(bool &null_v, bool v)
Translates the value of the naked EXISTS to a value taking into account the optional NULL and IS [NOT...
Definition: item_subselect.cc:1342
bool is_bool_func() const override
Definition: item_subselect.h:503
Table_ref * embedding_join_nest
Used by subquery optimizations to keep track about where this subquery predicate is located,...
Definition: item_subselect.h:483
bool choose_semijoin_or_antijoin()
Helper for is_semijoin_candidate() and is_derived_candidate().
Definition: item_subselect.cc:1665
Item_exists_subselect(const POS &pos)
Definition: item_subselect.h:456
my_decimal * val_decimal(my_decimal *) override
Return the result of EXISTS as a decimal value.
Definition: item_subselect.cc:1733
enum Item_result result_type() const override
Definition: item_subselect.h:509
Definition: item_cmpfunc.h:924
Definition: item_cmpfunc.h:491
Representation of IN subquery predicates of the form "left_expr IN (SELECT ...)".
Definition: item_subselect.h:585
void fix_after_pullout(Query_block *parent_query_block, Query_block *removed_query_block) override
Definition: item_subselect.cc:2726
my_decimal * val_decimal(my_decimal *) override
Return the result of EXISTS as a decimal value.
Definition: item_subselect.cc:1793
Item * compile(Item_analyzer analyzer, uchar **arg_p, Item_transformer transformer, uchar *arg_t) override
Perform a generic "compilation" of the Item tree, ie transform the Item tree by adding zero or more I...
Definition: item_subselect.cc:769
double val_real() override
Definition: item_subselect.cc:1751
struct Item_in_subselect::In2exists_info nullptr
bool m_left_expr_cache_filled
Whether m_left_expr_cache holds a value.
Definition: item_subselect.h:686
bool single_value_transformer(THD *thd, Comp_creator *func, Item **transformed)
Transform a single-column IN/ALL/ANY subquery predicate.
Definition: item_subselect.cc:1829
void set_cond_guard_var(int i, bool v) const
Definition: item_subselect.h:662
bool row_value_transformer(THD *thd, Item **transformed)
Transform a multi-column IN/ALL/ANY subquery predicate.
Definition: item_subselect.cc:2258
std::optional< ContainedSubquery > get_contained_subquery(const Query_block *outer_query_block) override
If this item represents a IN/ALL/ANY/comparison_operator subquery, return that (along with data on ho...
Definition: item_subselect.cc:2784
Item_func_not_all * m_upper_item
Point on NOT/NOP before ALL/SOME subquery.
Definition: item_subselect.h:676
bool need_expr_cache
The need for expr cache may be optimized away,.
Definition: item_subselect.h:689
bool finalize_materialization_transform(THD *thd, JOIN *join)
Once the decision to use materialization has been taken, performs some last steps of this transformat...
Definition: item_subselect.cc:481
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_subselect.cc:1438
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_subselect.cc:2614
void reset() override
Reset state after a single execution of a subquery, useful when a dependent subquery must be evaluate...
Definition: item_subselect.h:600
List< Cached_item > * m_left_expr_cache
Cache of the left operand of the subquery predicate.
Definition: item_subselect.h:683
bool mark_as_outer(Item *left_row, size_t col)
Decide whether to mark the injected left expression "outer" relative to the subquery.
Definition: item_subselect.cc:433
Item * transform(Item_transformer transformer, uchar *arg) override
Perform a generic transformation of the Item tree, by adding zero or more additional Item objects to ...
Definition: item_subselect.cc:762
bool * m_pushed_cond_guards
Used to trigger on/off conditions that were pushed down to subquery.
Definition: item_subselect.h:673
bool dependent_before_in2exists() const
Is reliable only if IN->EXISTS has been done.
Definition: item_subselect.h:655
AccessPath * root_access_path() const override
Definition: item_subselect.cc:572
PT_subquery * pt_subselect
Definition: item_subselect.h:739
longlong val_int() override
Definition: item_subselect.cc:1758
bool * get_cond_guard(int i) const
Definition: item_subselect.h:659
bool row_value_in_to_exists_transformer(THD *thd, Query_block *select, Item_in_optimizer *optimizer)
Transform a (possibly non-correlated) IN subquery into a correlated EXISTS.
Definition: item_subselect.cc:2323
bool finalize_exists_transform(THD *thd, Query_block *select)
Once the decision to use IN->EXISTS has been taken, performs some last steps of this transformation.
Definition: item_subselect.cc:440
bool val_bool() override
Definition: item_subselect.cc:1772
bool m_was_null
Definition: item_subselect.h:714
bool init_left_expr_cache(THD *thd)
Initialize the cache of the left operand of the IN predicate.
Definition: item_subselect.cc:2751
bool quantified_comp_transformer(THD *thd, Comp_creator *func, Item **transformed)
Perform transformation of quantified comparison predicates (ie.
Definition: item_subselect.cc:2533
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_subselect.cc:540
String * val_str(String *) override
Return the result of EXISTS as a string value.
Definition: item_subselect.cc:1765
void update_used_tables() override
Updates used tables, not null tables information and accumulates properties up the item tree,...
Definition: item_subselect.cc:2733
bool in2exists_added_to_where() const
Definition: item_subselect.h:650
bool walk(Item_processor processor, enum_walk walk, uchar *arg) override
Traverses a tree of Items in prefix and/or postfix order.
Definition: item_subselect.cc:756
Item_exists_subselect super
Definition: item_subselect.h:586
Subquery_type subquery_type() const override
Definition: item_subselect.h:598
bool single_value_in_to_exists_transformer(THD *thd, Query_block *select, Comp_creator *func)
Transform an IN predicate into EXISTS via predicate injection.
Definition: item_subselect.cc:2050
bool test_limit()
Definition: item_subselect.cc:1410
Item_in_subselect()
Definition: item_subselect.h:593
bool val_bool_naked()
Definition: item_subselect.cc:1779
Item_ref * m_injected_left_expr
In the case of.
Definition: item_subselect.h:712
bool exec(THD *thd) override
Definition: item_subselect.cc:785
bool transformer(THD *thd, Item **transformed) override
Definition: item_subselect.cc:2517
Definition: item_cmpfunc.h:2310
Definition: item_subselect.h:385
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_subselect.cc:1109
bool m_has_values
Set if we have found at least one row.
Definition: item_subselect.h:407
bool is_maxmin() const override
Definition: item_subselect.h:403
bool m_max
True if performing MAX, false if MIN.
Definition: item_subselect.h:406
void reset_has_values() override
Used by max/min subquery to initialize value presence registration mechanism.
Definition: item_subselect.h:402
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_subselect.cc:1102
bool has_values() const
Definition: item_subselect.h:400
void register_value()
Definition: item_subselect.h:401
Item_maxmin_subselect(Item_subselect *parent, Query_block *query_block, bool max_arg)
Create an Item for use in transformation of quantified comparison predicates that can be evaluated us...
Definition: item_subselect.cc:1086
Definition: item.h:6321
Definition: item.h:5897
Item with result field.
Definition: item.h:5832
Class that represents scalar subquery and row subquery.
Definition: item_subselect.h:266
Item * replace_scalar_subquery(uchar *arge) override
When walking the item tree seeing an Item_singlerow_subselect matching a target, replace it with a su...
Definition: item_subselect.cc:3016
enum Item_result result_type() const override
Definition: item_subselect.cc:1129
void store(uint i, Item *item)
Definition: item_subselect.cc:1120
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_subselect.cc:399
bool null_inside() override
Definition: item_subselect.cc:1183
Item ** addr(uint i) override
Definition: item_subselect.h:308
double val_real() override
Definition: item_subselect.cc:1197
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_subselect.cc:1134
void no_rows_in_result() override
Definition: item_subselect.cc:1166
bool val_json(Json_wrapper *result) override
Get a JSON value from an Item.
Definition: item_subselect.cc:1242
Item_cache ** m_row
Value cache for a row or scalar subquery.
Definition: item_subselect.h:380
bool m_no_rows
no_rows_in_result
Definition: item_subselect.h:381
void reset() override
Reset state after a single execution of a subquery, useful when a dependent subquery must be evaluate...
Definition: item_subselect.cc:1115
Subquery_type subquery_type() const override
Definition: item_subselect.h:273
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_subselect.cc:1253
std::optional< ContainedSubquery > get_contained_subquery(const Query_block *outer_query_block) override
If this item represents a IN/ALL/ANY/comparison_operator subquery, return that (along with data on ho...
Definition: item_subselect.cc:3074
longlong val_int() override
Definition: item_subselect.cc:1208
uint cols() const override
Definition: item_subselect.cc:3296
virtual bool is_maxmin() const
Definition: item_subselect.h:314
bool is_single_column_scalar_subquery() const override
Definition: item_subselect.cc:1125
bool fix_fields(THD *thd, Item **ref) override
Definition: item_subselect.cc:330
Item * element_index(uint i) override
Definition: item_subselect.h:307
Item_singlerow_subselect()
Definition: item_subselect.h:269
Query_block * invalidate_and_restore_query_block()
This method is used to implement a special case of semantic tree rewriting, mandated by a SQL:2003 ex...
Definition: item_subselect.cc:901
bool collect_scalar_subqueries(uchar *) override
Definition: item_subselect.cc:2941
bool get_time(MYSQL_TIME *ltime) override
Definition: item_subselect.cc:1265
String * val_str(String *) override
Definition: item_subselect.cc:1219
bool val_bool() override
Definition: item_subselect.cc:1276
void bring_value() override
Definition: item_subselect.cc:1190
bool create_row(const mem_root_deque< Item * > &item_list, Item_cache **row, bool possibly_empty)
Makes storage for the output values for a scalar or row subquery and calculates their data and column...
Definition: item_subselect.cc:3195
bool check_cols(uint c) override
Definition: item_subselect.cc:1175
my_decimal * val_decimal(my_decimal *) override
Definition: item_subselect.cc:1230
Item_cache * m_value
Value cache of a scalar subquery.
Definition: item_subselect.h:374
Base class that is common to all subqueries and subquery predicates.
Definition: item_subselect.h:80
subselect_indexsubquery_engine * indexsubquery_engine
Only relevant for Item_in_subselect; optimized structure used for execution in place of running the e...
Definition: item_subselect.h:228
Item * replace_item_view_ref(uchar *arg) override
Transform processor.
Definition: item_subselect.cc:3179
void set_value_assigned()
Definition: item_subselect.h:139
bool clean_up_after_removal(uchar *arg) override
Clean up after removing the subquery from the item tree.
Definition: item_subselect.cc:2877
bool is_evaluated() const
Definition: item_subselect.cc:2923
enum_parsing_context m_parsing_place
where subquery is placed
Definition: item_subselect.h:238
bool collect_subqueries(uchar *) override
Definition: item_subselect.cc:2910
table_map used_tables() const override
Definition: item_subselect.h:147
void accumulate_expression(Item *item)
Accumulate properties from a selected expression within a query block.
Definition: item_subselect.cc:264
void accumulate_used_tables(table_map add_tables)
Accumulate used tables.
Definition: item_subselect.h:113
Query_expression * m_query_expr
The query expression of the subquery.
Definition: item_subselect.h:221
virtual Subquery_type subquery_type() const =0
bool check_function_as_value_generator(uchar *args) override
Check if this item is allowed for a virtual column or inside a default expression.
Definition: item_subselect.h:183
table_map subquery_used_tables() const
Definition: item_subselect.h:150
bool is_uncacheable() const
Definition: item_subselect.cc:3301
void accumulate_properties()
Accumulate properties from underlying query expression.
Definition: item_subselect.cc:200
virtual bool exec(THD *thd)
Definition: item_subselect.cc:663
Query_expression * query_expr() const
Definition: item_subselect.h:84
enum_engine_type
Definition: item_subselect.h:93
@ INDEXSUBQUERY_ENGINE
Definition: item_subselect.h:93
@ HASH_SJ_ENGINE
Definition: item_subselect.h:93
@ OTHER_ENGINE
Definition: item_subselect.h:93
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_subselect.cc:861
Subquery_type
Definition: item_subselect.h:104
@ ALL_SUBQUERY
comp-op ALL quantified comparison predicate
Definition: item_subselect.h:108
@ IN_SUBQUERY
[NOT] IN subquery predicate
Definition: item_subselect.h:107
@ SCALAR_SUBQUERY
Scalar or row subquery.
Definition: item_subselect.h:105
@ ANY_SUBQUERY
comp-op ANY quantified comparison predicate
Definition: item_subselect.h:109
@ EXISTS_SUBQUERY
[NOT] EXISTS subquery predicate
Definition: item_subselect.h:106
bool explain_subquery_checker(uchar **arg) override
Register subquery to the table where it is used within a condition.
Definition: item_subselect.cc:656
bool m_value_assigned
True if value has been assigned to subquery.
Definition: item_subselect.h:254
Item * get_tmp_table_item(THD *thd) override
If an Item is materialized in a temporary table, a different Item may have to be used in the part of ...
Definition: item_subselect.cc:840
enum_parsing_context place()
Definition: item_subselect.h:176
virtual void reset()
Reset state after a single execution of a subquery, useful when a dependent subquery must be evaluate...
Definition: item_subselect.h:134
Item * replace_item_field(uchar *arg) override
Transform processor.
Definition: item_subselect.cc:3169
join_type get_join_type() const
Definition: item_subselect.cc:310
int in_cond_of_tab
If !=NO_PLAN_IDX: this Item is in the condition attached to the JOIN_TAB having this index in the par...
Definition: item_subselect.h:90
void accumulate_condition(Item *item)
Accumulate properties from a condition or GROUP/ORDER within a query block.
Definition: item_subselect.cc:275
virtual AccessPath * root_access_path() const
Definition: item_subselect.h:102
void create_iterators(THD *thd)
Definition: item_subselect.cc:280
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_subselect.cc:315
Query_result_interceptor * m_query_result
The query result object associated with the query expression.
Definition: item_subselect.h:224
table_map m_subquery_used_tables
cache of used tables from subquery only (not including LHS of IN subquery)
Definition: item_subselect.h:233
const TABLE * get_table() const
Definition: item_subselect.cc:300
enum Type type() const override
Definition: item_subselect.cc:838
bool fix_fields(THD *thd, Item **ref) override
Definition: item_subselect.cc:593
void set_indexsubquery_engine(subselect_indexsubquery_engine *eng)
Definition: item_subselect.h:156
void bind(Query_expression *qe)
Bind this subquery object with the supplied query expression.
Definition: item_subselect.cc:144
void reset_value_assigned()
Definition: item_subselect.h:140
table_map not_null_tables() const override
Return table map of tables that can't be NULL tables (tables that are used in a context where if they...
Definition: item_subselect.h:148
void update_used_tables() override
Updates used tables, not null tables information and accumulates properties up the item tree,...
Definition: item_subselect.cc:852
bool walk(Item_processor processor, enum_walk walk, uchar *arg) override
Traverses a tree of Items in prefix and/or postfix order.
Definition: item_subselect.cc:635
Item_subselect()
Definition: item_subselect.h:209
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_subselect.h:142
const Index_lookup & index_lookup() const
Definition: item_subselect.cc:305
enum_engine_type engine_type() const
Definition: item_subselect.cc:286
bool is_value_assigned() const
Definition: item_subselect.h:138
Item_result_field super
Definition: item_subselect.h:81
bool m_traced_before
Whether or not execution of this subquery has been traced by optimizer tracing already.
Definition: item_subselect.h:261
bool inform_item_in_cond_of_tab(uchar *arg) override
Tells an Item that it is in the condition of a JOIN_TAB of a query block.
Definition: item_subselect.cc:2849
virtual void reset_has_values()
Used by max/min subquery to initialize value presence registration mechanism.
Definition: item_subselect.h:171
bool subq_opt_away_processor(uchar *arg) override
Mark the subquery as optimized away, for EXPLAIN.
Definition: item_subselect.cc:2861
uint m_max_columns
allowed number of columns (1 for scalar subqueries)
Definition: item_subselect.h:236
virtual bool is_single_column_scalar_subquery() const
Definition: item_subselect.h:125
void fix_after_pullout(Query_block *parent_query_block, Query_block *removed_query_block) override
Definition: item_subselect.cc:736
Item_subselect(const POS &pos)
Definition: item_subselect.h:210
table_map m_used_tables_cache
cache of used tables
Definition: item_subselect.h:231
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:930
void set_subquery()
Set the "has subquery" property.
Definition: item.h:3424
bool null_value
True if item is null.
Definition: item.h:3695
Type
Definition: item.h:965
Bool_test
< Modifier for result transformation
Definition: item.h:1008
@ BOOL_NOT_FALSE
Definition: item.h:1012
@ BOOL_NOT_TRUE
Definition: item.h:1011
@ BOOL_IS_TRUE
Definition: item.h:1008
@ BOOL_IS_FALSE
Definition: item.h:1009
@ BOOL_IDENTITY
Definition: item.h:1014
bool get_date_from_int(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_int() to date in MYSQL_TIME.
Definition: item.cc:1680
bool update_null_value()
Make sure the null_value member has a correct value.
Definition: item.cc:7634
bool get_time_from_int(MYSQL_TIME *ltime)
Convert val_int() to time in MYSQL_TIME.
Definition: item.cc:1767
Definition: sql_optimizer.h:133
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1153
Definition: sql_list.h:467
Definition: parse_tree_nodes.h:1773
Base class for parse tree nodes (excluding the Parse_tree_root hierarchy)
Definition: parse_tree_node_base.h:231
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1175
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:626
Definition: item_subselect.cc:1287
Definition: query_result.h:181
Query result class for scalar and row subqueries.
Definition: item_subselect.cc:107
Base class for result from a subquery.
Definition: query_result.h:312
Definition: sql_union.h:40
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:2871
Object containing parameters used when creating and using temporary tables.
Definition: temp_table_param.h:97
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:111
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:95
Compute an IN predicate via a hash semi-join.
Definition: item_subselect.h:847
void create_iterators(THD *thd) override
Definition: item_subselect.cc:3539
void print(const THD *thd, String *str, enum_query_type query_type) override
Print the state of this engine into a string for debugging and views.
Definition: item_subselect.cc:3772
nulls_exist
Existence of inner NULLs in materialized table: By design, other values than IRRELEVANT_OR_FALSE are ...
Definition: item_subselect.h:859
@ NEX_TRUE
they matter, and we know there exists at least one.
Definition: item_subselect.h:865
@ NEX_UNKNOWN
they matter, and we don't know yet if they exists
Definition: item_subselect.h:863
@ NEX_IRRELEVANT_OR_FALSE
none, or they don't matter
Definition: item_subselect.h:861
void cleanup() override
Cleanup performed after each execution.
Definition: item_subselect.cc:3617
unique_ptr_destroy_only< RowIterator > m_iterator
Definition: item_subselect.h:869
subselect_hash_sj_engine(Item_in_subselect *in_predicate, Query_expression *query_expr)
Definition: item_subselect.h:876
TABLE * get_table() const
Definition: item_subselect.h:890
~subselect_hash_sj_engine() override
Definition: item_subselect.cc:3606
Query_result_interceptor * saved_result
Saved result object, must be restored after use.
Definition: item_subselect.h:873
enum nulls_exist mat_table_has_nulls
Definition: item_subselect.h:867
bool is_materialized
Definition: item_subselect.h:850
Query_expression *const m_query_expr
Definition: item_subselect.h:868
bool has_zero_rows
Definition: item_subselect.h:853
const Index_lookup & index_lookup() const
Definition: item_subselect.h:891
bool exec(THD *thd) override
Execute a subquery IN predicate via materialization.
Definition: item_subselect.cc:3652
enum_engine_type engine_type() const override
Definition: item_subselect.h:888
AccessPath * root_access_path() const
Definition: item_subselect.h:893
bool setup(THD *thd, const mem_root_deque< Item * > &tmp_columns)
Create all structures needed for subquery execution using hash semijoin.
Definition: item_subselect.cc:3369
enum join_type get_join_type() const
Definition: item_subselect.h:892
AccessPath * m_root_access_path
Definition: item_subselect.h:870
A subquery execution engine that evaluates the subquery by doing index lookups in a single table's in...
Definition: item_subselect.h:786
Item * m_cond
The WHERE condition of the subquery.
Definition: item_subselect.h:794
TABLE * table
Table which is read, using one of eq_ref, ref, ref_or_null.
Definition: item_subselect.h:790
virtual ~subselect_indexsubquery_engine()=default
virtual void cleanup()
Definition: item_subselect.h:829
virtual bool exec(THD *thd)
Definition: item_subselect.cc:3285
virtual void print(const THD *thd, String *str, enum_query_type query_type)
Definition: item_subselect.cc:3305
virtual void create_iterators(THD *)
Definition: item_subselect.h:830
Item * m_having
Definition: item_subselect.h:806
join_type type
Definition: item_subselect.h:793
enum_engine_type
Definition: item_subselect.h:811
@ HASH_SJ_ENGINE
Definition: item_subselect.h:811
@ INDEXSUBQUERY_ENGINE
Definition: item_subselect.h:811
Index_lookup ref
Definition: item_subselect.h:792
subselect_indexsubquery_engine(TABLE *table, Table_ref *table_ref, const Index_lookup &ref, enum join_type join_type, Item_in_subselect *subs, Item *where, Item *having)
Definition: item_subselect.h:813
ulonglong m_hash
Hash value calculated by RefIterator, when needed.
Definition: item_subselect.h:795
virtual enum_engine_type engine_type() const
Definition: item_subselect.h:828
Item_in_subselect * item
Definition: item_subselect.h:808
Table_ref * table_ref
Definition: item_subselect.h:791
Comp_creator *(*)(bool invert) chooser_compare_func_creator
Convenience typedef for a function that returns factories for Item comparators (ie....
Definition: comp_creator.h:39
static int cmp(Bigint *a, Bigint *b)
Definition: dtoa.cc:1059
enum_query_type
Query type constants (usable as bitmap flags).
Definition: enum_query_type.h:31
This file contains the field type.
bool subquery_allows_materialization(THD *thd, Query_block *query_block, const Query_block *outer)
Check if the subquery predicate can be executed via materialization.
Definition: sql_resolver.cc:1046
Item * all_any_subquery_creator(Item *left_expr, chooser_compare_func_creator cmp, bool all, Query_block *select)
Construct ALL/ANY/SOME subquery Item.
Definition: sql_parse.cc:6664
bool(Item::* Item_analyzer)(uchar **argp)
Definition: item.h:712
Item *(Item::* Item_transformer)(uchar *arg)
Type for transformers used by Item::transform and Item::compile.
Definition: item.h:721
Item * remove_in2exists_conds(Item *conds)
Removes every predicate injected by IN->EXISTS.
Definition: item_subselect.cc:465
Subquery_strategy
Classes that represent predicates over table subqueries: [NOT] EXISTS, [NOT] IN, ANY/SOME and ALL.
Definition: item_subselect.h:419
@ CANDIDATE_FOR_SEMIJOIN
Candidate for semi-join flattening.
@ UNSPECIFIED
Nothing decided yet.
@ SUBQ_EXISTS
Evaluate as EXISTS subquery (possibly after rewriting from another type)
@ DERIVED_TABLE
Rewrite to joined derived table.
@ DELETED
Subquery has been deleted, probably because it was always false.
@ CANDIDATE_FOR_IN2EXISTS_OR_MAT
Candidate for rewriting IN(subquery) to EXISTS, or subquery materialization.
@ SEMIJOIN
Semi-join flattening.
@ SUBQ_MATERIALIZATION
Subquery materialization (HASH_SJ_ENGINE)
@ CANDIDATE_FOR_DERIVED_TABLE
Candidate for rewriting to joined derived table.
bool is_quantified_comp_predicate(Item *item)
Definition: item_subselect.cc:2816
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
std::unique_ptr< T, Destroy_only< T > > unique_ptr_destroy_only
std::unique_ptr, but only destroying.
Definition: my_alloc.h:477
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
unsigned char uchar
Definition: my_inttypes.h:52
long long int longlong
Definition: my_inttypes.h:55
uint64_t table_map
Definition: my_table_map.h:30
Interface for low level time utilities.
unsigned int my_time_flags_t
Flags to str_to_datetime and number_to_datetime.
Definition: my_time.h:94
Time declarations shared between the server and client API: you should not add anything to this heade...
static char * where
Definition: mysqldump.cc:152
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1081
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:151
int add_tables(PFS_engine_table_share_proxy **, unsigned int) noexcept
Definition: pfs_plugin_table_v1_all_empty.cc:33
enum_parsing_context
Names for different query parse tree parts.
Definition: parse_tree_node_base.h:61
@ CTX_NONE
Empty value.
Definition: parse_tree_node_base.h:62
File containing constants that can be used throughout the server.
enum_walk
Enumeration for {Item,Query_block[_UNIT],Table_function}walk.
Definition: sql_const.h:288
bool(Item::*)(unsigned char *) Item_processor
Processor type for {Item,Query_block[_UNIT],Table_function}walk.
Definition: sql_const.h:306
enum_condition_context
Enumeration for Query_block::condition_context.
Definition: sql_const.h:312
Common types of the Optimizer, used by optimization and execution.
join_type
Definition: sql_opt_exec_shared.h:186
@ JT_UNKNOWN
Definition: sql_opt_exec_shared.h:188
#define NO_PLAN_IDX
undefined index
Definition: sql_opt_exec_shared.h:55
Access paths are a query planning structure that correspond 1:1 to iterators, in that an access path ...
Definition: access_path.h:213
Struct used to pass around arguments to/from check_function_as_value_generator.
Definition: item.h:488
int err_code
the error code found during check(if any)
Definition: item.h:495
int get_unnamed_function_error_code() const
Return the correct error code, based on whether or not if we are checking for disallowed functions in...
Definition: item.h:507
Structure used for index-based lookups.
Definition: sql_opt_exec_shared.h:67
This bundles several pieces of information useful when doing the IN->EXISTS transform.
Definition: item_subselect.h:721
bool added_to_where
True: if IN->EXISTS has been done and has added a condition to the subquery's WHERE clause.
Definition: item_subselect.h:726
bool dependent_before
True: if subquery was dependent (correlated) before IN->EXISTS was done.
Definition: item_subselect.h:731
bool dependent_after
True: if subquery was dependent (correlated) after IN->EXISTS was done.
Definition: item_subselect.h:736
Argument for walk method replace_scalar_subquery.
Definition: item_subselect.h:319
Query_block * m_outer_query_block
The immediately surrounding query block.
Definition: item_subselect.h:327
Field * m_field
The transformed query block.
Definition: item_subselect.h:325
TABLE * m_derived
the replacement field
Definition: item_subselect.h:323
Scalar_subquery_replacement(Item_singlerow_subselect *target, TABLE *derived, Field *field, Query_block *select, bool add_coalesce, bool add_having_compensation, uint having_idx)
Definition: item_subselect.h:340
Query_block * m_inner_query_block
True if subquery's selected item contains a COUNT aggregate.
Definition: item_subselect.h:330
bool m_add_coalesce
Presence of HAVING clause in subquery: Only relevant if m_add_coalesce is true.
Definition: item_subselect.h:332
Item_singlerow_subselect * m_target
< subquery to be replaced with ␓field from derived table
Definition: item_subselect.h:321
bool m_add_having_compensation
Index of field holding value of having clause in derived table's list of fields.
Definition: item_subselect.h:335
uint m_having_idx
Definition: item_subselect.h:338
argument used by walk method collect_scalar_subqueries ("css")
Definition: item_subselect.h:191
Collect_subq_info(Query_block *owner)
Definition: item_subselect.h:195
Query_block * m_query_block
Definition: item_subselect.h:194
std::vector< Item_subselect * > list
< accumulated all subq (or aggregates) found
Definition: item_subselect.h:193
bool contains(Query_expression *candidate)
Definition: item_subselect.h:196
Definition: mysql_time.h:82
Bison "location" class.
Definition: parse_location.h:43
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:420
Definition: table.h:1407
Definition: result.h:30
Item_result
Type of the user defined function return slot and arguments.
Definition: udf_registration_types.h:39
@ INT_RESULT
double
Definition: udf_registration_types.h:43
static int all(site_def const *s, node_no node)
Definition: xcom_transport.cc:884