classChain
final class Chain
The operands of a 5.x UNION in written order, the quantifiers between them and the clauses written after the last one.
Rule: MYSQL-UNION-LEGACY-001. The ORDER BY, LIMIT, INTO and locking clauses written inside the last unparenthesized SELECT of a union apply to the whole union, so they are taken out of that block; with the clauses written after a last parenthesized operand they wrap the union. In a 5.6 subquery an ORDER BY or LIMIT written after the last SELECT applies to the union instead, and the last SELECT keeps its own clauses (the union_order_or_limit action makes the fake query block the global parameters). The server rejects, while parsing, ORDER BY or INTO in an earlier unparenthesized SELECT, LIMIT there in 5.7 (5.6 keeps it as the block's own), an ORDER BY or LIMIT a 5.6 subquery writes after an earlier SELECT other than a LIMIT after the first one, and PROCEDURE ANALYSE in a union. An earlier SELECT keeps its own locking clauses and, in 5.6, its own LIMIT; when it is not the first operand the operands up to it form a leading union (MYSQL-LEADING-UNION-001). The operands are combined left-deep. A lowering-time value. Source: https://dev.mysql.com/doc/refman/5.7/en/union.html ("To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses"; "Only the last SELECT statement can use INTO OUTFILE"), https://github.com/mysql/mysql-server/blob/mysql-5.6.51/sql/sql_parse.cc (mysql_new_select), https://github.com/mysql/mysql-server/blob/mysql-5.7.44/sql/sql_lex.cc (LEX::new_union_query). Status: Implemented.
Properties§
public list<SetQuantifier|null> $quantifierspublic GrammarRelease $releasepublic Trailer $trailer = new \SqlSemantics\Platform\MySql\Lowering\Query\Shared\Trailer()Methods§
public function __construct(
public non-empty-list<array{Block|Query, Trailer}> $operands,
public list<SetQuantifier|null> $quantifiers,
public GrammarRelease $release,
public Trailer $trailer = new \SqlSemantics\Platform\MySql\Lowering\Query\Shared\Trailer(),
)Parameters
$operands | non-empty-list<array{Block|Query, Trailer}> | The operands in written order, each with the clauses written after it |
$quantifiers | list<SetQuantifier|null> | The quantifier written after each UNION |
$release | GrammarRelease | The grammar release the union is written in |
$trailer | Trailer | The clauses written after the last operand |
public function query(): QueryCombines the operands into one query.
Returns
Throws
AnalysisException When the last SELECT writes PROCEDURE ANALYSE (ER_WRONG_USAGE of the 5.6 `procedure_analyse_clause` action, which refuses any block but the first, and of 5.7 `PT_procedure_analyse::contextualize`), or an earlier operand writes a clause the server rejects thereImplementationGap When clauses cannot follow their query blockTest cases 3
Called from 3
- method-call SqlSemantics\Platform\MySql\Lowering\Query\Legacy\FactorRule::parens() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/FactorRule.php:123
- method-call SqlSemantics\Platform\MySql\Lowering\Query\Legacy\SubqueryRule::body() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/SubqueryRule.php:94
- method-call SqlSemantics\Platform\MySql\Lowering\Query\Legacy\UnionRule::chain() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/UnionRule.php:144
Calls 9
- function-call
countline 69 - function-call
array_sliceline 76 - method-call Chain::earlier() line 77
- method-call Chain::enclosed() line 77
- new LeadingUnion line 80
- new SetOperation line 81
- class-const SetOperator::Union() line 81
- new AnalysisException line 89
- new OrderedSetOperation line 92
Finishes an operand before the last one, rejecting the clauses the server does not accept there.
Returns
Throws
AnalysisException When the operand writes such a clause: ER_WRONG_USAGE "UNION and ORDER BY|LIMIT|INTO|PROCEDURE ANALYSE" of `add_select_to_union_list` and `mysql_new_select` (5.6, run by the `union_list` action) and of `LEX::new_union_query` (5.7, run while contextualizing the union), or ER_SYNTAX_ERROR when the clauses written after an earlier SELECT made the fake query block current (`add_select_to_union_list` refuses GLOBAL_OPTIONS_TYPE)Test cases 3
Called from 1
Calls 4
- class-const GrammarRelease::MySql5651() line 106
- new AnalysisException line 107
- method-call Trailer::empty() line 115
- method-call Block::finish() line 119
Finishes a parenthesized operand before the last one with the ORDER BY and LIMIT a 5.x subquery writes after it.
After the first operand the clauses belong to the query block in the parentheses, unless an ORDER BY follows a block that orders or limits its rows itself: the order_clause action (5.6) and PT_order::contextualize (5.7) then make a fake query block current. After a later operand the fake query block of the union is current. The next UNION refuses a current fake query block.
Returns
Throws
AnalysisException When the clauses made the fake query block current: ER_SYNTAX_ERROR of `add_select_to_union_list` (5.6, GLOBAL_OPTIONS_TYPE) and ER_WRONG_USAGE "UNION and ORDER BY|LIMIT" of `LEX::new_union_query` (5.7)Test cases 3
Called from 1
Calls 3
- method-call Trailer::empty() line 141
- new AnalysisException line 142
- method-call Trailer::wrap() line 145
Test cases 9§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 9
ChainTest::testEarlierKeepsALimitBeforeTheLastOperandIn56callsChainTest::testEarlierRejectsALimitBeforeTheLastOperandIn57callsChainTest::testEarlierRejectsAnOrderingAfterTheFirstOperandOfASubqueryUnioncallsChainTest::testEnclosedKeepsTheClausesAfterTheFirstParenthesizedOperandcallsChainTest::testEnclosedRejectsAnOrderingAfterAParenthesizedOperandThatLimitsItselfcallsChainTest::testEnclosedRejectsClausesAfterALaterParenthesizedOperandcallsChainTest::testQueryKeepsTheOwnClausesOfAMiddleSelectInALeadingUnioncallsChainTest::testQueryLiftsTheClausesOfTheLastBlockcallsChainTest::testQueryOrdersAUnionAfterTheClausesOfItsLastBlockcalls
Relations§
Instantiated in 3
- new SqlSemantics\Platform\MySql\Lowering\Query\Legacy\FactorRule::parens() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/FactorRule.php:123
- new SqlSemantics\Platform\MySql\Lowering\Query\Legacy\SubqueryRule::body() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/SubqueryRule.php:94
- new SqlSemantics\Platform\MySql\Lowering\Query\Legacy\UnionRule::chain() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/UnionRule.php:144
Method calls 3
- method-call SqlSemantics\Platform\MySql\Lowering\Query\Legacy\FactorRule::parens() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/FactorRule.php:123
- method-call SqlSemantics\Platform\MySql\Lowering\Query\Legacy\SubqueryRule::body() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/SubqueryRule.php:94
- method-call SqlSemantics\Platform\MySql\Lowering\Query\Legacy\UnionRule::chain() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/UnionRule.php:144