packages/sql-semantics-mysql/tests/Unit/Lowering/Query/Legacy/UnionRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Query\Legacy;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlParser\Parser\Node;
11use SqlSemantics\Platform\MySql\Lowering\Lowering;
12use SqlSemantics\Platform\MySql\Lowering\Query\Legacy\UnionRule;
13use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
14use SqlSemantics\Platform\MySql\Model\Diagnostic\AnalysisException;
15use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Semantics;
18use SqlSemantics\Platform\MySql\Statement\Query\ParenthesizedQuery;
19use SqlSemantics\Platform\MySql\Statement\Query\QueryExpression;
20use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation;
21
22#[CoversClass(UnionRule::class)]
23#[Medium]
24final class UnionRuleTest extends TestCase
25{
26 public function testStatementLiftsTheOrderingOfTheLastSelect(): void
27 {
28 $operation = (new Semantics('mysql-5.7.44'))->analyze('SELECT a FROM t UNION SELECT b FROM u ORDER BY 1 LIMIT 2');
29
30 self::assertInstanceOf(QueryExpression::class, $operation->statement);
31 self::assertInstanceOf(SetOperation::class, $operation->statement->body);
32 self::assertSame('SELECT a FROM t UNION SELECT b FROM u ORDER BY 1 LIMIT 2', $operation->toString());
33 }
34
35 public function testOperandLowersEveryOperandForm(): void
36 {
37 self::assertSame('SELECT 1 UNION (SELECT 2) UNION ALL SELECT 3', (new Semantics('mysql-5.6.51'))->analyze('select 1 union (select 2) union all select 3')->toString());
38 self::assertSame('(SELECT 1) UNION DISTINCT SELECT 2', (new Semantics('mysql-5.7.44'))->analyze('(select 1) union distinct select 2')->toString());
39 }
40
41 public function testParenLowersNestedParentheses(): void
42 {
43 self::assertSame('((SELECT 1))', (new Semantics('mysql-5.6.51'))->analyze('((select 1))')->toString());
44 }
45
46 public function testChainRejectsAnOrderingBeforeTheLastSelect(): void
47 {
48 $this->expectException(AnalysisException::class);
49
50 (new Semantics('mysql-5.7.44'))->analyze('SELECT 1 ORDER BY 1 UNION SELECT 2');
51 }
52
53 public function testOrderingLowersTheClausesAfterAParenthesizedOperand(): void
54 {
55 self::assertSame('(SELECT 1) UNION (SELECT 2) ORDER BY 1 LIMIT 1', (new Semantics('mysql-5.6.51'))->analyze('(select 1) union (select 2) order by 1 limit 1')->toString());
56 self::assertSame('(SELECT 1) LIMIT 1', (new Semantics('mysql-5.7.44'))->analyze('(select 1) limit 1')->toString());
57 }
58
59 public function testViewQueryLowersTheQueryOfAView(): void
60 {
61 $platform = new Platform();
62 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
63 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
64 $tree = $platform->parser($profile)->parse('CREATE VIEW v AS SELECT a FROM t UNION (SELECT 2)');
65 $statement = $tree->children[0];
66 self::assertInstanceOf(Node::class, $statement);
67 $create = $statement->children[0];
68 self::assertInstanceOf(Node::class, $create);
69 $definition = $create->children[0];
70 self::assertInstanceOf(Node::class, $definition);
71 $tail = $definition->children[1];
72 self::assertInstanceOf(Node::class, $tail);
73 $body = $tail->children[1];
74 self::assertInstanceOf(Node::class, $body);
75 $view = $body->children[0];
76 self::assertInstanceOf(Node::class, $view);
77 $select = $view->children[5];
78 self::assertInstanceOf(Node::class, $select);
79 $aux = $select->children[0];
80 self::assertInstanceOf(Node::class, $aux);
81
82 $query = (new UnionRule($lowering))->viewQuery($aux);
83
84 self::assertInstanceOf(SetOperation::class, $query);
85 self::assertInstanceOf(ParenthesizedQuery::class, $query->right);
86 }
87
88 public function testViewLowersTheBlockOfAView(): void
89 {
90 $platform = new Platform();
91 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
92 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
93 $tree = $platform->parser($profile)->parse('CREATE VIEW v AS SELECT a FROM t UNION (SELECT 2)');
94 $statement = $tree->children[0];
95 self::assertInstanceOf(Node::class, $statement);
96 $create = $statement->children[0];
97 self::assertInstanceOf(Node::class, $create);
98 $definition = $create->children[0];
99 self::assertInstanceOf(Node::class, $definition);
100 $tail = $definition->children[1];
101 self::assertInstanceOf(Node::class, $tail);
102 $body = $tail->children[1];
103 self::assertInstanceOf(Node::class, $body);
104 $view = $body->children[0];
105 self::assertInstanceOf(Node::class, $view);
106 $select = $view->children[5];
107 self::assertInstanceOf(Node::class, $select);
108 $aux = $select->children[0];
109 self::assertInstanceOf(Node::class, $aux);
110
111 $first = $aux->children[0];
112 self::assertInstanceOf(Node::class, $first);
113 $block = (new UnionRule($lowering))->view($first);
114
115 self::assertNotNull($block->from);
116 self::assertCount(1, $block->items);
117 }
118}
119