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