packages/sql-semantics-mysql/tests/Unit/Statement/Query/QueryExpressionTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Column;
11use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Table;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\AliasTarget;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Semantics;
18use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
19use SqlSemantics\Platform\MySql\Statement\Query\Clause\OutputOrdinal;
20use SqlSemantics\Platform\MySql\Statement\Query\Clause\RowLimit;
21use SqlSemantics\Platform\MySql\Statement\Query\OrderItem;
22use SqlSemantics\Platform\MySql\Statement\Query\ParenthesizedQuery;
23use SqlSemantics\Platform\MySql\Statement\Query\QueryExpression;
24use SqlSemantics\Platform\MySql\Statement\Query\Select;
25use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
26use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation;
27use SqlSemantics\Platform\MySql\Statement\Type\Integral;
28use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
29
30#[CoversClass(QueryExpression::class)]
31#[Medium]
32final class QueryExpressionTest extends TestCase
33{
34 public function testDeriveStatementRecordsTheRowsOfTheBody(): void
35 {
36 $semantics = new Semantics();
37 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
38 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
39 new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
40 ]);
41 $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
42 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
43 new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
44 ]);
45 $operation = $semantics->analyze('SELECT a FROM t UNION SELECT c FROM u ORDER BY a LIMIT 1', [$t, $u]);
46
47 self::assertInstanceOf(QueryExpression::class, $operation->statement);
48 self::assertInstanceOf(SetOperation::class, $operation->statement->body);
49 self::assertSame($operation->facts->query($operation->statement->body), $operation->facts->output);
50 }
51
52 public function testDeriveQueryResolvesOrdinalsAgainstTheOutput(): void
53 {
54 $operation = (new Semantics())->analyze('SELECT 1 UNION SELECT 2 ORDER BY 1');
55 $ordinal = $operation->statement instanceof QueryExpression ? $operation->statement->orderBy[0]->expression : null;
56
57 self::assertInstanceOf(OutputOrdinal::class, $ordinal);
58 self::assertInstanceOf(AliasTarget::class, $operation->facts->scalar($ordinal)->resolution);
59 self::assertSame([], $operation->facts->diagnostics);
60 }
61
62 public function testDeriveQueryBindsTheCommonTablesForTheBody(): void
63 {
64 $semantics = new Semantics();
65 $operation = $semantics->analyze('WITH c (x) AS (SELECT 1) SELECT x FROM c', []);
66
67 self::assertInstanceOf(QueryExpression::class, $operation->statement);
68 self::assertNotNull($operation->statement->with);
69 self::assertInstanceOf(Known::class, $operation->field('x')->type);
70 self::assertSame([], $operation->facts->diagnostics);
71 }
72
73 public function testRenderWritesTheWithClauseTheBodyTheOrderingAndTheLimit(): void
74 {
75 $semantics = new Semantics();
76
77 self::assertSame('WITH c AS (SELECT 1) SELECT * FROM c ORDER BY 1 LIMIT 2', $semantics->analyze('with c as (select 1) select * from c order by 1 limit 2')->toString());
78 self::assertSame('(SELECT 1) ORDER BY 1', $semantics->analyze('(select 1) order by 1')->toString());
79 }
80
81 public function testAWrapperWithoutClausesIsRejected(): void
82 {
83 $this->expectExceptionMessage('A query expression holds a WITH clause, an ordering or a limit.');
84
85 new QueryExpression(null, new ParenthesizedQuery(new Select([], [new SelectExpression(new NumberLiteral('1'))])));
86 }
87
88 public function testAnOrderingOverASingleBlockIsRejected(): void
89 {
90 $this->expectExceptionMessage('The ordering and the limit of a single query block belong to the block unless an ORDER BY follows a block that orders or limits its rows.');
91
92 new QueryExpression(null, new Select([], [new SelectExpression(new NumberLiteral('1'))]), [], new RowLimit(new NumberLiteral('1')));
93 }
94
95 public function testAnIntegerOrderingItemIsRejected(): void
96 {
97 $this->expectExceptionMessage('An integer in ORDER BY or GROUP BY is a select list position.');
98
99 new QueryExpression(null, new ParenthesizedQuery(new Select([], [new SelectExpression(new NumberLiteral('1'))])), [new OrderItem(new NumberLiteral('1'))]);
100 }
101}
102