packages/sql-semantics-mysql/tests/Unit/Lowering/Call/WindowRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Call;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Call\FrameRule;
11use SqlSemantics\Platform\MySql\Lowering\Call\WindowRule;
12use SqlSemantics\Platform\MySql\Lowering\Lowering;
13use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
14use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
15use SqlSemantics\Platform\MySql\Platform;
16use SqlSemantics\Platform\MySql\Statement\Call\Window\CountingEdge;
17use SqlSemantics\Platform\MySql\Statement\Call\Window\NullTreatment;
18use SqlSemantics\Platform\MySql\Statement\Call\Window\RoutineVariable;
19use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowFunctionKind;
20use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowSpec;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22use SqlSemantics\Platform\MySql\Statement\Literal\Parameter;
23use SqlSemantics\Platform\MySql\Statement\Variable\UserVariable;
24
25#[CoversClass(WindowRule::class)]
26#[Medium]
27final class WindowRuleTest extends TestCase
28{
29 public function testFunctionLowersTheValueFunctions(): void
30 {
31 $platform = new Platform();
32 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
33 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
34 $node = $platform->parser($profile)->parse('SELECT NTH_VALUE(a, 2) FROM LAST IGNORE NULLS OVER w')->find('window_func_call')[0];
35 $rule = new WindowRule($lowering, new FrameRule($lowering));
36 $call = $rule->function($node);
37
38 self::assertSame(WindowFunctionKind::NthValue, $call->kind);
39 self::assertSame(CountingEdge::Last, $call->edge);
40 self::assertSame(NullTreatment::Ignore, $call->nulls);
41 }
42
43 public function testFunctionReportsAnotherNode(): void
44 {
45 $platform = new Platform();
46 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
47 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
48 $node = $platform->parser($profile)->parse('SELECT 1')->find('select_item_list')[0];
49 $this->expectExceptionMessage('No semantic rule is implemented for: ');
50
51 $rule = new WindowRule($lowering, new FrameRule($lowering));
52 $rule->function($node);
53 }
54
55 public function testOffsetLowersTheOffsetAndTheDefault(): void
56 {
57 $platform = new Platform();
58 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
59 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
60 $node = $platform->parser($profile)->parse('SELECT LEAD(a, ?, 0) OVER w')->find('opt_lead_lag_info')[0];
61 $rule = new WindowRule($lowering, new FrameRule($lowering));
62 $arguments = $rule->offset($node);
63
64 self::assertInstanceOf(Parameter::class, $arguments[0]);
65 self::assertCount(2, $arguments);
66 }
67
68 public function testStableLowersEachForm(): void
69 {
70 $platform = new Platform();
71 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
72 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
73 $list = $platform->parser($profile)->parse('SELECT NTILE(n) OVER w, NTILE(@v) OVER w, NTILE(4) OVER w')->find('select_item_list')[0];
74 $rule = new WindowRule($lowering, new FrameRule($lowering));
75 $integers = $list->find('stable_integer');
76
77 self::assertInstanceOf(RoutineVariable::class, $rule->stable($integers[0]));
78 self::assertInstanceOf(UserVariable::class, $rule->stable($integers[1]));
79 self::assertInstanceOf(NumberLiteral::class, $rule->stable($integers[2]));
80 }
81
82 public function testNullsLowersNoTreatment(): void
83 {
84 $platform = new Platform();
85 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
86 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
87 $node = $platform->parser($profile)->parse('SELECT LAG(a) OVER w')->find('opt_null_treatment')[0];
88 $rule = new WindowRule($lowering, new FrameRule($lowering));
89 self::assertNull($rule->nulls($node));
90 }
91
92 public function testEdgeLowersFromFirst(): void
93 {
94 $platform = new Platform();
95 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
96 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
97 $node = $platform->parser($profile)->parse('SELECT NTH_VALUE(a, 1) FROM FIRST OVER w')->find('opt_from_first_last')[0];
98 $rule = new WindowRule($lowering, new FrameRule($lowering));
99 self::assertSame(CountingEdge::First, $rule->edge($node));
100 }
101
102 public function testWindowingLowersNoWindow(): void
103 {
104 $platform = new Platform();
105 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
106 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
107 $node = $platform->parser($profile)->parse('SELECT SUM(a)')->find('opt_windowing_clause')[0];
108 $rule = new WindowRule($lowering, new FrameRule($lowering));
109 self::assertNull($rule->windowing($node));
110 }
111
112 public function testWindowLowersASpecification(): void
113 {
114 $platform = new Platform();
115 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
116 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
117 $node = $platform->parser($profile)->parse('SELECT RANK() OVER (w ROWS CURRENT ROW)')->find('windowing_clause')[0];
118 $rule = new WindowRule($lowering, new FrameRule($lowering));
119 self::assertInstanceOf(WindowSpec::class, $rule->window($node));
120 }
121
122 public function testNameLowersAWindowName(): void
123 {
124 $platform = new Platform();
125 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
126 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
127 $node = $platform->parser($profile)->parse('SELECT RANK() OVER w')->find('window_name')[0];
128 $rule = new WindowRule($lowering, new FrameRule($lowering));
129 self::assertSame('w', $rule->name($node)->value);
130 }
131}
132