MySQL 8.4.0
Source Code Documentation
query_term.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23#ifndef QUERY_NODE_INCLUDED
24#define QUERY_NODE_INCLUDED
25
26#include "sql/join_optimizer/materialize_path_parameters.h" // MaterializePathParameters
27#include "sql/sql_list.h"
28#include "sql/sql_union.h"
29#include "sql/table.h"
30
31class Query_block;
32
33/**
34 This class hierarchy is used to represent SQL structures between <query
35 expression> and <query specification>. The class Query_expression
36 represents <query expression> and the class Query_block represents <query
37 specification>, <table value constructor> and <explicit table>,
38 cf. definitions in sql_lex.h. Originally, MySQL supported only one set
39 operation, UNION. This was implicitly represented by having one
40 Query_expression object own several Query_block objects via next pointers.
41 This made things simple, but limited us to left-deep nesting of unions, and
42 also from representing INTERSECTION and EXCEPT, as well as nesting several
43 layers of <query primary>s containing <query expression body>s, e.g.
44
45 (((SELECT a,b FROM t) ORDER BY a LIMIT 5) ORDER BY -b LIMIT 3) ORDER BY a;
46
47 could not be supported. With the present class hierarchy we enable the full
48 set of set operations and arbitrary nesting, as allowed by the SQL grammar,
49 viz:
50 \verbatim
51 <query expression> ::=
52 [ <with clause> ] <query expression body>
53 [ <order by clause> ] [ <limit/offset> ]
54
55 <query expression body> ::=
56 <query term>
57 | <query expression body> UNION [ ALL | DISTINCT ]
58 [ <corresponding spec> ] <query term>
59 | <query expression body> EXCEPT [ ALL | DISTINCT ]
60 [ <corresponding spec> ] <query term>
61
62 <query term> ::=
63 <query primary>
64 | <query expression body> INTERSECT [ ALL | DISTINCT ]
65 [ <corresponding spec> ] <query primary>
66
67 <query primary> ::=
68 <simple table>
69 | <left paren> <query expression body>
70 [ <order by clause> ] [ <limit/offset (*)> ]
71 <right paren>
72
73 <simple table> ::=
74 <query specification>
75 | <table value constructor>
76 | <explicit table>
77
78 (*) MySQL syntax and semantics. The standard uses /<result offset clause/> and
79 <fetch first clause>.
80
81 \endverbatim
82 Note that INTERSECT binds tighter than UNION and EXCEPT.
83 Now, let's turn to how these structures are represented in MySQL.
84 The node types are enumerated by Query_term_type. The nodes themselves
85 by the class hierarchy rooted in Query_term (abstract).
86*/
87
88/// Node type of the query term tree nodes
90 /// Represents Query specification, table value constructor and
91 /// explicit table
93 /// Represents a query primary with parentesized query expression body with
94 /// order by clause and/or limit/offset clause. If none of order by
95 /// or limit is present, we collapse this level of parentheses.
97 /// Represents the three set operations. Nodes are N-ary, i.e. a node can hold
98 /// two or more operands.
103
104/// Query term iterator template argument type: how to visit nodes in tree
106/// Query term iterator template argument type: whether to visit leaf nodes
108
109/**
110 Query term tree structure. There are five node types, cf. Query_term_type.
111 Leaf nodes are Query_block objects. We have three kinds of n-ary set operation
112 nodes corresponding to INTERSECT, UNION and EXCEPT. Finally, we have a "unary"
113 node which essentially adds a ORDER BY/LIMIT over another node.
114
115 Query blocks serve a dual purpose: they represent the query specification and
116 table constructors of the query. As such they are the leaf nodes of the query
117 tree. But they also serve as a way to realize ORDER BY and LIMIT for non-leaf
118 nodes, accessed via the function Query_term::query_block(). Therefore, every
119 non-leaf node in the tree has a companion Query_block to hold ORDER BY and
120 LIMIT information. For the leaf nodes, which are themselves query blocks, the
121 query_block() function just returns a pointer to self, i.e. the leaf nodes
122 handle ORDER BY and LIMIT themselves.
123
124 \verbatim
125 Example: ((SELECT * FROM t1 UNION SELECT * FROM t2 UNION ALL SELECT * FROM t3
126 ORDER BY a LIMIT 5) INTERSECT
127 (((SELECT * FROM t3 ORDER BY a LIMIT 4) ) EXCEPT SELECT * FROM t4)
128 ORDER BY a LIMIT 4) ORDER BY -a LIMIT 3;
129
130 ->
131 m_query_term +------------------+ slave(s)
132 +--------------|-Query_expression |------------------+
133 | +------------------+ |
134 V post_ |
135 +-------------------+processing_ +----------------------+ |
136 | Query_term_unary |block() |Query_block | |
137 | |----------->|order by -(`a) limit 3| |
138 +-------------------+ +----------------------+ |
139 |m_children |
140 | +-----------------------+ +----------------------+ |
141 | |Query_term_intersect | |Query_block | |
142 +>|last distinct index: 1 |-->|order by `a` limit 4 | |
143 +-----------------------+ +----------------------+ |
144 |m_children |
145 | +-----------------------+ +----------------------+ |
146 | |Query_term_union | |Query_block | |
147 +->|last distinct index: 1 |-->|order by `a` limit 5 | |
148 | +-----------------------+ +----------------------+ |
149 | |m_children |
150 | | +------------+ SELECT * FROM t1 /
151 | +-->|Query_block | <---------------------------------+
152 | | +------------+ ----------------------------------+ next
153 | | \
154 | | +------------+ SELECT * FROM t2 /
155 | +-->|Query_block | <---------------------------------+
156 | | +------------+ ----------------------------------+ next
157 | | \
158 | | +------------+ SELECT * FROM t3 /
159 | +-->|Query_block | <---------------------------------+
160 | +------------+ ----------------------------------+ next
161 | \
162 | +-----------------------+ +------------+ |
163 | |Query_term_except |->|Query_block | |
164 +->|last distinct index: 1 | +------------+ |
165 +-----------------------+ |
166 |m_children |
167 | +----------------------+ |
168 | |Query_block | SELECT * FROM t3 /
169 +-->|order by `a` limit 4 | <------------------------+
170 | +----------------------+ -------------------------+ next
171 | \
172 | +------------+ SELECT * FROM t4 |
173 +-->|Query_block | <------------------------------------+
174 +------------+
175 \endverbatim
176 Note that all leaf query blocks representing the query specifications are
177 linked under Query_expression via their next pointers. The nesting is achieved
178 by the arrows on the left side of the figure, via the nodes' m_children
179 members. The four classes Query_term_unary and Query_term_{union, intersect,
180 except} are modelled via the base class Query_term_set_op which contains a
181 m_children member. Each of these also contain a Query_block which will handle
182 its order by and/or limit clauses. These are similar to the old so-called
183 "fake_query_block" (which is now gone), and are not linked in with "next"
184 pointers.
185
186 The is also a back pointer from the children nodes to the parent Query_term
187 object (not shown).
188
189 In the simple case of a single query specification (or table value constructor
190 or explicit table), there is no super-structure over the Query_block linked
191 from the Query_expression, i.e. Query_expression's m_query_term member is just
192 a Query_block.
193
194 The query blocks (QT_QUERY_BLOCK nodes) corresponding to the query
195 specification (or table value constructors) are prepared and optimized by
196 running over them from the Query_expression via the slave/next pointers as
197 before. There are separate methods which handle prepare and optimization for
198 non-leaves, i.e. nodes of types QT_UNARY, QT_INTERSECT, QT_EXCEPT and
199 QT_UNION.
200
201 We also define an iterator class (Query_terms) for iterating over all
202 the nodes in the tree, see also Query_expression::query_terms() for its use.
203 When possible, we access all nodes using iterators.
204
205 The built structure can be traced with the debug trace keyword "ast", e.g.
206 as SET SESSION debug = 'd,ast:O,/tmp/mysqld.trace';
207*/
208
210 public:
211 // The next two methods used during construction of the tree.
212 /**
213 Called after contextualization to simplify query, c.f. sql_yacc.yy
214 CONTEXTUALIZE_VIEW. It also sets up the parent pointers.
215 @param parent parent of this if any
216 */
218
219 /**
220 Return true if structure is too deep, i.e. more than MAX_SELECT_NESTING.
221 As a side effect is also gives the post processing block a select number,
222 this is done late so as to get numbers higher the leaf blocks, and in
223 inside out order, so that the top set operation block will have the highest
224 number.
225 @param parent the real parent in the tree as visited recursively
226 @param depth the current depth in the tree, starting at 0 at the top.
227 It is increased with one for each time we recurse into a child.
228 @returns error if too deep structure, else false
229 */
230 bool validate_structure(const Query_term *parent, int depth = 0) const;
231
232 /**
233 Determine if we have a redundant ORDER BY in block. Used during prepare.
234 @param block the query block
235 @param level the current nesting level
236 @return tuple {bool found, bool redundant} redundant==true means ORDER BY of
237 the block is redundant and can be eliminated.
238 */
239 std::pair<bool, bool> redundant_order_by(Query_block *block, int level);
240
241 // Tree structure
242
243 /**
244 Get the node tree type.
245 @returns the tree node type
246 */
247 virtual Query_term_type term_type() const = 0;
248 /**
249 Get the node type description.
250 @returns descriptive string for each node type.
251 */
252 virtual const char *operator_string() const = 0;
253 /**
254 Node destructor
255 */
256 virtual ~Query_term() = default;
257 /**
258 Get the number of children this node has.
259 @return the number
260 */
261 virtual size_t child_count() const { return 0; }
262
263 protected:
264 /**
265 Back pointer to the node whose child we are, or nullptr (root term).
266 */
268
269 public:
270 /// Getter for m_parent, q.v.
271 Query_term_set_op *parent() const { return m_parent; }
272
273 /**
274 Reset resources used.
275 @param full do full cleanup. Same semantics as for Query_expression's
276 cleanup
277 */
278 virtual void cleanup(bool full [[maybe_unused]]) {
279 assert(false); // should be overridden
280 }
281
282 /// Destroy the query term tree structure
283 virtual void destroy_tree() = 0;
284
285 /**
286 Open tmp tables for the tree of set operation query results, by recursing
287 @param thd session context
288 @param level level in the tree, top should be called with 0.
289 @return true on error
290 */
291 virtual bool open_result_tables(THD *thd [[maybe_unused]],
292 int level [[maybe_unused]]) {
293 assert(false); // should be overridden
294 return false;
295 }
296
297 // Printable representation
298
299 /**
300 Print the tree rooted at this node to buf. Call on top level with level==0
301 @param level level we are at in tree.
302 @param buf the buffer to format output into
303 */
304 virtual void debugPrint(int level, std::ostringstream &buf) const = 0;
305 /**
306 Print blank space indentation (unit: two) to buf according to level. Minion.
307 @param level level we are at in tree.
308 @param buf the buffer to format output into
309 */
310 static void indent(int level, std::ostringstream &buf);
311 /**
312 Print the pointer of this node and its parent to buf. Minion.
313 @param buf the buffer to format output into
314 */
316 /**
317 Print into str the order indicated in ord, using standard print_for_order
318 Used by traditional explain.
319 @param thd session state
320 @param str string to accumulate formatted output into
321 @param ord the ORDER to be printed
322 @param query_type controls how printing happens
323 */
324 static void print_order(const THD *thd, String *str, ORDER *ord,
325 enum_query_type query_type);
326
327 // The next set of members his contains all that is needed to implement ORDER
328 // BY + LIMIT in several layers of unary/binary set operations: they have
329 // taken over information earlier stored in a single instance directly in
330 // Query_expression, which was then limited to single level only.
331
332 /**
333 The query_block which holds the ORDER BY and LIMIT information for this
334 set operation. Note that for the case where the root is a simple
335 query block, this will return self.
336 @returns the query block
337 */
338 virtual Query_block *query_block() const = 0;
339
340 protected:
341 /**
342 The query result for this term. Shared between n-ary set operands, the first
343 one holds it, cf. owning_operand. Except at top level, this is always a
344 Query_result_union.
345 */
347 /**
348 The operand of a n-ary set operation (that owns the common query result) has
349 this set to true. It is always the first one.
350 */
351 bool m_owning_operand{false};
352 /**
353 Result temporary table for the set operation, if applicable
354 */
356 /**
357 Used only when streaming, i.e. not materialized result set
358 */
360
361 public:
362 /// Setter for m_setop_query_result, q.v.
364 /// Getter for m_setop_query_result, q.v.
366 /// Getter for m_setop_query_result, q.v. Use only if we can down cast.
368 return down_cast<Query_result_union *>(m_setop_query_result);
369 }
370 /// Cleanup m_setop_query_result, q.v.
371 void cleanup_query_result(bool full);
372
373 /// Setter for m_owning_operand, q.v.
375 /// Getter for m_owning_operand, q.v.
377
378 /// Setter for m_result_table, q.v.
380 /// Getter for m_result_table, q.v.
382
383 // Setter for m_fields, q.v.
385 // Getter for m_fields, q.v.
387
388 private:
389 /// class Query_terms iterator traversal state variable, hence friend
390 /// declaration immediately below
392 template <Visit_order order, Visit_leaves visit_leaves>
393 friend class Query_terms;
394};
395
396/// Common base class for n-ary set operations, including unary.
398 /// Replaces the old "fake" query block for post processing result set with
399 /// ORDER BY, LIMIT.
400 /// The query block(s) of the query specifications can be found via
401 /// m_children.
403
404 protected: // this node type is abstract
406
407 public:
408 /// Tree structure. Cardinality is one for unary, two or more for UNION,
409 /// EXCEPT, INTERSECT
411 /**
412 Index of last query expression which has <set-op> DISTINCT on its left. In
413 a list of <set-op>ed blocks, UNION is left-associative; so UNION DISTINCT
414 eliminates duplicates in all blocks up to the first one on its right
415 included. Which is why we only need to remember that query block. Is -1
416 for Unary.
417 */
419 /**
420 Presently only needed by EXCEPT set operator: the index of the first
421 DISTINCT set operand: minimum legal value is 1. If not DISTINCT, it should
422 have the value std::numeric_limits<int64_t>::max(). The value is set
423 in PT_set_operation::merge_descendants.
424 */
426 /**
427 true if the result of this set operation is materialized. A priori true
428 unless we have a pure UNION ALL.
429 */
431
432 /// Getter for m_block, q.v.
433 Query_block *query_block() const override { return m_block; }
434
435 /// Setter for m_block, q.v.
437 assert(!m_block);
438 if (b == nullptr) return true;
439
440 m_block = b;
441 return false;
442 }
443
444 size_t child_count() const override { return m_children.size(); }
445 bool open_result_tables(THD *thd, int level) override;
446 void cleanup(bool full) override;
447 void destroy_tree() override {
448 m_parent = nullptr;
449 for (Query_term *child : m_children) {
450 child->destroy_tree();
451 }
452 m_children.clear();
453 }
454 /**
455 Check if this set operation has a mix of DISTINCT and ALL.
456 @return true if so. Always false for unary
457 */
459 /**
460 Check if this term is a unary set operation
461 @return true if so
462 */
463 bool is_unary() const { return term_type() == QT_UNARY; }
464
465 // Recursively set up materialization for a query term tree. For details,
466 // see implementation.
468 THD *thd, TABLE *dst_table, bool union_distinct_only,
469 bool calc_found_rows);
470
471 protected:
472 /**
473 Common printing minion for set operations.
474 @param level level in tree
475 @param buf the buffer to format output into
476 @param type descriptive string of set operation to use for printing
477 */
478 void print(int level, std::ostringstream &buf, const char *type) const;
479};
480
481/// Node type for n-ary UNION
483 public:
484 /**
485 Constructor.
486 @param mem_root the mem_root to use for allocation
487 */
489 Query_term_type term_type() const override { return QT_UNION; }
490 const char *operator_string() const override { return "union"; }
491 void debugPrint(int level, std::ostringstream &buf) const override;
492};
493
494/// Node type for n-ary INTERSECT
496 public:
497 /**
498 Constructor.
499 @param mem_root the mem_root to use for allocation
500 */
502 Query_term_type term_type() const override { return QT_INTERSECT; }
503 const char *operator_string() const override { return "intersect"; }
504 void debugPrint(int level, std::ostringstream &buf) const override;
505};
506
507/// Node type for n-ary EXCEPT
509 public:
510 /**
511 Constructor.
512 @param mem_root the mem_root to use for allocation
513 */
515 Query_term_type term_type() const override { return QT_EXCEPT; }
516 const char *operator_string() const override { return "except"; }
517 void debugPrint(int level, std::ostringstream &buf) const override;
518};
519
520/// A <query primary> which is a parenthesized query expression (aka qe) body
521/// with order by clause and/or limit/offset clause and the qe body
522/// is not a binary set operation (union, except, intersect), but is viewed here
523/// as a degenerate set operation; i.e. a "unary".
524/// \verbatim
525/// Example: (SELECT * FROM .. ORDER BY .. LIMIT n) ORDER BY .. LIMIT m
526/// Tree:
527/// Query_expression
528/// | m_query_term
529/// Query_term_unary
530/// +--------------------+
531/// | m_block ------+----> Query_block which holds outer
532/// | : | ORDER BY and LIMIT
533/// | m_children[0] |
534/// +--------|-----------+
535/// V
536/// Query_block holds inner SELECT and its ORDER BY/LIMIT
537/// \endverbatim
538/// One extra Query_term_unary is added for each level of nesting
539/// with the top one representing the outermost ORDER BY/LIMIT/OFFSET
540///
542 public:
543 /**
544 Constructor.
545 @param mem_root the mem_root to use for allocation
546 @param t the child term
547 */
550 m_last_distinct = 0;
551 m_children.push_back(t);
552 }
553 Query_term_type term_type() const override { return QT_UNARY; }
554 const char *operator_string() const override { return "result"; }
555 void debugPrint(int level, std::ostringstream &buf) const override;
556};
557
558/**
559 Containing class for iterator over the query term tree. The structure is
560 in part dictated by C++ conventions for iterators.
561 @tparam visit_order indicates whether pre or post order visiting is requested
562 @tparam visit_leaves indicated whether to visit the leaf nodes (query blocks)
563*/
564template <Visit_order visit_order, Visit_leaves visit_leaves>
566 private:
567 /**
568 The iterator class itself is private. Only used directly by begin and end
569 */
571 public:
572 /**
573 Construct an iterator over the query term tree rooted in root, optionally
574 skipping the leaves. Skipping is useful for those cases where the leaves
575 are visited separately[1] and we only want to visit the set operation
576 nodes in the tree.
577 [1] By walking the Query_expression::first_query_block and
578 Query_block::next_query_block chain
579 @param root the node to start iteration from
580 */
582 m_current = root;
583 if (m_current != nullptr) m_current->m_curr_id = 0;
584 if constexpr (visit_order == QTC_POST_ORDER) {
585 // start at left-most leaf node
586 while (m_current != nullptr &&
588 m_current = down_cast<Query_term_set_op *>(m_current)
589 ->m_children[m_current->m_curr_id];
590 m_current->m_curr_id = 0;
591 }
592 if constexpr (visit_leaves == VL_SKIP_LEAVES) operator++();
593 }
594 }
595
597
599 if (m_current == nullptr) {
600 return *this;
601 }
602 while (m_current != nullptr) {
603 uint children = m_current->child_count();
604 if constexpr (visit_order == QTC_PRE_ORDER) {
605 while (m_current != nullptr && (m_current->m_curr_id >= children)) {
606 // no more at this level, go back up
608 if (m_current != nullptr) {
609 children = m_current->child_count();
610 }
611 }
612
613 if (m_current == nullptr) return *this;
614 m_current = down_cast<Query_term_set_op *>(m_current)
615 ->m_children[m_current->m_curr_id++];
616 m_current->m_curr_id = 0;
617 } else {
619 if (m_current == nullptr) return *this;
621 while (m_current != nullptr &&
623 m_current = down_cast<Query_term_set_op *>(m_current)
624 ->m_children[m_current->m_curr_id];
625 m_current->m_curr_id = 0;
626 }
627 } else {
628 // return non-leaf
629 }
630 }
631 if constexpr (visit_leaves == VL_VISIT_LEAVES)
632 break;
633 else if (m_current->term_type() != QT_QUERY_BLOCK)
634 break;
635 }
636 return *this;
637 }
638
640
641 bool operator==(const Query_term_iterator &other) const {
642 return m_current == other.m_current;
643 }
644
645 bool operator!=(const Query_term_iterator &other) const {
646 return !((*this) == other);
647 }
648 /// Iterator state consists of this and Query_term::m_curr_id
650 };
651
652 public:
653 /// Construct an iterator starting at root.
654 Query_terms(Query_term *root) : m_root(root) {}
655
658
659 private:
661};
662
663/* Local Variables: */
664/* mode: c++ */
665/* fill-column: 80 */
666/* End: */
667
668#endif /* QUERY_NODE_INCLUDED */
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:426
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1163
Definition: sql_union.h:40
Definition: query_result.h:58
Node type for n-ary EXCEPT.
Definition: query_term.h:508
const char * operator_string() const override
Get the node type description.
Definition: query_term.h:516
Query_term_type term_type() const override
Get the node tree type.
Definition: query_term.h:515
void debugPrint(int level, std::ostringstream &buf) const override
Print the tree rooted at this node to buf.
Definition: query_term.cc:246
Query_term_except(MEM_ROOT *mem_root)
Constructor.
Definition: query_term.h:514
Node type for n-ary INTERSECT.
Definition: query_term.h:495
const char * operator_string() const override
Get the node type description.
Definition: query_term.h:503
void debugPrint(int level, std::ostringstream &buf) const override
Print the tree rooted at this node to buf.
Definition: query_term.cc:241
Query_term_intersect(MEM_ROOT *mem_root)
Constructor.
Definition: query_term.h:501
Query_term_type term_type() const override
Get the node tree type.
Definition: query_term.h:502
Common base class for n-ary set operations, including unary.
Definition: query_term.h:397
int64_t m_last_distinct
Index of last query expression which has <set-op> DISTINCT on its left.
Definition: query_term.h:418
mem_root_deque< Query_term * > m_children
Tree structure.
Definition: query_term.h:410
void destroy_tree() override
Destroy the query term tree structure.
Definition: query_term.h:447
void cleanup(bool full) override
Reset resources used.
Definition: query_term.cc:222
int64_t m_first_distinct
Presently only needed by EXCEPT set operator: the index of the first DISTINCT set operand: minimum le...
Definition: query_term.h:425
void print(int level, std::ostringstream &buf, const char *type) const
Common printing minion for set operations.
Definition: query_term.cc:191
Mem_root_array< MaterializePathParameters::Operand > setup_materialize_set_op(THD *thd, TABLE *dst_table, bool union_distinct_only, bool calc_found_rows)
Sets up each(*) query block in this query expression for materialization into the given table by maki...
Definition: sql_union.cc:1388
bool has_mixed_distinct_operators()
Check if this set operation has a mix of DISTINCT and ALL.
Definition: query_term.cc:186
bool set_block(Query_block *b)
Setter for m_block, q.v.
Definition: query_term.h:436
Query_term_set_op(MEM_ROOT *mem_root)
Definition: query_term.h:405
bool is_unary() const
Check if this term is a unary set operation.
Definition: query_term.h:463
Query_block * query_block() const override
Getter for m_block, q.v.
Definition: query_term.h:433
bool open_result_tables(THD *thd, int level) override
Open tmp tables for the tree of set operation query results, by recursing.
Definition: query_term.cc:208
bool m_is_materialized
true if the result of this set operation is materialized.
Definition: query_term.h:430
size_t child_count() const override
Get the number of children this node has.
Definition: query_term.h:444
Query_block * m_block
Replaces the old "fake" query block for post processing result set with ORDER BY, LIMIT.
Definition: query_term.h:402
A <query primary> which is a parenthesized query expression (aka qe) body with order by clause and/or...
Definition: query_term.h:541
Query_term_unary(MEM_ROOT *mem_root, Query_term *t)
Constructor.
Definition: query_term.h:548
void debugPrint(int level, std::ostringstream &buf) const override
Print the tree rooted at this node to buf.
Definition: query_term.cc:423
const char * operator_string() const override
Get the node type description.
Definition: query_term.h:554
Query_term_type term_type() const override
Get the node tree type.
Definition: query_term.h:553
Node type for n-ary UNION.
Definition: query_term.h:482
void debugPrint(int level, std::ostringstream &buf) const override
Print the tree rooted at this node to buf.
Definition: query_term.cc:237
const char * operator_string() const override
Get the node type description.
Definition: query_term.h:490
Query_term_union(MEM_ROOT *mem_root)
Constructor.
Definition: query_term.h:488
Query_term_type term_type() const override
Get the node tree type.
Definition: query_term.h:489
Query term tree structure.
Definition: query_term.h:209
Query_term * pushdown_limit_order_by(Query_term_set_op *parent=nullptr)
Called after contextualization to simplify query, c.f.
Definition: query_term.cc:74
virtual Query_block * query_block() const =0
The query_block which holds the ORDER BY and LIMIT information for this set operation.
void cleanup_query_result(bool full)
Cleanup m_setop_query_result, q.v.
Definition: query_term.cc:175
Query_term_set_op * m_parent
Back pointer to the node whose child we are, or nullptr (root term).
Definition: query_term.h:267
virtual bool open_result_tables(THD *thd, int level)
Open tmp tables for the tree of set operation query results, by recursing.
Definition: query_term.h:291
Query_result * m_setop_query_result
The query result for this term.
Definition: query_term.h:346
Query_result_union * setop_query_result_union()
Getter for m_setop_query_result, q.v. Use only if we can down cast.
Definition: query_term.h:367
void set_setop_query_result(Query_result *rs)
Setter for m_setop_query_result, q.v.
Definition: query_term.h:363
Query_result * setop_query_result()
Getter for m_setop_query_result, q.v.
Definition: query_term.h:365
void set_fields(mem_root_deque< Item * > *fields)
Definition: query_term.h:384
virtual Query_term_type term_type() const =0
Get the node tree type.
Table_ref & result_table()
Getter for m_result_table, q.v.
Definition: query_term.h:381
virtual const char * operator_string() const =0
Get the node type description.
bool owning_operand()
Getter for m_owning_operand, q.v.
Definition: query_term.h:376
mem_root_deque< Item * > * fields()
Definition: query_term.h:386
void set_owning_operand()
Setter for m_owning_operand, q.v.
Definition: query_term.h:374
Query_term_set_op * parent() const
Getter for m_parent, q.v.
Definition: query_term.h:271
void set_result_table(Table_ref *tl)
Setter for m_result_table, q.v.
Definition: query_term.h:379
virtual void cleanup(bool full)
Reset resources used.
Definition: query_term.h:278
virtual size_t child_count() const
Get the number of children this node has.
Definition: query_term.h:261
virtual ~Query_term()=default
Node destructor.
Table_ref * m_result_table
Result temporary table for the set operation, if applicable.
Definition: query_term.h:355
uint m_curr_id
class Query_terms iterator traversal state variable, hence friend declaration immediately below
Definition: query_term.h:391
std::pair< bool, bool > redundant_order_by(Query_block *block, int level)
Determine if we have a redundant ORDER BY in block.
Definition: query_term.cc:43
bool validate_structure(const Query_term *parent, int depth=0) const
Return true if structure is too deep, i.e.
Definition: query_term.cc:159
virtual void debugPrint(int level, std::ostringstream &buf) const =0
Print the tree rooted at this node to buf.
bool m_owning_operand
The operand of a n-ary set operation (that owns the common query result) has this set to true.
Definition: query_term.h:351
void printPointers(std::ostringstream &buf) const
Print the pointer of this node and its parent to buf.
Definition: query_term.cc:231
static void indent(int level, std::ostringstream &buf)
Print blank space indentation (unit: two) to buf according to level.
Definition: query_term.cc:227
virtual void destroy_tree()=0
Destroy the query term tree structure.
static void print_order(const THD *thd, String *str, ORDER *ord, enum_query_type query_type)
Print into str the order indicated in ord, using standard print_for_order Used by traditional explain...
Definition: query_term.cc:33
mem_root_deque< Item * > * m_fields
Used only when streaming, i.e.
Definition: query_term.h:359
The iterator class itself is private.
Definition: query_term.h:570
bool operator!=(const Query_term_iterator &other) const
Definition: query_term.h:645
Query_term_iterator & operator++()
Definition: query_term.h:598
bool operator==(const Query_term_iterator &other) const
Definition: query_term.h:641
Query_term * m_current
Iterator state consists of this and Query_term::m_curr_id.
Definition: query_term.h:649
Query_term * operator*()
Definition: query_term.h:639
Query_term_iterator(Query_term *root)
Construct an iterator over the query term tree rooted in root, optionally skipping the leaves.
Definition: query_term.h:581
Containing class for iterator over the query term tree.
Definition: query_term.h:565
Query_term_iterator end()
Definition: query_term.h:657
Query_terms(Query_term *root)
Construct an iterator starting at root.
Definition: query_term.h:654
Query_term * m_root
Definition: query_term.h:660
Query_term_iterator begin()
Definition: query_term.h:656
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:2863
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:111
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
enum_query_type
Query type constants (usable as bitmap flags).
Definition: enum_query_type.h:31
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1073
Definition: buf0block_hint.cc:30
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2870
Query_term_type
This class hierarchy is used to represent SQL structures between <query expression> and <query specif...
Definition: query_term.h:89
@ QT_UNARY
Represents a query primary with parentesized query expression body with order by clause and/or limit/...
Definition: query_term.h:96
@ QT_EXCEPT
Definition: query_term.h:100
@ QT_UNION
Definition: query_term.h:101
@ QT_INTERSECT
Represents the three set operations.
Definition: query_term.h:99
@ QT_QUERY_BLOCK
Represents Query specification, table value constructor and explicit table.
Definition: query_term.h:92
Visit_leaves
Query term iterator template argument type: whether to visit leaf nodes.
Definition: query_term.h:107
@ VL_SKIP_LEAVES
Definition: query_term.h:107
@ VL_VISIT_LEAVES
Definition: query_term.h:107
Visit_order
Query term iterator template argument type: how to visit nodes in tree.
Definition: query_term.h:105
@ QTC_PRE_ORDER
Definition: query_term.h:105
@ QTC_POST_ORDER
Definition: query_term.h:105
required string type
Definition: replication_group_member_actions.proto:34
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: table.h:285
Definition: table.h:1405