packages/sql-semantics-postgres/tests/Unit/Lowering/Invocation/WindowRuleTest.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\WindowRule;
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;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FrameBoundKind;
22
23#[CoversClass(WindowRule::class)]
24#[Small]
25final class WindowRuleTest extends TestCase
26{
27    public function testOverLowersNoClauseToNull(): void
28    {
29        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
30        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f()');
31        $node = (new WindowRule($lowering))->over($tree->find('over_clause')[0]);
32        self::assertNull($node);
33    }
34
35    public function testSpecificationLowersEveryPart(): void
36    {
37        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
38        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f() OVER (w PARTITION BY 1, 2 GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS)');
39        $node = (new WindowRule($lowering))->specification($tree->find('window_specification')[0]);
40        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
41        $out->node($node);
42        self::assertSame('(w PARTITION BY 1, 2 GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)', (new Lexical())->join($out->pieces()));
43    }
44
45    public function testFrameLowersNoFrameToNull(): void
46    {
47        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
48        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f() OVER ()');
49        $node = (new WindowRule($lowering))->frame($tree->find('opt_frame_clause')[0]);
50        self::assertNull($node);
51    }
52
53    public function testFrameRejectsAFrameEndingBeforeItStarts(): void
54    {
55        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
56        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f() OVER (ROWS BETWEEN CURRENT ROW AND 1 PRECEDING)');
57        $this->expectException(AnalysisException::class);
58        $this->expectExceptionMessage('frame starting from current row cannot have preceding rows');
59        (new WindowRule($lowering))->frame($tree->find('opt_frame_clause')[0]);
60    }
61
62    public function testCheckRejectsAStartAfterTheCurrentRowAlone(): void
63    {
64        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
65        $this->expectException(AnalysisException::class);
66        $this->expectExceptionMessage('frame starting from following row cannot end with current row');
67        (new WindowRule($lowering))->check(FrameBoundKind::OffsetFollowing, null);
68    }
69
70    public function testBoundLowersAnOffset(): void
71    {
72        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
73        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT f() OVER (RANGE BETWEEN 3 FOLLOWING AND 4 FOLLOWING)');
74        $node = (new WindowRule($lowering))->bound($tree->find('frame_bound')[0]);
75        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
76        $out->node($node);
77        self::assertSame('3 FOLLOWING', (new Lexical())->join($out->pieces()));
78    }
79}
80