packages/sql-semantics-postgres/tests/Unit/Lowering/Invocation/CallRuleTest.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\CallRule;
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\Diagnostic\AnalysisException;
16use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
17use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
18use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
21
22#[CoversClass(CallRule::class)]
23#[Small]
24final class CallRuleTest extends TestCase
25{
26    public function testCallLowersEveryCallForm(): void
27    {
28        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
29        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT count(*), f(ALL 1), f(DISTINCT 1), f(VARIADIC 1), f(1, VARIADIC 2), f()');
30        $node = array_map(static fn ($target) => (new CallRule($lowering))->call($target->find('func_expr')[0]), $tree->find('target_list')[0]->find('target_el'));
31        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
32        $out->list($node);
33        self::assertSame('count(*), f(1), f(DISTINCT 1), f(VARIADIC 1), f(1, VARIADIC 2), f()', (new Lexical())->join($out->pieces()));
34    }
35
36    public function testCallLowersAWindowlessCall(): void
37    {
38        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
39        $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE INDEX ON t (lower(a))');
40        $node = (new CallRule($lowering))->call($tree->find('func_expr_windowless')[0]);
41        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
42        $out->node($node);
43        self::assertSame('lower(a)', (new Lexical())->join($out->pieces()));
44    }
45
46    public function testApplicationLowersTheNameAndArguments(): void
47    {
48        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
49        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT s.f(a => 1)');
50        $node = (new CallRule($lowering))->application($tree->find('func_application')[0]);
51        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
52        $out->node($node);
53        self::assertSame('s.f(a => 1)', (new Lexical())->join($out->pieces()));
54    }
55
56    public function testApplicationRejectsDistinctWithinGroup(): void
57    {
58        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
59        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f(DISTINCT 1) WITHIN GROUP (ORDER BY 1)');
60        $this->expectException(AnalysisException::class);
61        $this->expectExceptionMessage('cannot use DISTINCT with WITHIN GROUP');
62        (new CallRule($lowering))->call($tree->find('func_expr')[0]);
63    }
64
65    public function testWindowedLowersWithinGroup(): void
66    {
67        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
68        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT mode() WITHIN GROUP (ORDER BY 1)');
69        $node = (new CallRule($lowering))->call($tree->find('func_expr')[0]);
70        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
71        $out->node($node);
72        self::assertSame('mode() WITHIN GROUP (ORDER BY 1)', (new Lexical())->join($out->pieces()));
73    }
74
75    public function testWithinGroupLowersNoClauseToNoOrdering(): 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()');
79        $node = (new CallRule($lowering))->withinGroup($tree->find('within_group_clause')[0]);
80        self::assertSame([], $node);
81    }
82
83    public function testFilterLowersNoClauseToNull(): void
84    {
85        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
86        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f()');
87        $node = (new CallRule($lowering))->filter($tree->find('filter_clause')[0]);
88        self::assertNull($node);
89    }
90
91    public function testArgumentsLowersAnEmptyOptionalList(): void
92    {
93        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
94        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT SUBSTRING()');
95        $node = (new CallRule($lowering))->arguments($tree->find('func_arg_list_opt')[0]);
96        self::assertSame([], $node);
97    }
98
99    public function testArgumentLowersTheAssignmentSpelling(): void
100    {
101        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
102        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f(a := 1)');
103        $node = (new CallRule($lowering))->argument($tree->find('func_arg_expr')[0]);
104        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
105        $out->node($node);
106        self::assertSame('a := 1', (new Lexical())->join($out->pieces()));
107    }
108
109    public function testOrderLowersNoOrdering(): void
110    {
111        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
112        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f(1)');
113        $node = (new CallRule($lowering))->order($tree->find('opt_sort_clause')[0]);
114        self::assertSame([], $node);
115    }
116}
117