packages/sql-semantics-sqlite/tests/Unit/Rules/Typing/FunctionsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Typing;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Fact\ScalarFact;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\UndeclaredRoutine;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Choice;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Dependent;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Invalid;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Type\NullOnly;
19use SqlSemantics\Platform\Sqlite\Rules\Typing\Functions;
20use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\WrongArgumentCount;
21use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
22
23#[CoversClass(Functions::class)]
24#[Small]
25final class FunctionsTest extends TestCase
26{
27    public function testRowFindsTheRowForTheArgumentCountCaseInsensitively(): void
28    {
29        $functions = new Functions();
30
31        self::assertSame([1, 1, 'IR', 'P', 's'], $functions->row('ABS', 1));
32        self::assertSame([1, 1, '1', 'Y', 'a'], $functions->row('max', 1));
33        self::assertSame([2, -1, '+', 'P', 's'], $functions->row('Max', 2));
34        self::assertSame([2, -1, '+', 'P', 's'], $functions->row('max', 5));
35        self::assertSame([0, -1, 'T', 'N', 's'], $functions->row('char', 0));
36        self::assertSame([0, 0, 'I', 'N', 'w'], $functions->row('row_number', 0));
37    }
38
39    public function testRowIsNullForAnUnknownNameOrArgumentCount(): void
40    {
41        $functions = new Functions();
42
43        self::assertNull($functions->row('nosuch', 1));
44        self::assertNull($functions->row('abs', 2));
45        self::assertNull($functions->row('abs', 0));
46        self::assertNull($functions->row('count', 2));
47        self::assertNull($functions->row('row_number', 1));
48    }
49
50    public function testAggregatesHoldsForABuiltInAggregateCalledWithItsArguments(): void
51    {
52        $functions = new Functions();
53
54        self::assertTrue($functions->aggregates('count', 0));
55        self::assertTrue($functions->aggregates('COUNT', 1));
56        self::assertTrue($functions->aggregates('sum', 1));
57        self::assertTrue($functions->aggregates('max', 1));
58        self::assertTrue($functions->aggregates('group_concat', 2));
59        self::assertFalse($functions->aggregates('max', 2));
60        self::assertFalse($functions->aggregates('row_number', 0));
61        self::assertFalse($functions->aggregates('abs', 1));
62        self::assertFalse($functions->aggregates('nosuch', 1));
63    }
64
65    public function testResultFollowsTheResultCodeOfTheRow(): void
66    {
67        $functions = new Functions();
68        $integer = new ScalarFact(new Known(Storage::Integer), Nullability::NotNull);
69        $text = new ScalarFact(new Known(Storage::Text), Nullability::Nullable);
70        $abs = $functions->result(new Name('abs'), [$integer]);
71        $typeof = $functions->result(new Name('typeof'), [new ScalarFact(new NullOnly(), Nullability::Nullable)]);
72        $likely = $functions->result(new Name('likely'), [$text]);
73        $nullif = $functions->result(new Name('nullif'), [$integer, $text]);
74        $substr = $functions->result(new Name('substr'), [$text, $integer]);
75        $extract = $functions->result(new Name('json_extract'), [$text, $text]);
76
77        self::assertInstanceOf(Choice::class, $abs->type);
78        self::assertSame([Storage::Integer, Storage::Real], $abs->type->alternatives);
79        self::assertSame(Nullability::NotNull, $abs->nullability);
80        self::assertInstanceOf(Known::class, $typeof->type);
81        self::assertSame(Storage::Text, $typeof->type->descriptor);
82        self::assertSame(Nullability::NotNull, $typeof->nullability);
83        self::assertInstanceOf(Known::class, $likely->type);
84        self::assertSame(Storage::Text, $likely->type->descriptor);
85        self::assertSame(Nullability::Nullable, $likely->nullability);
86        self::assertInstanceOf(Known::class, $nullif->type);
87        self::assertSame(Storage::Integer, $nullif->type->descriptor);
88        self::assertSame(Nullability::Nullable, $nullif->nullability);
89        self::assertInstanceOf(Choice::class, $substr->type);
90        self::assertSame([Storage::Text, Storage::Blob], $substr->type->alternatives);
91        self::assertInstanceOf(Choice::class, $extract->type);
92        self::assertSame(Storage::cases(), $extract->type->alternatives);
93    }
94
95    public function testResultUnitesTheClassesOfTheArgumentsTheCodeSelects(): void
96    {
97        $functions = new Functions();
98        $integer = new ScalarFact(new Known(Storage::Integer), Nullability::NotNull);
99        $real = new ScalarFact(new Known(Storage::Real), Nullability::NotNull);
100        $text = new ScalarFact(new Known(Storage::Text), Nullability::NotNull);
101        $null = new ScalarFact(new NullOnly(), Nullability::Nullable);
102        $iif = $functions->result(new Name('iif'), [$integer, $text, $real]);
103        $coalesce = $functions->result(new Name('coalesce'), [$null, new ScalarFact(new Known(Storage::Integer), Nullability::Nullable), $text]);
104        $lag = $functions->result(new Name('lag'), [$integer, $real, $text]);
105        $ifnull = $functions->result(new Name('ifnull'), [new ScalarFact(new Known(Storage::Integer), Nullability::Nullable), $null]);
106
107        self::assertInstanceOf(Choice::class, $iif->type);
108        self::assertSame([Storage::Real, Storage::Text], $iif->type->alternatives);
109        self::assertSame(Nullability::Nullable, $iif->nullability);
110        self::assertInstanceOf(Choice::class, $coalesce->type);
111        self::assertSame([Storage::Integer, Storage::Text], $coalesce->type->alternatives);
112        self::assertSame(Nullability::NotNull, $coalesce->nullability);
113        self::assertInstanceOf(Choice::class, $lag->type);
114        self::assertSame([Storage::Integer, Storage::Text], $lag->type->alternatives);
115        self::assertInstanceOf(Known::class, $ifnull->type);
116        self::assertSame(Storage::Integer, $ifnull->type->descriptor);
117        self::assertSame(Nullability::Nullable, $ifnull->nullability);
118    }
119
120    public function testResultIsInvalidForAWrongArgumentCount(): void
121    {
122        $integer = new ScalarFact(new Known(Storage::Integer), Nullability::NotNull);
123        $result = (new Functions())->result(new Name('abs'), [$integer, $integer]);
124
125        self::assertInstanceOf(Invalid::class, $result->type);
126        self::assertInstanceOf(WrongArgumentCount::class, $result->type->cause);
127        self::assertSame('abs', $result->type->cause->function->value);
128        self::assertSame(2, $result->type->cause->arguments);
129        self::assertSame('wrong number of arguments to function abs()', $result->type->cause->message());
130        self::assertSame(Nullability::Dependent, $result->nullability);
131    }
132
133    public function testResultDependsOnAnUndeclaredRoutine(): void
134    {
135        $result = (new Functions())->result(new Name('nosuch'), [new ScalarFact(new Known(Storage::Integer), Nullability::NotNull)]);
136
137        self::assertInstanceOf(Dependent::class, $result->type);
138        self::assertCount(1, $result->type->missing);
139        self::assertInstanceOf(UndeclaredRoutine::class, $result->type->missing[0]);
140        self::assertSame('nosuch', $result->type->missing[0]->name->name->value);
141        self::assertSame('the signature of routine nosuch', $result->type->missing[0]->describe());
142        self::assertSame(Nullability::Dependent, $result->nullability);
143    }
144
145    public function testNullabilityOfTheFixedCodesIgnoresTheArguments(): void
146    {
147        $functions = new Functions();
148
149        self::assertSame(Nullability::NotNull, $functions->nullability('N', [Nullability::Nullable]));
150        self::assertSame(Nullability::NotNull, $functions->nullability('N', []));
151        self::assertSame(Nullability::Nullable, $functions->nullability('Y', [Nullability::NotNull]));
152        self::assertSame(Nullability::Nullable, $functions->nullability('P', []));
153        self::assertSame(Nullability::Nullable, $functions->nullability('C', []));
154    }
155
156    public function testNullabilityPropagatesEveryArgumentForTheCodeP(): void
157    {
158        $functions = new Functions();
159
160        self::assertSame(Nullability::NotNull, $functions->nullability('P', [Nullability::NotNull, Nullability::NotNull]));
161        self::assertSame(Nullability::Nullable, $functions->nullability('P', [Nullability::NotNull, Nullability::Nullable]));
162        self::assertSame(Nullability::Dependent, $functions->nullability('P', [Nullability::NotNull, Nullability::Dependent]));
163        self::assertSame(Nullability::Nullable, $functions->nullability('P', [Nullability::Dependent, Nullability::Nullable]));
164    }
165
166    public function testNullabilityIsNotNullWhenAnyArgumentIsForTheCodeC(): void
167    {
168        $functions = new Functions();
169
170        self::assertSame(Nullability::NotNull, $functions->nullability('C', [Nullability::Nullable, Nullability::NotNull]));
171        self::assertSame(Nullability::NotNull, $functions->nullability('C', [Nullability::NotNull, Nullability::Dependent]));
172        self::assertSame(Nullability::Nullable, $functions->nullability('C', [Nullability::Nullable, Nullability::Nullable]));
173        self::assertSame(Nullability::Nullable, $functions->nullability('C', [Nullability::Dependent, Nullability::Nullable]));
174        self::assertSame(Nullability::Dependent, $functions->nullability('C', [Nullability::Dependent, Nullability::Dependent]));
175    }
176
177    public function testNullabilityFollowsTheFirstArgumentForTheCodeF(): void
178    {
179        $functions = new Functions();
180
181        self::assertSame(Nullability::Nullable, $functions->nullability('F', [Nullability::Nullable, Nullability::NotNull]));
182        self::assertSame(Nullability::NotNull, $functions->nullability('F', [Nullability::NotNull, Nullability::Nullable]));
183        self::assertSame(Nullability::Dependent, $functions->nullability('F', [Nullability::Dependent]));
184    }
185}
186