packages/sql-semantics-mysql/src/Statement/Query/Clause/OutputOrdinal.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Query\Clause;
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\ScalarFact;
12use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\AliasTarget;
13use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
14use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Invalid;
17use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\MySql\Statement\Literal\NumberForm;
19use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
20use SqlSemantics\Platform\MySql\Statement\Query\Problem\OrdinalOutOfRange;
21
22/**
23 * A position in the select list written as an integer in ORDER BY or GROUP BY.
24 *
25 * Rule: MYSQL-ORDINAL-001. An unsigned integer literal written as an ORDER
26 * BY or GROUP BY item denotes the select list item at that position,
27 * counting from one. The query derives the ordinal in an environment whose
28 * aliases are the output fields in order; when the select list holds a star
29 * whose columns are not all known, the environment holds the relations of
30 * that star and the position depends on their declarations. A position
31 * outside the select list is reported. Source:
32 * https://dev.mysql.com/doc/refman/8.4/en/select.html ("Columns selected for
33 * output can be referred to in ORDER BY and GROUP BY clauses using ... column
34 * positions"). Status: Implemented.
35 *
36 * @visibility public
37 * @example Reading the position of an ordering item
38 * $query = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SELECT a, b FROM t ORDER BY 2');
39 * $query->statement->orderBy[0]->expression->position() // => 2
40 * @example Refusing a decimal as a position
41 * new \SqlSemantics\Platform\MySql\Statement\Query\Clause\OutputOrdinal(new \SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral('1.0')) // throws \SqlSemantics\Platform\MySql\Model\Diagnostic\InvalidConstruction
42 */
43final class OutputOrdinal implements Scalar
44{
45 use Snapshot;
46
47 /**
48 * @param NumberLiteral $literal The integer as written
49 */
50 public function __construct(public readonly NumberLiteral $literal)
51 {
52 Check::input($literal->form === NumberForm::Integer, 'A select list position is an unsigned integer.');
53 }
54
55 /**
56 * Answers the position, counting from one; a position beyond the integer range of PHP answers the largest integer.
57 */
58 public function position(): int
59 {
60 $digits = ltrim($this->literal->text, '0');
61
62 return strlen($digits) > 18 ? PHP_INT_MAX : (int) $digits;
63 }
64
65 /**
66 * Derives the integer and takes the facts of the output field at the position.
67 */
68 public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact
69 {
70 $derivation->scalar($this->literal, $environment);
71 $field = $environment->aliases[$this->position() - 1] ?? null;
72 if ($field !== null) {
73 return new ScalarFact($field->type, $field->nullability, new AliasTarget($field));
74 }
75 $missing = [];
76 foreach ($environment->relations as $relation) {
77 array_push($missing, ...$relation->shape->missing);
78 }
79 if ($missing !== []) {
80 return new ScalarFact(new Dependent($missing), Nullability::Dependent);
81 }
82 $problem = new OrdinalOutOfRange($this->position(), count($environment->aliases));
83 $derivation->report($problem);
84
85 return new ScalarFact(new Invalid($problem), Nullability::Dependent);
86 }
87
88 /**
89 * Writes the integer.
90 */
91 public function render(Output $out): void
92 {
93 $out->node($this->literal);
94 }
95}
96