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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query\Clause;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\OutputPosition::class)]
12#[Medium]
13final class OutputPositionTest extends TestCase
14{
15    public function testValueIsTheSignedPosition(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $query = $semantics->inspect('SELECT 1 ORDER BY - (2)');
19        $select = $query->statement;
20        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Select::class, $select);
21        $position = $select->options?->order[0]->expression;
22        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\OutputPosition::class, $position);
23        self::assertSame('-2', $position->value());
24    }
25
26    public function testDeriveScalarDenotesTheField(): void
27    {
28        $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
29        $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)]);
30        $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)]);
31        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
32        $query = $semantics->inspect('SELECT a, b FROM t ORDER BY 2', [$t, $u]);
33        $select = $query->statement;
34        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Select::class, $select);
35        $position = $select->options?->order[0]->expression;
36        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\OutputPosition::class, $position);
37        $resolution = $query->facts->scalar($position)->resolution;
38        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Column\AliasTarget::class, $resolution);
39        self::assertSame($query->field(1), $resolution->field);
40    }
41
42    public function testDeriveScalarDependsOnAnUnexpandedStar(): void
43    {
44        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
45        $query = $semantics->inspect('SELECT * FROM v ORDER BY 3');
46        $select = $query->statement;
47        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Select::class, $select);
48        $position = $select->options?->order[0]->expression;
49        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\OutputPosition::class, $position);
50        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent::class, $query->facts->scalar($position)->type);
51    }
52
53    public function testDeriveScalarReportsAPositionPastTheList(): void
54    {
55        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
56        $query = $semantics->inspect('SELECT 1 ORDER BY 0');
57        self::assertSame('ORDER BY position 0 is not in select list', $query->facts->diagnostics[0]->message());
58    }
59
60    public function testRenderWritesTheConstant(): void
61    {
62        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
63        $query = $semantics->inspect('SELECT 1 GROUP BY (1)');
64        self::assertSame('SELECT 1 GROUP BY (1)', $query->toString());
65    }
66}
67