Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

BOT-2541 An unclosed `[[` or `{{` in the SQL ends Metabot's SQL edit with "Something went wrong." (#86068)

Voytek Pitułac13299b2c5f3
diff --git a/src/metabase/metabot/tools/sql.clj b/src/metabase/metabot/tools/sql.clj
index 34bdef3b445..18ebd6dadad 100644
--- a/src/metabase/metabot/tools/sql.clj
+++ b/src/metabase/metabot/tools/sql.clj
@@ -156,8 +156,9 @@
[:title :string]])
(defn- sql-edit-result
- [operation-result title instruction-fn ctx]
- (let [{:keys [query-id query-content] :as action-result} (sql.common/validated-sql-action! operation-result)
+ [operation-result title instruction-fn ctx & {:keys [edit?]}]
+ (let [{:keys [query-id query-content] :as action-result} (sql.common/validated-sql-action!
+ operation-result {:edit? edit?})
structured (assoc action-result :result-type :query)
buffer-id (first-code-editor-buffer-id (some-> (:memory-atom ctx) deref))
instr (instruction-fn query-id (some? buffer-id))]
@@ -183,7 +184,7 @@
:edits edits
:checklist checklist
:queries-state (shared/current-queries-state (:memory-atom ctx))}))
- title instructions/edit-sql-query-instructions-for ctx)))
+ title instructions/edit-sql-query-instructions-for ctx {:edit? true})))
(def edit-sql-query-tool
"The tool that applies structured edits to a stored SQL query."
diff --git a/src/metabase/metabot/tools/sql/common.clj b/src/metabase/metabot/tools/sql/common.clj
index 120c393ebb6..15817719a0b 100644
--- a/src/metabase/metabot/tools/sql/common.clj
+++ b/src/metabase/metabot/tools/sql/common.clj
@@ -94,6 +94,20 @@
{:message "The SQL query contains more than one statement, and only one is supported. No changes were saved."
:recovery [{:uses #{} :text "Rewrite the SQL as one statement, then retry."}]})
+(tools/defrecoverable invalid-template-syntax!
+ "The proposed SQL has `{{...}}` or `[[...]]` syntax that Metabase rejects."
+ {:payload [:map {:closed true} [:error-message :string] [:edit? :boolean]]}
+ [{:keys [error-message edit?]}]
+ (let [prefix (str "The SQL query has invalid Metabase `{{...}}` or `[[...]]` syntax. "
+ (when edit? "The problem can be in SQL that this edit did not change. ")
+ "No changes were saved. Parser details: ")
+ recovery [{:uses #{} :text (str "Close each `{{` with `}}` and each `[[` with `]]`. Give each `{{...}}` a name, "
+ "such as `{{category}}`. Put at least one `{{...}}` variable in each `[[...]]`, "
+ "or remove the `[[...]]` clause. Then retry.")}]
+ budget (tools.runtime/output-budget prefix recovery)]
+ {:message (str prefix (bounded-quote error-message budget))
+ :recovery recovery}))
+
(tools/defrecoverable blank-sql!
"Creating, replacing, or editing the query would leave it with no SQL."
{:payload [:map {:closed true} [:query-id {:optional true} :string]]}
@@ -134,13 +148,17 @@
[:action-result {:optional true} ::action-result]])
(defn validated-sql-action!
- "Return the action result from an operation result, or raise the recoverable error for SQL that failed validation."
- [{:keys [validation-result action-result]}]
+ "Return the action result from an operation result, or raise the recoverable error for SQL that failed validation.
+
+ Set `:edit?` when the validated SQL keeps text from before the call, as `edit_sql_query` does."
+ [{:keys [validation-result action-result]} & {:keys [edit?]}]
(let [{:keys [valid? dialect error-message reason]} validation-result]
(cond
- valid? action-result
- (= :multiple-statements reason) (multiple-sql-statements! {})
- :else (sql-syntax-error! {:dialect dialect :error-message error-message}))))
+ valid? action-result
+ (= :multiple-statements reason) (multiple-sql-statements! {})
+ (= :invalid-template-syntax reason) (invalid-template-syntax! {:error-message error-message
+ :edit? (boolean edit?)})
+ :else (sql-syntax-error! {:dialect dialect :error-message error-message}))))
(defn native-query-access?
"Whether the current user may write and run native queries against `database-id`.
diff --git a/src/metabase/metabot/tools/sql/validation.clj b/src/metabase/metabot/tools/sql/validation.clj
index 4725c3d1f61..8e695f55436 100644
--- a/src/metabase/metabot/tools/sql/validation.clj
+++ b/src/metabase/metabot/tools/sql/validation.clj
@@ -2,7 +2,9 @@
(:require
[clojure.string :as str]
[metabase.driver.util :as driver.u]
+ [metabase.lib.core :as lib]
[metabase.sql-tools.core :as sql-tools]
+ [metabase.util.i18n :as i18n]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]))
@@ -61,6 +63,29 @@
(boolean (and (string? sql)
(re-find #"\{\{|\[\[" sql))))
+(defn- template-syntax-error
+ "The parser message for `{{...}}` or `[[...]]` syntax in `sql` that Metabase rejects, or `nil`.
+
+ Metabase cannot make a query when the lax parse of [[lib/recognize-template-tags]] throws. It does the strict parse
+ of SQL drivers before a run only when the query has template tags; it sends other SQL, for example
+ `ARRAY[[1,2],[3,4]]`, to the database as it is. This check uses the same two rules. The strict parse marks its
+ syntax errors with `:type :invalid-query`. Other exceptions are not about the SQL, so they propagate."
+ [sql]
+ (when (and (contains-template-tags? sql)
+ (try
+ (seq (lib/recognize-template-tags sql))
+ (catch clojure.lang.ExceptionInfo _
+ true)))
+ (try
+ ;; Model-facing: the parser builds its messages with `tru`, so get them in English, never in the user's locale.
+ (binding [i18n/*user-locale* "en"]
+ (lib/parse-parameters sql))
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (if (= :invalid-query (:type (ex-data e)))
+ (ex-message e)
+ (throw e))))))
+
(mr/def ::validation-result
[:map
[:valid? :boolean]
@@ -72,7 +97,10 @@
(mu/defn validate-sql :- ::validation-result
"Validate sql query.
- Validation is short-circuited and query is considered valid for following cases:
+ For every dialect, sql with `{{...}}` or `[[...]]` syntax that Metabase rejects is not valid, with `:reason`
+ `:invalid-template-syntax` and the parser message in `:error-message`. Metabase cannot run such sql.
+
+ Otherwise validation is short-circuited and query is considered valid for following cases:
- sql string is empty,
- dialect is `nil` or maps to `nil` (e.g. h2, vertica),
- dialect is not in [[dialect-mapping]],
@@ -86,15 +114,25 @@
with the Python ai-service."
[dialect :- [:maybe :string]
sql :- :string]
- (let [mapped-dialect (get dialect-mapping dialect)]
- (if (or (nil? dialect)
- (str/blank? sql)
- (not (contains? dialect-mapping dialect))
- (nil? mapped-dialect)
- (contains-template-tags? sql))
+ (let [mapped-dialect (get dialect-mapping dialect)
+ template-error (template-syntax-error sql)]
+ (cond
+ template-error
+ {:valid? false
+ :dialect dialect
+ :reason :invalid-template-syntax
+ :error-message template-error}
+
+ (or (nil? dialect)
+ (str/blank? sql)
+ (not (contains? dialect-mapping dialect))
+ (nil? mapped-dialect)
+ (contains-template-tags? sql))
{:valid? true
:dialect dialect
:transpiled-sql sql}
+
+ :else
(let [{:keys [error-message reason transpiled-sql status]}
(sql-tools/transpile-sql sql mapped-dialect mapped-dialect)]
(merge
diff --git a/test/metabase/metabot/tools/document_test.clj b/test/metabase/metabot/tools/document_test.clj
index 43a6a5b808d..c8384829f57 100644
--- a/test/metabase/metabot/tools/document_test.clj
+++ b/test/metabase/metabot/tools/document_test.clj
@@ -153,6 +153,18 @@
"Rewrite the SQL as one statement, then retry.")
(:output result))))))))
+(deftest document-construct-sql-chart-invalid-template-syntax-test
+ (testing "SQL with an unclosed `[[` gets the SQL tools' template syntax recovery, without the edit-only sentence"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-temp [:model/Database {db-id :id} {:engine :postgres}]
+ (let [result (invoke-document-tool "document_construct_sql_chart"
+ (assoc sql-chart-args :database_id db-id :sql "SELECT * FROM t [[WHERE x = {{x}}"))
+ output (:output result)]
+ (is (= {:class :recoverable :code ::sql.common/invalid-template-syntax} (:error result)))
+ (is (str/includes? output "invalid Metabase `{{...}}` or `[[...]]` syntax"))
+ (is (str/includes? output "Parser details: \"Invalid query: found '[[' or '{{' with no matching"))
+ (is (not (str/includes? output "this edit did not change"))))))))
+
(deftest document-construct-sql-chart-blank-sql-test
(testing "blank SQL gets the SQL tools' recovery step instead of a draft"
(mt/with-current-user (mt/user->id :crowberto)
diff --git a/test/metabase/metabot/tools/sql/validation_test.clj b/test/metabase/metabot/tools/sql/validation_test.clj
index c2bf4cc8dad..2ef2a61aa10 100644
--- a/test/metabase/metabot/tools/sql/validation_test.clj
+++ b/test/metabase/metabot/tools/sql/validation_test.clj
@@ -2,8 +2,10 @@
(:require
[clojure.string :as str]
[clojure.test :refer [deftest testing is]]
+ [metabase.lib.core :as lib]
[metabase.metabot.tools.sql.validation :as metabot.tools.sql.validation]
- [metabase.sql-parsing.core :as sql-parsing]))
+ [metabase.sql-parsing.core :as sql-parsing]
+ [metabase.test :as mt]))
;;;; contains-template-tags?
@@ -61,6 +63,30 @@
{:context "Snippet references should skip validation"
:dialect "postgres" :sql "SELECT * FROM {{snippet: common_user_joins}}"
:expected {:valid? true :dialect "postgres"}}
+ {:context "An unclosed optional clause should fail validation"
+ :dialect "postgres" :sql "SELECT count(*) FROM products [[WHERE category = {{cat}}"
+ :expected {:valid? false :dialect "postgres" :reason :invalid-template-syntax
+ :error-message #(str/includes? % "with no matching")}}
+ {:context "An unclosed variable should fail validation when no dialect is provided"
+ :dialect nil :sql "SELECT * FROM products WHERE category = {{cat"
+ :expected {:valid? false :dialect nil :reason :invalid-template-syntax}}
+ {:context "An empty variable in SQL with template tags should fail validation"
+ :dialect "postgres" :sql "SELECT * FROM products WHERE category = {{cat}} AND price = {{}}"
+ :expected {:valid? false :dialect "postgres" :reason :invalid-template-syntax
+ :error-message #(str/includes? % "expected a param name")}}
+ {:context "An optional clause without a variable in SQL with template tags should fail validation"
+ :dialect "postgres" :sql "SELECT * FROM products WHERE price > {{min}} [[AND category = 'Widget']]"
+ :expected {:valid? false :dialect "postgres" :reason :invalid-template-syntax
+ :error-message #(str/includes? % "must contain at least one")}}
+ {:context "A nested array literal without template tags should pass validation"
+ :dialect "postgres" :sql "SELECT ARRAY[[1,2],[3,4]] AS m"
+ :expected {:valid? true :dialect "postgres"}}
+ {:context "Brackets in a quoted identifier without template tags should pass validation"
+ :dialect nil :sql "SELECT 1 AS \"[[x]]\""
+ :expected {:valid? true :dialect nil}}
+ {:context "Brackets in a string literal should not fail validation"
+ :dialect "postgres" :sql "SELECT '[[' AS a, '{{' AS b FROM products"
+ :expected {:valid? true :dialect "postgres"}}
{:context "Validation should be skipped when no dialect is provided"
:dialect nil :sql "SELECT * FROM users"
:expected {:valid? true :dialect nil}}
@@ -83,6 +109,18 @@
(is (=? expected
(metabot.tools.sql.validation/validate-sql dialect sql))))))
+(deftest template-syntax-error-is-english-test
+ (testing "The parser message is English for a user with another locale"
+ (mt/with-mock-i18n-bundles! {"zz" {:messages {"Invalid query: found ''[['' or '''{{''' with no matching '']]'' or ''}}''"
+ "TRANSLATED PARSER MESSAGE"}}}
+ (mt/with-user-locale "zz"
+ (testing "the parser itself returns the translation"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"TRANSLATED PARSER MESSAGE"
+ (lib/parse-parameters "SELECT {{x"))))
+ (is (=? {:valid? false
+ :error-message "Invalid query: found '[[' or '{{' with no matching ']]' or '}}'"}
+ (metabot.tools.sql.validation/validate-sql "postgres" "SELECT {{x")))))))
+
(def ^:private transpilation-cases
"Testing _context_ describe a test case. Apart from that, pretty formatting is checked by comparisons of raw output
strings in `:transpiled-sql`."
diff --git a/test/metabase/metabot/tools/sql_test.clj b/test/metabase/metabot/tools/sql_test.clj
index 8c3fce28834..f8bd949b87d 100644
--- a/test/metabase/metabot/tools/sql_test.clj
+++ b/test/metabase/metabot/tools/sql_test.clj
@@ -434,6 +434,51 @@
(is (not (str/includes? output "Common issues to check:")))
(is (nil? (:structured-output result))))))))))
+(deftest invalid-template-syntax-test
+ (mt/with-current-user (mt/user->id :crowberto)
+ (doseq [engine [:postgres :h2]]
+ (mt/with-temp [:model/Database {db-id :id :as db} {:engine engine}]
+ (mt/with-db db
+ (doseq [profile [:internal :sql]
+ tool-name ["create_sql_query" "edit_sql_query" "replace_sql_query"]]
+ (testing (str engine " / " profile " / " tool-name)
+ (let [result (invoke-with-proposed-sql profile tool-name db-id "SELECT * FROM t [[WHERE x = {{x}}")
+ output (:output result)]
+ (is (= {:class :recoverable :code ::sql.common/invalid-template-syntax} (:error result)))
+ (is (str/includes? output "invalid Metabase `{{...}}` or `[[...]]` syntax"))
+ (is (= (= tool-name "edit_sql_query")
+ (str/includes? output "The problem can be in SQL that this edit did not change.")))
+ (is (str/includes? output "Parser details: \"Invalid query: found '[[' or '{{' with no matching"))
+ (is (str/includes? output "No changes were saved."))
+ (is (nil? (:structured-output result)))))))))))
+
+(deftest brackets-without-template-tags-test
+ (testing "SQL with `[[` but no template tag passes, because Metabase sends it to the database as it is"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (doseq [profile [:internal :sql]
+ tool-name ["create_sql_query" "edit_sql_query" "replace_sql_query"]]
+ (testing (str profile " / " tool-name)
+ (let [result (invoke-with-proposed-sql profile tool-name (mt/id) "SELECT 1 AS \"[[x]]\"")]
+ (is (nil? (:error result)))
+ (is (= "SELECT 1 AS \"[[x]]\"" (get-in result [:structured-output :query-content])))))))))
+
+(deftest edit-elsewhere-in-sql-with-invalid-template-syntax-test
+ (testing "An edit that leaves an existing unclosed tag in place returns a recoverable error, and an edit that closes it succeeds"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (let [memory (assoc editor-memory :state {:queries {"q1" {:database (mt/id)
+ :type :native
+ :native {:query "SELECT * FROM venues [[WHERE id = {{id}}"}}}})]
+ (doseq [profile [:internal :sql]]
+ (testing profile
+ (is (= {:class :recoverable :code ::sql.common/invalid-template-syntax}
+ (:error (invoke-sql-tool profile memory "edit_sql_query"
+ {:edits [{:old_string "*" :new_string "name"}]}))))
+ (let [result (invoke-sql-tool profile memory "edit_sql_query"
+ {:edits [{:old_string "{{id}}" :new_string "{{id}}]]"}]})]
+ (is (nil? (:error result)))
+ (is (= "SELECT * FROM venues [[WHERE id = {{id}}]]"
+ (get-in result [:structured-output :query-content]))))))))))
+
(deftest closest-sql-match-test
(mt/with-current-user (mt/user->id :crowberto)
(let [ticket-sql "SELECT\n id,\n name\nFROM venues\nWHERE\n price > 2 AND category_id IN (1, 2)"