packages/sql-semantics-mysql/src/Rules/Query/SetFacts.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Rules\Query;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Contract\GrammarRelease;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
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\Statement\Query\Set\LeadingUnion;
14use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation;
15use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperator;
16
17/**
18 * Derives the facts of a set operation.
19 *
20 * Rule: MYSQL-SET-FACTS-001. Both operands are derived in the enclosing
21 * environment, each its own query block; the output columns combine them by
22 * MYSQL-RESULT-SLOTS-001. In the query of a recursive common table
23 * expression (MYSQL-WITH-001) a right operand that refers to the table sees
24 * it with the columns of the left operand, all nullable, and those columns
25 * are the output, whose types the recursive part does not change. The
26 * operands of a leading union (MYSQL-LEADING-UNION-001) are derived the
27 * same way and combined for the operation that continues it; the form
28 * needs MySQL 5.6 or 5.7, and the own LIMIT of its last SELECT needs 5.6.
29 * Terminates: both operands are strict parts. Source:
30 * https://dev.mysql.com/doc/refman/8.4/en/set-operations.html,
31 * https://dev.mysql.com/doc/refman/8.4/en/with.html.
32 * Status: Implemented.
33 *
34 * @visibility SqlSemantics\Platform\MySql
35 */
36final class SetFacts
37{
38 /**
39 * Derives both operands and answers the output.
40 */
41 public function derive(SetOperation $operation, Derivation $derivation, Environment $outer): QueryFact
42 {
43 return $this->operands($operation->left, $operation->operator, $operation->right, $derivation, $outer);
44 }
45
46 /**
47 * Derives the two operands of a set operator and answers the output.
48 */
49 public function operands(Query|LeadingUnion $leftQuery, SetOperator $operator, Query $rightQuery, Derivation $derivation, Environment $outer): QueryFact
50 {
51 $left = $leftQuery instanceof LeadingUnion ? $this->leading($leftQuery, $derivation, $outer) : $derivation->query($leftQuery, $outer);
52 $tables = new CommonTables();
53 $pending = $tables->pending($outer, $rightQuery, $derivation);
54 $right = $derivation->query($rightQuery, $pending === null ? $outer : $tables->anchored($outer, $pending, $left, $derivation));
55 $fact = new QueryFact((new ResultSlots())->combine($left, $right, $operator, $derivation), $derivation->context->columnNames);
56
57 return $pending === null ? $fact : $tables->nullable($left);
58 }
59
60 /**
61 * Derives the operands of a leading union and answers the rows they combine to.
62 *
63 * @throws \SqlSemantics\Platform\MySql\Model\Diagnostic\InvalidConstruction When the release does not read the form
64 */
65 public function leading(LeadingUnion $leading, Derivation $derivation, Environment $outer): QueryFact
66 {
67 $release = $derivation->context->profile->grammar;
68 Check::input(in_array($release, [GrammarRelease::MySql5651, GrammarRelease::MySql5744], true), 'A SELECT that keeps its own clauses before a later UNION needs MySQL 5.6 or 5.7.');
69 Check::input($leading->right->limit === null || $release === GrammarRelease::MySql5651, 'A SELECT that keeps its own LIMIT before a later UNION needs MySQL 5.6.');
70
71 return $this->operands($leading->left, SetOperator::Union, $leading->right, $derivation, $outer);
72 }
73}
74