packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/RowExpressionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
12use SqlSemantics\Platform\Sqlite\Semantics;
13use SqlSemantics\Platform\Sqlite\Statement\Expression\ColumnUse;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\Binary;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\RowExpression;
16use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\Misuse;
17use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\MisuseRule;
18use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
19use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
20use SqlSemantics\Platform\Sqlite\Statement\Type\Vector;
21
22#[CoversClass(RowExpression::class)]
23#[Medium]
24final class RowExpressionTest extends TestCase
25{
26    public function testDeriveScalarGivesARowOfTheWrittenWidth(): void
27    {
28        $semantics = new Semantics();
29        $create = $semantics->inspect('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
30        $operation = $semantics->inspect('SELECT (a, b) = (1, 2), (a, a, 3) = (1, 2, 3) FROM t', [$create]);
31        $statement = $operation->statement;
32
33        self::assertInstanceOf(Select::class, $statement);
34        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
35        self::assertInstanceOf(ResultColumn::class, $statement->columns[1]);
36        self::assertInstanceOf(Binary::class, $statement->columns[0]->expression);
37        self::assertInstanceOf(Binary::class, $statement->columns[1]->expression);
38        self::assertInstanceOf(RowExpression::class, $statement->columns[0]->expression->left);
39        self::assertInstanceOf(RowExpression::class, $statement->columns[1]->expression->left);
40        $row = $statement->columns[0]->expression->left;
41        self::assertCount(2, $row->items);
42        self::assertInstanceOf(ColumnUse::class, $row->items[0]);
43        $fact = $operation->facts->scalar($row);
44        self::assertEquals(new Known(new Vector(2)), $fact->type);
45        self::assertSame(Nullability::Nullable, $fact->nullability);
46        self::assertNull($fact->resolution);
47        self::assertEquals(new Known(new Vector(3)), $operation->facts->scalar($statement->columns[1]->expression->left)->type);
48        self::assertSame(Nullability::NotNull, $operation->facts->scalar($statement->columns[1]->expression->left)->nullability);
49        self::assertSame([], $operation->facts->diagnostics);
50    }
51
52    public function testDeriveScalarCombinesTheNullFactsOfTheElements(): void
53    {
54        $operation = (new Semantics())->inspect('SELECT (1, ?) = (1, 2), (NULL, 1) = (1, 2), (1, 2) = (3, 4)', []);
55        $statement = $operation->statement;
56
57        self::assertInstanceOf(Select::class, $statement);
58        $rows = array_map(static function (object $column): RowExpression {
59            self::assertInstanceOf(ResultColumn::class, $column);
60            self::assertInstanceOf(Binary::class, $column->expression);
61            self::assertInstanceOf(RowExpression::class, $column->expression->left);
62
63            return $column->expression->left;
64        }, $statement->columns);
65        self::assertSame(Nullability::Dependent, $operation->facts->scalar($rows[0])->nullability);
66        self::assertSame(Nullability::Nullable, $operation->facts->scalar($rows[1])->nullability);
67        self::assertSame(Nullability::NotNull, $operation->facts->scalar($rows[2])->nullability);
68        self::assertSame([], $operation->facts->diagnostics);
69    }
70
71    public function testDeriveScalarAsAResultColumnIsReportedByTheSelection(): void
72    {
73        $operation = (new Semantics())->inspect('SELECT (1, 2)', []);
74
75        self::assertEquals([new Misuse(MisuseRule::TooManyValueColumns)], $operation->facts->diagnostics);
76        self::assertEquals(new Known(new Vector(2)), $operation->field(0)->type);
77    }
78
79    public function testRenderWritesTheElementsInParentheses(): void
80    {
81        $operation = (new Semantics())->inspect('select (a,b) = (1,2) AS c1, (1, 2, 3) in (select 1, 2, 3) AS c2 from t');
82
83        self::assertSame('SELECT (a, b) = (1, 2) AS c1, (1, 2, 3) IN (SELECT 1, 2, 3) AS c2 FROM t', $operation->toString());
84    }
85}
86