The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
fix clickhouse parameter rewriting on sql-tools (#78110)
Nicola Mometto67de65ca3b4e
diff --git a/resources/python-sources/sql_tools.py b/resources/python-sources/sql_tools.pyindex 9cf4f3cf43f..a12f883dac4 100644--- a/resources/python-sources/sql_tools.py+++ b/resources/python-sources/sql_tools.py@@ -6,8 +6,26 @@ import sqlglot.lineage as lineageimport sqlglot.optimizer as optimizerimport sqlglot.optimizer.qualify as qualifyfrom sqlglot import exp+from sqlglot.dialects.clickhouse import ClickHousefrom sqlglot.errors import OptimizeError, ParseError+# sqlglot (as of 28.6.0) renders every placeholder in ClickHouse's named-parameter+# syntax `{name: Type}`, so a positional JDBC placeholder `?` (a nameless Placeholder)+# round-trips to `{?: }`, which ClickHouse rejects with "Expected substitution name"+# (Code 62). Compiled queries we rewrite can carry prepared-statement params — e.g.+# workspace table remapping of an incremental transform's checkpoint filter — so keep+# positional placeholders as `?`; named query parameters still take the upstream path.+_clickhouse_placeholder_sql = ClickHouse.Generator.placeholder_sql+++def _placeholder_sql_keep_positional(self, expression: exp.Placeholder) -> str:+ if not expression.this:+ return "?"+ return _clickhouse_placeholder_sql(self, expression)+++ClickHouse.Generator.placeholder_sql = _placeholder_sql_keep_positional+def is_quoted_identifier(name: str, dialect: str = None) -> bool:if not isinstance(name, str):diff --git a/test/metabase/sql_tools/sqlglot/replace_names_test.clj b/test/metabase/sql_tools/sqlglot/replace_names_test.cljindex 4e350f991da..c61e3675c30 100644--- a/test/metabase/sql_tools/sqlglot/replace_names_test.clj+++ b/test/metabase/sql_tools/sqlglot/replace_names_test.clj@@ -15,6 +15,7 @@:postgres "postgres":mysql "mysql":sqlserver "tsql"+ :clickhouse "clickhouse"nil)replacements' (-> replacements(update :tables #(when % (vec %)))@@ -119,3 +120,19 @@(replace-names :postgres"SELECT * FROM orders"{:tables {{:table "orders"} {:schema "public" :table "orders"}}})))))++(deftest ^:parallel clickhouse-positional-placeholders-preserved-test+ (testing "ClickHouse: positional `?` placeholders survive the rewrite"+ ;; sqlglot renders ClickHouse placeholders in named-parameter syntax, which turns a+ ;; positional `?` into the invalid `{?: }` — this broke workspace-isolated incremental+ ;; transforms, whose compiled checkpoint filters carry prepared-statement params+ ;; (GDGT-2847). Patched in sql_tools.py.+ (is (= "SELECT * FROM \"zz\".\"iso__src\" WHERE (\"ts\" > \"parseDateTimeBestEffort\"(?)) AND (\"ts\" <= \"parseDateTimeBestEffort\"(?))"+ (replace-names :clickhouse+ "SELECT * FROM `zz`.`src` WHERE (`ts` > `parseDateTimeBestEffort`(?)) AND (`ts` <= `parseDateTimeBestEffort`(?))"+ {:tables {{:schema "zz" :table "src"} "iso__src"}}))))+ (testing "ClickHouse: named query parameters still render as {name: Type}"+ (is (= "SELECT * FROM \"zz\".\"iso__src\" WHERE \"id\" = {uid: UInt32}"+ (replace-names :clickhouse+ "SELECT * FROM `zz`.`src` WHERE `id` = {uid: UInt32}"+ {:tables {{:schema "zz" :table "src"} "iso__src"}})))))