packages/sql-semantics-postgres/tests/Unit/Rules/Resolution/TableFunctionShapesTest.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\TableFunctionShapes::class)]
12#[Medium]
13final class TableFunctionShapesTest extends TestCase
14{
15    public function testXmlReportsASecondOrdinalityColumn(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $query = $semantics->inspect("SELECT * FROM XMLTABLE ('/r' PASSING '<r/>' COLUMNS a text NULL NOT NULL, n FOR ORDINALITY, m FOR ORDINALITY)");
19        self::assertSame('only one FOR ORDINALITY column is allowed', $query->facts->diagnostics[1]->message());
20    }
21
22    public function testOptionsReportsConflictingNullability(): void
23    {
24        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
25        $query = $semantics->inspect("SELECT * FROM XMLTABLE ('/r' PASSING '<r/>' COLUMNS a text NULL NOT NULL, n FOR ORDINALITY, m FOR ORDINALITY)");
26        self::assertSame('conflicting or redundant NULL / NOT NULL declarations for column "a"', $query->facts->diagnostics[0]->message());
27    }
28
29    public function testJsonHasTheColumnsInOrder(): void
30    {
31        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
32        $query = $semantics->inspect("SELECT * FROM JSON_TABLE ('[]', '\$[*]' COLUMNS (a text, NESTED '\$.b' COLUMNS (b text), c text))");
33        self::assertSame(['a:Nullable', 'b:Nullable', 'c:Nullable'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\Field $field): string => ($field->name->value ?? '') . ':' . $field->nullability->name, $query->fields()->items ?? []));
34    }
35
36    public function testColumnsAnswersOneSlotPerColumn(): void
37    {
38        $context = (new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true);
39        $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($context);
40        self::assertCount(1, (new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\TableFunctionShapes())->columns([new \SqlSemantics\Platform\PostgreSql\Statement\Relation\Json\JsonOrdinalityColumn(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('n'))], $derivation, $derivation->environment()));
41    }
42
43    public function testTypedReportsABehaviorTheColumnRefuses(): void
44    {
45        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
46        $query = $semantics->inspect("SELECT * FROM JSON_TABLE ('[]', '\$[*]' COLUMNS (e boolean EXISTS EMPTY ON ERROR))");
47        self::assertSame('invalid ON ERROR behavior for column "e"', $query->facts->diagnostics[0]->message());
48    }
49
50    public function testClauseIgnoresNoClause(): void
51    {
52        $context = (new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true);
53        $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($context);
54        (new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\TableFunctionShapes())->clause(null, $derivation, $derivation->environment());
55        self::assertSame([], $derivation->facts()->diagnostics);
56    }
57}
58