packages/sql-semantics-sqlite/tests/Unit/Rules/Expression/RowValueUseTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Construction\Derivation;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Fact\ScalarFact;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\UnboundParameter;
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\Expression\RowValueUse;
20use SqlSemantics\Platform\Sqlite\Semantics;
21use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\ArityMismatch;
22use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\ArityRule;
23use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\Misuse;
24use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\MisuseRule;
25use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
26use SqlSemantics\Platform\Sqlite\Statement\Type\Vector;
27
28#[CoversClass(RowValueUse::class)]
29#[Small]
30final class RowValueUseTest extends TestCase
31{
32 public function testWidthIsOneForASingleValueAndTheWidthOfARowValue(): void
33 {
34 $rows = new RowValueUse();
35
36 self::assertSame(1, $rows->width(new ScalarFact(new Known(Storage::Integer), Nullability::NotNull)));
37 self::assertSame(1, $rows->width(new ScalarFact(new NullOnly(), Nullability::Nullable)));
38 self::assertSame(1, $rows->width(new ScalarFact(new Choice([Storage::Integer, Storage::Real]), Nullability::NotNull)));
39 self::assertSame(3, $rows->width(new ScalarFact(new Known(new Vector(3)), Nullability::NotNull)));
40 }
41
42 public function testWidthIsUnknownForADependentOrInvalidType(): void
43 {
44 $rows = new RowValueUse();
45
46 self::assertNull($rows->width(new ScalarFact(new Dependent([new UnboundParameter('?')]), Nullability::Dependent)));
47 self::assertNull($rows->width(new ScalarFact(new Invalid(new Misuse(MisuseRule::HexLiteralTooBig)), Nullability::NotNull)));
48 }
49
50 public function testSingleReportsARowValueAndAnswersTheProblem(): void
51 {
52 $derivation = new Derivation((new Semantics())->context());
53 $problem = (new RowValueUse())->single(new ScalarFact(new Known(new Vector(2)), Nullability::NotNull), $derivation);
54
55 self::assertNotNull($problem);
56 self::assertSame(MisuseRule::TooManyValueColumns, $problem->rule);
57 self::assertSame('row value misused', $problem->message());
58 self::assertSame([$problem], $derivation->facts()->diagnostics);
59 }
60
61 public function testSingleAdmitsASingleValueAndAnUnknownWidth(): void
62 {
63 $derivation = new Derivation((new Semantics())->context());
64 $rows = new RowValueUse();
65
66 self::assertNull($rows->single(new ScalarFact(new Known(Storage::Text), Nullability::NotNull), $derivation));
67 self::assertNull($rows->single(new ScalarFact(new Dependent([new UnboundParameter('?')]), Nullability::Dependent), $derivation));
68 self::assertSame([], $derivation->facts()->diagnostics);
69 }
70
71 public function testUniformReportsRowsOfDifferentWidths(): void
72 {
73 $derivation = new Derivation((new Semantics())->context());
74 (new RowValueUse())->uniform([new ScalarFact(new Known(new Vector(2)), Nullability::NotNull), new ScalarFact(new Known(new Vector(3)), Nullability::NotNull)], $derivation);
75 $diagnostics = $derivation->facts()->diagnostics;
76
77 self::assertCount(1, $diagnostics);
78 self::assertInstanceOf(ArityMismatch::class, $diagnostics[0]);
79 self::assertSame(ArityRule::RowComparison, $diagnostics[0]->rule);
80 self::assertSame(2, $diagnostics[0]->expected);
81 self::assertSame(3, $diagnostics[0]->actual);
82 self::assertSame('Row value misused: 2 columns against 3.', $diagnostics[0]->message());
83 }
84
85 public function testUniformReportsARowValueBesideASingleValue(): void
86 {
87 $derivation = new Derivation((new Semantics())->context());
88 (new RowValueUse())->uniform([new ScalarFact(new Known(Storage::Integer), Nullability::NotNull), new ScalarFact(new Known(new Vector(2)), Nullability::NotNull)], $derivation);
89 $diagnostics = $derivation->facts()->diagnostics;
90
91 self::assertCount(1, $diagnostics);
92 self::assertInstanceOf(Misuse::class, $diagnostics[0]);
93 self::assertSame(MisuseRule::TooManyValueColumns, $diagnostics[0]->rule);
94 }
95
96 public function testUniformReportsNothingForEqualOrUnknownWidths(): void
97 {
98 $derivation = new Derivation((new Semantics())->context());
99 $rows = new RowValueUse();
100 $rows->uniform([new ScalarFact(new Known(new Vector(2)), Nullability::NotNull), new ScalarFact(new Known(new Vector(2)), Nullability::Nullable), new ScalarFact(new Dependent([new UnboundParameter('?')]), Nullability::Dependent)], $derivation);
101 $rows->uniform([new ScalarFact(new Known(Storage::Integer), Nullability::NotNull), new ScalarFact(new NullOnly(), Nullability::Nullable)], $derivation);
102 $rows->uniform([], $derivation);
103
104 self::assertSame([], $derivation->facts()->diagnostics);
105 }
106
107 public function testMembershipReportsAnOperandOfAnotherWidthThanTheColumns(): void
108 {
109 $derivation = new Derivation((new Semantics())->context());
110 (new RowValueUse())->membership(new ScalarFact(new Known(new Vector(2)), Nullability::NotNull), 3, $derivation);
111 $diagnostics = $derivation->facts()->diagnostics;
112
113 self::assertCount(1, $diagnostics);
114 self::assertInstanceOf(ArityMismatch::class, $diagnostics[0]);
115 self::assertSame(ArityRule::ScalarSubquery, $diagnostics[0]->rule);
116 self::assertSame(2, $diagnostics[0]->expected);
117 self::assertSame(3, $diagnostics[0]->actual);
118 self::assertSame('Sub-select returns 3 columns - expected 2.', $diagnostics[0]->message());
119 }
120
121 public function testMembershipReportsNothingForAMatchingOrUnknownWidth(): void
122 {
123 $derivation = new Derivation((new Semantics())->context());
124 $rows = new RowValueUse();
125 $rows->membership(new ScalarFact(new Known(Storage::Integer), Nullability::NotNull), 1, $derivation);
126 $rows->membership(new ScalarFact(new Known(new Vector(2)), Nullability::NotNull), 2, $derivation);
127 $rows->membership(new ScalarFact(new Dependent([new UnboundParameter('?')]), Nullability::Dependent), 5, $derivation);
128
129 self::assertSame([], $derivation->facts()->diagnostics);
130 }
131
132 public function testElementsReportsTheFirstElementOfAnotherWidthOnly(): void
133 {
134 $derivation = new Derivation((new Semantics())->context());
135 $pair = new ScalarFact(new Known(new Vector(2)), Nullability::NotNull);
136 (new RowValueUse())->elements($pair, [$pair, new ScalarFact(new Known(Storage::Integer), Nullability::NotNull), new ScalarFact(new Known(new Vector(3)), Nullability::NotNull)], $derivation);
137 $diagnostics = $derivation->facts()->diagnostics;
138
139 self::assertCount(1, $diagnostics);
140 self::assertInstanceOf(ArityMismatch::class, $diagnostics[0]);
141 self::assertSame(ArityRule::InListElement, $diagnostics[0]->rule);
142 self::assertSame(2, $diagnostics[0]->expected);
143 self::assertSame(1, $diagnostics[0]->actual);
144 self::assertSame('IN(...) element has 1 term - expected 2.', $diagnostics[0]->message());
145 }
146
147 public function testElementsSpellsTheTermsOfARowElementInThePlural(): void
148 {
149 $derivation = new Derivation((new Semantics())->context());
150 (new RowValueUse())->elements(new ScalarFact(new Known(Storage::Integer), Nullability::NotNull), [new ScalarFact(new Known(new Vector(2)), Nullability::NotNull)], $derivation);
151 $diagnostics = $derivation->facts()->diagnostics;
152
153 self::assertCount(1, $diagnostics);
154 self::assertSame('IN(...) element has 2 terms - expected 1.', $diagnostics[0]->message());
155 }
156
157 public function testElementsReportsNothingForMatchingOrUnknownWidths(): void
158 {
159 $derivation = new Derivation((new Semantics())->context());
160 $rows = new RowValueUse();
161 $single = new ScalarFact(new Known(Storage::Integer), Nullability::NotNull);
162 $unknown = new ScalarFact(new Dependent([new UnboundParameter('?')]), Nullability::Dependent);
163 $rows->elements($single, [$single, $unknown], $derivation);
164 $rows->elements($unknown, [new ScalarFact(new Known(new Vector(2)), Nullability::NotNull)], $derivation);
165 $rows->elements($single, [], $derivation);
166
167 self::assertSame([], $derivation->facts()->diagnostics);
168 }
169}
170