rusackas commented on code in PR #41803:
URL: https://github.com/apache/superset/pull/41803#discussion_r3845783764


##########
superset/sql/dialects/trino.py:
##########
@@ -0,0 +1,469 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+import typing as t
+
+from sqlglot import exp
+from sqlglot.dialects.trino import Trino as SqlglotTrino
+from sqlglot.tokens import Token, TokenType
+
+# Keywords that open a block terminated by ``END`` in Trino SQL routines
+# (https://trino.io/docs/current/udf/sql.html). ``CASE`` is included because
+# both the ``CASE`` statement and the ``CASE`` expression are terminated by
+# ``END``, so counting them keeps the depth balanced either way.
+BLOCK_OPENERS: set[str] = {"BEGIN", "CASE", "IF", "LOOP", "REPEAT", "WHILE"}
+
+# Keywords that are also scalar functions in Trino (e.g. ``IF(a, b, c)`` and
+# ``REPEAT('a', 3)``). When immediately followed by ``(`` they are function
+# calls, not block openers, unless the token stream shows otherwise (see
+# ``_is_paren_condition_block``).
+AMBIGUOUS_OPENERS: set[str] = {"IF", "REPEAT"}
+
+BODY_KEYWORDS: tuple[str, str] = ("RETURN", "BEGIN")
+
+# ``BEGIN``, ``CASE``, and ``END`` are reserved words in sqlglot's Trino
+# tokenizer, so they always carry one of these dedicated token types when
+# used as keywords, and a different one (``STRING``/``IDENTIFIER``) when
+# used as a string literal or quoted identifier, e.g. the string ``'END'``
+# or the quoted identifier ``"end"``. ``IF``, ``LOOP``, ``REPEAT``, and
+# ``WHILE`` are not reserved, so the tokenizer emits ``VAR`` for them both
+# when they're used as a keyword and when they're an unquoted identifier;
+# requiring ``VAR`` still rules out string literals and quoted identifiers,
+# which is the ambiguity ``_is_keyword_token`` guards against.
+_RESERVED_BLOCK_TOKEN_TYPES: dict[str, TokenType] = {
+    "BEGIN": TokenType.BEGIN,
+    "CASE": TokenType.CASE,
+    "END": TokenType.END,
+}
+
+# Token text that can immediately precede a new routine statement inside a
+# ``BEGIN ... END`` body: the start of the body itself, a statement
+# separator, a branch/loop keyword that introduces a nested statement list,
+# or ``:`` following a statement label (e.g. ``top: WHILE ... END WHILE``).
+# Used by ``_is_routine_keyword`` to tell a non-reserved block-opening
+# keyword (``IF``, ``LOOP``, ``REPEAT``, ``WHILE``) apart from an unquoted
+# routine parameter or column reference spelled the same way, since Trino
+# does not reserve these words and its tokenizer emits ``VAR`` for both.
+_STATEMENT_START_PREV_TEXTS: frozenset[str] = frozenset(
+    {"BEGIN", ";", "THEN", "ELSE", "DO", "LOOP", "REPEAT", ":"}
+)
+
+
+def _is_keyword_token(token: Token, text: str) -> bool:
+    """
+    Determine whether ``token`` (whose upper-cased text is ``text``) is an
+    actual occurrence of a routine keyword, as opposed to a string literal
+    or quoted identifier that happens to spell the same word.
+    """
+    if (expected := _RESERVED_BLOCK_TOKEN_TYPES.get(text)) is not None:
+        return token.token_type == expected
+    return token.token_type == TokenType.VAR
+
+
+def _is_routine_keyword(token: Token, text: str, prev_text: str) -> bool:
+    """
+    Determine whether ``token`` is an actual occurrence of a routine block
+    keyword, as opposed to a string literal or quoted identifier that
+    happens to spell the same word (see ``_is_keyword_token``), or, for the
+    non-reserved keywords (``IF``, ``LOOP``, ``REPEAT``, ``WHILE``), an
+    unquoted parameter or column reference spelled the same way, e.g. a UDF
+    parameter named ``loop`` in ``RETURN loop``. A block-opening keyword only
+    ever appears where a new statement can start, so ``prev_text`` (the
+    upper-cased text of the immediately preceding token) is checked against
+    ``_STATEMENT_START_PREV_TEXTS`` for these ambiguous, non-reserved words.
+    """
+    if not _is_keyword_token(token, text):
+        return False
+    if text in _RESERVED_BLOCK_TOKEN_TYPES:
+        return True
+    return prev_text in _STATEMENT_START_PREV_TEXTS
+
+
+def _is_paren_condition_block(tokens: t.Sequence[Token], paren_index: int) -> 
bool:
+    """
+    Determine whether the parenthesized group starting at 
``tokens[paren_index]``
+    (an ``L_PAREN``) is a procedural block condition, e.g. ``IF (a > b) THEN``,
+    as opposed to a scalar function call argument list, e.g. ``IF(a, b, c)``.
+
+    Only ``IF`` has this ambiguity: a parenthesized condition is followed by
+    ``THEN``, while a scalar function call's closing paren never is.
+    """
+    depth = 0
+    for i in range(paren_index, len(tokens)):
+        token_type = tokens[i].token_type
+        if token_type == TokenType.L_PAREN:
+            depth += 1
+        elif token_type == TokenType.R_PAREN:
+            depth -= 1
+            if depth == 0:
+                next_token = tokens[i + 1] if i + 1 < len(tokens) else None
+                return (
+                    next_token is not None and next_token.token_type == 
TokenType.THEN
+                )
+    return False
+
+
+def _extract_function_calls(tokens: t.Sequence[Token]) -> list[exp.Anonymous]:
+    """
+    Scan the raw tokens of an inline UDF specification for scalar function
+    calls, e.g. ``regexp_replace(...)`` in ``RETURN regexp_replace(...)``, so
+    that ``SQLScript.check_functions_present`` still sees them even though
+    the UDF body itself is kept as opaque, verbatim text.
+
+    A call is any word-like token immediately followed by ``(``. Most scalar
+    functions tokenize as plain ``VAR`` (Trino's tokenizer does not
+    distinguish an unquoted identifier from an unreserved keyword), but a few
+    (e.g. ``current_user``, ``localtime``) are reserved words with their own
+    dedicated ``TokenType`` and would otherwise slip past a ``VAR``-only
+    check while still being callable with parentheses, so the token text
+    itself (rather than its type) decides whether it looks like a call head.
+    This can also match a routine/parameter type name (e.g. ``varchar(10)``),
+    a keyword used with parenthesized syntax (e.g. ``CAST(...)``, ``IN
+    (...)``), or the UDF's own name at its declaration site; those false
+    positives are harmless here, since this list is only used to check for
+    the presence of specific denylisted function names, not to validate the
+    call itself.
+    """
+    return [
+        exp.Anonymous(this=tokens[i - 1].text)
+        for i in range(1, len(tokens))
+        if tokens[i].token_type == TokenType.L_PAREN

Review Comment:
   Fixed. `_extract_function_calls` now also matches by token type (reusing 
sqlglot's own `NO_PAREN_FUNCTIONS` set), so the bare `current_user` form gets 
caught too, plus a test for it.



##########
superset/sql/dialects/trino.py:
##########
@@ -0,0 +1,469 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+import typing as t
+
+from sqlglot import exp
+from sqlglot.dialects.trino import Trino as SqlglotTrino
+from sqlglot.tokens import Token, TokenType
+
+# Keywords that open a block terminated by ``END`` in Trino SQL routines
+# (https://trino.io/docs/current/udf/sql.html). ``CASE`` is included because
+# both the ``CASE`` statement and the ``CASE`` expression are terminated by
+# ``END``, so counting them keeps the depth balanced either way.
+BLOCK_OPENERS: set[str] = {"BEGIN", "CASE", "IF", "LOOP", "REPEAT", "WHILE"}
+
+# Keywords that are also scalar functions in Trino (e.g. ``IF(a, b, c)`` and
+# ``REPEAT('a', 3)``). When immediately followed by ``(`` they are function
+# calls, not block openers, unless the token stream shows otherwise (see
+# ``_is_paren_condition_block``).
+AMBIGUOUS_OPENERS: set[str] = {"IF", "REPEAT"}
+
+BODY_KEYWORDS: tuple[str, str] = ("RETURN", "BEGIN")
+
+# ``BEGIN``, ``CASE``, and ``END`` are reserved words in sqlglot's Trino
+# tokenizer, so they always carry one of these dedicated token types when
+# used as keywords, and a different one (``STRING``/``IDENTIFIER``) when
+# used as a string literal or quoted identifier, e.g. the string ``'END'``
+# or the quoted identifier ``"end"``. ``IF``, ``LOOP``, ``REPEAT``, and
+# ``WHILE`` are not reserved, so the tokenizer emits ``VAR`` for them both
+# when they're used as a keyword and when they're an unquoted identifier;
+# requiring ``VAR`` still rules out string literals and quoted identifiers,
+# which is the ambiguity ``_is_keyword_token`` guards against.
+_RESERVED_BLOCK_TOKEN_TYPES: dict[str, TokenType] = {
+    "BEGIN": TokenType.BEGIN,
+    "CASE": TokenType.CASE,
+    "END": TokenType.END,
+}
+
+# Token text that can immediately precede a new routine statement inside a
+# ``BEGIN ... END`` body: the start of the body itself, a statement
+# separator, a branch/loop keyword that introduces a nested statement list,
+# or ``:`` following a statement label (e.g. ``top: WHILE ... END WHILE``).
+# Used by ``_is_routine_keyword`` to tell a non-reserved block-opening
+# keyword (``IF``, ``LOOP``, ``REPEAT``, ``WHILE``) apart from an unquoted
+# routine parameter or column reference spelled the same way, since Trino
+# does not reserve these words and its tokenizer emits ``VAR`` for both.
+_STATEMENT_START_PREV_TEXTS: frozenset[str] = frozenset(
+    {"BEGIN", ";", "THEN", "ELSE", "DO", "LOOP", "REPEAT", ":"}
+)
+
+
+def _is_keyword_token(token: Token, text: str) -> bool:
+    """
+    Determine whether ``token`` (whose upper-cased text is ``text``) is an
+    actual occurrence of a routine keyword, as opposed to a string literal
+    or quoted identifier that happens to spell the same word.
+    """
+    if (expected := _RESERVED_BLOCK_TOKEN_TYPES.get(text)) is not None:
+        return token.token_type == expected
+    return token.token_type == TokenType.VAR
+
+
+def _is_routine_keyword(token: Token, text: str, prev_text: str) -> bool:
+    """
+    Determine whether ``token`` is an actual occurrence of a routine block
+    keyword, as opposed to a string literal or quoted identifier that
+    happens to spell the same word (see ``_is_keyword_token``), or, for the
+    non-reserved keywords (``IF``, ``LOOP``, ``REPEAT``, ``WHILE``), an
+    unquoted parameter or column reference spelled the same way, e.g. a UDF
+    parameter named ``loop`` in ``RETURN loop``. A block-opening keyword only
+    ever appears where a new statement can start, so ``prev_text`` (the
+    upper-cased text of the immediately preceding token) is checked against
+    ``_STATEMENT_START_PREV_TEXTS`` for these ambiguous, non-reserved words.
+    """
+    if not _is_keyword_token(token, text):
+        return False
+    if text in _RESERVED_BLOCK_TOKEN_TYPES:
+        return True
+    return prev_text in _STATEMENT_START_PREV_TEXTS
+
+
+def _is_paren_condition_block(tokens: t.Sequence[Token], paren_index: int) -> 
bool:
+    """
+    Determine whether the parenthesized group starting at 
``tokens[paren_index]``
+    (an ``L_PAREN``) is a procedural block condition, e.g. ``IF (a > b) THEN``,
+    as opposed to a scalar function call argument list, e.g. ``IF(a, b, c)``.
+
+    Only ``IF`` has this ambiguity: a parenthesized condition is followed by
+    ``THEN``, while a scalar function call's closing paren never is.
+    """
+    depth = 0
+    for i in range(paren_index, len(tokens)):
+        token_type = tokens[i].token_type
+        if token_type == TokenType.L_PAREN:
+            depth += 1
+        elif token_type == TokenType.R_PAREN:
+            depth -= 1
+            if depth == 0:
+                next_token = tokens[i + 1] if i + 1 < len(tokens) else None
+                return (
+                    next_token is not None and next_token.token_type == 
TokenType.THEN
+                )
+    return False
+
+
+def _extract_function_calls(tokens: t.Sequence[Token]) -> list[exp.Anonymous]:
+    """
+    Scan the raw tokens of an inline UDF specification for scalar function
+    calls, e.g. ``regexp_replace(...)`` in ``RETURN regexp_replace(...)``, so
+    that ``SQLScript.check_functions_present`` still sees them even though
+    the UDF body itself is kept as opaque, verbatim text.
+
+    A call is any word-like token immediately followed by ``(``. Most scalar
+    functions tokenize as plain ``VAR`` (Trino's tokenizer does not
+    distinguish an unquoted identifier from an unreserved keyword), but a few
+    (e.g. ``current_user``, ``localtime``) are reserved words with their own
+    dedicated ``TokenType`` and would otherwise slip past a ``VAR``-only
+    check while still being callable with parentheses, so the token text
+    itself (rather than its type) decides whether it looks like a call head.
+    This can also match a routine/parameter type name (e.g. ``varchar(10)``),
+    a keyword used with parenthesized syntax (e.g. ``CAST(...)``, ``IN
+    (...)``), or the UDF's own name at its declaration site; those false
+    positives are harmless here, since this list is only used to check for
+    the presence of specific denylisted function names, not to validate the
+    call itself.
+    """
+    return [
+        exp.Anonymous(this=tokens[i - 1].text)
+        for i in range(1, len(tokens))
+        if tokens[i].token_type == TokenType.L_PAREN
+        and tokens[i - 1].text.isidentifier()
+    ]
+
+
+class InlineUDF(exp.CTE):
+    """
+    An inline SQL user-defined function declared in a ``WITH`` clause.
+
+    Trino supports declaring UDFs inline as part of a query::
+
+        WITH FUNCTION meaning_of_life()
+          RETURNS tinyint
+          BEGIN
+            DECLARE a tinyint DEFAULT CAST(6 AS tinyint);
+            DECLARE b tinyint DEFAULT CAST(7 AS tinyint);
+            RETURN a * b;
+          END
+        SELECT meaning_of_life()
+
+    The function definition is stored verbatim as an opaque string (wrapped
+    in an ``exp.Var`` so that AST traversal helpers see an expression), since
+    sqlglot has no representation for SQL routine bodies. Trino does not
+    allow queries inside SQL UDF bodies, so no table references are hidden
+    by the opaque representation. Scalar function calls, however, would be
+    hidden from ``SQLScript.check_functions_present`` (used to enforce
+    ``DISALLOWED_SQL_FUNCTIONS``) since it walks the AST for ``exp.Func``
+    nodes, so those are additionally extracted into ``expressions`` as
+    ``exp.Anonymous`` nodes; they play no part in regenerating the SQL.
+
+    This subclasses ``exp.CTE`` because ``sqlglot.parser.Parser._parse_with``
+    only collects ``exp.CTE`` instances into the ``WITH`` clause.
+    """
+
+    arg_types = {"this": True, "expressions": False}
+
+
+class Trino(SqlglotTrino):
+    """
+    Custom Trino dialect with support for inline SQL UDFs.
+
+    sqlglot cannot parse Trino SQL routine syntax; see
+    https://github.com/tobymao/sqlglot/issues/5178. There are two separate
+    problems:
+
+    1. The parser splits statements on every semicolon, including the ones
+       inside a ``BEGIN ... END`` routine body.
+    2. The ``FUNCTION`` specification in a ``WITH`` clause is not valid CTE
+       syntax.
+
+    This dialect keeps routine bodies intact when splitting statements, and
+    parses inline function specifications into opaque `InlineUDF` nodes that
+    regenerate verbatim.
+
+    Note that sqlglot's ``Dialect`` metaclass registers subclasses by class
+    name, so once this module is imported this class also replaces the
+    built-in dialect for string-based lookups (``dialect="trino"``). This is
+    intentional, and consistent with how other Superset dialects (e.g.
+    ``Dremio``) shadow their sqlglot counterparts: the extensions are purely
+    additive, only activating on syntax that fails to parse upstream.
+    """
+
+    class Parser(SqlglotTrino.Parser):
+        @staticmethod
+        def _block_depth_delta(
+            tokens: list[Token],
+            index: int,
+            prev_text: str,
+        ) -> int:
+            """
+            Compute the block nesting change contributed by the routine token
+            at ``tokens[index]``.
+            """
+            token = tokens[index]
+            text = token.text.upper()
+            if text in BLOCK_OPENERS:
+                if not _is_routine_keyword(token, text, prev_text):
+                    return 0  # literal, identifier, or parameter reference
+                if prev_text == "END":
+                    return 0  # block terminator, e.g. `END IF`, `END CASE`
+                next_token = tokens[index + 1] if index + 1 < len(tokens) else 
None
+                if (
+                    text in AMBIGUOUS_OPENERS
+                    and next_token
+                    and next_token.token_type == TokenType.L_PAREN
+                ):
+                    if text == "IF" and _is_paren_condition_block(tokens, 
index + 1):
+                        return 1  # procedural `IF (...) THEN`, not a call
+                    return 0  # scalar function call, e.g. `IF(a, b, c)`
+                return 1
+            if text == "END" and _is_routine_keyword(token, text, prev_text):
+                return -1
+            return 0
+
+        @staticmethod
+        def _starts_routine(
+            heads: list[TokenType],
+            next_token_type: TokenType | None,
+            paren_depth: int,
+        ) -> bool:
+            """
+            Determine whether a ``FUNCTION`` token at the end of ``heads``
+            (excluded from the list) begins a new routine specification:
+            ``CREATE FUNCTION``, ``CREATE OR REPLACE FUNCTION``, or an entry
+            in a ``WITH`` list, either right after ``WITH`` itself or after a
+            top-level comma separating it from a preceding CTE, e.g.
+            ``WITH cte AS (...), FUNCTION f() ...``.
+
+            In the ``WITH`` case, ``FUNCTION`` may also just be an ordinary
+            CTE named "function", e.g. ``WITH function AS (...) SELECT ...``.
+            ``next_token_type`` (the token immediately after ``FUNCTION``) is
+            checked the same way ``_parse_cte`` disambiguates the two: a CTE
+            named "function" is followed by ``AS``, ``(``, or a comma (for a
+            column alias list), while a routine specification is followed by
+            the function name.
+            """
+            if heads[:1] == [TokenType.CREATE]:
+                return heads in (
+                    [TokenType.CREATE],
+                    [TokenType.CREATE, TokenType.OR, TokenType.REPLACE],
+                )
+            if heads[:1] == [TokenType.WITH]:
+                return (
+                    paren_depth == 0
+                    and heads[-1] in (TokenType.WITH, TokenType.COMMA)

Review Comment:
   Dug into this. Turns out it's not the heads check: Trino's tokenizer treats 
`EXPLAIN` as a passthrough command and swallows everything up to the first 
semicolon into one opaque `STRING` token, so `WITH`/`FUNCTION` never show up as 
separate tokens at all for this override to see. Fixing that means patching 
sqlglot's tokenizer, not just this `Parser` hook, and it'd touch how `EXPLAIN` 
parses everywhere, including the `is_mutating` check that relies on it staying 
an opaque `Command`. Feels like its own problem, separate from this PR's scope.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to