MySQL 9.0.0
Source Code Documentation
query_term.h File Reference

Go to the source code of this file.

Classes

class  Query_term
 Query term tree structure. More...
 
class  Query_term_set_op
 Common base class for n-ary set operations, including unary. More...
 
class  Query_term_union
 Node type for n-ary UNION. More...
 
class  Query_term_intersect
 Node type for n-ary INTERSECT. More...
 
class  Query_term_except
 Node type for n-ary EXCEPT. More...
 
class  Query_term_unary
 A <query primary> which is a parenthesized query expression (aka qe) body with order by clause and/or limit/offset clause and the qe body is not a binary set operation (union, except, intersect), but is viewed here as a degenerate set operation; i.e. More...
 
class  Query_terms< visit_order, visit_leaves >
 Containing class for iterator over the query term tree. More...
 
class  Query_terms< visit_order, visit_leaves >::Query_term_iterator
 The iterator class itself is private. More...
 

Enumerations

enum  Query_term_type {
  QT_QUERY_BLOCK , QT_UNARY , QT_INTERSECT , QT_EXCEPT ,
  QT_UNION
}
 This class hierarchy is used to represent SQL structures between <query expression> and <query specification>. More...
 
enum  Visit_order { QTC_POST_ORDER , QTC_PRE_ORDER }
 Query term iterator template argument type: how to visit nodes in tree. More...
 
enum  Visit_leaves { VL_VISIT_LEAVES , VL_SKIP_LEAVES }
 Query term iterator template argument type: whether to visit leaf nodes. More...
 

Enumeration Type Documentation

◆ Query_term_type

This class hierarchy is used to represent SQL structures between <query expression> and <query specification>.

The class Query_expression represents <query expression> and the class Query_block represents <query specification>, <table value constructor> and <explicit table>, cf. definitions in sql_lex.h. Originally, MySQL supported only one set operation, UNION. This was implicitly represented by having one Query_expression object own several Query_block objects via next pointers. This made things simple, but limited us to left-deep nesting of unions, and also from representing INTERSECTION and EXCEPT, as well as nesting several layers of <query primary>s containing <query expression body>s, e.g.

(((SELECT a,b FROM t) ORDER BY a LIMIT 5) ORDER BY -b LIMIT 3) ORDER BY a;

could not be supported. With the present class hierarchy we enable the full set of set operations and arbitrary nesting, as allowed by the SQL grammar, viz:

<query expression> ::=
    [ <with clause> ] <query expression body>
    [ <order by clause> ] [ <limit/offset> ]

<query expression body> ::=
   <query term>
 | <query expression body> UNION [ ALL | DISTINCT ]
   [ <corresponding spec> ] <query term>
 | <query expression body> EXCEPT [ ALL | DISTINCT ]
   [ <corresponding spec> ] <query term>

<query term> ::=
   <query primary>
 | <query expression body> INTERSECT [ ALL | DISTINCT ]
   [ <corresponding spec> ] <query primary>

<query primary> ::=
   <simple table>
 | <left paren> <query expression body>
   [ <order by clause> ] [ <limit/offset (*)> ]
   <right paren>

<simple table> ::=
   <query specification>
 | <table value constructor>
 | <explicit table>

(*) MySQL syntax and semantics. The standard uses /<result offset clause/> and
    \<fetch first clause\>.

Note that INTERSECT binds tighter than UNION and EXCEPT. Now, let's turn to how these structures are represented in MySQL. The node types are enumerated by Query_term_type. The nodes themselves by the class hierarchy rooted in Query_term (abstract). Node type of the query term tree nodes

Enumerator
QT_QUERY_BLOCK 

Represents Query specification, table value constructor and explicit table.

QT_UNARY 

Represents a query primary with parentesized query expression body with order by clause and/or limit/offset clause.

If none of order by or limit is present, we collapse this level of parentheses.

QT_INTERSECT 

Represents the three set operations.

Nodes are N-ary, i.e. a node can hold two or more operands.

QT_EXCEPT 
QT_UNION 

◆ Visit_leaves

Query term iterator template argument type: whether to visit leaf nodes.

Enumerator
VL_VISIT_LEAVES 
VL_SKIP_LEAVES 

◆ Visit_order

Query term iterator template argument type: how to visit nodes in tree.

Enumerator
QTC_POST_ORDER 
QTC_PRE_ORDER