packages/sql-semantics-mysql/src/Lowering/Query/Modern/PrimaryRule.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\Query\Modern;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\MySql\Lowering\Lowering;
9use SqlSemantics\Platform\MySql\Lowering\Query\Shared\Block;
10use SqlSemantics\Platform\MySql\Lowering\Query\Shared\ClauseRule;
11use SqlSemantics\Platform\MySql\Lowering\Query\Shared\FromRule;
12use SqlSemantics\Platform\MySql\Lowering\Query\Shared\ItemRule;
13use SqlSemantics\Platform\MySql\Lowering\Query\Shared\TableRule;
14use SqlSemantics\Platform\MySql\Lowering\Query\Shared\TailRule;
15use SqlSemantics\Platform\MySql\Model\Diagnostic\ImplementationGap;
16use SqlSemantics\Platform\MySql\Model\Lowering\Lists;
17use SqlSemantics\Platform\MySql\Model\Statement\Query;
18use SqlSemantics\Platform\MySql\Statement\Query\ExplicitTable;
19use SqlSemantics\Platform\MySql\Statement\Query\ValueRow;
20use SqlSemantics\Platform\MySql\Statement\Query\ValuesQuery;
21use SqlSemantics\Platform\MySql\Statement\Query\With\CommonTableExpression;
22use SqlSemantics\Platform\MySql\Statement\Query\With\With;
23
24/**
25 * Lowers the query primaries and the WITH clause of the 8.0 and later grammars.
26 *
27 * Rule: MYSQL-QUERY-PRIMARY-001. Scope: query_primary, query_specification
28 * (8.0 and later), table_value_constructor, values_row_list,
29 * row_value_explicit, explicit_table, with_clause, opt_with_clause,
30 * with_list, common_table_expr. A query specification is a query block
31 * whose INTO clause, when written, follows the select list. Constructs: the
32 * query block, ValuesQuery, ValueRow, ExplicitTable, With,
33 * CommonTableExpression. Terminates: lists are flattened iteratively.
34 * Source: https://dev.mysql.com/doc/refman/8.4/en/select.html,
35 * https://dev.mysql.com/doc/refman/8.4/en/values.html,
36 * https://dev.mysql.com/doc/refman/8.4/en/table.html,
37 * https://dev.mysql.com/doc/refman/8.4/en/with.html. Status: Implemented.
38 *
39 * @visibility SqlSemantics\Platform\MySql\Lowering\Query
40 */
41final class PrimaryRule
42{
43 /**
44 * The query specification productions by whether they write INTO after the select list and QUALIFY.
45 */
46 private const SPECIFICATIONS = [
47 'query_specification: SELECT_SYM select_options select_item_list into_clause opt_from_clause opt_where_clause opt_group_clause opt_having_clause opt_window_clause' => [true, false],
48 'query_specification: SELECT_SYM select_options select_item_list opt_from_clause opt_where_clause opt_group_clause opt_having_clause opt_window_clause' => [false, false],
49 'query_specification: SELECT_SYM select_options select_item_list into_clause opt_from_clause opt_where_clause opt_group_clause opt_having_clause opt_window_clause opt_qualify_clause' => [true, true],
50 'query_specification: SELECT_SYM select_options select_item_list opt_from_clause opt_where_clause opt_group_clause opt_having_clause opt_window_clause opt_qualify_clause' => [false, true],
51 ];
52
53 /**
54 * @param Lowering $lowering The lowering this rule belongs to
55 */
56 public function __construct(private readonly Lowering $lowering)
57 {
58 }
59
60 /**
61 * Lowers a query primary: a query block, a VALUES statement or a TABLE statement.
62 *
63 * @throws ImplementationGap When a production has no rule
64 */
65 public function primary(Node $primary): Block|Query
66 {
67 $form = $this->lowering->form($primary);
68
69 return match ($form->signature) {
70 'query_primary: query_specification' => $this->specification($form->node(0)),
71 'query_primary: table_value_constructor' => $this->values($form->node(0)),
72 'query_primary: explicit_table' => $this->explicit($form->node(0)),
73 default => throw ImplementationGap::production($form),
74 };
75 }
76
77 /**
78 * Lowers a query specification into an open query block.
79 *
80 * @throws ImplementationGap When a production has no rule
81 */
82 public function specification(Node $specification): Block
83 {
84 $form = $this->lowering->form($specification);
85 [$into, $qualify] = self::SPECIFICATIONS[$form->signature] ?? throw ImplementationGap::production($form);
86 $at = $into ? 4 : 3;
87 $clauses = new ClauseRule($this->lowering);
88 $items = new ItemRule($this->lowering);
89
90 return new Block(
91 $items->options($form->node(1)),
92 $items->items($form->node(2)),
93 $into ? (new TailRule($this->lowering))->into($form->node(3)) : null,
94 (new FromRule($this->lowering))->from($form->node($at)),
95 $clauses->predicate($form->node($at + 1)),
96 $clauses->grouping($form->node($at + 2)),
97 $clauses->predicate($form->node($at + 3)),
98 $clauses->windows($form->node($at + 4)),
99 $qualify ? $clauses->predicate($form->node($at + 5)) : null,
100 );
101 }
102
103 /**
104 * Lowers a VALUES statement.
105 *
106 * @throws ImplementationGap When a production has no rule
107 */
108 public function values(Node $constructor): ValuesQuery
109 {
110 $form = $this->lowering->form($constructor);
111 if ($form->signature !== 'table_value_constructor: VALUES values_row_list') {
112 throw ImplementationGap::production($form);
113 }
114 $list = $this->lowering->form($form->node(1));
115 if ($list->signature !== 'values_row_list: values_row_list , row_value_explicit' && $list->signature !== 'values_row_list: row_value_explicit') {
116 throw ImplementationGap::production($list);
117 }
118 $rows = [];
119 foreach ((new Lists())->items($list->node) as $item) {
120 $row = $this->lowering->form($item);
121 if ($row->signature !== 'row_value_explicit: ROW_SYM ( opt_values )') {
122 throw ImplementationGap::production($row);
123 }
124 $rows[] = new ValueRow($this->lowering->dml->rowValues($row->node(2)));
125 }
126
127 return new ValuesQuery($rows);
128 }
129
130 /**
131 * Lowers a TABLE statement.
132 *
133 * @throws ImplementationGap When the production has no rule
134 */
135 public function explicit(Node $table): ExplicitTable
136 {
137 $form = $this->lowering->form($table);
138 if ($form->signature !== 'explicit_table: TABLE_SYM table_ident') {
139 throw ImplementationGap::production($form);
140 }
141
142 return new ExplicitTable($this->lowering->names->qualified($form->node(1)));
143 }
144
145 /**
146 * Lowers a WITH clause; an absent clause is null.
147 *
148 * @throws ImplementationGap When a production has no rule
149 */
150 public function with(Node $clause): ?With
151 {
152 $form = $this->lowering->form($clause);
153 if ($form->signature === 'opt_with_clause:') {
154 return null;
155 }
156 if ($form->signature === 'opt_with_clause: with_clause') {
157 return $this->with($form->node(0));
158 }
159 $recursive = match ($form->signature) {
160 'with_clause: WITH with_list' => false,
161 'with_clause: WITH RECURSIVE_SYM with_list' => true,
162 default => throw ImplementationGap::production($form),
163 };
164 $list = $this->lowering->form($form->node($recursive ? 2 : 1));
165 if ($list->signature !== 'with_list: with_list , common_table_expr' && $list->signature !== 'with_list: common_table_expr') {
166 throw ImplementationGap::production($list);
167 }
168 $tables = [];
169 foreach ((new Lists())->items($list->node) as $item) {
170 $table = $this->lowering->form($item);
171 if ($table->signature !== 'common_table_expr: ident opt_derived_column_list AS table_subquery') {
172 throw ImplementationGap::production($table);
173 }
174 $tables[] = new CommonTableExpression($this->lowering->names->identifier($table->node(0)), (new TableRule($this->lowering))->columns($table->node(1)), (new ExpressionRule($this->lowering))->subquery($table->node(3)));
175 }
176
177 return new With($recursive, $tables);
178 }
179}
180