packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Window/FrameTest.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\FrameExclusion;
19use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\FrameUnit;
20use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\WindowSpec;
21use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
22use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
23
24#[CoversClass(Frame::class)]
25#[Medium]
26final class FrameTest extends TestCase
27{
28 public function testReadsBothEndsAndTheExclusion(): void
29 {
30 $semantics = new Semantics();
31 $create = $semantics->analyze('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
32 $statement = $semantics->analyze('SELECT sum(a) OVER (ROWS BETWEEN 1 PRECEDING AND CURRENT ROW EXCLUDE TIES), sum(a) OVER (RANGE UNBOUNDED PRECEDING) FROM t', [$create])->statement;
33
34 self::assertInstanceOf(Select::class, $statement);
35 self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
36 self::assertInstanceOf(ResultColumn::class, $statement->columns[1]);
37 self::assertInstanceOf(FunctionCall::class, $statement->columns[0]->expression);
38 self::assertInstanceOf(FunctionCall::class, $statement->columns[1]->expression);
39 self::assertInstanceOf(WindowSpec::class, $statement->columns[0]->expression->over);
40 self::assertInstanceOf(WindowSpec::class, $statement->columns[1]->expression->over);
41 $between = $statement->columns[0]->expression->over->frame;
42 $short = $statement->columns[1]->expression->over->frame;
43 self::assertNotNull($between);
44 self::assertNotNull($short);
45 self::assertSame(FrameUnit::Rows, $between->unit);
46 self::assertSame(FrameBoundKind::Preceding, $between->start->kind);
47 self::assertSame(FrameBoundKind::CurrentRow, $between->end?->kind);
48 self::assertSame(FrameExclusion::Ties, $between->exclusion);
49 self::assertSame(FrameUnit::Range, $short->unit);
50 self::assertSame(FrameBoundKind::UnboundedPreceding, $short->start->kind);
51 self::assertNull($short->end);
52 self::assertNull($short->exclusion);
53 }
54
55 public function testRenderWritesEveryFormOfFrame(): void
56 {
57 $semantics = new Semantics();
58 $operation = $semantics->analyze('select sum(a) over (rows between 1 preceding and current row) AS c1, sum(a) over (range unbounded preceding exclude no others) AS c2, sum(a) over (groups between current row and unbounded following exclude current row) AS c3, sum(a) over (rows between 2 following and 3 following exclude group) AS c4, sum(a) over (rows 1 preceding exclude ties) AS c5 from t');
59
60 self::assertSame('SELECT sum(a) OVER (ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS c1, sum(a) OVER (RANGE UNBOUNDED PRECEDING EXCLUDE NO OTHERS) AS c2, sum(a) OVER (GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) AS c3, sum(a) OVER (ROWS BETWEEN 2 FOLLOWING AND 3 FOLLOWING EXCLUDE GROUP) AS c4, sum(a) OVER (ROWS 1 PRECEDING EXCLUDE TIES) AS c5 FROM t', $operation->toString());
61 }
62
63 public function testRenderWritesANewlyBuiltFrame(): void
64 {
65 $semantics = new Semantics();
66 $frame = new Frame(FrameUnit::Range, new FrameBound(FrameBoundKind::UnboundedPreceding), new FrameBound(FrameBoundKind::Following, new IntegerLiteral('2')), FrameExclusion::NoOthers);
67 $operation = new Operation($semantics->context(), new Select([new ResultColumn(new FunctionCall(new Name('sum'), [new IntegerLiteral('1')], false, null, [], null, new WindowSpec(null, [], [], $frame)))]));
68
69 self::assertSame('SELECT sum(1) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING EXCLUDE NO OTHERS)', $operation->toString());
70 self::assertSame([], $operation->facts->diagnostics);
71 }
72}
73