packages/sql-semantics-mysql/tests/Unit/Rules/Query/SetFactsTest.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\Statement\Operation;
11use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
12use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
13use SqlSemantics\Platform\MySql\Rules\Query\SetFacts;
14use SqlSemantics\Platform\MySql\Semantics;
15use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
16use SqlSemantics\Platform\MySql\Statement\Query\Clause\RowLimit;
17use SqlSemantics\Platform\MySql\Statement\Query\Select;
18use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
19use SqlSemantics\Platform\MySql\Statement\Query\Set\LeadingUnion;
20use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation;
21use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperator;
22
23#[CoversClass(SetFacts::class)]
24#[Medium]
25final class SetFactsTest extends TestCase
26{
27    public function testDeriveSeesARecursiveTableWithTheColumnsOfItsAnchor(): void
28    {
29        $operation = (new Semantics())->analyze("WITH RECURSIVE c AS (SELECT 1 AS n UNION ALL SELECT CONCAT(n, 'x') FROM c) SELECT n FROM c", []);
30        $type = $operation->field('n')->type;
31
32        self::assertSame([], $operation->facts->diagnostics);
33        self::assertInstanceOf(Known::class, $type);
34        self::assertSame('BIGINT', $type->descriptor->name());
35        self::assertSame(Nullability::Nullable, $operation->field('n')->nullability);
36    }
37
38    public function testOperandsCombinesTheColumnsOfBothOperands(): void
39    {
40        $semantics = new Semantics('mysql-5.6.51');
41        $table = $semantics->analyze('CREATE TABLE t (a INT NOT NULL)');
42        $operation = $semantics->analyze('SELECT * FROM (SELECT a AS x FROM t UNION SELECT NULL FROM DUAL LIMIT 1 ORDER BY x) AS d', [$table]);
43        $type = $operation->field('x')->type;
44
45        self::assertSame([], $operation->facts->diagnostics);
46        self::assertInstanceOf(Known::class, $type);
47        self::assertSame('INT', $type->descriptor->name());
48        self::assertSame(Nullability::Nullable, $operation->field('x')->nullability);
49    }
50
51    public function testLeadingCombinesTheOperandsBeforeALaterUnion(): void
52    {
53        $semantics = new Semantics('mysql-5.6.51');
54        $table = $semantics->analyze('CREATE TABLE t (a INT NOT NULL)');
55        $operation = $semantics->analyze('SELECT a AS x FROM t UNION SELECT NULL LIMIT 1 UNION SELECT 2', [$table]);
56
57        self::assertSame([], $operation->facts->diagnostics);
58        self::assertSame(Nullability::Nullable, $operation->field('x')->nullability);
59        self::assertSame('BIGINT', $operation->field('x')->type instanceof Known ? $operation->field('x')->type->descriptor->name() : null);
60    }
61
62    public function testLeadingRefusesAnOwnLimitOnMySql57(): void
63    {
64        $limited = new Select([], [new SelectExpression(new NumberLiteral('2'))], null, null, null, null, [], null, [], new RowLimit(new NumberLiteral('1')));
65        $union = new SetOperation(new LeadingUnion(new Select([], [new SelectExpression(new NumberLiteral('1'))]), null, $limited), SetOperator::Union, null, new Select([], [new SelectExpression(new NumberLiteral('3'))]));
66
67        $this->expectExceptionMessage('A SELECT that keeps its own LIMIT before a later UNION needs MySQL 5.6.');
68
69        new Operation((new Semantics('mysql-5.7.44'))->context([]), $union);
70    }
71}
72