packages/sql-semantics-sqlite/tests/Unit/Statement/Query/Ordering/OutputOrdinalTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query\Ordering;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\AliasTarget;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Dependent;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Invalid;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\Sqlite\Semantics;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\Collate;
17use SqlSemantics\Platform\Sqlite\Statement\Expression\Grouped;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\HexLiteral;
19use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
20use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TextLiteral;
21use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\Unary;
22use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\UnaryOperator;
23use SqlSemantics\Platform\Sqlite\Statement\Query\Compound;
24use SqlSemantics\Platform\Sqlite\Statement\Query\Ordering\OutputOrdinal;
25use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\OrdinalOutOfRange;
26use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
27
28#[CoversClass(OutputOrdinal::class)]
29#[Medium]
30final class OutputOrdinalTest extends TestCase
31{
32    public function testPositionCountsFromOne(): void
33    {
34        self::assertSame(1, (new OutputOrdinal(new IntegerLiteral('1')))->position());
35        self::assertSame(12, (new OutputOrdinal(new IntegerLiteral('0012')))->position());
36        self::assertSame(-2, (new OutputOrdinal(new Unary(UnaryOperator::Minus, new IntegerLiteral('2'))))->position());
37        self::assertSame(3, (new OutputOrdinal(new Collate(new Grouped(new HexLiteral('3')), new Name('nocase'))))->position());
38    }
39
40    public function testDeriveScalarDenotesTheResultColumnAtThePosition(): void
41    {
42        $query = (new Semantics())->inspect("SELECT 'x' AS a, 2 AS b ORDER BY 2, +1");
43
44        self::assertInstanceOf(Select::class, $query->statement);
45        $second = $query->statement->orderBy[0]->expression;
46        $first = $query->statement->orderBy[1]->expression;
47        self::assertInstanceOf(OutputOrdinal::class, $second);
48        self::assertInstanceOf(OutputOrdinal::class, $first);
49        $fact = $query->facts->scalar($second);
50        self::assertInstanceOf(AliasTarget::class, $fact->resolution);
51        self::assertSame($query->field('b'), $fact->resolution->field);
52        self::assertSame($query->field('b')->type, $fact->type);
53        self::assertSame(Nullability::NotNull, $fact->nullability);
54        $resolution = $query->facts->scalar($first)->resolution;
55        self::assertInstanceOf(AliasTarget::class, $resolution);
56        self::assertSame(0, $resolution->field->position);
57        self::assertSame([], $query->facts->diagnostics);
58    }
59
60    public function testDeriveScalarAlsoRecordsTheConstant(): void
61    {
62        $query = (new Semantics())->inspect('SELECT 1 GROUP BY 1');
63
64        self::assertInstanceOf(Select::class, $query->statement);
65        $ordinal = $query->statement->groupBy[0];
66        self::assertInstanceOf(OutputOrdinal::class, $ordinal);
67        self::assertTrue($query->facts->covers($ordinal->constant));
68        self::assertSame(Nullability::NotNull, $query->facts->scalar($ordinal->constant)->nullability);
69    }
70
71    public function testDeriveScalarReportsAPositionOutsideTheResultColumns(): void
72    {
73        $semantics = new Semantics();
74        $beyond = $semantics->inspect('SELECT 1 ORDER BY 2');
75        $negative = $semantics->inspect('SELECT 1 AS a, 2 AS b ORDER BY -1');
76
77        self::assertInstanceOf(Select::class, $beyond->statement);
78        $fact = $beyond->facts->scalar($beyond->statement->orderBy[0]->expression);
79        self::assertInstanceOf(Invalid::class, $fact->type);
80        self::assertInstanceOf(OrdinalOutOfRange::class, $fact->resolution);
81        self::assertSame(2, $fact->resolution->ordinal);
82        self::assertSame(1, $fact->resolution->columns);
83        self::assertSame(Nullability::Dependent, $fact->nullability);
84        self::assertSame('Term 2 is out of range - should be between 1 and 1.', $beyond->facts->diagnostics[0]->message());
85        self::assertSame('Term -1 is out of range - should be between 1 and 2.', $negative->facts->diagnostics[0]->message());
86    }
87
88    public function testDeriveScalarDependsOnTheMissingInputsBeyondTheKnownColumns(): void
89    {
90        $query = (new Semantics())->inspect('SELECT a, * FROM t ORDER BY 3');
91
92        self::assertInstanceOf(Select::class, $query->statement);
93        $fact = $query->facts->scalar($query->statement->orderBy[0]->expression);
94        self::assertInstanceOf(Dependent::class, $fact->type);
95        self::assertSame('the declaration of relation t', $fact->type->missing[0]->describe());
96        self::assertSame(Nullability::Dependent, $fact->nullability);
97        self::assertNull($fact->resolution);
98        self::assertSame([], $query->facts->diagnostics);
99    }
100
101    public function testDeriveScalarCountsTheColumnsOfACompound(): void
102    {
103        $query = (new Semantics())->inspect('SELECT 1 AS a, 2 AS b UNION SELECT 3, 4 ORDER BY 2');
104
105        self::assertInstanceOf(Compound::class, $query->statement);
106        $fact = $query->facts->scalar($query->statement->orderBy[0]->expression);
107        self::assertInstanceOf(AliasTarget::class, $fact->resolution);
108        self::assertSame('b', $fact->resolution->field->name?->value);
109    }
110
111    public function testRenderWritesTheConstantAsWritten(): void
112    {
113        $query = (new Semantics())->inspect('select 1 as a, 2 as b order by (1) collate nocase, 0x2, +1');
114
115        self::assertSame('SELECT 1 AS a, 2 AS b ORDER BY (1) COLLATE nocase, 0x2, + 1', $query->toString());
116    }
117
118    public function testRenderRefusesAnExpressionThatIsNoIntegerConstant(): void
119    {
120        $this->expectExceptionMessage('A result column position is written as an integer constant.');
121
122        new OutputOrdinal(new TextLiteral('1'));
123    }
124
125    public function testRenderRefusesAnIntegerBeyondThirtyTwoBits(): void
126    {
127        $this->expectExceptionMessage('A result column position is written as an integer constant.');
128
129        new OutputOrdinal(new IntegerLiteral('2147483648'));
130    }
131}
132