packages/sql-semantics-mysql/tests/Unit/Statement/Relation/DerivedTableTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Relation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Column;
11use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Table;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\MissingColumn;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Semantics;
17use SqlSemantics\Platform\MySql\Statement\Query\Problem\Misuse;
18use SqlSemantics\Platform\MySql\Statement\Query\Problem\MisuseRule;
19use SqlSemantics\Platform\MySql\Statement\Relation\DerivedTable;
20use SqlSemantics\Platform\MySql\Statement\Type\Integral;
21use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
22
23#[CoversClass(DerivedTable::class)]
24#[Medium]
25final class DerivedTableTest extends TestCase
26{
27    public function testDeriveRelationNamesTheColumnsAfterTheQueryOrTheList(): void
28    {
29        $semantics = new Semantics();
30        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
31            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
32            new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
33        ]);
34        $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
35            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
36            new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
37        ]);
38        $plain = $semantics->inspect('SELECT d.a FROM (SELECT a FROM t) AS d', [$t]);
39        $listed = $semantics->inspect('SELECT x FROM (SELECT a FROM t) AS d (x)', [$t]);
40
41        self::assertSame($t->columns[0], $plain->field('a')->column());
42        self::assertSame($t->columns[0], $listed->field('x')->column());
43        self::assertSame([], $listed->facts->diagnostics);
44    }
45
46    public function testDeriveRelationSeesTheTablesToTheLeftWhenLateral(): void
47    {
48        $semantics = new Semantics();
49        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
50            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
51            new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
52        ]);
53        $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
54            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
55            new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
56        ]);
57        $lateral = $semantics->inspect('SELECT x FROM t, LATERAL (SELECT t.a AS x) AS d', [$t]);
58        $plain = $semantics->inspect('SELECT x FROM t, (SELECT t.a AS x) AS d', [$t]);
59
60        self::assertSame([], $lateral->facts->diagnostics);
61        self::assertSame($t->columns[0], $lateral->field('x')->column());
62        self::assertInstanceOf(MissingColumn::class, $plain->facts->diagnostics[0]);
63    }
64
65    public function testDeriveRelationReportsAMissingAlias(): void
66    {
67        $operation = (new Semantics())->inspect('SELECT 1 FROM (SELECT 1)');
68
69        self::assertCount(1, $operation->facts->diagnostics);
70        self::assertInstanceOf(Misuse::class, $operation->facts->diagnostics[0]);
71        self::assertSame(MisuseRule::DerivedWithoutAlias, $operation->facts->diagnostics[0]->rule);
72    }
73
74    public function testRenderWritesTheDerivedTable(): void
75    {
76        self::assertSame('SELECT 1 FROM LATERAL (SELECT 1) d (x), ((SELECT 2)) e', (new Semantics())->inspect('select 1 from lateral (select 1) d (x), ((select 2)) e')->toString());
77        self::assertSame('SELECT 1 FROM ((SELECT 2)) e', (new Semantics('mysql-5.7.44'))->inspect('select 1 from ((select 2)) e')->toString());
78        self::assertSame('SELECT 1 FROM (SELECT 2 UNION SELECT 3 ORDER BY 1) AS e', (new Semantics('mysql-5.6.51'))->inspect('select 1 from (select 2 union select 3 order by 1) as e')->toString());
79    }
80}
81