packages/sql-semantics-mysql/src/Statement/Query/Set/SetOperation.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Query\Set;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
11use SqlSemantics\Platform\MySql\Model\Statement\Fact\QueryFact;
12use SqlSemantics\Platform\MySql\Model\Statement\Query;
13use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
14use SqlSemantics\Platform\MySql\Model\Statement\Statement;
15use SqlSemantics\Platform\MySql\Rules\Query\QueryTails;
16use SqlSemantics\Platform\MySql\Rules\Query\SetFacts;
17use SqlSemantics\Platform\MySql\Statement\Query\QueryExpression;
18use SqlSemantics\Platform\MySql\Statement\Query\QueryStatement;
19use SqlSemantics\Platform\MySql\Statement\Query\Select;
20
21/**
22 * A set operation: UNION, EXCEPT or INTERSECT of two queries.
23 *
24 * Operations of equal binding associate to the left, so a chain is a
25 * left-deep tree; an operand that would need parentheses to keep its place
26 * is refused: a set operation on the right that binds no more tightly, a
27 * looser one on the left of INTERSECT, a query with a WITH clause or with
28 * statement-level INTO or locking clauses, and on the right a query whose
29 * ORDER BY, LIMIT or other trailing clauses would be read as applying to
30 * the whole operation.
31 *
32 * Rule: MYSQL-SET-OPERATION-001. The output is derived by
33 * MYSQL-SET-FACTS-001. Source: https://dev.mysql.com/doc/refman/8.4/en/set-operations.html,
34 * https://dev.mysql.com/doc/refman/8.4/en/union.html. Status: Implemented.
35 *
36 * @visibility public
37 * @example Reading the operands of a union
38 * $query = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SELECT a FROM t UNION SELECT b FROM u');
39 * [$query->statement->left->from->name->name->value, $query->statement->right->from->name->name->value] // => ['t', 'u']
40 * @example Refusing a right operand that would associate differently without parentheses
41 * $one = new \SqlSemantics\Platform\MySql\Statement\Query\Select([], [new \SqlSemantics\Platform\MySql\Statement\Query\SelectExpression(new \SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral('1'))]);
42 * $two = new \SqlSemantics\Platform\MySql\Statement\Query\Select([], [new \SqlSemantics\Platform\MySql\Statement\Query\SelectExpression(new \SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral('2'))]);
43 * $three = new \SqlSemantics\Platform\MySql\Statement\Query\Select([], [new \SqlSemantics\Platform\MySql\Statement\Query\SelectExpression(new \SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral('3'))]);
44 * 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\InvalidConstruction
45 */
46final class SetOperation implements Statement, Query
47{
48 use Snapshot;
49
50 /**
51 * @param Query|LeadingUnion $left 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
52 * @param SetOperator $operator The operator
53 * @param SetQuantifier|null $quantifier The written DISTINCT or ALL
54 * @param Query $right The right operand
55 */
56 public function __construct(public readonly Query|LeadingUnion $left, public readonly SetOperator $operator, public readonly ?SetQuantifier $quantifier, public readonly Query $right)
57 {
58 Check::input(!$right instanceof self || $right->operator->tighter($operator), 'A set operation on the right of another is written in parentheses unless it binds more tightly.');
59 Check::input(!$left instanceof self || !$operator->tighter($left->operator), 'A looser set operation on the left of INTERSECT is written in parentheses.');
60 Check::input(!$left instanceof LeadingUnion || $operator === SetOperator::Union, 'A leading union continues with UNION.');
61 Check::input(!$left instanceof QueryStatement && !$right instanceof QueryStatement, 'A query with INTO or locking clauses is written in parentheses as a set operand.');
62 Check::input(!$left instanceof OrderedSetOperation && !$right instanceof OrderedSetOperation, 'A set operation ordered after its last SELECT ends its subquery.');
63 Check::input(!$right instanceof QueryExpression && !($left instanceof QueryExpression && $left->with !== null), 'A query with a WITH clause or with ordering of its own is written in parentheses as a set operand.');
64 Check::input(!$right instanceof Select || !$right->trailed(), 'The last operand of a set operation has no ORDER BY, LIMIT, INTO or locking clause of its own unless it is written in parentheses.');
65 }
66
67 /**
68 * Derives the set operation as a statement root and records its rows as the output.
69 */
70 public function deriveStatement(Derivation $derivation): void
71 {
72 $derivation->output($derivation->query($this, $derivation->environment()));
73 }
74
75 /**
76 * Derives both operands and the output columns.
77 */
78 public function deriveQuery(Derivation $derivation, Environment $outer): QueryFact
79 {
80 return (new SetFacts())->derive($this, $derivation, $outer);
81 }
82
83 /**
84 * Writes the operands around the operator.
85 */
86 public function render(Output $out): void
87 {
88 $out->node($this->left);
89 (new QueryTails())->operation($this, $out);
90 }
91}
92