packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/CreateIndexTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Schema;
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\Statement\Reference\Column\MissingColumn;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\DeclaredTable;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\MissingTable;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\UndeclaredTable;
16use SqlSemantics\Platform\Sqlite\Semantics;
17use SqlSemantics\Platform\Sqlite\Statement\Expression\Collate;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\Binary;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateIndex;
20use SqlSemantics\Platform\Sqlite\Statement\Schema\Limit\ProhibitedExpression;
21
22#[CoversClass(CreateIndex::class)]
23#[Medium]
24final class CreateIndexTest extends TestCase
25{
26 public function testIndexedGivesTheTableTheSchemaOfTheIndexName(): void
27 {
28 $statement = (new Semantics())->inspect('CREATE INDEX aux.i ON t (a)')->statement;
29
30 self::assertInstanceOf(CreateIndex::class, $statement);
31 self::assertSame('aux', $statement->indexed()->schema?->value);
32 self::assertSame('t', $statement->indexed()->name->value);
33 }
34
35 public function testDeriveStatementResolvesTheTableAndTheIndexedColumns(): void
36 {
37 $semantics = new Semantics();
38 $table = $semantics->inspect('CREATE TABLE t (a, b)');
39 $operation = $semantics->inspect('CREATE INDEX i ON t (b, a COLLATE nocase) WHERE rowid > 0', [$table]);
40 $statement = $operation->statement;
41
42 self::assertInstanceOf(CreateIndex::class, $statement);
43 $resolution = $operation->facts->relation($statement)->table;
44 self::assertInstanceOf(DeclaredTable::class, $resolution);
45 self::assertSame($table->declarations()[0], $resolution->table);
46 $column = $operation->facts->scalar($statement->terms[0]->expression)->resolution;
47 self::assertInstanceOf(ResolvedColumn::class, $column);
48 self::assertSame($table->declarations()[0]->columns[1], $column->slot->column);
49 self::assertSame($statement, $column->relation);
50 self::assertInstanceOf(Collate::class, $statement->terms[1]->expression);
51 self::assertInstanceOf(ResolvedColumn::class, $operation->facts->scalar($statement->terms[1]->expression->operand)->resolution);
52 self::assertInstanceOf(Binary::class, $statement->where);
53 self::assertInstanceOf(ResolvedColumn::class, $operation->facts->scalar($statement->where->left)->resolution);
54 self::assertSame([], $operation->facts->diagnostics);
55 self::assertSame([], $operation->declarations());
56 }
57
58 public function testDeriveStatementSeesNoRowIdentifierInAnIndexedExpression(): void
59 {
60 $semantics = new Semantics();
61 $table = $semantics->inspect('CREATE TABLE t (a)');
62 $operation = $semantics->inspect('CREATE INDEX i ON t (a + rowid)', [$table]);
63
64 self::assertCount(1, $operation->facts->diagnostics);
65 self::assertInstanceOf(MissingColumn::class, $operation->facts->diagnostics[0]);
66 }
67
68 public function testDeriveStatementReportsWhatAnIndexExpressionAndACondionMayNotContain(): void
69 {
70 $semantics = new Semantics();
71 $table = $semantics->inspect('CREATE TABLE t (a)');
72 $operation = $semantics->inspect('CREATE INDEX i ON t (t.a, random()) WHERE a IN (SELECT 1) AND ? AND current_time > 0', [$table]);
73 $reported = array_map(static fn (object $diagnostic): array => $diagnostic instanceof ProhibitedExpression ? [$diagnostic->construct->name, $diagnostic->position->name] : [$diagnostic::class], $operation->facts->diagnostics);
74
75 self::assertSame([
76 ['DotOperator', 'IndexExpression'],
77 ['NonDeterministicFunction', 'IndexExpression'],
78 ['Subquery', 'PartialIndexWhere'],
79 ['Parameter', 'PartialIndexWhere'],
80 ['NonDeterministicFunction', 'PartialIndexWhere'],
81 ], $reported);
82 }
83
84 public function testDeriveStatementReportsAMissingTableAndAMissingColumn(): void
85 {
86 $semantics = new Semantics();
87 $table = $semantics->inspect('CREATE TABLE t (a)');
88
89 self::assertInstanceOf(MissingTable::class, $semantics->inspect('CREATE INDEX i ON u (a)', [$table])->facts->diagnostics[0]);
90 self::assertInstanceOf(MissingColumn::class, $semantics->inspect('CREATE INDEX i ON t (zz)', [$table])->facts->diagnostics[0]);
91 }
92
93 public function testDeriveRelationKeepsAnUndeclaredTableOpen(): void
94 {
95 $semantics = new Semantics();
96 $statement = $semantics->inspect('CREATE INDEX i ON t (a)')->statement;
97 $derivation = new Derivation($semantics->context());
98
99 self::assertInstanceOf(CreateIndex::class, $statement);
100 $fact = $statement->deriveRelation($derivation, $derivation->environment());
101 self::assertInstanceOf(UndeclaredTable::class, $fact->table);
102 self::assertFalse($fact->shape->complete());
103 }
104
105 public function testRenderWritesEveryPartInGrammarOrder(): void
106 {
107 $operation = (new Semantics())->inspect('create unique index if not exists main.i on t (a collate nocase desc, b + 1) where a is not null');
108
109 self::assertSame('CREATE UNIQUE INDEX IF NOT EXISTS main.i ON t (a COLLATE nocase DESC, b + 1) WHERE a IS NOT NULL', $operation->toString());
110 }
111
112 public function testRefusesAStringLiteralTermThatNamesAColumn(): void
113 {
114 $this->expectExceptionMessage('A string literal written as an index term names a column: write it as a LiteralColumn.');
115
116 new CreateIndex(new \SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name('i')), new \SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name('t'), [new \SqlSemantics\Platform\Sqlite\Statement\Query\Ordering\SortTerm(new \SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TextLiteral('a'))]);
117 }
118}
119