packages/sql-semantics-mysql/tests/Unit/Statement/Relation/JoinedTableTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Relation;
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\MissingColumn;
15use SqlSemantics\Platform\MySql\Model\Statement\Shape\Field;
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\Select;
20use SqlSemantics\Platform\MySql\Statement\Relation\Dual;
21use SqlSemantics\Platform\MySql\Statement\Relation\Join\JoinOperator;
22use SqlSemantics\Platform\MySql\Statement\Relation\JoinedTable;
23use SqlSemantics\Platform\MySql\Statement\Relation\TableList;
24use SqlSemantics\Platform\MySql\Statement\Type\Integral;
25use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
26
27#[CoversClass(JoinedTable::class)]
28#[Medium]
29final class JoinedTableTest extends TestCase
30{
31 public function testDeriveRelationMergesUsingColumnsAndExtendsTheOuterSide(): void
32 {
33 $semantics = new Semantics();
34 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
35 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
36 new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
37 ]);
38 $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
39 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
40 new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
41 ]);
42 $operation = $semantics->inspect('SELECT a, t.a, u.a, c FROM t RIGHT JOIN u USING (a)', [$t, $u]);
43
44 self::assertSame($u->columns[0], $operation->field(0)->column());
45 self::assertSame(Nullability::NotNull, $operation->field(0)->nullability);
46 self::assertSame(Nullability::Nullable, $operation->field(1)->nullability);
47 self::assertSame(Nullability::NotNull, $operation->field(3)->nullability);
48 self::assertSame([], $operation->facts->diagnostics);
49 }
50
51 public function testDeriveRelationMergesTheCommonColumnsOfANaturalJoin(): void
52 {
53 $semantics = new Semantics();
54 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
55 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
56 new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
57 ]);
58 $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
59 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
60 new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
61 ]);
62 $operation = $semantics->inspect('SELECT * FROM t NATURAL JOIN u', [$t, $u]);
63
64 self::assertSame(['a', 'b', 'c'], array_map(static fn (Field $field): ?string => $field->name?->value, $operation->fields()->items ?? []));
65 self::assertSame([], $operation->facts->diagnostics);
66 }
67
68 public function testDeriveRelationReportsAUsingColumnAnOperandLacks(): void
69 {
70 $semantics = new Semantics();
71 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
72 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
73 new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
74 ]);
75 $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
76 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
77 new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
78 ]);
79 $operation = $semantics->inspect('SELECT 1 FROM t JOIN u USING (c)', [$t, $u]);
80
81 self::assertCount(1, $operation->facts->diagnostics);
82 self::assertInstanceOf(MissingColumn::class, $operation->facts->diagnostics[0]);
83 }
84
85 public function testDeriveRelationNestsLikeTheParseOfTheRelease(): void
86 {
87 $semantics = new Semantics();
88 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
89 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
90 new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
91 ]);
92 $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
93 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
94 new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
95 ]);
96 $modern = $semantics->inspect('SELECT 1 FROM t JOIN u JOIN u AS v ON t.a = v.a', [$t, $u]);
97 $legacy = (new Semantics('mysql-5.7.44'))->inspect('SELECT 1 FROM t JOIN u JOIN u AS v ON t.a = v.a', []);
98
99 self::assertInstanceOf(Select::class, $modern->statement);
100 self::assertInstanceOf(JoinedTable::class, $modern->statement->from);
101 self::assertInstanceOf(JoinedTable::class, $modern->statement->from->right);
102 self::assertCount(1, $modern->facts->diagnostics);
103 self::assertInstanceOf(Select::class, $legacy->statement);
104 self::assertInstanceOf(JoinedTable::class, $legacy->statement->from);
105 self::assertInstanceOf(JoinedTable::class, $legacy->statement->from->left);
106 }
107
108 public function testRenderWritesTheOperatorAndTheCondition(): void
109 {
110 $semantics = new Semantics();
111
112 self::assertSame('SELECT 1 FROM t LEFT OUTER JOIN u ON t.a = u.a NATURAL RIGHT OUTER JOIN v CROSS JOIN w STRAIGHT_JOIN x INNER JOIN y USING (a, b)', $semantics->inspect('select 1 from t left outer join u on t.a = u.a natural right outer join v cross join w straight_join x inner join y using (a, b)')->toString());
113 self::assertSame('SELECT 1 FROM t NATURAL INNER JOIN u', $semantics->inspect('select 1 from t natural inner join u')->toString());
114 self::assertSame('SELECT 1 FROM t LEFT OUTER JOIN u USING (a)', (new Semantics('mysql-5.6.51'))->inspect('select 1 from t left outer join u using (a)')->toString());
115 }
116
117 public function testAConditionOnANaturalJoinIsRejected(): void
118 {
119 $this->expectExceptionMessage('A natural join takes no condition.');
120
121 new JoinedTable(new Dual(), JoinOperator::Natural, new Dual(), null, [new Name('a')]);
122 }
123
124 public function testBothConditionsAreRejected(): void
125 {
126 $this->expectExceptionMessage('A join has an ON condition or a USING list, not both.');
127
128 new JoinedTable(new Dual(), JoinOperator::Join, new Dual(), new NumberLiteral('1'), [new Name('a')]);
129 }
130
131 public function testAJoinOnTheRightOfANaturalJoinIsRejected(): void
132 {
133 $this->expectExceptionMessage('The right operand of a natural join is no unparenthesized join.');
134
135 new JoinedTable(new Dual(), JoinOperator::Natural, new JoinedTable(new Dual(), JoinOperator::Cross, new Dual()));
136 }
137
138 public function testACommaListAsAnOperandIsRejected(): void
139 {
140 $this->expectExceptionMessage('A comma list joined to a table is written in parentheses.');
141
142 new JoinedTable(new TableList([new Dual(), new Dual()]), JoinOperator::Cross, new Dual());
143 }
144}
145