You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by va...@apache.org on 2025/09/02 19:32:25 UTC
(superset) branch fix/databricks-string-escaper created (now b32fd1b043)
This is an automated email from the ASF dual-hosted git repository.
vavila pushed a change to branch fix/databricks-string-escaper
in repository https://gitbox.apache.org/repos/asf/superset.git
at b32fd1b043 fix(databricks): string escaper v2
This branch includes the following new commits:
new b32fd1b043 fix(databricks): string escaper v2
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(superset) 01/01: fix(databricks): string escaper v2
Posted by va...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
vavila pushed a commit to branch fix/databricks-string-escaper
in repository https://gitbox.apache.org/repos/asf/superset.git
commit b32fd1b04359826dc51d49bf8ba47f34678e1a37
Author: Vitor Avila <vi...@preset.io>
AuthorDate: Tue Sep 2 16:30:37 2025 -0300
fix(databricks): string escaper v2
---
superset/db_engine_specs/databricks.py | 45 +++++++++++++++++++++++++---------
1 file changed, 34 insertions(+), 11 deletions(-)
diff --git a/superset/db_engine_specs/databricks.py b/superset/db_engine_specs/databricks.py
index 7f84802080..faadf9185a 100644
--- a/superset/db_engine_specs/databricks.py
+++ b/superset/db_engine_specs/databricks.py
@@ -72,19 +72,42 @@ class DatabricksStringType(types.TypeDecorator):
def monkeypatch_dialect() -> None:
"""
- Monkeypatch dialect to correctly escape single quotes.
-
- The Databricks SQLAlchemy dialect we currently use does not escape single quotes
- correctly -- it doubles the single quotes, instead of adding a backslash. The fixed
- version requires SQLAlchemy 2.0, which is not yet available in Superset.
+ Monkeypatch dialect to correctly escape single quotes for Databricks.
+
+ The Databricks SQLAlchemy dialect (<3.0) incorrectly escapes single quotes by
+ doubling them ('O''Hara') instead of using backslash escaping ('O\'Hara'). The
+ fixed version requires SQLAlchemy>=2.0, which is not yet compatible with Superset.
+
+ The challenge: DatabricksDialect inherits from HiveDialect and accesses the
+ `colspecs` type mapping through its parent class rather than its own. This means:
+ - Direct assignment (`DatabricksDialect.colspecs[types.String] = ...`) works because
+ DatabricksDialect.colspecs points to the same dict object as HiveDialect.colspecs,
+ but then it affects all other dialects that inherit from HiveDialect, causing
+ incorrect escaping for them.
+ - Shallow copying then modifying `DatabricksDialect.colspecs` doesn't work
+ because it's never accessed.
+
+ The solution: A context-aware approach by creating a custom String type
+ that checks which dialect is being used during SQL compilation, allowing
+ to apply Databricks-specific escaping only for Databricks connections.
"""
try:
- from sqlalchemy_databricks._dialect import DatabricksDialect
+ from pyhive.sqlalchemy_hive import HiveDialect
+ from sqlalchemy.engine.default import DefaultDialect
+
+ class ContextAwareStringType(types.TypeDecorator):
+ impl = types.String
+ cache_ok = True
+
+ def literal_processor(
+ self, dialect: DefaultDialect
+ ) -> Callable[[Any], str]:
+ if dialect.__class__.__name__ == "DatabricksDialect":
+ return DatabricksStringType().literal_processor(dialect)
+ return super().literal_processor(dialect)
+
+ HiveDialect.colspecs[types.String] = ContextAwareStringType
- # copy because the dictionary is shared with other subclasses if it hasn't been
- # overwritten
- DatabricksDialect.colspecs = DatabricksDialect.colspecs.copy()
- DatabricksDialect.colspecs[types.String] = DatabricksStringType
except ImportError:
pass
@@ -646,5 +669,5 @@ class DatabricksPythonConnectorEngineSpec(DatabricksDynamicBaseEngineSpec):
return uri, connect_args
-# remove once we've upgraded to SQLAlchemy 2.0 and the 2.x databricks-sqlalchemy lib
+# TODO: remove once we've upgraded to SQLAlchemy>=2.0 and databricks-sql-python>=3.x
monkeypatch_dialect()