feat: Firebolt sqlglot dialect (#31825)

This commit is contained in:
Beto Dealmeida
2025-01-14 09:36:25 -05:00
committed by GitHub
parent 274aa143d3
commit c2d7cf388d
7 changed files with 136 additions and 14 deletions

View File

@@ -0,0 +1,16 @@
# 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.

View File

@@ -0,0 +1,75 @@
# 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
from sqlglot import exp, generator, parser
from sqlglot.dialects.dialect import Dialect
from sqlglot.tokens import TokenType
class Firebolt(Dialect):
"""
A sqlglot dialect for Firebolt.
"""
class Parser(parser.Parser):
"""
Custom parser for Firebolt.
In Firebolt `NOT` has higher precedence than `IN`, so we need to wrap the
expression in parentheses when we find a negated range.
"""
UNARY_PARSERS = {
**parser.Parser.UNARY_PARSERS,
TokenType.NOT: lambda self: self.expression(
exp.Not,
this=self._parse_unary(),
),
}
def _negate_range(
self,
this: exp.Expression | None = None,
) -> exp.Expression | None:
if not this:
return this
return self.expression(exp.Not, this=self.expression(exp.Paren, this=this))
class Generator(generator.Generator):
"""
Custom generator for Firebolt.
"""
TYPE_MAPPING = {
**generator.Generator.TYPE_MAPPING,
exp.DataType.Type.VARBINARY: "BYTEA",
}
def not_sql(self, expression: exp.Not) -> str:
"""
Parenthesize negated expressions.
Firebolt requires negated to be wrapped in parentheses, since NOT has higher
precedence than IN.
"""
if isinstance(expression.this, exp.In):
return f"NOT ({self.sql(expression, 'this')})"
return super().not_sql(expression)

View File

@@ -36,9 +36,12 @@ from sqlglot.optimizer.pushdown_predicates import pushdown_predicates
from sqlglot.optimizer.scope import Scope, ScopeType, traverse_scope
from superset.exceptions import SupersetParseError
from superset.sql.dialects.firebolt import Firebolt
logger = logging.getLogger(__name__)
# register 3rd party dialects
Dialect.classes["firebolt"] = Firebolt
# mapping between DB engine specs and sqlglot dialects
SQLGLOT_DIALECTS = {
@@ -62,7 +65,7 @@ SQLGLOT_DIALECTS = {
# "elasticsearch": ???
# "exa": ???
# "firebird": ???
# "firebolt": ???
"firebolt": "firebolt",
"gsheets": Dialects.SQLITE,
"hana": Dialects.POSTGRES,
"hive": Dialects.HIVE,
@@ -81,7 +84,7 @@ SQLGLOT_DIALECTS = {
"presto": Dialects.PRESTO,
"pydoris": Dialects.DORIS,
"redshift": Dialects.REDSHIFT,
# "risingwave": ???
"risingwave": Dialects.RISINGWAVE,
# "rockset": ???
"shillelagh": Dialects.SQLITE,
"snowflake": Dialects.SNOWFLAKE,