packages/sql-semantics-sqlite/tests/Unit/Rules/Typing/OperatorsTest.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\Reference\Missing\UnboundParameter;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Choice;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Dependent;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Type\NullOnly;
17use SqlSemantics\Platform\Sqlite\Rules\Typing\Operators;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\BinaryOperator;
19use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\UnaryOperator;
20use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
21
22#[CoversClass(Operators::class)]
23#[Small]
24final class OperatorsTest extends TestCase
25{
26    public function testBinaryIsFamilyIsAnIntegerThatIsNeverNull(): void
27    {
28        $operators = new Operators();
29        $null = new ScalarFact(new NullOnly(), Nullability::Nullable);
30        $dependent = new ScalarFact(new Dependent([new UnboundParameter('?')]), Nullability::Dependent);
31        $facts = [
32            $operators->binary(BinaryOperator::Is, $null, $null),
33            $operators->binary(BinaryOperator::IsNot, $null, $dependent),
34            $operators->binary(BinaryOperator::IsDistinctFrom, $dependent, $null),
35            $operators->binary(BinaryOperator::IsNotDistinctFrom, $dependent, $dependent),
36        ];
37
38        self::assertSame([Nullability::NotNull, Nullability::NotNull, Nullability::NotNull, Nullability::NotNull], array_map(static fn (ScalarFact $fact): Nullability => $fact->nullability, $facts));
39        self::assertInstanceOf(Known::class, $facts[0]->type);
40        self::assertSame(Storage::Integer, $facts[0]->type->descriptor);
41        self::assertInstanceOf(Known::class, $facts[3]->type);
42        self::assertSame(Storage::Integer, $facts[3]->type->descriptor);
43    }
44
45    public function testBinaryComparisonsAndBitwiseOperatorsAreIntegerAndNullWhenAnOperandCanBe(): void
46    {
47        $operators = new Operators();
48        $integer = new ScalarFact(new Known(Storage::Integer), Nullability::NotNull);
49        $nullable = new ScalarFact(new Known(Storage::Text), Nullability::Nullable);
50        $equal = $operators->binary(BinaryOperator::Equal, $integer, $nullable);
51        $less = $operators->binary(BinaryOperator::Less, $integer, $integer);
52        $and = $operators->binary(BinaryOperator::BitAnd, $integer, new ScalarFact(new NullOnly(), Nullability::Nullable));
53        $shift = $operators->binary(BinaryOperator::ShiftLeft, new ScalarFact(new Dependent([new UnboundParameter('?')]), Nullability::Dependent), $integer);
54
55        self::assertInstanceOf(Known::class, $equal->type);
56        self::assertSame(Storage::Integer, $equal->type->descriptor);
57        self::assertSame(Nullability::Nullable, $equal->nullability);
58        self::assertSame(Nullability::NotNull, $less->nullability);
59        self::assertInstanceOf(NullOnly::class, $and->type);
60        self::assertInstanceOf(Known::class, $shift->type);
61        self::assertSame(Storage::Integer, $shift->type->descriptor);
62        self::assertSame(Nullability::Dependent, $shift->nullability);
63    }
64
65    public function testBinaryLogicalOperatorsAreIntegerEvenWithANullOperand(): void
66    {
67        $operators = new Operators();
68        $integer = new ScalarFact(new Known(Storage::Integer), Nullability::NotNull);
69        $and = $operators->binary(BinaryOperator::And, new ScalarFact(new NullOnly(), Nullability::Nullable), $integer);
70        $or = $operators->binary(BinaryOperator::Or, $integer, $integer);
71
72        self::assertInstanceOf(Known::class, $and->type);
73        self::assertSame(Storage::Integer, $and->type->descriptor);
74        self::assertSame(Nullability::Nullable, $and->nullability);
75        self::assertInstanceOf(Known::class, $or->type);
76        self::assertSame(Nullability::NotNull, $or->nullability);
77    }
78
79    public function testBinaryArithmeticIsIntegerOrRealUnlessAnOperandIsCertainlyRealOrNull(): void
80    {
81        $operators = new Operators();
82        $integer = new ScalarFact(new Known(Storage::Integer), Nullability::NotNull);
83        $real = new ScalarFact(new Known(Storage::Real), Nullability::NotNull);
84        $sum = $operators->binary(BinaryOperator::Add, $integer, $integer);
85        $difference = $operators->binary(BinaryOperator::Subtract, $real, $integer);
86        $product = $operators->binary(BinaryOperator::Multiply, new ScalarFact(new NullOnly(), Nullability::Nullable), $integer);
87        $open = $operators->binary(BinaryOperator::Add, new ScalarFact(new Dependent([new UnboundParameter('?')]), Nullability::Dependent), $integer);
88
89        self::assertInstanceOf(Choice::class, $sum->type);
90        self::assertSame([Storage::Integer, Storage::Real], $sum->type->alternatives);
91        self::assertSame(Nullability::NotNull, $sum->nullability);
92        self::assertInstanceOf(Known::class, $difference->type);
93        self::assertSame(Storage::Real, $difference->type->descriptor);
94        self::assertInstanceOf(NullOnly::class, $product->type);
95        self::assertSame(Nullability::Nullable, $product->nullability);
96        self::assertInstanceOf(Choice::class, $open->type);
97        self::assertSame(Nullability::Dependent, $open->nullability);
98    }
99
100    public function testBinaryDivisionAndRemainderCanAlwaysBeNull(): void
101    {
102        $operators = new Operators();
103        $integer = new ScalarFact(new Known(Storage::Integer), Nullability::NotNull);
104        $quotient = $operators->binary(BinaryOperator::Divide, $integer, $integer);
105        $remainder = $operators->binary(BinaryOperator::Modulo, $integer, $integer);
106        $real = $operators->binary(BinaryOperator::Divide, new ScalarFact(new Known(Storage::Real), Nullability::NotNull), $integer);
107
108        self::assertInstanceOf(Choice::class, $quotient->type);
109        self::assertSame([Storage::Integer, Storage::Real], $quotient->type->alternatives);
110        self::assertSame(Nullability::Nullable, $quotient->nullability);
111        self::assertSame(Nullability::Nullable, $remainder->nullability);
112        self::assertInstanceOf(Known::class, $real->type);
113        self::assertSame(Storage::Real, $real->type->descriptor);
114        self::assertSame(Nullability::Nullable, $real->nullability);
115    }
116
117    public function testBinaryConcatenationIsTextUnlessAnOperandIsNull(): void
118    {
119        $operators = new Operators();
120        $integer = new ScalarFact(new Known(Storage::Integer), Nullability::NotNull);
121        $text = $operators->binary(BinaryOperator::Concat, $integer, new ScalarFact(new Known(Storage::Blob), Nullability::Nullable));
122        $null = $operators->binary(BinaryOperator::Concat, $integer, new ScalarFact(new NullOnly(), Nullability::Nullable));
123
124        self::assertInstanceOf(Known::class, $text->type);
125        self::assertSame(Storage::Text, $text->type->descriptor);
126        self::assertSame(Nullability::Nullable, $text->nullability);
127        self::assertInstanceOf(NullOnly::class, $null->type);
128    }
129
130    public function testBinaryJsonExtractionYieldsNullableTextOrAValue(): void
131    {
132        $operators = new Operators();
133        $text = new ScalarFact(new Known(Storage::Text), Nullability::NotNull);
134        $null = new ScalarFact(new NullOnly(), Nullability::Nullable);
135        $extract = $operators->binary(BinaryOperator::Extract, $text, $text);
136        $value = $operators->binary(BinaryOperator::ExtractValue, $text, $text);
137
138        self::assertInstanceOf(Known::class, $extract->type);
139        self::assertSame(Storage::Text, $extract->type->descriptor);
140        self::assertSame(Nullability::Nullable, $extract->nullability);
141        self::assertInstanceOf(Choice::class, $value->type);
142        self::assertSame([Storage::Integer, Storage::Real, Storage::Text], $value->type->alternatives);
143        self::assertSame(Nullability::Nullable, $value->nullability);
144        self::assertInstanceOf(NullOnly::class, $operators->binary(BinaryOperator::Extract, $null, $text)->type);
145        self::assertInstanceOf(NullOnly::class, $operators->binary(BinaryOperator::ExtractValue, $text, $null)->type);
146    }
147
148    public function testUnaryPlusKeepsTheTypeAndTheNullability(): void
149    {
150        $type = new Known(Storage::Text);
151        $fact = (new Operators())->unary(UnaryOperator::Plus, new ScalarFact($type, Nullability::Nullable));
152
153        self::assertSame($type, $fact->type);
154        self::assertSame(Nullability::Nullable, $fact->nullability);
155        self::assertNull($fact->resolution);
156    }
157
158    public function testUnaryMinusFollowsTheArithmeticRule(): void
159    {
160        $operators = new Operators();
161        $integer = $operators->unary(UnaryOperator::Minus, new ScalarFact(new Known(Storage::Integer), Nullability::NotNull));
162        $real = $operators->unary(UnaryOperator::Minus, new ScalarFact(new Known(Storage::Real), Nullability::NotNull));
163        $text = $operators->unary(UnaryOperator::Minus, new ScalarFact(new Known(Storage::Text), Nullability::Nullable));
164        $null = $operators->unary(UnaryOperator::Minus, new ScalarFact(new NullOnly(), Nullability::Nullable));
165
166        self::assertInstanceOf(Choice::class, $integer->type);
167        self::assertSame([Storage::Integer, Storage::Real], $integer->type->alternatives);
168        self::assertSame(Nullability::NotNull, $integer->nullability);
169        self::assertInstanceOf(Known::class, $real->type);
170        self::assertSame(Storage::Real, $real->type->descriptor);
171        self::assertInstanceOf(Choice::class, $text->type);
172        self::assertSame(Nullability::Nullable, $text->nullability);
173        self::assertInstanceOf(NullOnly::class, $null->type);
174    }
175
176    public function testUnaryNotAndBitNotAreIntegerUnlessTheOperandIsNull(): void
177    {
178        $operators = new Operators();
179        $not = $operators->unary(UnaryOperator::Not, new ScalarFact(new Known(Storage::Text), Nullability::NotNull));
180        $bitNot = $operators->unary(UnaryOperator::BitNot, new ScalarFact(new Known(Storage::Integer), Nullability::Nullable));
181        $null = $operators->unary(UnaryOperator::Not, new ScalarFact(new NullOnly(), Nullability::Nullable));
182
183        self::assertInstanceOf(Known::class, $not->type);
184        self::assertSame(Storage::Integer, $not->type->descriptor);
185        self::assertSame(Nullability::NotNull, $not->nullability);
186        self::assertInstanceOf(Known::class, $bitNot->type);
187        self::assertSame(Storage::Integer, $bitNot->type->descriptor);
188        self::assertSame(Nullability::Nullable, $bitNot->nullability);
189        self::assertInstanceOf(NullOnly::class, $null->type);
190    }
191}
192