packages/sql-semantics-postgres/tests/Unit/Lowering/Expression/ExpressionsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Expression\Expressions;
12use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
13use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
15use SqlSemantics\Platform\PostgreSql\Model\Lowering\Form;
16use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Statement\Expression\BinaryOperation;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnReference;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnStar;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\RowSpelling;
23use SqlSemantics\Platform\PostgreSql\Statement\Expression\FieldSelection;
24use SqlSemantics\Platform\PostgreSql\Statement\Expression\Indirection;
25use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\UnaryOperation;
26use SqlSemantics\Platform\PostgreSql\Statement\Expression\Step\Slice;
27use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
28use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
29use SqlSemantics\Platform\PostgreSql\Statement\Literal\PositionalParameter;
30
31#[CoversClass(Expressions::class)]
32#[Small]
33final class ExpressionsTest extends TestCase
34{
35    public function testExpressionLowersOperatorsByPrecedence(): void
36    {
37        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
38        $expression = $lowering->expressions->expression((new PostgreSqlParser('pg-17.2'))->parse('SELECT a + 1 * 2 FROM t')->find('a_expr')[0]);
39        self::assertInstanceOf(BinaryOperation::class, $expression);
40        self::assertSame('+', $expression->operator->name->value);
41        self::assertInstanceOf(BinaryOperation::class, $expression->right);
42    }
43
44    public function testExpressionsFlattensAnExpressionList(): void
45    {
46        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
47        $expressions = $lowering->expressions->expressions((new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 FROM t WHERE a IN (1, 2, $3)')->find('expr_list')[0]);
48        self::assertCount(3, $expressions);
49        self::assertInstanceOf(PositionalParameter::class, $expressions[2]);
50    }
51
52    public function testColumnReferenceKeepsThePartsAsWritten(): void
53    {
54        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
55        $reference = $lowering->expressions->columnReference((new PostgreSqlParser('pg-17.2'))->parse('SELECT s.t.A FROM s.t')->find('columnref')[0]);
56        self::assertInstanceOf(ColumnReference::class, $reference);
57        self::assertSame(['s', 't', 'a'], array_map(static fn (Name $part): string => $part->value, $reference->parts));
58    }
59
60    public function testColumnReferenceLowersAStar(): void
61    {
62        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
63        $reference = $lowering->expressions->columnReference((new PostgreSqlParser('pg-17.2'))->parse('SELECT f(t.*) FROM t')->find('columnref')[0]);
64        self::assertInstanceOf(ColumnStar::class, $reference);
65    }
66
67    public function testIndirectionLowersStepsInOrder(): void
68    {
69        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
70        $steps = $lowering->expressions->indirection((new PostgreSqlParser('pg-17.2'))->parse('SELECT t.a[1:].b FROM t')->find('indirection')[0]);
71        self::assertCount(3, $steps);
72        self::assertInstanceOf(Slice::class, $steps[1]);
73        self::assertInstanceOf(FieldSelection::class, $steps[2]);
74    }
75
76    public function testComparisonLowersTheOperatorToken(): void
77    {
78        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
79        $node = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 <> 2')->find('a_expr')[0];
80        self::assertSame('<>', $lowering->expressions->comparison(new Form($node, 'a_expr: a_expr NOT_EQUALS a_expr'))->operator->name->value);
81    }
82
83    public function testParameterAppliesTheSteps(): void
84    {
85        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
86        $node = (new PostgreSqlParser('pg-17.2'))->parse('SELECT $1[2]')->find('c_expr')[0];
87        self::assertInstanceOf(Indirection::class, $lowering->expressions->parameter(new Form($node, 'c_expr: PARAM opt_indirection')));
88    }
89
90    public function testNegationBuildsAPrefixMinus(): void
91    {
92        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
93        $minus = $lowering->productions->form((new PostgreSqlParser('pg-17.2'))->parse('SELECT -1')->find('a_expr')[0])->token(0);
94        $negation = $lowering->expressions->negation($minus, new Constant(new IntegerConstant('1')));
95        self::assertInstanceOf(UnaryOperation::class, $negation);
96        self::assertSame('-', $negation->operator->name->value);
97    }
98
99    public function testRowLowersTheFields(): void
100    {
101        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
102        self::assertCount(0, $lowering->expressions->row((new PostgreSqlParser('pg-17.2'))->parse('SELECT ROW()')->find('explicit_row')[0]));
103    }
104
105    public function testRowConstructorKeepsTheSpelling(): void
106    {
107        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
108        self::assertSame(RowSpelling::Implicit, $lowering->expressions->rowConstructor((new PostgreSqlParser('pg-17.2'))->parse('SELECT (1, 2)')->find('implicit_row')[0])->spelling);
109    }
110
111    public function testCaseExpressionLowersTheBranches(): void
112    {
113        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
114        $case = $lowering->expressions->caseExpression((new PostgreSqlParser('pg-17.2'))->parse('SELECT CASE WHEN true THEN 1 WHEN false THEN 2 ELSE 3 END')->find('case_expr')[0]);
115        self::assertCount(2, $case->branches);
116        self::assertNull($case->operand);
117    }
118
119    public function testArrayLiteralLowersNestedLevels(): void
120    {
121        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
122        $items = $lowering->expressions->arrayLiteral((new PostgreSqlParser('pg-17.2'))->parse('SELECT ARRAY[[1], [2, 3]]')->find('array_expr')[0]);
123        self::assertCount(2, $items->nested);
124        self::assertCount(2, $items->nested[1]->values);
125    }
126}
127