packages/sql-semantics-postgres/tests/Unit/Rules/Invocation/CallTypingTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Invocation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\ScalarFact;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\SessionState;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRoutine;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
21use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
22use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\NullOnly;
23use SqlSemantics\Platform\PostgreSql\Platform;
24use SqlSemantics\Platform\PostgreSql\Rules\Invocation\CallTyping;
25use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FunctionCall;
26use SqlSemantics\Platform\PostgreSql\Statement\Invocation\NamedArgument;
27use SqlSemantics\Platform\PostgreSql\Statement\Invocation\PositionalArgument;
28use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
29use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
30use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
31use SqlSemantics\Platform\PostgreSql\Statement\Name\ImproperName;
32use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
33use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Parameterized;
34
35#[CoversClass(CallTyping::class)]
36#[Small]
37final class CallTypingTest extends TestCase
38{
39 public function testCallTypesAnExactCatalogMatch(): void
40 {
41 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42 $fact = (new CallTyping())->call($derivation, new FunctionCall(new DottedName([new Name('abs')]), [new PositionalArgument(new Constant(new IntegerConstant('1')))]), [new ScalarFact(new Known(Builtin::Int4), Nullability::NotNull)]);
43 self::assertEquals(new ScalarFact(new Known(Builtin::Int4), Nullability::NotNull), $fact);
44 }
45
46 public function testCallDependsWhenAnotherSchemaIsSearchedFirst(): void
47 {
48 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), ['public', 'pg_catalog'], [], true));
49 $fact = (new CallTyping())->call($derivation, new FunctionCall(new DottedName([new Name('abs')]), [new PositionalArgument(new Constant(new IntegerConstant('1')))]), [new ScalarFact(new Known(Builtin::Int4), Nullability::NotNull)]);
50 self::assertEquals(new Dependent([new UndeclaredRoutine(new QualifiedName(new Name('abs')))]), $fact->type);
51 }
52
53 public function testCallDependsOnTheCurrentDatabaseForACatalogQualifier(): void
54 {
55 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
56 $fact = (new CallTyping())->call($derivation, new FunctionCall(new DottedName([new Name('db'), new Name('pg_catalog'), new Name('now')])), []);
57 self::assertEquals(new Dependent([new SessionState('the name of the current database'), new UndeclaredRoutine(new QualifiedName(new Name('now'), new Name('pg_catalog'), new Name('db')))]), $fact->type);
58 }
59
60 public function testCallReportsAnImproperName(): void
61 {
62 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
63 $fact = (new CallTyping())->call($derivation, new FunctionCall(new DottedName([new Name('a'), new Name('b'), new Name('c'), new Name('d')])), []);
64 self::assertInstanceOf(Invalid::class, $fact->type);
65 self::assertInstanceOf(ImproperName::class, $derivation->facts()->diagnostics[0]);
66 }
67
68 public function testCallLeavesNamedArgumentsToTheRoutine(): void
69 {
70 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
71 $fact = (new CallTyping())->call($derivation, new FunctionCall(new DottedName([new Name('abs')]), [new NamedArgument(new Name('x'), new Constant(new IntegerConstant('1')))]), [new ScalarFact(new Known(Builtin::Int4), Nullability::NotNull)]);
72 self::assertInstanceOf(Dependent::class, $fact->type);
73 }
74
75 public function testCatalogMatchesThroughImplicitCasts(): void
76 {
77 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
78 $fact = (new CallTyping())->catalog($derivation->context, 'substring', [new ScalarFact(new Known(Builtin::Varchar), Nullability::NotNull), new ScalarFact(new Known(Builtin::Int2), Nullability::Nullable)]);
79 self::assertEquals(new ScalarFact(new Known(Builtin::Text), Nullability::Nullable), $fact);
80 }
81
82 public function testCatalogPassesAnInvalidArgumentOn(): void
83 {
84 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
85 $invalid = new Invalid(new ImproperName(new DottedName([new Name('a')])));
86 $fact = (new CallTyping())->catalog($derivation->context, 'upper', [new ScalarFact($invalid, Nullability::Dependent)]);
87 self::assertSame($invalid, $fact->type);
88 }
89
90 public function testRowAnswersNullForATypeOutsideTheTable(): void
91 {
92 self::assertNull((new CallTyping())->row('upper', [new ScalarFact(new Known(new Parameterized(Builtin::Varchar, 3)), Nullability::NotNull)], false));
93 self::assertNotNull((new CallTyping())->row('upper', [new ScalarFact(new Known(new Parameterized(Builtin::Varchar, 3)), Nullability::NotNull)], true));
94 }
95
96 public function testResultAppliesTheNullRule(): void
97 {
98 $typing = new CallTyping();
99 self::assertEquals(new ScalarFact(new Known(Builtin::Int8), Nullability::Nullable), $typing->result(['int8', 'a', 'Y', []], []));
100 self::assertEquals(new ScalarFact(new Known(Builtin::Uuid), Nullability::NotNull), $typing->result(['uuid', 's', 'N', []], []));
101 }
102
103 public function testStrictIsNullableWhenAnArgumentIs(): void
104 {
105 self::assertSame([Nullability::Nullable, Nullability::NotNull], [(new CallTyping())->strict([new ScalarFact(new NullOnly(), Nullability::NotNull)]), (new CallTyping())->strict([new ScalarFact(new Known(Builtin::Int4), Nullability::NotNull)])]);
106 }
107
108 public function testInputsMergesTheMissingInputsOfDependentArguments(): void
109 {
110 $missing = new UndeclaredRoutine(new QualifiedName(new Name('f')));
111 $fact = (new CallTyping())->inputs([new ScalarFact(new Dependent([$missing]), Nullability::Dependent), new ScalarFact(new Dependent([$missing]), Nullability::Dependent)]);
112 self::assertEquals(new Dependent([$missing, $missing]), $fact?->type);
113 self::assertNull((new CallTyping())->inputs([new ScalarFact(new Known(Builtin::Int4), Nullability::NotNull)]));
114 }
115
116 public function testCatalogFirstReadsTheSearchPath(): void
117 {
118 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), ['public', 'pg_catalog'], [], true));
119 self::assertFalse((new CallTyping())->catalogFirst($derivation->context));
120 }
121}
122