packages/sql-semantics/tests/Unit/Core/Binding/ExpressionBinderTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Core\Binding;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\Attributes\TestWith;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Core\Binder;
12use SqlSemantics\Core\Dialect;
13use SqlSemantics\Core\SchemaBuilder;
14use SqlSemantics\Core\SemanticException;
15use SqlSemantics\Core\Type\Nullability;
16use SqlSemantics\Platform\MySql\Dialect as MySqlDialect;
17use SqlSemantics\Platform\PostgreSql\Dialect as PostgreSqlDialect;
18use SqlSemantics\Platform\Sqlite\Dialect as SqliteDialect;
19
20#[CoversClass(\SqlSemantics\Core\Binding\ExpressionBinder::class)]
21#[CoversClass(\SqlSemantics\Core\Binding\ExpressionRules::class)]
22#[CoversClass(\SqlSemantics\Core\Binding\FromBinder::class)]
23#[CoversClass(\SqlSemantics\Core\Binding\LiteralBinder::class)]
24#[CoversClass(\SqlSemantics\Core\Binding\NullFacts::class)]
25#[CoversClass(\SqlSemantics\Core\Binding\ProjectionBinder::class)]
26#[CoversClass(\SqlSemantics\Core\Binding\SelectBinder::class)]
27#[CoversClass(\SqlSemantics\Core\Binding\SyntaxGuard::class)]
28#[CoversClass(\SqlSemantics\Core\Binding\SelectModifiersBinder::class)]
29#[CoversClass(\SqlSemantics\Core\Binding\TypeResolution::class)]
30#[CoversClass(Binder::class)]
31#[CoversClass(SchemaBuilder::class)]
32#[CoversClass(\SqlSemantics\Core\Ast\DialectParser::class)]
33#[CoversClass(\SqlSemantics\Core\Ast\ColumnReader::class)]
34#[CoversClass(\SqlSemantics\Core\Ast\ConstraintReader::class)]
35#[CoversClass(\SqlSemantics\Core\Ast\Identifiers::class)]
36#[CoversClass(\SqlSemantics\Core\Ast\SchemaReader::class)]
37#[CoversClass(\SqlSemantics\Core\Ast\StatementList::class)]
38#[CoversClass(\SqlSemantics\Core\Ast\TokenGroups::class)]
39#[CoversClass(\SqlSemantics\Core\Ast\Tree::class)]
40#[CoversClass(\SqlSemantics\Core\Ast\TypeReader::class)]
41#[CoversClass(\SqlSemantics\Core\Binding\BoundRelation::class)]
42#[CoversClass(\SqlSemantics\Core\Binding\IdentitySequence::class)]
43#[CoversClass(\SqlSemantics\Core\Binding\Scope::class)]
44#[CoversClass(\SqlSemantics\Core\Binding\TableResolver::class)]
45#[CoversClass(\SqlSemantics\Core\Model\ColumnBinding::class)]
46#[CoversClass(\SqlSemantics\Core\Model\Expression::class)]
47#[CoversClass(\SqlSemantics\Core\Model\Join::class)]
48#[CoversClass(\SqlSemantics\Core\Model\Ordering::class)]
49#[CoversClass(\SqlSemantics\Core\Model\OutputColumn::class)]
50#[CoversClass(\SqlSemantics\Core\Model\BoundSelect::class)]
51#[CoversClass(\SqlSemantics\Core\Model\TableUse::class)]
52#[CoversClass(\SqlSemantics\Core\Schema::class)]
53#[CoversClass(\SqlSemantics\Core\Schema\ColumnDefinition::class)]
54#[CoversClass(\SqlSemantics\Core\Schema\TableConstraint::class)]
55#[CoversClass(\SqlSemantics\Core\Schema\TableDefinition::class)]
56#[CoversClass(SemanticException::class)]
57#[CoversClass(\SqlSemantics\Core\Type\TypeDescriptor::class)]
58#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Core\Policy\SyntaxRules::class)]
59#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\QueryRules::class)]
60#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\Platform::class)]
61#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\TypeRules::class)]
62#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\NameRules::class)]
63#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\SchemaRules::class)]
64#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\QueryRules::class)]
65#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\Platform::class)]
66#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\TypeRules::class)]
67#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\NameRules::class)]
68#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\SchemaRules::class)]
69#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\QueryRules::class)]
70#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\Platform::class)]
71#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\TypeRules::class)]
72#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\NameRules::class)]
73#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\SchemaRules::class)]
74#[Medium]
75final class ExpressionBinderTest extends TestCase
76{
77 #[TestWith([PostgreSqlDialect::PostgreSql])]
78 #[TestWith([MySqlDialect::MySql])]
79 #[TestWith([SqliteDialect::Sqlite])]
80 public function testOperationPreservesOperatorPrecedence(Dialect $dialect): void
81 {
82 $schema = (new SchemaBuilder($dialect))->build();
83 $statement = (new Binder($schema))->bind('SELECT 1+2*3 AS result');
84 self::assertSame('+', $statement->outputs[0]->expression->symbol);
85 self::assertSame('*', $statement->outputs[0]->expression->operands[1]->symbol);
86 }
87
88 #[TestWith([PostgreSqlDialect::PostgreSql])]
89 #[TestWith([MySqlDialect::MySql])]
90 #[TestWith([SqliteDialect::Sqlite])]
91 public function testBindsParenthesesAndUnaryOperators(Dialect $dialect): void
92 {
93 $schema = (new SchemaBuilder($dialect))->build('CREATE TABLE users (id INTEGER PRIMARY KEY, parent_id INTEGER, score INTEGER NOT NULL)');
94 $statement = (new Binder($schema))->bind('SELECT -(score + 1) AS negative FROM users WHERE NOT (score > 0)');
95 self::assertSame('-', $statement->outputs[0]->expression->symbol);
96 self::assertSame('+', $statement->outputs[0]->expression->operands[0]->symbol);
97 self::assertSame('NOT', $statement->where?->symbol);
98 }
99
100 public function testTokenRejectsUnmodeledTerminals(): void
101 {
102 $scope = new \SqlSemantics\Core\Binding\Scope(new \SqlSemantics\Core\Ast\Identifiers(PostgreSqlDialect::PostgreSql));
103 $this->expectException(SemanticException::class);
104 (new \SqlSemantics\Core\Binding\ExpressionBinder())->token(new \SqlParser\Lexer\Token(0, 'BCONST', "B'101'", 0), $scope);
105 }
106
107 public function testQualifiedRejectsNonNameOperands(): void
108 {
109 $tree = (new \SqlParser\Sqlite\SqliteParser())->parse('SELECT a.id');
110 $reader = new \SqlSemantics\Core\Binding\ExpressionBinder();
111 self::assertTrue($reader->qualified(\SqlSemantics\Core\Ast\Tree::significant($tree->find('expr')[0]), SqliteDialect::Sqlite->platform()->syntax()));
112 self::assertFalse($reader->qualified([], SqliteDialect::Sqlite->platform()->syntax()));
113 self::assertFalse($reader->qualified([new \SqlParser\Lexer\Token(1, 'ID', 'a', 0)], SqliteDialect::Sqlite->platform()->syntax()));
114 }
115
116 public function testCallRetainsNestedCoalesceInputs(): void
117 {
118 $schema = (new SchemaBuilder(PostgreSqlDialect::PostgreSql))->build('CREATE TABLE users (id INTEGER PRIMARY KEY, parent_id INTEGER, score INTEGER NOT NULL)');
119 $statement = (new Binder($schema))->bind('SELECT COALESCE(parent_id, COALESCE(NULL, score)) FROM users');
120 self::assertSame(Nullability::NotNull, $statement->outputs[0]->expression->nullability);
121 self::assertSame(\SqlSemantics\Core\Model\ExpressionKind::Coalesce, $statement->outputs[0]->expression->operands[1]->kind);
122 }
123}
124