packages/sql-semantics-sqlite/tests/Unit/Rules/Resolution/ColumnFactsTest.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;
10use SqlSemantics\Platform\Sqlite\Model\Construction\Derivation;
11use SqlSemantics\Platform\Sqlite\Model\Resolution\Environment;
12use SqlSemantics\Platform\Sqlite\Model\Resolution\VisibleRelation;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\AliasTarget;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\AmbiguousColumn;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ConditionalColumn;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
19use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
20use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Dependent;
21use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Invalid;
22use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
23use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
24use SqlSemantics\Platform\Sqlite\Rules\Resolution\ColumnFacts;
25use SqlSemantics\Platform\Sqlite\Rules\Resolution\TableShapes;
26use SqlSemantics\Platform\Sqlite\Semantics;
27use SqlSemantics\Platform\Sqlite\Statement\Expression\ColumnUse;
28use SqlSemantics\Platform\Sqlite\Statement\Expression\DoubleQuotedWord;
29use SqlSemantics\Platform\Sqlite\Statement\Expression\Grouped;
30use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
31use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\Binary;
32use SqlSemantics\Platform\Sqlite\Statement\Expression\TruthWord;
33use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
34use SqlSemantics\Platform\Sqlite\Statement\Relation\TableInput;
35use SqlSemantics\Platform\Sqlite\Statement\Type\ColumnDomain;
36
37#[CoversClass(ColumnFacts::class)]
38#[Medium]
39final class ColumnFactsTest extends TestCase
40{
41    public function testOfAnswersTheSlotOfAResolvedColumn(): void
42    {
43        $semantics = new Semantics();
44        $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
45        $query = $semantics->inspect('SELECT b FROM t', [$t]);
46        $resolution = $query->field(0)->resolution;
47
48        self::assertInstanceOf(ResolvedColumn::class, $resolution);
49        $fact = (new ColumnFacts())->of($resolution);
50        self::assertSame($resolution, $fact->resolution);
51        self::assertSame($resolution->slot->type, $fact->type);
52        self::assertInstanceOf(Known::class, $fact->type);
53        self::assertInstanceOf(ColumnDomain::class, $fact->type->descriptor);
54        self::assertSame('TEXT', $fact->type->descriptor->name());
55        self::assertSame(Nullability::Nullable, $fact->nullability);
56    }
57
58    public function testOfAnswersTheFieldOfAnAliasTarget(): void
59    {
60        $semantics = new Semantics();
61        $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
62        $query = $semantics->inspect('SELECT a AS q FROM t WHERE q = 1', [$t]);
63
64        self::assertInstanceOf(Select::class, $query->statement);
65        self::assertInstanceOf(Binary::class, $query->statement->where);
66        $resolution = $query->facts->scalar($query->statement->where->left)->resolution;
67        self::assertInstanceOf(AliasTarget::class, $resolution);
68        $fact = (new ColumnFacts())->of($resolution);
69        self::assertSame($query->field('q'), $resolution->field);
70        self::assertSame($resolution->field->type, $fact->type);
71        self::assertSame(Nullability::NotNull, $fact->nullability);
72        self::assertSame($resolution, $fact->resolution);
73    }
74
75    public function testOfDependsOnTheMissingInputsOfAConditionalColumn(): void
76    {
77        $query = (new Semantics())->inspect('SELECT a FROM t');
78        $resolution = $query->field(0)->resolution;
79
80        self::assertInstanceOf(ConditionalColumn::class, $resolution);
81        $fact = (new ColumnFacts())->of($resolution);
82        self::assertInstanceOf(Dependent::class, $fact->type);
83        self::assertSame($resolution->missing, $fact->type->missing);
84        self::assertSame(Nullability::Dependent, $fact->nullability);
85        self::assertSame($resolution, $fact->resolution);
86    }
87
88    public function testOfIsInvalidForAProblemResolution(): void
89    {
90        $semantics = new Semantics();
91        $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
92        $ambiguous = $semantics->inspect('SELECT a FROM t, t AS t2', [$t])->field(0)->resolution;
93        $missing = new MissingColumn(new Name('zz'));
94
95        self::assertInstanceOf(AmbiguousColumn::class, $ambiguous);
96        $fact = (new ColumnFacts())->of($missing);
97        self::assertInstanceOf(Invalid::class, $fact->type);
98        self::assertSame($missing, $fact->type->cause);
99        self::assertSame(Nullability::Dependent, $fact->nullability);
100        self::assertSame($missing, $fact->resolution);
101        $problem = (new ColumnFacts())->of($ambiguous);
102        self::assertInstanceOf(Invalid::class, $problem->type);
103        self::assertSame($ambiguous, $problem->type->cause);
104    }
105
106    public function testDenotedLooksThroughGroupingsToTheNameUse(): void
107    {
108        $semantics = new Semantics();
109        $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
110        $query = $semantics->inspect('SELECT ((b)), ("a"), (zz), b COLLATE nocase, (1), ("zz"), (true) FROM t', [$t]);
111
112        self::assertInstanceOf(ResolvedColumn::class, $query->field(0)->resolution);
113        self::assertSame($t->declarations()[0]->columns[2], $query->field(0)->column());
114        self::assertSame('b', $query->field(0)->name?->value);
115        self::assertSame($t->declarations()[0]->columns[1], $query->field(1)->column());
116        self::assertInstanceOf(MissingColumn::class, $query->field(2)->resolution);
117        self::assertNull($query->field(3)->resolution);
118        self::assertNull($query->field(4)->resolution);
119        self::assertNull($query->field(5)->resolution);
120        self::assertNull($query->field(6)->resolution);
121        self::assertCount(1, $query->facts->diagnostics);
122    }
123
124    public function testDenotedAnswersTheOutcomeOfAnUngroupedNameUse(): void
125    {
126        $semantics = new Semantics();
127        $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
128        $derivation = new Derivation($semantics->context([$t]));
129        $input = new TableInput(new QualifiedName(new Name('t')));
130        $environment = new Environment($derivation->context, null, [new VisibleRelation($input, (new TableShapes())->fact($derivation, $input->name, $derivation->environment())->shape, null, $input->name)]);
131        $facts = new ColumnFacts();
132        $resolved = $facts->denoted(new Grouped(new ColumnUse(new Name('b'))), $environment);
133
134        self::assertInstanceOf(ResolvedColumn::class, $resolved);
135        self::assertSame($t->declarations()[0]->columns[2], $resolved->slot->column);
136        self::assertInstanceOf(MissingColumn::class, $facts->denoted(new ColumnUse(new Name('zz')), $environment));
137        self::assertInstanceOf(ResolvedColumn::class, $facts->denoted(new DoubleQuotedWord(new Name('a')), $environment));
138        self::assertNull($facts->denoted(new DoubleQuotedWord(new Name('zz')), $environment));
139        self::assertNull($facts->denoted(new TruthWord(true), $environment));
140        self::assertNull($facts->denoted(new IntegerLiteral('1'), $environment));
141    }
142}
143