packages/sql-semantics-postgres/tests/Unit/Lowering/Invocation/InvocationsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Invocation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Invocation\Invocations;
12use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
13use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
15use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
17use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonNullHandling;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\NamedArgument;
23
24#[CoversClass(Invocations::class)]
25#[Small]
26final class InvocationsTest extends TestCase
27{
28    public function testCallLowersAFunctionCall(): void
29    {
30        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
31        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f(1, x => 2) FILTER (WHERE TRUE) OVER w');
32        $node = $lowering->invocations->call($tree->find('func_expr')[0]);
33        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
34        $out->node($node);
35        self::assertSame('f(1, x => 2) FILTER (WHERE TRUE) OVER w', (new Lexical())->join($out->pieces()));
36    }
37
38    public function testArgumentsLowersAnArgumentList(): void
39    {
40        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
41        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f(1, x := 2)');
42        $node = $lowering->invocations->arguments($tree->find('func_arg_list')[0]);
43        self::assertCount(2, $node);
44        self::assertInstanceOf(NamedArgument::class, $node[1]);
45    }
46
47    public function testSubexpressionLowersASqlSyntaxFunction(): void
48    {
49        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
50        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT CURRENT_TIME(2)');
51        $node = $lowering->invocations->subexpression($tree->find('func_expr_common_subexpr')[0]);
52        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
53        $out->node($node);
54        self::assertSame('CURRENT_TIME(2)', (new Lexical())->join($out->pieces()));
55    }
56
57    public function testWindowLowersAWindowSpecification(): void
58    {
59        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
60        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f() OVER (w PARTITION BY 1 ROWS 1 PRECEDING EXCLUDE TIES)');
61        $node = $lowering->invocations->window($tree->find('window_specification')[0]);
62        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
63        $out->node($node);
64        self::assertSame('(w PARTITION BY 1 ROWS 1 PRECEDING EXCLUDE TIES)', (new Lexical())->join($out->pieces()));
65    }
66
67    public function testOverLowersAWindowName(): void
68    {
69        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
70        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f() OVER w');
71        $node = $lowering->invocations->over($tree->find('over_clause')[0]);
72        self::assertEquals(new Name('w'), $node);
73    }
74
75    public function testFilterLowersTheCondition(): void
76    {
77        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
78        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f() FILTER (WHERE TRUE)');
79        $node = $lowering->invocations->filter($tree->find('filter_clause')[0]);
80        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
81        $out->node($node);
82        self::assertSame('TRUE', (new Lexical())->join($out->pieces()));
83    }
84
85    public function testXmlPassingLowersThePassingClause(): void
86    {
87        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
88        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT XMLEXISTS(\'//a\' PASSING BY VALUE \'<a/>\' BY REF)');
89        $node = $lowering->invocations->xmlPassing($tree->find('xmlexists_argument')[0]);
90        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
91        $out->node($node);
92        self::assertSame('PASSING \'<a/>\'', (new Lexical())->join($out->pieces()));
93    }
94
95    public function testXmlAttributesLowersTheList(): void
96    {
97        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
98        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT XMLFOREST(1 AS a, 2)');
99        $node = $lowering->invocations->xmlAttributes($tree->find('xml_attribute_list')[0]);
100        self::assertCount(2, $node);
101        self::assertNull($node[1]->name);
102    }
103
104    public function testJsonValueLowersAFormattedValue(): void
105    {
106        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
107        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_SCALAR(1), JSON(\'{}\' FORMAT JSON)');
108        $node = $lowering->invocations->jsonValue($tree->find('json_value_expr')[0]);
109        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
110        $out->node($node);
111        self::assertSame('\'{}\' FORMAT JSON', (new Lexical())->join($out->pieces()));
112    }
113
114    public function testJsonPassingLowersTheArguments(): void
115    {
116        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
117        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_EXISTS(\'{}\', \'$.a\' PASSING 1 AS x, 2 AS y)');
118        $node = $lowering->invocations->jsonPassing($tree->find('json_passing_clause_opt')[0]);
119        self::assertCount(2, $node);
120        self::assertSame('y', $node[1]->name->value);
121    }
122
123    public function testJsonFormatLowersNoClauseToNull(): void
124    {
125        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
126        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON(\'{}\')');
127        $node = $lowering->invocations->jsonFormat($tree->find('json_format_clause_opt')[0]);
128        self::assertNull($node);
129    }
130
131    public function testJsonReturningLowersTheType(): void
132    {
133        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
134        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_SERIALIZE(\'{}\' RETURNING bytea)');
135        $node = $lowering->invocations->jsonReturning($tree->find('json_returning_clause_opt')[0]);
136        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
137        $out->node($node);
138        self::assertSame('RETURNING bytea', (new Lexical())->join($out->pieces()));
139    }
140
141    public function testJsonKeyValueLowersAPair(): void
142    {
143        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
144        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_OBJECT(\'a\' VALUE 1)');
145        $node = $lowering->invocations->jsonKeyValue($tree->find('json_name_and_value')[0]);
146        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
147        $out->node($node);
148        self::assertSame('\'a\' VALUE 1', (new Lexical())->join($out->pieces()));
149    }
150
151    public function testJsonNullsLowersTheNullClause(): void
152    {
153        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
154        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_ARRAY(1 NULL ON NULL)');
155        $node = $lowering->invocations->jsonNulls($tree->find('json_array_constructor_null_clause_opt')[0]);
156        self::assertSame(JsonNullHandling::Keep, $node);
157    }
158
159    public function testJsonBehaviorLowersBothBehaviors(): void
160    {
161        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
162        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_VALUE(\'{}\', \'$\' DEFAULT 1 ON EMPTY ERROR ON ERROR)');
163        $node = $lowering->invocations->jsonBehavior($tree->find('json_behavior_clause_opt')[0]);
164        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
165        $out->node($node);
166        self::assertSame('DEFAULT 1 ON EMPTY ERROR ON ERROR', (new Lexical())->join($out->pieces()));
167    }
168
169    public function testJsonWrapperDropsTheNoiseWords(): void
170    {
171        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
172        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_QUERY(\'{}\', \'$\' WITH UNCONDITIONAL ARRAY WRAPPER)');
173        $node = $lowering->invocations->jsonWrapper($tree->find('json_wrapper_behavior')[0]);
174        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
175        $out->node($node);
176        self::assertSame('WITH WRAPPER', (new Lexical())->join($out->pieces()));
177    }
178
179    public function testJsonQuotesDropsTheNoiseWords(): void
180    {
181        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
182        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_QUERY(\'{}\', \'$\' OMIT QUOTES ON SCALAR STRING)');
183        $node = $lowering->invocations->jsonQuotes($tree->find('json_quotes_clause_opt')[0]);
184        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
185        $out->node($node);
186        self::assertSame('OMIT QUOTES', (new Lexical())->join($out->pieces()));
187    }
188
189    public function testJsonKeyUniquenessLowersTheClause(): void
190    {
191        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
192        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_OBJECT(\'a\' : 1 WITHOUT UNIQUE)');
193        $node = $lowering->invocations->jsonKeyUniqueness($tree->find('json_key_uniqueness_constraint_opt')[0]);
194        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
195        $out->node($node);
196        self::assertSame('WITHOUT UNIQUE KEYS', (new Lexical())->join($out->pieces()));
197    }
198}
199