packages/sql-semantics-sqlite/tests/Unit/Model/Resolution/LookupLevelTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Resolution;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Contract\AnalysisContext;
11use SqlSemantics\Platform\Sqlite\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\Sqlite\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\Sqlite\Model\Resolution\Environment;
14use SqlSemantics\Platform\Sqlite\Model\Resolution\ImplicitSlot;
15use SqlSemantics\Platform\Sqlite\Model\Resolution\LookupLevel;
16use SqlSemantics\Platform\Sqlite\Model\Resolution\VisibleRelation;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Comparison;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
19use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
20use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\SessionState;
21use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\UndeclaredRelation;
22use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\OutputSlot;
23use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\RowShape;
24use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
25use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
26use SqlSemantics\Platform\Sqlite\Statement\Relation\TableInput;
27use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
28
29#[CoversClass(LookupLevel::class)]
30#[Small]
31final class LookupLevelTest extends TestCase
32{
33 public function testAdmitsEveryOccurrenceWithoutAQualifier(): void
34 {
35 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
36 $scope = new Environment($context);
37 $relation = new VisibleRelation(new TableInput(new QualifiedName(new Name('t'))), new RowShape([]));
38
39 self::assertTrue((new LookupLevel($scope, new Name('a'), null, 0))->admits($scope, $relation, null));
40 }
41
42 public function testAdmitsAnAliasedOccurrenceByItsAliasOnly(): void
43 {
44 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')], [], true, Comparison::AsciiInsensitive);
45 $scope = new Environment($context);
46 $relation = new VisibleRelation(new TableInput(new QualifiedName(new Name('t')), new Name('x')), new RowShape([]), new Name('x'), new QualifiedName(new Name('t')));
47 $level = new LookupLevel($scope, new Name('a'), null, 0);
48
49 self::assertTrue($level->admits($scope, $relation, new QualifiedName(new Name('X'))));
50 self::assertFalse($level->admits($scope, $relation, new QualifiedName(new Name('t'))));
51 self::assertFalse($level->admits($scope, $relation, new QualifiedName(new Name('x'), new Name('main'))));
52 }
53
54 public function testAdmitsAPlainOccurrenceByItsNameAndSchema(): void
55 {
56 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
57 $scope = new Environment($context);
58 $unqualified = new VisibleRelation(new TableInput(new QualifiedName(new Name('t'))), new RowShape([]), null, new QualifiedName(new Name('t')));
59 $qualified = new VisibleRelation(new TableInput(new QualifiedName(new Name('t'), new Name('aux'))), new RowShape([]), null, new QualifiedName(new Name('t'), new Name('aux')));
60 $nameless = new VisibleRelation(new TableInput(new QualifiedName(new Name('t'))), new RowShape([]));
61 $level = new LookupLevel($scope, new Name('a'), null, 0);
62
63 self::assertTrue($level->admits($scope, $unqualified, new QualifiedName(new Name('t'))));
64 self::assertTrue($level->admits($scope, $unqualified, new QualifiedName(new Name('t'), new Name('main'))));
65 self::assertTrue($level->admits($scope, $qualified, new QualifiedName(new Name('t'), new Name('aux'))));
66 self::assertFalse($level->admits($scope, $qualified, new QualifiedName(new Name('t'), new Name('main'))));
67 self::assertFalse($level->admits($scope, $unqualified, new QualifiedName(new Name('u'))));
68 self::assertFalse($level->admits($scope, $nameless, new QualifiedName(new Name('t'))));
69 }
70
71 public function testFoundListsTheMatchingSlotsOfEveryAdmittedOccurrenceAtTheGivenDepth(): void
72 {
73 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')], [], true, Comparison::Sensitive, Comparison::AsciiInsensitive);
74 $first = new TableInput(new QualifiedName(new Name('t')));
75 $firstSlot = new OutputSlot(new Name('A'), new Known(Storage::Integer), Nullability::NotNull);
76 $second = new TableInput(new QualifiedName(new Name('u')));
77 $secondSlot = new OutputSlot(new Name('a'), new Known(Storage::Text), Nullability::Nullable);
78 $scope = new Environment($context, null, [
79 new VisibleRelation($first, new RowShape([$firstSlot, new OutputSlot(new Name('b'), new Known(Storage::Text), Nullability::Nullable)])),
80 new VisibleRelation($second, new RowShape([$secondSlot])),
81 ]);
82
83 $found = (new LookupLevel($scope, new Name('a'), null, 2))->found();
84
85 self::assertCount(2, $found);
86 self::assertSame($firstSlot, $found[0]->slot);
87 self::assertSame($first, $found[0]->relation);
88 self::assertSame($secondSlot, $found[1]->slot);
89 self::assertSame(2, $found[1]->depth);
90 }
91
92 public function testFoundSearchesImplicitSlotsOnlyWhenNoDeclaredSlotMatches(): void
93 {
94 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
95 $declared = new OutputSlot(new Name('rowid'), new Known(Storage::Text), Nullability::Nullable);
96 $implicit = new OutputSlot(new Name('rowid'), new Known(Storage::Integer), Nullability::NotNull);
97 $relation = new VisibleRelation(new TableInput(new QualifiedName(new Name('t'))), new RowShape([$declared]), null, null, [], [new ImplicitSlot([new Name('rowid'), new Name('oid')], $implicit)]);
98 $scope = new Environment($context, null, [$relation]);
99
100 self::assertSame($declared, (new LookupLevel($scope, new Name('rowid'), null, 0))->found()[0]->slot);
101 self::assertSame($implicit, (new LookupLevel($scope, new Name('oid'), null, 0))->found()[0]->slot);
102 }
103
104 public function testOpenListsTheIncompleteOccurrencesWithoutAMatch(): void
105 {
106 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
107 $open = new VisibleRelation(new TableInput(new QualifiedName(new Name('t'))), new RowShape([], [new UndeclaredRelation(new QualifiedName(new Name('t')))]));
108 $matching = new VisibleRelation(new TableInput(new QualifiedName(new Name('u'))), new RowShape([new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull)], [new UndeclaredRelation(new QualifiedName(new Name('u')))]));
109 $scope = new Environment($context, null, [$open, $matching]);
110
111 $level = new LookupLevel($scope, new Name('a'), null, 0);
112
113 self::assertSame([$open], $level->open());
114 self::assertCount(1, $level->found());
115 self::assertSame([], (new LookupLevel($scope, new Name('a'), new QualifiedName(new Name('u')), 0))->open());
116 }
117
118 public function testOpenListsAnOccurrenceWithASlotWhoseNameIsUndecided(): void
119 {
120 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
121 $missing = new SessionState('the client character set');
122 $relation = new VisibleRelation(new TableInput(new QualifiedName(new Name('d'))), new RowShape([new OutputSlot(null, new Known(Storage::Text), Nullability::NotNull, null, null, [$missing])]));
123
124 $level = new LookupLevel(new Environment($context, null, [$relation]), new Name('x'), null, 0);
125
126 self::assertSame([$relation], $level->open());
127 }
128
129 public function testUndecidedCollectsTheMissingInputsOfTheShapeAndOfUnnamedSlots(): void
130 {
131 $shapeMissing = new UndeclaredRelation(new QualifiedName(new Name('t')));
132 $nameMissing = new SessionState('the client character set');
133 $relation = new VisibleRelation(new TableInput(new QualifiedName(new Name('t'))), new RowShape([new OutputSlot(null, new Known(Storage::Text), Nullability::NotNull, null, null, [$nameMissing])], [$shapeMissing]));
134
135 self::assertSame([$shapeMissing, $nameMissing], LookupLevel::undecided($relation));
136 }
137}
138