packages/sql-semantics-mysql/src/Lowering/TableChange/Partition/DefinitionRule.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\TableChange\Partition;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\MySql\Lowering\Lowering;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\ImplementationGap;
10use SqlSemantics\Platform\MySql\Model\Lowering\Lists;
11use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
12use SqlSemantics\Platform\MySql\Statement\Partition\Bound\LessThan;
13use SqlSemantics\Platform\MySql\Statement\Partition\Bound\PartitionBound;
14use SqlSemantics\Platform\MySql\Statement\Partition\Bound\PartitionMaximum;
15use SqlSemantics\Platform\MySql\Statement\Partition\Bound\PartitionRow;
16use SqlSemantics\Platform\MySql\Statement\Partition\Bound\ValuesIn;
17use SqlSemantics\Platform\MySql\Statement\Partition\Bound\ValuesInRows;
18use SqlSemantics\Platform\MySql\Statement\Partition\PartitionDefinition;
19use SqlSemantics\Platform\MySql\Statement\Partition\SubpartitionDefinition;
20
21/**
22 * Lowers partition and subpartition definitions with their values.
23 *
24 * Rule: MYSQL-PARTITION-DEFINITION-001. Scope: part_defs, opt_part_defs,
25 * part_def_list, part_definition, part_name, opt_part_values,
26 * part_func_max, part_values_in, part_value_list, part_value_item,
27 * part_value_item_list, part_value_expr_item, part_value_item_list_paren,
28 * opt_sub_partition, sub_part_list, sub_part_definition, sub_name. A
29 * parenthesized row of values is PT_part_value_item_list_paren; `VALUES IN`
30 * with one row is PT_part_values_in_item and with a parenthesized list of
31 * rows PT_part_values_in_list; MAXVALUE without parentheses after LESS THAN
32 * is the bound without a row. Constructs: PartitionDefinition,
33 * SubpartitionDefinition, LessThan, ValuesIn, ValuesInRows, PartitionRow,
34 * PartitionMaximum. Terminates: lists are flattened iteratively.
35 * Source: https://dev.mysql.com/doc/refman/8.4/en/create-table.html#create-table-partitioning.
36 * Status: Implemented.
37 *
38 * @visibility SqlSemantics\Platform\MySql\Lowering\TableChange
39 */
40final class DefinitionRule
41{
42    /**
43     * The list spines of partition and subpartition definitions.
44     */
45    private const SPINES = [
46        'part_def_list: part_definition' => true, 'part_def_list: part_def_list , part_definition' => true,
47        'sub_part_list: sub_part_definition' => true, 'sub_part_list: sub_part_list , sub_part_definition' => true,
48    ];
49
50    /**
51     * The list spines of rows and of row items.
52     */
53    private const ROW_SPINES = [
54        'part_value_list: part_value_item' => true, 'part_value_list: part_value_list , part_value_item' => true,
55        'part_value_list: part_value_item_list_paren' => true, 'part_value_list: part_value_list , part_value_item_list_paren' => true,
56        'part_value_item_list: part_value_expr_item' => true, 'part_value_item_list: part_value_item_list , part_value_expr_item' => true,
57        'part_value_item_list: part_value_item' => true, 'part_value_item_list: part_value_item_list , part_value_item' => true,
58    ];
59
60    /**
61     * The parenthesized row productions.
62     */
63    private const ROWS = ['part_value_item: ( part_value_item_list )' => true, 'part_value_item_list_paren: ( part_value_item_list )' => true];
64
65    /**
66     * @param Lowering $lowering The lowering this rule belongs to
67     */
68    public function __construct(private readonly Lowering $lowering)
69    {
70    }
71
72    /**
73     * Lowers the definitions of a partitioning or of a partition change; an absent list is empty.
74     *
75     * @return list<PartitionDefinition>
76     * @throws ImplementationGap When a production has no rule
77     */
78    public function definitions(Node $list): array
79    {
80        $form = $this->lowering->form($list);
81        if ($form->signature === 'part_defs:' || $form->signature === 'opt_part_defs:') {
82            return [];
83        }
84        if ($form->signature === 'part_defs: ( part_def_list )' || $form->signature === 'opt_part_defs: ( part_def_list )') {
85            return $this->definitions($form->node(1));
86        }
87        $this->spine($list);
88        $definitions = [];
89        foreach ((new Lists())->items($list) as $item) {
90            $definitions[] = $this->definition($item);
91        }
92
93        return $definitions;
94    }
95
96    /**
97     * Confirms that a node is a list spine this rule flattens.
98     *
99     * @throws ImplementationGap When the production has no rule
100     */
101    public function spine(Node $list): void
102    {
103        $form = $this->lowering->form($list);
104        if (!isset(self::SPINES[$form->signature]) && !isset(self::ROW_SPINES[$form->signature])) {
105            throw ImplementationGap::production($form);
106        }
107    }
108
109    /**
110     * Lowers one partition definition: a node of `part_definition`.
111     *
112     * @throws ImplementationGap When a production has no rule
113     */
114    public function definition(Node $definition): PartitionDefinition
115    {
116        $form = $this->lowering->form($definition);
117        $name = match ($form->signature) {
118            'part_definition: PARTITION_SYM part_name opt_part_values opt_part_options opt_sub_partition' => $this->lowering->form($form->node(1)),
119            'part_definition: PARTITION_SYM ident opt_part_values opt_part_options opt_sub_partition' => null,
120            default => throw ImplementationGap::production($form),
121        };
122        if ($name !== null && $name->signature !== 'part_name: ident') {
123            throw ImplementationGap::production($name);
124        }
125
126        return new PartitionDefinition(
127            $this->lowering->names->identifier($name === null ? $form->node(1) : $name->node(0)),
128            $this->bound($form->node(2)),
129            (new OptionRule($this->lowering))->options($form->node(3)),
130            $this->subpartitions($form->node(4)),
131        );
132    }
133
134    /**
135     * Lowers the values of a partition: a node of `opt_part_values`; no values is null.
136     *
137     * @throws ImplementationGap When a production has no rule
138     */
139    public function bound(Node $values): ?PartitionBound
140    {
141        $form = $this->lowering->form($values);
142
143        return match ($form->signature) {
144            'opt_part_values:' => null,
145            'opt_part_values: VALUES LESS_SYM THAN_SYM part_func_max' => $this->lessThan($form->node(3)),
146            'opt_part_values: VALUES IN_SYM part_values_in' => $this->valuesIn($form->node(2)),
147            default => throw ImplementationGap::production($form),
148        };
149    }
150
151    /**
152     * Lowers the bound of a RANGE partition: a node of `part_func_max`.
153     *
154     * @throws ImplementationGap When a production has no rule
155     */
156    public function lessThan(Node $bound): LessThan
157    {
158        $form = $this->lowering->form($bound);
159
160        return match ($form->signature) {
161            'part_func_max: MAX_VALUE_SYM' => new LessThan(null),
162            'part_func_max: part_value_item', 'part_func_max: part_value_item_list_paren' => new LessThan($this->row($form->node(0))),
163            default => throw ImplementationGap::production($form),
164        };
165    }
166
167    /**
168     * Lowers the values of a LIST partition: a node of `part_values_in`.
169     *
170     * @throws ImplementationGap When a production has no rule
171     */
172    public function valuesIn(Node $values): ValuesIn|ValuesInRows
173    {
174        $form = $this->lowering->form($values);
175        if ($form->signature === 'part_values_in: part_value_item' || $form->signature === 'part_values_in: part_value_item_list_paren') {
176            return new ValuesIn($this->row($form->node(0)));
177        }
178        if ($form->signature !== 'part_values_in: ( part_value_list )') {
179            throw ImplementationGap::production($form);
180        }
181        $this->spine($form->node(1));
182        $rows = [];
183        foreach ((new Lists())->items($form->node(1)) as $item) {
184            $rows[] = $this->row($item);
185        }
186
187        return new ValuesInRows($rows);
188    }
189
190    /**
191     * Lowers a parenthesized row of values: a node of `part_value_item` (5.6, 5.7) or `part_value_item_list_paren`.
192     *
193     * @throws ImplementationGap When a production has no rule
194     */
195    public function row(Node $row): PartitionRow
196    {
197        $form = $this->lowering->form($row);
198        if (!isset(self::ROWS[$form->signature])) {
199            throw ImplementationGap::production($form);
200        }
201        $this->spine($form->node(1));
202        $items = [];
203        foreach ((new Lists())->items($form->node(1)) as $item) {
204            $items[] = $this->item($item);
205        }
206
207        return new PartitionRow($items);
208    }
209
210    /**
211     * Lowers one value of a row: a node of `part_value_expr_item` (5.6, 5.7) or `part_value_item`.
212     *
213     * @throws ImplementationGap When a production has no rule
214     */
215    public function item(Node $item): Scalar|PartitionMaximum
216    {
217        $form = $this->lowering->form($item);
218
219        return match ($form->signature) {
220            'part_value_expr_item: MAX_VALUE_SYM', 'part_value_item: MAX_VALUE_SYM' => new PartitionMaximum(),
221            'part_value_expr_item: bit_expr', 'part_value_item: bit_expr' => $this->lowering->expressions->bitExpression($form->node(0)),
222            default => throw ImplementationGap::production($form),
223        };
224    }
225
226    /**
227     * Lowers the subpartitions of a partition: a node of `opt_sub_partition`; none is empty.
228     *
229     * @return list<SubpartitionDefinition>
230     * @throws ImplementationGap When a production has no rule
231     */
232    public function subpartitions(Node $list): array
233    {
234        $form = $this->lowering->form($list);
235        if ($form->signature === 'opt_sub_partition:') {
236            return [];
237        }
238        if ($form->signature !== 'opt_sub_partition: ( sub_part_list )') {
239            throw ImplementationGap::production($form);
240        }
241        $this->spine($form->node(1));
242        $subpartitions = [];
243        foreach ((new Lists())->items($form->node(1)) as $item) {
244            $subpartitions[] = $this->subpartition($item);
245        }
246
247        return $subpartitions;
248    }
249
250    /**
251     * Lowers one subpartition: a node of `sub_part_definition`.
252     *
253     * @throws ImplementationGap When a production has no rule
254     */
255    public function subpartition(Node $definition): SubpartitionDefinition
256    {
257        $form = $this->lowering->form($definition);
258        $name = match ($form->signature) {
259            'sub_part_definition: SUBPARTITION_SYM sub_name opt_part_options' => $this->lowering->form($form->node(1)),
260            'sub_part_definition: SUBPARTITION_SYM ident_or_text opt_part_options' => null,
261            default => throw ImplementationGap::production($form),
262        };
263        if ($name !== null && $name->signature !== 'sub_name: ident_or_text') {
264            throw ImplementationGap::production($name);
265        }
266
267        return new SubpartitionDefinition(
268            $this->lowering->names->identifier($name === null ? $form->node(1) : $name->node(0)),
269            (new OptionRule($this->lowering))->options($form->node(2)),
270        );
271    }
272}
273