packages/sql-semantics-mysql/tests/Unit/Lowering/Call/CallRulesTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Call;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Call\CallRules;
11use SqlSemantics\Platform\MySql\Lowering\Lowering;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Statement\Call\Aggregate\Aggregate;
16use SqlSemantics\Platform\MySql\Statement\Call\Clock;
17use SqlSemantics\Platform\MySql\Statement\Call\ClockCall;
18use SqlSemantics\Platform\MySql\Statement\Call\Json\JsonValueCall;
19use SqlSemantics\Platform\MySql\Statement\Call\KeywordCall;
20use SqlSemantics\Platform\MySql\Statement\Call\Weight\WeightString;
21use SqlSemantics\Platform\MySql\Statement\Call\Window\FrameBoundKind;
22use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowFunction;
23use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowSpec;
24
25#[CoversClass(CallRules::class)]
26#[Small]
27final class CallRulesTest extends TestCase
28{
29    public function testCallDispatchesEveryFamilyOfCalls(): 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        $list = $platform->parser($profile)->parse('SELECT GEOMETRYCOLLECTION(), GROUPING(a), COUNT(*), ROW_NUMBER() OVER w, NOW(), WEIGHT_STRING(a)')->find('select_item_list')[0];
35        $rules = new CallRules($lowering);
36
37        self::assertInstanceOf(KeywordCall::class, $rules->call($list->find('function_call_conflict')[0]));
38        self::assertInstanceOf(KeywordCall::class, $rules->call($list->find('set_function_specification')[0]));
39        self::assertInstanceOf(Aggregate::class, $rules->call($list->find('set_function_specification')[1]));
40        self::assertInstanceOf(WindowFunction::class, $rules->call($list->find('window_func_call')[0]));
41        self::assertInstanceOf(ClockCall::class, $rules->call($list->find('function_call_nonkeyword')[0]));
42        self::assertInstanceOf(WeightString::class, $rules->call($list->find('function_call_conflict')[1]));
43    }
44
45    public function testFunctionLowersJsonValue(): void
46    {
47        $platform = new Platform();
48        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
49        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
50        $node = $platform->parser($profile)->parse("SELECT JSON_VALUE(a, '$')")->find('function_call_keyword')[0];
51        self::assertInstanceOf(JsonValueCall::class, (new CallRules($lowering))->function($lowering->form($node)));
52    }
53
54    public function testFunctionReportsAProductionOutsideTheFamily(): void
55    {
56        $platform = new Platform();
57        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
58        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
59        $node = $platform->parser($profile)->parse('SELECT 1')->find('select_item_list')[0];
60        $this->expectExceptionMessage('No semantic rule is implemented for: ');
61
62        (new CallRules($lowering))->function($lowering->form($node));
63    }
64
65    public function testCurrentTimestampLowersNow(): void
66    {
67        $platform = new Platform();
68        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
69        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
70        $node = $platform->parser($profile)->parse('CREATE TABLE t (c DATETIME ON UPDATE NOW())')->find('now')[0];
71        self::assertEquals(new ClockCall(Clock::Now), (new CallRules($lowering))->currentTimestamp($node));
72    }
73
74    public function testWindowNameLowersAWindowName(): void
75    {
76        $platform = new Platform();
77        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
78        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
79        $node = $platform->parser($profile)->parse('SELECT RANK() OVER w')->find('window_name')[0];
80        self::assertSame('w', (new CallRules($lowering))->windowName($node)->value);
81    }
82
83    public function testWindowSpecificationLowersASpecification(): void
84    {
85        $platform = new Platform();
86        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
87        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
88        $node = $platform->parser($profile)->parse('SELECT RANK() OVER (ROWS UNBOUNDED PRECEDING)')->find('window_spec')[0];
89        $specification = (new CallRules($lowering))->windowSpecification($node);
90
91        self::assertInstanceOf(WindowSpec::class, $specification);
92        self::assertSame(FrameBoundKind::UnboundedPreceding, $specification->frame?->start->kind);
93    }
94
95    public function testJsonTableColumnsLowersTheColumns(): void
96    {
97        $platform = new Platform();
98        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
99        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
100        $node = $platform->parser($profile)->parse("SELECT * FROM JSON_TABLE('[]', '$' COLUMNS (n FOR ORDINALITY))")->find('columns_clause')[0];
101        self::assertCount(1, (new CallRules($lowering))->jsonTableColumns($node));
102    }
103}
104