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
Improve error messages for unsupported H2 functions (#75149)
Ben Grabowa943448e90e2
diff --git a/resources/sample-database.db.mv.db b/resources/sample-database.db.mv.dbindex d0b761d7b0d..1a94eaa4f7c 100644Binary files a/resources/sample-database.db.mv.db and b/resources/sample-database.db.mv.db differdiff --git a/src/metabase/driver/h2.clj b/src/metabase/driver/h2.cljindex 06520afc994..d29164d1e65 100644--- a/src/metabase/driver/h2.clj+++ b/src/metabase/driver/h2.clj@@ -30,12 +30,13 @@[metabase.util.i18n :refer [deferred-tru tru]][metabase.util.log :as log][metabase.util.malli :as mu]- [metabase.util.performance :refer [every? some]])+ [metabase.util.performance :as perf :refer [every? some]])(:import(java.sql Clob Connection ResultSet ResultSetMetaData SQLException Statement Types)(java.time OffsetTime)(org.h2.command CommandInterface Parser)- (org.h2.engine SessionLocal)))+ (org.h2.engine SessionLocal)+ (org.h2.util StringUtils)))(set! *warn-on-reflection* true)@@ -251,6 +252,43 @@(catch org.h2.message.DbException _{:command-types [] :remaining-sql nil}))))+(def ^:private unsupported-builtin-functions+ "H2 built-in functions that operate on the server environment (filesystem, linked databases,+ session and memory state) rather than on query data. They are not supported in native queries+ or actions, and can be embedded inside otherwise-ordinary SELECT/CALL statements, so they are+ matched by name rather than by statement type."+ #{"ABORT_SESSION"+ "CANCEL_SESSION"+ "CSVREAD"+ "CSVWRITE"+ "DATABASE_PATH"+ "DB_OBJECT_ID"+ "DB_OBJECT_SQL"+ "FILE_READ"+ "FILE_WRITE"+ "LINK_SCHEMA"+ "MEMORY_FREE"+ "MEMORY_USED"})++(def ^:private unsupported-builtin-function-regex+ (re-pattern (str "(?i)\\b(?:" (str/join "|" unsupported-builtin-functions) ")\\b")))++(defn- unsupported-functions-in-query+ "The set of `unsupported-builtin-functions` referenced by `sql`, or nil if none. Scans the SQL+ text rather than the parsed command, so it still applies when the H2 parser is unavailable."+ [sql]+ (when sql+ (perf/not-empty+ (into #{} (map u/upper-case-en)+ (re-seq unsupported-builtin-function-regex+ (StringUtils/toUpperEnglish sql))))))++(defn- check-no-unsupported-functions [sql]+ (when-let [fns (unsupported-functions-in-query sql)]+ (throw (ex-info (tru "These functions are not supported in native queries: {0}" (str/join ", " (sort fns)))+ {:type driver-api/qp.error-type.db+ :functions fns}))))+(defn- every-command-allowed-for-actions? [{:keys [command-types remaining-sql]}](let [cmd-type-nums command-types](boolean@@ -281,6 +319,7 @@(defn- check-action-commands-allowed [{:keys [database] {:keys [query]} :native}](when query+ (check-no-unsupported-functions query)(when-let [query-classification (classify-query database query)](when-not (every-command-allowed-for-actions? query-classification)(throw (ex-info "DDL commands are not allowed to be used with H2."@@ -300,6 +339,7 @@[:map[:query string?]]]]](when sql+ (check-no-unsupported-functions sql)(let [query-classification (classify-query (driver-api/database (driver-api/metadata-provider))sql)](when-not (read-only-statements? query-classification)diff --git a/test/metabase/driver/h2_test.clj b/test/metabase/driver/h2_test.cljindex 47c1ad1ea49..5fdce45992d 100644--- a/test/metabase/driver/h2_test.clj+++ b/test/metabase/driver/h2_test.clj@@ -346,6 +346,42 @@"CREATE OR REPLACE TRIGGER MY_SPECIAL_TRIG BEFORE SELECT ON INFORMATION_SCHEMA.Users AS '';""SELECT * FROM INFORMATION_SCHEMA.Users;"]))))+(deftest ^:parallel unsupported-functions-detection-test+ (let [hits #'h2/unsupported-functions-in-query]+ (testing "unsupported built-in functions are detected regardless of statement type, casing, or comments"+ (are [expected query] (= expected (hits query))+ #{"FILE_READ"} "SELECT FILE_READ('a','UTF-8')"+ #{"CSVREAD"} "CALL CSVREAD('a')"+ #{"FILE_WRITE"} "WITH w AS (SELECT FILE_WRITE(STRINGTOUTF8('x'),'y') AS b) SELECT b FROM w"+ #{"CSVWRITE"} "CALL CSVWRITE('a','SELECT 1')"+ #{"LINK_SCHEMA"} "CALL LINK_SCHEMA('LK','org.h2.Driver','jdbc:h2:mem:t','sa','','PUBLIC')"+ #{"MEMORY_USED"} "select memory_used()"+ ;; a comment between the function name and its arg list is still detected+ #{"FILE_READ"} "SELECT FiLe_ReAd/* c */ ('x')"+ #{"FILE_READ"} "SELECT 1 AS \"a'\", FILE_READ('/cat_pics.gif','UTF-8')"+ #{"FILE_READ"} "SELECT 'FILE_READ' AS col" ;; the name only appears inside a string literal but we disallow it anyway+ ))+ (testing "ordinary queries are not flagged"+ (are [query] (nil? (hits query))+ "SELECT * FROM orders"+ ;; identifier merely containing an unsupported name as a substring+ "SELECT my_file_read_col FROM t"+ nil))))++(deftest ^:parallel check-read-only-rejects-unsupported-functions-test+ (testing "unsupported built-in functions are rejected even inside an allowed SELECT/CALL"+ (are [query] (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"not supported in native queries"+ (mt/with-metadata-provider (mt/id)+ (#'h2/check-read-only-statements+ {:database 1+ :type :native+ :native {:query query}})))+ "SELECT FILE_READ('a','UTF-8')"+ "CALL CSVREAD('a')"+ "CALL LINK_SCHEMA('LK','org.h2.Driver','jdbc:h2:mem:t','sa','','PUBLIC')")))+(deftest disallowed-commands-in-action-test(mt/test-driver :h2(mt/with-actions-test-data-and-actions-enabled