classSetOperation
final class SetOperation
implements Statement, Query
uses Snapshot
@visibility public.A set operation: UNION, EXCEPT or INTERSECT of two queries.
Operations of equal binding associate to the left, so a chain is a left-deep tree; an operand that would need parentheses to keep its place is refused: a set operation on the right that binds no more tightly, a looser one on the left of INTERSECT, a query with a WITH clause or with statement-level INTO or locking clauses, and on the right a query whose ORDER BY, LIMIT or other trailing clauses would be read as applying to the whole operation.
Rule: MYSQL-SET-OPERATION-001. The output is derived by MYSQL-SET-FACTS-001. Source: https://dev.mysql.com/doc/refman/8.4/en/set-operations.html, https://dev.mysql.com/doc/refman/8.4/en/union.html. Status: Implemented.
$query = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SELECT a FROM t UNION SELECT b FROM u');
[$query->statement->left->from->name->name->value, $query->statement->right->from->name->name->value] // => ['t', 'u']$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'))]);
$three = new \SqlSemantics\Platform\MySql\Statement\Query\Select([], [new \SqlSemantics\Platform\MySql\Statement\Query\SelectExpression(new \SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral('3'))]);
new \SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation($one, \SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperator::Union, null, new \SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation($two, \SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperator::Union, null, $three)) // throws \SqlSemantics\Platform\MySql\Model\Diagnostic\InvalidConstructionProperties§
public Query|LeadingUnion $leftpublic SetOperator $operatorpublic SetQuantifier|null $quantifierpublic Query $rightMethods§
public function __construct(
public Query|LeadingUnion $left,
public SetOperator $operator,
public SetQuantifier|null $quantifier,
public Query $right,
)Parameters
$left | Query|LeadingUnion | The left operand: a query, or the operands of a 5.x union before this UNION when the last of them keeps its own clauses |
$operator | SetOperator | The operator |
$quantifier | SetQuantifier|null | The written DISTINCT or ALL |
$right | Query | The right operand |
Calls 4
- static-call Check::input() line 58
- method-call SetOperator::tighter() line 59
- class-const SetOperator::Union() line 60
- method-call Query::trailed() line 64
public function deriveStatement(Derivation $derivation): voidDerives the set operation as a statement root and records its rows as the output.
Parameters
$derivation | Derivation |
Calls 3
- method-call Derivation::output() line 72
- method-call Derivation::query() line 72
- method-call Derivation::environment() line 72
public function deriveQuery(Derivation $derivation, Environment $outer): QueryFactDerives both operands and the output columns.
Parameters
$derivation | Derivation | |
$outer | Environment |
Returns
Calls 2
- method-call SetFacts::derive() line 80
- new SetFacts line 80
public function render(Output $out): voidWrites the operands around the operator.
Parameters
$out | Output |
Calls 3
- method-call Output::node() line 88
- method-call QueryTails::operation() line 89
- new QueryTails line 89
Test cases 26§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 7
SetOperationTest::testALastOperandWithItsOwnOrderingIsRejectedcallsSetOperationTest::testALooserOperationOnTheLeftOfIntersectIsRejectedcallsSetOperationTest::testANestedOperationOnTheRightIsRejectedcallsSetOperationTest::testAQueryExpressionOnTheRightIsRejectedcallsSetOperationTest::testAQueryStatementAsAnOperandIsRejectedcallsSetOperationTest::testDeriveStatementRecordsTheRowsAsTheOutputcallsSetOperationTest::testRenderWritesTheOperandsLeftDeepAndKeepsParenthesescalls
Other tests reaching this symbol 19
ChainTest::testQueryKeepsTheOwnClausesOfAMiddleSelectInALeadingUnioncallsChainTest::testQueryLiftsTheClausesOfTheLastBlockcallsUnionRuleTest::testStatementLiftsTheOrderingOfTheLastSelectcallsUnionRuleTest::testViewQueryLowersTheQueryOfAViewcallsExpressionRuleTest::testBodyBuildsALeftDeepChaincallsExpressionRuleTest::testQuantifierLowersDistinctAndAllcallsQueryRulesTest::testLegacyParenthesizedQueryKeepsTheParenthesesAndTheUnioncallsQueryRulesTest::testLegacyQueryLowersCreateSelectWithItsUnioncallsBlockRuleTest::providerDeclarationLowersACursorcallsCommonTablesTest::testOperandsAnswersTheChainInWrittenOrdercallsCommonTablesTest::testOperandsWalksALeadingUnioncallsQueryTailsTest::testOperationWritesTheOperatorTheQuantifierAndTheRightOperandcallsSetFactsTest::testLeadingRefusesAnOwnLimitOnMySql57callsParenthesizedQueryTest::testDeriveQueryAnswersTheFactsOfTheInnerQuerycallsQueryExpressionTest::testDeriveStatementRecordsTheRowsOfTheBodycallsLeadingUnionTest::testRenderKeepsTheOwnClausesOfAnEarlierSelectcallsLeadingUnionTest::testRenderNeedsMySql5ForTheFormAndMySql56ForAnOwnLimitcallsLeadingUnionTest::testRenderNeedsUnionAfterItcallsOrderedSetOperationTest::testRejectsAsAnOperandOfAnotherSetOperationcalls
Relations§
Instantiated in 4
- new SqlSemantics\Platform\MySql\Lowering\Query\Legacy\Chain::query() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/Chain.php:81
- new SqlSemantics\Platform\MySql\Lowering\Query\Legacy\Chain::query() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/Chain.php:86
- new SqlSemantics\Platform\MySql\Lowering\Query\Legacy\Chain::query() packages/sql-semantics-mysql/src/Lowering/Query/Legacy/Chain.php:95
- new SqlSemantics\Platform\MySql\Lowering\Query\Modern\ExpressionRule::body() packages/sql-semantics-mysql/src/Lowering/Query/Modern/ExpressionRule.php:153
Type checks 7
- instanceof SqlSemantics\Platform\MySql\Rules\Query\CommonTables::operands() packages/sql-semantics-mysql/src/Rules/Query/CommonTables.php:158
- instanceof SqlSemantics\Platform\MySql\Rules\Routine\ProgramRestrictions::into() packages/sql-semantics-mysql/src/Rules/Routine/ProgramRestrictions.php:154
- instanceof SqlSemantics\Platform\MySql\Rules\TableDefinition\EnclosedQuery::accepts() packages/sql-semantics-mysql/src/Rules/TableDefinition/EnclosedQuery.php:44
- instanceof SqlSemantics\Platform\MySql\Rules\TableDefinition\EnclosedQuery::write() packages/sql-semantics-mysql/src/Rules/TableDefinition/EnclosedQuery.php:63
- instanceof SqlSemantics\Platform\MySql\Rules\TableDefinition\ViewColumns::block() packages/sql-semantics-mysql/src/Rules/TableDefinition/ViewColumns.php:135
- instanceof SqlSemantics\Platform\MySql\Statement\Query\Set\LeadingUnion::__construct() packages/sql-semantics-mysql/src/Statement/Query/Set/LeadingUnion.php:61
- instanceof SqlSemantics\Platform\MySql\Statement\Query\Set\OrderedSetOperation::__construct() packages/sql-semantics-mysql/src/Statement/Query/Set/OrderedSetOperation.php:83