packages/sql-semantics-mysql/src/Lowering/Expression/PrimaryRule.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\Expression;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\MySql\Lowering\Lowering;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\ImplementationGap;
10use SqlSemantics\Platform\MySql\Model\Lowering\Form;
11use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
12use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Collated;
13use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Concatenation;
14use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Unary;
15use SqlSemantics\Platform\MySql\Statement\Expression\Operator\UnaryOperator;
16
17/**
18 * Lowers the simple_expr level: names, literals, parameters, variables, calls and the prefix and postfix operators.
19 *
20 * Rule: MYSQL-SIMPLE-EXPR-001. Scope: simple_expr, not2. COLLATE and the
21 * concatenation `||` of PIPES_AS_CONCAT are left recursive; the left spine
22 * is walked in a loop. A function-like form is handed to the call family,
23 * a name, literal, parameter or variable to the leaf rules, and the
24 * bracketed, subquery and keyword forms to MYSQL-SIMPLE-FORM-001 and
25 * MYSQL-CONSTRUCT-001. Under HIGH_NOT_PRECEDENCE the keyword NOT is the
26 * tight negation `!`. Constructs: Collated, Concatenation, Unary.
27 * Terminates: the spine loop descends one left child per step; every
28 * other child is a strict subtree.
29 * Source: https://dev.mysql.com/doc/refman/8.4/en/expressions.html.
30 * Status: Implemented.
31 *
32 * @visibility SqlSemantics\Platform\MySql
33 */
34final class PrimaryRule
35{
36 /**
37 * The productions whose only child is a function-like expression.
38 */
39 private const CALLS = [
40 'simple_expr: function_call_keyword' => true, 'simple_expr: function_call_nonkeyword' => true, 'simple_expr: function_call_generic' => true,
41 'simple_expr: function_call_conflict' => true, 'simple_expr: sum_expr' => true, 'simple_expr: set_function_specification' => true,
42 'simple_expr: window_func_call' => true,
43 ];
44
45 /**
46 * The productions whose only child is a leaf: the leaf kind.
47 */
48 private const LEAVES = [
49 'simple_expr: simple_ident' => 'column', 'simple_expr: literal' => 'literal', 'simple_expr: literal_or_null' => 'literal',
50 'simple_expr: param_marker' => 'parameter', 'simple_expr: variable' => 'variable', 'simple_expr: rvalue_system_or_user_variable' => 'variable',
51 'simple_expr: in_expression_user_variable_assignment' => 'variable',
52 ];
53
54 /**
55 * The prefix operator productions.
56 */
57 private const PREFIX = [
58 'simple_expr: + simple_expr' => UnaryOperator::Plus, 'simple_expr: - simple_expr' => UnaryOperator::Minus,
59 'simple_expr: ~ simple_expr' => UnaryOperator::Invert, 'simple_expr: not2 simple_expr' => UnaryOperator::Not,
60 ];
61
62 /**
63 * The left-recursive productions.
64 */
65 private const POSTFIX = ['simple_expr: simple_expr COLLATE_SYM ident_or_text' => true, 'simple_expr: simple_expr OR_OR_SYM simple_expr' => true];
66
67 /**
68 * @param Lowering $lowering The lowering this rule belongs to
69 */
70 public function __construct(private readonly Lowering $lowering)
71 {
72 }
73
74 /**
75 * Lowers a node of simple_expr.
76 *
77 * @throws ImplementationGap When a production has no rule
78 */
79 public function simpleExpression(Node $expression): Scalar
80 {
81 $pending = [];
82 $form = $this->lowering->form($expression);
83 while (isset(self::POSTFIX[$form->signature])) {
84 $pending[] = $form;
85 $form = $this->lowering->form($form->node(0));
86 }
87 $result = $this->unit($form);
88 foreach (array_reverse($pending) as $operation) {
89 $result = $operation->signature === 'simple_expr: simple_expr OR_OR_SYM simple_expr'
90 ? new Concatenation($result, $this->simpleExpression($operation->node(2)))
91 : new Collated($result, $this->lowering->names->identifier($operation->node(2)));
92 }
93
94 return $result;
95 }
96
97 /**
98 * Lowers a simple_expr production that is not left recursive.
99 *
100 * @throws ImplementationGap When a production has no rule
101 */
102 public function unit(Form $form): Scalar
103 {
104 if (isset(self::CALLS[$form->signature])) {
105 return $this->lowering->calls->call($form->node(0));
106 }
107 if (isset(self::LEAVES[$form->signature])) {
108 return $this->leaf(self::LEAVES[$form->signature], $form->node(0));
109 }
110 $prefix = self::PREFIX[$form->signature] ?? null;
111 if ($prefix !== null) {
112 if ($prefix === UnaryOperator::Not) {
113 $this->negation($form->node(0));
114 }
115
116 return new Unary($prefix, $this->simpleExpression($form->node(1)));
117 }
118
119 return (new FormRule($this->lowering))->lower($form);
120 }
121
122 /**
123 * Lowers a leaf of a kind.
124 */
125 public function leaf(string $kind, Node $leaf): Scalar
126 {
127 return match ($kind) {
128 'column' => $this->lowering->names->column($leaf),
129 'literal' => $this->lowering->literals->literal($leaf),
130 'parameter' => $this->lowering->literals->parameter($leaf),
131 default => $this->lowering->variables->variable($leaf),
132 };
133 }
134
135 /**
136 * Confirms the tight negation keyword: `!`, or NOT under HIGH_NOT_PRECEDENCE.
137 *
138 * @throws ImplementationGap When the production has no rule
139 */
140 public function negation(Node $keyword): void
141 {
142 $form = $this->lowering->form($keyword);
143 if ($form->signature !== 'not2: !' && $form->signature !== 'not2: NOT2_SYM') {
144 throw ImplementationGap::production($form);
145 }
146 }
147}
148