Skip to content

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

Funny SQL (#82980)

Cam Saul5c4fa2dc09fe
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 2280649bfb0..657960cd811 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -366,6 +366,7 @@
metabase.app-db.transient-error}
:uses #{auth-provider
connection-pool
+ funnysql
task.secure-delegate
util
util.classloader
@@ -1139,6 +1140,11 @@
request
util}}
+ funnysql
+ {:team "Querying Platform"
+ :api #{metabase.funnysql.core}
+ :uses #{util}}
+
geojson
{:team "UX West"
:api #{metabase.geojson.api
diff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/db.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/db.clj
index c42ca1fda65..9e9ba0d51c3 100644
--- a/enterprise/backend/src/metabase_enterprise/advanced_config/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/db.clj
@@ -17,7 +17,7 @@
month :- ms/PositiveInt]
(let [date-part (fn [part-key part-value]
(if (= (mdb/db-type) :postgres)
- [:= [:date_part ^:allow-raw-sql [:inline (name part-key)] :started_at] part-value]
+ [:= [:date_part (name part-key) :started_at] part-value]
[:= [part-key :started_at] part-value]))]
(t2/select :query_execution
{:order-by [[:started_at :desc]]
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/pages/common.clj b/enterprise/backend/src/metabase_enterprise/audit_app/pages/common.clj
index 4bb44857900..5e25ecac4a3 100644
--- a/enterprise/backend/src/metabase_enterprise/audit_app/pages/common.clj
+++ b/enterprise/backend/src/metabase_enterprise/audit_app/pages/common.clj
@@ -213,10 +213,10 @@
[query query-string & fields-to-search]
(sql.helpers/where query (when (seq query-string)
(let [query-string (h2x/like-substring query-string)]
- (cons
- :or
- (for [field fields-to-search]
- [:like (lowercase-field field) query-string]))))))
+ ;; this has to be a vector: a seq compiles as a list of values, not a call to `:or`
+ (into [:or]
+ (for [field fields-to-search]
+ [:like (lowercase-field field) query-string]))))))
(def ^:private sort-directions
"The only two directions an audit page may sort in."
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/pages/common/cards.clj b/enterprise/backend/src/metabase_enterprise/audit_app/pages/common/cards.clj
index 72a8a8ff6d9..9528d66a68a 100644
--- a/enterprise/backend/src/metabase_enterprise/audit_app/pages/common/cards.clj
+++ b/enterprise/backend/src/metabase_enterprise/audit_app/pages/common/cards.clj
@@ -12,8 +12,8 @@
[[:over [[:row_number]
^:allow-subquery
{:partition-by [:query_execution.card_id]
- :order-by [[:query_execution.id :desc]]}
- :rn]]]]
+ :order-by [[:query_execution.id :desc]]}]]
+ :rn]]
:from [:query_execution]
;; Join on BOTH card_id and started_at, because some cards share the same timestamp.
:join [[^:allow-subquery
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.clj b/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.clj
index b511bf3449d..eec624b9c9b 100644
--- a/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.clj
+++ b/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.clj
@@ -72,7 +72,7 @@
[(common/user-full-name :u) :user_name]
[:card.updated_at :updated_at]
;; Keep this last: the streaming xform strips it positionally and hoists it to the root.
- [[:over [[:count :*] ^:allow-subquery {} :total_count]]]]
+ [[:over [[:count :*] ^:allow-subquery {}]] :total_count]]
:from [[:report_card :card]]
:left-join [[:collection :coll] [:= :card.collection_id :coll.id]
[:metabase_database :db] [:= :card.database_id :db.id]
diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/api/common.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/api/common.clj
index 3b0ab744636..bd96a70ac83 100644
--- a/enterprise/backend/src/metabase_enterprise/content_diagnostics/api/common.clj
+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/api/common.clj
@@ -18,6 +18,7 @@
[metabase.premium-features.core :as premium-features]
[metabase.queries.schema :as queries.schema]
[metabase.util :as u]
+ [metabase.util.honey-sql-2 :as h2x]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -105,11 +106,11 @@
which no personal prefix can match (a personal root is caught by its own `personal_owner_id` instead).
Standard SQL, because H2, Postgres and MySQL agree on `SUBSTRING`, `POSITION` and `CONCAT` but not on
- `SPLIT_PART`, `LOCATE`, `STRPOS` or `CAST(… AS INTEGER)`. `POSITION` needs `:raw` since Honey SQL emits the
- comma form, which Postgres rejects; only that constant is spliced and Honey SQL still quotes the column.
- Comparing strings rather than ints avoids a dialect-specific cast."
+ `SPLIT_PART`, `LOCATE`, `STRPOS` or `CAST(… AS INTEGER)`. Funny SQL compiles `:position` to the standard
+ `POSITION(… IN …)` form; the `/` is spliced as a literal rather than bound. Comparing strings rather than ints
+ avoids a dialect-specific cast."
[:substring :location [:inline 1]
- [:+ ^:allow-raw-sql [:raw ["POSITION('/' IN " [:substring :location [:inline 2]] ")"]]
+ [:+ [:position (h2x/literal "/") [:substring :location [:inline 2]]]
[:inline 1]]])
(defn- personal-collection-clause
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/db.clj b/enterprise/backend/src/metabase_enterprise/dependencies/db.clj
index b413983f665..8cd5a0d3cb2 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/db.clj
@@ -520,7 +520,10 @@
location-column default-visible-restriction)
all-joins (set/union joins sort-joins)
item-visible-expr (edge-restriction-expr (name entity-type) :entity.id item-visible-restriction)
- select-clause [[^:allow-raw-sql [:inline (name entity-type)] :entity_type]
+ ;; a plain string here compiles to a `?` parameter, and H2 can't work out the type of a parameter in the
+ ;; select list of a derived table -- `h2x/literal` emits a real quoted SQL literal instead. The value comes
+ ;; from the closed `dependency-types` enum, never from user input.
+ select-clause [[(h2x/literal (name entity-type)) :entity_type]
[:entity.id :entity_id]
[sort-expr :sort_key]]]
^:allow-subquery
diff --git a/enterprise/backend/src/metabase_enterprise/search/semantic/db.clj b/enterprise/backend/src/metabase_enterprise/search/semantic/db.clj
index 724a455ca04..ab00bc28ed4 100644
--- a/enterprise/backend/src/metabase_enterprise/search/semantic/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/search/semantic/db.clj
@@ -9,6 +9,7 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.search.config :as search.config]
[metabase.search.scoring :as search.scoring]
+ [metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
[metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
@@ -33,7 +34,7 @@
:where [:and
[:= :active true]
[:or [:= :is_published true]
- [:= :data_authority ^:allow-raw-sql [:inline "authoritative"]]]]}))
+ [:= :data_authority "authoritative"]]]}))
(mu/defn official-collection-ids
"The IDs of the official Collections."
@@ -72,8 +73,10 @@
(defn- search-doc-select
[{:keys [id model]}]
+ ;; these have to be real SQL literals rather than plain strings: a string compiles to a `?` parameter, and H2 cannot
+ ;; work out the type of a parameter in the select list of a CTE (`Unknown data type: "?, ?"`)
^:allow-subquery
- {:select [[^:allow-raw-sql [:inline (str id)]] [^:allow-raw-sql [:inline model]]]})
+ {:select [[(h2x/literal (str id))] [(h2x/literal model)]]})
(defn- search-index-select
"A `search_index` CTE selecting the `:id` and `:model` of each of `search-results` (each `{:id :model}`)."
diff --git a/enterprise/backend/src/metabase_enterprise/search/semantic/index.clj b/enterprise/backend/src/metabase_enterprise/search/semantic/index.clj
index bdd7e74c575..30d4551e215 100644
--- a/enterprise/backend/src/metabase_enterprise/search/semantic/index.clj
+++ b/enterprise/backend/src/metabase_enterprise/search/semantic/index.clj
@@ -7,6 +7,7 @@
[com.climate.claypoole :as cp]
[honey.sql :as sql]
[honey.sql.helpers :as sql.helpers]
+ [honey.sql.pg-ops :as sql.pg-ops]
[java-time.api :as t]
[metabase-enterprise.search.semantic.appdb-scoring :as appdb-scoring]
[metabase-enterprise.search.semantic.db :as semantic-search.db]
@@ -708,7 +709,7 @@
;; Using a join allows us to share the query expression between our SELECT and WHERE clauses.
:join [[[:to_tsquery ^:allow-raw-sql [:inline tsv-lang] [:lift ts-search-expr]]
:query] [:= 1 1]]
- :where (let [ts-query-filter [:raw (format "%s @@ query" (name vector-column))]]
+ :where (let [ts-query-filter [sql.pg-ops/atat vector-column :query]]
(if (seq filters)
(into [:and ts-query-filter] [filters])
ts-query-filter))
diff --git a/enterprise/backend/src/metabase_enterprise/security_center/matching.clj b/enterprise/backend/src/metabase_enterprise/security_center/matching.clj
index 6fe67e2af53..0b1f79a8f80 100644
--- a/enterprise/backend/src/metabase_enterprise/security_center/matching.clj
+++ b/enterprise/backend/src/metabase_enterprise/security_center/matching.clj
@@ -2,6 +2,7 @@
"Advisory matching engine. Evaluates HoneySQL queries against the appdb
and combines with version checks to determine match status."
(:require
+ [honey.sql :as sql]
[metabase-enterprise.security-center.db :as security-center.db]
[metabase.app-db.core :as mdb]
[metabase.config.core :as config]
@@ -68,7 +69,9 @@
even if the driver doesn't enforce read-only
3. setQueryTimeout — kills the query if it exceeds [[*query-timeout-seconds*]]"
[hsql-query]
- (let [[sql & params] (mdb/compile hsql-query)
+ (let [[sql & params] (sql/format hsql-query {:quoted true
+ :dialect :metabase.app-db.setup/application-db
+ :quoted-snake false})
driver (mdb/db-type)]
(with-open [^java.sql.Connection conn (.getConnection (mdb/data-source))]
(.setReadOnly conn true)
diff --git a/enterprise/backend/test/metabase_enterprise/audit_app/pages/common_test.clj b/enterprise/backend/test/metabase_enterprise/audit_app/pages/common_test.clj
index 5ddc32f96e3..50c779ab0ef 100644
--- a/enterprise/backend/test/metabase_enterprise/audit_app/pages/common_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/audit_app/pages/common_test.clj
@@ -21,20 +21,20 @@
{:metadata [[:a {:display_name "A", :base_type :type/DateTime}]
[:b {:display_name "B", :base_type :type/Integer}]]
:results (common/query
- {:union-all [{:select [[[:inline a1] :A]
- [[:inline 2] :B]]}
- {:select [[[:inline 3] :A]
- [[:inline 4] :B]]}]})})
+ {:union-all [^:allow-subquery {:select [[[:inline a1] :A]
+ [[:inline 2] :B]]}
+ ^:allow-subquery {:select [[[:inline 3] :A]
+ [[:inline 4] :B]]}]})})
(defmethod audit.i/internal-query ::reducible-format-query-fn
[_ a1]
{:metadata [[:a {:display_name "A", :base_type :type/DateTime}]
[:b {:display_name "B", :base_type :type/Integer}]]
:results (common/reducible-query
- {:union-all [{:select [[[:inline a1] :A]
- [[:inline 2] :B]]}
- {:select [[[:inline 3] :A]
- [[:inline 4] :B]]}]})
+ {:union-all [^:allow-subquery {:select [[[:inline a1] :A]
+ [[:inline 2] :B]]}
+ ^:allow-subquery {:select [[[:inline 3] :A]
+ [[:inline 4] :B]]}]})
:xform (map #(update (vec %) 0 inc))})
(deftest transform-results-test
diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/scan_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/scan_test.clj
index 0f260b03603..dd0498dd44d 100644
--- a/enterprise/backend/test/metabase_enterprise/content_diagnostics/scan_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/scan_test.clj
@@ -5,7 +5,6 @@
(:require
[clojure.set :as set]
[clojure.test :refer :all]
- [honey.sql :as sql]
[java-time.api :as t]
[medley.core :as m]
[metabase-enterprise.content-diagnostics.api.common :as api.common]
@@ -15,6 +14,7 @@
[metabase-enterprise.content-diagnostics.test-util :as cd.tu]
[metabase.analytics-interface.core :as analytics]
[metabase.analytics.prometheus :as prometheus]
+ [metabase.app-db.core :as mdb]
[metabase.collections.models.collection :as collection]
[metabase.documents.prose-mirror :as prose-mirror]
[metabase.permissions.core :as perms]
@@ -654,9 +654,9 @@
;; makes it impossible instead: the clause is data-independent. A reintroduced id set would grow these
;; params and fail here, which `api-personal-collection-exclusion-spans-descendants-test` cannot do -
;; the old LIKE matcher handled any depth too, so that test is a semantic-equivalence guard only.
- (let [params #(rest (sql/format {:select [:*]
- :from [:content_diagnostics_finding]
- :where (api.common/exclude-personal-collections-clause true)}))
+ (let [params #(rest (mdb/compile {:select [:*]
+ :from [:content_diagnostics_finding]
+ :where (api.common/exclude-personal-collections-clause true)}))
baseline (params)]
(mt/with-temp [:model/User {user-id :id} {}]
(collection/user->personal-collection user-id)
diff --git a/mage/src/mage/modules.clj b/mage/src/mage/modules.clj
index cc9e83d4427..4570e866325 100644
--- a/mage/src/mage/modules.clj
+++ b/mage/src/mage/modules.clj
@@ -165,6 +165,7 @@
events
explorations
formatter
+ funnysql
geojson
glossary
indexed-entities
diff --git a/src/metabase/app_db/cluster_lock.clj b/src/metabase/app_db/cluster_lock.clj
index 999c6780241..8fff404c76d 100644
--- a/src/metabase/app_db/cluster_lock.clj
+++ b/src/metabase/app_db/cluster_lock.clj
@@ -85,11 +85,14 @@
" LOCK IN SHARE MODE"
" FOR SHARE")))
+;; TODO (Cam 2026-10-06) this isn't going to work if we change the app DB type in tests or the REPL, use
+;; [[metabase.app-db.core/memoize-for-application-db]] instead of a delay to fix this
(def ^:private base-lock-sql
(delay
(first (mdb.query/compile {:select [:lock.lock_name]
:from [[:metabase_cluster_lock :lock]]
- :where [:= :lock.lock_name [:raw "?"]]}))))
+ ;; `(Object.)` here to force this to spit out a `?`
+ :where [:= :lock.lock_name (Object.)]}))))
(defn- lock-sql ^String [mode]
(str @base-lock-sql (lock-clause mode)))
@@ -194,7 +197,7 @@
[lock-name-str timeout]
(let [[sql] (mdb.query/compile {:insert-into [:metabase_cluster_lock]
:columns [:lock_name]
- :values [[[:raw "?"]]]})]
+ :values [[(Object.)]]})]
(with-open [conn (checkout-connection!)
stmt (.prepareStatement conn ^String sql)]
;; The row must be durable before the SELECT below can find it, and the pool does not guarantee autocommit.
@@ -236,7 +239,7 @@
[^Connection conn lock-name-str timeout]
(let [[sql] (mdb.query/compile {:insert-into [:metabase_cluster_lock]
:columns [:lock_name]
- :values [[[:raw "?"]]]})]
+ :values [[(Object.)]]})]
(with-open [insert-stmt (.prepareStatement conn ^String sql)]
(u.connection/set-query-timeout! insert-stmt timeout)
(.setString insert-stmt 1 lock-name-str)
diff --git a/src/metabase/app_db/honeysql_guard.clj b/src/metabase/app_db/honeysql_guard.clj
index b0e337f82dc..589b8291ed7 100644
--- a/src/metabase/app_db/honeysql_guard.clj
+++ b/src/metabase/app_db/honeysql_guard.clj
@@ -97,7 +97,8 @@
(when-not (or (instance? IdentityQuery built-query)
(safe-syntax? built-query))
(throw (ex-info (str "A forbidden HoneySQL clause reached the app-DB compile step. Mark a deliberate subquery "
- "with ^:allow-subquery, a deliberate [:raw ...] splice with ^:allow-raw-sql, and use "
- "[:inline ...] only with a scalar literal.")
+ "with ^:allow-subquery, use [:inline ...] only with a scalar literal, and use "
+ "metabase.util.honey-sql-2/literal for a string literal. App-DB queries are compiled with "
+ "Funny SQL, which does not support [:raw ...].")
{:type ::unmarked-nested-map, :model model, :query built-query})))
built-query)
diff --git a/src/metabase/app_db/query.clj b/src/metabase/app_db/query.clj
index c389528700d..c7a14a832c8 100644
--- a/src/metabase/app_db/query.clj
+++ b/src/metabase/app_db/query.clj
@@ -21,9 +21,10 @@
(:refer-clojure :exclude [compile])
(:require
[clojure.string :as str]
- [honey.sql :as sql]
+ [metabase.app-db.connection :as mdb.connection]
[metabase.app-db.db :as app-db.db]
[metabase.app-db.format :as app-db.format]
+ [metabase.funnysql.core :as funnysql]
[metabase.util :as u]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
@@ -104,18 +105,8 @@
sql-args)
(defmethod compile clojure.lang.IPersistentMap
- [honey-sql]
- (let [sql-args (try
- (sql/format honey-sql {:quoted true, :dialect :metabase.app-db.setup/application-db, :quoted-snake false})
- (catch Throwable e
- ;; this is not i18n'ed because it (hopefully) shouldn't be user-facing -- we shouldn't be running
- ;; in to unexpected Honey SQL compilation errors at run time -- if we are it means we're not being
- ;; careful enough with the Honey SQL forms we create which is a bug in the Metabase code we should
- ;; have caught in tests.
- (throw (ex-info (str "Error compiling Honey SQL: " (ex-message e))
- {:honey-sql honey-sql}
- e))))]
- sql-args))
+ [honeysql]
+ (funnysql/format honeysql (mdb.connection/db-type)))
(defn current-timestamp-string
"The application DB's own current timestamp, as a string. Read from the DB rather than from this machine's clock,
diff --git a/src/metabase/app_db/setup.clj b/src/metabase/app_db/setup.clj
index 65138ce813f..927a16b2309 100644
--- a/src/metabase/app_db/setup.clj
+++ b/src/metabase/app_db/setup.clj
@@ -20,6 +20,7 @@
[metabase.app-db.liquibase :as liquibase]
[metabase.app-db.setting :as mdb.setting]
[metabase.config.core :as config]
+ [metabase.funnysql.core :as funnysql]
[metabase.util :as u]
[metabase.util.encryption :as encryption]
[metabase.util.honey-sql-2]
@@ -30,7 +31,8 @@
[methodical.core :as methodical]
[toucan2.honeysql2 :as t2.honeysql]
[toucan2.jdbc.options :as t2.jdbc.options]
- [toucan2.pipeline :as t2.pipeline])
+ [toucan2.pipeline :as t2.pipeline]
+ [toucan2.util :as t2.util])
(:import
(com.mchange.v2.c3p0 PoolBackedDataSource WrapperConnectionPoolDataSource)
(liquibase.exception LockException)))
@@ -335,7 +337,8 @@
{:pre [(#{:h2 :ansi :mysql} dialect)]}
((:quote (sql/get-dialect dialect)) s)))
-(defn- clause-order-fn-for-application-db [clauses]
+(defn- clause-order-fn-for-application-db
+ [clauses]
(case (mdb.connection/db-type)
(:postgres :h2) clauses
:mysql (let [{f :clause-order-fn} (sql/get-dialect :mysql)]
@@ -388,3 +391,13 @@
(contains? query :delete-from)
(-> (dissoc :delete-from)
(assoc :from [(:delete-from query)]))))
+
+(methodical/defmethod t2.pipeline/compile [#_query-type :default
+ #_model :default
+ #_query clojure.lang.IPersistentMap]
+ "Override default Honey SQL 2 backend; compile using Funny SQL instead."
+ [query-type model honeysql]
+ (let [options (t2.honeysql/options)
+ sql-args (t2.util/try-with-error-context ["compile SQL with Funny SQL" {:honeysql honeysql, :options options}]
+ (funnysql/format honeysql (mdb.connection/db-type) options))]
+ (t2.pipeline/compile query-type model sql-args)))
diff --git a/src/metabase/bookmarks/db.clj b/src/metabase/bookmarks/db.clj
index 3ef8e5118f7..e9a3d1e507f 100644
--- a/src/metabase/bookmarks/db.clj
+++ b/src/metabase/bookmarks/db.clj
@@ -207,6 +207,7 @@
:created_at]
:from [:document_bookmark]
:where [:= :user_id user-id]}]]
+ ^:allow-subquery
{:union-all (conj base-queries
^:allow-subquery {:select [[as-null :card_id]
[as-null :dashboard_id]
diff --git a/src/metabase/collections/children.clj b/src/metabase/collections/children.clj
index 6b455d36f9d..59e73ce513c 100644
--- a/src/metabase/collections/children.clj
+++ b/src/metabase/collections/children.clj
@@ -1023,10 +1023,10 @@
(defn- official-collections-first-sort-clause [{:keys [official-collections-first?]}]
(when official-collections-first?
- [:authority_level :asc :nulls-last]))
+ [:authority_level :asc-nulls-last]))
(def ^:private normal-collections-first-sort-clause
- [:type :asc :nulls-first])
+ [:type :asc-nulls-first])
(defn children-sort-clause
"Given the client side sort-info, return sort clause to effect this. `db-type` is necessary due to complications from
@@ -1074,8 +1074,8 @@
[:%lower.name :asc]]
[:model :asc] [[:model_ranking :asc] [:%lower.name :asc]]
[:model :desc] [[:model_ranking :desc] [:%lower.name :asc]]
- [:description :asc] [[:%lower.description :asc :nulls-last] [:%lower.name :asc]]
- [:description :desc] [[:%lower.description :desc :nulls-last] [:%lower.name :asc]])
+ [:description :asc] [[:%lower.description :asc-nulls-last] [:%lower.name :asc]]
+ [:description :desc] [[:%lower.description :desc-nulls-last] [:%lower.name :asc]])
;; add a fallback sort order so paging is still deterministic even if collection have the same name or
;; whatever
[[:id :asc]]]))
@@ -1096,7 +1096,8 @@
(let [sql-order (children-sort-clause sort-info (mdb/db-type))
models (sort (map keyword models))
queries (for [model models
- :let [query (collection-children-query model collection options)
+ :let [query (-> (collection-children-query model collection options)
+ (vary-meta assoc :allow-subquery true))
select-clause-type (some
(fn [k]
(when (get query k)
@@ -1111,7 +1112,7 @@
:include-trash-collection? archived?}
search-clause (search-text-clause search-text)
rows-query (cond-> {:with [[:visible_collection_ids (collection/visible-collection-query viz-config)]]
- :select [:* [[:over [[:count :*] ^:allow-subquery {} :total_count]]]]
+ :select [:* [[:over [[:count :*] ^:allow-subquery {}]] :total_count]]
:from [[^:allow-subquery {:union-all queries} :dummy_alias]]
:order-by sql-order}
search-clause
@@ -1218,7 +1219,9 @@
{:with [[:visible_collection_ids (collection/visible-collection-query viz-config)]]
:select (vec
(for [model candidates]
- [[:exists (collection-children-query model collection options)] model]))}))]
+ [[:exists (-> (collection-children-query model collection options)
+ (vary-meta assoc :allow-subquery true))]
+ model]))}))]
{:available_models
(->> candidates
(keep (fn [model]
diff --git a/src/metabase/collections/models/collection.clj b/src/metabase/collections/models/collection.clj
index 17b75ac00c4..a228cad9d6e 100644
--- a/src/metabase/collections/models/collection.clj
+++ b/src/metabase/collections/models/collection.clj
@@ -1038,7 +1038,7 @@
(when (perms/is-data-analyst? current-user-id)
^:allow-subquery {:select visible-union-columns
:from [[:collection :c]]
- :where [:= :namespace ^:allow-raw-sql [:inline "transforms"]]})])}
+ :where [:= :namespace "transforms"]})])}
:c])]
;; The `WHERE` clause is where we apply the other criteria we were given:
:where [:and
@@ -1058,7 +1058,7 @@
[:not [:exists ^:allow-subquery {:select [1]
:from [[:collection :sub_c]]
:where [:and [:= :c.id :sub_c.id]
- [:= :sub_c.namespace ^:allow-raw-sql [:inline "shared-tenant-collection"]]]}]])
+ [:= :sub_c.namespace (h2x/literal "shared-tenant-collection")]]}]])
;; excluding things outside of the `archive_operation_id` you wanted...
(when-let [op-id (:archive-operation-id visibility-config)]
[:or
@@ -2216,7 +2216,7 @@
[:not= (maybe-alias :type) instance-analytics-collection-type]
[:not= (maybe-alias :type) trash-collection-type]]]
[:or [:= (maybe-alias :namespace) nil]
- [:not= (maybe-alias :namespace) ^:allow-raw-sql [:inline "analytics"]]]
+ [:not= (maybe-alias :namespace) "analytics"]]
[:not (maybe-alias :is_sample)]]))
(defmethod serdes/extract-query "Collection" [_model {:keys [collection-set filter-column filter-ids skip-archived]}]
diff --git a/src/metabase/explorations/db.clj b/src/metabase/explorations/db.clj
index 43871ddbc44..9f4a4604e6b 100644
--- a/src/metabase/explorations/db.clj
+++ b/src/metabase/explorations/db.clj
@@ -67,7 +67,7 @@
:group-by [:eid]}]
(cond-> {:select [:exploration.*
[:agg.max_ts :current_user_last_touched_at]
- [[:over [[:count :*] ^:allow-subquery {} :total_count]]]]
+ [[:over [[:count :*] ^:allow-subquery {}]] :total_count]]
:from [:exploration]
:join [[agg :agg] [:= :agg.eid :exploration.id]]
:where [:and
diff --git a/src/metabase/funnysql/core.clj b/src/metabase/funnysql/core.clj
new file mode 100644
index 00000000000..a6f0fd56308
--- /dev/null
+++ b/src/metabase/funnysql/core.clj
@@ -0,0 +1,1256 @@
+(ns metabase.funnysql.core
+ "Compiles Honey SQL-shaped app-DB queries to `[sql & args]` for `:h2`, `:postgres` or `:mysql`. Closed: a clause or
+ function absent from [[clause-fns]]/[[fn-call!]] throws. Values bind as `?`; only integers, booleans, validated
+ tokens, [[h2x/literal]] and `[:inline <number>]` are spliced. A map compiles as a query only at top level or when marked
+ `^:allow-subquery`. No support at all for `:raw`."
+ (:refer-clojure :exclude [format])
+ (:require
+ [clojure.string :as str]
+ [flatland.ordered.map :as ordered-map]
+ [flatland.ordered.set :as ordered-set]
+ [honey.sql.pg-ops]
+ [metabase.util :as u]
+ [metabase.util.honey-sql-2 :as h2x]
+ [metabase.util.log :as log])
+ (:import
+ (java.time DateTimeException ZoneId)))
+
+(set! *warn-on-reflection* true)
+
+(defprotocol ^:private Context
+ (^:private engine [this])
+ (^:private options [this])
+ (^:private append-sql! [this s])
+ (^:private append-arg! [this arg])
+ (^:private result! [this]))
+
+(defn- default-context [engine options]
+ (let [sb (StringBuilder.)
+ args (volatile! (transient []))]
+ (reify Context
+ (engine [_this] engine)
+ (options [_this] options)
+ (append-sql! [_this s] (.append sb s))
+ (append-arg! [_this arg] (vswap! args conj! arg))
+ (result! [_this] (into [(str sb)] (persistent! @args))))))
+
+(defprotocol ^:private Compile
+ (^:private compile! [x context]))
+
+(defn- subquery? [x]
+ (and (map? x)
+ (:allow-subquery (meta x))))
+
+(defn- fn-call? [x]
+ (and (vector? x)
+ (keyword? (get x 0))))
+
+(defn- fn-call-of-type? [x k-or-k-set]
+ (and (fn-call? x)
+ (let [f (get x 0)]
+ (if (set? k-or-k-set)
+ (contains? k-or-k-set f)
+ (= f k-or-k-set)))))
+
+(defn- object! [x context]
+ (append-sql! context "?")
+ (append-arg! context x))
+
+(defn- null! [_x context]
+ (append-sql! context "NULL"))
+
+(defn- boolean! [x context]
+ (append-sql! context (str x)))
+
+(defn- splice-number!
+ "Write `n` into the SQL text as a numeric literal. A Ratio is written as a double, since `1/2` is not SQL."
+ [n context]
+ (let [s (str (cond-> n (ratio? n) double))]
+ ;; don't trust `(str n)` blindly -- fail closed instead of splicing whatever it produces. This rejects
+ ;; non-finite Doubles (`NaN`, `Infinity`) and guards against a hostile custom `Number` implementation whose
+ ;; `toString` isn't numeric SQL syntax.
+ (when-not (re-matches #"-?\d+(\.\d+)?([eE][+-]?\d+)?" s)
+ (throw (ex-info "Invalid number" {:n n})))
+ (append-sql! context s)))
+
+(defn- number!
+ "Splice an integer; bind anything else. Postgres and H2 type a decimal literal like `0.5` as an exact `NUMERIC`, so a
+ spliced Double, Float or Ratio would come back as a `BigDecimal`, and a Float would be written as its shortest
+ decimal rather than the value it holds. Bound, each keeps its type -- [[metabase.app-db.jdbc-protocols]] sets a
+ Ratio as a double -- which is what Honey SQL did (#9246). Use `[:inline n]` where a non-integral number has to be a
+ literal."
+ [n context]
+ (if (integer? n)
+ (splice-number! n context)
+ (object! n context)))
+
+(defn- interpose-fn
+ "Iterate all elements in `xs`. Execute `(x-fn <x>)` for each item in `xs`. Execute `(separator-fn)` in between each
+ item in `xs`."
+ [xs x-fn separator-fn]
+ {:pre [(or (nil? xs) (coll? xs))]} ; maps are ok here, we can iterate over the pairs
+ (when (seq xs)
+ (loop [[x & more] xs]
+ (x-fn x)
+ (when (seq more)
+ (separator-fn)
+ (recur more)))))
+
+(defn- ->sequence
+ "Normalize a clause value that is allowed to be either a single item or a sequence of them. Honey SQL accepts
+ `{:group-by :id}` as shorthand for `{:group-by [:id]}` -- likewise `:select`, `:order-by`, `:columns`, `:returning`
+ and `:partition-by` -- and plenty of app-db queries rely on that, so accept it instead of blowing up on `(seq :id)`."
+ [x]
+ (cond
+ ;; `nil` means the clause is absent, not a one-element list containing `NULL`
+ (nil? x) nil
+ (or (sequential? x) (set? x)) x
+ :else [x]))
+
+(defn- commas! [xs context]
+ (interpose-fn xs #(compile! % context) #(append-sql! context ", ")))
+
+(declare map!)
+
+(defn- parens!
+ "Compile `x` wrapped in parens. A `^:allow-subquery` map is compiled directly rather than via [[compile!]], which
+ would parenthesize it a second time."
+ [x context]
+ (append-sql! context "(")
+ ((if (subquery? x) map! compile!) x context)
+ (append-sql! context ")"))
+
+(defn- list! [xs context]
+ (append-sql! context "(")
+ (commas! xs context)
+ (append-sql! context ")"))
+
+(defn- identifier-form?
+ "True if `x` is something we already know how to compile as an identifier: a keyword, or an `h2x/identifier`
+ tagged form."
+ [x]
+ (or (keyword? x)
+ (fn-call-of-type? x ::h2x/identifier)))
+
+(defn- check-identifier-form
+ "Table/column-name positions must never silently fall through to [[object!]]'s `?`-parameter handling just
+ because someone passed the wrong shape of value (a string, a number, a map...) -- that produces a confusing
+ runtime error from the database instead of a clear one from the compiler. Call this before compiling anything
+ in one of those positions."
+ [x]
+ (when-not (identifier-form? x)
+ (throw (ex-info "Expected an identifier" {:x x}))))
+
+(defn- check-subquery-marked
+ "In a position that only takes a query -- a CTE, `FROM`, a join, `EXISTS`, `UNION`, `INSERT INTO ... SELECT` -- a map
+ not marked `^:allow-subquery` would be bound as a `?` the database can only reject, so throw instead, like `:in`
+ does. Call this before compiling anything in one of those positions."
+ [x]
+ (when (and (map? x)
+ (not (subquery? x)))
+ (throw (ex-info "A subquery must be marked ^:allow-subquery" {:subquery x}))))
+
+(declare alias!)
+
+(defn- identifier-with-optional-alias!
+ "Handle an identifier form as seen in `:select`, `:from`, etc.; unwrapped or a vector with one element will act an
+ unaliased identifier while a vector with two elements will emit `<x> AS <y>`. `check-lhs`, if given, is called with
+ the unaliased part before it is compiled."
+ [identifier context & {:keys [check-lhs include-as?], :or {include-as? true}}]
+ (let [[lhs rhs] (if (vector? identifier)
+ identifier
+ [identifier])]
+ (when check-lhs
+ (check-lhs lhs))
+ (compile! lhs context)
+ (when rhs
+ ;; the alias is a name, not a value: a string here would otherwise become a `?` parameter and the database
+ ;; would reject `count(*) AS ?`
+ (check-identifier-form rhs)
+ (append-sql! context (if include-as? " AS " " "))
+ (alias! rhs context))))
+
+(defn- identifier-list! [xs context]
+ (let [xs (->sequence xs)]
+ (run! check-identifier-form xs)
+ (list! xs context)))
+
+(defn- kvs-map! [kvs context]
+ (letfn [(x-equals-y! [[x y]]
+ (check-identifier-form x)
+ (compile! x context)
+ (append-sql! context " = ")
+ (compile! y context))]
+ (interpose-fn kvs x-equals-y! #(append-sql! context ", "))))
+
+(defn- with! [sql ctes context]
+ (append-sql! context sql)
+ (letfn [(cte! [[identifier subquery & options]]
+ (let [[identifier {:keys [columns]}] (if (vector? identifier)
+ identifier
+ [identifier])]
+ (check-identifier-form identifier)
+ (compile! identifier context)
+ (when columns
+ (append-sql! context \space)
+ (identifier-list! columns context)))
+ (append-sql! context " AS ")
+ (doseq [option options]
+ (case option
+ :materialized (append-sql! context "MATERIALIZED ")))
+ (check-subquery-marked subquery)
+ (parens! subquery context))]
+ (interpose-fn ctes cte! #(append-sql! context ", "))))
+
+(defn- create-table! [identifier context]
+ (let [[identifier & options] (if (vector? identifier)
+ identifier
+ [identifier])]
+ (check-identifier-form identifier)
+ (append-sql! context "CREATE TABLE ")
+ (doseq [option options]
+ (case option
+ :if-not-exists (append-sql! context "IF NOT EXISTS ")))
+ (compile! identifier context)))
+
+(declare simple-fn!)
+
+(defn- raw-type-name! [type-name context]
+ ;; `[::h2x/raw-type-name "<type>"]` is how `h2x/cast` carries an already-validated type name through Honey SQL,
+ ;; which has no other form that splices one without mangling it. Here the name is just the name, so unwrap it.
+ (let [type-name (if (and (vector? type-name)
+ (= (first type-name) ::h2x/raw-type-name))
+ (second type-name)
+ type-name)
+ ;; a keyword spells the spaces in a type name as dashes, e.g. `:timestamp-with-time-zone`. A string is the name
+ ;; exactly as given, where a dash can be meaningful -- ClickHouse's `DateTime64(3, 'America/Port-au-Prince')`
+ keyword-name (fn [k]
+ (str/replace (name k) #"-" " "))
+ type-name-str (cond
+ ;; support annoying forms like `[:varchar 26]` -- we still want to validate these so compile
+ ;; them recursively to a string like `varchar(26)` so we can validate them
+ (vector? type-name)
+ (let [recursive-context (default-context (engine context) (options context))]
+ (simple-fn! (keyword-name (first type-name)) (rest type-name) recursive-context)
+ (first (result! recursive-context)))
+
+ (keyword? type-name)
+ (keyword-name type-name)
+
+ :else
+ (name type-name))]
+ (when-not (h2x/raw-type-name? type-name-str)
+ (throw (ex-info "Invalid type" {:type type-name-str})))
+ (append-sql! context type-name-str)))
+
+(defn- column-default!
+ "Compile the value of a column's `DEFAULT`. DDL takes no parameters, so the value has to end up in the SQL text: a
+ number is spliced even where [[number!]] would bind it, and anything that would compile to a `?` -- a string, say --
+ throws rather than producing a `CREATE TABLE` the database rejects. Use [[h2x/literal]] for a string default."
+ [x context]
+ (if (number? x)
+ (splice-number! x context)
+ (let [recursive-context (default-context (engine context) (options context))
+ _ (compile! x recursive-context)
+ [sql & args] (result! recursive-context)]
+ (when (seq args)
+ (throw (ex-info "A column default cannot be a parameter; use h2x/literal for a string default"
+ {:default x, :args args})))
+ (append-sql! context sql))))
+
+(defn- with-columns! [column-specs context]
+ (append-sql! context "(")
+ (letfn [(column-identifier! [column-identifier]
+ (check-identifier-form column-identifier)
+ (compile! column-identifier context))
+ (option! [option]
+ (let [[option & args] (if (vector? option)
+ option
+ [option])]
+ (case option
+ :primary-key
+ (append-sql! context "PRIMARY KEY")
+
+ :generated-by-default-as-identity
+ (append-sql! context "GENERATED BY DEFAULT AS IDENTITY")
+
+ ;; `AUTO_INCREMENT` keeps its underscore -- it is a single keyword rather than a sequence of words
+ ;; like the options above, so it is not spelled `AUTO INCREMENT`.
+ :auto-increment
+ (append-sql! context "AUTO_INCREMENT")
+
+ :not-null
+ (append-sql! context "NOT NULL")
+
+ :default
+ ;; check `args` for emptiness rather than truthiness of its first element -- `false` and `nil` are
+ ;; both perfectly good column defaults.
+ (do
+ (when-not (seq args)
+ (throw (ex-info "Missing value for :default" {})))
+ (append-sql! context "DEFAULT ")
+ (column-default! (first args) context))
+
+ (throw (ex-info "Unknown column option" {:option option, :args args})))))
+ (options! [options]
+ (when (seq options)
+ (append-sql! context \space)
+ (interpose-fn options option! #(append-sql! context \space))))
+ (column-spec! [[column-identifier type-name & options]]
+ (column-identifier! column-identifier)
+ (append-sql! context \space)
+ (raw-type-name! type-name context)
+ (options! options))]
+ (interpose-fn column-specs column-spec! #(append-sql! context ", ")))
+ (append-sql! context ")"))
+
+(defn- insert-into! [x context]
+ (let [[identifier subquery] (if (and (vector? x)
+ (vector? (get x 0)))
+ x
+ [x])]
+ (append-sql! context "INSERT INTO ")
+ (let [[identifier columns] (if (vector? identifier)
+ identifier
+ [identifier])]
+ (check-identifier-form identifier)
+ (compile! identifier context)
+ (when (seq columns)
+ (append-sql! context \space)
+ (identifier-list! columns context)))
+ (when subquery
+ (check-subquery-marked subquery)
+ (append-sql! context \space)
+ ;; `INSERT INTO t SELECT ...` -- the subquery is not wrapped in parens here
+ ((if (subquery? subquery) map! compile!) subquery context))))
+
+(defn- values! [rows context]
+ (when-not (and (coll? rows)
+ (seq rows))
+ (throw (ex-info ":values cannot have empty or nil rows" {:rows rows})))
+ (if (map? (first rows))
+ ;; if rows are maps, infer columns from the map keys; look at all rows to get the complete set since some maps
+ ;; might be partial.
+ (let [columns (into (ordered-set/ordered-set) (mapcat keys) rows)]
+ (identifier-list! columns context)
+ (append-sql! context " VALUES ")
+ (interpose-fn rows #(list! (map (or % {}) columns) context) #(append-sql! context ", ")))
+ ;; otherwise assume columns have been specified with `:columns` and assume `rows` is a sequence of sequences, one
+ ;; for each row.
+ (do
+ (append-sql! context "VALUES ")
+ (interpose-fn rows #(list! % context) #(append-sql! context ", ")))))
+
+(defn- drop-table! [table context]
+ (let [options (butlast table)
+ table (last table)]
+ (append-sql! context "DROP TABLE ")
+ ;; options like `IF EXISTS` have to come before the table name -- Postgres and MySQL reject
+ ;; `DROP TABLE x IF EXISTS` (H2 is the only one of the three that accepts it)
+ (doseq [option options]
+ (case option
+ :if-exists (append-sql! context "IF EXISTS ")))
+ (check-identifier-form table)
+ (compile! table context)))
+
+(defn- update! [identifier context]
+ (append-sql! context "UPDATE ")
+ (identifier-with-optional-alias! identifier context :check-lhs check-identifier-form, :include-as? false))
+
+(defn- set-clause! [kvs context]
+ (append-sql! context "SET ")
+ (kvs-map! kvs context))
+
+(defn- delete-from! [identifier context]
+ (append-sql! context "DELETE FROM ")
+ (identifier-with-optional-alias! identifier context :check-lhs check-identifier-form, :include-as? false))
+
+(defn- select! [sql cols context]
+ (append-sql! context sql)
+ (interpose-fn (->sequence cols) #(identifier-with-optional-alias! % context) #(append-sql! context ", ")))
+
+(defn- from! [from context]
+ (append-sql! context "FROM ")
+ (if (keyword? from)
+ (identifier-with-optional-alias! from context)
+ (interpose-fn from
+ #(identifier-with-optional-alias! % context :check-lhs check-subquery-marked)
+ #(append-sql! context ", "))))
+
+(defn- join!
+ [join-type joins context]
+ ;; `joins` alternates `<thing-to-join> <condition>`. A missing or `nil` condition -- what a `(when ...)` around one
+ ;; gives you -- would otherwise compile to `ON NULL`, which the database happily runs, silently returning no rows.
+ (when-not (even? (count joins))
+ (throw (ex-info "Every join needs a condition: expected [<thing-to-join> <condition> ...]" {:joins joins})))
+ ;; don't spit out anything if `joins` is empty
+ (when (seq joins)
+ (let [join-type-sql (case join-type
+ :join "JOIN "
+ :left "LEFT JOIN "
+ :right "RIGHT JOIN "
+ :inner "INNER JOIN ")]
+ (loop [[thing-to-join condition & more] joins]
+ (when (nil? condition)
+ (throw (ex-info "A join condition cannot be nil" {:thing-to-join thing-to-join})))
+ (append-sql! context join-type-sql)
+ (identifier-with-optional-alias! thing-to-join context :check-lhs check-subquery-marked)
+ (append-sql! context " ON ")
+ (compile! condition context)
+ (when (seq more)
+ (append-sql! context \space)
+ (recur more))))))
+
+(defn- condition!
+ "Compile a `WHERE`/`HAVING` condition, dropping the clause entirely when there isn't one. Honey SQL ignores a nil or
+ empty clause value and callers rely on that -- the `dashboard` search spec declares `:where []` to mean \"no extra
+ filter\". Emitting it anyway is not merely untidy: `[]` compiles to `()`, which H2 reads as an empty ROW
+ (`Data conversion error converting \"ROW to BOOLEAN\"`), and `WHERE NULL` would silently match no rows at all.
+
+ Only `nil` and `[]` mean \"no condition\". Dropping a `WHERE` fails open -- the query matches every row -- so any
+ other empty value, like `{}`, `#{}` or `()`, is compiled like any other condition rather than silently disappearing."
+ [sql condition context]
+ (when-not (or (nil? condition)
+ (and (vector? condition) (empty? condition)))
+ (append-sql! context sql)
+ (compile! condition context)))
+
+(defn- where! [condition context]
+ (condition! "WHERE " condition context))
+
+(defn- group-by! [cols context]
+ (when-let [cols (not-empty (->sequence cols))]
+ (append-sql! context "GROUP BY ")
+ (commas! cols context)))
+
+(defn- having! [condition context]
+ (condition! "HAVING " condition context))
+
+(defn- partition-by! [xs context]
+ (when-let [xs (not-empty (->sequence xs))]
+ (append-sql! context "PARTITION BY ")
+ (commas! xs context)))
+
+(def ^:private order-by-directions
+ "Each `:order-by` direction as `[<sort direction> <placement of NULLs>]`; `nil` means the default."
+ {:asc [:asc nil]
+ :desc [:desc nil]
+ :nulls-last [nil :last]
+ :nulls-first [nil :first]
+ :asc-nulls-last [:asc :last]
+ :desc-nulls-last [:desc :last]
+ :asc-nulls-first [:asc :first]
+ :desc-nulls-first [:desc :first]})
+
+(defn- mysql-order-by-subclause!
+ "MySQL and MariaDB have no `NULLS FIRST`/`NULLS LAST`. They sort `NULL` below every other value -- first ascending, last
+ descending -- so when that isn't the placement asked for, sort on whether `expr` is `NULL` first."
+ [expr sort-direction nulls context]
+ (let [sort-direction (or sort-direction :asc)]
+ (when (and nulls
+ (not= nulls (case sort-direction
+ :asc :first
+ :desc :last)))
+ (append-sql! context "(")
+ (compile! expr context)
+ (append-sql! context (case nulls
+ :last ") IS NULL ASC, "
+ :first ") IS NULL DESC, ")))
+ (compile! expr context)
+ (append-sql! context (case sort-direction
+ :asc " ASC"
+ :desc " DESC"))))
+
+(defn- order-by! [subclauses context]
+ (when-let [subclauses (not-empty (->sequence subclauses))]
+ (append-sql! context "ORDER BY ")
+ (letfn [(subclause! [subclause]
+ (let [[expr direction :as args] (if (vector? subclause)
+ subclause
+ [subclause])]
+ ;; Honey SQL 1 supported `[:field :asc :nulls-last]`, but in Funny SQL and Honey SQL 2 you should do
+ ;; `[:field :asc-nulls-last]` instead. Interestingly enough HoneySQL doesn't error on these and just
+ ;; silently ignores them 😢. Let's be nicer than that.
+ (when (> (count args) 2)
+ (throw (ex-info "`:order-by` only supports [<expression> <direction>], but got more than 2 args" {:args args})))
+ ;; the direction is optional, e.g. `[:field]` or `:field`, and defaults to `ASC`
+ (let [[sort-direction nulls] (or (order-by-directions (or direction :asc))
+ (throw (ex-info "Invalid order by direction"
+ {:direction direction, :subclause subclause})))]
+ (if (= (engine context) :mysql)
+ (mysql-order-by-subclause! expr sort-direction nulls context)
+ (do
+ (compile! expr context)
+ (when sort-direction
+ (append-sql! context (case sort-direction
+ :asc " ASC"
+ :desc " DESC")))
+ (when nulls
+ (append-sql! context (case nulls
+ :last " NULLS LAST"
+ :first " NULLS FIRST"))))))))]
+ (interpose-fn subclauses subclause! #(append-sql! context ", ")))))
+
+(defn- inline? [x]
+ (fn-call-of-type? x :inline))
+
+(defn- limit!
+ [n context]
+ (when-not ((some-fn nat-int? inline?) n)
+ (throw (ex-info "Invalid limit" {:n n})))
+ (append-sql! context "LIMIT ")
+ (compile! n context))
+
+(defn- offset!
+ [n context]
+ (when-not ((some-fn nat-int? inline?) n)
+ (throw (ex-info "Invalid offset" {:n n})))
+ (append-sql! context "OFFSET ")
+ (compile! n context))
+
+(defn- on-conflict!
+ [columns context]
+ (append-sql! context "ON CONFLICT ")
+ (identifier-list! columns context))
+
+(defn- do-update-set!
+ [kvs context]
+ (append-sql! context "DO UPDATE SET ")
+ (kvs-map! kvs context))
+
+(defn- for!
+ [options context]
+ (append-sql! context "FOR ")
+ (let [options (if (vector? options)
+ options
+ [options])
+ ;; other options like `:key-share`, `:no-key-update`, `:share`, `:nowait`, and `:of` exist, but we're not
+ ;; currently using them; implement them if you need them.
+ option! (fn [option]
+ (append-sql! context (case option
+ :update "UPDATE"
+ :skip-locked "SKIP LOCKED")))]
+ (interpose-fn options option! #(append-sql! context \space))))
+
+(defn- returning! [cols context]
+ (append-sql! context "RETURNING ")
+ (commas! (->sequence cols) context))
+
+(defn- union!
+ "Compile the queries combined by a `UNION`. These are complete `SELECT`s rather than scalar subqueries, so unlike
+ [[compile!]] don't wrap them in parens -- use `:nest` for that."
+ [separator queries context]
+ (interpose-fn queries
+ (fn [query]
+ (check-subquery-marked query)
+ ((if (subquery? query) map! compile!) query context))
+ #(append-sql! context separator)))
+
+(def ^:private clause-fns
+ (ordered-map/ordered-map
+ :with (partial with! "WITH ")
+ :with-recursive (partial with! "WITH RECURSIVE ")
+ :create-table create-table!
+ :with-columns with-columns!
+ :insert-into insert-into!
+ :columns identifier-list!
+ :values values!
+ :drop-table drop-table!
+ :update update!
+ :set set-clause!
+ :delete-from delete-from!
+ :select (partial select! "SELECT ")
+ :select-distinct (partial select! "SELECT DISTINCT ")
+ :from from!
+ :join (partial join! :join)
+ :left-join (partial join! :left)
+ :right-join (partial join! :right)
+ :inner-join (partial join! :inner)
+ :where where!
+ :group-by group-by!
+ :having having!
+ :partition-by partition-by!
+ ;; a `UNION` combines complete `SELECT`s and any trailing `ORDER BY`/`LIMIT`/`OFFSET` applies to the combined
+ ;; result, so the union body has to be emitted *before* those, not after
+ :union (partial union! " UNION ")
+ :union-all (partial union! " UNION ALL ")
+ :order-by order-by!
+ :limit limit!
+ :offset offset!
+ :for for!
+ :on-conflict on-conflict!
+ :do-update-set do-update-set!
+ :returning returning!
+ :nest parens!))
+
+(def ^:private clause-rank
+ (into {}
+ (map-indexed (fn [i [k _f]] [k i]))
+ clause-fns))
+
+(defn- map!
+ "Compile a map. This is normally only allowed by [[format]] but not by [[compile!]] to avoid accidentally compiling
+ subqueries where unintended."
+ [m context]
+ (interpose-fn (sort-by clause-rank (keys m))
+ (fn [k]
+ (let [f (or (clause-fns k)
+ (throw (ex-info (clojure.core/format "Top-level map key %s is not currently supported" (pr-str k))
+ {:k k})))]
+ (f (get m k) context)))
+ #(append-sql! context " ")))
+
+(defn- quoted-identifier!
+ "Emit `s` as a single quoted identifier. `s` must already be validated -- it is not escaped."
+ [s context]
+ (let [engine (engine context)
+ quote-char (case engine
+ :mysql "`"
+ "\"")]
+ (append-sql! context quote-char)
+ (append-sql! context (case engine
+ :h2 (u/upper-case-en s)
+ s))
+ (append-sql! context quote-char)))
+
+(defn- identifier-part!
+ "Emit a single quoted and escaped identifier part."
+ [part context]
+ (if (= part "*")
+ (append-sql! context "*")
+ (do
+ (when-not (re-matches #"^[A-Za-z_][?A-Za-z0-9_-]*$" part)
+ (throw (ex-info "Invalid identifier" {:identifier part})))
+ (quoted-identifier! part context))))
+
+(defn- identifier!
+ "Emit a (possibly qualified) identifier composed of multiple [[identifier-part!]]s."
+ [s context]
+ (interpose-fn (str/split s #"\.") #(identifier-part! % context) #(append-sql! context ".")))
+
+(defn- alias!
+ "Emit the alias in `<x> AS <alias>`. An alias names a single thing, so like Honey SQL, an unqualified keyword alias is
+ quoted whole rather than split on `.` into qualified parts -- `:report_card.name` is the column `\"report_card.name\"`,
+ not `\"report_card\".\"name\"`, which isn't valid SQL in an alias. For the same reason a qualified keyword or a
+ multi-part [[h2x/identifier]] throws rather than compiling to `AS \"a\".\"b\"`."
+ [alias context]
+ (cond
+ (simple-keyword? alias)
+ (let [s (name alias)]
+ (when-not (re-matches #"^[A-Za-z_][?A-Za-z0-9_.-]*$" s)
+ (throw (ex-info "Invalid alias" {:alias alias})))
+ (quoted-identifier! s context))
+
+ (or (qualified-keyword? alias)
+ ;; otherwise it's `[::h2x/identifier <identifier-type> <parts>]`, since [[check-identifier-form]] ran first
+ (> (count (get alias 2)) 1))
+ (throw (ex-info "Invalid alias: an alias cannot be qualified" {:alias alias}))
+
+ :else
+ (compile! alias context)))
+
+(defn- keyword!
+ "Compile a keyword as a quoted and escaped identifier."
+ [k context]
+ ;; the `%` shorthand can carry a `/` (`:%lower.metabase_field/name`), so match against the keyword's whole printed
+ ;; form rather than just its name
+ (let [s (if-let [kw-ns (namespace k)]
+ (str kw-ns "/" (name k))
+ (name k))]
+ (cond
+ ;; function keyword e.g. `:%now`, `:%count.*`, or `:%lower.metabase_field/name`. This has to come before the
+ ;; qualified-keyword case: `:%lower.metabase_field/name` *is* a qualified keyword -- its namespace is
+ ;; `%lower.metabase_field` -- but it means `lower(metabase_field.name)`, not an identifier. `.` separates the
+ ;; function name from its arguments, and a `/` within an argument qualifies it as `table.column`.
+ (str/starts-with? s "%")
+ (let [[f & args] (str/split (subs s 1) #"\.")]
+ (compile! (into [(keyword f)] (map keyword) args) context))
+
+ (qualified-keyword? k)
+ (do
+ (identifier! (namespace k) context)
+ (append-sql! context ".")
+ (identifier! (name k) context))
+
+ :else
+ (identifier! (name k) context))))
+
+(def ^:private predicate-operators
+ "Operators that compile to a bare SQL predicate -- `x IS NULL`, `a AND b`, `x IN (...)`, `x < 1`. Used as the operand
+ of a comparison these have to be parenthesized; everything else ([[simple-fn!]] calls, `:cast`, the `h2x/` forms,
+ arithmetic) either brings its own delimiters or binds tighter than a comparison already, and wrapping those would
+ just add noise."
+ #{:!=
+ :<
+ :<=
+ :<>
+ :=
+ :>
+ :>=
+ :and
+ :between
+ :escape
+ :exists
+ :ilike
+ :in
+ :is
+ :is-not
+ :like
+ :not
+ :not-exists
+ :not-in
+ :not-like
+ :not=
+ :or
+ honey.sql.pg-ops/atat})
+
+(defn- unwrap-h2x-typed
+ "`x` without its `::h2x/typed` wrappers, if any. `h2x/+` and friends wrap their result in one when an argument carries
+ database type info, and it compiles to just the form it wraps, so look through it to see what that form is."
+ [x]
+ (if (fn-call-of-type? x ::h2x/typed)
+ (recur (get x 1))
+ x))
+
+(defn- predicate-call?
+ "Whether `x` is a [[fn-call?]] for one of the [[predicate-operators]]."
+ [x]
+ (fn-call-of-type? (unwrap-h2x-typed x) predicate-operators))
+
+(def ^:private infix-operators
+ (into predicate-operators #{:+ :- :/ :* :% :||}))
+
+(defn- infix-call?
+ "Whether `x` compiles to a bare infix expression -- a [[predicate-call?]], arithmetic, or `||` -- which needs parens
+ as the operand of another infix operator: `[:* [:+ :x 1] 2]` is `(x + 1) * 2`, not `x + 1 * 2`. `:escape` is the
+ exception. It is the postfix `ESCAPE` of the `LIKE` pattern it is the operand of, and `x LIKE (? ESCAPE '!')` is not
+ valid SQL."
+ [x]
+ (let [x (unwrap-h2x-typed x)]
+ (and (fn-call? x)
+ (not (fn-call-of-type? x :escape))
+ (fn-call-of-type? x infix-operators))))
+
+(defn- infix-operand!
+ "Compile `x` as the operand of an infix operator like `*`, `IS`, `BETWEEN` or `IN`, parenthesized if it is an
+ [[infix-call?]]."
+ [x context]
+ ((if (infix-call? x) parens! compile!) x context))
+
+(defn- equals! [sql nil-sql [x y :as args] context]
+ (when-not (= (count args) 2)
+ (throw (ex-info "Wrong number of args to :=/:!=/:<>/:not= (expected 2 args)" {:args args})))
+ ;; A `nil` on *either* side becomes `IS [NOT] NULL` against whichever side is non-`nil`, matching Honey SQL's
+ ;; `:transform-null-equals`. This has to cover `[:= nil x]` as well as `[:= x nil]`: compiling the `nil` as a plain
+ ;; operand would give `NULL = x`, which evaluates to `NULL` rather than `TRUE` and so silently matches no rows.
+ ;; `[:= nil nil]` keeps a literal `NULL` on the left, giving `NULL IS NULL`.
+ (letfn [(operand! [v]
+ ;; make sure if the operand is itself something like `[:= x nil]` we get `(x IS NULL) = <y>` instead of
+ ;; the unparsable `x IS NULL = y`. (A scalar subquery gets the same treatment, but [[compile!]] already
+ ;; parenthesizes those.)
+ ((if (predicate-call? v) parens! compile!) v context))]
+ (if (or (nil? x) (nil? y))
+ (do
+ (operand! (if (nil? x) y x))
+ (append-sql! context nil-sql))
+ (do
+ (operand! x)
+ (append-sql! context sql)
+ (operand! y)))))
+
+(defn- compound!
+ "Compile a compound boolean expression like `:and` or `:or`. Like Honey SQL we ignore `nil` args -- this is what makes
+ `[:and x (when y z)]` work -- and if nothing is left we compile `identity-value` (`TRUE` for AND, `FALSE` for OR)
+ rather than emitting nothing at all."
+ [sql identity-value xs context]
+ (let [xs (filter some? xs)]
+ (condp = (count xs)
+ 0 (compile! identity-value context)
+ 1 (compile! (first xs) context)
+ (interpose-fn xs #(parens! % context) #(append-sql! context sql)))))
+
+(defn- not! [[x] context]
+ (append-sql! context "NOT ")
+ (parens! x context))
+
+(defn- param-value
+ "The value `context`'s options bind to `k`, for a `[:param k]` form. Throws if param named by `k` is missing from
+ `:params` in the options map."
+ [k context]
+ {:pre [(keyword? k)]}
+ ;; look the key up with a sentinel rather than testing the value for truthiness -- `false` and `nil` are both
+ ;; legitimate parameter values.
+ (let [v (get-in (options context) [:params k] ::not-found)]
+ (when (= v ::not-found)
+ (throw (ex-info "Missing value for :param" {:param k})))
+ v))
+
+(defn- lift-value [x]
+ (cond
+ ((some-fn fn-call? keyword?) x)
+ [:lift x]
+
+ ((some-fn sequential? set?) x)
+ (mapv lift-value x)
+
+ :else
+ x))
+
+(defn- in-values
+ "The values side of an `:in`/`:not-in` form, with a `[:param k]` naming a collection resolved to
+ that collection.
+
+ `IN` takes a list of values rather than one value, so a collection bound to a param has to expand
+ into `(?, ?)`. Compiled as an ordinary param it emits a single `?` and binds the whole collection,
+ which no database accepts -- and an empty one would slip past the rewrite below and emit `IN ()`.
+ Honey SQL expands a collection-valued param in this position the same way.
+
+ A param naming anything else is left alone, and compiles to a single `?` as it would in any other
+ value slot. A scalar is not a list of values, and a map must stay one bound value rather than
+ become a list of its entries -- `metabase.app-db.honeysql-guard` is what refuses an unmarked one."
+ [vs context]
+ (if (fn-call-of-type? vs :param)
+ (let [v (lift-value (param-value (second vs) context))]
+ (if (or (sequential? v) (set? v))
+ v
+ vs))
+ vs))
+
+(defn- in! [f [lhs vs] context]
+ (let [vs (in-values vs context)]
+ (when-not (or (empty? vs)
+ (sequential? vs)
+ (set? vs)
+ (subquery? vs))
+ (throw (ex-info "Invalid sequence of values (maps must be marked with ^:allow-subquery)" {:vs vs})))
+ (if (empty? vs)
+ (compile! (case f
+ :in false
+ :not-in true)
+ context)
+ ;; non-empty values
+ (do
+ (infix-operand! lhs context)
+ (append-sql! context (case f
+ :in " IN "
+ :not-in " NOT IN "))
+ (cond
+ (subquery? vs)
+ (compile! vs context)
+
+ ;; sequence of sequences
+ (and (sequential? (first vs))
+ (not (fn-call? (first vs))))
+ (do
+ (append-sql! context "(")
+ (interpose-fn vs #(list! % context) #(append-sql! context ", "))
+ (append-sql! context ")"))
+
+ ;; Handle nonsense like`[:in :field [:inline [3]]]`
+ (fn-call? vs)
+ (compile! vs context)
+
+ :else
+ (list! vs context))))))
+
+(defn- between! [[x y z] context]
+ (infix-operand! x context)
+ (append-sql! context " BETWEEN ")
+ (infix-operand! y context)
+ (append-sql! context " AND ")
+ (infix-operand! z context))
+
+(defn- cast! [[x type-name] context]
+ (append-sql! context "CAST(")
+ (compile! x context)
+ (append-sql! context " AS ")
+ (raw-type-name! type-name context)
+ (append-sql! context ")"))
+
+(defn- case! [args context]
+ (append-sql! context "CASE ")
+ (loop [[condition expr & more] args]
+ (if-not (= condition :else)
+ (do
+ (append-sql! context "WHEN ")
+ (compile! condition context)
+ (append-sql! context " THEN ")
+ (compile! expr context))
+ (do
+ (append-sql! context "ELSE ")
+ (compile! expr context)))
+ (when (seq more)
+ (append-sql! context \space)
+ (recur more)))
+ (append-sql! context " END"))
+
+(defn- exists! [sql subquery context]
+ (check-subquery-marked subquery)
+ (append-sql! context sql)
+ (compile! subquery context))
+
+(defn- inline! [x context]
+ (letfn [(inlineable-atomic-value? [x]
+ ((some-fn nil? number? boolean?) x))
+ ;; splice numbers directly rather than via [[compile!]], since [[number!]] binds non-integral ones
+ (inline-atomic-value! [x]
+ ((if (number? x) splice-number! compile!) x context))]
+ (cond
+ (inlineable-atomic-value? x)
+ (inline-atomic-value! x)
+
+ (and ((some-fn sequential? set?) x)
+ (every? inlineable-atomic-value? x))
+ (do
+ (append-sql! context "(")
+ (interpose-fn x inline-atomic-value! #(append-sql! context ", "))
+ (append-sql! context ")"))
+
+ :else
+ (do
+ ;; TODO (Cam 2026-10-01) Make this an actual error instead of just a warning
+ (log/warnf ":inline is only allowed for numbers, booleans and nil, got: %s" (pr-str x))
+ (compile! x context)))))
+
+(defn- check-valid-unit [unit]
+ (when-not (and ((some-fn keyword? string?) unit)
+ (re-matches #"^[a-zA-Z0-9]+$" (name unit)))
+ (throw (ex-info "Invalid unit" {:unit unit}))))
+
+(defn- timestamp-diff! [[unit col-x col-y] context]
+ (check-valid-unit unit)
+ (append-sql! context "timestampdiff(")
+ (append-sql! context (name unit))
+ (append-sql! context ", ")
+ (compile! col-x context)
+ (append-sql! context ", ")
+ (compile! col-y context)
+ (append-sql! context ")"))
+
+(defn- current-timestamp! [context]
+ (append-sql! context "current_timestamp"))
+
+(defn- over! [[expr m & more :as args] context]
+ ;; Honey SQL also accepts an alias inside the `:over` form, `[:over [expr window alias]]`. Don't silently drop it --
+ ;; the query would fail later with a confusing `column "alias" does not exist`.
+ (when (seq more)
+ (throw (ex-info (str "`:over` only supports [<expression> <window>]; put the alias outside instead, e.g. "
+ "[[:over [<expression> <window>]] <alias>]")
+ {:args args})))
+ ;; likewise don't silently drop anything else in the window -- [[map!]] would happily compile e.g. a `:where`, so
+ ;; anything other than `:partition-by` and `:order-by` has to be rejected up front
+ (let [unsupported-keys (remove #{:order-by :partition-by} (keys m))]
+ (when (seq unsupported-keys)
+ (throw (ex-info "`:over` only supports :partition-by and :order-by in the window"
+ {:window m, :unsupported-keys (vec unsupported-keys)}))))
+ (compile! expr context)
+ (append-sql! context " OVER (")
+ (when (seq m)
+ (map! m context))
+ (append-sql! context ")"))
+
+(defn- param! [k context]
+ (object! (param-value k context) context))
+
+(defn- unary-binary-operator! [f x context]
+ (case f
+ :+ (compile! x context)
+ :- (if (number? x)
+ (compile! (-' x) context)
+ (do
+ (append-sql! context "-")
+ ;; always parenthesize the operand: written bare, one that compiles to a negative number would give `--1`,
+ ;; which starts a SQL comment
+ (parens! x context)))))
+
+(defn- binary-operator! [f args context]
+ (if (= (count args) 1)
+ (unary-binary-operator! f (first args) context)
+ (let [f-str (case f
+ :like " LIKE "
+ :ilike " ILIKE "
+ :not-like " NOT LIKE "
+ :is " IS "
+ :is-not " IS NOT "
+ (str \space (name f) \space))]
+ (interpose-fn args #(infix-operand! % context) #(append-sql! context f-str)))))
+
+(defn- simple-fn! [f args context]
+ (let [f (name f)]
+ (append-sql! context f))
+ (list! args context))
+
+(defn- h2x-identifier! [[_identifier-type parts] context]
+ (interpose-fn parts #(identifier-part! % context) #(append-sql! context ".")))
+
+(defn- h2x-literal! [s context]
+ ;; only double backslashes on engines that actually treat `\` as an escape character inside a plain `'...'`
+ ;; literal. MySQL always does, unless the session's `NO_BACKSLASH_ESCAPES` sql_mode is set (see
+ ;; [[metabase.driver.mysql/utf8-string-literal]]'s docstring for the same caveat). Postgres and H2 do *not*, as long
+ ;; as `standard_conforming_strings` is on (the default) -- doubling backslashes there would corrupt a value that
+ ;; legitimately contains one instead of protecting it. See [[metabase.driver.sql.util/escape-sql]], which documents
+ ;; this exact per-engine distinction (`:ansi` vs `:backslashes` vs `:ansi+backslashes`) and warns against relying on
+ ;; any of these styles to sanitize untrusted input in the first place.
+ (let [s (as-> s s
+ (u/qualified-name s)
+ (cond-> s
+ (= (engine context) :mysql) (str/replace "\\" "\\\\"))
+ (str/replace s "'" "''"))]
+ (append-sql! context "'")
+ (append-sql! context s)
+ (append-sql! context "'")))
+
+(defn- h2x-extract! [[unit expr] context]
+ (check-valid-unit unit)
+ (append-sql! context "extract(")
+ (append-sql! context (name unit))
+ (append-sql! context " FROM ")
+ (compile! expr context)
+ (append-sql! context ")"))
+
+(defn- h2x-distinct-count! [expr context]
+ (append-sql! context "count(DISTINCT ")
+ (compile! expr context)
+ (append-sql! context ")"))
+
+(defn- h2x-percentile-cont! [[expr fraction] context]
+ (when-not (number? fraction)
+ (throw (ex-info "Invalid continuous percentile fraction" {:fraction fraction})))
+ (append-sql! context "percentile_cont(")
+ ;; splice the fraction as a literal, which [[number!]] would otherwise bind as a parameter
+ (splice-number! fraction context)
+ (append-sql! context ") WITHIN GROUP (ORDER BY ")
+ (compile! expr context)
+ (append-sql! context ")"))
+
+(defn- h2x-collate! [[expr collation] context]
+ (when-not (re-matches #"^\w+$" (name collation))
+ (throw (ex-info "Invalid collation" {:collation collation})))
+ (compile! expr context)
+ (append-sql! context " COLLATE ")
+ (append-sql! context (name collation)))
+
+(defn- h2x-at-time-zone! [[expr zone] context]
+ ;; `ZoneId/of` only accepts a known region like `America/New_York` or `Etc/GMT+5`, or an offset like `+05:00`, so
+ ;; its ID can't contain a `'` and is safe to splice into the literal below
+ (let [^ZoneId zone-id (try
+ (ZoneId/of (name zone))
+ (catch DateTimeException e
+ (throw (ex-info "Invalid time zone" {:time-zone zone} e))))]
+ (append-sql! context "(")
+ (compile! expr context)
+ (append-sql! context " AT TIME ZONE '")
+ (append-sql! context (.getId zone-id))
+ (append-sql! context "')")))
+
+(defn- h2x-interval! [engine [amount unit] context]
+ (when-not (number? amount)
+ (throw (ex-info "Invalid amount" {:amount amount})))
+ (when-not (#{:millisecond :second :minute :hour :day :week :month :year} unit)
+ (throw (ex-info "Invalid unit" {:unit unit})))
+ (append-sql! context (case engine
+ :postgres "INTERVAL '"
+ :mysql "INTERVAL "))
+ ;; always splice the amount: a non-integral one would otherwise be bound, and on Postgres the `?` would land inside
+ ;; the quoted literal, where JDBC ignores it
+ (splice-number! amount context)
+ (append-sql! context " ")
+ (append-sql! context (name unit))
+ (when (= engine :postgres)
+ (append-sql! context "'")))
+
+(defn- escape! [[pattern escape-chars] context]
+ ;; `ESCAPE` is a postfix operator on a `LIKE` pattern (`... LIKE ? ESCAPE '!'`), not a function call -- compiling it
+ ;; as `escape(?, '!')` produces a `Function "ESCAPE" not found` error from the database.
+ (compile! pattern context)
+ (append-sql! context " ESCAPE ")
+ (compile! escape-chars context))
+
+(defn- position!
+ "`[:position <substring> <string>]` compiles to `position(<substring> IN <string>)`, the 1-based index of the first
+ occurrence of `substring` in `string`, or `0` if there isn't one. H2, Postgres and MySQL all accept this standard
+ form, but Postgres rejects the comma form `position(<substring>, <string>)` that Honey SQL emits."
+ [[substring s :as args] context]
+ (when-not (= (count args) 2)
+ (throw (ex-info "Wrong number of args to :position (expected 2 args)" {:args args})))
+ (append-sql! context "position(")
+ (infix-operand! substring context)
+ (append-sql! context " IN ")
+ (infix-operand! s context)
+ (append-sql! context ")"))
+
+(defn- postgres-full-text-search-match
+ "Implementation for [[honey.sql.pg-ops/atat]] (`@@`)."
+ [[lhs rhs] context]
+ (check-identifier-form lhs)
+ (check-identifier-form rhs)
+ (compile! lhs context)
+ (append-sql! context " @@ ")
+ (compile! rhs context))
+
+(defn- fn-call! [[f & args] context]
+ ;; This `case` has no default behavior for an unknown function on purpose: `f` can come from an attacker-derived
+ ;; `:%foo` keyword (see `keyword!`), so an unrecognized function name must throw instead of being spliced into the
+ ;; SQL raw. All allowed functions need to be explicitly whitelisted here.
+ ;;
+ ;; ⚠⚠⚠ DO NOT ADD SUPPORT FOR `:raw` -- IT IS NOT SUPPORTED ON PURPOSE ⚠⚠⚠
+ ;;
+ (case f
+ (:<> :!= :not=) (equals! " <> " " IS NOT NULL" args context)
+ := (equals! " = " " IS NULL" args context)
+ :and (compound! " AND " true args context)
+ :between (between! args context)
+ :case (case! args context)
+ :cast (cast! args context)
+ :composite (list! args context)
+ :current-timestamp (current-timestamp! context)
+ :escape (escape! args context)
+ :exists (exists! "EXISTS " (first args) context)
+ :in (in! f args context)
+ :inline (inline! (first args) context)
+ :lift (object! (first args) context)
+ :not (not! args context)
+ :not-exists (exists! "NOT EXISTS " (first args) context)
+ :not-in (in! f args context)
+ :or (compound! " OR " false args context)
+ :over (over! (first args) context)
+ :param (param! (first args) context)
+ :position (position! args context)
+ :timestampdiff (timestamp-diff! args context)
+
+ (:< :<= :> :>= :like :ilike :not-like :+ :- :/ :* :% :|| :is :is-not)
+ (binary-operator! f args context)
+
+ ;; `:call` exists for Honey SQL 1 compatibility e.g. `[:call f & args]`, equivalent to `[f & args]`
+ :call
+ (recur args context)
+
+ ;; custom legacy `h2x/` operators
+ ::h2x/identifier (h2x-identifier! args context)
+ ::h2x/literal (h2x-literal! (first args) context)
+ ::h2x/extract (h2x-extract! args context)
+ ::h2x/timestampdiff (timestamp-diff! args context)
+ ::h2x/distinct-count (h2x-distinct-count! (first args) context)
+ ::h2x/percentile-cont (h2x-percentile-cont! args context)
+ ::h2x/collate (h2x-collate! args context)
+ ::h2x/at-time-zone (h2x-at-time-zone! args context)
+ ::h2x/typed (compile! (first args) context)
+ ::h2x/raw-type-name (raw-type-name! (first args) context)
+ ::h2x/postgres-interval (h2x-interval! :postgres args context)
+ ::h2x/mysql-interval (h2x-interval! :mysql args context)
+
+ ;; other custom operators
+ (:abs
+ :avg
+ :ceil
+ :coalesce
+ :concat
+ :concat_ws
+ :count
+ :current_database
+ :current_schema
+ :database
+ :date_part
+ :dateadd
+ :date_add
+ :datediff
+ :day
+ :distinct
+ :floor
+ :greatest
+ :least
+ :isnull
+ :json_contains_path
+ :json_search
+ :jsonb_build_object
+ :jsonb_path_exists
+ :left
+ :length
+ :locate
+ :lower
+ :max
+ :min
+ :month
+ :now
+ :rand
+ :random
+ :regexp_replace
+ :replace
+ :row_number
+ :round
+ :setweight
+ :split_part
+ :sum
+ :substring
+ :to_regclass
+ :to_tsquery
+ :to_tsvector
+ :trim
+ :ts_rank
+ :upper
+ :year)
+ (simple-fn! f args context)
+
+ #_else
+ (condp = f
+ ;; [[honey.sql.pg-ops/atat]] is just `:@@`, but that doesn't work as a keyword literal, and [[case]] treats the
+ ;; raw symbol as a compile-time constant instead of evaluating it to `:@@`, so unfortunately we just have to
+ ;; have this fallback [[condp]] here 😢
+ ;;
+ ;; TODO (Cam 2026-10-15) consider whether we should switch to a custom namespaced keyword called
+ ;; `:metabase.funnysql.core/postgres-full-text-search-match`, this was actually what I did originally but I
+ ;; changed it to the Honey SQL keyword for compatibility
+ honey.sql.pg-ops/atat
+ (postgres-full-text-search-match args context)
+
+ #_else
+ (throw (ex-info (clojure.core/format "Function %s is not currently supported; add it to metabase.funnysql.core/fn-call! if it should be"
+ (pr-str f))
+ {:f f, :args args})))))
+
+(defn- sequence! [xs context]
+ (cond
+ (fn-call? xs)
+ (fn-call! xs context)
+
+ ;; a function call has to be a vector. Compiled as a list of values, a seq like `(cons :or clauses)` gives
+ ;; `("or", ...)`, which the database only rejects later with a misleading `Column "OR" not found`
+ (and (sequential? xs)
+ (keyword? (first xs)))
+ (throw (ex-info "A function call must be a vector, not a seq" {:form xs}))
+
+ :else
+ (list! xs context)))
+
+(extend-protocol Compile
+ Object (compile! [this context] (object! this context))
+ nil (compile! [this context] (null! this context))
+ Boolean (compile! [this context] (boolean! this context))
+ Number (compile! [this context] (number! this context))
+ clojure.lang.Keyword (compile! [this context] (keyword! this context))
+ ;; a `^:allow-subquery` map compiled anywhere other than the top level is a subquery -- a scalar subquery in a
+ ;; `SELECT` list or function argument, a derived table in `FROM`, etc. -- all of which need to be parenthesized.
+ clojure.lang.IPersistentMap (compile! [this context] ((if (:allow-subquery (meta this))
+ parens!
+ object!) this context))
+ clojure.lang.IPersistentSet (compile! [this context] (sequence! this context))
+ clojure.lang.Sequential (compile! [this context] (sequence! this context)))
+
+(defn format
+ "Compile `honeysql-form` (either a top-level map or an individual clause) to SQL for `engine`.
+
+ Returns the standard `[sql & args]` shape if able to compile successfully; throws an exception on unsupported or
+ invalid forms."
+ ([honeysql-form engine]
+ (format honeysql-form engine nil))
+ ([honeysql-form engine options]
+ {:pre [(#{:postgres :h2 :mysql} engine)]}
+ (try
+ (let [context (default-context engine options)]
+ ;; [[compile!]] doesn't support compiling maps recursively unless marked `^:allow-subquery`
+ ((if (map? honeysql-form) map! compile!) honeysql-form context)
+ (result! context))
+ (catch Exception e
+ (throw (ex-info (str "Error compiling Honey SQL: " (ex-message e))
+ {:form honeysql-form, :engine engine, :options options}
+ e))))))
diff --git a/src/metabase/internal_stats/db.clj b/src/metabase/internal_stats/db.clj
index 75d7aa287b5..97a99193b2c 100644
--- a/src/metabase/internal_stats/db.clj
+++ b/src/metabase/internal_stats/db.clj
@@ -5,6 +5,7 @@
[metabase.app-db.core :as mdb]
[metabase.internal-stats.util :as u]
[metabase.models.interface :as mi]
+ [metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
[toucan2.core :as t2]))
@@ -138,24 +139,26 @@
(condp = (mdb/db-type)
:mysql [:json_contains_path
:dataset_query
- ^:allow-raw-sql [:inline "one"]
- ^:allow-raw-sql [:inline "$.native.\"template-tags\".*"]]
+ "one"
+ "$.native.\"template-tags\".*"]
:postgres [:jsonb_path_exists
[:cast :dataset_query :jsonb]
- ^:allow-raw-sql [:inline "$.native.\"template-tags\" ? (exists(@.*))"]]))
+ ;; the path is a `jsonpath`, not a value: as a `?` parameter Postgres binds it as `varchar` and can't
+ ;; find a matching `jsonb_path_exists` overload, so it has to be a real SQL literal
+ (h2x/literal "$.native.\"template-tags\" ? (exists(@.*))")]))
(defn- contains-embedding-param
[param]
(condp = (mdb/db-type)
:mysql [:!= [:json_search
:embedding_params
- ^:allow-raw-sql [:inline "one"]
+ "one"
param]
nil]
:postgres [:jsonb_path_exists
[:cast :embedding_params :jsonb]
- ^:allow-raw-sql [:inline "$.* ? (@ == $val)"]
- [:jsonb_build_object ^:allow-raw-sql [:inline "val"] param]]))
+ (h2x/literal "$.* ? (@ == $val)")
+ [:jsonb_build_object "val" param]]))
(def ^:private embedding-on [:= :enable_embedding [:inline true]])
diff --git a/src/metabase/parameters/db.clj b/src/metabase/parameters/db.clj
index 8f919a6c9e8..d77b8351c52 100644
--- a/src/metabase/parameters/db.clj
+++ b/src/metabase/parameters/db.clj
@@ -2,8 +2,6 @@
"Application database queries for the parameters module. Every function here is a direct Toucan 2 call with no
additional logic, so no other namespace in the module runs a query itself (hydration definitions still use `toucan2.core`)."
(:require
- [clojure.string :as str]
- [honey.sql :as sql]
[metabase.app-db.core :as mdb]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
@@ -11,20 +9,27 @@
[metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
-(defn- format-union
- "Workaround for https://github.com/seancorfield/honeysql/issues/451. Wrap the subselects in parens, otherwise it
- will fail on Postgres."
- [_clause exprs]
- (let [[sqls args] (sql/format-expr-list exprs)
- formatted (str/join " UNION " sqls)]
- (into [formatted] args)))
+(defn- nested
+ "Wrap a subselect in parens so it can be used as a `:union` arm. Each arm here has its own `:limit`, which Postgres
+ rejects unless the arm is parenthesized."
+ [query]
+ ^:allow-subquery {:nest query})
-(sql/register-clause! ::union format-union :union)
+(def ^:private mapping-type-code->name
+ "Each `:union` arm in [[remapped-field]] tags its row with the kind of remapping it found. The tag is a small integer
+ rather than the name itself because only numbers get spliced into the SQL as literals -- a string would become a `?`
+ parameter, and H2 cannot work out the type of a parameter that sits in the same position in every arm of a `UNION`."
+ {1 "fk->field"
+ 2 "fk->pk->name"
+ 3 "pk->name"})
+
+(def ^:private mapping-type-name->code
+ (into {} (map (fn [[code mapping-type]] [mapping-type code])) mapping-type-code->name))
(defn- implicit-pk->name-mapping-query
[field-id mapping-type]
^:allow-subquery
- {:select [[:dest.id :id] [^:allow-raw-sql [:inline mapping-type] :mapping_type]]
+ {:select [[:dest.id :id] [(mapping-type-name->code mapping-type) :mapping_type]]
:from [(warehouse-schema-overlay/field-query {:alias :source})]
:left-join [[:metabase_table :table] [:= :source.table_id :table.id]
(warehouse-schema-overlay/field-query {:alias :dest}) [:= :dest.table_id :table.id]]
@@ -39,32 +44,36 @@
when `allow-implicit-uuid-remapping?` — an implicit FK->PK->Name or PK->Name mapping, or nil."
[field-id :- ::lib.schema.id/field
allow-implicit-uuid-remapping? :- :boolean]
- (t2/query-one
- {:select [[:mapping.id :id] [:mapping.mapping_type :mapping_type]]
- :from [[^:allow-subquery
- {::union (into [;; Explicit FK Field->Field remapping
- ^:allow-subquery
- {:select [[:dimension.human_readable_field_id :id] [^:allow-raw-sql [:inline "fk->field"] :mapping_type]]
- :from [[:dimension :dimension]]
- :where [:and
- [:= :dimension.field_id field-id]
- [:not= :dimension.human_readable_field_id nil]]
- :limit 1}]
- (when allow-implicit-uuid-remapping?
- [;; Implicit FK Field -> PK Field -> [Name] Field remapping
- (implicit-pk->name-mapping-query
- ^:allow-subquery
- {:select [:fk_target_field_id]
- :from [(warehouse-schema-overlay/field-query)]
- :where [:and
- [:= :id field-id]
- (mdb/isa :semantic_type :type/FK)]
- :limit 1}
- "fk->pk->name")
- ;; Implicit PK Field-> [Name] Field remapping
- (implicit-pk->name-mapping-query field-id "pk->name")]))}
- :mapping]]
- :limit 1}))
+ (some-> (t2/query-one
+ {:select [[:mapping.id :id] [:mapping.mapping_type :mapping_type]]
+ :from [[^:allow-subquery
+ {:union (into [;; Explicit FK Field->Field remapping
+ (nested
+ ^:allow-subquery
+ {:select [[:dimension.human_readable_field_id :id]
+ [(mapping-type-name->code "fk->field") :mapping_type]]
+ :from [[:dimension :dimension]]
+ :where [:and
+ [:= :dimension.field_id field-id]
+ [:not= :dimension.human_readable_field_id nil]]
+ :limit 1})]
+ (when allow-implicit-uuid-remapping?
+ [;; Implicit FK Field -> PK Field -> [Name] Field remapping
+ (nested
+ (implicit-pk->name-mapping-query
+ ^:allow-subquery
+ {:select [:fk_target_field_id]
+ :from [(warehouse-schema-overlay/field-query)]
+ :where [:and
+ [:= :id field-id]
+ (mdb/isa :semantic_type :type/FK)]
+ :limit 1}
+ "fk->pk->name"))
+ ;; Implicit PK Field-> [Name] Field remapping
+ (nested (implicit-pk->name-mapping-query field-id "pk->name"))]))}
+ :mapping]]
+ :limit 1})
+ (update :mapping_type mapping-type-code->name)))
(mu/defn field
"The Field with `field-id`, or nil."
diff --git a/src/metabase/search/appdb/index_schema.clj b/src/metabase/search/appdb/index_schema.clj
index f90a1db4a0d..82cf2612180 100644
--- a/src/metabase/search/appdb/index_schema.clj
+++ b/src/metabase/search/appdb/index_schema.clj
@@ -32,7 +32,9 @@
[:legacy_input :text :not-null]
;; useful for tracking the speed and age of the index
[:created_at :timestamp-with-time-zone
- [:default ^:allow-raw-sql [:raw "CURRENT_TIMESTAMP"]]
+ ;; TODO (Cam 2026-09-29) shouldn't this be :%current-timestamp.6 for MySQL?? Maybe this should be
+ ;; using [[metabase.util.honey-sql-2/current-datetime-honeysql-form]]
+ [:default :%current-timestamp]
:not-null]
[:updated_at :timestamp-with-time-zone :not-null]]
(keep (fn [[k t]]
diff --git a/src/metabase/search/appdb/specialization/postgres.clj b/src/metabase/search/appdb/specialization/postgres.clj
index ed438b93d28..498aa0d400d 100644
--- a/src/metabase/search/appdb/specialization/postgres.clj
+++ b/src/metabase/search/appdb/specialization/postgres.clj
@@ -1,13 +1,14 @@
(ns metabase.search.appdb.specialization.postgres
(:require
[clojure.string :as str]
+ [honey.sql.pg-ops]
[metabase.search.appdb.specialization.api :as specialization]
[metabase.search.db :as search.db]
[metabase.search.util :as search.util]
[metabase.util.honey-sql-2 :as h2x]))
(defmethod specialization/table-schema :postgres [base-schema]
- (into [[:id :bigint [:primary-key] ^:allow-raw-sql [:raw "GENERATED BY DEFAULT AS IDENTITY"]]
+ (into [[:id :bigint [:primary-key] :generated-by-default-as-identity]
[:search_vector :tsvector :not-null]
[:with_native_query_vector :tsvector :not-null]]
base-schema))
@@ -32,18 +33,18 @@
{:select select-items
:from [[active-table :search_index]]
;; Using a join allows us to share the query expression between our SELECT and WHERE clauses.
- :join [[[:to_tsquery ^:allow-raw-sql [:inline (search.util/tsv-language)]
+ ;; the text-search config name is a `regconfig`, not a value: as a `?` parameter Postgres cannot resolve the
+ ;; overload, so it has to be a real SQL literal
+ :join [[[:to_tsquery (h2x/literal (search.util/tsv-language))
[:lift (search.util/to-tsquery-expr search-term)]]
:query] [:= 1 1]]
:where (if (str/blank? search-term)
[:= [:inline 1] [:inline 1]]
- ^:allow-raw-sql
- [:raw
- (str
- (if (:search-native-query search-ctx)
- "with_native_query_vector"
- "search_vector")
- " @@ query")])})
+ [honey.sql.pg-ops/atat
+ (if (:search-native-query search-ctx)
+ :with_native_query_vector
+ :search_vector)
+ :query])})
(defmethod specialization/extra-entry-fields :postgres [entity]
{:search_vector
@@ -80,7 +81,8 @@
(defmethod specialization/text-score :postgres
[]
- [:ts_rank ^:allow-raw-sql [:inline ts-rank-weights] :search_vector :query [:inline ts-rank-normalization]])
+ ;; the weights are a `float4[]` literal; a `?` parameter leaves the `ts_rank` overload unresolvable
+ [:ts_rank (h2x/literal ts-rank-weights) :search_vector :query [:inline ts-rank-normalization]])
(defmethod specialization/view-count-percentile-query :postgres
[index-table p-value]
diff --git a/src/metabase/search/scoring.clj b/src/metabase/search/scoring.clj
index 44b6d1ca8f9..62f9e7b3a51 100644
--- a/src/metabase/search/scoring.clj
+++ b/src/metabase/search/scoring.clj
@@ -74,7 +74,7 @@
;; Use seconds for granularity in the fraction.
(if (= :mysql db-type)
[:coalesce
- [[:timestampdiff ^:allow-raw-sql [:raw "SECOND"] from-column to-column]]
+ [[:timestampdiff :second from-column to-column]]
[:* ceiling (double seconds-in-a-day)]]
[[::h2x/extract :epoch [:- to-column from-column]]])
[:inline (double seconds-in-a-day)]]]
diff --git a/src/metabase/search/util.clj b/src/metabase/search/util.clj
index 8138ea6393f..f39104fa89f 100644
--- a/src/metabase/search/util.clj
+++ b/src/metabase/search/util.clj
@@ -6,6 +6,7 @@
[metabase.search.ingestion.query :as search.ingestion.query]
[metabase.search.settings :as search.settings]
[metabase.util :as u]
+ [metabase.util.honey-sql-2 :as h2x]
[metabase.util.i18n :as i18n]
[metabase.util.string :as u.str]))
@@ -161,5 +162,11 @@
([weight text]
(weighted-tsvector weight text (tsv-language)))
([weight text lang]
- ;; tsvector has a max value size of 1048575 bytes, limit to less than that because the multiple values get concatenated together
- [:setweight [:to_tsvector ^:allow-raw-sql [:inline lang] [:cast (u.str/limit-bytes text search.ingestion.query/max-searchable-value-length) :text]] ^:allow-raw-sql [:inline weight]]))
+ ;; tsvector has a max value size of 1048575 bytes, limit to less than that because the multiple values get
+ ;; concatenated together
+ ;; `lang` is a `regconfig` and `weight` is a `"char"`; both have to be real SQL literals, since Postgres cannot
+ ;; resolve either overload from an `?` parameter bound as `varchar`
+ [:setweight
+ [:to_tsvector (h2x/literal lang)
+ [:cast (u.str/limit-bytes text search.ingestion.query/max-searchable-value-length) :text]]
+ (h2x/literal weight)]))
diff --git a/src/metabase/server/db.clj b/src/metabase/server/db.clj
index af670ea81f9..26c67004207 100644
--- a/src/metabase/server/db.clj
+++ b/src/metabase/server/db.clj
@@ -6,6 +6,7 @@
[metabase.app-db.core :as mdb]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.session.core :as session]
+ [metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[toucan2.core :as t2]
[toucan2.pipeline :as t2.pipeline]))
@@ -22,7 +23,7 @@
(def ^:private ^{:arglists '([db-type max-age-minutes session-type enable-advanced-permissions? enable-tenants? session-timeout-seconds mfa-required])} session-with-id-query
(mdb/memoize-for-application-db
- (fn [db-type max-age-minutes session-type enable-advanced-permissions? enable-tenants? session-timeout-seconds mfa-required]
+ (fn [db-type max-age-minutes session-type enable-advanced-permissions? enable-tenants? session-timeout-seconds mfa-required?]
(first
(t2.pipeline/compile*
(cond-> (merge session/session-from-and-joins
@@ -32,24 +33,23 @@
[:user.locale :user-locale]
[:auth_identity.provider :auth-provider]]
:where (into [:and
- [:= :session.key_hashed ^:allow-raw-sql [:raw "?"]]
+ [:= :session.key_hashed (Object.)] ; force a `?` placeholder
[:= :session.anti_csrf_token (case session-type
:normal nil
- :full-app-embed ^:allow-raw-sql [:raw "?"])]]
+ :full-app-embed (Object.))]]
cat
[(session/live-session-conditions
{:db-type db-type
:max-age-minutes max-age-minutes
:enable-tenants? enable-tenants?
:session-timeout-seconds session-timeout-seconds})
- (when mfa-required
+ (when mfa-required?
[[:or
[:not= :session.mfa_auth_identity_id nil]
(into [:and]
(map (fn [mfa-supporting-provider]
[:not= :auth_identity.provider
- ^:allow-raw-sql
- [:raw (str "'" (name mfa-supporting-provider) "'")]])
+ (h2x/literal (name mfa-supporting-provider))])
mfa-supported-methods))]])])
:limit [:inline 1]})
enable-advanced-permissions?
@@ -81,7 +81,7 @@
:left-join [[:core_user :user] [:= :api_key.user_id :user.id]]
:where [:and
[:= :user.is_active true]
- [:= :api_key.key_prefix ^:allow-raw-sql [:raw "?"]]]
+ [:= :api_key.key_prefix (Object.)]] ; to produce `?` in the SQL
:limit [:inline 1]}
enable-advanced-permissions?
(->
@@ -104,7 +104,7 @@
:from [[:core_user :user]]
:where [:and
[:= :user.is_active true]
- [:= :user.id ^:allow-raw-sql [:raw "?"]]]
+ [:= :user.id (Object.)]]
:limit [:inline 1]}
enable-advanced-permissions?
(->
@@ -120,20 +120,20 @@
Session whose `key_hashed` is `session-key-hash`, or nil if there is none. `anti-csrf-token`, when present,
additionally requires the Session's `anti_csrf_token` to match it (a full-app-embed session); `max-age-minutes`
and `session-timeout-seconds` (which may be nil) bound how old or idle the Session may be."
- [session-key-hash :- :string
- anti-csrf-token :- [:maybe :string]
- max-age-minutes :- [:maybe :int]
+ [session-key-hash :- :string
+ anti-csrf-token :- [:maybe :string]
+ max-age-minutes :- [:maybe :int]
enable-advanced-permissions? :- :boolean
enable-tenants? :- :boolean
session-timeout-seconds :- [:maybe :int]
- mfa-required :- :boolean]
+ mfa-required? :- :boolean]
(let [sql (session-with-id-query (mdb/db-type)
max-age-minutes
(if (seq anti-csrf-token) :full-app-embed :normal)
enable-advanced-permissions?
enable-tenants?
session-timeout-seconds
- mfa-required)
+ mfa-required?)
params (concat [session-key-hash] (when (seq anti-csrf-token) [anti-csrf-token]))]
(t2/query-one (cons sql params))))
diff --git a/src/metabase/transforms/db.clj b/src/metabase/transforms/db.clj
index b8c346e82be..b3590b7dd82 100644
--- a/src/metabase/transforms/db.clj
+++ b/src/metabase/transforms/db.clj
@@ -333,28 +333,28 @@
"Builds a `:order-by` clause for the paged run listing, translating display values for sortable columns per
`status-labels`/`run-method-labels`/`tag-name-labels` (maps of raw value to display label)."
[sort-column sort-direction status-labels run-method-labels tag-name-labels]
- (let [sort-column (or (keyword sort-column) :start-time)
- sort-direction (or (keyword sort-direction) :desc)
- nulls-sort (if (= sort-direction :asc)
- :nulls-last
- :nulls-first)]
+ (let [sort-column (or (keyword sort-column) :start-time)
+ sort-direction (or (keyword sort-direction) :desc)
+ sort-direction-with-nulls (case sort-direction
+ :asc :asc-nulls-last
+ :desc :desc-nulls-first)]
(conj
(case sort-column
- :transform-name [[:transform.name sort-direction]]
- :start-time [[:start_time sort-direction]]
- :end-time [[:end_time sort-direction nulls-sort]]
- :status [[(label-case :status status-labels) sort-direction]]
- :run-method [[(label-case :run_method run-method-labels) sort-direction]]
- :transform-tags [[(first-tag-name-subquery tag-name-labels) sort-direction nulls-sort]]
+ :transform-name [[:transform.name sort-direction]]
+ :start-time [[:start_time sort-direction]]
+ :end-time [[:end_time sort-direction-with-nulls]]
+ :status [[(label-case :status status-labels) sort-direction]]
+ :run-method [[(label-case :run_method run-method-labels) sort-direction]]
+ :transform-tags [[(first-tag-name-subquery tag-name-labels) sort-direction-with-nulls]]
;; In-progress runs (end_time = nil) sink to the bottom in BOTH
;; directions — null means "no measurable duration yet," not
;; "longest duration."
- :duration [[[:is :end_time nil] :asc]
- [(h2x/calculate-interval-honeysql-form
- (mdb/db-type) :end_time :start_time)
- sort-direction]]
+ :duration [[[:is :end_time nil] :asc]
+ [(h2x/calculate-interval-honeysql-form
+ (mdb/db-type) :end_time :start_time)
+ sort-direction]]
[[:start_time sort-direction]
- [:end_time sort-direction nulls-sort]])
+ [:end_time sort-direction-with-nulls]])
[:transform_run.id sort-direction])))
(def ^:private RunFilters
@@ -570,13 +570,15 @@
(defn- job-run-order-by
[sort-column sort-direction]
- (let [sort-direction (or (keyword sort-direction) :desc)
- nulls-sort (if (= sort-direction :asc) :nulls-last :nulls-first)]
+ (let [sort-direction (or (keyword sort-direction) :desc)
+ sort-direction-with-nulls (case sort-direction
+ :asc :asc-nulls-last
+ :desc :desc-nulls-first)]
(case (keyword sort-column)
:start_time [[:start_time sort-direction]]
- :end_time [[:end_time sort-direction nulls-sort]]
+ :end_time [[:end_time sort-direction-with-nulls]]
[[:start_time sort-direction]
- [:end_time sort-direction nulls-sort]])))
+ [:end_time sort-direction-with-nulls]])))
(mu/defn job-runs
"Up to `limit` (offset by `offset`) TransformJobRuns, optionally narrowed to `job-id`, `status`, `run-method`, and
@@ -663,11 +665,13 @@
;;; ------------------------------------------ Root run listing ------------------------------------------
;; Each branch must project the same columns in the same order for the UNION ALL to line up;
-;; `[nil :col]` fills in columns a table lacks.
+;; `[nil :col]` fills in columns a table lacks. The `run_type`/`run_method` discriminators have to be real SQL
+;; literals rather than plain strings: a string compiles to a `?` parameter, and H2 cannot work out the type of
+;; a parameter in the select list of a derived table (`Unknown data type: "RUN_TYPE"`).
(defn- job-run-subquery [transform-ids]
^:allow-subquery
- {:select [[^:allow-raw-sql [:inline "job"] :run_type]
+ {:select [[(h2x/literal "job") :run_type]
:id
[:job_id :entity_id]
[:job_name :entity_name]
@@ -688,13 +692,13 @@
(defn- dag-run-subquery [transform-ids]
^:allow-subquery
- {:select [[^:allow-raw-sql [:inline "dag"] :run_type]
+ {:select [[(h2x/literal "dag") :run_type]
:id
[:source_transform_id :entity_id]
[:source_transform_name :entity_name]
:direction
:transform_count
- [^:allow-raw-sql [:inline "manual"] :run_method]
+ [(h2x/literal "manual") :run_method]
:status :is_active :start_time :end_time :message
:user_id]
:from [:transform_dag_run]
@@ -708,7 +712,7 @@
(defn- transform-run-subquery [transform-ids]
^:allow-subquery
- {:select [[^:allow-raw-sql [:inline "transform"] :run_type]
+ {:select [[(h2x/literal "transform") :run_type]
:id
[:transform_id :entity_id]
[:transform_name :entity_name]
@@ -751,15 +755,18 @@
(defn- root-run-order-by
"Standard `:order-by` clause for a paged root-run listing. Sorts by `sort-column` (`:start_time` or `:end_time`;
anything else — including nil — falls back to ordering by start_time then end_time) in `sort-direction`
- (`:asc`/`:desc`, defaulting to `:desc`), with in-progress rows (null `end_time`) always ordered last."
+ (`:asc`/`:desc`, defaulting to `:desc`), with in-progress rows (null `end_time`) ordered as the most recent: last
+ ascending, first descending."
[sort-column sort-direction]
- (let [sort-direction (or (keyword sort-direction) :desc)
- nulls-sort (if (= sort-direction :asc) :nulls-last :nulls-first)]
+ (let [sort-direction (or (keyword sort-direction) :desc)
+ sort-direction-with-nulls (case sort-direction
+ :asc :asc-nulls-last
+ :desc :desc-nulls-first)]
(case (keyword sort-column)
:start_time [[:start_time sort-direction]]
- :end_time [[:end_time sort-direction nulls-sort]]
+ :end_time [[:end_time sort-direction-with-nulls]]
[[:start_time sort-direction]
- [:end_time sort-direction nulls-sort]])))
+ [:end_time sort-direction-with-nulls]])))
(mu/defn root-run-summaries-page
"Up to `limit` (offset by `offset`) root-run summary rows -- see [[metabase.transforms.run-listing]] -- of `types`
diff --git a/src/metabase/util/honey_sql_2.clj b/src/metabase/util/honey_sql_2.clj
index 12ae0a54e45..fc36a71de11 100644
--- a/src/metabase/util/honey_sql_2.clj
+++ b/src/metabase/util/honey_sql_2.clj
@@ -98,6 +98,24 @@
{:pre [(some-fn keyword? string?) (re-matches #"^[a-zA-Z0-9]+$" (name unit))]}
[::extract unit expr])
+;; register the `::timestampdiff` function with HoneySQL. MySQL's `TIMESTAMPDIFF` takes its unit as a bare keyword, which
+;; no plain Honey SQL form produces short of `:raw` -- a keyword unit comes out as a quoted identifier, `` `microsecond` ``.
+;; Funny SQL ([[metabase.funnysql.core]]) compiles this form to the same SQL.
+(defn- format-timestampdiff
+ "(sql/format-expr [::timestampdiff :microsecond :a :b])
+ => [\"timestampdiff(microsecond, a, b)\"]"
+ [_tag [unit x y]]
+ (when-not (and ((some-fn keyword? string?) unit)
+ (re-matches #"^[a-zA-Z0-9]+$" (name unit)))
+ (throw (ex-info "Invalid unit" {:unit unit})))
+ (let [[x-sql & x-args] (sql/format-expr x {:nested true})
+ [y-sql & y-args] (sql/format-expr y {:nested true})]
+ (into [(clojure.core/format "timestampdiff(%s, %s, %s)" (name unit) x-sql y-sql)]
+ cat
+ [x-args y-args])))
+
+(sql/register-fn! ::timestampdiff #'format-timestampdiff)
+
;; register the function `::distinct-count` with HoneySQL
(defn- format-distinct-count
"(sql/format-expr [::h2x/distinct-count :x])
@@ -465,12 +483,30 @@
[sql-type]
(boolean (re-matches raw-type-name-regex (name sql-type))))
+(defn- format-raw-type-name
+ "Emit a [[raw-type-name?]] type name as the SQL it names: unquoted, and with its spelling preserved exactly as the
+ engine reported it.
+
+ A type name needs its own form because neither shape Honey SQL splices on its own is the name we were given. A
+ string in the type position of `:cast` becomes a `?` parameter, which is not a type at all, so the engine rejects
+ the statement. A keyword goes through `honey.sql/sql-kw`, which upper-cases it and drops everything before a `/` --
+ so `timestamp` becomes `TIMESTAMP`, and ClickHouse's `DateTime64(3, 'America/New_York')` loses its timezone."
+ [_fn [sql-type]]
+ (let [type-name (name sql-type)]
+ ;; [[cast]] checks this before building the form; check it again here so the guarantee holds for anyone who writes
+ ;; the form directly, and so nothing unvalidated can reach the SQL through this path.
+ (when-not (raw-type-name? type-name)
+ (throw (ex-info "Invalid SQL type name" {:type type-name})))
+ [type-name]))
+
+(sql/register-fn! ::raw-type-name #'format-raw-type-name)
+
(mu/defn cast :- TypedExpression
"Generate a statement like `cast(expr AS sql-type)`. Returns a typed HoneySQL form."
[sql-type :- ms/KeywordOrString
expr :- ::honeysql-expr]
(-> (if (raw-type-name? sql-type)
- [:cast expr ^:allow-raw-sql [:raw (name sql-type)]]
+ [:cast expr [::raw-type-name (name sql-type)]]
[:cast expr (identifier :type-name (name sql-type))])
(with-database-type-info sql-type)))
@@ -705,7 +741,7 @@
(defmethod calculate-interval-honeysql-form :mysql
[_db-type end-form start-form]
- [:timestampdiff ^:allow-raw-sql [:raw "MICROSECOND"] start-form end-form])
+ [::timestampdiff :microsecond start-form end-form])
(defmethod calculate-interval-honeysql-form :h2
[_db-type end-form start-form]
diff --git a/src/metabase/xrays/db.clj b/src/metabase/xrays/db.clj
index 7ca070af3d7..cb37a87f8bc 100644
--- a/src/metabase/xrays/db.clj
+++ b/src/metabase/xrays/db.clj
@@ -58,7 +58,7 @@
[:= :ts.count_non_pks 1]] :list-like?]]
{:from [(warehouse-schema-overlay/table-query)]
:inner-join [[^:allow-subquery {:select [:f.table_id
- [:%count.* "count"]
+ [:%count.* :count]
[[:count [:case [:or [:not= :semantic_type "type/PK"]
[:= :f.semantic_type nil]]
[:inline 1] :else [:inline nil]]]
diff --git a/test/metabase/collections_rest/api_test.clj b/test/metabase/collections_rest/api_test.clj
index d0a9d1f3eee..b87571e37af 100644
--- a/test/metabase/collections_rest/api_test.clj
+++ b/test/metabase/collections_rest/api_test.clj
@@ -1760,16 +1760,16 @@
;; we always place "special" collection types (i.e. "Metabase Analytics") last
(testing "Default sort"
(doseq [app-db [:mysql :h2 :postgres]]
- (is (= [[:authority_level :asc :nulls-last]
- [:type :asc :nulls-first]
+ (is (= [[:authority_level :asc-nulls-last]
+ [:type :asc-nulls-first]
[:%lower.name :asc]
[:id :asc]]
(collections.children/children-sort-clause {:official-collections-first? true} app-db))))))
(deftest ^:parallel children-sort-clause-test-2
(testing "Sorting by last-edited-at"
- (is (= [[:authority_level :asc :nulls-last]
- [:type :asc :nulls-first]
+ (is (= [[:authority_level :asc-nulls-last]
+ [:type :asc-nulls-first]
[:%isnull.last_edit_timestamp]
[:last_edit_timestamp :asc]
[:%lower.name :asc]
@@ -1780,8 +1780,8 @@
(deftest ^:parallel children-sort-clause-test-2b
(testing "Sorting by last-edited-at"
- (is (= [[:authority_level :asc :nulls-last]
- [:type :asc :nulls-first]
+ (is (= [[:authority_level :asc-nulls-last]
+ [:type :asc-nulls-first]
[:last_edit_timestamp :nulls-last]
[:last_edit_timestamp :asc]
[:%lower.name :asc]
@@ -1792,8 +1792,8 @@
(deftest ^:parallel children-sort-clause-test-2c
(testing "Sorting by last-edited-by"
- (is (= [[:authority_level :asc :nulls-last]
- [:type :asc :nulls-first]
+ (is (= [[:authority_level :asc-nulls-last]
+ [:type :asc-nulls-first]
[:last_edit_last_name :nulls-last]
[:last_edit_last_name :asc]
[:last_edit_first_name :nulls-last]
@@ -1806,8 +1806,8 @@
(deftest ^:parallel children-sort-clause-test-2d
(testing "Sorting by last-edited-by"
- (is (= [[:authority_level :asc :nulls-last]
- [:type :asc :nulls-first]
+ (is (= [[:authority_level :asc-nulls-last]
+ [:type :asc-nulls-first]
[:%isnull.last_edit_last_name]
[:last_edit_last_name :asc]
[:%isnull.last_edit_first_name]
@@ -1820,8 +1820,8 @@
(deftest ^:parallel children-sort-clause-test-3
(testing "Sorting by model"
- (is (= [[:authority_level :asc :nulls-last]
- [:type :asc :nulls-first]
+ (is (= [[:authority_level :asc-nulls-last]
+ [:type :asc-nulls-first]
[:model_ranking :asc]
[:%lower.name :asc]
[:id :asc]]
@@ -1831,8 +1831,8 @@
(deftest ^:parallel children-sort-clause-test-3b
(testing "Sorting by model"
- (is (= [[:authority_level :asc :nulls-last]
- [:type :asc :nulls-first]
+ (is (= [[:authority_level :asc-nulls-last]
+ [:type :asc-nulls-first]
[:model_ranking :desc]
[:%lower.name :asc]
[:id :asc]]
@@ -1843,18 +1843,18 @@
(deftest ^:parallel children-sort-clause-description-test
(testing "Sorting by description"
(testing "ascending"
- (is (= [[:authority_level :asc :nulls-last]
- [:type :asc :nulls-first]
- [:%lower.description :asc :nulls-last]
+ (is (= [[:authority_level :asc-nulls-last]
+ [:type :asc-nulls-first]
+ [:%lower.description :asc-nulls-last]
[:%lower.name :asc]
[:id :asc]]
(collections.children/children-sort-clause {:sort-column :description
:sort-direction :asc
:official-collections-first? true} :postgres))))
(testing "descending"
- (is (= [[:authority_level :asc :nulls-last]
- [:type :asc :nulls-first]
- [:%lower.description :desc :nulls-last]
+ (is (= [[:authority_level :asc-nulls-last]
+ [:type :asc-nulls-first]
+ [:%lower.description :desc-nulls-last]
[:%lower.name :asc]
[:id :asc]]
(collections.children/children-sort-clause {:sort-column :description
diff --git a/test/metabase/driver/sql/query_processor_test.clj b/test/metabase/driver/sql/query_processor_test.clj
index d058aaf3f25..c54078190d0 100644
--- a/test/metabase/driver/sql/query_processor_test.clj
+++ b/test/metabase/driver/sql/query_processor_test.clj
@@ -293,10 +293,10 @@
[:week (-> [:dateadd
(h2x/literal "day")
[:inline 1]
- (-> [:cast :created_at [:raw "datetime"]]
+ (-> [:cast :created_at [::h2x/raw-type-name "datetime"]]
(h2x/with-database-type-info "datetime"))]
(h2x/with-database-type-info "datetime"))]
- [:raw "datetime"]]
+ [::h2x/raw-type-name "datetime"]]
(h2x/with-database-type-info "datetime"))]
(h2x/with-database-type-info "datetime"))
(sql.qp/adjust-start-of-week :h2 (fn [x] [:week x]) :created_at))))
diff --git a/test/metabase/funnysql/core_test.clj b/test/metabase/funnysql/core_test.clj
new file mode 100644
index 00000000000..9ce03795ec6
--- /dev/null
+++ b/test/metabase/funnysql/core_test.clj
@@ -0,0 +1,1686 @@
+(ns metabase.funnysql.core-test
+ (:require
+ [clojure.string :as str]
+ [clojure.test :refer [are deftest is testing]]
+ [honey.sql.pg-ops]
+ [metabase.funnysql.core :as funnysql]
+ [metabase.util.honey-sql-2 :as h2x]))
+
+(deftest ^:parallel interpose-fn-test
+ (letfn [(interpose-fn* [xs]
+ (let [result (atom [])]
+ (#'funnysql/interpose-fn
+ xs
+ (fn [x]
+ (swap! result conj x))
+ (fn []
+ (swap! result conj "-")))
+ @result))]
+ (are [xs expected] (= expected
+ (interpose-fn* xs))
+ nil []
+ [] []
+ [:a] [:a]
+ [:a :b] [:a "-" :b]
+ [:a :b :c] [:a "-" :b "-" :c])
+ (testing "anything other than nil or a collection is a bug"
+ (is (thrown? AssertionError (interpose-fn* :a))))))
+
+(deftest ^:parallel sequences-test
+ (testing "Support compiling different types of sequences"
+ (are [xs] (= ["SELECT (1, 2) AS \"ONE\""]
+ (funnysql/format {:select [[xs :one]]} :h2))
+ [1 2]
+ (list 1 2)
+ (lazy-seq [1 2])
+ #{1 2})))
+
+(deftest ^:parallel seq-function-call-test
+ (testing "a function call has to be a vector -- one built as a seq throws instead of compiling to a list of values like
+ `(\"or\", ...)` that the database rejects with a misleading error"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"A function call must be a vector, not a seq"
+ (funnysql/format {:where (cons :or [[:= :a 1] [:= :b 2]])} :postgres))))
+ (testing "a seq of values is still fine"
+ (are [form expected] (= expected
+ (funnysql/format form :postgres))
+ {:where [:in :a (list 1 2)]} ["WHERE \"a\" IN (1, 2)"]
+ {:select (list :a :b), :from [:t]} ["SELECT \"a\", \"b\" FROM \"t\""])))
+
+(deftest ^:parallel equals-test
+ (are [value expected] (= expected
+ (funnysql/format {:where [:= :field value]} :postgres))
+ "x" ["WHERE \"field\" = ?" "x"]
+ nil ["WHERE \"field\" IS NULL"]
+ ;; booleans and numbers can be inlined
+ true ["WHERE \"field\" = true"]
+ 1 ["WHERE \"field\" = 1"]
+ [:= :y nil] ["WHERE \"field\" = (\"y\" IS NULL)"])
+ (is (= ["WHERE (\"x\" IS NULL) = \"y\""]
+ (funnysql/format {:where [:= [:= :x nil] :y]} :postgres))))
+
+(deftest ^:parallel equals-with-more-than-two-args-test
+ (testing "throw if := or :!= have more than two args"
+ (are [op] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"\QWrong number of args to :=/:!=/:<>/:not= (expected 2 args)\E"
+ (funnysql/format [op 1 2 3] :postgres))
+ := :!= :not= :<>)))
+
+(deftest ^:parallel nil-on-the-left-test
+ (testing "`nil` on the LHS has to become `IS [NOT] NULL` too -- `NULL = x` is always NULL, so it matches no rows"
+ (are [clause expected] (= [expected]
+ (funnysql/format {:where clause} :postgres))
+ [:= nil :field] "WHERE \"field\" IS NULL"
+ [:<> nil :field] "WHERE \"field\" IS NOT NULL"
+ [:!= nil :field] "WHERE \"field\" IS NOT NULL"
+ [:not= nil :field] "WHERE \"field\" IS NOT NULL"))
+ (testing "a bare predicate moved to the left of `IS NULL` still gets parenthesized, since `a AND b IS NULL` would
+ otherwise parse as `a AND (b IS NULL)`"
+ (is (= ["WHERE ((\"a\" = 1) AND (\"b\" = 2)) IS NULL"]
+ (funnysql/format {:where [:= nil [:and [:= :a 1] [:= :b 2]]]} :postgres))))
+ (testing "a self-delimiting LHS like a function call does not need the parens"
+ (is (= ["WHERE lower(\"field\") IS NULL"]
+ (funnysql/format {:where [:= nil [:lower :field]]} :postgres))))
+ (testing "`nil` on both sides keeps a literal NULL"
+ (is (= ["WHERE NULL IS NULL"]
+ (funnysql/format {:where [:= nil nil]} :postgres)))
+ (is (= ["WHERE NULL IS NOT NULL"]
+ (funnysql/format {:where [:not= nil nil]} :postgres)))))
+
+(deftest ^:parallel is-null-semantics-test
+ (testing "[:is-not nil true] and [:is nil false] should compile correctly"
+ (are [x expected] (= [expected]
+ (funnysql/format x :postgres))
+ [:is nil true] "NULL IS true"
+ [:is nil false] "NULL IS false"
+ [:is-not nil true] "NULL IS NOT true"
+ [:is-not nil false] "NULL IS NOT false"
+ [:is :x true] "\"x\" IS true"
+ [:is :x false] "\"x\" IS false"
+ [:is-not :x true] "\"x\" IS NOT true"
+ [:is-not :x false] "\"x\" IS NOT false"
+ ;; these seem wacky especially compared to what we do for `:=` and friends with LHS `nil` but this is how Honey
+ ;; SQL handles this situation
+ [:is nil :x] "NULL IS \"x\""
+ [:is-not nil :x] "NULL IS NOT \"x\"")))
+
+(deftest ^:parallel ratio-test
+ (testing "a Ratio is bound as a parameter (set as a double by metabase.app-db.jdbc-protocols) rather than spliced as an exact NUMERIC literal (#9246)"
+ (let [ratio (/ 1 10)]
+ (is (instance? clojure.lang.Ratio ratio))
+ (is (= ["SELECT ? AS \"one_tenth\"" ratio]
+ (funnysql/format {:select [[ratio :one_tenth]]} :postgres))))))
+
+(deftest ^:parallel non-integral-numbers-are-bound-test
+ (testing (str "a non-integral number is bound rather than spliced: Postgres and H2 read a decimal literal back as a "
+ "BigDecimal, and a Float spliced as `0.1` is not the value it holds (#9246)")
+ (are [n] (= ["SELECT ? AS \"x\"" n]
+ (funnysql/format {:select [[n :x]]} :postgres))
+ 0.5
+ -0.5
+ (float 0.1)
+ 0.1M
+ 1.0E+20M
+ (/ 1 2))
+ (testing "including where one is negated"
+ (is (= ["?" -0.5]
+ (funnysql/format [:- 0.5] :postgres))))))
+
+(deftest ^:parallel integers-are-spliced-test
+ (testing "every integer type is spliced, including the ones whose rendering the number regex has to accept"
+ (are [n expected] (= [(str "WHERE \"field\" = " expected)]
+ (funnysql/format {:where [:= :field n]} :postgres))
+ 1 "1"
+ -5 "-5"
+ (int 7) "7"
+ (short 7) "7"
+ (byte -7) "-7"
+ 12345678901234567890N "12345678901234567890"
+ (biginteger -12345678901234567890N) "-12345678901234567890")))
+
+(deftest ^:parallel inline-number-test
+ (testing "`[:inline n]` splices any finite number, the non-integral ones that would otherwise be bound included"
+ (are [n expected] (= [expected]
+ (funnysql/format [:inline n] :postgres))
+ 1 "1"
+ -1 "-1"
+ 0.5 "0.5"
+ -1.5 "-1.5"
+ 1.0E20 "1.0E20"
+ 1.0E-7 "1.0E-7"
+ 1.0E+20M "1.0E+20"
+ -1.25M "-1.25"
+ (/ 1 2) "0.5"
+ (float 2) "2.0"))
+ (testing "a sequence of numbers is spliced element by element"
+ (is (= ["(0.5, -1, 1.0E+20, true)"]
+ (funnysql/format [:inline [0.5 -1 1.0E+20M true]] :postgres)))
+ (is (= ["WHERE \"field\" IN (0.5, 1.5)"]
+ (funnysql/format {:where [:in :field [:inline [0.5 1.5]]]} :postgres)))))
+
+(deftest ^:parallel number-rejects-non-numeric-rendering-test
+ (testing "splicing a Number must fail closed instead of splicing whatever `(str n)` happens to produce"
+ (let [evil (proxy [Number] []
+ (toString [] "1); DROP TABLE users; --")
+ (intValue [] (int 1))
+ (longValue [] (long 1))
+ (floatValue [] (float 1))
+ (doubleValue [] (double 1)))]
+ (testing "a hostile Number implementation's toString is not guaranteed to be numeric SQL syntax"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid number"
+ (funnysql/format [:inline evil] :postgres))))
+ (testing "an unknown Number type isn't an integer, so in a value position it is bound and its toString never reaches the SQL"
+ (is (= ["WHERE \"field\" = ?" evil]
+ (funnysql/format {:where [:= :field evil]} :postgres)))))
+ (testing "Double's non-finite values don't render as valid numeric SQL literals either"
+ (are [n] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid number"
+ (funnysql/format [:inline n] :postgres))
+ Double/NaN
+ Double/POSITIVE_INFINITY
+ Double/NEGATIVE_INFINITY))))
+
+(deftest ^:parallel not-equals-test
+ (are [op expected] (= expected
+ (funnysql/format {:where [op :field "x"]} :postgres))
+ :<> ["WHERE \"field\" <> ?" "x"]
+ :!= ["WHERE \"field\" <> ?" "x"]
+ :not= ["WHERE \"field\" <> ?" "x"])
+ (testing "NOT NULL"
+ (are [op expected] (= expected
+ (funnysql/format {:where [op :field nil]} :postgres))
+ :<> ["WHERE \"field\" IS NOT NULL"]
+ :!= ["WHERE \"field\" IS NOT NULL"]
+ :not= ["WHERE \"field\" IS NOT NULL"]))
+ (is (= ["WHERE (\"x\" IS NOT NULL) = \"y\""]
+ (funnysql/format {:where [:= [:not= :x nil] :y]} :postgres)))
+ (is (= ["WHERE \"x\" = (\"y\" IS NOT NULL)"]
+ (funnysql/format {:where [:= :x [:not= :y nil]]} :postgres))))
+
+(deftest ^:parallel and-or-test
+ (are [op sql] (= [(str "WHERE (\"field\" = ?) " sql " (\"other_field\" = ?)") "x" "y"]
+ (funnysql/format {:where [op [:= :field "x"] [:= :other_field "y"]]} :postgres))
+ :and "AND"
+ :or "OR"))
+
+(deftest ^:parallel comparison-test
+ (are [op sql] (= [(str "WHERE \"field\" " sql " ?") "s"]
+ (funnysql/format {:where [op :field "s"]} :postgres))
+ :< "<"
+ :<= "<="
+ :> ">"
+ :>= ">="))
+
+(deftest ^:parallel in-test
+ (is (= ["WHERE \"table\".\"field\" IN (1, 2, 3)"]
+ (funnysql/format {:where [:in :table.field [1 2 3]]} :postgres)))
+ (is (= ["WHERE \"table\".\"field\" IN (3)"]
+ (funnysql/format {:where [:in :table.field [[:inline 3]]]} :postgres)
+ (funnysql/format {:where [:in :table.field [:inline [3]]]} :postgres)))
+ (testing "multiple sequences (used in combination with `:composite`)"
+ (is (= ["WHERE \"table\".\"field\" IN ((1, 2, 3), (4, 5, 6))"]
+ (funnysql/format {:where [:in :table.field [[1 2 3] [4 5 6]]]} :postgres)))))
+
+(deftest ^:parallel not-in-test
+ (is (= ["WHERE \"table\".\"field\" NOT IN (1, 2)"]
+ (funnysql/format {:where [:not-in :table.field [1 2]]} :postgres))))
+
+(deftest ^:parallel like-test
+ (are [op sql] (= [(str "WHERE \"field\" " sql " ?") "%x%"]
+ (funnysql/format {:where [op :field "%x%"]} :postgres))
+ :like "LIKE"
+ :not-like "NOT LIKE"))
+
+(deftest ^:parallel not-test
+ (is (= ["WHERE NOT (\"field\" = 1)"]
+ (funnysql/format {:where [:not [:= :field 1]]} :postgres))))
+
+(deftest ^:parallel is-test
+ (are [op sql] (= [(str "WHERE \"field\" " sql)]
+ (funnysql/format {:where [op :field nil]} :postgres))
+ :is "IS NULL"
+ :is-not "IS NOT NULL"))
+
+(deftest ^:parallel and-ignore-nils-test
+ (is (= ["SELECT * FROM \"metabase_table\" WHERE (\"schema\" IN (?)) AND (\"db_id\" = 429)" "PUBLIC"]
+ (funnysql/format {:select [:*]
+ :from [:metabase_table]
+ :where [:and
+ [:in :schema ["PUBLIC"]]
+ nil
+ [:= :db_id 429]]}
+ :postgres))))
+
+(deftest ^:parallel between-test
+ (is (= ["WHERE \"field\" BETWEEN 1 AND 10"]
+ (funnysql/format {:where [:between :field 1 10]} :postgres))))
+
+(deftest ^:parallel exists-test
+ (are [op sql] (= [(str "WHERE " sql " (SELECT 1 FROM \"table\")")]
+ (funnysql/format {:where [op ^:allow-subquery {:select [1] :from [:table]}]} :postgres))
+ :exists "EXISTS"
+ :not-exists "NOT EXISTS"))
+
+(deftest ^:parallel exists-unmarked-map-is-not-a-subquery-test
+ (testing "an unmarked map under `:exists`/`:not-exists` is never compiled as a subquery, and since binding it as a
+ `?` could only fail in the database, it throws"
+ (are [op] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"A subquery must be marked \^:allow-subquery"
+ (funnysql/format {:where [op {:from [:table], :select [1]}]} :postgres))
+ :exists
+ :not-exists)))
+
+(deftest ^:parallel infix-operand-parens-test
+ (testing "an infix expression used as the operand of another infix operator is parenthesized, so it means the same
+ thing it does in Honey SQL"
+ (are [form expected] (= expected
+ (funnysql/format form :postgres))
+ [:* [:|| :a :b] 2] ["(\"a\" || \"b\") * 2"]
+ [:is [:or [:= :a 1] [:= :b 2]] false] ["((\"a\" = 1) OR (\"b\" = 2)) IS false"]
+ [:between [:|| :a :b] "a" "z"] ["(\"a\" || \"b\") BETWEEN ? AND ?" "a" "z"]
+ [:in [:= :a :b] [true]] ["(\"a\" = \"b\") IN (true)"]))
+ (testing "including when `h2x/` helpers hide the operator in `::h2x/typed`"
+ (let [x (h2x/with-database-type-info :x "integer")]
+ (is (= ["(\"x\" + 1) * 2"]
+ (funnysql/format (h2x/* (h2x/+ x 1) 2) :postgres))))
+ (is (= ["\"deactivated_at\" < (\"x\" + 1)"]
+ (funnysql/format [:< :deactivated_at (h2x/+ (h2x/with-database-type-info :x "integer") 1)] :postgres))))
+ (testing "but not `:escape`, which is the postfix `ESCAPE` of the `LIKE` pattern rather than an expression of its own"
+ (is (= ["\"x\" LIKE ? ESCAPE '!'" "a!%"]
+ (funnysql/format [:like :x [:escape "a!%" (h2x/literal "!")]] :postgres)))))
+
+(deftest ^:parallel cast-test
+ (is (= ["WHERE CAST(\"field\" AS integer) = 1"]
+ (funnysql/format {:where [:= [:cast :field :integer] 1]} :postgres)))
+ (testing "type validation"
+ (testing "valid types"
+ (are [type-name] (some? (funnysql/format {:where [:= [:cast :field type-name] 1]} :postgres))
+ :integer
+ "integer"
+ "timestamp"
+ (keyword "timestamp(6)")
+ "timestamp(6)"
+ "datetime2"
+ :datetime2))
+ (testing "invalid types"
+ (are [type-name] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid type"
+ (funnysql/format {:where [:= [:cast :field type-name] 1]} :postgres))
+ "X) OR SELECT * FROM another_table; --"
+ (keyword "X) OR SELECT * FROM another_table; --")
+ :2
+ "2"))))
+
+(deftest ^:parallel cast-type-name-dashes-test
+ (testing "a keyword type name spells its spaces as dashes"
+ (are [type-name expected] (= [(str "CAST(\"x\" AS " expected ")")]
+ (funnysql/format [:cast :x type-name] :postgres))
+ :timestamp-with-time-zone "timestamp with time zone"
+ :double-precision "double precision"
+ [:character-varying 32] "character varying(32)"))
+ (testing "a string type name is used exactly as given, so a dash inside a quoted argument survives"
+ (are [type-name] (= [(str "CAST(\"x\" AS " type-name ")")]
+ (funnysql/format [:cast :x type-name] :postgres)
+ (funnysql/format (h2x/cast type-name :x) :postgres))
+ "DateTime64(3, 'America/Port-au-Prince')"
+ "Nullable(DateTime64(3, 'America/Port-au-Prince'))")))
+
+(deftest ^:parallel case-test
+ (is (= ["WHERE \"field\" = CASE WHEN \"other\" > 1 THEN ? ELSE ? END" "big" "small"]
+ (funnysql/format {:where [:= :field [:case [:> :other 1] "big" :else "small"]]} :postgres))))
+
+(deftest ^:parallel lower-upper-test
+ (are [op sql] (= [(str "WHERE \"field\" = " sql "(?)") "X"]
+ (funnysql/format {:where [:= :field [op "X"]]} :postgres))
+ :lower "lower"
+ :upper "upper"))
+
+(deftest ^:parallel concat-test
+ (is (= ["WHERE \"field\" = concat(\"a\", \"b\")"]
+ (funnysql/format {:where [:= :field [:concat :a :b]]} :postgres))))
+
+(deftest ^:parallel coalesce-test
+ (is (= ["WHERE \"field\" = coalesce(\"a\", 1)"]
+ (funnysql/format {:where [:= :field [:coalesce :a 1]]} :postgres))))
+
+(deftest ^:parallel aggregate-function-test
+ (are [op sql] (= [(str "WHERE \"field\" = " sql "(\"other\")")]
+ (funnysql/format {:where [:= :field [op :other]]} :postgres))
+ :count "count"
+ :sum "sum"
+ :avg "avg"
+ :min "min"
+ :max "max"
+ :distinct "distinct"))
+
+(deftest ^:parallel select-distinct-test
+ (is (= ["SELECT DISTINCT \"field\", \"other_field\""]
+ (funnysql/format {:select-distinct [:field :other_field]} :postgres))))
+
+(deftest ^:parallel group-by-test
+ (is (= ["GROUP BY \"field\", \"other_field\""]
+ (funnysql/format {:group-by [:field :other_field]} :postgres))))
+
+(deftest ^:parallel having-test
+ (is (= ["HAVING count(\"field\") > 1"]
+ (funnysql/format {:having [:> [:count :field] 1]} :postgres))))
+
+(deftest ^:parallel order-by-test
+ (are [order-by sql] (= [(str "ORDER BY " sql)]
+ (funnysql/format {:order-by order-by} :postgres))
+ [:field] "\"field\" ASC"
+ [[:field]] "\"field\" ASC"
+ [[:field :asc]] "\"field\" ASC"
+ [[:field] [:other_field :desc]] "\"field\" ASC, \"other_field\" DESC"
+ [[:field :desc]] "\"field\" DESC"
+ [[:field] [:other_field :desc]] "\"field\" ASC, \"other_field\" DESC"
+ [[:field :asc] [:other_field :desc]] "\"field\" ASC, \"other_field\" DESC"))
+
+(deftest ^:parallel order-by-invalid-direction-test
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid order by direction"
+ (funnysql/format {:order-by [[:field :sideways]]} :postgres))))
+
+(deftest ^:parallel order-by-nulls-ordering-test
+ (are [k expected] (= [(str "SELECT \"a\" FROM \"t\" ORDER BY \"a\" " expected)]
+ (funnysql/format {:select [:a], :from [:t], :order-by [[:a k]]} :postgres))
+ :nulls-last "NULLS LAST"
+ :nulls-first "NULLS FIRST"
+ :asc-nulls-last "ASC NULLS LAST"
+ :desc-nulls-last "DESC NULLS LAST"
+ :asc-nulls-first "ASC NULLS FIRST"
+ :desc-nulls-first "DESC NULLS FIRST")
+ (testing (str "Error on Honey SQL 1-style [<field> <direction> <nulls-behavior>] (Honey SQL 2 just silently ignores"
+ " this 😢, but we can be nice)")
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"\Q`:order-by` only supports [<expression> <direction>], but got more than 2 args\E"
+ (funnysql/format {:select [:a], :from [:t], :order-by [[:a :asc :nulls-first]]} :postgres)))))
+
+(deftest ^:parallel order-by-nulls-ordering-mysql-test
+ (testing (str "MySQL and MariaDB don't support NULLS FIRST/NULLS LAST. They sort NULL first ascending and last "
+ "descending, so sort on `IS NULL` first only when that isn't what we want")
+ (are [k expected] (= [(str "SELECT `a` FROM `t` ORDER BY " expected)]
+ (funnysql/format {:select [:a], :from [:t], :order-by [[:a k]]} :mysql))
+ :asc "`a` ASC"
+ :desc "`a` DESC"
+ :nulls-last "(`a`) IS NULL ASC, `a` ASC"
+ :nulls-first "`a` ASC"
+ :asc-nulls-last "(`a`) IS NULL ASC, `a` ASC"
+ :desc-nulls-last "`a` DESC"
+ :asc-nulls-first "`a` ASC"
+ :desc-nulls-first "(`a`) IS NULL DESC, `a` DESC"))
+ (testing "the expression is emitted twice, so its parameters are bound twice"
+ (is (= ["SELECT `a` FROM `t` ORDER BY (coalesce(`a`, ?)) IS NULL DESC, coalesce(`a`, ?) DESC" "x" "x"]
+ (funnysql/format {:select [:a], :from [:t], :order-by [[[:coalesce :a "x"] :desc-nulls-first]]} :mysql)))))
+
+(deftest ^:parallel limit-test
+ (is (= ["LIMIT 10"]
+ (funnysql/format {:limit 10} :postgres)))
+ (testing "should validate limit"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid limit"
+ (funnysql/format {:limit -10} :postgres)))))
+
+(deftest ^:parallel offset-test
+ (is (= ["OFFSET 5"]
+ (funnysql/format {:offset 5} :postgres)))
+ (testing "should validate offset"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid offset"
+ (funnysql/format {:offset "s"} :postgres)))))
+
+(deftest ^:parallel limit-offset-test
+ (is (= ["LIMIT 10 OFFSET 5"]
+ (funnysql/format {:limit 10, :offset 5} :postgres))))
+
+(deftest ^:parallel join-test
+ (are [clause sql] (= [(str sql " \"other\" ON \"a\".\"id\" = \"other\".\"a_id\"")]
+ (funnysql/format {clause [:other [:= :a.id :other.a_id]]} :postgres))
+ :join "JOIN"
+ :left-join "LEFT JOIN"
+ :right-join "RIGHT JOIN"
+ :inner-join "INNER JOIN"))
+
+(deftest ^:parallel join-with-alias-test
+ (is (= ["JOIN \"dashboard\" AS \"d\" ON \"d\".\"id\" = \"dc\".\"dashboard_id\""]
+ (funnysql/format {:join [[:dashboard :d] [:= :d.id :dc.dashboard_id]]} :postgres))))
+
+(deftest ^:parallel multiple-joins-test
+ (is (= ["JOIN \"a\" ON \"a\".\"id\" = \"x\".\"a_id\" JOIN \"b\" ON \"b\".\"id\" = \"x\".\"b_id\""]
+ (funnysql/format {:join [:a [:= :a.id :x.a_id]
+ :b [:= :b.id :x.b_id]]} :postgres))))
+
+(deftest ^:parallel join-subquery-test
+ (testing "a subquery in a `:join` needs to be wrapped in parens, just like one in `:from`"
+ (is (= [(str "SELECT \"f\".\"table_id\""
+ " FROM \"metabase_field\" AS \"f\""
+ " JOIN (SELECT \"id\", \"db_id\" FROM \"metabase_table\") AS \"t\" ON \"t\".\"id\" = \"f\".\"table_id\"")]
+ (funnysql/format {:select [:f.table_id]
+ :from [[:metabase_field :f]]
+ :join [[^:allow-subquery {:select [:id :db_id]
+ :from [:metabase_table]} :t]
+ [:= :t.id :f.table_id]]}
+ :postgres)))))
+
+(deftest ^:parallel join-missing-condition-test
+ (testing "a join without a condition throws instead of compiling to `ON NULL`, which silently returns no rows"
+ (are [joins] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Every join needs a condition"
+ (funnysql/format {:select [:*] :from [:a] :join joins} :postgres))
+ [:b]
+ [:b [:= :a.id :b.a_id] :c]))
+ (testing "likewise a `nil` condition, which is what a `(when ...)` around one gives you"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"A join condition cannot be nil"
+ (funnysql/format {:select [:*] :from [:a] :join [:b nil]} :postgres))))
+ (testing "`false` is a condition like any other"
+ (is (= ["SELECT * FROM \"a\" JOIN \"b\" ON false"]
+ (funnysql/format {:select [:*] :from [:a] :join [:b false]} :postgres)))))
+
+(deftest ^:parallel from-and-join-unmarked-map-test
+ (testing "an unmarked map in `:from` or a join is never compiled as a subquery, and since binding it as a `?` could
+ only fail in the database, it throws"
+ (are [form] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"A subquery must be marked \^:allow-subquery"
+ (funnysql/format form :postgres))
+ {:select [:*] :from [{:select [:id] :from [:t]}]}
+ {:select [:*] :from [[{:select [:id] :from [:t]} :s]]}
+ {:select [:*] :from [:a] :join [[{:select [:id] :from [:t]} :s] [:= :s.id :a.id]]})))
+
+(deftest ^:parallel empty-join-test
+ (testing "Handle `nil`/empty joins; we still spit out an extra space because of the way things work but that's ok I guess"
+ (is (= ["SELECT 1 AS \"v\" "]
+ (funnysql/format {:select [[1 :v]] :left-join nil} :postgres)))))
+
+(deftest ^:parallel with-test
+ (is (= ["WITH \"cte\" AS (SELECT \"id\" FROM \"table\"), \"cte2\" AS (SELECT * FROM \"cte\") SELECT \"id\" FROM \"cte\""]
+ (funnysql/format {:with [[:cte ^:allow-subquery {:select [:id] :from [:table]}]
+ [:cte2 ^:allow-subquery {:select [:*] :from [:cte]}]]
+ :select [:id]
+ :from [:cte]} :postgres))))
+
+(deftest ^:parallel with-not-marked-allow-subquery-test
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"A subquery must be marked \^:allow-subquery"
+ (funnysql/format {:with [[:cte {:select [:id] :from [:table]}]
+ [:cte2 {:select [:*] :from [:cte]}]]
+ :select [:id]
+ :from [:cte]} :postgres))))
+
+(deftest ^:parallel with-recursive-test
+ (is (= ["WITH RECURSIVE \"cte\" AS (SELECT \"id\" FROM \"table\") SELECT \"id\" FROM \"cte\""]
+ (funnysql/format {:with-recursive [[:cte ^:allow-subquery {:select [:id] :from [:table]}]]
+ :select [:id]
+ :from [:cte]} :postgres))))
+
+(deftest ^:parallel with-recursive-not-marked-allow-subquery-test
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"A subquery must be marked \^:allow-subquery"
+ (funnysql/format {:with-recursive [[:cte {:select [:id] :from [:table]}]]
+ :select [:id]
+ :from [:cte]} :postgres))))
+
+(deftest ^:parallel with-recursive-columns-test
+ (is (= [(str "WITH RECURSIVE \"parents\" (\"id\", \"name\") AS (SELECT \"id\", \"name\" FROM \"metabase_field\")"
+ " SELECT \"id\" FROM \"parents\"")]
+ (funnysql/format {:with-recursive [[[:parents {:columns [:id :name]}]
+ ^:allow-subquery {:select [:id :name]
+ :from [:metabase_field]}]]
+ :select [:id]
+ :from [:parents]}
+ :postgres))))
+
+(deftest ^:parallel non-vector-identifier-with-options-test
+ (testing "as everywhere else, only a vector is `[<identifier> <options>]` -- any other sequence is not an identifier"
+ (are [form] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Expected an identifier"
+ (funnysql/format form :postgres))
+ {:with [[(list :cte {:columns [:a]}) ^:allow-subquery {:select [:a] :from [:t]}]]
+ :select [:*]
+ :from [:cte]}
+ {:insert-into (list :t [:a])
+ :values [[1]]})))
+
+(deftest ^:parallel with-materialized-test
+ (is (= ["WITH \"cte\" AS MATERIALIZED (SELECT \"id\" FROM \"t\") SELECT \"id\" FROM \"cte\""]
+ (funnysql/format {:with [[:cte ^:allow-subquery {:select [:id] :from [:t]} :materialized]]
+ :select [:id]
+ :from [:cte]}
+ :postgres))))
+
+(deftest ^:parallel union-test
+ (is (= ["SELECT \"id\" FROM \"a\" UNION SELECT \"id\" FROM \"b\""]
+ (funnysql/format {:union [^:allow-subquery {:select [:id] :from [:a]}
+ ^:allow-subquery {:select [:id] :from [:b]}]}
+ :postgres))))
+
+(deftest ^:parallel union-not-marked-allow-subquery-test
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"A subquery must be marked \^:allow-subquery"
+ (funnysql/format {:union [{:select [:id] :from [:a]}
+ {:select [:id] :from [:b]}]}
+ :postgres))))
+
+(deftest ^:parallel union-all-test
+ (is (= ["SELECT \"id\" FROM \"a\" UNION ALL SELECT \"id\" FROM \"b\""]
+ (funnysql/format {:union-all [^:allow-subquery {:select [:id] :from [:a]}
+ ^:allow-subquery {:select [:id] :from [:b]}]} :postgres))))
+
+(deftest ^:parallel union-all-not-marked-allow-subquery-test
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"A subquery must be marked \^:allow-subquery"
+ (funnysql/format {:union-all [{:select [:id] :from [:a]}
+ {:select [:id] :from [:b]}]}
+ :postgres))))
+
+(deftest ^:parallel insert-into-values-test
+ (are [table] (= ["INSERT INTO \"my_table\" (\"a\", \"b\") VALUES (?, ?)" "x" "y"]
+ (funnysql/format {:insert-into table
+ :values [{:a "x" :b "y"}]}
+ :postgres))
+ :my_table
+ [:my_table]
+ [[:my_table]]))
+
+(deftest ^:parallel insert-into-multiple-rows-test
+ (is (= ["INSERT INTO \"my_table\" (\"a\", \"b\", \"c\") VALUES (?, ?, NULL), (?, ?, ?)" "a1" "b1" "a2" "b2" "c2"]
+ (funnysql/format {:insert-into :my_table
+ :values [{:a "a1", :b "b1"} {:a "a2", :b "b2", :c "c2"}]}
+ :postgres))))
+
+(deftest ^:parallel insert-into-columns-test
+ (testing "`:columns` specified as separate top-level key"
+ (is (= ["INSERT INTO \"permissions\" (\"object\", \"group_id\") VALUES (?, 1)" "/db/1/"]
+ (funnysql/format {:insert-into :permissions
+ :columns [:object :group_id]
+ :values [["/db/1/" 1]]}
+ :postgres)))))
+
+(deftest ^:parallel insert-from-table-with-columns-test
+ (testing "columns specified in `:insert-into` itself"
+ (is (= ["INSERT INTO \"my_table\" (\"a\", \"b\", \"c\") VALUES (1, 2, 3), (4, 5, 6)"]
+ (funnysql/format {:insert-into [:my_table [:a :b :c]]
+ :values [[1 2 3] [4 5 6]]}
+ :postgres)))))
+
+(deftest ^:parallel insert-from-select-test
+ (testing "INSERT ... SELECT must not silently drop the column list and the SELECT"
+ (is (= [(str "INSERT INTO \"permissions_group_membership\" (\"group_id\", \"user_id\", \"is_group_manager\")"
+ " SELECT \"g\".\"id\", \"u\".\"id\" FROM \"permissions_group\" AS \"g\""
+ " JOIN \"core_user\" AS \"u\" ON \"u\".\"id\" = 1")]
+ (funnysql/format {:insert-into
+ [[:permissions_group_membership [:group_id :user_id :is_group_manager]]
+ ^:allow-subquery
+ {:select [:g.id :u.id]
+ :from [[:permissions_group :g]]
+ :join [[:core_user :u] [:= :u.id [:inline 1]]]}]}
+ :postgres)))))
+
+(deftest ^:parallel insert-from-select-not-marked-allow-subquery-test
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"A subquery must be marked \^:allow-subquery"
+ (funnysql/format {:insert-into
+ [[:permissions_group_membership [:group_id :user_id :is_group_manager]]
+ {:select [:g.id :u.id]
+ :from [[:permissions_group :g]]
+ :join [[:core_user :u] [:= :u.id [:inline 1]]]}]}
+ :postgres))))
+
+(deftest ^:parallel degenerate-insert-test
+ (testing "an INSERT with nothing to insert must fail closed rather than emit invalid SQL"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #":values cannot have empty or nil rows"
+ (funnysql/format {:insert-into :permissions, :values []} :postgres)))))
+
+(deftest ^:parallel update-with-table-alias-test
+ (is (= ["UPDATE \"table\" \"t\" SET \"t\".\"field\" = 1"]
+ (funnysql/format {:update [:table :t], :set {:t.field 1}} :postgres))))
+
+(deftest ^:parallel on-conflict-do-update-set-test
+ (is (= ["INSERT INTO \"my_table\" (\"a\", \"b\") VALUES (?, ?) ON CONFLICT (\"a\") DO UPDATE SET \"b\" = ?, \"c\" = ?" "x" "y" "z" "a"]
+ (funnysql/format {:insert-into :my_table
+ :values [{:a "x" :b "y"}]
+ :on-conflict [:a]
+ :do-update-set {:b "z", :c "a"}} :postgres))))
+
+(deftest ^:parallel update-set-test
+ (is (= ["UPDATE \"persisted_info\" SET \"state\" = ?, \"x\" = ? WHERE \"id\" = 1" "deletable" "y"]
+ (funnysql/format {:update [:persisted_info]
+ :set {:state "deletable", :x "y"}
+ :where [:= :id 1]} :postgres))))
+
+(deftest ^:parallel update-set-null-test
+ (testing "UPDATE <table> SET <field> = NULL should compile correctly"
+ (is (= ["UPDATE `setting` SET `value` = NULL WHERE `key` = ?" "query-caching-ttl-ratio"]
+ (funnysql/format {:update [:setting]
+ :set {:value nil}
+ :where [:= :key "query-caching-ttl-ratio"]}
+ :mysql)))))
+
+(deftest ^:parallel delete-from-test
+ (is (= ["DELETE FROM \"card\" WHERE \"database_id\" = 1"]
+ (funnysql/format {:delete-from :card
+ :where [:= :database_id 1]} :postgres))))
+
+(deftest ^:parallel delete-from-with-alias-test
+ (is (= ["DELETE FROM \"card\" \"c\" WHERE \"c\".\"database_id\" = 1"]
+ (funnysql/format {:delete-from [:card :c]
+ :where [:= :c/database_id 1]} :postgres))))
+
+(deftest ^:parallel returning-test
+ (is (= ["DELETE FROM \"card\" WHERE \"database_id\" = 1 RETURNING \"id\", \"x\""]
+ (funnysql/format {:delete-from :card
+ :where [:= :database_id 1]
+ :returning [:id :x]} :postgres))))
+
+(deftest ^:parallel expected-identifier-test
+ (testing "table/column-name positions must reject a value that isn't an identifier (a keyword or `h2x/identifier`
+ form), instead of silently falling through to `object!`'s `?`-parameter handling and producing a
+ confusing runtime error from the database instead of a clear one from the compiler"
+ (testing "insert-into"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Expected an identifier"
+ (funnysql/format {:insert-into "my_table"
+ :values [{:a "x"}]} :postgres))))
+ (testing "update"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Expected an identifier"
+ (funnysql/format {:update ["persisted_info"]
+ :set {:state "deletable"}} :postgres))))
+ (testing "delete-from"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Expected an identifier"
+ (funnysql/format {:delete-from 1}
+ :postgres))))
+ (testing "values columns"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Expected an identifier"
+ (funnysql/format {:insert-into :my_table
+ :values [{"a" "x"}]} :postgres))))
+ (testing "set"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Expected an identifier"
+ (funnysql/format {:update [:persisted_info]
+ :set {"state" "deletable"}} :postgres))))
+ (testing "do-update-set"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Expected an identifier"
+ (funnysql/format {:insert-into :my_table
+ :values [{:a "x"}]
+ :on-conflict [:a]
+ :do-update-set {"b" "z"}} :postgres))))
+ (testing "on-conflict"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Expected an identifier"
+ (funnysql/format {:insert-into :my_table
+ :values [{:a "x"}]
+ :on-conflict ["a"]
+ :do-update-set {:b "z"}} :postgres))))
+ (testing "with (CTE name)"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Expected an identifier"
+ (funnysql/format {:with [["cte" {:select [:id] :from [:table]}]]
+ :select [:id]
+ :from [:cte]} :postgres))))))
+
+(deftest ^:parallel for-update-test
+ (is (= ["SELECT \"id\" FROM \"revision\" WHERE \"model\" = ? FOR UPDATE" "Card"]
+ (funnysql/format {:select [:id]
+ :from [:revision]
+ :where [:= :model "Card"]
+ :for :update} :postgres))))
+
+(deftest ^:parallel for-lock-options-test
+ (are [engine expected] (= expected
+ (funnysql/format {:select [:id] :from [:t] :for [:update :skip-locked]} engine))
+ :postgres ["SELECT \"id\" FROM \"t\" FOR UPDATE SKIP LOCKED"]
+ :mysql ["SELECT `id` FROM `t` FOR UPDATE SKIP LOCKED"]))
+
+(deftest ^:parallel for-throws-on-unknown-options-test
+ (testing "other `:for` options compile or throw; they are never dropped"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"\QError compiling Honey SQL: No matching clause: :nowait\E"
+ (funnysql/format {:select [:id], :from [:t], :for [:update :nowait]} :postgres)))))
+
+(deftest ^:parallel percent-keyword-niladic-function-test
+ (are [k sql] (= [(str "WHERE \"field\" = " sql)]
+ (funnysql/format {:where [:= :field k]} :postgres))
+ :%now "now()"
+ :%current_schema "current_schema()"
+ :%current_database "current_database()"
+ :%database "database()"))
+
+(deftest ^:parallel percent-keyword-function-with-arg-test
+ (are [k sql] (= [(str "WHERE \"field\" = " sql)]
+ (funnysql/format {:where [:= :field k]} :postgres))
+ :%count.* "count(*)"
+ :%count.id "count(\"id\")"
+ :%lower.email "lower(\"email\")"
+ :%lower.name "lower(\"name\")"
+ :%max.id "max(\"id\")"
+ :%max.started_at "max(\"started_at\")"
+ :%min.date_joined "min(\"date_joined\")"
+ :%min.executor_id "min(\"executor_id\")"
+ :%avg.running_time "avg(\"running_time\")"
+ :%sum.total_tokens "sum(\"total_tokens\")"
+ :%isnull.last_edit_timestamp "isnull(\"last_edit_timestamp\")"))
+
+(deftest ^:parallel percent-keyword-with-qualified-arg-test
+ (testing "the `:%fn.arg` shorthand accepts a `/`-qualified argument, as Honey SQL does -- the keyword is a qualified
+ keyword whose namespace starts with `%`, which must not be mistaken for an identifier"
+ (are [k sql] (= [(str "WHERE " sql " = 1")]
+ (funnysql/format {:where [:= k 1]} :postgres))
+ :%lower.metabase_field/name "lower(\"metabase_field\".\"name\")"
+ :%lower.name "lower(\"name\")"
+ :%count.* "count(*)"
+ :%now "now()"
+ ;; a `.` separates arguments, so this is a two-argument call
+ :%coalesce.a.b "coalesce(\"a\", \"b\")")))
+
+(deftest ^:parallel percent-keyword-rejects-unknown-function-test
+ (testing "the `:%function` shorthand must not let an arbitrary, attacker-derived function name reach the SQL --
+ an unrecognized name must be rejected, not passed through raw"
+ (are [k] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Function :.* is not currently supported"
+ (funnysql/format {:where [:= :field k]} :postgres))
+ (keyword "%'; DROP TABLE users; --")
+ (keyword "%count); DROP TABLE users; --"))))
+
+(deftest ^:parallel e2e-test
+ (is (= ["SELECT \"X\", \"Y\" AS \"ALIAS\" FROM \"TABLE\" WHERE (\"FIELD\" = 100) AND (\"FIELD\" < ?) AND (\"TABLE\".\"FIELD\" IN (1, 2, 3))"
+ "s"]
+ (funnysql/format {:select [:x [:y :alias]]
+ :from [[:table]]
+ :where [:and
+ [:= :field 100]
+ [:< :field "s"]
+ [:in :table.field [1 2 3]]]}
+ :h2))))
+
+(deftest ^:parallel star-keyword-test
+ (are [k expected] (= [expected]
+ (funnysql/format {:select [k]} :postgres))
+ :* "SELECT *"
+ :table.* "SELECT \"table\".*"
+ :table/* "SELECT \"table\".*"))
+
+(deftest ^:parallel h2x-identifier-test
+ (is (= ["WHERE \"a\".\"b\" = 1"]
+ (funnysql/format {:where [:= (h2x/identifier :field "a" "b") 1]} :postgres)))
+ (testing "should strictly validate identifier parts; the app DB doesn't use crazy column names"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid identifier"
+ (funnysql/format {:where [:= (h2x/identifier :field "a" "b\" UNION ALL another_table; --") 1]} :postgres)))))
+
+(deftest ^:parallel h2x-literal-test
+ (are [s expected] (= [expected]
+ (funnysql/format {:where [:= :field (h2x/literal s)]} :postgres))
+ "foo"
+ "WHERE \"field\" = 'foo'"
+
+ ;; should escape single quotes
+ "foo' OR 1 = 1; --"
+ "WHERE \"field\" = 'foo'' OR 1 = 1; --'"))
+
+(deftest ^:parallel h2x-literal-escaping-on-every-engine-test
+ (testing "single quotes are doubled on every engine, whether the literal started out as a string or a keyword"
+ (are [engine s expected] (= [expected]
+ (funnysql/format [::h2x/literal s] engine))
+ :postgres "it's" "'it''s'"
+ :h2 "it's" "'it''s'"
+ :mysql "it's" "'it''s'"
+ :h2 "foo' OR 1 = 1; --" "'foo'' OR 1 = 1; --'"
+ :mysql "foo' OR 1 = 1; --" "'foo'' OR 1 = 1; --'"
+ ;; `h2x/literal` takes a keyword too, and keeps its namespace
+ :postgres (keyword "it's") "'it''s'"
+ :h2 (keyword "it's") "'it''s'"
+ :mysql (keyword "it's") "'it''s'"
+ :postgres :provider/password "'provider/password'"
+ :h2 (keyword "a'b" "c'd") "'a''b/c''d'"))
+ (testing "`h2x/literal` builds the same form"
+ (are [engine] (= ["'it''s'"]
+ (funnysql/format (h2x/literal (keyword "it's")) engine))
+ :postgres :h2 :mysql)))
+
+(deftest ^:parallel h2x-literal-backslash-injection-test
+ (testing "on MySQL, backslashes must be escaped too, not just quotes"
+ (testing "MySQL reads `\\'` inside a string literal as an escaped quote, not the end of the string (unless
+ NO_BACKSLASH_ESCAPES is set) -- a trailing backslash right before the closing quote lets the
+ doubled quote that follows become the *real* terminator, and everything after it becomes raw SQL
+ instead of part of the literal"
+ (is (= ["WHERE `field` = 'foo\\\\'' OR 1=1; --'"]
+ (funnysql/format {:where [:= :field (h2x/literal "foo\\' OR 1=1; --")]} :mysql))))))
+
+(deftest ^:parallel h2x-literal-does-not-mangle-backslash-on-ansi-engines-test
+ (testing "on Postgres and H2, a backslash is an ordinary character inside a plain '...' literal (as long as
+ standard_conforming_strings is on, the default) -- doubling it there would corrupt a value that
+ legitimately contains one, instead of protecting anything, since there's no backslash/quote
+ interaction to guard against on these engines"
+ (are [engine expected] (= [expected]
+ (funnysql/format {:where [:= :field (h2x/literal "C:\\temp")]} engine))
+ :postgres "WHERE \"field\" = 'C:\\temp'"
+ ;; h2 uppercases identifiers, hence "FIELD" not "field"
+ :h2 "WHERE \"FIELD\" = 'C:\\temp'")))
+
+(deftest ^:parallel h2x-extract-test
+ (is (= ["WHERE \"field\" = extract(epoch FROM \"created_at\")"]
+ (funnysql/format {:where [:= :field (h2x/extract :epoch :created_at)]} :postgres)))
+ (testing "should validate unit"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid unit"
+ (funnysql/format {:where [:= :field [::h2x/extract "epoch FROM whatever) OR 1 = 1; --" :created_at]]} :postgres)))))
+
+(deftest ^:parallel h2x-distinct-count-test
+ (is (= ["WHERE \"field\" = count(DISTINCT \"id\")"]
+ (funnysql/format {:where [:= :field [::h2x/distinct-count :id]]} :postgres))))
+
+(deftest ^:parallel h2x-percentile-cont-test
+ (is (= ["WHERE \"field\" = percentile_cont(0.9) WITHIN GROUP (ORDER BY \"x\")"]
+ (funnysql/format {:where [:= :field [::h2x/percentile-cont :x 0.9]]} :postgres)))
+ (testing "should work with clojure.lang.Ratio"
+ (let [ratio (/ 9 10)]
+ (is (instance? clojure.lang.Ratio ratio))
+ (is (= ["WHERE \"field\" = percentile_cont(0.9) WITHIN GROUP (ORDER BY \"x\")"]
+ (funnysql/format {:where [:= :field [::h2x/percentile-cont :x ratio]]} :postgres))))))
+
+(deftest ^:parallel h2x-collate-test
+ (is (= ["WHERE \"field\" = \"x\" COLLATE utf8mb4_unicode_ci"]
+ (funnysql/format {:where [:= :field [::h2x/collate :x :utf8mb4_unicode_ci]]} :postgres)))
+ (testing "should validate collation"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid collation"
+ (funnysql/format {:where [:= :field [::h2x/collate :x "utf8mb4_unicode_ci UNION ALL SELECT * FROM sensitive_table; --"]]} :postgres)))))
+
+(deftest ^:parallel h2x-at-time-zone-test
+ (are [zone] (= [(format "WHERE \"field\" = (\"x\" AT TIME ZONE '%s')" zone)]
+ (funnysql/format {:where [:= :field (h2x/at-time-zone :x zone)]} :postgres))
+ "UTC"
+ "America/New_York"
+ "Etc/GMT+5"
+ "America/Indiana/Indianapolis"
+ "+05:00")
+ (testing "Invalid time zone"
+ (are [zone] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid time zone"
+ (funnysql/format {:where [:= :field (h2x/at-time-zone :x zone)]} :postgres))
+ "UTC') OR 1 = 1; --"
+ ;; well-formed, but not a real zone
+ "America/Atlantis")))
+
+(deftest ^:parallel h2x-typed-test
+ (is (= ["WHERE \"field\" = \"x\""]
+ (funnysql/format {:where [:= :field (h2x/with-type-info :x {:database-type "date"})]} :postgres))))
+
+(deftest ^:parallel h2x-postgres-interval-test
+ (is (= ["WHERE \"field\" = INTERVAL '2 day'"]
+ (funnysql/format {:where [:= :field [::h2x/postgres-interval 2 :day]]} :postgres)))
+ (testing "Should validate amount"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid amount"
+ (funnysql/format {:where [:= :field [::h2x/postgres-interval "2" :day]]} :postgres))))
+ (testing "Should validate unit"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid unit"
+ (funnysql/format {:where [:= :field [::h2x/postgres-interval 2 :raw]]} :postgres)))))
+
+(deftest ^:parallel h2x-interval-non-integral-amount-test
+ (testing (str "a non-integral amount is spliced too: bound, its `?` would land inside the quoted Postgres literal, "
+ "where JDBC ignores it")
+ (are [form engine expected] (= [expected]
+ (funnysql/format form engine))
+ [::h2x/postgres-interval (/ 1 2) :day] :postgres "INTERVAL '0.5 day'"
+ [::h2x/postgres-interval 0.5 :day] :postgres "INTERVAL '0.5 day'"
+ [::h2x/postgres-interval 1.5M :hour] :postgres "INTERVAL '1.5 hour'"
+ [::h2x/postgres-interval -2 :day] :postgres "INTERVAL '-2 day'"
+ [::h2x/mysql-interval (/ 1 2) :second] :mysql "INTERVAL 0.5 second"
+ [::h2x/mysql-interval 0.25 :second] :mysql "INTERVAL 0.25 second"))
+ (testing "including the forms `h2x/add-interval-honeysql-form` builds"
+ (is (= ["date_add(`x`, INTERVAL 0.5 second)"]
+ (funnysql/format (h2x/add-interval-honeysql-form :mysql :x 500 :millisecond) :mysql)))))
+
+(deftest ^:parallel h2x-mysql-interval-test
+ (is (= ["WHERE `field` = INTERVAL 2 day"]
+ (funnysql/format {:where [:= :field [::h2x/mysql-interval 2 :day]]} :mysql)))
+ (testing "Should validate amount"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid amount"
+ (funnysql/format {:where [:= :field [::h2x/mysql-interval "2" :day]]} :mysql))))
+ (testing "Should validate unit"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid unit"
+ (funnysql/format {:where [:= :field [::h2x/mysql-interval 2 :raw]]} :postgres)))))
+
+(deftest ^:parallel validate-identifier-valid-test
+ (testing "valid identifiers"
+ (are [identifier engine expected] (= [(str "SELECT " expected)]
+ (funnysql/format {:select [identifier]} engine))
+ :field :postgres "\"field\""
+ :field_x :postgres "\"field_x\""
+ :field-y :postgres "\"field-y\""
+ :_field :postgres "\"_field\""
+ :field? :postgres "\"field?\""
+ :t.field :postgres "\"t\".\"field\""
+ :t/field :postgres "\"t\".\"field\""
+ :s.t.field :postgres "\"s\".\"t\".\"field\""
+ :field :h2 "\"FIELD\""
+ :t.field :h2 "\"T\".\"FIELD\""
+ :field :mysql "`field`"
+ :t.field :mysql "`t`.`field`")))
+
+(deftest ^:parallel validate-identifier-invalid-test
+ (testing "invalid identifiers"
+ (are [identifier] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid identifier"
+ (funnysql/format {:select [identifier]} :postgres))
+ :2field
+ (keyword "field()")
+ (keyword "field;")
+ (keyword "\" OR 1 = 1; --")))
+ (testing "invalid identifiers are rejected on every engine, whichever quote character it uses"
+ (are [engine identifier] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid identifier"
+ (funnysql/format {:select [identifier]} engine))
+ :h2 (keyword "\" OR 1 = 1; --")
+ :h2 (keyword "field;")
+ :mysql (keyword "` OR 1 = 1; --")
+ :mysql (keyword "field;")
+ :mysql (keyword "t.` OR 1 = 1; --"))))
+
+(deftest ^:parallel validate-identifier-outside-of-select-test
+ (testing "invalid identifiers (and aliases) are rejected in every identifier position, not just `:select`"
+ (are [form] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid (identifier|alias)"
+ (funnysql/format form :postgres))
+ {:select [:*], :from [(keyword "t; --")]}
+ {:select [:*], :from [[:t (keyword "a; --")]]}
+ {:select [:*], :from [:t], :join [(keyword "u; --") [:= :t.id :u.id]]}
+ {:select [:*], :from [:t], :where [:= (keyword "a; --") 1]}
+ {:select [:*], :from [:t], :group-by [(keyword "a; --")]}
+ {:select [:*], :from [:t], :order-by [[(keyword "a; --") :desc]]}
+ {:update (keyword "t; --"), :set {:a 1}}
+ {:update :t, :set {(keyword "a; --") 1}}
+ {:delete-from (keyword "t; --")}
+ {:insert-into (keyword "t; --"), :values [{:a 1}]}
+ {:insert-into :t, :values [{(keyword "a; --") 1}]}
+ {:insert-into :t, :columns [(keyword "a; --")], :values [[1]]}
+ {:insert-into :t, :values [{:a 1}], :on-conflict [(keyword "a; --")], :do-update-set {:a 2}}
+ {:select [:*], :from [:t], :returning [(keyword "a; --")]}
+ {:with [[(keyword "cte; --") ^:allow-subquery {:select [:id], :from [:t]}]], :select [:*], :from [:cte]}
+ {:create-table [(keyword "t; --")], :with-columns [[:a :text]]}
+ {:create-table [:t], :with-columns [[(keyword "a; --") :text]]}
+ {:drop-table [(keyword "t; --")]}
+ [::h2x/identifier :field ["a" "b; --"]])))
+
+(deftest ^:parallel h2x-current-datetime-form-test
+ (are [engine expected] (= [expected]
+ (funnysql/format (h2x/current-datetime-honeysql-form engine) engine))
+ :mysql "now(6)"
+ :postgres "now()"
+ :h2 "now()"))
+
+(deftest ^:parallel inline-test
+ (are [x expected] (= [expected]
+ (funnysql/format [:inline x] :mysql))
+ 6 "6"
+ true "true")
+ (testing "a value that can't be inlined is not inlined: `inline!` logs a warning and the value falls through to the
+ ordinary `?`-parameter handling, so nothing is spliced into the SQL. See the TODO on
+ metabase.funnysql.core/inline!, which plans to make this an error instead of a warning."
+ (is (= ["?" "s"]
+ (funnysql/format [:inline "s"] :mysql)))))
+
+(deftest ^:parallel map-recursion-blocked-test
+ (testing "a map used as an ordinary value must never be compiled/recursed into as SQL -- only the top-level
+ entry point (or an explicit subquery-accepting clause) treats a map as a query to compile; anywhere
+ else it must fall through to the generic Object handling and get bound as an opaque `?` parameter,
+ per the comment above the `Compile` protocol's `extend-protocol` in metabase.funnysql.core"
+ (let [subquery-shaped-map {:select [:*] :from [:secrets]}
+ [sql & args] (funnysql/format {:where [:= :field subquery-shaped-map]} :postgres)]
+ (is (= "WHERE \"field\" = ?" sql)
+ "the map must not be expanded into subquery SQL text")
+ (is (= [subquery-shaped-map] args)
+ "the map must be passed through as an opaque bound parameter"))))
+
+(deftest ^:parallel rewrite-empty-in-clauses-test
+ (testing "rewrites nested empty IN clauses"
+ (are [expected clause] (= [expected]
+ (funnysql/format clause :postgres))
+ "false" [:in :id []]
+ "false" [:in :id nil]
+ "false" [:in :id '()]
+ "false" [:in :id #{}]
+ "false" [:in :id (lazy-seq [])]
+ "false" [:in :id {}]
+ "true" [:not-in :id []]
+ "true" [:not-in :id nil]
+ "true" [:not-in :id '()]
+ "true" [:not-in :id #{}]
+ "true" [:not-in :id (lazy-seq [])]
+ "\"id\" IN (1)" [:in :id [1]]
+ "\"id\" IN (1)" [:in :id #{1}]
+ "\"id\" IN (1)" [:in :id (lazy-seq [1])])))
+
+(deftest ^:parallel in-param-collection-test
+ (testing "a `:param` naming a collection expands into a list, the way a literal collection does"
+ ;; `IN` takes a list of values rather than one value, so compiling the param as a single `?`
+ ;; would bind the whole collection and emit `IN ?`, which no database accepts.
+ (are [expected clause params] (= expected
+ (funnysql/format clause :postgres {:params params}))
+ ["\"id\" IN (1, 2)"] [:in :id [:param :p]] {:p [1 2]}
+ ["\"id\" NOT IN (1, 2)"] [:not-in :id [:param :p]] {:p [1 2]}
+ ["\"id\" IN (1)"] [:in :id [:param :p]] {:p #{1}}
+ ["\"id\" IN (1)"] [:in :id [:param :p]] {:p (lazy-seq [1])}
+ ;; a non-numeric element still binds, one `?` per element
+ ["\"id\" IN (?, ?)" "a" "b"] [:in :id [:param :p]] {:p ["a" "b"]}))
+ (testing "an empty one is rewritten like an empty literal collection, rather than emitting `IN ()`"
+ (are [expected clause params] (= expected
+ (funnysql/format clause :postgres {:params params}))
+ ["false"] [:in :id [:param :p]] {:p []}
+ ["true"] [:not-in :id [:param :p]] {:p []}))
+ (testing "a `:param` naming something that is not a sequence or set is left alone"
+ ;; One value is not a list of them, so there is nothing to expand. A map stays one opaque bound
+ ;; value here, as it would in any other value slot -- an unmarked one is refused upstream by
+ ;; `metabase.app-db.honeysql-guard`.
+ (are [expected clause params] (= expected
+ (funnysql/format clause :postgres {:params params}))
+ ["\"id\" IN ?" 1] [:in :id [:param :p]] {:p 1}
+ ["\"id\" IN ?" {:select [:id]}] [:in :id [:param :p]] {:p {:select [:id]}}))
+ (testing "error on missing parameter"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Missing value for :param"
+ (funnysql/format [:in :id [:param :p]] :postgres)))))
+
+(deftest ^:parallel in-param-collection-binds-elements-test
+ (testing "the elements of a collection bound to a `:param` are bound as values, never compiled as SQL"
+ (are [xs expected] (= expected
+ (funnysql/format [:in :email [:param :xs]] :postgres {:params {:xs xs}}))
+ ["a@x" :email] ["\"email\" IN (?, ?)" "a@x" :email]
+ ["a@x" [:lower :email]] ["\"email\" IN (?, ?)" "a@x" [:lower :email]]
+ ;; this SQL is wrong but we always want to parameterize a `:param`, and
+ ;; never let you inject an identifier or function call
+ :email ["\"email\" IN ?" :email]
+ [:lower :email] ["\"email\" IN ?" [:lower :email]])))
+
+(deftest ^:parallel in-param-collection-preserves-order-test
+ (testing "a list or lazy seq bound to a `:param` keeps its order, the way a vector does"
+ ;; `(into (empty x) ...)` conjes onto the front of a list, so a list would come out reversed
+ (are [xs expected] (= expected
+ (funnysql/format [:in :id [:param :p]] :postgres {:params {:p xs}}))
+ [1 2 3] ["\"id\" IN (1, 2, 3)"]
+ (list 1 2 3) ["\"id\" IN (1, 2, 3)"]
+ (map inc [0 1 2]) ["\"id\" IN (1, 2, 3)"]
+ (list "a" "b" "c") ["\"id\" IN (?, ?, ?)" "a" "b" "c"]))
+ (testing "a list nested in a tuple keeps its order, since reversing it changes which rows match"
+ (are [xs expected] (= expected
+ (funnysql/format [:in [:composite :a :b] [:param :p]] :postgres {:params {:p xs}}))
+ [[1 2] [3 4]] ["(\"a\", \"b\") IN ((1, 2), (3, 4))"]
+ [(list 1 2) (list 3 4)] ["(\"a\", \"b\") IN ((1, 2), (3, 4))"]
+ (list (list 1 2) (list 3 4)) ["(\"a\", \"b\") IN ((1, 2), (3, 4))"])))
+
+(deftest ^:parallel in-subquery-test
+ (is (= ["WHERE \"dp\".\"group_id\" IN (SELECT \"group_id\" FROM \"permissions_group_membership\" WHERE \"user_id\" = 1)"]
+ (funnysql/format {:where [:in :dp.group_id ^:allow-subquery {:select [:group_id]
+ :from [:permissions_group_membership]
+ :where [:= :user_id [:inline 1]]}]}
+ :postgres)))
+ (testing "throw exception if subselect is not marked `^:allow-subquery`"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"\QInvalid sequence of values (maps must be marked with ^:allow-subquery)\E"
+ (funnysql/format {:where [:in :dp.group_id {:select [:group_id]
+ :from [:permissions_group_membership]
+ :where [:= :user_id [:inline 1]]}]}
+ :postgres)))))
+
+(deftest ^:parallel timestamp-diff-test
+ (is (= ["timestampdiff(second, \"col_a\", \"col_b\")"]
+ (funnysql/format [:timestampdiff :second :col_a :col_b]
+ :postgres)
+ (funnysql/format [::h2x/timestampdiff :second :col_a :col_b]
+ :postgres)))
+ (testing "the unit is spliced, so it has to be validated"
+ (are [unit] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid unit"
+ (funnysql/format [:timestampdiff unit :col_a :col_b] :mysql))
+ "second, `col_a`, `col_b`) OR 1 = 1; --"
+ (keyword "second, x) OR 1 = 1; --")
+ "micro second"
+ 1
+ nil)))
+
+(deftest ^:parallel h2x-timestamp-diff-test
+ (testing (str "metabase.util.honey-sql-2 is shared by Funny SQL (the app DB) and Honey SQL (warehouses, pgvector), "
+ "so its helpers have to produce working SQL under both")
+ (testing "calculate-interval-honeysql-form for MySQL needs `TIMESTAMPDIFF`'s unit to be a bare keyword"
+ (let [form {:select [[(h2x/calculate-interval-honeysql-form :mysql :end_time :start_time)]]}]
+ (is (= ["SELECT timestampdiff(microsecond, `start_time`, `end_time`)"]
+ (funnysql/format form :mysql)))))
+ (testing "a string type name passed to cast keeps its spelling"
+ (let [form {:select [[(h2x/cast "DateTime64(3, 'America/Port-au-Prince')" :x)]]}]
+ (is (= ["SELECT CAST(\"x\" AS DateTime64(3, 'America/Port-au-Prince'))"]
+ (funnysql/format form :postgres)))))))
+
+(deftest ^:parallel date-part-test
+ (is (= ["date_part(?, \"started_at\")" "year"]
+ (funnysql/format [:date_part "year" :started_at] :postgres))))
+
+(deftest ^:parallel empty-condition-clause-test
+ (testing "a nil or empty `:where`/`:having` is dropped, the way Honey SQL drops it"
+ ;; `[]` would otherwise compile to `()`, which H2 reads as an empty ROW
+ (are [form] (= ["SELECT \"id\" FROM \"t\""]
+ (update (funnysql/format form :postgres) 0 str/trim))
+ {:select :id, :from :t, :where []}
+ {:select :id, :from :t, :where nil}
+ {:select :id, :from :t, :having []}
+ {:select :id, :from :t, :having nil}))
+ (testing "only nil and `[]` mean no condition -- dropping a `WHERE` fails open, so other empty values are kept"
+ (are [condition] (str/includes? (first (funnysql/format {:select :id, :from :t, :where condition} :postgres))
+ "WHERE")
+ {}
+ #{}
+ ()))
+ (testing "a real condition is still compiled"
+ (is (= ["SELECT \"id\" FROM \"t\" WHERE \"a\" = 1"]
+ (funnysql/format {:select :id, :from :t, :where [:= :a 1]} :postgres)))))
+
+(deftest ^:parallel bare-value-clause-test
+ (testing "a clause that takes a list also accepts a single bare value, the way Honey SQL does"
+ (are [form expected] (= [expected]
+ (funnysql/format form :postgres))
+ {:select :id, :from :t, :group-by :id} "SELECT \"id\" FROM \"t\" GROUP BY \"id\""
+ {:select [:id], :from [:t], :group-by [:id]} "SELECT \"id\" FROM \"t\" GROUP BY \"id\""
+ {:select :id, :from :t, :order-by :id} "SELECT \"id\" FROM \"t\" ORDER BY \"id\" ASC"
+ {:select :id, :from :t, :group-by :a, :order-by :b} "SELECT \"id\" FROM \"t\" GROUP BY \"a\" ORDER BY \"b\" ASC"))
+ (testing "`:order-by` with an explicit direction still works, and is not mistaken for two columns"
+ (is (= ["SELECT \"id\" FROM \"t\" ORDER BY \"id\" DESC"]
+ (funnysql/format {:select :id, :from :t, :order-by [[:id :desc]]} :postgres))))
+ (testing "nil and empty mean the clause is absent, not `GROUP BY NULL`"
+ ;; `map!` emits its \" \" clause separator between every key whether or not the clause itself writes anything, so
+ ;; an absent clause leaves behind whitespace. That is cosmetic and predates this test; what matters is that no
+ ;; dangling `GROUP BY`/`ORDER BY` is emitted.
+ (let [[sql & args] (funnysql/format {:select :id, :from :t, :group-by nil, :order-by []} :postgres)]
+ (is (= "SELECT \"id\" FROM \"t\"" (str/trim sql)))
+ (is (empty? args))
+ (is (not (str/includes? sql "GROUP BY")))
+ (is (not (str/includes? sql "ORDER BY"))))))
+
+(deftest ^:parallel union-with-order-by-and-paging-test
+ (testing "a `UNION`'s body comes before the `ORDER BY`/`LIMIT`/`OFFSET` that apply to the combined result"
+ (is (= [(str "(SELECT \"id\" FROM \"a\") UNION ALL (SELECT \"id\" FROM \"b\")"
+ " ORDER BY \"id\" DESC LIMIT 50 OFFSET 10")]
+ (funnysql/format {:union-all [^:allow-subquery {:nest ^:allow-subquery {:select [:id] :from [:a]}}
+ ^:allow-subquery {:nest ^:allow-subquery {:select [:id] :from [:b]}}]
+ :order-by [[:id :desc]]
+ :limit 50
+ :offset 10}
+ :postgres))))
+ (testing "same for plain `:union`"
+ (is (= ["SELECT \"id\" FROM \"a\" UNION SELECT \"id\" FROM \"b\" ORDER BY \"id\" ASC"]
+ (funnysql/format {:union [^:allow-subquery {:select [:id] :from [:a]}
+ ^:allow-subquery {:select [:id] :from [:b]}]
+ :order-by [:id]}
+ :postgres)))))
+
+(deftest ^:parallel ilike-test
+ (testing "`:ilike` is an infix operator like `:like`, not a function call"
+ (is (= ["WHERE \"name\" ILIKE ?" "%foo%"]
+ (funnysql/format {:where [:ilike :name "%foo%"]} :postgres)))))
+
+(deftest ^:parallel concat-operator-test
+ (testing "`:||` is an infix operator, e.g. for concatenating `tsvector`s"
+ (is (= ["SELECT \"a\" || \"b\" || \"c\""]
+ (funnysql/format {:select [[[:|| :a :b :c]]]} :postgres)))))
+
+(deftest ^:parallel scalar-subquery-test
+ (testing "a subquery in a `SELECT` list or function argument has to be parenthesized"
+ (is (= [(str "SELECT \"id\", (SELECT count(*) FROM \"b\" WHERE \"b\".\"a_id\" = \"a\".\"id\") AS \"n\","
+ " coalesce((SELECT max(\"x\") FROM \"b\"), 0) AS \"m\" FROM \"a\"")]
+ (funnysql/format {:select [:id
+ [^:allow-subquery {:select [:%count.*]
+ :from [:b]
+ :where [:= :b.a_id :a.id]}
+ :n]
+ [[:coalesce ^:allow-subquery {:select [[:%max.x]] :from [:b]} 0]
+ :m]]
+ :from [:a]}
+ :postgres))))
+ (testing "but not double-parenthesized where the position already brings its own parens"
+ (is (= ["SELECT * FROM (SELECT \"id\" FROM \"b\") AS \"sub\" WHERE (\"x\" IN (SELECT \"id\" FROM \"c\")) AND ((SELECT max(\"y\") FROM \"d\") = 1)"]
+ (funnysql/format {:select [:*]
+ :from [[^:allow-subquery {:select [:id] :from [:b]} :sub]]
+ :where [:and
+ [:in :x ^:allow-subquery {:select [:id] :from [:c]}]
+ [:= ^:allow-subquery {:select [[:%max.y]] :from [:d]} 1]]}
+ :postgres))))
+ (testing "`INSERT INTO ... SELECT` doesn't parenthesize the `SELECT`"
+ (is (= ["INSERT INTO \"a\" (\"id\") SELECT \"id\" FROM \"b\""]
+ (funnysql/format {:insert-into [[:a [:id]] ^:allow-subquery {:select [:id] :from [:b]}]} :postgres)))))
+
+(deftest ^:parallel unknown-function-is-rejected-test
+ (testing "a function that isn't whitelisted in `fn-call!` must throw rather than being spliced into the SQL"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Function :nope-not-a-real-function is not currently supported"
+ (funnysql/format {:where [:= :field [:nope-not-a-real-function :x]]} :postgres)))))
+
+(deftest ^:parallel escape-test
+ (testing "`:escape` is a postfix operator on a LIKE pattern, not a function call"
+ (is (= ["WHERE \"NAME\" LIKE ? ESCAPE '!'" "foo%"]
+ (funnysql/format {:where [:like :name [:escape "foo%" (h2x/literal "!")]]}
+ :h2))))
+ (testing "the pattern itself can be an arbitrary expression"
+ (is (= ["WHERE \"name\" LIKE lower(\"other\") ESCAPE '!'"]
+ (funnysql/format {:where [:like :name [:escape [:lower :other] (h2x/literal "!")]]}
+ :postgres)))))
+
+(deftest ^:parallel position-test
+ (testing "`:position` compiles to the standard `position(x IN y)` on every engine, not the comma form Postgres rejects"
+ (are [engine expected] (= expected
+ (funnysql/format [:position (h2x/literal "/") :location] engine))
+ :h2 ["position('/' IN \"LOCATION\")"]
+ :postgres ["position('/' IN \"location\")"]
+ :mysql ["position('/' IN `location`)"]))
+ (testing "a plain string is bound like any other value"
+ (is (= ["position(? IN \"location\")" "/"]
+ (funnysql/format [:position "/" :location] :postgres))))
+ (testing "infix operands are parenthesized"
+ (is (= ["position((\"a\" || \"b\") IN (\"c\" || \"d\"))"]
+ (funnysql/format [:position [:|| :a :b] [:|| :c :d]] :postgres))))
+ (testing "nested in other expressions, e.g. the first segment of a collection `location`"
+ (is (= ["substring(\"location\", 1, position('/' IN substring(\"location\", 2)) + 1)"]
+ (funnysql/format [:substring :location [:inline 1]
+ [:+ [:position (h2x/literal "/") [:substring :location [:inline 2]]] [:inline 1]]]
+ :postgres))))
+ (testing "wrong number of args"
+ (are [form] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Wrong number of args to :position \(expected 2 args\)"
+ (funnysql/format form :postgres))
+ [:position :a]
+ [:position :a :b :c])))
+
+(deftest ^:parallel create-table-test
+ (is (= [(str "CREATE TABLE \"table\" ("
+ "\"id\" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "
+ "\"created_at\" timestamp with time zone DEFAULT current_timestamp NOT NULL, "
+ "\"model\" varchar(32) NOT NULL"
+ ")")]
+ (funnysql/format {:create-table [:table]
+ :with-columns [[:id :bigint [:primary-key] [:generated-by-default-as-identity]]
+ [:created_at :timestamp-with-time-zone
+ [:default :%current-timestamp]
+ :not-null]
+ [:model [:varchar 32] :not-null]]}
+ :postgres))))
+
+(deftest ^:parallel create-table-auto-increment-test
+ (testing ":auto-increment keeps its underscore -- it is one SQL keyword, not two words"
+ (is (= [(str "CREATE TABLE \"SEARCH_INDEX\" ("
+ "\"ID\" bigint AUTO_INCREMENT PRIMARY KEY, "
+ "\"SEARCH_TERMS\" text"
+ ")")]
+ (funnysql/format {:create-table [:search_index]
+ :with-columns [[:id :bigint :auto-increment :primary-key]
+ [:search_terms :text]]}
+ :h2)))))
+
+(deftest ^:parallel create-table-falsey-default-test
+ (testing "`false` is a legitimate column default and must not be mistaken for a missing one"
+ (is (= ["CREATE TABLE \"T\" (\"ARCHIVED\" boolean NOT NULL DEFAULT false)"]
+ (funnysql/format {:create-table [:t]
+ :with-columns [[:archived :boolean :not-null [:default false]]]}
+ :h2))))
+ (testing "a `:default` with no value at all is still an error"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"\QMissing value for :default\E"
+ (funnysql/format {:create-table [:t]
+ :with-columns [[:archived :boolean [:default]]]}
+ :h2)))))
+
+(deftest ^:parallel create-table-default-is-never-a-parameter-test
+ (testing "DDL takes no parameters, so a column default has to be written into the SQL text"
+ (are [default expected] (= [(str "CREATE TABLE \"t\" (\"c\" text DEFAULT " expected ")")]
+ (funnysql/format {:create-table [:t]
+ :with-columns [[:c :text [:default default]]]}
+ :postgres))
+ nil "NULL"
+ true "true"
+ 1 "1"
+ ;; spliced here even though a non-integral number is bound everywhere else
+ 0.5 "0.5"
+ (/ 1 4) "0.25"
+ (h2x/literal "x") "'x'"
+ :%current-timestamp "current_timestamp"))
+ (testing "anything that would compile to a `?` throws instead of producing a CREATE TABLE the database rejects"
+ (are [default] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"\QA column default cannot be a parameter; use h2x/literal for a string default\E"
+ (funnysql/format {:create-table [:t]
+ :with-columns [[:c :text [:default default]]]}
+ :postgres
+ {:params {:p "x"}}))
+ "x"
+ [:lower "x"]
+ [:param :p])))
+
+(deftest ^:parallel create-table-unknown-column-option-test
+ (testing "an unrecognized column option should throw a clear error rather than falling off the end of a `case`"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"\QUnknown column option\E"
+ (funnysql/format {:create-table [:table]
+ :with-columns [[:id :bigint :totally-bogus-option]]}
+ :h2)))))
+
+(deftest ^:parallel create-table-if-not-exists-test
+ (is (= ["CREATE TABLE IF NOT EXISTS \"t\" (\"id\" integer)"]
+ (funnysql/format {:create-table [:t :if-not-exists], :with-columns [[:id :integer]]} :postgres))))
+
+(deftest ^:parallel from-subquery-test
+ (is (= [(str "SELECT EXISTS ("
+ "SELECT 1 FROM ("
+ "SELECT * FROM \"metabase_field\" AS \"f\""
+ ") AS \"metabase_field\" "
+ "WHERE \"table_id\" IN ("
+ "SELECT \"id\" FROM \"metabase_table\" WHERE \"db_id\" = 415"
+ ")) AS \"exists\"")]
+ (funnysql/format {:select
+ [[[:exists
+ ^:allow-subquery
+ {:select [[[:inline 1]]]
+ :from
+ [[^:allow-subquery
+ {:select [:*]
+ :from [[:metabase_field :f]]}
+ :metabase_field]]
+ :where [:in :table_id ^:allow-subquery {:select [:id]
+ :from [:metabase_table]
+ :where [:= :db_id 415]}]}]
+ :exists]]}
+ :postgres))))
+
+(deftest ^:parallel param-test
+ (testing ":param should look up value from options :parameters; should always compile as `?`"
+ (is (= ["UPDATE \"SETTING\" SET \"VALUE\" = ?, \"VALUE_WITH_AAD\" = ? WHERE \"KEY\" = ?"
+ "2026-10-01 18:37:49.894829"
+ "2026-10-01 18:37:49.894829"
+ 100]
+ (funnysql/format {:update [:setting]
+ :set {:value "2026-10-01 18:37:49.894829"
+ :value_with_aad "2026-10-01 18:37:49.894829"}
+ :where [:= :key [:param :p17uc4hjfp068j]]}
+ :h2
+ {:params {:p17uc4hjfp068j 100}})))
+ (testing "error on missing parameter"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Missing value for :param"
+ (funnysql/format {:update [:setting]
+ :set {:value "2026-10-01 18:37:49.894829"
+ :value_with_aad "2026-10-01 18:37:49.894829"}
+ :where [:= :key [:param :p17uc4hjfp068j]]}
+ :h2))))
+ (testing "`false` and `nil` are legitimate parameter values, not missing ones"
+ ;; assert on the parameters rather than the whole SQL string so this does not also depend on the
+ ;; (separate, pre-existing) extra-parens behavior around a compiled `:param`.
+ (are [v] (= [v]
+ (rest (funnysql/format {:where [:= :key [:param :p]]} :h2 {:params {:p v}})))
+ false
+ nil)
+ (testing "including in an `:in` list"
+ (are [v] (= ["WHERE \"KEY\" IN ?" v]
+ (funnysql/format {:where [:in :key [:param :p]]} :h2 {:params {:p v}}))
+ false
+ nil)))))
+
+(deftest ^:parallel over-test
+ (are [form expected] (= expected
+ (funnysql/format form :postgres))
+ {:select [:*
+ [[:over [[:row_number]
+ {:partition-by :transform_id
+ :order-by [[:start_time :desc]]}]]
+ :rn]]
+ :from [:transform_run]}
+ ["SELECT *, row_number() OVER (PARTITION BY \"transform_id\" ORDER BY \"start_time\" DESC) AS \"rn\" FROM \"transform_run\""]
+
+ {:select [[[:over [[:row_number]
+ {:partition-by [:run_id]
+ :order-by [[:started_at :desc]]}]]
+ :rn]]
+ :from [:task_history]}
+ ["SELECT row_number() OVER (PARTITION BY \"run_id\" ORDER BY \"started_at\" DESC) AS \"rn\" FROM \"task_history\""]
+
+ {:select [:* [[:over [[:count :*] {}]]
+ :total_count]]
+ :from [:collection]}
+ ["SELECT *, count(*) OVER () AS \"total_count\" FROM \"collection\""]))
+
+(deftest ^:parallel dotted-alias-test
+ (testing "like Honey SQL, an alias containing a `.` is a single identifier, not a qualified one"
+ (are [engine expected] (= [expected]
+ (funnysql/format {:select [[:card.name :report_card.name]]
+ :from [[:report_card :card]]}
+ engine))
+ :postgres "SELECT \"card\".\"name\" AS \"report_card.name\" FROM \"report_card\" AS \"card\""
+ :h2 "SELECT \"CARD\".\"NAME\" AS \"REPORT_CARD.NAME\" FROM \"REPORT_CARD\" AS \"CARD\""
+ :mysql "SELECT `card`.`name` AS `report_card.name` FROM `report_card` AS `card`"))
+ (testing "an alias is still validated"
+ (is (thrown? clojure.lang.ExceptionInfo
+ (funnysql/format {:select [[:x (keyword "y\" FROM users; --")]]} :postgres)))))
+
+(deftest ^:parallel qualified-alias-test
+ (testing "an alias names a single thing, so a qualified one throws rather than compiling to `AS \"a\".\"b\"`"
+ (are [alias] (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid alias: an alias cannot be qualified"
+ (funnysql/format {:select [[:x alias]]} :postgres))
+ :a/b
+ (h2x/identifier :field-alias "a" "b")))
+ (testing "a single-part `h2x/identifier` alias is fine"
+ (is (= ["SELECT \"x\" AS \"a\""]
+ (funnysql/format {:select [[:x (h2x/identifier :field-alias "a")]]} :postgres)))))
+
+(deftest ^:parallel over-alias-inside-over-form-test
+ (testing "Honey SQL's alias-inside-`:over` form should fail loudly rather than silently dropping the alias"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"put the alias outside"
+ (funnysql/format {:select [[[:over [[:row_number] {:order-by [[:id :desc]]} :rn]]]]
+ :from [:query_execution]}
+ :postgres)))))
+
+(deftest ^:parallel over-unsupported-window-keys-test
+ (testing "anything in the window besides `:partition-by` and `:order-by` throws rather than being silently dropped"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"`:over` only supports :partition-by and :order-by"
+ (funnysql/format {:select [[[:over [[:row_number] {:partition-by [:a], :where [:= :a 1]}]] :rn]]
+ :from [:t]}
+ :postgres)))))
+
+(deftest ^:parallel nest-test
+ (testing ":nest wraps a subquery in parens"
+ (is (= [(str "(SELECT \"dc\".\"card_id\" AS \"card_id\" FROM \"report_dashboardcard\" AS \"dc\")"
+ " UNION ALL "
+ "(SELECT \"dcs\".\"card_id\" AS \"card_id\" FROM \"dashboardcard_series\" AS \"dcs\")")]
+ (funnysql/format {:union-all [^:allow-subquery
+ {:nest ^:allow-subquery {:select [[:dc.card_id :card_id]]
+ :from [[:report_dashboardcard :dc]]}}
+ ^:allow-subquery
+ {:nest ^:allow-subquery {:select [[:dcs.card_id :card_id]]
+ :from [[:dashboardcard_series :dcs]]}}]}
+ :postgres)))
+ (testing "don't splice in subqueries unless they are marked `^:allow-subquery`"
+ (is (= ["(?) UNION ALL (?)"
+ {:select [[:dc.card_id :card_id]], :from [[:report_dashboardcard :dc]]}
+ {:select [[:dcs.card_id :card_id]], :from [[:dashboardcard_series :dcs]]}]
+ (funnysql/format {:union-all [^:allow-subquery
+ {:nest {:select [[:dc.card_id :card_id]]
+ :from [[:report_dashboardcard :dc]]}}
+ ^:allow-subquery
+ {:nest {:select [[:dcs.card_id :card_id]]
+ :from [[:dashboardcard_series :dcs]]}}]}
+ :postgres))))))
+
+(deftest ^:parallel composite-test
+ (testing ":composite builds a row constructor"
+ (are [form expected] (= expected
+ (funnysql/format form :postgres))
+ {:select [[:f.id]]
+ :from [:metabase_field]
+ :where [:in
+ [:composite [:coalesce :t.schema "__null__"] :t.name :f.name]
+ [["public" "orders" "id"]]]}
+ [(str "SELECT \"f\".\"id\" FROM \"metabase_field\""
+ " WHERE (coalesce(\"t\".\"schema\", ?), \"t\".\"name\", \"f\".\"name\") IN ((?, ?, ?))")
+ "__null__" "public" "orders" "id"]
+
+ {:select [[[:count [:distinct [:composite :error_type :error_detail]]]]]
+ :from [:analysis_finding_error]}
+ ["SELECT count(distinct((\"error_type\", \"error_detail\"))) FROM \"analysis_finding_error\""])))
+
+(deftest ^:parallel to-regclass-test
+ (is (= ["SELECT \"reltuples\", \"relpages\" FROM \"pg_class\" WHERE \"oid\" = to_regclass(?)" "search_index"]
+ (funnysql/format {:select [:reltuples :relpages]
+ :from [:pg_class]
+ :where [:= :oid [:to_regclass "search_index"]]}
+ :postgres))))
+
+(deftest ^:parallel call-test
+ (testing "the h2x helpers built on sql/call emit [:call <fn> ...]"
+ (are [form expected] (= expected
+ (funnysql/format form :postgres))
+ {:update :query
+ :set {:average_execution_time (h2x/cast :integer
+ (h2x/round (h2x/+ (h2x/* 2 :average_execution_time) 1)
+ [:inline 0]))}
+ :where [:= :id [:inline 1]]}
+ ["UPDATE \"query\" SET \"average_execution_time\" = CAST(round((2 * \"average_execution_time\") + 1, 0) AS integer) WHERE \"id\" = 1"]
+
+ {:select [[(h2x/abs :x)]] :from [:t]} ["SELECT abs(\"x\") FROM \"t\""]
+ {:select [[(h2x/ceil :x)]] :from [:t]} ["SELECT ceil(\"x\") FROM \"t\""]
+ {:select [[(h2x/floor :x)]] :from [:t]} ["SELECT floor(\"x\") FROM \"t\""]
+ {:select [[(h2x/year :x)]] :from [:t]} ["SELECT year(\"x\") FROM \"t\""])))
+
+(deftest ^:parallel mod-test
+ (is (= ["SELECT \"x\" % 2 FROM \"t\""]
+ (funnysql/format {:select [[(h2x/mod :x 2)]] :from [:t]} :postgres))))
+
+(deftest ^:parallel for-test
+ (testing ":for accepts a vector as well as a bare keyword"
+ (are [for-clause] (= ["SELECT \"id\" FROM \"exploration_thread\" WHERE \"id\" = 1 FOR UPDATE"]
+ (funnysql/format {:select [:id]
+ :from [:exploration_thread]
+ :where [:= :id [:inline 1]]
+ :for for-clause}
+ :postgres))
+ :update
+ [:update])))
+
+(deftest ^:parallel drop-table-test
+ (is (= ["DROP TABLE \"table\""]
+ (funnysql/format {:drop-table [:table]} :postgres)))
+ (is (= ["DROP TABLE IF EXISTS \"table\""]
+ (funnysql/format {:drop-table [:if-exists :table]} :postgres))))
+
+(deftest ^:parallel postgres-full-text-search-match-test
+ (is (= ["\"search_vector\" @@ \"query\""]
+ (funnysql/format [honey.sql.pg-ops/atat :search_vector :query] :postgres)))
+ (testing "an argument that is not an identifier is rejected"
+ (are [lhs rhs] (thrown? clojure.lang.ExceptionInfo
+ (funnysql/format [honey.sql.pg-ops/atat lhs rhs] :postgres))
+ "abc" :query
+ :search_vector "abc"
+ [:to_tsvector :a] :query)))
+
+(deftest ^:parallel unary-plus-minus-test
+ (testing "`:+` or `:-` with a single arg should do what we expect"
+ (are [x expected] (= expected
+ (funnysql/format x :postgres))
+ [:- 1] ["-1"]
+ [:- -1] ["1"]
+ [:+ 1] ["1"]
+ [:- [:- 1 2]] ["-(1 - 2)"]
+ [:+ [:- 1 2]] ["1 - 2"]
+ [:- :x] ["-(\"x\")"]))
+ (testing "negating something that compiles to a negative number must not emit `--`, which starts a SQL comment"
+ (are [x expected] (= expected
+ (funnysql/format x :postgres {:params {:p -1}}))
+ {:select [[[:- [:inline -1]] :x]] :from [:t]} ["SELECT -(-1) AS \"x\" FROM \"t\""]
+ [:- [:- -1]] ["-(1)"]
+ [:- [:- [:inline -1]]] ["-(-(-1))"]
+ [:- [:param :p]] ["-(?)" -1]
+ [:- 1 [:inline -1]] ["1 - -1"])
+ (are [x] (not (str/includes? (first (funnysql/format x :postgres {:params {:p -1}}))
+ "--"))
+ [:- [:inline -1]]
+ [:- [:inline -1.5]]
+ [:- [:- [:inline -1]]]
+ [:- [:abs -1]]
+ [:- [:param :p]]))
+ (testing "other operators should error"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"No matching clause: :/"
+ (funnysql/format [:/ 1] :postgres)))))
+
+(deftest ^:parallel nested-arithmetic-test
+ (testing "Wrap nested arithmetic expressions in parens"
+ (is (= ["1 * (2 + 3)"]
+ (funnysql/format [:* 1 [:+ 2 3]] :postgres)))))
+
+(deftest ^:parallel no-raw-test
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Function :raw is not currently supported"
+ (funnysql/format [:raw "x"] :postgres)))
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Top-level map key :raw is not currently supported"
+ (funnysql/format {:raw "x"} :postgres))))
diff --git a/test/metabase/settings/models/setting/cache_test.clj b/test/metabase/settings/models/setting/cache_test.clj
index 81520ae411e..bf0e2dc3065 100644
--- a/test/metabase/settings/models/setting/cache_test.clj
+++ b/test/metabase/settings/models/setting/cache_test.clj
@@ -9,6 +9,7 @@
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[metabase.util.encryption :as encryption]
+ [metabase.util.honey-sql-2 :as h2x]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -29,16 +30,19 @@
Written raw, and to both columns, exactly as
[[metabase.settings.models.setting.cache/update-settings-last-updated!]] writes them -- through `:model/Setting` the
- raw SQL below would end up inside the JSON envelope rather than being evaluated."
+ query below would end up inside the JSON envelope rather than being evaluated."
[]
(let [ts (-> (t2/query-one
- {:select [[^:allow-raw-sql
- [:raw (case (mdb/db-type)
- ;; make it one second in the future so we don't end up getting an exact match when we try to test
- ;; to see if things update below
- :h2 "cast(dateadd('second', 1, current_timestamp) AS text)"
- :mysql "cast((current_timestamp + interval 1 second) AS char)"
- :postgres "cast((current_timestamp + interval '1 second') AS text)")]
+ {:select [[(let [db-type (mdb/db-type)
+ now-form (h2x/current-datetime-honeysql-form db-type)
+ target-cast-type (case db-type
+ (:h2 :postgres) :text
+ :mysql :char)]
+ [:cast
+ ;; make it one second in the future so we don't end up getting an exact match when we try
+ ;; to test to see if things update below
+ (h2x/add-interval-honeysql-form db-type now-form 1 :second)
+ target-cast-type])
:timestamp]]})
:timestamp)]
(t2/update! :setting {:key setting.cache/settings-last-updated-key}
diff --git a/test/metabase/util/honey_sql_2_test.clj b/test/metabase/util/honey_sql_2_test.clj
index 4a838b809c4..dab30b108cb 100644
--- a/test/metabase/util/honey_sql_2_test.clj
+++ b/test/metabase/util/honey_sql_2_test.clj
@@ -388,3 +388,20 @@
:where [:like [:lower :name] (h2x/like-prefix "A%b")]}))))
(testing "passes the app-DB HoneySQL guard"
(is (honeysql-guard/safe-syntax? {:select [:*] :from [:t] :where [:like :name (h2x/like-substring "a%b")]}))))
+
+(deftest ^:parallel h2x-timestamp-diff-test
+ (testing (str "metabase.util.honey-sql-2 is shared by Funny SQL (the app DB) and Honey SQL (warehouses, pgvector), "
+ "so its helpers have to produce working SQL under both")
+ (testing "calculate-interval-honeysql-form for MySQL needs `TIMESTAMPDIFF`'s unit to be a bare keyword"
+ (let [form {:select [[(h2x/calculate-interval-honeysql-form :mysql :end_time :start_time)]]}]
+ (is (= ["SELECT timestampdiff(microsecond, `start_time`, `end_time`)"]
+ (sql/format form {:dialect :mysql})))))
+ (testing "a string type name passed to cast keeps its spelling"
+ (let [form {:select [[(h2x/cast "DateTime64(3, 'America/Port-au-Prince')" :x)]]}]
+ (is (= ["SELECT CAST(\"x\" AS DateTime64(3, 'America/Port-au-Prince'))"]
+ (sql/format form {:dialect :ansi})))))
+ (testing "the Honey SQL side of `::h2x/timestampdiff` validates the unit too"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid unit"
+ (sql/format {:select [[[::h2x/timestampdiff "second, x) OR 1 = 1; --" :a :b]]]} {:dialect :mysql}))))))
diff --git a/test/metabase/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.clj
index 7dea818480d..582f21d28da 100644
--- a/test/metabase/warehouses_rest/api_test.clj
+++ b/test/metabase/warehouses_rest/api_test.clj
@@ -1908,8 +1908,10 @@
(binding [driver.settings/*allow-testing-h2-connections* true]
(f)))
t2.jdbc.query/reduce-jdbc-query (fn [rf init conn model [sql :as sql-args] opts]
- ;; app DB quoting differs: "..." on Postgres and H2, `...` on MySQL
- (when (re-find #"(?i)^SELECT \* FROM [\"`]?metabase_database[\"`]? WHERE [\"`]?id[\"`]? = \?$" sql)
+ ;; app DB quoting differs: "..." on Postgres and H2, `...` on MySQL.
+ ;; The id is a `?` parameter under Honey SQL but an inlined literal
+ ;; under FunnySQL, so accept either.
+ (when (re-find #"(?i)^SELECT \* FROM [\"`]?metabase_database[\"`]? WHERE [\"`]?id[\"`]? = (?:\?|\d+)$" sql)
(swap! db-reads inc))
(run-query rf init conn model sql-args opts))]
(t2/select-one :model/Database :id db-id)