packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/WindowSpecificationTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FrameBound;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FrameBoundKind;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FrameMode;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\WindowProblem;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\WindowProblemKind;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\WindowFrame;
24use SqlSemantics\Platform\PostgreSql\Statement\Invocation\WindowSpecification;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
27use SqlSemantics\Platform\PostgreSql\Statement\Query\SortDirection;
28use SqlSemantics\Platform\PostgreSql\Statement\Query\SortItem;
29
30#[CoversClass(WindowSpecification::class)]
31#[Small]
32final class WindowSpecificationTest extends TestCase
33{
34 public function testDeriveClauseDerivesPartitionOrderAndFrame(): void
35 {
36 $partition = new Constant(new IntegerConstant('1'));
37 $key = new Constant(new IntegerConstant('2'));
38 $offset = new Constant(new IntegerConstant('3'));
39 $window = new WindowSpecification(null, [$partition], [new SortItem($key)], new WindowFrame(FrameMode::Rows, new FrameBound(FrameBoundKind::OffsetPreceding, $offset)));
40 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
41 $window->deriveClause($derivation, $derivation->environment());
42 self::assertSame([true, true, true], [$derivation->facts()->covers($partition), $derivation->facts()->covers($key), $derivation->facts()->covers($offset)]);
43 }
44
45 public function testDeriveClauseReportsAFrameTheOrderingCannotSupport(): void
46 {
47 $window = new WindowSpecification(null, [], [], new WindowFrame(FrameMode::Groups, new FrameBound(FrameBoundKind::CurrentRow)));
48 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
49 $window->deriveClause($derivation, $derivation->environment());
50 self::assertEquals([new WindowProblem(WindowProblemKind::GroupsWithoutOrder)], $derivation->facts()->diagnostics);
51 }
52
53 public function testRenderWritesTheSpecificationInParentheses(): void
54 {
55 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
56 (new WindowSpecification(new Name('w'), [new Constant(new IntegerConstant('1'))], [new SortItem(new Constant(new IntegerConstant('2')), SortDirection::Descending)], new WindowFrame(FrameMode::Rows, new FrameBound(FrameBoundKind::UnboundedPreceding))))->render($out);
57 self::assertSame('(w PARTITION BY 1 ORDER BY 2 DESC ROWS UNBOUNDED PRECEDING)', (new Lexical())->join($out->pieces()));
58 $second = new Output(new Codec(GrammarRelease::PostgreSql172));
59 (new WindowSpecification())->render($second);
60 self::assertSame('()', (new Lexical())->join($second->pieces()));
61 }
62
63 public function testRenderQuotesAnExistingWindowNamedLikeAFrameKeyword(): void
64 {
65 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
66 (new WindowSpecification(new Name('rows')))->render($out);
67 self::assertSame('("rows")', (new Lexical())->join($out->pieces()));
68 }
69}
70