packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/FunctionCallTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\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\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\ScalarFact;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRoutine;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
21use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
22use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
23use SqlSemantics\Platform\PostgreSql\Platform;
24use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
25use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FunctionCall;
26use SqlSemantics\Platform\PostgreSql\Statement\Invocation\PositionalArgument;
27use SqlSemantics\Platform\PostgreSql\Statement\Invocation\WindowSpecification;
28use SqlSemantics\Platform\PostgreSql\Statement\Literal\BooleanLiteral;
29use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
30use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
31use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
32use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
33use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
34use SqlSemantics\Platform\PostgreSql\Statement\Query\SortItem;
35use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
36
37#[CoversClass(FunctionCall::class)]
38#[Small]
39final class FunctionCallTest extends TestCase
40{
41 public function testRejectsArgumentsNextToTheStar(): void
42 {
43 $this->expectExceptionMessage('The star form takes no argument, DISTINCT, VARIADIC or ordering inside the parentheses.');
44 new FunctionCall(new DottedName([new Name('count')]), [new PositionalArgument(new NullLiteral())], true);
45 }
46
47 public function testRejectsWithinGroupWithDistinct(): void
48 {
49 $this->expectExceptionMessage('WITHIN GROUP holds an ordering and goes with neither DISTINCT nor VARIADIC.');
50 new FunctionCall(new DottedName([new Name('f')]), [new PositionalArgument(new NullLiteral())], false, true, false, [new SortItem(new Constant(new IntegerConstant('1')))], true);
51 }
52
53 public function testRejectsAnOrderingWithoutArguments(): void
54 {
55 $this->expectExceptionMessage('DISTINCT, VARIADIC and an ordering inside the parentheses need an argument.');
56 new FunctionCall(new DottedName([new Name('f')]), [], false, false, false, [new SortItem(new Constant(new IntegerConstant('1')))]);
57 }
58
59 public function testOutputNameIsTheLastPartOfTheName(): void
60 {
61 self::assertSame('lower', (new FunctionCall(new DottedName([new Name('pg_catalog'), new Name('lower')])))->outputName()->value);
62 }
63
64 public function testDeriveScalarTypesACatalogFunction(): void
65 {
66 $call = new FunctionCall(new DottedName([new Name('upper')]), [new PositionalArgument(new Constant(new StringConstant('x')))]);
67 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
68 $fact = $derivation->scalar($call, $derivation->environment());
69 self::assertEquals(new Known(Builtin::Text), $fact->type);
70 self::assertSame(Nullability::NotNull, $fact->nullability);
71 }
72
73 public function testDeriveScalarTypesTheStarAggregate(): void
74 {
75 $call = new FunctionCall(new DottedName([new Name('count')]), star: true);
76 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
77 $fact = $derivation->scalar($call, $derivation->environment());
78 self::assertEquals(new ScalarFact(new Known(Builtin::Int8), Nullability::NotNull), $fact);
79 }
80
81 public function testDeriveScalarDependsOnAnUndeclaredRoutine(): void
82 {
83 $call = new FunctionCall(new DottedName([new Name('public'), new Name('lower')]), [new PositionalArgument(new Constant(new StringConstant('x')))]);
84 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
85 $fact = $derivation->scalar($call, $derivation->environment());
86 self::assertEquals(new Dependent([new UndeclaredRoutine(new QualifiedName(new Name('lower'), new Name('public')))]), $fact->type);
87 self::assertSame(Nullability::Dependent, $fact->nullability);
88 }
89
90 public function testDeriveScalarDerivesTheClauses(): void
91 {
92 $filter = new BooleanLiteral(true);
93 $partition = new Constant(new IntegerConstant('1'));
94 $call = new FunctionCall(new DottedName([new Name('sum')]), [new PositionalArgument(new Constant(new IntegerConstant('1')))], filter: $filter, over: new WindowSpecification(null, [$partition]));
95 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
96 $fact = $derivation->scalar($call, $derivation->environment());
97 self::assertTrue($derivation->facts()->covers($filter));
98 self::assertTrue($derivation->facts()->covers($partition));
99 self::assertEquals(new Known(Builtin::Int8), $fact->type);
100 self::assertSame(Nullability::Nullable, $fact->nullability);
101 }
102
103 public function testDeriveScalarReportsAMisusedClause(): void
104 {
105 $call = new FunctionCall(new DottedName([new Name('row_number')]));
106 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
107 $fact = $derivation->scalar($call, $derivation->environment());
108 self::assertInstanceOf(Invalid::class, $fact->type);
109 self::assertSame('window function row_number requires an OVER clause', $derivation->facts()->diagnostics[0]->message());
110 }
111
112 public function testRenderWritesEveryPartInOrder(): void
113 {
114 $call = new FunctionCall(new DottedName([new Name('percentile_cont')]), [new PositionalArgument(new Constant(new IntegerConstant('1')))], false, false, false, [new SortItem(new Constant(new IntegerConstant('1')))], true, new BooleanLiteral(true), new Name('w'));
115 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
116 $call->render($out);
117 self::assertSame('percentile_cont(1) WITHIN GROUP (ORDER BY 1) FILTER (WHERE TRUE) OVER w', (new Lexical())->join($out->pieces()));
118 }
119
120 public function testRenderWritesDistinctVariadicAndAnInnerOrdering(): void
121 {
122 $call = new FunctionCall(new DottedName([new Name('s'), new Name('f')]), [new PositionalArgument(new Constant(new IntegerConstant('1'))), new PositionalArgument(new Constant(new IntegerConstant('1')))], false, false, true, [new SortItem(new Constant(new IntegerConstant('1')))], false, null, new WindowSpecification());
123 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
124 $call->render($out);
125 self::assertSame('s.f(1, VARIADIC 1 ORDER BY 1) OVER ()', (new Lexical())->join($out->pieces()));
126 }
127
128 public function testRenderQuotesANameThatIsAColumnNameKeyword(): void
129 {
130 $call = new FunctionCall(new DottedName([new Name('coalesce')]), [], true);
131 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
132 $call->render($out);
133 self::assertSame('"coalesce"(*)', (new Lexical())->join($out->pieces()));
134 }
135
136 public function testRenderQuotesAnUnqualifiedCubeOrRollup(): void
137 {
138 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
139 (new FunctionCall(new DottedName([new Name('cube')]), [new PositionalArgument(new Constant(new IntegerConstant('1')))]))->render($out);
140 $qualified = new Output(new Codec(GrammarRelease::PostgreSql172));
141 (new FunctionCall(new DottedName([new Name('s'), new Name('rollup')]), [new PositionalArgument(new Constant(new IntegerConstant('1')))]))->render($qualified);
142 self::assertSame(['"cube"(1)', 's.rollup(1)'], [(new Lexical())->join($out->pieces()), (new Lexical())->join($qualified->pieces())]);
143 }
144}
145