packages/sql-semantics-postgres/tests/Unit/Rules/Query/Facts/QueryRootsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Query\Facts;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Query\Facts\QueryRoots::class)]
12#[Medium]
13final class QueryRootsTest extends TestCase
14{
15    public function testDeriveDeclaresTheTableOfSelectInto(): void
16    {
17        $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
18        $t = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text)]);
19        $u = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('u')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int8, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('c'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Bool, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]);
20        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
21        $query = $semantics->analyze('SELECT a, b INTO n FROM t', [$t, $u]);
22        self::assertSame([null, 'n', 2], [$query->facts->output, $query->declarations()[0]->name->name->value, count($query->declarations()[0]->columns)]);
23    }
24
25    public function testFirstFindsTheFirstSelection(): void
26    {
27        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
28        $query = $semantics->analyze('(SELECT 1 AS a INTO n) UNION SELECT 2');
29        $operation = $query->statement;
30        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\SetOperation::class, $operation);
31        self::assertSame('n', (new \SqlSemantics\Platform\PostgreSql\Rules\Query\Facts\QueryRoots())->first($operation)?->into?->table->name->value);
32    }
33
34    public function testTableDeclaresAColumnOfADependentTypeWithTheMissingInputs(): void
35    {
36        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
37        $query = $semantics->analyze('SELECT x INTO n FROM v');
38        self::assertTrue($query->declarations()[0]->complete);
39        $type = $query->declarations()[0]->columns[0]->type;
40        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Undetermined::class, $type);
41        self::assertSame('the declaration of relation v', $type->missing[0]->describe());
42    }
43
44    public function testTableIsIncompleteAtAnOpenRow(): void
45    {
46        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
47        $query = $semantics->analyze('SELECT 1 AS a, * INTO n FROM v');
48        self::assertFalse($query->declarations()[0]->complete);
49        self::assertCount(1, $query->declarations()[0]->columns);
50    }
51}
52