packages/sql-semantics-postgres/tests/Unit/Lowering/Query/TableFunctionRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Lowering\Query\TableFunctionRule::class)]
12#[Medium]
13final class TableFunctionRuleTest extends TestCase
14{
15    public function testXmlLowersTheNamespaces(): void
16    {
17        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
18        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse("SELECT * FROM XMLTABLE (XMLNAMESPACES ('u' AS x, DEFAULT 'v'), '/r' PASSING '<r/>' COLUMNS n FOR ORDINALITY, a text PATH 'a' DEFAULT 'z' NOT NULL, b int)");
19        $rule = new \SqlSemantics\Platform\PostgreSql\Lowering\Query\TableFunctionRule($lowering);
20        self::assertCount(2, $rule->xml($tree->find('xmltable')[0], [null, []], false)->namespaces);
21    }
22
23    public function testNamespacesReadsDefault(): void
24    {
25        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
26        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse("SELECT * FROM XMLTABLE (XMLNAMESPACES ('u' AS x, DEFAULT 'v'), '/r' PASSING '<r/>' COLUMNS n FOR ORDINALITY, a text PATH 'a' DEFAULT 'z' NOT NULL, b int)");
27        $rule = new \SqlSemantics\Platform\PostgreSql\Lowering\Query\TableFunctionRule($lowering);
28        self::assertNull($rule->namespaces($tree->find('xml_namespace_list')[0])[1]->prefix);
29    }
30
31    public function testXmlColumnsReadsOrdinality(): void
32    {
33        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
34        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse("SELECT * FROM XMLTABLE (XMLNAMESPACES ('u' AS x, DEFAULT 'v'), '/r' PASSING '<r/>' COLUMNS n FOR ORDINALITY, a text PATH 'a' DEFAULT 'z' NOT NULL, b int)");
35        $rule = new \SqlSemantics\Platform\PostgreSql\Lowering\Query\TableFunctionRule($lowering);
36        self::assertTrue($rule->xmlColumns($tree->find('xmltable_column_list')[0])[0]->ordinality());
37    }
38
39    public function testOptionsKeepsTheWrittenOrder(): void
40    {
41        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
42        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse("SELECT * FROM XMLTABLE (XMLNAMESPACES ('u' AS x, DEFAULT 'v'), '/r' PASSING '<r/>' COLUMNS n FOR ORDINALITY, a text PATH 'a' DEFAULT 'z' NOT NULL, b int)");
43        $rule = new \SqlSemantics\Platform\PostgreSql\Lowering\Query\TableFunctionRule($lowering);
44        self::assertSame([\SqlSemantics\Platform\PostgreSql\Statement\Relation\Xml\XmlColumnOptionKind::Path, \SqlSemantics\Platform\PostgreSql\Statement\Relation\Xml\XmlColumnOptionKind::Default, \SqlSemantics\Platform\PostgreSql\Statement\Relation\Xml\XmlColumnOptionKind::NotNull], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Statement\Relation\Xml\XmlColumnOption $option): \SqlSemantics\Platform\PostgreSql\Statement\Relation\Xml\XmlColumnOptionKind => $option->kind, $rule->options($tree->find('xmltable_column_option_list')[0])));
45    }
46
47    public function testJsonLowersTheColumns(): void
48    {
49        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
50        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse("SELECT * FROM JSON_TABLE ('[]', '\$[*]' COLUMNS (n FOR ORDINALITY, v integer PATH '\$.v', f jsonb FORMAT JSON, e boolean EXISTS, NESTED '\$.b' AS q COLUMNS (b text)))");
51        $rule = new \SqlSemantics\Platform\PostgreSql\Lowering\Query\TableFunctionRule($lowering);
52        self::assertCount(5, $rule->json($tree->find('json_table')[0], [null, []], false)->columns);
53    }
54
55    public function testJsonColumnsLowersNestedColumns(): void
56    {
57        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
58        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse("SELECT * FROM JSON_TABLE ('[]', '\$[*]' COLUMNS (n FOR ORDINALITY, v integer PATH '\$.v', f jsonb FORMAT JSON, e boolean EXISTS, NESTED '\$.b' AS q COLUMNS (b text)))");
59        $rule = new \SqlSemantics\Platform\PostgreSql\Lowering\Query\TableFunctionRule($lowering);
60        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Relation\Json\JsonNestedColumns::class, $rule->jsonColumns($tree->find('json_table_column_definition_list')[0])[4]);
61    }
62
63    public function testValueReadsThePath(): void
64    {
65        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
66        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse("SELECT * FROM JSON_TABLE ('[]', '\$[*]' COLUMNS (n FOR ORDINALITY, v integer PATH '\$.v', f jsonb FORMAT JSON, e boolean EXISTS, NESTED '\$.b' AS q COLUMNS (b text)))");
67        $rule = new \SqlSemantics\Platform\PostgreSql\Lowering\Query\TableFunctionRule($lowering);
68        self::assertSame('$.v', $rule->value($lowering->productions->form($tree->find('json_table_column_definition')[1]), null, 2)->path?->value);
69    }
70
71    public function testNestedReadsThePathName(): void
72    {
73        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
74        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse("SELECT * FROM JSON_TABLE ('[]', '\$[*]' COLUMNS (n FOR ORDINALITY, v integer PATH '\$.v', f jsonb FORMAT JSON, e boolean EXISTS, NESTED '\$.b' AS q COLUMNS (b text)))");
75        $rule = new \SqlSemantics\Platform\PostgreSql\Lowering\Query\TableFunctionRule($lowering);
76        self::assertSame('q', $rule->nested($lowering->productions->form($tree->find('json_table_column_definition')[4]), 7, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('q'))->pathName?->value);
77    }
78
79    public function testPathIsNullWithoutClause(): void
80    {
81        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
82        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse("SELECT * FROM JSON_TABLE ('[]', '\$[*]' COLUMNS (n FOR ORDINALITY, v integer PATH '\$.v', f jsonb FORMAT JSON, e boolean EXISTS, NESTED '\$.b' AS q COLUMNS (b text)))");
83        $rule = new \SqlSemantics\Platform\PostgreSql\Lowering\Query\TableFunctionRule($lowering);
84        self::assertNull($rule->path($tree->find('json_table_column_path_clause_opt')[2]));
85    }
86
87    public function testClauseRefusesANodeThatIsNoClause(): void
88    {
89        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
90        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse("SELECT * FROM JSON_TABLE ('[]', '\$[*]' COLUMNS (n FOR ORDINALITY, v integer PATH '\$.v', f jsonb FORMAT JSON, e boolean EXISTS, NESTED '\$.b' AS q COLUMNS (b text)))");
91        $rule = new \SqlSemantics\Platform\PostgreSql\Lowering\Query\TableFunctionRule($lowering);
92        $this->expectExceptionMessage('No semantic rule is implemented for: json_table:');
93        $rule->clause(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\Json\JsonOrdinalityColumn(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('n')), $lowering->productions->form($tree->find('json_table')[0]));
94    }
95}
96