packages/sql-semantics-postgres/tests/Unit/Rules/Resolution/TableShapesTest.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;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Resolution\TableShapes::class)]
12#[Medium]
13final class TableShapesTest extends TestCase
14{
15 public function testInputReportsASampleOfACommonTable(): void
16 {
17 $context = (new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true);
18 $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($context);
19 $fact = $derivation->query(new \SqlSemantics\Platform\PostgreSql\Statement\Query\QueryExpression(new \SqlSemantics\Platform\PostgreSql\Statement\Query\With\WithClause([new \SqlSemantics\Platform\PostgreSql\Statement\Query\With\CommonTableExpression(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('x'), new \SqlSemantics\Platform\PostgreSql\Statement\Query\Select([new \SqlSemantics\Platform\PostgreSql\Statement\Query\ExpressionTarget(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('1')))]), [], null, null, null)], false), new \SqlSemantics\Platform\PostgreSql\Statement\Query\Select([new \SqlSemantics\Platform\PostgreSql\Statement\Query\StarTarget()], new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('x'))), null, [], new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableSample(new \SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('system')]), [new \SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('1'))])))), $derivation->environment());
20 self::assertSame('TABLESAMPLE clause can only be applied to tables and materialized views', $derivation->facts()->diagnostics[0]->message());
21 }
22
23 public function testInputReportsATableWithoutReturning(): void
24 {
25 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
26 $query = $semantics->inspect('WITH x AS (INSERT INTO t VALUES (1)) SELECT * FROM x');
27 self::assertSame('WITH query "x" does not have a RETURNING clause', $query->facts->diagnostics[0]->message());
28 }
29
30 public function testImplicitIsEmptyWithoutADeclaration(): void
31 {
32 self::assertSame([], (new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\TableShapes())->implicit(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\RelationFact(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([]))));
33 }
34
35 public function testInputReportsSamplingAView(): void
36 {
37 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
38 $context = [$semantics->inspect('CREATE VIEW v AS SELECT 1 AS a'), $semantics->inspect('CREATE MATERIALIZED VIEW m AS SELECT 1 AS a')];
39 self::assertSame(['TABLESAMPLE clause can only be applied to tables and materialized views'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('SELECT * FROM v TABLESAMPLE system (1)', $context)->facts->diagnostics));
40 self::assertSame([], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('SELECT * FROM m TABLESAMPLE system (1)', $context)->facts->diagnostics));
41 }
42}
43