packages/sql-semantics-postgres/tests/Unit/Statement/Query/SetOperationTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Query\SetOperation::class)]
12#[Medium]
13final class SetOperationTest extends TestCase
14{
15    public function testOutputNameIsThatOfTheFirstOperand(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $query = $semantics->analyze('SELECT 1 AS a UNION SELECT 2 AS b');
19        $statement = $query->statement;
20        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\SetOperation::class, $statement);
21        self::assertSame('a', $statement->outputName()?->value);
22    }
23
24    public function testDeriveStatementRecordsTheCombinedRows(): void
25    {
26        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
27        $query = $semantics->analyze('SELECT 1 UNION SELECT NULL');
28        self::assertSame(\SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::Nullable, $query->field(0)->nullability);
29    }
30
31    public function testDeriveQueryResolvesTheCommonType(): void
32    {
33        $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
34        $t = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text)]);
35        $u = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('u')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int8, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('c'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Bool, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]);
36        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
37        $query = $semantics->analyze('SELECT a FROM t UNION SELECT a FROM u', [$t, $u]);
38        self::assertSame('bigint', ($query->field(0)->type instanceof \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known ? $query->field(0)->type->descriptor->name() : null));
39    }
40
41    public function testRenderKeepsTheGrouping(): void
42    {
43        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
44        $query = $semantics->analyze('SELECT 1 EXCEPT DISTINCT SELECT 2 INTERSECT SELECT 3');
45        self::assertSame('SELECT 1 EXCEPT DISTINCT SELECT 2 INTERSECT SELECT 3', $query->toString());
46    }
47}
48