packages/sql-semantics-sqlite/tests/Unit/Statement/Relation/NestedInputTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Relation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\AmbiguousColumn;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\Field;
17use SqlSemantics\Platform\Sqlite\Semantics;
18use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
19use SqlSemantics\Platform\Sqlite\Statement\Query\Star;
20use SqlSemantics\Platform\Sqlite\Statement\Relation\JoinChain;
21use SqlSemantics\Platform\Sqlite\Statement\Relation\NestedInput;
22use SqlSemantics\Platform\Sqlite\Statement\Relation\TableInput;
23
24#[CoversClass(NestedInput::class)]
25#[Medium]
26final class NestedInputTest extends TestCase
27{
28 public function testDeriveRelationExposesTheJoinWrittenInParentheses(): void
29 {
30 $semantics = new Semantics();
31 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
32 $u = $semantics->inspect('CREATE TABLE u (a INTEGER NOT NULL, c TEXT)');
33 $query = $semantics->inspect('SELECT * FROM (t JOIN u USING (a))', [$t, $u]);
34 $fields = $query->fields();
35
36 self::assertInstanceOf(Select::class, $query->statement);
37 self::assertInstanceOf(NestedInput::class, $query->statement->from);
38 self::assertInstanceOf(JoinChain::class, $query->statement->from->relation);
39 self::assertNull($query->statement->from->alias);
40 self::assertNotNull($fields);
41 self::assertSame(['id', 'a', 'b', 'c'], array_map(static fn (Field $field): ?string => $field->name?->value, $fields->items));
42 self::assertCount(4, $query->facts->relation($query->statement->from)->shape->slots);
43 self::assertCount(4, $query->facts->relation($query->statement->from->relation)->shape->slots);
44 self::assertSame([], $query->facts->diagnostics);
45 }
46
47 public function testDeriveRelationRenamesASingleTableByTheAlias(): void
48 {
49 $semantics = new Semantics();
50 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
51 $aliased = $semantics->inspect('SELECT x.a FROM (t) AS x', [$t]);
52 $original = $semantics->inspect('SELECT t.a FROM (t) AS x', [$t]);
53 $resolution = $aliased->field(0)->resolution;
54
55 self::assertInstanceOf(Select::class, $aliased->statement);
56 self::assertInstanceOf(NestedInput::class, $aliased->statement->from);
57 self::assertInstanceOf(ResolvedColumn::class, $resolution);
58 self::assertSame($aliased->statement->from->relation, $resolution->relation);
59 self::assertSame($t->declarations()[0]->columns[1], $resolution->declaration());
60 self::assertInstanceOf(MissingColumn::class, $original->field(0)->resolution);
61 }
62
63 public function testDeriveRelationQualifiesAJoinedPairByTheAlias(): void
64 {
65 $semantics = new Semantics();
66 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
67 $u = $semantics->inspect('CREATE TABLE u (a INTEGER NOT NULL, c TEXT)');
68 $qualified = $semantics->inspect('SELECT j.a, j.c FROM (t JOIN u USING (a)) AS j', [$t, $u]);
69 $ambiguous = $semantics->inspect('SELECT a FROM (t JOIN u) AS j', [$t, $u]);
70
71 self::assertSame($t->declarations()[0]->columns[1], $qualified->field(0)->column());
72 self::assertSame($u->declarations()[0]->columns[1], $qualified->field(1)->column());
73 self::assertSame([], $qualified->facts->diagnostics);
74 self::assertInstanceOf(AmbiguousColumn::class, $ambiguous->field(0)->resolution);
75 }
76
77 public function testRenderWritesParenthesesAndTheAlias(): void
78 {
79 $semantics = new Semantics();
80 $built = new Operation($semantics->context(), new Select([new Star()], new NestedInput(new TableInput(new QualifiedName(new Name('t'))), new Name('x'))));
81
82 self::assertSame('SELECT * FROM (t) AS x', $built->toString());
83 self::assertSame('SELECT * FROM (t JOIN u USING (a)) j, u u2', $semantics->inspect('select * from (t join u using (a)) j, u u2')->toString());
84 self::assertSame('SELECT * FROM ((t))', $semantics->inspect('SELECT * FROM ((t))')->toString());
85 }
86
87 public function testRenderRefusesToLeaveOutAsWithoutAnAlias(): void
88 {
89 $this->expectExceptionMessage('AS is left out only before an alias.');
90
91 new NestedInput(new TableInput(new QualifiedName(new Name('t'))), null, false);
92 }
93}
94