packages/sql-semantics-mysql/tests/Unit/Rules/Query/Having/ResultReferencesTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Query\Having;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
11use SqlSemantics\Platform\MySql\Model\Resolution\VisibleRelation;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\AliasTarget;
15use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\AmbiguousColumn;
16use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\ConditionalColumn;
17use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\ResolvedColumn;
18use SqlSemantics\Platform\MySql\Model\Statement\Reference\Missing\SessionState;
19use SqlSemantics\Platform\MySql\Rules\Query\Having\GroupedRow;
20use SqlSemantics\Platform\MySql\Rules\Query\Having\ResultReferences;
21use SqlSemantics\Platform\MySql\Semantics;
22use SqlSemantics\Platform\MySql\Statement\Expression\Comparison;
23use SqlSemantics\Platform\MySql\Statement\Expression\Logical;
24use SqlSemantics\Platform\MySql\Statement\Query\Select;
25use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
26
27#[CoversClass(ResultReferences::class)]
28#[Medium]
29final class ResultReferencesTest extends TestCase
30{
31    public function testFindPrefersAGroupByColumnToASelectListItem(): void
32    {
33        $semantics = new Semantics();
34        $table = $semantics->inspect('CREATE TABLE t (a INT NOT NULL, b INT)');
35        $operation = $semantics->inspect('SELECT b AS a FROM t GROUP BY a HAVING a > 0', [$table]);
36        $select = $operation->statement;
37        self::assertInstanceOf(Select::class, $select);
38        self::assertInstanceOf(Comparison::class, $select->having);
39        $resolution = $operation->facts->scalar($select->having->left)->resolution;
40
41        self::assertInstanceOf(ResolvedColumn::class, $resolution);
42        self::assertSame('a', $resolution->slot->name?->value);
43        self::assertSame([], $operation->facts->diagnostics);
44    }
45
46    public function testGroupingSkipsAColumnOfAnotherOccurrenceAndReportsTwoDifferentColumns(): void
47    {
48        $semantics = new Semantics();
49        $table = $semantics->inspect('CREATE TABLE t (a INT NOT NULL)');
50        $other = $semantics->inspect('SELECT u.a AS a FROM t, t AS u GROUP BY t.a HAVING u.a > 0', [$table]);
51        $ambiguous = $semantics->inspect('SELECT 1 FROM t, t AS u GROUP BY t.a, u.a HAVING a > 0', [$table]);
52        $select = $other->statement;
53        self::assertInstanceOf(Select::class, $select);
54        self::assertInstanceOf(Comparison::class, $select->having);
55        $item = $select->items[0];
56        self::assertInstanceOf(SelectExpression::class, $item);
57        $resolution = $other->facts->scalar($select->having->left)->resolution;
58        $selected = $other->facts->scalar($item->expression)->resolution;
59        self::assertInstanceOf(ResolvedColumn::class, $resolution);
60        self::assertInstanceOf(ResolvedColumn::class, $selected);
61
62        self::assertSame($selected->relation, $resolution->relation);
63        self::assertSame([], $other->facts->diagnostics);
64        self::assertCount(1, $ambiguous->facts->diagnostics);
65        self::assertInstanceOf(AmbiguousColumn::class, $ambiguous->facts->diagnostics[0]);
66    }
67
68    public function testSelectedResolvesAColumnItemToItsColumnAndAnotherItemToTheItem(): void
69    {
70        $semantics = new Semantics();
71        $table = $semantics->inspect('CREATE TABLE t (a INT NOT NULL, b INT)');
72        $column = $semantics->inspect('SELECT a AS x FROM t HAVING x > 0', [$table]);
73        $computed = $semantics->inspect('SELECT a + 1 AS x FROM t HAVING x > 0', [$table]);
74        $hidden = $semantics->inspect('SELECT t.a AS x FROM t HAVING a > 0', [$table]);
75        $ambiguous = $semantics->inspect('SELECT a, b AS a FROM t HAVING a > 0', [$table]);
76        $columnSelect = $column->statement;
77        $computedSelect = $computed->statement;
78        $hiddenSelect = $hidden->statement;
79        self::assertInstanceOf(Select::class, $columnSelect);
80        self::assertInstanceOf(Select::class, $computedSelect);
81        self::assertInstanceOf(Select::class, $hiddenSelect);
82        self::assertInstanceOf(Comparison::class, $columnSelect->having);
83        self::assertInstanceOf(Comparison::class, $computedSelect->having);
84        self::assertInstanceOf(Comparison::class, $hiddenSelect->having);
85        $alias = $computed->facts->scalar($computedSelect->having->left)->resolution;
86
87        self::assertInstanceOf(ResolvedColumn::class, $column->facts->scalar($columnSelect->having->left)->resolution);
88        self::assertInstanceOf(AliasTarget::class, $alias);
89        self::assertSame($computed->field('x'), $alias->field);
90        self::assertInstanceOf(ResolvedColumn::class, $hidden->facts->scalar($hiddenSelect->having->left)->resolution);
91        self::assertCount(1, $ambiguous->facts->diagnostics);
92        self::assertInstanceOf(AmbiguousColumn::class, $ambiguous->facts->diagnostics[0]);
93    }
94
95    public function testNamedComparesTheColumnNameByTheColumnComparisonOfTheContext(): void
96    {
97        $semantics = new Semantics();
98        $operation = $semantics->inspect('SELECT a FROM t', [$semantics->inspect('CREATE TABLE t (a INT)')]);
99        $resolution = $operation->field('a')->resolution;
100        self::assertInstanceOf(ResolvedColumn::class, $resolution);
101        $environment = new Environment($operation->context);
102
103        self::assertTrue((new ResultReferences())->named($environment, $resolution, new Name('A')));
104        self::assertFalse((new ResultReferences())->named($environment, $resolution, new Name('b')));
105    }
106
107    public function testAdmittedTellsWhetherTheQualifierFindsTheColumn(): void
108    {
109        $semantics = new Semantics();
110        $operation = $semantics->inspect('SELECT a FROM t', [$semantics->inspect('CREATE TABLE t (a INT)')]);
111        $resolution = $operation->field('a')->resolution;
112        self::assertInstanceOf(ResolvedColumn::class, $resolution);
113        $environment = new Environment($operation->context, null, [new VisibleRelation($resolution->relation, $operation->facts->relation($resolution->relation)->shape, null, new QualifiedName(new Name('t')))]);
114
115        self::assertTrue((new ResultReferences())->admitted($environment, $resolution, new Name('a'), new QualifiedName(new Name('t'))));
116        self::assertFalse((new ResultReferences())->admitted($environment, $resolution, new Name('a'), new QualifiedName(new Name('u'))));
117    }
118
119    public function testSameComparesTheOccurrenceAndTheSlot(): void
120    {
121        $semantics = new Semantics();
122        $operation = $semantics->inspect('SELECT a, b FROM t', [$semantics->inspect('CREATE TABLE t (a INT, b INT)')]);
123        $first = $operation->field('a')->resolution;
124        $second = $operation->field('b')->resolution;
125        self::assertInstanceOf(ResolvedColumn::class, $first);
126        self::assertInstanceOf(ResolvedColumn::class, $second);
127
128        self::assertTrue((new ResultReferences())->same($first, new ResolvedColumn($first->relation, $first->slot, 2)));
129        self::assertFalse((new ResultReferences())->same($first, $second));
130    }
131
132    public function testDeeperCountsTheQueriesBetweenTheUseAndTheColumn(): void
133    {
134        $semantics = new Semantics();
135        $operation = $semantics->inspect('SELECT a FROM t', [$semantics->inspect('CREATE TABLE t (a INT)')]);
136        $resolution = $operation->field('a')->resolution;
137        self::assertInstanceOf(ResolvedColumn::class, $resolution);
138
139        self::assertSame($resolution, (new ResultReferences())->deeper($resolution, 0));
140        self::assertSame($resolution->depth + 2, (new ResultReferences())->deeper($resolution, 2)->depth);
141        self::assertSame($resolution->slot, (new ResultReferences())->deeper($resolution, 2)->slot);
142        self::assertNull((new ResultReferences())->find(new GroupedRow([], [], true), new Environment($operation->context), new Name('a'), null, 0));
143    }
144
145    public function testUnaliasedMatchesAColumnItemByItsColumnInAnAdmittedOccurrence(): void
146    {
147        $semantics = new Semantics();
148        $operation = $semantics->inspect('SELECT a AS x FROM t', [$semantics->inspect('CREATE TABLE t (a INT)')]);
149        $resolution = $operation->field('x')->resolution;
150        self::assertInstanceOf(ResolvedColumn::class, $resolution);
151        $environment = new Environment($operation->context, null, [new VisibleRelation($resolution->relation, $operation->facts->relation($resolution->relation)->shape, null, new QualifiedName(new Name('t')))]);
152
153        self::assertTrue((new ResultReferences())->unaliased($environment, $resolution, new Name('a'), null));
154        self::assertTrue((new ResultReferences())->unaliased($environment, $resolution, new Name('a'), new QualifiedName(new Name('t'))));
155        self::assertFalse((new ResultReferences())->unaliased($environment, $resolution, new Name('a'), new QualifiedName(new Name('u'))));
156        self::assertFalse((new ResultReferences())->unaliased($environment, $resolution, new Name('x'), null));
157    }
158
159    public function testChosenPrefersTheAliasedMatchAndReportsTwoDifferentUnaliasedOnes(): void
160    {
161        $semantics = new Semantics();
162        $operation = $semantics->inspect('SELECT a, b FROM t', [$semantics->inspect('CREATE TABLE t (a INT, b INT)')]);
163        $first = $operation->field('a')->resolution;
164        $second = $operation->field('b')->resolution;
165        self::assertInstanceOf(ResolvedColumn::class, $first);
166        self::assertInstanceOf(ResolvedColumn::class, $second);
167
168        self::assertSame($first, (new ResultReferences())->chosen(new Name('a'), $first, $second, $second, 0));
169        self::assertSame($second, (new ResultReferences())->chosen(new Name('a'), null, $second, null, 0));
170        self::assertInstanceOf(AmbiguousColumn::class, (new ResultReferences())->chosen(new Name('a'), null, $first, $second, 0));
171        self::assertNull((new ResultReferences())->chosen(new Name('a'), null, null, null, 0));
172        $deeper = (new ResultReferences())->chosen(new Name('a'), $first, null, null, 1);
173        self::assertInstanceOf(ResolvedColumn::class, $deeper);
174        self::assertSame($first->depth + 1, $deeper->depth);
175    }
176
177    public function testSelectedIsConditionalWhileAnItemWithoutADecidedNameCouldHaveTheName(): void
178    {
179        $semantics = new Semantics();
180        $table = $semantics->inspect('CREATE TABLE t (a INT, q INT)')->declarations();
181        $operation = $semantics->inspect("SELECT 'é', a AS q, 1 AS r FROM t HAVING q > 0 AND r > 0", $table);
182        $select = $operation->statement;
183        self::assertInstanceOf(Select::class, $select);
184        $having = $select->having;
185        self::assertInstanceOf(Logical::class, $having);
186        self::assertInstanceOf(Comparison::class, $having->left);
187        self::assertInstanceOf(Comparison::class, $having->right);
188        $q = $operation->facts->scalar($having->left->left)->resolution;
189        $r = $operation->facts->scalar($having->right->left)->resolution;
190
191        self::assertInstanceOf(ConditionalColumn::class, $q);
192        self::assertCount(1, $q->candidates);
193        self::assertInstanceOf(ConditionalColumn::class, $r);
194        self::assertSame([], $r->candidates);
195        self::assertSame([], $operation->facts->diagnostics);
196    }
197
198    public function testUnnamedStopsAtTheItemTheNameNames(): void
199    {
200        $semantics = new Semantics();
201        $operation = $semantics->inspect("SELECT 'é', 1 AS r, 'ü' FROM DUAL HAVING r > 0");
202        $output = $operation->facts->output;
203        self::assertNotNull($output);
204        $row = new GroupedRow($output->projection, [], false);
205        $scope = new Environment($operation->context);
206
207        self::assertCount(1, (new ResultReferences())->unnamed($row, $scope, new Name('r'), null));
208        self::assertCount(2, (new ResultReferences())->unnamed($row, $scope, new Name('s'), null));
209        self::assertSame([], (new ResultReferences())->unnamed($row, $scope, new Name('s'), new QualifiedName(new Name('t'))));
210    }
211
212    public function testUndecidedKeepsAResolvedMatchAsCandidate(): void
213    {
214        $semantics = new Semantics();
215        $operation = $semantics->inspect('SELECT 1 AS r FROM DUAL');
216        $alias = new AliasTarget($operation->field(0));
217        $missing = [new SessionState('character_set_client')];
218
219        self::assertSame($alias, (new ResultReferences())->undecided(new Name('r'), $alias, []));
220        self::assertInstanceOf(ConditionalColumn::class, (new ResultReferences())->undecided(new Name('r'), $alias, $missing));
221        self::assertNull((new ResultReferences())->undecided(new Name('r'), null, []));
222    }
223}
224