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 sqlglot parse failure for ?::type placeholder casts (#81381)
Riley Thompsonaf1c635d4ef7
diff --git a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.cljindex 676d22be23f..193021344e5 100644--- a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj+++ b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj@@ -19,6 +19,7 @@[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata]+ [metabase.lib.test-metadata :as meta][metabase.query-processor.compile :as qp.compile][metabase.query-processor.error-type :as qp.error-type][metabase.query-processor.test :as qp]@@ -1228,6 +1229,14 @@(is (= qp.error-type/invalid-query (:type (ex-data thrown))))(is (re-find #"single select statement" (ex-message thrown))))))))+(deftest ^:parallel impersonated-query-placeholder-cast-test+ (let [sql "SELECT (?::date - bill_date::date) AS diff FROM some_table"+ query (lib/native-query meta/metadata-provider sql)+ out-sql (-> (driver/validate-impersonated-query :postgres query)+ (get-in [:stages 0 :native]))]+ (is (string? out-sql))+ (is (re-find #"\?" out-sql) "the placeholder must survive into the re-emitted SQL")))+(deftest ^:parallel validate-impersonated-query-keys-on-allow-write-flag-test(testing "validate-impersonated-query* derives read-vs-write from *impersonation-allow-write?*"(let [query (fn [sql] {:stages [{:lib/type :mbql.stage/native :native sql}]})diff --git a/resources/python-sources/sql_tools.py b/resources/python-sources/sql_tools.pyindex 5b337fddba1..ca4c52e60f5 100644--- a/resources/python-sources/sql_tools.py+++ b/resources/python-sources/sql_tools.py@@ -1761,13 +1761,32 @@ def transpile_sql(sql: str, from_dialect: str = None, to_dialect: str = None):return json.dumps(result)+def _split_placeholder_casts(sql: str, dialect: str = None) -> str:+ """sqlglot's tokenizer treats `?::` as a single token in every dialect, but only Databricks'+ parser consumes it (the `expr?::type` try-cast operator). So a JDBC parameter followed by a+ cast (e.g. `select ?::text`) gets treated as this single token and fails to be parsed in every+ other dialect. To work around this we rewrite `?::` to `? ::` outside Databricks (#81373).+ """+ if dialect == "databricks" or "?::" not in sql:+ return sql+ try:+ tokens = sqlglot.tokenize(sql, read=dialect)+ except Exception:+ return sql+ # Insert right-to-left so earlier token offsets stay valid.+ for tok in reversed(tokens):+ if tok.token_type == sqlglot.TokenType.QDCOLON:+ sql = sql[: tok.start + 1] + " " + sql[tok.start + 1 :]+ return sql++def is_single_stmt_of_type(sql: str, stmt_type: str = "read", dialect: str = None) -> str:"""Validates that a query is a single read statement (SELECT) or a single write statement (INSERT, UPDATE, DELETE)and returns the query reconstructed from the parsed AST."""result = {"is_single_stmt?": False, "allowed_stmt_type?": False}try:- stmts = sqlglot.parse(sql, read=dialect)+ stmts = sqlglot.parse(_split_placeholder_casts(sql, dialect), read=dialect)allowed_types = (exp.Select, exp.SetOperation)if stmt_type != "read": allowed_types = (exp.Update, exp.Insert, exp.Delete)if len(stmts) == 1:diff --git a/test/metabase/sql_tools/core_test.clj b/test/metabase/sql_tools/core_test.cljindex 5f168d5090a..47fdcdf1afd 100644--- a/test/metabase/sql_tools/core_test.clj+++ b/test/metabase/sql_tools/core_test.clj@@ -234,6 +234,33 @@"SELECT * FROM foo EXCEPT ALL SELECT * FROM bar UNION ALL SELECT * FROM baz""SELECT * FROM foo EXCEPT ALL SELECT * FROM bar INTERSECT ALL SELECT * FROM baz"))))+(defn- placeholder-count+ [sql]+ (count (re-seq #"\?" sql)))++(deftest ^:parallel is-single-stmt-of-type-placeholder-cast-test+ (testing "queries with `?::` are parsed correctly"+ (doseq [sql ["SELECT ?::date"+ "SELECT (?::date - x::date)"+ "SELECT ?::text, ?::integer, ?::boolean FROM t WHERE x = ?"+ "SELECT (?::date - CURRENT_DATE) AS diff"]]+ (let [{out-sql :sql :as result} (sql-tools/is-single-stmt-of-type? :postgres sql "read")]+ (is (=? {:is-single-stmt? true :allowed-stmt-type? true :sql string?} result))+ (is (= (placeholder-count sql) (placeholder-count out-sql))))))+ (testing "a query with `?::` inside string literals are left untouched"+ (is (= {:is-single-stmt? true :allowed-stmt-type? true :sql "SELECT '?::date'"}+ (sql-tools/is-single-stmt-of-type? :postgres "select '?::date'" "read"))))+ (testing "multi-statement queries with placeholder casts are still rejected"+ (are [sql] (=? {:is-single-stmt? false :allowed-stmt-type? false}+ (sql-tools/is-single-stmt-of-type? :postgres sql "read"))+ "SELECT ?::date; DROP TABLE t"+ "SET ROLE NONE; SELECT ?::date")))++(deftest ^:parallel is-single-stmt-of-type-qdcolon-dialects-test+ (testing "databricks' native `expr?::type` try-cast operator is not split apart"+ (is (=? {:is-single-stmt? true :allowed-stmt-type? true :sql #"(?i).*TRY_CAST\(x AS DATE\).*"}+ (sql-tools/is-single-stmt-of-type? :databricks "SELECT x?::date FROM t" "read")))))+(deftest ^:parallel is-single-stmt-of-type-not-stripped-test(testing "we don't remove value clauses when validating impersonated queries (#74284)"(let [values-query (str "SELECT x FROM (VALUES " (str/join ", " (repeat 105 "(1)")) ") AS t(x)")]