rusackas commented on code in PR #41803: URL: https://github.com/apache/superset/pull/41803#discussion_r3724880446
########## superset/sql/dialects/trino.py: ########## @@ -0,0 +1,413 @@ +# 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, or a branch/loop keyword that introduces a nested statement +# list. 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( Review Comment: Confirmed — the label's trailing `:` isn't in `_STATEMENT_START_PREV_TEXTS`, so the loop opener was never counted while its `END WHILE` still decremented depth, unbalancing the block. Added `:` to that set and a test with your `top: WHILE ... END WHILE` example. -- 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]
