packages/sql-semantics-postgres/tests/Unit/Rules/Query/OrderingTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Query\Ordering::class)]
12#[Medium]
13final class OrderingTest extends TestCase
14{
15 public function testSortPrefersTheOutputName(): void
16 {
17 $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
18 $t = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text)]);
19 $u = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('u')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int8, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('c'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Bool, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]);
20 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
21 $query = $semantics->inspect('SELECT b AS a FROM t ORDER BY a', [$t, $u]);
22 $select = $query->statement;
23 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Select::class, $select);
24 $term = $select->options?->order[0]->expression;
25 self::assertNotNull($term);
26 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Column\AliasTarget::class, $query->facts->scalar($term)->resolution);
27 }
28
29 public function testGroupPrefersTheInputColumn(): void
30 {
31 $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
32 $t = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text)]);
33 $u = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('u')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int8, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('c'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Bool, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]);
34 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
35 $query = $semantics->inspect('SELECT b AS a FROM t GROUP BY a', [$t, $u]);
36 $select = $query->statement;
37 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Select::class, $select);
38 $item = $select->groupBy[0];
39 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Scalar::class, $item);
40 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Column\ResolvedColumn::class, $query->facts->scalar($item)->resolution);
41 }
42
43 public function testOutputsReportsAnExpressionAfterASetOperation(): void
44 {
45 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
46 $query = $semantics->inspect('SELECT 1 AS a UNION SELECT 2 ORDER BY a + 1');
47 self::assertSame('invalid UNION/INTERSECT/EXCEPT ORDER BY clause', $query->facts->diagnostics[0]->message());
48 }
49
50 public function testTermReportsANonIntegerConstant(): void
51 {
52 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
53 $query = $semantics->inspect("SELECT 1 ORDER BY 'x'");
54 self::assertSame('non-integer constant in ORDER BY', $query->facts->diagnostics[0]->message());
55 }
56
57 public function testPositionsListsTheKnownFields(): void
58 {
59 $context = (new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true);
60 $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($context);
61 $projection = [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\Field(0, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull))];
62 self::assertCount(1, (new \SqlSemantics\Platform\PostgreSql\Rules\Query\Ordering())->positions($projection, $derivation->environment())->aliases);
63 }
64
65 public function testKnownStopsAtAnOpenStar(): void
66 {
67 $projection = [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\Field(0, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OpenStar([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRelation(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('v')))]), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\Field(2, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull))];
68 self::assertCount(1, (new \SqlSemantics\Platform\PostgreSql\Rules\Query\Ordering())->known($projection));
69 }
70
71 public function testNamedKeepsOneOfEqualExpressions(): void
72 {
73 $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
74 $t = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text)]);
75 $u = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('u')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int8, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('c'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Bool, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]);
76 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
77 $query = $semantics->inspect('SELECT a, a FROM t ORDER BY a', [$t, $u]);
78 self::assertSame([], $query->facts->diagnostics);
79 }
80
81 public function testSameComparesTheExpressions(): void
82 {
83 $first = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\Field(0, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('1')));
84 $second = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\Field(1, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('1')));
85 self::assertTrue((new \SqlSemantics\Platform\PostgreSql\Rules\Query\Ordering())->same($first, $second));
86 }
87
88 public function testBareFindsANameInParentheses(): void
89 {
90 self::assertSame('a', (new \SqlSemantics\Platform\PostgreSql\Rules\Query\Ordering())->bare(new \SqlSemantics\Platform\PostgreSql\Statement\Expression\Grouped(new \SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnReference([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a')])))?->value);
91 }
92
93 public function testGroupReadsTheMembersOfAGroupingRowAsGroupingTerms(): void
94 {
95 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
96 $query = $semantics->inspect('SELECT 1 AS x, 2 AS y GROUP BY ROLLUP ((x, 2))', []);
97 $select = $query->statement;
98 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Select::class, $select);
99 $set = $select->groupBy[0];
100 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Grouping\GroupingSet::class, $set);
101 $row = $set->members[0];
102 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Grouping\GroupingRow::class, $row);
103 $name = $row->members[0];
104 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Scalar::class, $name);
105 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Column\AliasTarget::class, $query->facts->scalar($name)->resolution);
106 self::assertSame([], $query->facts->diagnostics);
107 }
108
109 public function testGroupReportsAPositionOutOfRangeInsideARow(): void
110 {
111 $query = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('SELECT 1 AS x GROUP BY (x, 3)', []);
112 self::assertSame('GROUP BY position 3 is not in select list', $query->facts->diagnostics[0]->message());
113 }
114}
115