packages/sql-semantics-postgres/tests/Unit/Lowering/Table/PartitionRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Table;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Lowering\Table\PartitionRule::class)]
12#[Medium]
13final class PartitionRuleTest extends TestCase
14{
15    public function testSpecificationIsNullWhenNotWritten(): void
16    {
17        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
18        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('CREATE TABLE t (a int)');
19        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\PartitionRule($lowering))->specification($tree->find('OptPartitionSpec')[0]);
20        self::assertSame(null, $value);
21    }
22
23    public function testElementLowersAnExpressionKey(): void
24    {
25        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
26        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('CREATE TABLE t (a int) PARTITION BY RANGE ((a + 1))');
27        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\PartitionRule($lowering))->element($tree->find('part_elem')[0]);
28        $n1 = $value->key;
29        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Index\ExpressionKey::class, $n1);
30        self::assertSame(true, $n1->parenthesized);
31    }
32
33    public function testBoundLowersAHashBound(): void
34    {
35        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
36        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('CREATE TABLE p1 PARTITION OF p FOR VALUES WITH (MODULUS 2, REMAINDER 0)');
37        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\PartitionRule($lowering))->bound($tree->find('PartitionBoundSpec')[0]);
38        self::assertSame('SqlSemantics\\Platform\\PostgreSql\\Statement\\Table\\Partition\\HashBound', get_debug_type($value));
39    }
40
41    public function testHashLowersTheItems(): void
42    {
43        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
44        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('CREATE TABLE p1 PARTITION OF p FOR VALUES WITH (MODULUS 2, REMAINDER 0)');
45        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\PartitionRule($lowering))->hash($tree->find('hash_partbound')[0]);
46        self::assertSame(2, count($value));
47    }
48
49    public function testDatumsTurnsMinvalueIntoAnInfiniteBound(): void
50    {
51        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
52        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('CREATE TABLE p1 PARTITION OF p FOR VALUES FROM (MINVALUE, 1) TO (2, 3)');
53        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\PartitionRule($lowering))->datums($tree->find('expr_list')[0]);
54        self::assertSame([
55          0 => 'SqlSemantics\\Platform\\PostgreSql\\Statement\\Table\\Partition\\RangeLimit',
56          1 => 'SqlSemantics\\Platform\\PostgreSql\\Statement\\Literal\\Constant',
57        ], array_map(static fn ($datum): string => $datum::class, $value));
58    }
59}
60