packages/sql-semantics-sqlite/tests/Unit/Model/Resolution/ColumnLookupTest.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\ColumnLookup;
14use SqlSemantics\Platform\Sqlite\Model\Resolution\Environment;
15use SqlSemantics\Platform\Sqlite\Model\Resolution\ImplicitSlot;
16use SqlSemantics\Platform\Sqlite\Model\Resolution\VisibleRelation;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
19use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\AmbiguousColumn;
20use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ConditionalColumn;
21use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
22use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
23use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\UndeclaredRelation;
24use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\OutputSlot;
25use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\RowShape;
26use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
27use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
28use SqlSemantics\Platform\Sqlite\Statement\Relation\TableInput;
29use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
30
31#[CoversClass(ColumnLookup::class)]
32#[Small]
33final class ColumnLookupTest extends TestCase
34{
35 public function testFindResolvesTheOnlySlotWithTheName(): void
36 {
37 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
38 $relation = new TableInput(new QualifiedName(new Name('t')));
39 $slot = new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull);
40 $environment = new Environment($context, null, [new VisibleRelation($relation, new RowShape([$slot]))]);
41
42 $resolution = (new ColumnLookup())->find($environment, new Name('a'));
43
44 self::assertInstanceOf(ResolvedColumn::class, $resolution);
45 self::assertSame($relation, $resolution->relation);
46 self::assertSame($slot, $resolution->slot);
47 self::assertSame(0, $resolution->depth);
48 }
49
50 public function testFindIsAmbiguousForSeveralSlotsAtOnePosition(): void
51 {
52 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
53 $first = new VisibleRelation(new TableInput(new QualifiedName(new Name('t'))), new RowShape([new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull)]));
54 $second = new VisibleRelation(new TableInput(new QualifiedName(new Name('u'))), new RowShape([new OutputSlot(new Name('a'), new Known(Storage::Text), Nullability::Nullable)]));
55 $environment = new Environment($context, null, [$first, $second]);
56
57 $resolution = (new ColumnLookup())->find($environment, new Name('a'));
58
59 self::assertInstanceOf(AmbiguousColumn::class, $resolution);
60 self::assertSame([$first->relation, $second->relation], [$resolution->candidates[0]->relation, $resolution->candidates[1]->relation]);
61 }
62
63 public function testFindContinuesInTheEnclosingPositionWithAGreaterDepth(): void
64 {
65 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
66 $outerRelation = new TableInput(new QualifiedName(new Name('t')));
67 $outer = new Environment($context, null, [new VisibleRelation($outerRelation, new RowShape([new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull)]))]);
68 $inner = new Environment($context, $outer, [new VisibleRelation(new TableInput(new QualifiedName(new Name('u'))), new RowShape([new OutputSlot(new Name('b'), new Known(Storage::Integer), Nullability::NotNull)]))]);
69
70 $resolution = (new ColumnLookup())->find($inner, new Name('a'));
71
72 self::assertInstanceOf(ResolvedColumn::class, $resolution);
73 self::assertSame($outerRelation, $resolution->relation);
74 self::assertSame(1, $resolution->depth);
75 }
76
77 public function testFindPrefersTheNearerPositionOverAFartherSlot(): void
78 {
79 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
80 $outer = new Environment($context, null, [new VisibleRelation(new TableInput(new QualifiedName(new Name('t'))), new RowShape([new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull)]))]);
81 $innerRelation = new TableInput(new QualifiedName(new Name('u')));
82 $inner = new Environment($context, $outer, [new VisibleRelation($innerRelation, new RowShape([new OutputSlot(new Name('a'), new Known(Storage::Text), Nullability::Nullable)]))]);
83
84 $resolution = (new ColumnLookup())->find($inner, new Name('a'));
85
86 self::assertInstanceOf(ResolvedColumn::class, $resolution);
87 self::assertSame($innerRelation, $resolution->relation);
88 self::assertSame(0, $resolution->depth);
89 }
90
91 public function testFindIsMissingOnlyWhenEverySearchedOccurrenceIsComplete(): void
92 {
93 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
94 $environment = new Environment($context, null, [new VisibleRelation(new TableInput(new QualifiedName(new Name('t'))), new RowShape([new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull)]))]);
95
96 $resolution = (new ColumnLookup())->find($environment, new Name('b'), new QualifiedName(new Name('t')));
97
98 self::assertInstanceOf(MissingColumn::class, $resolution);
99 self::assertSame('Column t.b does not exist.', $resolution->message());
100 }
101
102 public function testFindIsConditionalWhileANearerOccurrenceIsIncompletelyKnown(): void
103 {
104 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
105 $outerRelation = new TableInput(new QualifiedName(new Name('t')));
106 $outerSlot = new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull);
107 $outer = new Environment($context, null, [new VisibleRelation($outerRelation, new RowShape([$outerSlot]))]);
108 $innerRelation = new TableInput(new QualifiedName(new Name('u')));
109 $missing = new UndeclaredRelation(new QualifiedName(new Name('u')));
110 $inner = new Environment($context, $outer, [new VisibleRelation($innerRelation, new RowShape([], [$missing]))]);
111
112 $resolution = (new ColumnLookup())->find($inner, new Name('a'));
113
114 self::assertInstanceOf(ConditionalColumn::class, $resolution);
115 self::assertCount(1, $resolution->candidates);
116 self::assertSame($outerSlot, $resolution->candidates[0]->slot);
117 self::assertSame(1, $resolution->candidates[0]->depth);
118 self::assertSame([$innerRelation], $resolution->relations);
119 self::assertSame([$missing], $resolution->missing);
120 }
121
122 public function testFindIsConditionalWithoutCandidatesWhenNothingIsKnown(): void
123 {
124 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
125 $missing = new UndeclaredRelation(new QualifiedName(new Name('t')));
126 $environment = new Environment($context, null, [new VisibleRelation(new TableInput(new QualifiedName(new Name('t'))), new RowShape([], [$missing]))]);
127
128 $resolution = (new ColumnLookup())->find($environment, new Name('a'));
129
130 self::assertInstanceOf(ConditionalColumn::class, $resolution);
131 self::assertSame([], $resolution->candidates);
132 self::assertSame([$missing], $resolution->missing);
133 }
134
135 public function testFindAdmitsOnlyTheOccurrenceTheQualifierNames(): void
136 {
137 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
138 $aliased = new TableInput(new QualifiedName(new Name('t')), new Name('x'));
139 $plain = new TableInput(new QualifiedName(new Name('u')));
140 $environment = new Environment($context, null, [
141 new VisibleRelation($aliased, new RowShape([new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull)]), new Name('x')),
142 new VisibleRelation($plain, new RowShape([new OutputSlot(new Name('a'), new Known(Storage::Text), Nullability::Nullable)]), null, new QualifiedName(new Name('u'))),
143 ]);
144
145 $byAlias = (new ColumnLookup())->find($environment, new Name('a'), new QualifiedName(new Name('x')));
146 $byName = (new ColumnLookup())->find($environment, new Name('a'), new QualifiedName(new Name('u')));
147
148 self::assertInstanceOf(ResolvedColumn::class, $byAlias);
149 self::assertSame($aliased, $byAlias->relation);
150 self::assertInstanceOf(ResolvedColumn::class, $byName);
151 self::assertSame($plain, $byName->relation);
152 self::assertInstanceOf(MissingColumn::class, (new ColumnLookup())->find($environment, new Name('a'), new QualifiedName(new Name('t'))));
153 }
154
155 public function testFindSkipsHiddenSlotsForAnUnqualifiedName(): void
156 {
157 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
158 $relation = new TableInput(new QualifiedName(new Name('t')), new Name('x'));
159 $hidden = new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull);
160 $environment = new Environment($context, null, [new VisibleRelation($relation, new RowShape([$hidden]), new Name('x'), null, [0])]);
161
162 $unqualified = (new ColumnLookup())->find($environment, new Name('a'));
163 $qualified = (new ColumnLookup())->find($environment, new Name('a'), new QualifiedName(new Name('x')));
164
165 self::assertInstanceOf(MissingColumn::class, $unqualified);
166 self::assertInstanceOf(ResolvedColumn::class, $qualified);
167 self::assertSame($hidden, $qualified->slot);
168 }
169
170 public function testFindUsesAnImplicitSlotWhenNoShapeSlotHasTheName(): void
171 {
172 $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
173 $relation = new TableInput(new QualifiedName(new Name('t')));
174 $rowid = new OutputSlot(new Name('rowid'), new Known(Storage::Integer), Nullability::NotNull);
175 $environment = new Environment($context, null, [new VisibleRelation($relation, new RowShape([new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull)]), null, null, [], [new ImplicitSlot([new Name('rowid'), new Name('oid')], $rowid)])]);
176
177 $resolution = (new ColumnLookup())->find($environment, new Name('oid'));
178
179 self::assertInstanceOf(ResolvedColumn::class, $resolution);
180 self::assertSame($rowid, $resolution->slot);
181 }
182
183 public function testConditionalCollectsTheRelationsAndMissingInputsOfEveryOpenOccurrence(): void
184 {
185 $first = new TableInput(new QualifiedName(new Name('t')));
186 $second = new TableInput(new QualifiedName(new Name('u')));
187 $missingFirst = new UndeclaredRelation(new QualifiedName(new Name('t')));
188 $missingSecond = new UndeclaredRelation(new QualifiedName(new Name('u')));
189
190 $resolution = (new ColumnLookup())->conditional(new Name('a'), [], [
191 new VisibleRelation($first, new RowShape([], [$missingFirst])),
192 new VisibleRelation($second, new RowShape([], [$missingSecond])),
193 ]);
194
195 self::assertSame('a', $resolution->name->value);
196 self::assertSame([$first, $second], $resolution->relations);
197 self::assertSame([$missingFirst, $missingSecond], $resolution->missing);
198 }
199}
200