packages/sql-semantics-postgres/tests/Unit/Rules/Resolution/FromScopeTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Resolution;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Resolution\FromScope::class)]
12#[Medium]
13final class FromScopeTest extends TestCase
14{
15 public function testOpenLetsFunctionsSeeEarlierItems(): void
16 {
17 $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
18 $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)]);
19 $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)]);
20 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
21 $query = $semantics->inspect('SELECT * FROM t, ROWS FROM (f(t.a) AS (x integer))', [$t, $u]);
22 self::assertSame([], $query->facts->diagnostics);
23 }
24
25 public function testVisibleNamesXmltableWithoutAlias(): void
26 {
27 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
28 $query = $semantics->inspect("SELECT xmltable.a FROM XMLTABLE ('/r' PASSING '<r/>' COLUMNS a text)");
29 self::assertSame([], $query->facts->diagnostics);
30 }
31
32 public function testEnterLimitsTheOnConditionToTheJoin(): void
33 {
34 $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
35 $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)]);
36 $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)]);
37 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
38 $query = $semantics->inspect('SELECT 1 FROM t, u JOIN (SELECT 1 AS z) AS w ON t.a = w.z', [$t, $u]);
39 self::assertCount(1, $query->facts->diagnostics);
40 }
41
42 public function testEnterBarsTheLeftSideOfARightJoinForLateralItems(): void
43 {
44 $query = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('SELECT * FROM (SELECT 1 AS a) AS t RIGHT JOIN generate_series(1, t.a) AS g ON true', []);
45 self::assertSame('invalid reference to FROM-clause entry for table "t"', $query->facts->diagnostics[0]->message());
46 }
47
48 public function testEnterKeepsEarlierCommaItemsReachableFromARightJoin(): void
49 {
50 $query = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('SELECT * FROM (SELECT 1 AS a) AS t, (SELECT 2 AS b) AS u RIGHT JOIN LATERAL (SELECT t.a AS x) AS s ON true', []);
51 self::assertSame([], $query->facts->diagnostics);
52 }
53}
54