packages/sql-semantics-mysql/tests/Unit/Rules/Call/WindowResultsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Call;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
12use SqlSemantics\Platform\MySql\Model\Statement\Fact\ScalarFact;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Rules\Call\TypeClass;
18use SqlSemantics\Platform\MySql\Rules\Call\WindowResults;
19use SqlSemantics\Platform\MySql\Statement\Call\Problem\UnsupportedWindowing;
20use SqlSemantics\Platform\MySql\Statement\Call\Problem\WindowingLimit;
21use SqlSemantics\Platform\MySql\Statement\Call\Window\CountingEdge;
22use SqlSemantics\Platform\MySql\Statement\Call\Window\NullTreatment;
23use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowFunction;
24use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowFunctionKind;
25use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
26use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
27
28#[CoversClass(WindowResults::class)]
29#[Small]
30final class WindowResultsTest extends TestCase
31{
32    public function testResultTypesEachKindOfWindowFunction(): void
33    {
34        $platform = new Platform();
35        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], false));
36        $int = new ScalarFact(new Known(TypeClass::Integer->descriptor()), Nullability::NotNull);
37        $text = new ScalarFact(new Known(TypeClass::Character->descriptor()), Nullability::NotNull);
38        $results = new WindowResults();
39        $rank = $results->result(new WindowFunction(WindowFunctionKind::Rank, [], new Name('w')), [], $derivation);
40        $lead = $results->result(new WindowFunction(WindowFunctionKind::Lead, [new NumberLiteral('1'), new NumberLiteral('1'), new StringLiteral(['a'])], new Name('w')), [$int, $int, $text], $derivation);
41        $first = $results->result(new WindowFunction(WindowFunctionKind::FirstValue, [new NumberLiteral('1')], new Name('w')), [$int], $derivation);
42
43        self::assertEquals(new Known(TypeClass::Unsigned->descriptor()), $rank->type);
44        self::assertSame(Nullability::NotNull, $rank->nullability);
45        self::assertEquals(new Known(TypeClass::Character->descriptor()), $lead->type);
46        self::assertSame(Nullability::Nullable, $lead->nullability);
47        self::assertSame($int->type, $first->type);
48    }
49
50    public function testResultReportsIgnoreNullsAndFromLast(): void
51    {
52        $platform = new Platform();
53        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], false));
54        $int = new ScalarFact(new Known(TypeClass::Integer->descriptor()), Nullability::NotNull);
55        $call = new WindowFunction(WindowFunctionKind::NthValue, [new NumberLiteral('1'), new NumberLiteral('2')], new Name('w'), NullTreatment::Ignore, CountingEdge::Last);
56        (new WindowResults())->result($call, [$int, $int], $derivation);
57        $diagnostics = $derivation->facts()->diagnostics;
58
59        self::assertInstanceOf(UnsupportedWindowing::class, $diagnostics[0]);
60        self::assertInstanceOf(UnsupportedWindowing::class, $diagnostics[1]);
61        self::assertSame([WindowingLimit::IgnoreNulls, WindowingLimit::FromLast], [$diagnostics[0]->limit, $diagnostics[1]->limit]);
62    }
63}
64