packages/sql-semantics-postgres/tests/Unit/Rules/Resolution/LateralReachTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Resolution;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Resolution\LateralReach::class)]
12#[Medium]
13final class LateralReachTest extends TestCase
14{
15 public function testBarMarksEveryRelationOnce(): void
16 {
17 $reach = new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\LateralReach();
18 $barred = $reach->bar($reach->bar([new \SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([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)]), null, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), [], [new \SqlSemantics\Platform\PostgreSql\Model\Resolution\ImplicitSlot([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid')], new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Tid), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull))])]));
19 self::assertSame([-2], $barred[0]->hidden);
20 }
21
22 public function testBarredTellsABarredRelation(): void
23 {
24 $reach = new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\LateralReach();
25 $relation = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([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)]), null, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), [], [new \SqlSemantics\Platform\PostgreSql\Model\Resolution\ImplicitSlot([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid')], new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Tid), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull))]);
26 self::assertSame([false, true], [$reach->barred($relation), $reach->barred($reach->bar([$relation])[0])]);
27 }
28
29 public function testReachedFindsTheBarredRelationOfAQualifier(): void
30 {
31 $reach = new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\LateralReach();
32 $relation = $reach->bar([new \SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([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)]), null, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), [], [new \SqlSemantics\Platform\PostgreSql\Model\Resolution\ImplicitSlot([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid')], new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Tid), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull))])])[0];
33 $environment = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment((new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true), null, [$relation]);
34 $missing = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Column\MissingColumn(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('zz'));
35 self::assertSame($relation, $reach->reached($environment, $missing, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t'))));
36 }
37
38 public function testReachedFindsTheBarredRelationOfAResolvedColumn(): void
39 {
40 $reach = new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\LateralReach();
41 $relation = $reach->bar([new \SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([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)]), null, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), [], [new \SqlSemantics\Platform\PostgreSql\Model\Resolution\ImplicitSlot([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid')], new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Tid), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull))])])[0];
42 $context = (new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true);
43 $environment = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment($context, new \SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment($context, null, [$relation]));
44 $resolved = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Column\ResolvedColumn($relation->relation, $relation->shape->slots[0], 1);
45 self::assertSame($relation, $reach->reached($environment, $resolved, null));
46 }
47
48 public function testReportNamesAnUnaliasedJoin(): void
49 {
50 $query = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('SELECT * FROM (SELECT 1 AS a) AS t JOIN (SELECT 2 AS b) AS u ON true FULL JOIN LATERAL (SELECT b AS x) AS s ON true', []);
51 self::assertSame(['invalid reference to FROM-clause entry for table "unnamed_join"'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), $query->facts->diagnostics));
52 }
53
54 public function testReportNamesTheAliasOfAWholeRow(): void
55 {
56 $query = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('SELECT * FROM (SELECT 1 AS a) AS t RIGHT JOIN LATERAL (SELECT t AS x) AS s ON true', []);
57 self::assertSame('invalid reference to FROM-clause entry for table "t"', $query->facts->diagnostics[0]->message());
58 }
59
60 public function testReportIsNotRaisedForALeftJoin(): void
61 {
62 $query = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('SELECT * FROM (SELECT 1 AS a) AS t LEFT JOIN LATERAL (SELECT t.a AS x) AS s ON true', []);
63 self::assertSame([], $query->facts->diagnostics);
64 }
65}
66