packages/sql-semantics-mysql/tests/Unit/Rules/Query/TableShapesTest.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\Declaration\Column;
12use SqlSemantics\Platform\MySql\Model\Statement\Declaration\ImplicitColumn;
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\Shape\Field;
18use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
19use SqlSemantics\Platform\MySql\Rules\Query\TableShapes;
20use SqlSemantics\Platform\MySql\Semantics;
21use SqlSemantics\Platform\MySql\Statement\Query\ExplicitTable;
22use SqlSemantics\Platform\MySql\Statement\Query\Select;
23use SqlSemantics\Platform\MySql\Statement\Type\Integral;
24use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
25
26#[CoversClass(TableShapes::class)]
27#[Medium]
28final class TableShapesTest extends TestCase
29{
30    public function testNamedPrefersACommonTable(): void
31    {
32        $semantics = new Semantics();
33        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
34            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
35            new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
36        ]);
37        $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
38            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
39            new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
40        ]);
41        $operation = $semantics->analyze('WITH t AS (SELECT 1 AS z) SELECT * FROM t', [$t]);
42
43        self::assertSame(['z'], array_map(static fn (Field $field): ?string => $field->name?->value, $operation->fields()->items ?? []));
44    }
45
46    public function testImplicitFindsInvisibleColumnsByNameOnly(): void
47    {
48        $semantics = new Semantics();
49        $hidden = new Column(new Name('h'), new Integral(IntegralKind::Int), Nullability::Nullable);
50        $table = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull)], [new ImplicitColumn([new Name('h')], $hidden)]);
51        $named = $semantics->analyze('SELECT h FROM t', [$table]);
52        $star = $semantics->analyze('SELECT * FROM t', [$table]);
53        $fact = $named->statement instanceof Select && $named->statement->from !== null ? $named->facts->relation($named->statement->from) : null;
54
55        self::assertSame($hidden, $named->field('h')->column());
56        self::assertSame([], $named->facts->diagnostics);
57        self::assertSame(['a'], array_map(static fn (Field $field): ?string => $field->name?->value, $star->fields()->items ?? []));
58        self::assertNotNull($fact);
59        self::assertCount(1, (new TableShapes())->implicit($fact));
60    }
61
62    public function testExplicitAnswersEveryColumn(): void
63    {
64        $semantics = new Semantics();
65        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
66            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
67            new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
68        ]);
69        $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
70            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
71            new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
72        ]);
73        $operation = $semantics->analyze('TABLE t', [$t]);
74        $table = $operation->statement;
75
76        self::assertInstanceOf(ExplicitTable::class, $table);
77        self::assertCount(2, (new TableShapes())->explicit($table, $operation->facts->relation($table), new Derivation($semantics->context()))->projection);
78    }
79
80    public function testExplicitKeepsTheInputsAnUnnamedColumnDependsOn(): void
81    {
82        $operation = (new Semantics())->analyze("WITH c AS (SELECT 'é') TABLE c");
83
84        self::assertSame(1, $operation->fields()?->count());
85        self::assertEquals([new SessionState('character_set_client')], $operation->field(0)->slot->unnamed);
86    }
87}
88