packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Window/WindowSpecTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Window;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
12use SqlSemantics\Platform\Sqlite\Semantics;
13use SqlSemantics\Platform\Sqlite\Statement\Expression\FunctionCall;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\Frame;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\FrameBound;
17use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\FrameBoundKind;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\FrameUnit;
19use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\WindowSpec;
20use SqlSemantics\Platform\Sqlite\Statement\Query\Ordering\SortTerm;
21use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
22use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
23
24#[CoversClass(WindowSpec::class)]
25#[Medium]
26final class WindowSpecTest extends TestCase
27{
28    public function testReadsEveryPartOfAnAnalyzedWindow(): void
29    {
30        $semantics = new Semantics();
31        $create = $semantics->analyze('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
32        $operation = $semantics->analyze('SELECT rank() OVER (w PARTITION BY a ORDER BY b DESC NULLS LAST ROWS 1 PRECEDING) FROM t WINDOW w AS ()', [$create]);
33        $statement = $operation->statement;
34
35        self::assertInstanceOf(Select::class, $statement);
36        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
37        self::assertInstanceOf(FunctionCall::class, $statement->columns[0]->expression);
38        self::assertInstanceOf(WindowSpec::class, $statement->columns[0]->expression->over);
39        $window = $statement->columns[0]->expression->over;
40        self::assertSame('w', $window->base?->value);
41        self::assertCount(1, $window->partition);
42        self::assertCount(1, $window->order);
43        self::assertSame(FrameUnit::Rows, $window->frame?->unit);
44        self::assertNull($statement->windows[0]->window->base);
45        self::assertSame([], $statement->windows[0]->window->partition);
46        self::assertNull($statement->windows[0]->window->frame);
47        self::assertSame([], $operation->facts->diagnostics);
48    }
49
50    public function testExpressionsListsPartitionOrderingAndOffsetsInWrittenOrder(): void
51    {
52        $semantics = new Semantics();
53        $create = $semantics->analyze('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
54        $operation = $semantics->analyze('SELECT sum(a) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 2 FOLLOWING) FROM t', [$create]);
55        $statement = $operation->statement;
56
57        self::assertInstanceOf(Select::class, $statement);
58        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
59        self::assertInstanceOf(FunctionCall::class, $statement->columns[0]->expression);
60        self::assertInstanceOf(WindowSpec::class, $statement->columns[0]->expression->over);
61        $window = $statement->columns[0]->expression->over;
62        $expressions = $window->expressions();
63        self::assertCount(4, $expressions);
64        self::assertNotNull($window->frame);
65        self::assertNotNull($window->frame->end);
66        self::assertSame($window->partition[0], $expressions[0]);
67        self::assertSame($window->order[0]->expression, $expressions[1]);
68        self::assertSame($window->frame->start->offset, $expressions[2]);
69        self::assertSame($window->frame->end->offset, $expressions[3]);
70        self::assertTrue($operation->facts->covers($expressions[2]));
71        self::assertTrue($operation->facts->covers($expressions[3]));
72    }
73
74    public function testExpressionsIsEmptyForAnEmptyWindow(): void
75    {
76        self::assertSame([], (new WindowSpec())->expressions());
77        self::assertSame([], (new WindowSpec(new Name('w'), [], [], new Frame(FrameUnit::Rows, new FrameBound(FrameBoundKind::CurrentRow))))->expressions());
78    }
79
80    public function testRenderWritesThePartsWithoutParentheses(): void
81    {
82        $operation = (new Semantics())->analyze('select rank() over () AS c1, rank() over (w) AS c2, rank() over (partition by a, b order by a desc nulls last, b) AS c3, sum(a) over (w order by a rows 1 preceding) AS c4 from t window w as ()');
83
84        self::assertSame('SELECT rank() OVER () AS c1, rank() OVER (w) AS c2, rank() OVER (PARTITION BY a, b ORDER BY a DESC NULLS LAST, b) AS c3, sum(a) OVER (w ORDER BY a ROWS 1 PRECEDING) AS c4 FROM t WINDOW w AS ()', $operation->toString());
85    }
86
87    public function testRenderWritesANewlyBuiltSpecification(): void
88    {
89        $semantics = new Semantics();
90        $window = new WindowSpec(null, [new IntegerLiteral('1')], [new SortTerm(new IntegerLiteral('2'))], new Frame(FrameUnit::Groups, new FrameBound(FrameBoundKind::CurrentRow)));
91        $operation = new Operation($semantics->context(), new Select([new ResultColumn(new FunctionCall(new Name('sum'), [new IntegerLiteral('1')], false, null, [], null, $window))]));
92
93        self::assertSame('SELECT sum(1) OVER (PARTITION BY 1 ORDER BY 2 GROUPS CURRENT ROW)', $operation->toString());
94    }
95}
96