packages/sql-semantics-mysql/tests/Unit/Rules/Query/ResultSlotsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Statement\Fact\QueryFact;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Shape\Field;
14use SqlSemantics\Platform\MySql\Model\Statement\Shape\OutputSlot;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\MySql\Rules\Query\ResultSlots;
19use SqlSemantics\Platform\MySql\Semantics;
20use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperator;
23use SqlSemantics\Platform\MySql\Statement\Query\ValueRow;
24use SqlSemantics\Platform\MySql\Statement\Query\ValuesQuery;
25use SqlSemantics\Platform\MySql\Statement\Type\Integral;
26use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
27
28#[CoversClass(ResultSlots::class)]
29#[Medium]
30final class ResultSlotsTest extends TestCase
31{
32 public function testCombineNamesAfterTheLeftOperand(): void
33 {
34 $semantics = new Semantics();
35 $derivation = new Derivation($semantics->context());
36 $left = new QueryFact([new Field(0, new OutputSlot(new Name('a'), new Known(new Integral(IntegralKind::Int)), Nullability::NotNull))], $semantics->context()->columnNames);
37 $right = new QueryFact([new Field(0, new OutputSlot(new Name('b'), new Known(new Integral(IntegralKind::Int)), Nullability::Nullable))], $semantics->context()->columnNames);
38 $fields = (new ResultSlots())->combine($left, $right, SetOperator::Union, $derivation);
39
40 self::assertInstanceOf(Field::class, $fields[0]);
41 self::assertSame('a', $fields[0]->name?->value);
42 self::assertSame(Nullability::Nullable, $fields[0]->nullability);
43 }
44
45 public function testNullabilityFollowsTheOperator(): void
46 {
47 self::assertSame(Nullability::Nullable, (new ResultSlots())->nullability(SetOperator::Union, Nullability::NotNull, Nullability::Nullable));
48 self::assertSame(Nullability::NotNull, (new ResultSlots())->nullability(SetOperator::Intersect, Nullability::NotNull, Nullability::Nullable));
49 self::assertSame(Nullability::Nullable, (new ResultSlots())->nullability(SetOperator::Intersect, Nullability::Nullable, Nullability::Nullable));
50 self::assertSame(Nullability::Dependent, (new ResultSlots())->nullability(SetOperator::Intersect, Nullability::Dependent, Nullability::Nullable));
51 self::assertSame(Nullability::NotNull, (new ResultSlots())->nullability(SetOperator::Except, Nullability::NotNull, Nullability::Nullable));
52 }
53
54 public function testValuesReportsARowAsAValue(): void
55 {
56 $operation = (new Semantics())->inspect('VALUES ROW((1, 2))');
57
58 self::assertSame(['Operand should contain 1 column(s), not 2.'], array_map(static fn ($diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
59 }
60
61 public function testValuesNamesTheColumnsByPosition(): void
62 {
63 $semantics = new Semantics();
64 $derivation = new Derivation($semantics->context());
65 $fact = (new ResultSlots())->values(new ValuesQuery([new ValueRow([new NumberLiteral('1'), new NullLiteral()])]), $derivation, $derivation->environment());
66
67 self::assertSame(['column_0', 'column_1'], array_map(static fn (Field $field): ?string => $field->name?->value, $fact->fields()->items ?? []));
68 self::assertSame(Nullability::Nullable, $fact->fields()?->at(1)->nullability);
69 }
70
71 public function testCombineKeepsTheInputsAnUnnamedLeftColumnDependsOn(): void
72 {
73 $operation = (new Semantics())->inspect("SELECT x FROM (SELECT 'é' UNION SELECT 1) AS d");
74
75 self::assertSame([], $operation->facts->diagnostics);
76 self::assertInstanceOf(Dependent::class, $operation->field(0)->type);
77 }
78}
79