packages/sql-semantics-mysql/tests/Unit/Rules/Query/From/JoiningTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Query\From;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Resolution\VisibleRelation;
11use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Column;
12use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Table;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\MySql\Model\Statement\Reference\Missing\SessionState;
16use SqlSemantics\Platform\MySql\Model\Statement\Shape\Field;
17use SqlSemantics\Platform\MySql\Model\Statement\Shape\OutputSlot;
18use SqlSemantics\Platform\MySql\Model\Statement\Shape\RowShape;
19use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
20use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
21use SqlSemantics\Platform\MySql\Rules\Query\From\Joining;
22use SqlSemantics\Platform\MySql\Semantics;
23use SqlSemantics\Platform\MySql\Statement\Query\Problem\Misuse;
24use SqlSemantics\Platform\MySql\Statement\Query\Problem\MisuseRule;
25use SqlSemantics\Platform\MySql\Statement\Relation\Dual;
26use SqlSemantics\Platform\MySql\Statement\Type\Integral;
27use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
28
29#[CoversClass(Joining::class)]
30#[Medium]
31final class JoiningTest extends TestCase
32{
33    public function testJoinMergesTheUsingColumns(): void
34    {
35        $semantics = new Semantics();
36        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
37            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
38            new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
39        ]);
40        $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
41            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
42            new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
43        ]);
44        $operation = $semantics->inspect('SELECT * FROM t JOIN u USING (a)', [$t, $u]);
45
46        self::assertSame(['a', 'b', 'c'], array_map(static fn (Field $field): ?string => $field->name?->value, $operation->fields()->items ?? []));
47    }
48
49    public function testNamesAnswersNothingForANaturalJoinOverUndeclaredColumns(): void
50    {
51        $operation = (new Semantics())->inspect('SELECT * FROM t NATURAL JOIN u');
52
53        self::assertNull($operation->fields());
54        self::assertSame([], $operation->facts->diagnostics);
55    }
56
57    public function testLocateReportsAColumnSelectedTwice(): void
58    {
59        $semantics = new Semantics();
60        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull)]);
61        $operation = $semantics->inspect('SELECT 1 FROM (t AS x, t AS y) JOIN t USING (a)', [$t]);
62
63        self::assertCount(1, $operation->facts->diagnostics);
64        self::assertInstanceOf(Misuse::class, $operation->facts->diagnostics[0]);
65        self::assertSame(MisuseRule::AmbiguousJoinColumn, $operation->facts->diagnostics[0]->rule);
66    }
67
68    public function testExtendMakesEveryColumnNullable(): void
69    {
70        $slot = new OutputSlot(new Name('a'), new Known(new Integral(IntegralKind::Int)), Nullability::NotNull);
71        $extended = (new Joining())->extend(new VisibleRelation(new Dual(), new RowShape([$slot])));
72
73        self::assertSame(Nullability::Nullable, $extended->shape->slots[0]->nullability);
74        self::assertSame($slot, $extended->shape->slots[0]->origin);
75    }
76
77    public function testNullableKeepsANullableSlot(): void
78    {
79        $slot = new OutputSlot(new Name('a'), new Known(new Integral(IntegralKind::Int)), Nullability::Nullable);
80
81        self::assertSame($slot, (new Joining())->nullable($slot));
82    }
83
84    public function testNullableKeepsTheInputsAnUnnamedSlotDependsOn(): void
85    {
86        $slot = new OutputSlot(null, new Known(new Integral(IntegralKind::Int)), Nullability::NotNull, null, null, [new SessionState('character_set_client')]);
87
88        self::assertEquals([new SessionState('character_set_client')], (new Joining())->nullable($slot)->unnamed);
89    }
90
91    public function testLocateLeavesAUsingColumnAnUnnamedColumnMayBe(): void
92    {
93        $semantics = new Semantics();
94        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [new Column(new Name('x'), new Integral(IntegralKind::Int), Nullability::NotNull)]);
95
96        self::assertSame([], $semantics->inspect("SELECT x FROM (SELECT 'é') AS d JOIN t USING (x)", [$t])->facts->diagnostics);
97    }
98}
99