packages/sql-semantics-mysql/src/Lowering/Call/CallRules.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\Call;
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\Identifier\Name;
12use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
13use SqlSemantics\Platform\MySql\Statement\Call\JsonTableColumn;
14use SqlSemantics\Platform\MySql\Statement\Call\WindowSpecification;
15
16/**
17 * The entry rules of the call family: the methods other families and the statement dispatcher call.
18 *
19 * Rule: MYSQL-CALL-ENTRY-001. Scope: function calls, aggregates, window functions and specifications, and
20 * JSON_TABLE columns. A function-like expression is handed to the rule of
21 * its production: FunctionRule (keyword functions, CHAR, TRIM, POSITION and
22 * calls written as `name(...)`), TemporalRule (clock functions and the
23 * temporal special syntaxes), WeightRule (WEIGHT_STRING), JsonRule
24 * (JSON_VALUE and the JSON_TABLE columns), AggregateRule (set functions)
25 * and WindowRule (window functions and windows). The unit productions
26 * function_call_conflict: geometry_function and the two alternatives of
27 * set_function_specification forward to their only child.
28 * The method names, parameters and return types are fixed by the family
29 * plan. Terminates: each method lowers a strict subtree.
30 * Source: https://dev.mysql.com/doc/refman/8.4/en/functions.html.
31 * Status: Implemented.
32 *
33 * @visibility SqlSemantics\Platform\MySql
34 */
35final class CallRules
36{
37    private readonly FrameRule $frames;
38
39    private readonly WindowRule $windows;
40
41    /**
42     * @param Lowering $lowering The lowering this rule belongs to
43     */
44    public function __construct(public readonly Lowering $lowering)
45    {
46        $this->frames = new FrameRule($lowering);
47        $this->windows = new WindowRule($lowering, $this->frames);
48    }
49
50    /**
51     * Lowers a function-like expression: a node of `function_call_keyword`, `function_call_nonkeyword`,
52     * `function_call_generic`, `function_call_conflict`, `sum_expr`, `set_function_specification` or
53     * `window_func_call`.
54     *
55     * @throws ImplementationGap When a production has no rule
56     */
57    public function call(Node $call): Scalar
58    {
59        $form = $this->lowering->form($call);
60        if (in_array($form->signature, ['function_call_conflict: geometry_function', 'set_function_specification: sum_expr', 'set_function_specification: grouping_operation'], true)) {
61            return $this->call($form->node(0));
62        }
63        if ($call->name === 'sum_expr') {
64            return (new AggregateRule($this->lowering, $this->windows))->aggregate($call);
65        }
66        if ($call->name === 'window_func_call') {
67            return $this->windows->function($call);
68        }
69
70        return $this->function($form);
71    }
72
73    /**
74     * Lowers a function call production of function_call_keyword, function_call_nonkeyword, function_call_conflict,
75     * geometry_function, grouping_operation or function_call_generic.
76     *
77     * @throws ImplementationGap When the production has no rule
78     */
79    public function function(Form $form): Scalar
80    {
81        $functions = new FunctionRule($this->lowering);
82        if ($functions->claims($form->signature)) {
83            return $functions->call($form);
84        }
85        $temporal = new TemporalRule($this->lowering);
86        if ($temporal->claims($form->signature)) {
87            return $temporal->call($form);
88        }
89        $weights = new WeightRule($this->lowering);
90        if ($weights->claims($form->signature)) {
91            return $weights->call($form);
92        }
93        if ($form->signature === 'function_call_keyword: JSON_VALUE_SYM ( simple_expr , text_literal opt_returning_type opt_on_empty_or_error )') {
94            return (new JsonRule($this->lowering))->value($form);
95        }
96
97        throw ImplementationGap::production($form);
98    }
99
100    /**
101     * Lowers the current timestamp function of a column default or ON UPDATE clause: a node of `now`.
102     *
103     * @throws ImplementationGap When the production has no rule
104     */
105    public function currentTimestamp(Node $now): Scalar
106    {
107        return (new TemporalRule($this->lowering))->currentTimestamp($now);
108    }
109
110    /**
111     * Lowers a window name: a node of `window_name`.
112     *
113     * @throws ImplementationGap When the production has no rule
114     */
115    public function windowName(Node $name): Name
116    {
117        return $this->windows->name($name);
118    }
119
120    /**
121     * Lowers a window specification: a node of `window_spec`.
122     *
123     * @throws ImplementationGap When a production has no rule
124     */
125    public function windowSpecification(Node $specification): WindowSpecification
126    {
127        return $this->frames->specification($specification);
128    }
129
130    /**
131     * Lowers the COLUMNS clause of JSON_TABLE: a node of `columns_clause`.
132     *
133     * @return list<JsonTableColumn>
134     * @throws ImplementationGap When a production has no rule
135     */
136    public function jsonTableColumns(Node $columns): array
137    {
138        return (new JsonRule($this->lowering))->columns($columns);
139    }
140}
141