packages/sql-semantics-mysql/tests/Unit/Rules/Query/ProjectionTest.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\Resolution\VisibleRelation;
12use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Column;
13use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Table;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
16use SqlSemantics\Platform\MySql\Model\Statement\Reference\Missing\SessionState;
17use SqlSemantics\Platform\MySql\Model\Statement\Reference\Table\MissingTable;
18use SqlSemantics\Platform\MySql\Model\Statement\Shape\Field;
19use SqlSemantics\Platform\MySql\Model\Statement\Shape\OutputSlot;
20use SqlSemantics\Platform\MySql\Model\Statement\Shape\RowShape;
21use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
22use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
23use SqlSemantics\Platform\MySql\Rules\Query\Projection;
24use SqlSemantics\Platform\MySql\Semantics;
25use SqlSemantics\Platform\MySql\Statement\Query\Problem\Misuse;
26use SqlSemantics\Platform\MySql\Statement\Relation\Dual;
27use SqlSemantics\Platform\MySql\Statement\Type\Integral;
28use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
29
30#[CoversClass(Projection::class)]
31#[Medium]
32final class ProjectionTest extends TestCase
33{
34 public function testItemsDerivesExpressionsAndStars(): void
35 {
36 $semantics = new Semantics();
37 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
38 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
39 new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
40 ]);
41 $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
42 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
43 new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
44 ]);
45 $operation = $semantics->inspect('SELECT *, b, t.* FROM t', [$t]);
46
47 self::assertSame(['a', 'b', 'b', 'a', 'b'], array_map(static fn (Field $field): ?string => $field->name?->value, $operation->fields()->items ?? []));
48 self::assertSame($t->columns[0], $operation->field(3)->column());
49 }
50
51 public function testStarReportsAStarWithoutTables(): void
52 {
53 $operation = (new Semantics())->inspect('SELECT *');
54
55 self::assertInstanceOf(Misuse::class, $operation->facts->diagnostics[0]);
56 }
57
58 public function testQualifiedReportsAnUnknownTable(): void
59 {
60 $semantics = new Semantics();
61 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
62 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
63 new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
64 ]);
65 $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
66 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
67 new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
68 ]);
69 $operation = $semantics->inspect('SELECT u.* FROM t', [$t]);
70
71 self::assertCount(1, $operation->facts->diagnostics);
72 self::assertInstanceOf(MissingTable::class, $operation->facts->diagnostics[0]);
73 }
74
75 public function testExpandAppendsAnOpenStarForUndeclaredColumns(): void
76 {
77 $operation = (new Semantics())->inspect('SELECT t.*, 1 FROM t');
78
79 self::assertNull($operation->fields());
80 self::assertNotNull($operation->shape());
81 self::assertFalse($operation->shape()->complete());
82 self::assertCount(1, $operation->shape()->slots);
83 }
84
85 public function testFieldReferencesTheSlotOfTheRelation(): void
86 {
87 $relation = new VisibleRelation(new Dual(), new RowShape([]));
88 $slot = new OutputSlot(new Name('a'), new Known(new Integral(IntegralKind::Int)), Nullability::NotNull);
89 $field = (new Projection())->field(3, $relation, $slot);
90
91 self::assertSame(3, $field->position);
92 self::assertSame($slot, $field->slot->origin);
93 }
94
95 public function testAdmitsMatchesTheCorrelationOrTableName(): void
96 {
97 $semantics = new Semantics();
98 $derivation = new Derivation($semantics->context());
99 $aliased = new VisibleRelation(new Dual(), new RowShape([]), new Name('x'), new QualifiedName(new Name('t'), new Name('db')));
100 $named = new VisibleRelation(new Dual(), new RowShape([]), null, new QualifiedName(new Name('t'), new Name('db')));
101
102 self::assertTrue((new Projection())->admits($derivation, $aliased, new QualifiedName(new Name('x'))));
103 self::assertFalse((new Projection())->admits($derivation, $aliased, new QualifiedName(new Name('t'))));
104 self::assertTrue((new Projection())->admits($derivation, $named, new QualifiedName(new Name('t'), new Name('db'))));
105 self::assertFalse((new Projection())->admits($derivation, $named, new QualifiedName(new Name('t'), new Name('other'))));
106 }
107
108 public function testMissingCollectsTheMissingInputs(): void
109 {
110 $missing = new SessionState('x');
111
112 self::assertSame([$missing], (new Projection())->missing([new VisibleRelation(new Dual(), new RowShape([], [$missing])), new VisibleRelation(new Dual(), new RowShape([]))]));
113 }
114}
115