classOrderedSetOperation
final class OrderedSetOperation
implements Query
uses Snapshot
@visibility public.A set operation of a 5.6 subquery whose ORDER BY or LIMIT is written after the clauses of its last SELECT.
In a 5.6 subquery or derived table the ORDER BY and LIMIT written inside the last unparenthesized SELECT of a union apply to the union, unless an ORDER BY or LIMIT follows that SELECT (opt_union_order_or_limit): then the server makes its fake query block the global parameters of the union, the last SELECT keeps its own ORDER BY, LIMIT and locking clauses, and the clauses written after it order and limit the result of the union. This node holds that form: the operation, its last SELECT with its own clauses, and the ORDER BY and LIMIT of the result. Nothing follows it in the 5.6 grammar, so it is never an operand or a body itself.
Rule: MYSQL-ORDERED-SET-OPERATION-001. Both operands are derived by MYSQL-SET-FACTS-001; the ORDER BY and LIMIT see the output columns of the operation as MYSQL-QUERY-EXPRESSION-FACTS-001 does. Source: https://dev.mysql.com/doc/refman/5.6/en/union.html, https://github.com/mysql/mysql-server/blob/mysql-5.6.51/sql/sql_yacc.yy (union_order_or_limit). Status: Implemented.
$query = (new \SqlSemantics\Platform\MySql\Semantics('mysql-5.6.51'))->analyze('SELECT (SELECT 1 UNION SELECT a FROM t FOR UPDATE ORDER BY 1)');
$union = $query->statement->items[0]->expression->query;
[count($union->right->locking), count($union->orderBy)] // => [1, 1]$one = new \SqlSemantics\Platform\MySql\Statement\Query\Select([], [new \SqlSemantics\Platform\MySql\Statement\Query\SelectExpression(new \SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral('1'))]);
$two = new \SqlSemantics\Platform\MySql\Statement\Query\Select([], [new \SqlSemantics\Platform\MySql\Statement\Query\SelectExpression(new \SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral('2'))]);
new \SqlSemantics\Platform\MySql\Statement\Query\Set\OrderedSetOperation($one, \SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperator::Union, null, $two, [], new \SqlSemantics\Platform\MySql\Statement\Query\Clause\RowLimit(new \SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral('1'))) // throws \SqlSemantics\Platform\MySql\Model\Diagnostic\InvalidConstructionProperties§
public list<OrderItem> $orderBypublic Query|LeadingUnion $leftpublic SetOperator $operatorpublic SetQuantifier|null $quantifierpublic Select $rightpublic Limit|null $limit = nullMethods§
public function __construct(
public Query|LeadingUnion $left,
public SetOperator $operator,
public SetQuantifier|null $quantifier,
public Select $right,
list<OrderItem> $orderBy = [],
public Limit|null $limit = null,
)Parameters
$left | Query|LeadingUnion | The operands before the last SELECT |
$operator | SetOperator | The operator before the last SELECT |
$quantifier | SetQuantifier|null | The written DISTINCT or ALL |
$right | Select | The last SELECT with its own ORDER BY, LIMIT or locking clauses |
$orderBy | list<OrderItem> | The ORDER BY items of the result |
$limit | Limit|null | The LIMIT of the result |
Calls 8
- static-call Check::listOf() line 79
- class-const OrderItem::class() line 79
- method-call SortScopes::check() line 80
- new SortScopes line 80
- static-call Check::input() line 81
- method-call Select::trailed() line 82
- method-call SetOperator::tighter() line 83
- class-const SetOperator::Union() line 84
public function deriveQuery(Derivation $derivation, Environment $outer): QueryFactDerives both operands, the output columns, the ordering and the limit.
Parameters
$derivation | Derivation | |
$outer | Environment |
Returns
Calls 6
- static-call Check::input() line 93
- class-const GrammarRelease::MySql5651() line 93
- method-call SetFacts::operands() line 94
- new SetFacts line 94
- method-call ExpressionFacts::ordering() line 95
- new ExpressionFacts line 95
public function render(Output $out): voidWrites the operands around the operator, then the ORDER BY and LIMIT of the result.
Parameters
$out | Output |
Calls 2
- method-call Output::node() line 105
- method-call Output::keyword() line 107
Test cases 5§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 4
OrderedSetOperationTest::testDeriveQueryNeedsMySql56callsOrderedSetOperationTest::testDeriveQueryOrdersTheResultAndKeepsTheClausesOfTheLastSelectcallsOrderedSetOperationTest::testRejectsALastSelectWithoutClausesOfItsOwncallsOrderedSetOperationTest::testRejectsAsAnOperandOfAnotherSetOperationcalls
Other tests reaching this symbol 1
Relations§
Instantiated in 1
Type checks 6
- instanceof SqlSemantics\Platform\MySql\Rules\Routine\ProgramRestrictions::into() packages/sql-semantics-mysql/src/Rules/Routine/ProgramRestrictions.php:154
- instanceof SqlSemantics\Platform\MySql\Rules\TableDefinition\ViewColumns::block() packages/sql-semantics-mysql/src/Rules/TableDefinition/ViewColumns.php:135
- instanceof SqlSemantics\Platform\MySql\Statement\Query\QueryExpression::__construct() packages/sql-semantics-mysql/src/Statement/Query/QueryExpression.php:66
- instanceof SqlSemantics\Platform\MySql\Statement\Query\QueryStatement::__construct() packages/sql-semantics-mysql/src/Statement/Query/QueryStatement.php:64
- instanceof SqlSemantics\Platform\MySql\Statement\Query\Set\LeadingUnion::__construct() packages/sql-semantics-mysql/src/Statement/Query/Set/LeadingUnion.php:62
- instanceof SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation::__construct() packages/sql-semantics-mysql/src/Statement/Query/Set/SetOperation.php:62