packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Window/FrameBoundTest.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\BindParameter;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\FunctionCall;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\ParameterPrefix;
17use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\Frame;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\FrameBound;
19use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\FrameBoundKind;
20use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\FrameUnit;
21use SqlSemantics\Platform\Sqlite\Statement\Expression\Window\WindowSpec;
22use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
23use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
24
25#[CoversClass(FrameBound::class)]
26#[Medium]
27final class FrameBoundTest extends TestCase
28{
29 public function testReadsTheOffsetOfAnAnalyzedBoundary(): void
30 {
31 $semantics = new Semantics();
32 $create = $semantics->analyze('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
33 $operation = $semantics->analyze('SELECT sum(a) OVER (ROWS BETWEEN 2 PRECEDING AND ? FOLLOWING) FROM t', [$create]);
34 $statement = $operation->statement;
35
36 self::assertInstanceOf(Select::class, $statement);
37 self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
38 self::assertInstanceOf(FunctionCall::class, $statement->columns[0]->expression);
39 self::assertInstanceOf(WindowSpec::class, $statement->columns[0]->expression->over);
40 $frame = $statement->columns[0]->expression->over->frame;
41 self::assertNotNull($frame);
42 self::assertInstanceOf(IntegerLiteral::class, $frame->start->offset);
43 self::assertSame('2', $frame->start->offset->digits);
44 self::assertSame(FrameBoundKind::Following, $frame->end?->kind);
45 self::assertInstanceOf(BindParameter::class, $frame->end->offset);
46 self::assertTrue($operation->facts->covers($frame->start->offset));
47 self::assertTrue($operation->facts->covers($frame->end->offset));
48 }
49
50 public function testRenderWritesEveryKindOfBoundary(): void
51 {
52 $operation = (new Semantics())->analyze('select sum(a) over (rows between unbounded preceding and 1 preceding) AS c1, sum(a) over (rows between current row and 1 following) AS c2, sum(a) over (rows between 1 following and unbounded following) AS c3 from t');
53
54 self::assertSame('SELECT sum(a) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS c1, sum(a) OVER (ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS c2, sum(a) OVER (ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) AS c3 FROM t', $operation->toString());
55 }
56
57 public function testRenderWritesANewlyBuiltBoundary(): void
58 {
59 $semantics = new Semantics();
60 $frame = new Frame(FrameUnit::Rows, new FrameBound(FrameBoundKind::Preceding, new BindParameter(ParameterPrefix::Colon, 'n')), new FrameBound(FrameBoundKind::UnboundedFollowing));
61 $operation = new Operation($semantics->context(), new Select([new ResultColumn(new FunctionCall(new Name('sum'), [new IntegerLiteral('1')], false, null, [], null, new WindowSpec(null, [], [], $frame)))]));
62
63 self::assertSame('SELECT sum(1) OVER (ROWS BETWEEN :n PRECEDING AND UNBOUNDED FOLLOWING)', $operation->toString());
64 }
65}
66