Skip to content

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

Transform tests (#82495)

Alexander Polyankin86b1f4bca4c0
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn
index 5430e42f781..52cfaee5bef 100644
--- a/.clj-kondo/config.edn
+++ b/.clj-kondo/config.edn
@@ -320,6 +320,12 @@
{metabase-enterprise.entity-retrieval.core/search-unfiltered {:message "Permission bypass risk: library-index hits are unfiltered — resolve each entity and drop what the current user can't read"}
metabase.entity-retrieval.core/search-unfiltered {:message "Permission bypass risk: library-index hits are unfiltered — resolve each entity and drop what the current user can't read"}
metabase.entity-retrieval.mirror/search-unfiltered {:message "Permission bypass risk: library-index hits are unfiltered — resolve each entity and drop what the current user can't read"}
+ metabase-enterprise.transform-testing.expectations.equals/->EqualsRows {:message "Build an expectation with metabase-enterprise.transform-testing.expectations.protocol/build, over a value the schema has passed — the generated constructors skip validation"}
+ metabase-enterprise.transform-testing.expectations.equals/map->EqualsRows {:message "Build an expectation with metabase-enterprise.transform-testing.expectations.protocol/build, over a value the schema has passed — the generated constructors skip validation"}
+ metabase-enterprise.transform-testing.expectations.equals/->EqualsSql {:message "Build an expectation with metabase-enterprise.transform-testing.expectations.protocol/build, over a value the schema has passed — the generated constructors skip validation"}
+ metabase-enterprise.transform-testing.expectations.equals/map->EqualsSql {:message "Build an expectation with metabase-enterprise.transform-testing.expectations.protocol/build, over a value the schema has passed — the generated constructors skip validation"}
+ metabase-enterprise.transform-testing.expectations.empty/->Empty {:message "Build an expectation with metabase-enterprise.transform-testing.expectations.protocol/build, over a value the schema has passed — the generated constructors skip validation"}
+ metabase-enterprise.transform-testing.expectations.empty/map->Empty {:message "Build an expectation with metabase-enterprise.transform-testing.expectations.protocol/build, over a value the schema has passed — the generated constructors skip validation"}
clojure.core/print {:message "Use metabase.util.log instead of clojure.core/print"}
clojure.core/println {:message "Use metabase.util.log instead of clojure.core/println"}
clojure.core/printf {:message "Use metabase.util.log instead of clojure.core/printf"}
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index d64ef9679e3..e14553953ef 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -4113,6 +4113,7 @@
:model/Timeline
:model/Transform
:model/TransformTag
+ :model/TransformTest
:model/User}}
enterprise/search
@@ -4347,6 +4348,27 @@
:model-exports #{:model/Tenant}
:model-imports #{:model/Collection :model/User}}
+ enterprise/transform-testing
+ {:team "Graphy"
+ :api #{metabase-enterprise.transform-testing.api metabase-enterprise.transform-testing.init}
+ :uses #{api
+ driver
+ events
+ lib
+ lib.schema
+ models
+ premium-features
+ sql-tools
+ sql-tools.parsing
+ transforms.base
+ util
+ warehouses}
+ :model-exports #{:model/TransformTest}
+ :model-imports #{:model/Collection
+ :model/Database
+ :model/Transform
+ :model/User}}
+
enterprise/transforms
{:team "Graphy"
:module-exports #{enterprise/transforms.inspector
diff --git a/.clj-kondo/ratchets.edn b/.clj-kondo/ratchets.edn
index 350ef2ab465..5efdd817a85 100644
--- a/.clj-kondo/ratchets.edn
+++ b/.clj-kondo/ratchets.edn
@@ -17,7 +17,7 @@
:deprecated-var 85
:discouraged-java-method 4
:discouraged-namespace 69
- :discouraged-var 130
+ :discouraged-var 132
:duplicate-require 1
:equals-true 1
:main-without-gen-class 1
diff --git a/.env.example b/.env.example
index d2d9d1ea1db..cb401296fa5 100644
--- a/.env.example
+++ b/.env.example
@@ -161,3 +161,46 @@ TZ="US/Pacific"
# DO NOT USE Docker Desktop. Use Orbstack instead. Metabase will fail to connect
# to the dockerized Mongo QA database otherwise.
EXPERIMENTAL_DOCKER_DESKTOP_FORCE_QEMU=1
+
+
+# ==============================================================================
+# 4. BACKEND DRIVER TESTS
+# ==============================================================================
+#
+# Connection details for running backend tests against a driver, e.g.
+# ./bin/test-agent --drivers=snowflake :only '[metabase.foo-test]'
+
+# 4.1 BigQuery
+# ------------------------------------------------------------------------------
+# Service account with BigQuery access in the project. The test user creates and drops the test-data datasets itself.
+# https://cloud.google.com/iam/docs/keys-create-delete
+
+# MB_BIGQUERY_CLOUD_SDK_TEST_PROJECT_ID="<project-id>"
+# MB_BIGQUERY_CLOUD_SDK_TEST_SERVICE_ACCOUNT_JSON='{"type": "service_account", ...}'
+# Only for database routing tests
+# MB_BIGQUERY_CLOUD_SDK_TEST_SERVICE_ACCOUNT_JSON_ROUTING='{"type": "service_account", ...}'
+
+# 4.2 Redshift
+# ------------------------------------------------------------------------------
+# Test data is loaded into a unique schema per test run.
+
+# MB_REDSHIFT_TEST_HOST="<cluster>.<id>.<region>.redshift.amazonaws.com"
+# Comma-separated hosts to pick one at random from, instead of MB_REDSHIFT_TEST_HOST
+# MB_REDSHIFT_TEST_HOSTS=""
+# MB_REDSHIFT_TEST_PORT=5439
+# MB_REDSHIFT_TEST_DB="testdb"
+# MB_REDSHIFT_TEST_USER="metabase_ci"
+# MB_REDSHIFT_TEST_PASSWORD="<password>"
+# Only for database routing tests
+# MB_REDSHIFT_TEST_DB_ROUTING="dev"
+
+# 4.3 Snowflake
+# ------------------------------------------------------------------------------
+# Key-pair authentication. The test user creates and drops the test-data databases itself.
+# https://docs.snowflake.com/en/user-guide/key-pair-auth
+
+# MB_SNOWFLAKE_TEST_ACCOUNT="<orgname>-<accountname>"
+# MB_SNOWFLAKE_TEST_USER="<username>"
+# MB_SNOWFLAKE_TEST_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----"
+# MB_SNOWFLAKE_TEST_WAREHOUSE="<warehouse>"
+# MB_SNOWFLAKE_TEST_ADDITIONAL_OPTIONS=""
diff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.md
index 30812c7e1d5..f4fbb0c3925 100644
--- a/docs/developers-guide/driver-changelog.md
+++ b/docs/developers-guide/driver-changelog.md
@@ -6,6 +6,20 @@ title: Driver interface changelog
## Metabase 0.64.0
+- `metabase.driver.sql.normalize/default-schema` now takes the database as well as the driver:
+ `[driver database]`. The schema an unqualified table reference resolves to is a property of the connection for a
+ driver that opens a database where others have a default schema — ClickHouse now answers with the database its
+ connection opened, where before it inherited `"public"` from `:sql`. `database` may be nil when the caller has none
+ in hand, and such a driver answers nil for it.
+
+- New feature `:transforms/testing` -- whether the driver can run transform test suites against temp tables. Drivers
+ with this feature implement the new multimethods `metabase.driver/temp-table-name` `[driver]`,
+ `metabase.driver/compile-create-temp-table` `[driver {:keys [table query]}]`,
+ `metabase.driver/compile-drop-temp-table` `[driver table]`,
+ `metabase.driver/do-with-test-connection` `[driver database f]`,
+ `metabase.driver/execute-on-connection!` `[driver conn query]` and
+ `metabase.driver/query-on-connection` `[driver conn query {:keys [max-rows]}]`.
+
- `metabase.driver.sql-jdbc.execute/cancelation-poisons-connection?` `[driver]` -- whether canceling a `Statement`
leaves the `Connection` unfit for the next query, so that it must be discarded rather than returned to the
connection pool. The query processor cancels a `Statement` whenever reduction stops before the `ResultSet` runs out
diff --git a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj
index ea552fcd983..37880a3b7ef 100644
--- a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj
+++ b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj
@@ -43,6 +43,7 @@
[metabase-enterprise.stale.api]
[metabase-enterprise.support-access-grants.api]
[metabase-enterprise.tenants.api]
+ [metabase-enterprise.transform-testing.api]
[metabase-enterprise.transforms-python.api]
[metabase-enterprise.transforms.api]
[metabase-enterprise.upload-management.api]
@@ -80,7 +81,8 @@
:metabot-v3 (deferred-tru "Metabot")
:cloud-custom-smtp (deferred-tru "Custom SMTP")
:support-users (deferred-tru "Support Users")
- :transforms-python (deferred-tru "Transforms Python")})
+ :transforms-python (deferred-tru "Transforms Python")
+ :transforms-testing (deferred-tru "Transforms Testing")})
(defn- premium-handler [handler required-feature]
(let [handler (cond-> handler
@@ -150,6 +152,7 @@
;; feature gates setup paths. MFA verification lives under /api/session/mfa/* (OSS mount).
"/mfa" metabase-enterprise.mfa.routes/routes
"/permission_debug" (premium-handler metabase-enterprise.permission-debug.api/routes :advanced-permissions)
+ "/transform-test" (premium-handler metabase-enterprise.transform-testing.api/routes :transforms-testing)
;; TODO (Ngoc 2026-03-25) -- use :transforms-advanced feature flag once it exists
"/transforms" (premium-handler metabase-enterprise.transforms.api/routes :transforms-python)
"/transforms-python" (premium-handler metabase-enterprise.transforms-python.api/routes :transforms-python)
diff --git a/enterprise/backend/src/metabase_enterprise/core/init.clj b/enterprise/backend/src/metabase_enterprise/core/init.clj
index eb2a57b7434..5f5f6c46683 100644
--- a/enterprise/backend/src/metabase_enterprise/core/init.clj
+++ b/enterprise/backend/src/metabase_enterprise/core/init.clj
@@ -31,4 +31,5 @@
[metabase-enterprise.stale.init]
[metabase-enterprise.support-access-grants.init]
[metabase-enterprise.tenants.init]
+ [metabase-enterprise.transform-testing.init]
[metabase-enterprise.transforms-python.init]))
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/native_validation.clj b/enterprise/backend/src/metabase_enterprise/dependencies/native_validation.clj
index 65f7419305d..f65b4c025df 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/native_validation.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/native_validation.clj
@@ -153,6 +153,7 @@
[driver mp table-spec]
(:table (sql-tools/find-table-or-transform
driver
+ (lib.metadata/database mp)
(lib.metadata/tables mp)
(lib.metadata/transforms mp)
table-spec)))
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/events.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/events.clj
index 624c94efe21..c0da33fb6dc 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/events.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/events.clj
@@ -79,21 +79,18 @@
;;; ----------------------------------------- Helper Functions ---------------------------------------------------------
(defn- resolve-status
- "Suppresses a no-op 'update' based on status and content_hash, otherwise keep status unchanged."
+ "Suppresses a no-op 'update' whose content_hash and stored file_path both still match, otherwise keeps status
+ unchanged."
[model-type model-id status existing]
- (cond
- (not= "update" status)
- status
-
- (nil? (:content_hash existing))
+ (if (or (not= "update" status)
+ (nil? (:content_hash existing)))
status
-
- (not= (:content_hash existing)
- (source/row->content-hash {:model_type model-type :model_id model-id}))
- status
-
- :else ;; hash has not changed
- "synced"))
+ (let [{:keys [path content-hash]} (source/row->file-info {:model_type model-type :model_id model-id})]
+ (if (and (= (:content_hash existing) content-hash)
+ (or (nil? (:file_path existing))
+ (= (:file_path existing) path)))
+ "synced"
+ status))))
(defn- create-or-update-remote-sync-object-entry!
"Creates or updates a remote sync object entry for a model change.
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/source.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/source.clj
index 5df7f6a69ad..fd3705a5a23 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/source.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/source.clj
@@ -99,12 +99,19 @@
[^String content]
(codecs/bytes->hex (buddy-hash/sha256 content)))
+(defn row->file-info
+ "The repo `:path` and the SHA-256 (hex) `:content-hash` of the serialized YAML for the entity named by `row`
+ ({:model_type :model_id}), or nil if it can't be extracted."
+ [row]
+ (when-let [entity (first (spec/extract-entities-for-rows [row]))]
+ (let [{:keys [path content]} (entity->file-spec (storage-context) entity)]
+ {:path path, :content-hash (content-hash content)})))
+
(defn row->content-hash
"SHA-256 (hex) of the serialized YAML for the entity named by `row` ({:model_type :model_id}), or nil if it
can't be extracted. Hashes the live DB serialization (never on-disk bytes), so it's stable across sync points."
[row]
- (when-let [entity (first (spec/extract-entities-for-rows [row]))]
- (content-hash (:content (entity->file-spec (storage-context) entity)))))
+ (:content-hash (row->file-info row)))
(defn serialize-specs
"Serializes a stream of entities into an eager vector of `{:path :content}` file specs. Reports progress
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj
index c9213f189bd..379a76d630c 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj
@@ -304,6 +304,25 @@
:export-scope :all ; query for all instances
:enabled? :remote-sync-transforms}
+ :model/TransformTest
+ {:model-type "TransformTest"
+ :model-key :model/TransformTest
+ :identity :entity-id
+ :delete-after [:model/Transform] ; has transform_id FK
+ :parent-model :model/Transform
+ :parent-fk :transform_id
+ :events {:prefix :event/transform-test
+ :types [:create :update :delete]}
+ :eligibility {:type :setting
+ :setting :remote-sync-transforms}
+ :archived-key nil ; no archived field
+ :tracking {:select-fields [:name]
+ :field-mappings {:model_name :name}}
+ :removal {:statuses #{"removed" "delete"} ; no scope-key = global deletion
+ :all-on-setting-disable :remote-sync-transforms}
+ :export-scope :all ; query for all instances
+ :enabled? :remote-sync-transforms}
+
:model/PythonLibrary
{:model-type "PythonLibrary"
:model-key :model/PythonLibrary
diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
index e5ee7ba20ff..cb35344cf1c 100644
--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
@@ -34,7 +34,7 @@
(conj "Setting")
(not (:no-transforms opts))
- (conj "Transform" "TransformTag" "TransformJob" "PythonLibrary")
+ (conj "Transform" "TransformTag" "TransformJob" "TransformTest" "PythonLibrary")
(not (:no-embedding-themes opts))
(conj "EmbeddingTheme")
diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj
index f7a80dd154d..2612bb681a7 100644
--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj
+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj
@@ -43,7 +43,8 @@
"Setting"
"Transform"
"TransformJob"
- "TransformTag"]))
+ "TransformTag"
+ "TransformTest"]))
(def inlined-models
"An additional list of models which are inlined into parent entities for serialization.
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/api.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/api.clj
new file mode 100644
index 00000000000..59e89f1a727
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/api.clj
@@ -0,0 +1,96 @@
+(ns metabase-enterprise.transform-testing.api
+ (:require
+ [metabase-enterprise.transform-testing.db :as transform-testing.db]
+ [metabase-enterprise.transform-testing.errors :as transform-testing.errors]
+ [metabase-enterprise.transform-testing.runner :as transform-testing.runner]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase.api.common :as api]
+ [metabase.api.macros :as api.macros]
+ [metabase.api.routes.common :refer [+auth]]
+ [metabase.models.interface :as mi]
+ [metabase.util.malli.schema :as ms]))
+
+(set! *warn-on-reflection* true)
+
+(defn- refusing
+ "Call `thunk`, rethrowing a typed transform-test refusal as the status and `:error-code` it publishes.
+
+ 400 — the test is wrong, and its author can fix it.
+ 422 — the test is fine; the transform or its database prevents a run here.
+ 501 — the test asks for something not built yet."
+ [thunk]
+ (try
+ (thunk)
+ (catch clojure.lang.ExceptionInfo e
+ (if-let [error-type (:error-type (ex-data e))]
+ (throw (ex-info (ex-message e)
+ (assoc (dissoc (ex-data e) :error-type)
+ :status-code (transform-testing.errors/status-code error-type)
+ :error-code (transform-testing.errors/code error-type))))
+ (throw e)))))
+
+(api.macros/defendpoint :get "/" :- [:sequential ::transform-testing.schema/transform-test]
+ "List the transform tests, optionally only those of one transform."
+ [_route-params
+ {:keys [transform-id]} :- [:map {:closed true}
+ [:transform-id {:optional true} [:maybe ms/PositiveInt]]]]
+ (api/check-data-analyst)
+ (filter mi/can-read? (transform-testing.db/transform-tests {:transform-id transform-id})))
+
+(api.macros/defendpoint :get "/:id" :- ::transform-testing.schema/transform-test
+ "Get a transform test."
+ [{:keys [id]} :- [:map {:closed true}
+ [:id ms/PositiveInt]]]
+ (api/read-check (transform-testing.db/transform-test id)))
+
+(api.macros/defendpoint :post "/" :- ::transform-testing.schema/transform-test
+ "Create a transform test."
+ [_route-params
+ _query-params
+ {:keys [transform_id inputs expectations] :as body} :- ::transform-testing.schema/transform-test.create]
+ (api/write-check :model/Transform transform_id)
+ (refusing #(transform-testing.runner/validate-transform-test transform_id inputs expectations))
+ (transform-testing.db/insert-transform-test! (assoc body :creator_id api/*current-user-id*)))
+
+(api.macros/defendpoint :put "/:id" :- ::transform-testing.schema/transform-test
+ "Update a transform test."
+ [{:keys [id]} :- [:map {:closed true}
+ [:id ms/PositiveInt]]
+ _query-params
+ {:keys [transform_id] :as body} :- ::transform-testing.schema/transform-test.update]
+ (let [transform-test (api/write-check (transform-testing.db/transform-test id))]
+ (when transform_id
+ (api/write-check :model/Transform transform_id))
+ (when (seq body)
+ (when (some #(contains? body %) [:transform_id :inputs :expectations])
+ (let [{:keys [transform_id inputs expectations]} (merge transform-test body)]
+ (refusing #(transform-testing.runner/validate-transform-test transform_id inputs expectations))))
+ (transform-testing.db/update-transform-test! id body)))
+ (transform-testing.db/transform-test id))
+
+(api.macros/defendpoint :delete "/:id" :- :nil
+ "Delete a transform test."
+ [{:keys [id]} :- [:map {:closed true}
+ [:id ms/PositiveInt]]]
+ (api/write-check (transform-testing.db/transform-test id))
+ (transform-testing.db/delete-transform-test! id)
+ nil)
+
+(api.macros/defendpoint :post "/:id/run" :- ::transform-testing.schema/run-result
+ "Run a transform test.
+
+ A 200 means the run happened: `:status` is then `passed` or `failed`, and every expectation
+ reports what it found — including one that could not be evaluated, which comes back as that
+ expectation's own `:error` rather than discarding the answers of the others.
+
+ Any other status means the run was refused and nothing meaningful ran; the body then carries the `:error-code`
+ naming which refusal it was, as [[refusing]] describes."
+ [{:keys [id]} :- [:map {:closed true}
+ [:id ms/PositiveInt]]]
+ (refusing #(-> (transform-testing.db/transform-test id)
+ api/write-check
+ transform-testing.runner/run-transform-test!)))
+
+(def ^{:arglists '([request respond raise])} routes
+ "`/api/ee/transform-test` routes."
+ (api.macros/ns-handler *ns* +auth))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/compile.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/compile.clj
new file mode 100644
index 00000000000..0eb3afc87f7
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/compile.clj
@@ -0,0 +1,223 @@
+(ns metabase-enterprise.transform-testing.compile
+ "Pure SQL for a transform test run: the transform's source rewritten to read temp tables, and every query the run
+ executes against them. No I/O.
+
+ parsing — `compile-source`, `referenced-tables`, `dangling-qualifiers`
+ rewrite — `table-replacements`, `replace-tables`, `compile-transform`
+ queries — `compile-input`, `rows-query`, `columns-query`, `row-count-query`, `comparison-query`
+
+ Every identifier is built with `h2x/identifier` and every cast with `h2x/cast`, which quote what cannot be written
+ bare, and every value is a bound parameter, so nothing an author writes is spliced into the SQL."
+ (:require
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase-enterprise.transform-testing.util :as transform-testing.u]
+ [metabase.driver.sql.query-processor :as sql.qp]
+ [metabase.lib.schema.common :as lib.schema.common]
+ [metabase.sql-tools.core :as sql-tools]
+ [metabase.transforms-base.schema :as transforms-base.schema]
+ [metabase.transforms-base.util :as transforms-base.u]
+ [metabase.util.honey-sql-2 :as h2x]
+ [metabase.util.malli :as mu]
+ [metabase.util.malli.registry :as mr]))
+
+(set! *warn-on-reflection* true)
+
+;;; ------------------------------------------------- Schemas -------------------------------------------------
+
+(mr/def ::compiled-query
+ "A compiled native query."
+ [:map {:closed true}
+ [:query :string]
+ [:params [:maybe [:sequential ::lib.schema.common/field-value]]]])
+
+(mr/def ::compiled-source
+ "The transform's source compiled to SQL, before any temp-table replacement, with the tables it reads."
+ [:map {:closed true}
+ [:query :string]
+ [:params [:maybe [:sequential ::lib.schema.common/field-value]]]
+ [:referenced-tables [:set ::transform-testing.schema/table]]])
+
+(mr/def ::table-key
+ "A way a query can refer to a table, as a `sql-tools/replace-names` key."
+ [:map {:closed true}
+ [:schema {:optional true} [:maybe :string]]
+ [:table :string]])
+
+(mr/def ::table-replacements
+ "The `sql-tools/replace-names` `:tables` map from the input and output tables to their temp tables."
+ [:map-of
+ ::table-key
+ [:map {:closed true}
+ [:db [:maybe :string]]
+ [:schema [:maybe :string]]
+ [:table :string]]])
+
+;;; ------------------------------------------------- Parsing -------------------------------------------------
+
+(mu/defn referenced-tables :- [:set ::transform-testing.schema/table]
+ "The tables `sql` reads."
+ [driver :- :keyword
+ sql :- :string]
+ (into #{}
+ (map (fn [{:keys [schema table]}] {:schema schema :name table}))
+ (sql-tools/referenced-tables-raw driver sql {:fail-on-parse-error? true})))
+
+(mu/defn dangling-qualifiers :- [:set :string]
+ "The names qualifying a column in `sql` that are not a FROM-clause table or alias."
+ [driver :- :keyword
+ sql :- :string]
+ (into #{}
+ (comp (filter (comp #{:missing-table-alias} :type))
+ (map :name))
+ (:errors (sql-tools/field-references driver sql))))
+
+(mu/defn compile-source :- ::compiled-source
+ "The transform's source compiled to SQL, with the tables it reads."
+ [driver :- :keyword
+ transform :- ::transforms-base.schema/transform]
+ (let [{:keys [query params]} (transforms-base.u/compile-source transform nil)]
+ {:query query
+ :params params
+ :referenced-tables (referenced-tables driver query)}))
+
+;;; ------------------------------------------------- Rewrite -------------------------------------------------
+
+(mu/defn table-keys :- [:sequential ::table-key]
+ "The ways a query can refer to the table `table` in `schema`: qualified, and bare when `schema` is the database's
+ `default-schema`, the one an unqualified reference resolves to."
+ [schema :- [:maybe :string]
+ table :- :string
+ default-schema :- [:maybe :string]]
+ (cond-> []
+ schema
+ (conj {:schema schema :table table})
+
+ (or (nil? schema)
+ (= (transform-testing.u/fold-identifier schema)
+ (transform-testing.u/fold-identifier default-schema)))
+ (conj {:table table})))
+
+(mu/defn table-replacements :- ::table-replacements
+ "The replacements mapping each input's table to its temp table in `input->table`, and the transform's target table to
+ `output-table`."
+ [transform :- ::transforms-base.schema/transform
+ input->table :- [:map-of ::transform-testing.schema/input ::lib.schema.common/non-blank-string]
+ output-table :- ::lib.schema.common/non-blank-string
+ default-schema :- [:maybe :string]]
+ (let [{target-schema :schema target-name :name} (:target transform)]
+ (into {}
+ (for [[{:keys [schema name]} table] (conj (mapv (fn [[input table]] [(:table input) table]) input->table)
+ [{:schema target-schema :name target-name} output-table])
+ table-key (table-keys schema name default-schema)]
+ [table-key {:db nil :schema nil :table table}]))))
+
+(mu/defn replace-tables :- :string
+ "`sql` reading from the temp tables of `replacements` instead of the tables they replace."
+ [driver :- :keyword
+ sql :- :string
+ replacements :- ::table-replacements]
+ (sql-tools/replace-names driver sql {:tables replacements} {:allow-unused? true}))
+
+(mu/defn compile-transform :- ::compiled-query
+ "`compiled-source` reading from the temp tables of `replacements` instead of the tables they replace."
+ [driver :- :keyword
+ compiled-source :- ::compiled-source
+ replacements :- ::table-replacements]
+ {:query (replace-tables driver (:query compiled-source) replacements)
+ :params (:params compiled-source)})
+
+;;; ------------------------------------------------- Queries -------------------------------------------------
+
+(defn- compiled
+ "`honeysql` formatted for `driver` as a compiled query."
+ [driver honeysql]
+ (let [[query & params] (sql.qp/format-honeysql driver honeysql)]
+ {:query query :params (vec params)}))
+
+(defn- from
+ "The temp table `table` as a `:from` entry."
+ [table]
+ [(h2x/identifier :table table)])
+
+(defn- field
+ "`column-name` as a column reference."
+ [column-name]
+ (h2x/identifier :field column-name))
+
+(defn- as
+ "`alias-name` as a column or table alias, also the shape a bare expression takes as a `:select` entry."
+ ([alias-name]
+ [alias-name])
+ ([identifier-type alias-name]
+ [(h2x/identifier identifier-type alias-name)]))
+
+(def ^:private no-rows
+ "A condition no row satisfies."
+ [:= [:inline 1] [:inline 0]])
+
+(mu/defn rows-query :- ::compiled-query
+ "The query returning the literal `rows` over `columns`, each cell cast to its column's `cast_type` and named by
+ the matching entry of `sql-names`."
+ [driver :- :keyword
+ columns :- [:sequential ::transform-testing.schema/column]
+ sql-names :- [:sequential :string]
+ rows :- [:sequential ::transform-testing.schema/row]]
+ (let [select-row (fn [row]
+ {:select (mapv (fn [{:keys [name cast_type]} sql-name]
+ [(h2x/cast cast_type (get row name)) (as :field-alias sql-name)])
+ columns
+ sql-names)})
+ relation (if (seq rows)
+ {:union-all (mapv select-row rows)}
+ (assoc (select-row {}) :where no-rows))]
+ (compiled driver {:select [:*] :from [[relation (as :table-alias "mb_rows")]]})))
+
+(mu/defn columns-query :- ::compiled-query
+ "The query returning no rows from the temp table `table`, for its columns."
+ [driver :- :keyword
+ table :- :string]
+ (compiled driver {:select [:*] :from [(from table)] :where no-rows}))
+
+(mu/defn row-count-query :- ::compiled-query
+ "The query counting the rows of the temp table `table`."
+ [driver :- :keyword
+ table :- :string]
+ (compiled driver {:select [[[:count [:inline 1]] (as :field-alias "__mb_count")]]
+ :from [(from table)]}))
+
+(mu/defn comparison-query :- ::compiled-query
+ "The query returning each row over the columns `sql-names` whose count differs between the temp tables `output-table`
+ and `expected-table`, followed by how many more times it appears in `output-table`."
+ [driver :- :keyword
+ output-table :- :string
+ expected-table :- :string
+ sql-names :- [:sequential :string]]
+ (let [columns (mapv field sql-names)
+ src (field "__mb_src")
+ tagged (fn [table tag]
+ {:select (conj (mapv as columns) [[:inline tag] (as :field-alias "__mb_src")])
+ :from [(from table)]})]
+ (compiled driver {:select (conj (mapv as columns) [[:sum src] (as :field-alias "__mb_delta")])
+ :from [[{:union-all [(tagged output-table 1)
+ (tagged expected-table -1)]}
+ (as :table-alias "__mb_comparison")]]
+ :group-by columns
+ :having [:<> [:sum src] [:inline 0]]})))
+
+;;; ------------------------------------------------- Inputs --------------------------------------------------
+
+(defmulti compile-input
+ "The query returning the test data of `input`."
+ {:arglists '([driver input])}
+ (fn [_driver input]
+ (:format input)))
+
+(mu/defmethod compile-input :sql :- ::compiled-query
+ [_driver :- :keyword
+ input :- ::transform-testing.schema/input]
+ {:query (:sql input), :params []})
+
+(mu/defmethod compile-input :rows :- ::compiled-query
+ [driver :- :keyword
+ {:keys [columns rows]} :- ::transform-testing.schema/input]
+ (rows-query driver columns (mapv :name columns) rows))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/db.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/db.clj
new file mode 100644
index 00000000000..26c878aeb00
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/db.clj
@@ -0,0 +1,60 @@
+(ns metabase-enterprise.transform-testing.db
+ "Application database queries for the transform-testing 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."
+ (:require
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase.lib.schema.id :as lib.schema.id]
+ [metabase.util.malli :as mu]
+ [metabase.util.malli.schema :as ms]
+ [toucan2.core :as t2]))
+
+(mu/defn transform-tests :- [:sequential ::transform-testing.schema/transform-test]
+ "The TransformTests in name order, only those of the Transform with `:transform-id` when it is given."
+ [{:keys [transform-id]} :- [:map {:closed true}
+ [:transform-id {:optional true} [:maybe ::lib.schema.id/transform]]]]
+ (t2/select :model/TransformTest (cond-> {:order-by [[:name :asc] [:id :asc]]}
+ transform-id (assoc :where [:= :transform_id transform-id]))))
+
+(mu/defn transform-test :- [:maybe ::transform-testing.schema/transform-test]
+ "The TransformTest with `id`, or nil."
+ [id :- ms/PositiveInt]
+ (t2/select-one :model/TransformTest :id id))
+
+(mu/defn insert-transform-test! :- ::transform-testing.schema/transform-test
+ "Insert the TransformTest `transform-test` and return the inserted instance."
+ [transform-test :- [:merge
+ ::transform-testing.schema/transform-test.create
+ [:map {:closed true}
+ [:creator_id ::lib.schema.id/user]]]]
+ (t2/insert-returning-instance! :model/TransformTest transform-test))
+
+(mu/defn update-transform-test!
+ "Apply `updates` to the TransformTest with `id`."
+ [id :- ms/PositiveInt
+ updates :- ::transform-testing.schema/transform-test.update]
+ (t2/update! :model/TransformTest id updates))
+
+(mu/defn delete-transform-test!
+ "Delete the TransformTest with `id`."
+ [id :- ms/PositiveInt]
+ (t2/delete! :model/TransformTest :id id))
+
+(mu/defn delete-transform-tests!
+ "Delete the TransformTests of the Transform with `transform-id`."
+ [transform-id :- ::lib.schema.id/transform]
+ (t2/delete! :model/TransformTest :transform_id transform-id))
+
+(mu/defn transform
+ "The Transform with `transform-id`, or nil."
+ [transform-id :- ::lib.schema.id/transform]
+ (t2/select-one :model/Transform :id transform-id))
+
+(mu/defn database
+ "The Database with `database-id`, or nil."
+ [database-id :- ::lib.schema.id/database]
+ (t2/select-one :model/Database :id database-id))
+
+(mu/defn transform-test-by-entity-id :- [:maybe ::transform-testing.schema/transform-test]
+ "The TransformTest with `entity-id`, or nil."
+ [entity-id :- :string]
+ (t2/select-one :model/TransformTest :entity_id entity-id))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/errors.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/errors.clj
new file mode 100644
index 00000000000..760374e630b
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/errors.clj
@@ -0,0 +1,86 @@
+(ns metabase-enterprise.transform-testing.errors
+ "The `:error-type` vocabulary for transform tests, and [[ex]], the constructor every typed throw
+ goes through.
+
+ A refusal and a failure are different outcomes, and a caller that confuses them either gives up
+ on a real bug or chases one that does not exist. Everything here is a refusal: the run did not
+ happen, or could not be completed. A failing expectation is not an error — it is a result, and
+ it rides back on the expectation that produced it.")
+
+(set! *warn-on-reflection* true)
+
+(def ^:private error-types
+ "Every `:error-type` a transform test run can throw, and the HTTP status it carries.
+
+ 400 — the test is wrong, and its author can fix it.
+ 422 — the test is fine; the transform or its database prevents a run here.
+ 501 — the test asks for something not built yet."
+ {;; 400 — authoring
+ ::unknown-column 400 ; An expectation names a column the output does not have.
+ ::ambiguous-column 400 ; An expectation names a column that matches several, case aside.
+ ::missing-inputs 400 ; The transform reads a table with no declared input.
+ ::unused-inputs 400 ; An input is declared for a table the transform never reads.
+ ::duplicate-input-table 400 ; Two inputs stand in for the same table.
+ ::unparseable-source 400 ; The transform's SQL could not be parsed to find what it reads.
+ ::unremapped-reference 400 ; A real table survived the rewrite into the test's SQL.
+
+ ;; 422 — the transform or its environment
+ ::unsupported-transform 422 ; Not a query transform (Python, say).
+ ::unsupported-driver 422 ; The database does not support transform testing.
+ ::transform-failed 422 ; The transform under test would not run.
+ ::setup-failed 422 ; A test input could not be materialized.
+ ::expectation-failed 422 ; An expectation's own query failed to execute.
+
+ ;; 501 — not built yet
+ ::unsupported-format 501}); The expectation asks for a form that is not implemented.
+
+(def all
+ "Every declared `:error-type`."
+ (set (keys error-types)))
+
+(defn checked
+ "Return `error-type` iff it is a member of [[all]]; otherwise throw."
+ [error-type]
+ (when-not (contains? all error-type)
+ (throw (ex-info (str error-type " is not a declared transform-test error type; see"
+ " metabase-enterprise.transform-testing.errors/all.")
+ {:invalid-error-type error-type})))
+ error-type)
+
+(defmacro ex
+ "Construct (not throw) a typed transform-test ExceptionInfo. `error-type` is assoc'd into `data`
+ under `:error-type` and must be a member of [[all]] — a literal keyword is checked at
+ macro-expansion time, so a typo fails the build; a computed one is checked at runtime."
+ ([error-type msg data]
+ `(ex ~error-type ~msg ~data nil))
+ ([error-type msg data cause]
+ (if (keyword? error-type)
+ (do (checked error-type)
+ `(ex-info ~msg (assoc ~data :error-type ~error-type) ~cause))
+ `(ex-info ~msg (assoc ~data :error-type (checked ~error-type)) ~cause))))
+
+(defn code
+ "The public wire spelling of `error-type` — the vocabulary a client switches on.
+
+ Derived from the keyword's name so the two cannot drift, and prefixed so that the Clojure
+ namespace this vocabulary happens to live in is not what gets published."
+ [error-type]
+ (str "transform-test." (name error-type)))
+
+(defn status-code
+ "The HTTP status for `error-type`, or 500 for an untyped or unknown one."
+ [error-type]
+ (get error-types error-type 500))
+
+(defn remap-message
+ "`message` with every temp-table name in `temp->logical` replaced by the name the author wrote.
+
+ Matching ignores case because Snowflake and H2 upper-case the generated names. A nil message
+ stays nil."
+ [message temp->logical]
+ (when message
+ (reduce (fn [^String msg [temp logical]]
+ (.replaceAll msg (str "(?i)\\b" (java.util.regex.Pattern/quote temp) "\\b")
+ (java.util.regex.Matcher/quoteReplacement logical)))
+ (str message)
+ temp->logical)))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/executor.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/executor.clj
new file mode 100644
index 00000000000..4f521eda5b3
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/executor.clj
@@ -0,0 +1,50 @@
+(ns metabase-enterprise.transform-testing.executor
+ "The one place warehouse I/O lives. Given a single test-run connection (from
+ `driver/do-with-test-connection`), it creates temp tables from compiled queries, drops them, and
+ runs read-back queries. It knows nothing about transform tests, expectations, or judgment — it takes
+ compiled queries and table names and talks to the connection.
+
+ Kept separate from the runner (orchestration) and the compiler (pure SQL) so the module's I/O is
+ in one auditable namespace. No plan object is marshaled between stages — callers pass ordinary
+ arguments (driver, conn, compiled-query, table name)."
+ (:require
+ [metabase-enterprise.transform-testing.compile :as transform-testing.compile]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase.driver :as driver]
+ [metabase.lib.schema.common :as lib.schema.common]
+ [metabase.util.log :as log]
+ [metabase.util.malli :as mu]))
+
+(mu/defn create-temp-table!
+ "Create the temp table `table` from the compiled `query` on `conn`."
+ [driver :- :keyword
+ conn :- ::transform-testing.schema/connection
+ table :- :string
+ query :- ::transform-testing.compile/compiled-query]
+ (driver/execute-on-connection! driver conn (driver/compile-create-temp-table driver {:table table :query query})))
+
+(mu/defn drop-temp-table!
+ "Drop the temp table `table` on `conn` if it exists, logging a failure instead of throwing it."
+ [driver :- :keyword
+ conn :- ::transform-testing.schema/connection
+ table :- :string]
+ (try
+ (driver/execute-on-connection! driver conn (driver/compile-drop-temp-table driver table))
+ (catch Exception e
+ (log/warnf "Failed to drop transform test temp table %s: %s" table (ex-message e)))))
+
+(mu/defn table-columns :- [:sequential ::transform-testing.schema/result-column]
+ "The columns of the temp table `table` on `conn`, in order, fetching no rows."
+ [driver :- :keyword
+ conn :- ::transform-testing.schema/connection
+ table :- :string]
+ (let [{:keys [query params]} (transform-testing.compile/columns-query driver table)]
+ (:columns (driver/query-on-connection driver conn [query params] {:max-rows 1}))))
+
+(mu/defn run-query
+ "Run the compiled `[sql params]` `query` on `conn`, returning `{:rows :columns}`."
+ [driver :- :keyword
+ conn :- ::transform-testing.schema/connection
+ query :- [:tuple :string [:maybe [:sequential ::lib.schema.common/field-value]]]
+ max-rows :- :int]
+ (driver/query-on-connection driver conn query {:max-rows max-rows}))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/empty.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/empty.clj
new file mode 100644
index 00000000000..8398977c887
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/empty.clj
@@ -0,0 +1,70 @@
+(ns metabase-enterprise.transform-testing.expectations.empty
+ "The `empty` expectation: a query over the transform output returns no rows.
+
+ The query is the author's own SQL, rewritten so the tables it names resolve to the run's temp
+ tables rather than to the real ones."
+ (:require
+ [metabase-enterprise.transform-testing.compile :as transform-testing.compile]
+ [metabase-enterprise.transform-testing.expectations.protocol :as expectations.protocol]
+ [metabase-enterprise.transform-testing.expectations.report :as expectations.report]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase.util.malli :as mu]
+ [metabase.util.malli.registry :as mr]))
+
+(set! *warn-on-reflection* true)
+
+;;; ---------------------------------------------- Result schema -----------------------------------------
+
+(def ^:private base
+ "The shared result keys, pinned to this type."
+ [:merge
+ ::transform-testing.schema/expectation-result.base
+ [:map [:type [:= :empty]]]])
+
+(def ^:private findings
+ [:merge
+ base
+ [:map {:description "The rows the query returned, and the columns describing them."}
+ [:columns [:sequential ::transform-testing.schema/result-column]]
+ [:sample [:sequential ::transform-testing.schema/row]]
+ [:truncated :int]]])
+
+(mr/def ::result
+ "What an `empty` expectation found.
+
+ A pass carries nothing beyond the shared keys. A failure adds the rows the query returned as
+ `:sample`, with `:columns` describing them; the sample is capped, and `:truncated` is how many
+ rows that cap dropped."
+ [:multi {:dispatch :status}
+ [:error base]
+ [:passed base]
+ [:failed findings]])
+
+(defrecord Empty [type name sql]
+ expectations.protocol/Expectation
+ (temp-tables [_this _context]
+ {})
+
+ (probes [_this {:keys [driver replacements]}]
+ {:violations {:query (transform-testing.compile/replace-tables driver sql replacements)
+ :params []
+ :max-rows expectations.report/row-cap}})
+
+ (interpret [_this _context results]
+ (let [{:keys [rows columns]} (:violations results)
+ rows (vec rows)
+ [sample dropped] (expectations.report/capped rows)
+ column-names (mapv :name columns)]
+ (cond-> {:name name :type :empty :status (if (empty? rows) :passed :failed)}
+ (seq rows)
+ (assoc :sample (mapv #(zipmap column-names (map expectations.report/cell %)) sample)
+ :columns (vec columns)
+ :truncated dropped)))))
+
+(defmethod expectations.protocol/build :empty
+ [m]
+ (mu/validate-throw ::transform-testing.schema/expectation m)
+ ;; The linter forbids this constructor everywhere, so that an expectation is only ever built from a
+ ;; value the schema has passed. This is the one place it is the right call.
+ #_{:clj-kondo/ignore [:discouraged-var]}
+ (->Empty :empty (:name m) (:sql m)))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/equals.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/equals.clj
new file mode 100644
index 00000000000..0b22c5ef039
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/equals.clj
@@ -0,0 +1,211 @@
+(ns metabase-enterprise.transform-testing.expectations.equals
+ "The `equals` expectation: the transform output holds exactly the declared rows, over exactly the
+ declared columns.
+
+ Only the declared columns are compared, so a column the expectation does not name — a `NOW()`
+ timestamp, say — never enters the query. It also means `equals` says nothing about a column the
+ output has and the expectation does not."
+ (:require
+ [clojure.string :as str]
+ [metabase-enterprise.transform-testing.compile :as transform-testing.compile]
+ [metabase-enterprise.transform-testing.errors :as transform-testing.errors]
+ [metabase-enterprise.transform-testing.expectations.protocol :as expectations.protocol]
+ [metabase-enterprise.transform-testing.expectations.report :as expectations.report]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase.util.i18n :refer [tru]]
+ [metabase.util.malli :as mu]
+ [metabase.util.malli.registry :as mr]))
+
+(set! *warn-on-reflection* true)
+
+;;; ---------------------------------------------- Result schema -----------------------------------------
+
+(def ^:private cell
+ "A value as a failure report carries it: a string, a number, a boolean, or null."
+ [:maybe [:or :boolean number? :string]])
+
+(def ^:private base
+ "The shared result keys, pinned to this type."
+ [:merge
+ ::transform-testing.schema/expectation-result.base
+ [:map [:type [:= :equals]]]])
+
+(def ^:private findings
+ [:merge
+ base
+ [:map {:description "The comparison's findings, reported in full on a pass as well as a failure."}
+ [:columns [:sequential ::transform-testing.schema/result-column]]
+ [:row-counts [:map {:closed true}
+ [:actual :int]
+ [:expected :int]]]
+ [:extra-rows [:sequential ::transform-testing.schema/row]]
+ [:missing-rows [:sequential ::transform-testing.schema/row]]
+ [:cell-mismatches [:sequential [:map {:closed true}
+ [:column :string]
+ [:expected cell]
+ [:actual cell]]]]
+ [:truncated :int]]])
+
+(mr/def ::result
+ "What an `equals` expectation found.
+
+ Unless `:status` is `error`, every key is present on a pass as well as a failure:
+ `:extra-rows`, `:missing-rows` and `:cell-mismatches` come back empty rather than missing.
+ `:row-counts` sets the output's row count against the number of rows the expectation declared.
+ `:cell-mismatches` is filled only when exactly one row is missing and exactly one is extra.
+ `:extra-rows` and `:missing-rows` are capped; `:truncated` is how many rows that cap dropped."
+ [:multi {:dispatch :status}
+ [:error base]
+ [:passed findings]
+ [:failed findings]])
+
+;;; --------------------------------------------- Reporting ---------------------------------------------
+
+(defn- cell-mismatches
+ "The cells that differ between the one missing row and the one extra row, or nil when the diff is
+ not one-for-one.
+
+ Only the unambiguous case is paired. Sorting both sides and pairing positionally for a larger
+ diff produces output that reads as authoritative and is arbitrary."
+ [column-names [missing :as missing-rows] [extra :as extra-rows]]
+ (when (= 1 (count missing-rows) (count extra-rows))
+ (not-empty
+ (vec (for [c column-names
+ :let [e (get missing c) a (get extra c)]
+ :when (not= e a)]
+ {:column c :expected e :actual a})))))
+
+;;; ---------------------------------------- Resolving declared columns ----------------------------------
+
+(defn- resolve-column
+ "The output table's own spelling of `declared-name`.
+
+ The author writes the name as it appears in the transform's SQL; the warehouse stores whatever its
+ folding rules produced, which for the same unquoted identifier is upper case on H2 and Snowflake
+ and lower on Postgres. Resolving against the materialized table means the comparison names a
+ column the table actually has — and, since the result is always one of the output's own names, no
+ author-supplied string ever reaches the SQL as an identifier."
+ [expectation-name output-columns declared-name]
+ (let [folded (filter #(.equalsIgnoreCase ^String % declared-name) output-columns)]
+ (or (first (filter #(= % declared-name) output-columns))
+ (when (= 1 (count folded)) (first folded))
+ (if (seq folded)
+ ;; Several columns differ from each other only in case, so folding cannot choose between
+ ;; them. Reporting this as "not found" would send the author hunting for a typo while
+ ;; listing columns that plainly match.
+ (throw (transform-testing.errors/ex
+ ::transform-testing.errors/ambiguous-column
+ (tru "Expectation {0} names column {1}, which matches more than one output column: {2}. Write it exactly as the output spells it."
+ (pr-str expectation-name) (pr-str declared-name)
+ (str/join ", " folded))
+ {:expectation expectation-name
+ :column declared-name
+ :candidates (vec folded)}))
+ (throw (transform-testing.errors/ex
+ ::transform-testing.errors/unknown-column
+ (tru "Expectation {0} compares column {1}, which the transform output does not have. It has: {2}"
+ (pr-str expectation-name) (pr-str declared-name)
+ (str/join ", " output-columns))
+ {:expectation expectation-name
+ :column declared-name
+ :available (vec output-columns)}))))))
+
+(defn resolve-columns
+ "The output table's own spelling of each column `expectation` declares, in declared order.
+
+ Only the SQL uses these. A failure reports the column under the name the author wrote, which is
+ what they can find in their own test."
+ [{:keys [name columns]} output-columns]
+ (let [output-names (mapv :name output-columns)]
+ (mapv #(resolve-column name output-names (:name %)) columns)))
+
+;;; ----------------------------------------------- Types -----------------------------------------------
+
+(defrecord EqualsRows [type name format columns rows]
+ expectations.protocol/Expectation
+ (temp-tables [this {:keys [driver output-columns]}]
+ {"expected" (transform-testing.compile/rows-query driver columns (resolve-columns this output-columns) rows)})
+
+ (probes [this {:keys [driver output-table output-columns temp-tables]}]
+ {:comparison (assoc (transform-testing.compile/comparison-query driver output-table (get temp-tables "expected")
+ (resolve-columns this output-columns))
+ :max-rows (inc expectations.report/row-cap))
+ :actual-count (assoc (transform-testing.compile/row-count-query driver output-table) :max-rows 1)})
+
+ (interpret [this {:keys [output-columns]} results]
+ (let [column-names (mapv :name columns)
+ ;; A comparison row is the declared columns followed by the signed multiplicity: how many
+ ;; times too often the row appears in the output (+) or in the expectation (-).
+ diffs (for [row (:rows (:comparison results))]
+ [(long (last row))
+ (zipmap column-names (map expectations.report/cell (butlast row)))])
+ expand (fn [keep?]
+ ;; Take inside the expansion, not after it. A single comparison row can
+ ;; carry a multiplicity of millions, and realizing that many copies just to
+ ;; drop all but `row-cap` of them turns a failed assertion into a memory
+ ;; event. The transducer short-circuits, so `repeat` is never realized past
+ ;; the cap; the dropped count comes from arithmetic rather than counting.
+ (let [matching (filterv (comp keep? first) diffs)
+ total (reduce + 0 (map (comp abs first) matching))
+ rows (into [] (comp (mapcat (fn [[delta cells]]
+ (repeat (abs delta) cells)))
+ (take expectations.report/row-cap))
+ matching)]
+ [rows (- total (count rows))]))
+ [extra extra-dropped] (expand pos?)
+ [missing missing-dropped] (expand neg?)
+ actual-count (some-> results :actual-count :rows ffirst long)
+ ;; name -> the output table's type for each column
+ output-type (into {} (map (juxt :name :database_type)) output-columns)
+ reported (mapv (fn [declared resolved]
+ {:name declared :database_type (output-type resolved)})
+ column-names
+ (resolve-columns this output-columns))]
+ ;; Every key is always present: a consumer should not have to tell nil from [].
+ (cond-> {:name name
+ :type :equals
+ :status (if (and (empty? extra) (empty? missing)) :passed :failed)
+ :columns reported
+ :row-counts {:actual (or actual-count 0) :expected (count rows)}
+ :extra-rows extra
+ :missing-rows missing
+ :truncated (+ extra-dropped missing-dropped)
+ :cell-mismatches []}
+
+ (cell-mismatches column-names missing extra)
+ (assoc :cell-mismatches (cell-mismatches column-names missing extra))))))
+
+(defn- not-implemented [expectation-name]
+ (transform-testing.errors/ex
+ ::transform-testing.errors/unsupported-format
+ (tru "Expectation {0} compares against a SQL query, which is not implemented yet. Give ''columns'' and ''rows'' instead."
+ (pr-str expectation-name))
+ {:expectation expectation-name :format :sql}))
+
+(defrecord EqualsSql [type name format sql]
+ expectations.protocol/Expectation
+ ;; The form is accepted at authoring time and refused when it runs. Refusing it at construction
+ ;; would be the friendlier place, but reading is construction here, so it would also make every
+ ;; already-stored test of this shape unreadable — including the one upstream's serdes round-trip
+ ;; carries. Until the form is implemented, the refusal rides back on the expectation itself,
+ ;; naming it, while its siblings still report.
+ (temp-tables [_this _context]
+ (throw (not-implemented name)))
+ (probes [_this _context]
+ (throw (not-implemented name)))
+ (interpret [_this _context _results]
+ (throw (not-implemented name))))
+
+;;; --------------------------------------------- Building ----------------------------------------------
+
+(defmethod expectations.protocol/build :equals
+ [{:keys [format] :as m}]
+ (mu/validate-throw ::transform-testing.schema/expectation m)
+ ;; The linter forbids these constructors everywhere, so that an expectation is only ever built from a
+ ;; value the schema has passed. This is the one place they are the right call.
+ #_{:clj-kondo/ignore [:discouraged-var]}
+ (case format
+ ;; A declared `cast_type` is not checked here, nor anywhere else: `compile/rows-query` quotes
+ ;; a cast target that is not a plain type name, leaving the database to refuse it.
+ :rows (->EqualsRows :equals (:name m) :rows (:columns m) (:rows m))
+ :sql (->EqualsSql :equals (:name m) :sql (:sql m))))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/protocol.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/protocol.clj
new file mode 100644
index 00000000000..862d3824b26
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/protocol.clj
@@ -0,0 +1,44 @@
+(ns metabase-enterprise.transform-testing.expectations.protocol
+ "The protocol every expectation type implements, and the multimethod that builds one.
+
+ A type needing more than one round of probes is not precluded, but every round has to run inside
+ the test connection, while the temp tables still exist."
+ (:require
+ [metabase-enterprise.transform-testing.errors :as transform-testing.errors]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase.util.i18n :refer [tru]]
+ [metabase.util.malli :as mu]))
+
+(set! *warn-on-reflection* true)
+
+(defprotocol Expectation
+ "One check on the output of the transform under test."
+ (temp-tables [this context]
+ "The queries this expectation needs materialized before its probes run, as `{id compiled-query}`.
+
+ The runner creates each one as a temp table on the test connection and passes their names to [[probes]] as
+ `:temp-tables` in its context, `{id table}`. Pure.")
+ (probes [this context]
+ "The queries this expectation needs, as `{probe-id {:query :params :max-rows}}`.
+
+ `context` carries the driver, the name of the output temp table, the columns that table actually
+ has, the input-table replacements, and this expectation's own [[temp-tables]] by name. Pure.")
+ (interpret [this context results]
+ "This expectation's result map, given `context` and `{probe-id {:rows :columns}}` from the probes.
+
+ Always carries `:name`, `:type` and `:status`; what else it says about a failure is the type's
+ own business. Pure."))
+
+(defmulti build
+ "The validated record for an already-normalized `m`, of the appropriate expectation type."
+ {:arglists '([m])}
+ (comp keyword :type))
+
+(defmethod build :default
+ [m]
+ (mu/validate-throw ::transform-testing.schema/expectation m)
+ (throw (transform-testing.errors/ex
+ ::transform-testing.errors/unsupported-format
+ (tru "Expectation {0} is of type {1}, which is not implemented yet."
+ (pr-str (:name m)) (pr-str (:type m)))
+ {:expectation (:name m) :type (:type m)})))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/report.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/report.clj
new file mode 100644
index 00000000000..8ff5f871e19
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/expectations/report.clj
@@ -0,0 +1,31 @@
+(ns metabase-enterprise.transform-testing.expectations.report
+ "Turning warehouse values into something a failure report can carry.
+
+ Shared by every expectation type, so it sits below them rather than in the front door."
+ (:import
+ (java.math BigDecimal)))
+
+(set! *warn-on-reflection* true)
+
+(def row-cap
+ "How many rows of any one failure list are reported. The rest are counted as truncated."
+ 50)
+
+(defn cell
+ "`v` as something JSON can carry.
+
+ A BigDecimal keeps its scale: when scale is the difference between expected and actual,
+ normalizing it away makes the report deny the very thing it is reporting."
+ [v]
+ (cond
+ (nil? v) nil
+ (instance? BigDecimal v) (.toPlainString ^BigDecimal v)
+ (number? v) v
+ (boolean? v) v
+ (string? v) v
+ :else (str v)))
+
+(defn capped
+ "`[first-n dropped]` — `rows` cut to [[row-cap]], and how many that lost."
+ [rows]
+ [(vec (take row-cap rows)) (max 0 (- (count rows) row-cap))])
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/init.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/init.clj
new file mode 100644
index 00000000000..aa62d78e8ed
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/init.clj
@@ -0,0 +1,4 @@
+(ns metabase-enterprise.transform-testing.init
+ (:require
+ [metabase-enterprise.transform-testing.api]
+ [metabase-enterprise.transform-testing.models]))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/models.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/models.clj
new file mode 100644
index 00000000000..566a588e99a
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/models.clj
@@ -0,0 +1,102 @@
+(ns metabase-enterprise.transform-testing.models
+ (:require
+ [metabase-enterprise.transform-testing.db :as transform-testing.db]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase.events.core :as events]
+ [metabase.lib.core :as lib]
+ [metabase.models.interface :as mi]
+ [metabase.models.serialization :as serdes]
+ [metabase.premium-features.core :refer [defenterprise]]
+ [metabase.util.malli :as mu]
+ [methodical.core :as methodical]
+ [toucan2.core :as t2]))
+
+(methodical/defmethod t2/table-name :model/TransformTest [_model] :transform_test)
+
+(doto :model/TransformTest
+ (derive :metabase/model)
+ (derive :hook/entity-id)
+ (derive :hook/timestamped?))
+
+(defn- json-column
+ "A Toucan transform storing a value of `schema` as JSON, normalized and validated on its way in and normalized on its
+ way out."
+ [schema]
+ {:in (comp mi/json-in #(mu/validate-throw schema %) #(lib/normalize schema %))
+ :out (comp #(lib/normalize schema %) mi/json-out-with-keywordization)})
+
+(methodical/defmethod t2/model-for-automagic-hydration [:model/TransformTest :transform]
+ [_original-model _k]
+ :model/Transform)
+
+(t2/deftransforms :model/TransformTest
+ {:inputs (json-column ::transform-testing.schema/inputs)
+ :expectations (json-column ::transform-testing.schema/expectations)})
+
+;;; ------------------------------------------------- Permissions --------------------------------------------------
+
+(defmethod mi/can-read? :model/TransformTest
+ ([instance]
+ (mi/can-read? :model/Transform (:transform_id instance)))
+ ([_model pk]
+ (when-let [transform-test (transform-testing.db/transform-test pk)]
+ (mi/can-read? transform-test))))
+
+(defmethod mi/can-write? :model/TransformTest
+ ([instance]
+ (mi/can-write? :model/Transform (:transform_id instance)))
+ ([_model pk]
+ (when-let [transform-test (transform-testing.db/transform-test pk)]
+ (mi/can-write? transform-test))))
+
+(defmethod mi/can-create? :model/TransformTest
+ [_model instance]
+ (mi/can-write? :model/Transform (:transform_id instance)))
+
+;;; ---------------------------------------------------- Events ----------------------------------------------------
+
+(t2/define-after-insert :model/TransformTest [transform-test]
+ (when-not mi/*deserializing?*
+ (events/publish-event! :event/transform-test-create {:object transform-test}))
+ transform-test)
+
+(t2/define-after-update :model/TransformTest [transform-test]
+ (when-not mi/*deserializing?*
+ (events/publish-event! :event/transform-test-update {:object transform-test}))
+ transform-test)
+
+(defenterprise delete-transform-tests!
+ "Enterprise implementation: delete the tests of the transform `transform-id`, each through its own hook. Runs
+ whatever the token says, since the rows outlive the feature. See the OSS declaration in
+ `metabase.transforms.models.transform`."
+ :feature :none
+ [transform-id]
+ (transform-testing.db/delete-transform-tests! transform-id))
+
+(t2/define-before-delete :model/TransformTest [transform-test]
+ (when-not mi/*deserializing?*
+ (events/publish-event! :event/transform-test-delete {:object transform-test}))
+ transform-test)
+
+;;; ------------------------------------------------- Serialization ------------------------------------------------
+
+(defmethod serdes/make-spec "TransformTest"
+ [_model-name _opts]
+ {:copy [:entity_id :name :description :inputs :expectations]
+ :transform {:created_at (serdes/date)
+ :transform_id (serdes/fk :model/Transform)
+ :creator_id (serdes/fk :model/User)}})
+
+(defmethod serdes/deserialization-dependencies "TransformTest"
+ [{:keys [transform_id]}]
+ #{[{:model "Transform" :id transform_id}]})
+
+(defmethod serdes/storage-path "TransformTest" [transform-test ctx]
+ (let [{:keys [transform]} (t2/hydrate (transform-testing.db/transform-test-by-entity-id (:entity_id transform-test))
+ :transform)]
+ (conj (serdes/storage-path {:serdes/meta [{:model "Transform" :id (:entity_id transform)}]
+ :name (:name transform)
+ :entity_id (:entity_id transform)
+ :collection_id (serdes/*export-fk* (:collection_id transform) :model/Collection)}
+ ctx)
+ {:label (:name transform-test) :key (:entity_id transform-test)})))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/runner.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/runner.clj
new file mode 100644
index 00000000000..d2d26320edc
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/runner.clj
@@ -0,0 +1,261 @@
+(ns metabase-enterprise.transform-testing.runner
+ "Orchestrates a transform test run over the module's separated concerns. The runner is the only
+ namespace that resolves from the app db or owns the connection lifecycle; it hands ordinary
+ arguments to each concern (no marshaled plan object) and owns the HTTP decisions.
+
+ db — app-db reads (transform, database)
+ compile — pure: source → SQL + referenced tables (once), then rewrites to temp tables
+ validator — pure: may this test run? every read faked, every rewritten query on temp tables
+ executor — the one place warehouse I/O lives: create/drop temp tables, run read-backs
+ expectations — check each expectation against the output temp table (via executor)
+
+ Flow — compile the source once, then thread that one result so the guard and the rewrite agree:
+
+ resolve (app-db)
+ → compile-source (pure) : the transform's SQL + the tables it reads
+ → name + rewrite (pure) : a temp table per input, and the source reading them
+ → validate (pure) : reject a test that is incomplete, or whose queries would still
+ read a real table
+ → run (executor) : one connection — create the temp tables, check, drop on exit
+
+ Everything before the connection is pure; a bad test is rejected before any temp table exists."
+ (:require
+ [metabase-enterprise.transform-testing.compile :as transform-testing.compile]
+ [metabase-enterprise.transform-testing.db :as transform-testing.db]
+ [metabase-enterprise.transform-testing.errors :as transform-testing.errors]
+ [metabase-enterprise.transform-testing.executor :as transform-testing.executor]
+ [metabase-enterprise.transform-testing.expectations.empty]
+ [metabase-enterprise.transform-testing.expectations.equals]
+ [metabase-enterprise.transform-testing.expectations.protocol :as expectations.protocol]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase-enterprise.transform-testing.validator :as transform-testing.validator]
+ [metabase.api.common :as api]
+ [metabase.driver :as driver]
+ [metabase.driver.sql.normalize :as sql.normalize]
+ [metabase.driver.util :as driver.u]
+ [metabase.lib.schema.id :as lib.schema.id]
+ [metabase.sql-parsing.core :as sql-parsing]
+ [metabase.transforms-base.util :as transforms-base.u]
+ [metabase.util.i18n :refer [tru]]
+ [metabase.util.malli :as mu]
+ [metabase.util.malli.registry :as mr]
+ [metabase.warehouses.schema :as warehouses.schema]))
+
+(set! *warn-on-reflection* true)
+
+;;; Required for the [[expectations.protocol/build]] methods and result schemas each type registers.
+(comment
+ metabase-enterprise.transform-testing.expectations.empty/keep-me
+ metabase-enterprise.transform-testing.expectations.equals/keep-me)
+
+(mr/def ::plan
+ "What [[validated-plan]] hands the run that follows it."
+ [:map {:closed true}
+ [:driver :keyword]
+ [:database ::warehouses.schema/database]
+ [:input->table [:map-of ::transform-testing.schema/input :string]]
+ [:output-table :string]
+ [:labels [:map-of :string :string]]
+ [:replacements ::transform-testing.compile/table-replacements]
+ [:compiled ::transform-testing.compile/compiled-query]])
+
+(defn- table-labels
+ "The map from each temp table of the run to the table the author wrote, for rewriting warehouse error messages that
+ can only speak in generated names."
+ [transform input->table output-table]
+ (into {output-table (transform-testing.validator/table-label (select-keys (:target transform) [:schema :name]))}
+ (map (fn [[input table]] [table (transform-testing.validator/table-label (:table input))]))
+ input->table))
+
+(defn- warehouse-said
+ "What the warehouse told us about `e`, with every generated temp-table name replaced by the one
+ the author wrote, or nil when it said nothing."
+ [e labels]
+ (transform-testing.errors/remap-message (ex-message e) labels))
+
+(defn- rethrow-remapped
+ "Rethrow `e` as `error-type` behind `message`, quoting the warehouse where it had something to say."
+ [e error-type message labels]
+ (let [cause (warehouse-said e labels)]
+ (throw (transform-testing.errors/ex
+ error-type
+ (if cause (str message " " cause) message)
+ {:cause cause}
+ e))))
+
+(defn- run-probes!
+ "Run one expectation's probes on `conn`, returning `{probe-id rows}`."
+ [driver conn probes]
+ (update-vals probes
+ (fn [{:keys [query params max-rows]}]
+ (transform-testing.executor/run-query driver conn [query (vec params)] max-rows))))
+
+(defn- check-expectation
+ "The result map for one expectation. An expectation whose own query fails is reported as an error
+ on that expectation rather than failing the whole run. The temp tables it needs are created
+ before its probes and dropped after them."
+ [driver conn context expectation labels]
+ (let [temp-tables (atom {})]
+ (try
+ (doseq [[id query] (expectations.protocol/temp-tables expectation context)]
+ (let [table (driver/temp-table-name driver)]
+ (swap! temp-tables assoc id table)
+ (transform-testing.executor/create-temp-table! driver conn table query)))
+ (->> (expectations.protocol/probes expectation (assoc context :temp-tables @temp-tables))
+ (run-probes! driver conn)
+ (expectations.protocol/interpret expectation context))
+ (catch Exception e
+ {:name (:name expectation)
+ :type (:type expectation)
+ :status :error
+ :error {:type (or (:error-type (ex-data e))
+ ::transform-testing.errors/expectation-failed)
+ :message (or (warehouse-said e labels)
+ (tru "The expectation could not be run, and the database gave no reason."))}})
+ (finally
+ (doseq [table (vals @temp-tables)]
+ (transform-testing.executor/drop-temp-table! driver conn table))))))
+
+(defn- testable-transform
+ "The transform `transform-id` names, if it is one a test can run."
+ [transform-id]
+ (let [transform (api/check-404 (transform-testing.db/transform transform-id))]
+ (when-not (transforms-base.u/query-transform? transform)
+ (throw (transform-testing.errors/ex
+ ::transform-testing.errors/unsupported-transform
+ (tru "Only query transforms can be tested.")
+ {:transform-id transform-id})))
+ transform))
+
+(defn- testable-database
+ "The database `transform` reads, if its driver can run a transform test."
+ [transform]
+ (let [database (api/check-404 (transform-testing.db/database
+ (transforms-base.u/transform-source-database transform)))
+ driver (keyword (:engine database))]
+ (when-not (driver.u/supports? driver :transforms/testing database)
+ (throw (transform-testing.errors/ex
+ ::transform-testing.errors/unsupported-driver
+ (tru "The {0} database of this transform does not support transform testing."
+ (name driver))
+ {:driver driver})))
+ database))
+
+(defn- parsed-source
+ "The transform's source compiled to SQL, with the tables it reads, or a refusal naming what would not parse."
+ [driver transform transform-id]
+ (try
+ (transform-testing.compile/compile-source driver transform)
+ (catch Exception e
+ (if (sql-parsing/parse-error? e)
+ (throw (transform-testing.errors/ex
+ ::transform-testing.errors/unparseable-source
+ (tru "The transform source SQL could not be parsed for test input validation: {0}"
+ (sql-parsing/parse-error-message e))
+ {:transform-id transform-id}))
+ (throw e)))))
+
+(defn- create-inputs!
+ "Materialize each input of `input->table` as its temp table on `conn`."
+ [driver conn input->table labels]
+ (doseq [[input table] input->table]
+ (try
+ (transform-testing.executor/create-temp-table!
+ driver conn table (transform-testing.compile/compile-input driver input))
+ (catch Exception e
+ (rethrow-remapped e ::transform-testing.errors/setup-failed
+ (tru "Could not create the test input for {0}:"
+ (transform-testing.validator/table-label (:table input)))
+ labels)))))
+
+(defn- create-output!
+ "Run the transform under test on `conn`, materializing its output as the temp table `output-table`."
+ [driver conn output-table compiled-transform labels]
+ (try
+ (transform-testing.executor/create-temp-table! driver conn output-table compiled-transform)
+ (catch Exception e
+ (rethrow-remapped e ::transform-testing.errors/transform-failed
+ (tru "The transform under test failed to run:")
+ labels))))
+
+(defn- check-expectations
+ "The result of every expectation against the output temp table, passing ones included."
+ [driver conn context expectations labels]
+ (let [context (assoc context
+ :output-columns
+ (transform-testing.executor/table-columns driver conn (:output-table context)))]
+ (mapv (fn [expectation]
+ (check-expectation driver conn context (expectations.protocol/build expectation) labels))
+ expectations)))
+
+(mu/defn- validated-plan :- ::plan
+ "Everything a run needs, resolved and checked but not yet executed: the database and driver the transform runs
+ against, a temp table per input and one for its output, the source rewritten to read them, and the labels a
+ message renders them as.
+
+ Throws the run's typed refusals — an untestable transform or driver, SQL that will not parse, a test the
+ validator rejects — so a caller that only wants the answer to \"could this run?\" gets it by asking for the plan
+ and discarding it."
+ [transform-id :- ::lib.schema.id/transform
+ inputs :- ::transform-testing.schema/inputs
+ expectations :- ::transform-testing.schema/expectations]
+ (let [transform (testable-transform transform-id)
+ database (testable-database transform)
+ driver (keyword (:engine database))
+ source (parsed-source driver transform transform-id)
+ input->table (into {} (map (fn [input] [input (driver/temp-table-name driver)])) inputs)
+ output-table (driver/temp-table-name driver)
+ default-schema (sql.normalize/default-schema driver database)
+ replacements (transform-testing.compile/table-replacements transform input->table output-table default-schema)
+ compiled (transform-testing.compile/compile-transform driver source replacements)]
+ (transform-testing.validator/validate
+ driver
+ {:inputs inputs
+ :expectations expectations
+ :referenced-tables (:referenced-tables source)
+ :rewritten-transform (:query compiled)
+ :replacements replacements
+ :default-schema default-schema})
+ {:driver driver
+ :database database
+ :input->table input->table
+ :output-table output-table
+ :labels (table-labels transform input->table output-table)
+ :replacements replacements
+ :compiled compiled}))
+
+(mu/defn validate-transform-test :- :nil
+ "Refuse a test of the transform `transform-id` with these `inputs` and `expectations` unless it could run: the same
+ checks a run makes, up to but not including the connection.
+
+ Returns nil when there is nothing to refuse, and otherwise throws the refusal the run would have thrown, so a test
+ is turned away as it is authored rather than only when somebody runs it."
+ [transform-id :- ::lib.schema.id/transform
+ inputs :- ::transform-testing.schema/inputs
+ expectations :- ::transform-testing.schema/expectations]
+ (validated-plan transform-id inputs expectations)
+ nil)
+
+(mu/defn run-transform-test! :- ::transform-testing.schema/run-result
+ "Run the transform test `transform-test` against temp tables and report what each expectation found.
+
+ Throws a typed refusal from [[metabase-enterprise.transform-testing.errors]] when the run cannot happen. A
+ failing expectation is not a refusal: it rides back as that expectation's own result."
+ [{:keys [transform_id inputs expectations]} :- ::transform-testing.schema/transform-test]
+ (let [{:keys [driver database input->table output-table labels replacements compiled]}
+ (validated-plan transform_id inputs expectations)]
+ (driver/do-with-test-connection
+ driver database
+ (fn [conn]
+ (try
+ (create-inputs! driver conn input->table labels)
+ (create-output! driver conn output-table compiled labels)
+ (let [results (check-expectations driver conn
+ {:driver driver :output-table output-table :replacements replacements}
+ expectations labels)]
+ {:status (if (every? #(= :passed (:status %)) results) :passed :failed)
+ :expectations results
+ :tables labels})
+ (finally
+ (doseq [table (cons output-table (vals input->table))]
+ (transform-testing.executor/drop-temp-table! driver conn table))))))))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/schema.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/schema.clj
new file mode 100644
index 00000000000..93d3f19eff0
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/schema.clj
@@ -0,0 +1,216 @@
+(ns metabase-enterprise.transform-testing.schema
+ "The shapes a transform test is written in, and the only gate they pass: each schema explains its own
+ refusals, so a caller reads what to fix from the explanation rather than from a hand-written check."
+ (:require
+ [clojure.string :as str]
+ [malli.core :as mc]
+ [metabase.lib.schema.common :as lib.schema.common]
+ [metabase.lib.schema.id :as lib.schema.id]
+ [metabase.util.malli.registry :as mr]
+ [metabase.util.malli.schema :as ms]))
+
+(defn- unknown-dispatch
+ "An `:error/fn` for a `:multi` dispatching on `dispatch-key`: says what the value named and what the schema
+ knows, where Malli would say only \"invalid dispatch value\"."
+ [dispatch-key]
+ (fn [{:keys [schema value]} _options]
+ (let [known (str/join ", " (sort (map (comp name first) (mc/children schema))))]
+ (if (map? value)
+ (str "unknown " (name dispatch-key) " " (pr-str (get value dispatch-key)) ", must be one of: " known)
+ (str "must be a map whose " (name dispatch-key) " is one of: " known)))))
+
+(defn- rows-match-columns?
+ "Whether every row of a `rows` value carries a value for each declared column and none besides, which is nothing to
+ answer when no column is declared -- the columns have their own schema to fail."
+ [{:keys [columns rows]}]
+ (let [declared (into #{} (map :name) columns)]
+ (or (empty? declared)
+ (every? (fn [row] (and (map? row) (= declared (set (keys row))))) rows))))
+
+(defn- rows-match-columns-message
+ "What the first row that does not carry exactly its declared columns got wrong."
+ [{{:keys [columns rows]} :value} _options]
+ (let [declared (into #{} (map :name) columns)
+ offender (first (remove (fn [row] (and (map? row) (= declared (set (keys row))))) rows))
+ missing (sort (remove (set (keys offender)) declared))
+ extra (sort (remove declared (keys offender)))]
+ (str "every row must carry exactly the declared columns"
+ (when (seq missing) (str "; missing " (str/join ", " (map pr-str missing))))
+ (when (seq extra) (str "; not declared: " (str/join ", " (map pr-str extra)))))))
+
+(mr/def ::table
+ "A table read by the transform under test, by schema and name."
+ [:map {:closed true, :decode/normalize lib.schema.common/normalize-map-no-kebab-case}
+ [:schema {:optional true} [:maybe ::lib.schema.common/non-blank-string]]
+ [:name ::lib.schema.common/non-blank-string]])
+
+(mr/def ::column
+ "A column of inline test data, with the type its cells are cast to.
+
+ A cast target, which on some engines is not a type name the warehouse ever reports: MySQL takes
+ `SIGNED` and reports `BIGINT`."
+ [:map {:closed true, :decode/normalize lib.schema.common/normalize-map-no-kebab-case}
+ [:name ::lib.schema.common/non-blank-string]
+ [:cast_type ::lib.schema.common/non-blank-string]])
+
+(mr/def ::result-column
+ "A column reported by a test run, with its database native type."
+ [:map {:closed true}
+ [:name ::lib.schema.common/non-blank-string]
+ [:database_type ::lib.schema.common/non-blank-string]])
+
+(mr/def ::row
+ "A row of inline test data, keyed by column name."
+ (ms/string-keyed-map [:maybe [:or :boolean number? :string]]))
+
+(mr/def ::sql-data
+ "Test data returned by a SQL query run against the transform's source database."
+ [:map {:closed true, :decode/normalize lib.schema.common/normalize-map-no-kebab-case}
+ [:format {:decode/normalize lib.schema.common/normalize-keyword} [:= :sql]]
+ [:sql ::lib.schema.common/non-blank-string]])
+
+(mr/def ::rows-data
+ "Test data written out in the request: the columns, with the type each is cast to, and the rows.
+
+ A row's keys are the declared column names, exactly: the query reads each row by those names, so a key that names
+ no column would be dropped and a column a row leaves out would become a null cell, neither of which the author
+ asked for."
+ [:and
+ [:map {:closed true, :decode/normalize lib.schema.common/normalize-map-no-kebab-case}
+ [:format {:decode/normalize lib.schema.common/normalize-keyword} [:= :rows]]
+ [:columns [:sequential {:min 1} ::column]]
+ [:rows [:sequential ::row]]]
+ [:fn {:error/fn rows-match-columns-message} rows-match-columns?]])
+
+(mr/def ::input
+ "An input table of the transform under test, replaced with test data."
+ [:multi {:decode/normalize lib.schema.common/normalize-map-no-kebab-case
+ :dispatch (comp keyword :format)
+ :error/fn (unknown-dispatch :format)}
+ [:sql [:merge [:map {:closed true, :decode/normalize lib.schema.common/normalize-map-no-kebab-case} [:table ::table]] ::sql-data]]
+ [:rows [:merge [:map {:closed true, :decode/normalize lib.schema.common/normalize-map-no-kebab-case} [:table ::table]] ::rows-data]]])
+
+(mr/def ::inputs
+ "Test data for the tables the transform reads. Every table it reads needs one, and an input for
+ a table it does not read is refused."
+ [:sequential ::input])
+
+(mr/def ::expectation.equals
+ "A check that the transform output holds exactly the declared rows, over exactly the declared columns."
+ [:multi {:decode/normalize lib.schema.common/normalize-map-no-kebab-case
+ :dispatch (comp keyword :format)
+ :error/fn (unknown-dispatch :format)}
+ [:sql [:merge
+ [:map {:closed true, :decode/normalize lib.schema.common/normalize-map-no-kebab-case}
+ [:type {:decode/normalize lib.schema.common/normalize-keyword} [:= :equals]]
+ [:name ::lib.schema.common/non-blank-string]]
+ ::sql-data]]
+ [:rows [:merge
+ [:map {:closed true, :decode/normalize lib.schema.common/normalize-map-no-kebab-case}
+ [:type {:decode/normalize lib.schema.common/normalize-keyword} [:= :equals]]
+ [:name ::lib.schema.common/non-blank-string]]
+ ::rows-data]]])
+
+(mr/def ::expectation.empty
+ "A check that a SQL query over the transform output returns no rows."
+ [:map {:closed true, :decode/normalize lib.schema.common/normalize-map-no-kebab-case}
+ [:type {:decode/normalize lib.schema.common/normalize-keyword} [:= :empty]]
+ [:name ::lib.schema.common/non-blank-string]
+ [:sql ::lib.schema.common/non-blank-string]])
+
+(mr/def ::expectation
+ "A check on the output of the transform under test."
+ [:multi {:decode/normalize lib.schema.common/normalize-map-no-kebab-case
+ :dispatch (comp keyword :type)
+ :error/fn (unknown-dispatch :type)}
+ [:equals ::expectation.equals]
+ [:empty ::expectation.empty]])
+
+(mr/def ::expectations
+ "The checks run against the transform's output. Names must be unique within a test,
+ case-sensitively."
+ [:and
+ [:sequential ::expectation]
+ [:fn {:error/message "expectation names must be unique within a test"}
+ (fn [expectations]
+ (let [names (map :name expectations)]
+ (or (empty? names) (apply distinct? names))))]])
+
+(mr/def ::transform-test
+ "A saved transform test: the transform it covers, the inputs that stand in for its source
+ tables, and the expectations checked against its output."
+ [:map {:closed true}
+ [:id ms/PositiveInt]
+ [:entity_id :string]
+ [:transform_id ::lib.schema.id/transform]
+ [:creator_id ::lib.schema.id/user]
+ [:name :string]
+ [:description [:maybe :string]]
+ [:inputs ::inputs]
+ [:expectations ::expectations]
+ [:created_at ms/TemporalInstant]
+ [:updated_at ms/TemporalInstant]])
+
+(mr/def ::transform-test.create
+ "What a client sends to create a transform test."
+ [:map {:closed true}
+ [:transform_id ::lib.schema.id/transform]
+ [:name ::lib.schema.common/non-blank-string]
+ [:description {:optional true} [:maybe :string]]
+ [:inputs ::inputs]
+ [:expectations ::expectations]])
+
+(mr/def ::transform-test.update
+ "What a client sends to update a transform test. Omitted fields are left unchanged."
+ [:map {:closed true}
+ [:transform_id {:optional true} ::lib.schema.id/transform]
+ [:name {:optional true} ::lib.schema.common/non-blank-string]
+ [:description {:optional true} [:maybe :string]]
+ [:inputs {:optional true} ::inputs]
+ [:expectations {:optional true} ::expectations]])
+
+(mr/def ::status
+ "The outcome of a transform test run, or of one of its expectations.
+
+ `:error` is an expectation outcome only: that expectation's own query could not be run, while
+ the rest of the run completed and still reported."
+ [:enum {:decode/normalize lib.schema.common/normalize-keyword} :passed :failed :error])
+
+(mr/def ::expectation-result.base
+ "What every expectation result carries: which expectation this is, and how it came out.
+
+ A `:status` of `error` means this one expectation could not be evaluated while the rest of the
+ run still reported; `:error` then says why, and nothing else is reported for it."
+ [:map {:closed true}
+ [:name :string]
+ [:type :keyword]
+ [:status ::status]
+ [:columns {:optional true} [:sequential ::result-column]]
+ [:error {:optional true} [:map {:closed true}
+ [:type :keyword]
+ [:message :string]]]])
+
+(mr/def ::expectation-result
+ "What one expectation found. Each type owns the schema of its own result, registered under the key
+ named here."
+ [:multi {:dispatch :type}
+ [:equals :metabase-enterprise.transform-testing.expectations.equals/result]
+ [:empty :metabase-enterprise.transform-testing.expectations.empty/result]])
+
+(mr/def ::run-result
+ "The outcome of a transform test run.
+
+ `:tables` maps each temp table the run created to the table it stood in for."
+ [:map {:closed true}
+ [:status ::status]
+ [:expectations [:sequential ::expectation-result]]
+ [:tables [:map-of :string :string]]])
+
+(mr/def ::connection
+ "A connection from `driver/do-with-test-connection`: a JDBC connection, or the BigQuery client and session that stand
+ in for one."
+ [:or
+ (ms/InstanceOfClass java.sql.Connection)
+ [:map {:closed true}
+ [:client (ms/InstanceOfClass Object)]
+ [:session-id :string]]])
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/util.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/util.clj
new file mode 100644
index 00000000000..e944f847045
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/util.clj
@@ -0,0 +1,15 @@
+(ns metabase-enterprise.transform-testing.util
+ "Helpers shared across the module."
+ (:require
+ [metabase.util :as u]))
+
+(set! *warn-on-reflection* true)
+
+(defn fold-identifier
+ "`identifier` — a schema or a table name — as this module compares it, or nil when there is none.
+
+ Every comparison here ignores case: a reference against a declared input, a declared schema against the database's
+ default one, and two declared inputs against each other. That is how the rewrite matches, and how an engine reads an
+ unquoted identifier."
+ [identifier]
+ (some-> identifier u/lower-case-en))
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/validator.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/validator.clj
new file mode 100644
index 00000000000..73110d6c54b
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/validator.clj
@@ -0,0 +1,211 @@
+(ns metabase-enterprise.transform-testing.validator
+ "May this transform test run? [[validate]] answers, once, for the whole test, and throws a typed refusal when the
+ answer is no. Pure: it parses and rewrites SQL, and opens no connection.
+
+ Two guards, run against the test as a whole:
+
+ - Guard A (pre-rewrite): every table the transform reads must have a declared input, and every declared input must
+ be read, and no two inputs may be ones a reference cannot tell apart. A read with no input would leave that table
+ pointing at the REAL table (via `replace-names` `:allow-unused?`) — a false-green that reads production data into
+ a passing test.
+ - Guard B (post-rewrite): every table the rewritten queries read must be one of the run's temp tables. A leftover
+ real table means `replace-names` did not rewrite something it should have (e.g. a reference shape the parser
+ could not match), and running it would hit the real table or error at execution. This covers the transform and
+ every expectation that carries the author's own SQL.
+
+ [[table-label]] is here too, and is not validation: it is the one way this module renders a table in a message."
+ (:require
+ [clojure.string :as str]
+ [metabase-enterprise.transform-testing.compile :as transform-testing.compile]
+ [metabase-enterprise.transform-testing.errors :as transform-testing.errors]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase-enterprise.transform-testing.util :as transform-testing.u]
+ [metabase.sql-parsing.core :as sql-parsing]
+ [metabase.util.i18n :refer [tru]]
+ [metabase.util.malli :as mu]))
+
+(set! *warn-on-reflection* true)
+
+(mu/defn- table-match? :- :boolean
+ "Do a query's referenced table `ref` and a declared input `decl` name the same table? Matches by name, with schema
+ equal, or the reference bare (nil schema) against a declared table in `default-schema` — the one-directional
+ defaulting `table-replacements` uses when it rewrites (a bare read resolves to the default schema; a bare
+ declaration does not cover a qualified read).
+
+ Names match case-agnostically. The parser reports a reference as a bare string, so nothing here can tell a quoted
+ `\"Orders\"` — which an engine reads literally — from an unquoted `ORDERS`, and counting them as the same table is
+ the safe direction: it can only make this guard accept, and the rewrite's own guard still refuses a reference it
+ did not remap."
+ [{ref-schema :schema ref-name :name} :- ::transform-testing.schema/table
+ {d-schema :schema d-name :name} :- ::transform-testing.schema/table
+ default-schema :- [:maybe :string]]
+ (and (= (transform-testing.u/fold-identifier d-name) (transform-testing.u/fold-identifier ref-name))
+ (or (= (transform-testing.u/fold-identifier d-schema) (transform-testing.u/fold-identifier ref-schema))
+ (and (nil? ref-schema)
+ (= (transform-testing.u/fold-identifier d-schema)
+ (transform-testing.u/fold-identifier default-schema))))))
+
+(mu/defn table-label :- :string
+ "A human/agent-facing name for a table ref: `schema.name`, or just `name` when the schema is
+ unknown. For error messages — never surface the raw `{:schema :name}` map."
+ [{:keys [schema name]} :- ::transform-testing.schema/table]
+ (if schema (str schema \. name) name))
+
+(mu/defn- missing-inputs :- [:sequential ::transform-testing.schema/table]
+ "The tables in `referenced-tables` with no matching declared input — reads that would fall
+ through to a real table. Empty means every read is faked (the safety-critical direction)."
+ [inputs :- ::transform-testing.schema/inputs
+ referenced-tables :- [:set ::transform-testing.schema/table]
+ default-schema :- [:maybe :string]]
+ (let [declared (into #{} (map :table) inputs)]
+ (into [] (remove (fn [ref] (some #(table-match? ref % default-schema) declared))) referenced-tables)))
+
+(mu/defn- unused-inputs :- [:sequential ::transform-testing.schema/table]
+ "The declared inputs the transform does not read — a fake for a table the query never touches
+ (usually a stale or mistyped input). Empty means the suite declares nothing extraneous. Together
+ with `missing-inputs`, this makes the declared set exactly the referenced set: every read faked,
+ no fake unused."
+ [inputs :- ::transform-testing.schema/inputs
+ referenced-tables :- [:set ::transform-testing.schema/table]
+ default-schema :- [:maybe :string]]
+ (into [] (comp (map :table)
+ (remove (fn [decl] (some #(table-match? % decl default-schema) referenced-tables))))
+ inputs))
+
+(mu/defn- surviving-references :- [:sequential :string]
+ "Guard B: the real-table references still in the *rewritten* query, as labels for the author.
+ After the rewrite every reference should be a temp table we created; a leftover means
+ `replace-names` did not remap something and running it would read the real table or error at
+ execution. Two ways a reference survives, both caught here:
+
+ - a real *table* reference (`rewritten-tables` not in `temp-tables`) — a relation the rewrite
+ missed entirely;
+ - a *column qualified by a table name* that is no longer a FROM alias (`dangling-qualifiers`,
+ from the parser's `:missing-table-alias` errors) — e.g. `people.id` after `FROM people` was
+ rewritten to a temp table, which the table-level parse does not see as a reference.
+
+ Empty means the rewrite was total. Deduplicated, sorted, rendered as names for the 400 message."
+ [rewritten-tables :- [:set ::transform-testing.schema/table]
+ temp-tables :- [:set :string]
+ dangling-qualifiers :- [:set :string]]
+ (->> (concat (->> rewritten-tables
+ (remove (fn [{:keys [name]}] (contains? temp-tables name)))
+ (map table-label))
+ dangling-qualifiers)
+ distinct
+ sort
+ vec))
+
+(mu/defn- colliding-inputs :- [:sequential :string]
+ "The labels of the declared `inputs` that a query cannot tell apart, and so would be replaced by one another's temp
+ table.
+
+ Two inputs collide when a reference could resolve to either: the same table declared twice, once in either case, or
+ the same table declared once bare and once in the default schema. The rewrite maps each reference to one temp table,
+ so a collision would silently drop one input's fixture and read the other's.
+
+ Names are compared case-agnostically, which refuses a pair an engine could tell apart — Postgres can hold both
+ `orders` and `\"Orders\"`. Refusing a test whose author can rename its way out is the better failure than running one
+ whose second fixture is never read."
+ [inputs :- ::transform-testing.schema/inputs
+ default-schema :- [:maybe :string]]
+ (let [fold-key (fn [{:keys [schema table]}]
+ {:schema (transform-testing.u/fold-identifier schema)
+ :table (transform-testing.u/fold-identifier table)})
+ input-keys (fn [{{:keys [schema name]} :table}]
+ (into #{} (map fold-key) (transform-testing.compile/table-keys schema name default-schema)))
+ colliding (->> (map input-keys inputs)
+ (mapcat identity)
+ frequencies
+ (keep (fn [[table-key n]] (when (< 1 n) table-key)))
+ set)]
+ (into []
+ (comp (filter (fn [input] (some colliding (input-keys input))))
+ (map (comp table-label :table)))
+ inputs)))
+
+(mu/defn- check-rewrite
+ "Throw unless every table `rewritten` reads is one of `temp-tables`. `source` names the query for the author: nil is
+ the transform under test, a string is the expectation that carries the SQL."
+ [driver :- :keyword
+ rewritten :- :string
+ temp-tables :- [:set :string]
+ source :- [:maybe :string]]
+ (when-let [surviving (seq (surviving-references
+ (transform-testing.compile/referenced-tables driver rewritten)
+ temp-tables
+ (transform-testing.compile/dangling-qualifiers driver rewritten)))]
+ (throw (transform-testing.errors/ex
+ ::transform-testing.errors/unremapped-reference
+ (if source
+ (tru "Expectation {0} reads table(s) this test does not stand in for: {1}. An expectation may only read the transform''s output and its declared inputs."
+ (pr-str source) (str/join ", " surviving))
+ (tru "The transform test could not fully remap the source to test tables; these reference(s) remain: {0}. Alias each source table and qualify its columns by the alias (e.g. `FROM my_table t ... t.col`), not by the table name."
+ (str/join ", " surviving)))
+ (cond-> {:references (vec surviving)}
+ source (assoc :expectation source))))))
+
+(mu/defn- check-expectation-sql
+ "Throw unless the expectation named `expectation-name` reads only `temp-tables` once rewritten. Its SQL is the
+ author's, so SQL the parser cannot read is a refusal naming the expectation rather than the parser's own escape."
+ [driver :- :keyword
+ expectation-name :- :string
+ sql :- :string
+ replacements :- ::transform-testing.compile/table-replacements
+ temp-tables :- [:set :string]]
+ (try
+ (check-rewrite driver
+ (transform-testing.compile/replace-tables driver sql replacements)
+ temp-tables
+ expectation-name)
+ (catch Exception e
+ (if (sql-parsing/parse-error? e)
+ (throw (transform-testing.errors/ex
+ ::transform-testing.errors/unparseable-source
+ (tru "Expectation {0} has SQL that could not be parsed: {1}"
+ (pr-str expectation-name) (sql-parsing/parse-error-message e))
+ {:expectation expectation-name}))
+ (throw e)))))
+
+(mu/defn validate :- :nil
+ "Refuse the test unless it is complete against the transform it tests and every query it will run reads only the
+ run's temp tables. Returns nil when there is nothing to refuse, and otherwise throws a typed refusal from
+ [[metabase-enterprise.transform-testing.errors]].
+
+ `referenced-tables` are the tables the transform's source reads, parsed before any replacement; `rewritten-transform`
+ is that source after it; `replacements` is the map both it and the expectations are rewritten with, whose values name
+ the temp tables everything may read; `default-schema` is the schema an unqualified reference resolves to in the
+ database under test."
+ [driver :- :keyword
+ {:keys [inputs expectations referenced-tables rewritten-transform replacements default-schema]}
+ :- [:map {:closed true}
+ [:inputs ::transform-testing.schema/inputs]
+ [:expectations ::transform-testing.schema/expectations]
+ [:referenced-tables [:set ::transform-testing.schema/table]]
+ [:rewritten-transform :string]
+ [:replacements ::transform-testing.compile/table-replacements]
+ [:default-schema [:maybe :string]]]]
+ (when-let [missing (seq (missing-inputs inputs referenced-tables default-schema))]
+ (throw (transform-testing.errors/ex
+ ::transform-testing.errors/missing-inputs
+ (tru "The transform reads table(s) with no declared test input: {0}. Add an input for each."
+ (str/join ", " (map table-label missing)))
+ {:tables (mapv table-label missing)})))
+ (when-let [unused (seq (unused-inputs inputs referenced-tables default-schema))]
+ (throw (transform-testing.errors/ex
+ ::transform-testing.errors/unused-inputs
+ (tru "Test input(s) declared for table(s) the transform does not read: {0}. Remove them."
+ (str/join ", " (map table-label unused)))
+ {:tables (mapv table-label unused)})))
+ (when-let [colliding (seq (colliding-inputs inputs default-schema))]
+ (throw (transform-testing.errors/ex
+ ::transform-testing.errors/duplicate-input-table
+ (tru "Duplicate test inputs; each input table may be declared only once: {0}"
+ (str/join ", " colliding))
+ {:tables (vec colliding)})))
+ (let [temp-tables (into #{} (map :table) (vals replacements))]
+ (check-rewrite driver rewritten-transform temp-tables nil)
+ (doseq [{:keys [name sql]} expectations
+ :when sql]
+ (check-expectation-sql driver name sql replacements temp-tables)))
+ nil)
diff --git a/enterprise/backend/test/metabase_enterprise/api/session_test.clj b/enterprise/backend/test/metabase_enterprise/api/session_test.clj
index ccec273e565..ff6d5bf3636 100644
--- a/enterprise/backend/test/metabase_enterprise/api/session_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/api/session_test.clj
@@ -62,6 +62,7 @@
:support-users
:transforms-basic
:transforms-python
+ :transforms-testing
:upload-management
:whitelabel
:collection-cleanup
@@ -116,6 +117,7 @@
:table_data_editing false
:transforms-basic true
:transforms-python true
+ :transforms-testing true
:upload_management true
:whitelabel true
:collection_cleanup true
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj
index fbc71bb231e..62db8591515 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj
@@ -131,8 +131,9 @@
(is (contains? types "Measure"))
(is (contains? types "Transform"))
(is (contains? types "TransformTag"))
+ (is (contains? types "TransformTest"))
(is (contains? types "Glossary"))
- (is (= 14 (count types))))))
+ (is (= 15 (count types))))))
(deftest specs-by-identity-type-test
(testing "specs-by-identity-type filters correctly"
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/transforms_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/transforms_test.clj
index aba12f0ec1e..14e98d418cd 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/transforms_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/transforms_test.clj
@@ -1058,3 +1058,83 @@ serdes/meta:
"Local transform tag should be deleted after import since it wasn't on remote")
(is (t2/exists? :model/TransformTag :entity_id remote-tag-entity-id)
"Remote transform tag should be imported")))))))))
+
+(defn- transform-test-rso [test-id]
+ (t2/select-one :model/RemoteSyncObject :model_type "TransformTest" :model_id test-id))
+
+(deftest transform-test-events-track-sync-object-test
+ (testing "Creating, updating and deleting a transform test tracks it when remote-sync-transforms is enabled"
+ (mt/with-premium-features #{:transforms-basic}
+ (mt/with-temporary-setting-values [remote-sync-transforms true
+ remote-sync-enabled true]
+ (mt/with-temp [:model/Transform {transform-id :id} {:name "Orders Summary"}
+ :model/TransformTest {test-id :id} {:transform_id transform-id :name "My test"}]
+ (is (= "create" (:status (transform-test-rso test-id))))
+ (t2/update! :model/RemoteSyncObject (:id (transform-test-rso test-id))
+ {:status "synced" :status_changed_at (t/offset-date-time)})
+ (t2/update! :model/TransformTest test-id {:name "Renamed test"})
+ (is (=? {:status "update" :model_name "Renamed test"} (transform-test-rso test-id)))
+ (t2/update! :model/RemoteSyncObject (:id (transform-test-rso test-id))
+ {:status "synced" :status_changed_at (t/offset-date-time)})
+ (t2/delete! :model/TransformTest test-id)
+ (is (= "delete" (:status (transform-test-rso test-id)))))))))
+
+(deftest transform-delete-marks-its-tests-deleted-test
+ (testing "Deleting a transform marks its tests for deletion, so their files leave the branch with its own"
+ (mt/with-premium-features #{:transforms-basic}
+ (mt/with-temporary-setting-values [remote-sync-transforms true
+ remote-sync-enabled true]
+ (mt/with-temp [:model/Transform {transform-id :id} {:name "Orders Summary"}
+ :model/TransformTest {test-id :id} {:transform_id transform-id :name "My test"}]
+ (t2/update! :model/RemoteSyncObject (:id (transform-test-rso test-id))
+ {:status "synced" :status_changed_at (t/offset-date-time)})
+ (t2/delete! :model/Transform transform-id)
+ (is (false? (t2/exists? :model/TransformTest :id test-id)))
+ (is (= "delete" (:status (transform-test-rso test-id)))))))))
+
+(deftest transform-test-events-ignored-when-setting-disabled-test
+ (testing "Transform tests aren't tracked when remote-sync-transforms is disabled"
+ (mt/with-premium-features #{:transforms-basic}
+ (mt/with-temporary-setting-values [remote-sync-transforms false
+ remote-sync-enabled true]
+ (mt/with-temp [:model/Transform {transform-id :id} {:name "Orders Summary"}
+ :model/TransformTest {test-id :id} {:transform_id transform-id :name "My test"}]
+ (is (nil? (transform-test-rso test-id))))))))
+
+(defn- new-export-task! []
+ (t2/delete! :model/RemoteSyncTask)
+ (t2/insert-returning-pk! :model/RemoteSyncTask {:sync_task_type "export" :initiated_by (mt/user->id :rasta)}))
+
+(deftest transform-test-file-moves-with-its-transform-test
+ (testing "A transform test is stored under its transform's path, so renaming the transform moves the test's file"
+ (mt/with-premium-features #{:transforms-basic}
+ (mt/with-temporary-setting-values [remote-sync-type :read-write
+ remote-sync-transforms true
+ remote-sync-enabled true]
+ (mt/with-model-cleanup [:model/RemoteSyncTask]
+ (mt/with-temp [:model/Collection {coll-id :id} {:name "ETL" :namespace collection/transforms-ns :location "/"}
+ :model/Transform {transform-id :id} {:name "Orders Summary" :collection_id coll-id}
+ :model/TransformTest {test-id :id test-eid :entity_id} {:transform_id transform-id :name "No nulls"}]
+ (testing "creating a transform test tracks it"
+ (is (= "create" (:status (t2/select-one :model/RemoteSyncObject :model_type "TransformTest" :model_id test-id)))))
+ (t2/delete! :model/RemoteSyncObject)
+ (doseq [[model-type model-id] [["Collection" coll-id] ["Transform" transform-id] ["TransformTest" test-id]]]
+ (t2/insert! :model/RemoteSyncObject {:model_type model-type
+ :model_id model-id
+ :model_name model-type
+ :status "create"
+ :status_changed_at (t/offset-date-time)}))
+ (let [mock (test-helpers/create-mock-source :initial-files {"main" {}})
+ test-path (fn []
+ (some (fn [[path content]] (when (str/includes? content test-eid) path))
+ (get @(:files-atom mock) "main")))]
+ (is (= :success (:status (impl/export! (source.p/snapshot mock) (new-export-task!) "init"))))
+ (let [old-path (test-path)]
+ (is (str/ends-with? old-path "/etl/orders_summary/no_nulls.yaml"))
+ (t2/update! :model/Transform transform-id {:name "Orders Report"})
+ (events/publish-event! :event/transform-update {:object (t2/select-one :model/Transform transform-id)})
+ (testing "renaming the transform marks its test for update"
+ (is (= "update" (:status (t2/select-one :model/RemoteSyncObject :model_type "TransformTest" :model_id test-id)))))
+ (is (= :success (:status (impl/export! (source.p/snapshot mock) (new-export-task!) "rename"))))
+ (is (str/ends-with? (test-path) "/etl/orders_report/no_nulls.yaml"))
+ (is (not (contains? (get @(:files-atom mock) "main") old-path)))))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.clj
index 1f4b84b4459..a37fe531661 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.clj
@@ -12,6 +12,7 @@
[metabase-enterprise.serialization.v2.load :as serdes.load]
[metabase-enterprise.serialization.v2.storage :as storage]
[metabase-enterprise.serialization.v2.storage.files :as storage.files]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
[metabase.lib-be.core :as lib-be]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
@@ -22,6 +23,7 @@
[metabase.settings.core :as setting]
[metabase.test :as mt]
[metabase.test.generate :as test-gen]
+ [metabase.util.malli.registry :as mr]
[metabase.util.yaml :as yaml]
[metabase.warehouses.models.database :as models.database]
[reifyhealth.specmonstah.core :as rs]
@@ -1082,3 +1084,116 @@
(is (=? {"database" nil
"stages" [{"source-table" pos-int?}]}
(get-in transform [:source :query]))))))))))
+
+(deftest transform-test-round-trip-test
+ (testing "A transform test is exported under its transform's path and imported with its transform"
+ (mt/with-premium-features #{:transforms-basic}
+ (ts/with-random-dump-dir [dump-dir "serdesv2-"]
+ (ts/with-dbs [source-db dest-db]
+ (ts/with-db source-db
+ (let [db (ts/create! :model/Database :name "my-db")
+ coll (ts/create! :model/Collection :name "ETL" :namespace :transforms)
+ creator (ts/create! :model/User :email "[email protected]")
+ transform (ts/create! :model/Transform
+ :name "Orders Summary"
+ :collection_id (:id coll)
+ :creator_id (:id creator)
+ :source {:type "query"
+ :query {:database (:id db) :type "native" :native {:query "SELECT 1 AS ID"}}}
+ :target {:database (:id db) :type "table" :schema "PUBLIC" :name "orders_summary"})]
+ (ts/create! :model/TransformTest
+ :transform_id (:id transform)
+ :creator_id (:id creator)
+ :name "My test"
+ :inputs [{:table {:schema "PUBLIC" :name "ORDERS"}
+ :format :rows
+ :columns [{:name "ID" :cast_type "INTEGER"}]
+ :rows [{"ID" 1}]}]
+ :expectations [{:type :equals :name "one row" :format :sql :sql "SELECT 1 AS ID"}
+ {:type :empty :name "no nulls" :sql "SELECT * FROM PUBLIC.orders_summary WHERE ID IS NULL"}])
+ (storage/store! (seq (serdes/with-cache (into [] (extract/extract {})))) (storage.files/file-writer dump-dir))))
+ (testing "the file sits under the transform's path, with keywords written as strings"
+ (is (=? {:transform_id string?
+ :creator_id "[email protected]"
+ :inputs [{:format "rows" :columns [{:cast_type "INTEGER"}]}]
+ :expectations [{:type "equals" :format "sql"} {:type "empty"}]}
+ (yaml/parse-string
+ (slurp (io/file dump-dir "collections" "transforms" "etl" "orders_summary" "my_test.yaml"))))))
+ (ts/with-db dest-db
+ (is (serdes/with-cache (serdes.load/load-metabase! (ingest/ingest-yaml dump-dir))))
+ (is (=? {:name "My test"
+ :transform_id (t2/select-one-pk :model/Transform :name "Orders Summary")
+ :creator_id (t2/select-one-pk :model/User :email "[email protected]")
+ :inputs [{:table {:schema "PUBLIC" :name "ORDERS"}
+ :format :rows
+ :columns [{:name "ID" :cast_type "INTEGER"}]
+ :rows [{"ID" 1}]}]
+ :expectations [{:type :equals :name "one row" :format :sql :sql "SELECT 1 AS ID"}
+ {:type :empty :name "no nulls" :sql "SELECT * FROM PUBLIC.orders_summary WHERE ID IS NULL"}]}
+ (t2/select-one :model/TransformTest :name "My test")))))))))
+
+;;; ---------------------------------- Transform test expectations round trip ----------------------------------
+;;; Companion to transform-test-round-trip-test above, which covers the `equals`/`sql` and `empty` shapes. This
+;;; one covers the `equals`/`rows` payload -- the only one carrying author-named columns and row maps -- and the
+;;; claim that import normalizes and validates it the way a POST does.
+
+(def ^:private round-trip-expectations
+ "An `equals` whose column names are the hazardous ones -- `2024` would come back a number from YAML and a keyword
+ from a JSON column, `order-id` a kebab-case keyword -- plus an `empty`."
+ [{:type :equals
+ :name "rows match"
+ :format :rows
+ :columns [{:name "2024" :cast_type "INTEGER"}
+ {:name "order-id" :cast_type "VARCHAR"}]
+ :rows [{"2024" 7 "order-id" "A-1"}
+ {"2024" nil "order-id" "B-2"}]}
+ {:type :empty
+ :name "no nulls"
+ :sql "SELECT * FROM PUBLIC.revenue WHERE \"order-id\" IS NULL"}])
+
+(deftest transform-test-expectations-round-trip-test
+ (testing "A transform test's expectations come back from an import as validated records, payload intact"
+ (mt/with-premium-features #{:transforms-basic}
+ (ts/with-random-dump-dir [dump-dir "serdesv2-"]
+ (ts/with-dbs [source-db dest-db]
+ (let [transform-eid
+ (ts/with-db source-db
+ (let [db (ts/create! :model/Database :name "my-db")
+ coll (ts/create! :model/Collection :name "ETL" :namespace :transforms)
+ creator (ts/create! :model/User :email "[email protected]")
+ transform (ts/create! :model/Transform
+ :name "Revenue Report"
+ :collection_id (:id coll)
+ :creator_id (:id creator)
+ :source {:type "query"
+ :query {:database (:id db) :type "native" :native {:query "SELECT 1 AS ID"}}}
+ :target {:database (:id db) :type "table" :schema "PUBLIC" :name "revenue"})]
+ (ts/create! :model/TransformTest
+ :transform_id (:id transform)
+ :creator_id (:id creator)
+ :name "Revenue by year"
+ :inputs []
+ :expectations round-trip-expectations)
+ (storage/store! (seq (serdes/with-cache (into [] (extract/extract {})))) (storage.files/file-writer dump-dir))
+ (:entity_id transform)))
+ ;; Pins the storage path: a transform test is stored under its transform's serdes path.
+ exported (yaml/parse-string
+ (slurp (io/file dump-dir "collections" "transforms" "etl" "revenue_report" "revenue_by_year.yaml")))]
+ (testing "the transform is referenced by entity id, not by primary key"
+ (is (= transform-eid (:transform_id exported))))
+ (testing "row keys are written as YAML strings -- `2024:` unquoted would read back as a number"
+ (is (= [#{(keyword "2024") :order-id} #{(keyword "2024") :order-id}]
+ (mapv (comp set keys) (get-in exported [:expectations 0 :rows])))))
+ (ts/with-db dest-db
+ (is (serdes/with-cache (serdes.load/load-metabase! (ingest/ingest-yaml dump-dir))))
+ (let [imported (t2/select-one :model/TransformTest :name "Revenue by year")
+ expectations (:expectations imported)]
+ (testing "the transform reference resolves"
+ (is (= (t2/select-one-pk :model/Transform :name "Revenue Report")
+ (:transform_id imported))))
+ (testing "serdes import gets the same validation as a POST: what it stored satisfies the schema"
+ (is (mr/validate ::transform-testing.schema/expectations expectations))
+ (testing "and normalization ran, so a type arrives as a keyword rather than the YAML's string"
+ (is (= [:equals :empty] (mapv :type expectations)))))
+ (testing "the payload is unchanged, row keys still strings"
+ (is (= round-trip-expectations expectations)))))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/round_trip_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/round_trip_test.clj
index ebae014236e..74a11b1e898 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/round_trip_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/round_trip_test.clj
@@ -146,8 +146,12 @@
(def ^:private covered-by-dedicated-round-trip-test?
"Models that have full export/import coverage in their own round-trip test and so don't need a
fixture in this shared baseline. OsiAiContext is covered (in-memory + on-disk) by
- metabase-enterprise.serialization.v2.osi-ai-context-test."
- #{"OsiAiContext"})
+ metabase-enterprise.serialization.v2.osi-ai-context-test. TransformTest is covered by
+ transform-test-round-trip-test and transform-test-expectations-round-trip-test in
+ metabase-enterprise.serialization.v2.e2e-test, and stays out of the baseline until
+ @metabase/representations publishes a schema for it — the baseline is validated against that
+ package, which refuses a model it does not know."
+ #{"OsiAiContext" "TransformTest"})
(defn add-to-baseline!
"Use this within v2.extract-test where relevant to add their fixtures to the baseline."
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/api_e2e_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/api_e2e_test.clj
new file mode 100644
index 00000000000..3faa7c3edf0
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/api_e2e_test.clj
@@ -0,0 +1,149 @@
+(ns ^:mb/driver-tests metabase-enterprise.transform-testing.api-e2e-test
+ "End-to-end tests for `/api/ee/transform-test`: HTTP in, a real warehouse out. The unit tests pin what
+ each piece does; these pin that the seams hold — that a test authored over the API is the test the
+ runner runs, and that a refusal reaches the client as a refusal."
+ (:require
+ [clojure.test :refer :all]
+ [metabase-enterprise.transform-testing.test-util :as transform-testing.test-util]
+ [metabase-enterprise.transform-testing.validator :as transform-testing.validator]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
+ [metabase.test :as mt]
+ [toucan2.core :as t2]))
+
+(defmacro ^:private with-transforms-enabled
+ "Body with `features` licensed and the transforms setting on."
+ [features & body]
+ `(mt/with-premium-features ~features
+ (mt/with-temporary-raw-setting-values [~'transforms-enabled "true"]
+ ~@body)))
+
+(defn- with-people-transform
+ "Call `f` with the schema and name of the people table and the id of a transform selecting `id, name`
+ from it into `people_summary`."
+ [f]
+ (let [mp (mt/metadata-provider)
+ {schema :schema, table :name} (lib.metadata/table mp (mt/id :people))]
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query mp (str "SELECT id, name FROM " schema "." table))}
+ :target {:type "table"
+ :schema schema
+ :name "people_summary"
+ :database (mt/id)}}]
+ (f schema table transform-id))))
+
+(defn- outcomes
+ "The identifying keys of each expectation result, in reported order."
+ [run-result]
+ (mapv #(select-keys % [:name :type :status]) (:expectations run-result)))
+
+(deftest transform-test-lifecycle-test
+ (testing "author, read, run, edit, re-run and delete a transform test over the API"
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (with-transforms-enabled #{:transforms-basic :transforms-testing}
+ (with-people-transform
+ (fn [schema table transform-id]
+ (let [input {:table {:schema schema :name table}
+ :format "sql"
+ :sql "SELECT 1 AS id, 'abc' AS name"}
+ passing {:type "empty"
+ :name "no null ids"
+ :sql (str "SELECT * FROM " schema ".people_summary WHERE id IS NULL")}
+ ;; The output row is (1, 'abc'), so this one differs in a single cell.
+ [int-type text-type] (transform-testing.test-util/cast-types)
+ failing {:type "equals"
+ :name "output"
+ :format "rows"
+ :columns [{:name "id" :cast_type int-type}
+ {:name "name" :cast_type text-type}]
+ :rows [{"id" 1 "name" "xyz"}]}
+ created (mt/user-http-request :crowberto :post 200 "ee/transform-test"
+ {:transform_id transform-id
+ :name "End to end"
+ :inputs [input]
+ :expectations [passing]})
+ path (str "ee/transform-test/" (:id created))]
+ (try
+ (testing "POST stores the test against the transform, owned by the caller"
+ (is (=? {:id pos-int?
+ :transform_id transform-id
+ :creator_id (mt/user->id :crowberto)
+ :name "End to end"}
+ created)))
+ (testing "GET returns what POST created"
+ (is (= created (mt/user-http-request :crowberto :get 200 path))))
+ (testing "running a passing test"
+ (let [result (mt/user-http-request :crowberto :post 200 (str path "/run"))]
+ (is (= #{:status :expectations :tables} (set (keys result))))
+ (is (= "passed" (:status result)))
+ (is (= [{:name "no null ids" :type "empty" :status "passed"}]
+ (outcomes result)))))
+ (testing "PUT replaces the expectations"
+ (is (=? {:expectations [{:name "no null ids"} {:name "output"}]}
+ (mt/user-http-request :crowberto :put 200 path
+ {:expectations [passing failing]}))))
+ (testing "every expectation reports, and one failure fails the run"
+ (let [result (mt/user-http-request :crowberto :post 200 (str path "/run"))
+ [pass fail] (:expectations result)]
+ (is (= "failed" (:status result)))
+ (is (= [{:name "no null ids" :type "empty" :status "passed"}
+ {:name "output" :type "equals" :status "failed"}]
+ (outcomes result)))
+ (testing "the failing one carries its own detail"
+ (is (= [{:column "name" :expected "xyz" :actual "abc"}]
+ (:cell-mismatches fail))))
+ (testing "the passing one carries none"
+ (is (nil? (:cell-mismatches pass))))))
+ (testing "DELETE removes the test"
+ (mt/user-http-request :crowberto :delete 204 path)
+ (is (not (t2/exists? :model/TransformTest (:id created))))
+ (mt/user-http-request :crowberto :get 404 path))
+ (finally
+ (t2/delete! :model/TransformTest :id (:id created)))))))))))
+
+(deftest run-refusal-test
+ (testing "a non-200 from the run endpoint means nothing ran"
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (with-transforms-enabled #{:transforms-basic :transforms-python :transforms-testing}
+ (testing "422 — the environment prevents a run: not a query transform"
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "python" :source-database (mt/id)}}
+ :model/TransformTest {test-id :id} {:transform_id transform-id}]
+ (let [response (mt/user-http-request :crowberto :post 422
+ (format "ee/transform-test/%d/run" test-id))]
+ (is (= "transform-test.unsupported-transform"
+ (:error-code response)))
+ (is (string? (:message response)))
+ ;; The structured branch of the exception middleware, so no stacktrace rides along —
+ ;; which also means the body survives an instance with stacktraces suppressed.
+ (is (not (contains? response :trace))))))
+ (testing "400 — an authoring error: the transform reads a table with no declared input"
+ (with-people-transform
+ (fn [schema table transform-id]
+ (mt/with-temp [:model/TransformTest {test-id :id}
+ {:transform_id transform-id :inputs [] :expectations []}]
+ (let [response (mt/user-http-request :crowberto :post 400
+ (format "ee/transform-test/%d/run" test-id))]
+ (is (= "transform-test.missing-inputs"
+ (:error-code response)))
+ (is (re-find (re-pattern (str "(?i)" table)) (:message response)))
+ ;; The refusal's own ex-data rides along, so the offending tables are readable as
+ ;; data and not only out of the prose. Derived from the metadata rather than
+ ;; written out: engines differ on the case they fold identifiers to, and MySQL
+ ;; reports no schema at all.
+ (is (= [(transform-testing.validator/table-label {:schema schema :name table})]
+ (:tables response)))
+ (is (not (contains? response :trace))))))))))))
+
+(deftest run-is-gated-test
+ (with-transforms-enabled #{:transforms-basic :transforms-testing}
+ (mt/with-temp [:model/Transform {transform-id :id} {}
+ :model/TransformTest {test-id :id} {:transform_id transform-id}]
+ (let [run-path (format "ee/transform-test/%d/run" test-id)]
+ (testing "the routes are behind +auth"
+ (is (= "Unauthenticated" (mt/client :get 401 "ee/transform-test")))
+ (is (= "Unauthenticated" (mt/client :post 401 run-path))))
+ (testing "a user with no transform access cannot run someone else's test"
+ (mt/user-http-request :rasta :post 403 run-path)
+ (is (t2/exists? :model/TransformTest test-id)))))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/api_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/api_test.clj
new file mode 100644
index 00000000000..07071c11aed
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/api_test.clj
@@ -0,0 +1,190 @@
+(ns metabase-enterprise.transform-testing.api-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.lib.core :as lib]
+ [metabase.test :as mt]
+ [toucan2.core :as t2]))
+
+(def ^:private inputs
+ [{:table {:schema "PUBLIC" :name "PEOPLE"}
+ :format "rows"
+ :columns [{:name "ID" :cast_type "INTEGER"}]
+ :rows [{"ID" 1}]}])
+
+(def ^:private expectations
+ [{:type "empty" :name "no rows" :sql "SELECT * FROM PUBLIC.PEOPLE_SUMMARY WHERE ID IS NULL"}])
+
+(defn- transform
+ "A transform reading the table `inputs` stands in for, writing the table `expectations` reads, so that a test of
+ it is one the write endpoints accept."
+ []
+ {:source {:type "query"
+ :query (lib/native-query (mt/metadata-provider) "SELECT ID, NAME FROM PUBLIC.PEOPLE")}
+ :target {:type "table" :schema "PUBLIC" :name "PEOPLE_SUMMARY" :database (mt/id)}})
+
+(defmacro ^:private with-transforms-enabled [& body]
+ `(mt/with-premium-features #{:transforms-basic :transforms-testing}
+ (mt/with-temporary-raw-setting-values [~'transforms-enabled "true"]
+ ~@body)))
+
+(deftest premium-feature-test
+ (testing "every endpoint refuses an instance without the feature"
+ (mt/with-premium-features #{:transforms-basic}
+ (mt/with-temporary-raw-setting-values [transforms-enabled "true"]
+ (mt/with-temp [:model/Transform {transform-id :id} {}
+ :model/TransformTest {test-id :id} {:transform_id transform-id}]
+ (let [path (str "ee/transform-test/" test-id)
+ body {:transform_id transform-id :name "t" :inputs [] :expectations []}]
+ (doseq [request [[:get 402 "ee/transform-test"]
+ [:post 402 "ee/transform-test" body]
+ [:get 402 path]
+ [:put 402 path {:name "t"}]
+ [:delete 402 path]
+ [:post 402 (str path "/run")]]]
+ (mt/assert-has-premium-feature-error
+ "Transforms Testing"
+ (apply mt/user-http-request :crowberto request)))))))))
+
+(deftest crud-test
+ (with-transforms-enabled
+ (mt/with-temp [:model/Transform {transform-id :id} (transform)
+ :model/Transform {other-id :id} (transform)]
+ (let [created (mt/user-http-request :crowberto :post 200 "ee/transform-test"
+ {:transform_id transform-id
+ :name "My test"
+ :description "abc"
+ :inputs inputs
+ :expectations expectations})
+ path (str "ee/transform-test/" (:id created))]
+ (try
+ (testing "POST creates the test as the current user"
+ (is (=? {:id pos-int?
+ :entity_id string?
+ :transform_id transform-id
+ :creator_id (mt/user->id :crowberto)
+ :name "My test"
+ :description "abc"
+ :inputs [{:format "rows" :columns [{:name "ID" :cast_type "INTEGER"}] :rows [{:ID 1}]}]
+ :expectations expectations}
+ created)))
+ (testing "GET returns the test"
+ (is (= created (mt/user-http-request :crowberto :get 200 path))))
+ (testing "GET / filters by transform"
+ (is (= [(:id created)]
+ (map :id (mt/user-http-request :crowberto :get 200 "ee/transform-test" :transform-id transform-id))))
+ (is (= [] (mt/user-http-request :crowberto :get 200 "ee/transform-test" :transform-id other-id))))
+ (testing "PUT changes only the given keys"
+ (is (=? {:name "Renamed" :description "abc" :transform_id other-id}
+ (mt/user-http-request :crowberto :put 200 path {:name "Renamed" :transform_id other-id}))))
+ (testing "DELETE removes the test"
+ (mt/user-http-request :crowberto :delete 204 path)
+ (is (not (t2/exists? :model/TransformTest (:id created))))
+ (mt/user-http-request :crowberto :get 404 path))
+ (finally
+ (t2/delete! :model/TransformTest (:id created))))))))
+
+(deftest write-endpoints-refuse-a-test-that-could-not-run-test
+ (with-transforms-enabled
+ (mt/with-temp [:model/Transform {transform-id :id} (transform)]
+ (testing "POST refuses a test the runner would refuse, as that refusal"
+ (let [response (mt/user-http-request :crowberto :post 400 "ee/transform-test"
+ {:transform_id transform-id
+ :name "no inputs"
+ :inputs []
+ :expectations expectations})]
+ (is (= "transform-test.missing-inputs" (:error-code response)))
+ (is (= ["PUBLIC.PEOPLE"] (:tables response)))
+ (is (not (t2/exists? :model/TransformTest :name "no inputs")))))
+ (testing "and an expectation reading a table the test does not stand in for"
+ (let [response (mt/user-http-request :crowberto :post 400 "ee/transform-test"
+ {:transform_id transform-id
+ :name "leaky expectation"
+ :inputs inputs
+ :expectations [{:type "empty" :name "leaks"
+ :sql "SELECT * FROM PUBLIC.ORDERS"}]})]
+ (is (= "transform-test.unremapped-reference" (:error-code response)))))
+ (testing "PUT refuses an update that would leave the test unrunnable, and changes nothing"
+ (mt/with-temp [:model/TransformTest {test-id :id} {:transform_id transform-id
+ :name "My test"
+ :inputs inputs
+ :expectations expectations}]
+ (let [path (str "ee/transform-test/" test-id)
+ response (mt/user-http-request :crowberto :put 400 path {:inputs []})]
+ (is (= "transform-test.missing-inputs" (:error-code response)))
+ (is (= (:inputs (t2/select-one :model/TransformTest test-id))
+ (mapv #(update % :format keyword) inputs))))
+ (testing "while an update that touches nothing a run reads is let through"
+ (is (=? {:name "Renamed"}
+ (mt/user-http-request :crowberto :put 200 (str "ee/transform-test/" test-id)
+ {:name "Renamed"})))))))))
+
+(deftest validation-test
+ (with-transforms-enabled
+ (mt/with-temp [:model/Transform {transform-id :id} {}
+ :model/TransformTest {test-id :id} {:transform_id transform-id}]
+ (let [valid {:transform_id transform-id :name "t" :inputs [] :expectations []}]
+ (testing "inputs and expectations are required on create"
+ (mt/user-http-request :crowberto :post 400 "ee/transform-test" (dissoc valid :inputs :expectations)))
+ (testing "rows data needs columns"
+ (mt/user-http-request :crowberto :post 400 "ee/transform-test"
+ (assoc valid :inputs [{:table {:name "PEOPLE"} :format "rows" :rows []}])))
+ (testing "expectations need a name"
+ (mt/user-http-request :crowberto :post 400 "ee/transform-test"
+ (assoc valid :expectations [{:type "empty" :sql "SELECT 1"}])))
+ (testing "the transform must exist"
+ (mt/user-http-request :crowberto :post 404 "ee/transform-test" (assoc valid :transform_id Integer/MAX_VALUE))
+ (mt/user-http-request :crowberto :put 404 (str "ee/transform-test/" test-id) {:transform_id Integer/MAX_VALUE}))))))
+
+(deftest run-transform-test-endpoint-test
+ (mt/test-driver :h2
+ (with-transforms-enabled
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query (mt/metadata-provider) "SELECT ID, NAME FROM PUBLIC.PEOPLE")}
+ :target {:type "table" :schema "PUBLIC" :name "PEOPLE_SUMMARY" :database (mt/id)}}]
+ (let [run (fn [expected-status transform-test]
+ (mt/with-temp [:model/TransformTest {test-id :id} (assoc transform-test :transform_id transform-id)]
+ (mt/user-http-request :crowberto :post expected-status (format "ee/transform-test/%d/run" test-id))))
+ input {:table {:schema "PUBLIC" :name "PEOPLE"} :format :sql :sql "SELECT 1 AS ID, 'abc' AS NAME"}]
+ (testing "returns passed when every expectation passes"
+ (let [result (run 200 {:inputs [input]
+ :expectations [{:type :empty :name "one id" :sql "SELECT * FROM PUBLIC.PEOPLE_SUMMARY WHERE ID <> 1"}]})]
+ (is (= "passed" (:status result)))
+ (is (= [{:name "one id" :type "empty" :status "passed"}]
+ (:expectations result)))))
+ (testing "returns failed when an expectation fails"
+ (let [result (run 200 {:inputs [input]
+ :expectations [{:type :empty :name "no abc" :sql "SELECT * FROM PUBLIC.PEOPLE_SUMMARY WHERE NAME = 'abc'"}]})]
+ (is (= "failed" (:status result)))
+ (is (= ["no abc"] (mapv :name (:expectations result))))
+ (testing "the sample names its cells rather than making the reader zip them"
+ (is (= [{:ID 1 :NAME "abc"}] (mapv (comp first :sample) (:expectations result)))))
+ (testing "and the columns carry the warehouse's own type for each"
+ (is (= [[{:name "ID" :database_type "INTEGER"}
+ {:name "NAME" :database_type "CHARACTER VARYING"}]]
+ (mapv :columns (:expectations result)))))))
+ (testing "rejects a test that doesn't replace every table the transform reads"
+ (run 400 {:inputs [] :expectations []})))))))
+
+(deftest permissions-test
+ (with-transforms-enabled
+ (mt/with-temp [:model/Transform {transform-id :id} {}
+ :model/TransformTest {test-id :id} {:transform_id transform-id}]
+ (let [path (str "ee/transform-test/" test-id)
+ body {:transform_id transform-id :name "nope" :inputs [] :expectations []}]
+ (testing "users without transform access can't read or change transform tests"
+ (mt/user-http-request :rasta :get 403 "ee/transform-test")
+ (mt/user-http-request :rasta :get 403 path)
+ (mt/user-http-request :rasta :post 403 "ee/transform-test" body)
+ (mt/user-http-request :rasta :put 403 path {:name "nope"})
+ (mt/user-http-request :rasta :delete 403 path)
+ (mt/user-http-request :rasta :post 403 (str path "/run")))
+ (testing "data analysts who can read but not write the transform can read but not change its tests"
+ (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (is (some #{test-id} (map :id (mt/user-http-request :lucky :get 200 "ee/transform-test"))))
+ (mt/user-http-request :lucky :get 200 path)
+ (mt/user-http-request :lucky :post 403 "ee/transform-test" body)
+ (mt/user-http-request :lucky :put 403 path {:name "nope"})
+ (mt/user-http-request :lucky :delete 403 path)
+ (mt/user-http-request :lucky :post 403 (str path "/run"))))
+ (is (t2/exists? :model/TransformTest test-id))))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/compile_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/compile_test.clj
new file mode 100644
index 00000000000..5dcfdadd788
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/compile_test.clj
@@ -0,0 +1,201 @@
+(ns metabase-enterprise.transform-testing.compile-test
+ "Tests for the SQL-building half of transform testing: the queries a run executes, and the
+ quoting and parameterization that keep author-supplied text out of them."
+ (:require
+ [clojure.string :as str]
+ [clojure.test :refer [deftest is testing]]
+ [metabase-enterprise.transform-testing.compile :as transform-testing.compile]
+ [metabase.driver :as driver]
+ [metabase.driver.h2]
+ [metabase.driver.postgres]))
+
+(comment metabase.driver.h2/keep-me)
+(comment metabase.driver.postgres/keep-me)
+
+(defn- rows-query
+ "`rows` compiled for `driver`, as `{:query :params}`."
+ [driver columns sql-names rows]
+ (transform-testing.compile/rows-query driver columns sql-names rows))
+
+;;; --------------------------------------------- rows-query ---------------------------------------------
+
+(deftest row-values-are-bound-parameters-test
+ (testing "no cell value reaches the SQL as text"
+ ;; Security property. A hostile string is a parameter and nothing else; the SQL text it produces
+ ;; is a bare `?`.
+ (let [hostile "'); DROP TABLE users; --"
+ {:keys [query params]} (rows-query :postgres
+ [{:name "note" :cast_type "VARCHAR(255)"}]
+ ["NOTE"]
+ [{"note" hostile}])]
+ (is (= [hostile] params))
+ (is (str/includes? query "CAST(? AS VARCHAR(255))"))
+ (is (not (str/includes? query "DROP")) (pr-str query))
+ (is (not (str/includes? query "--")) (pr-str query))
+ (is (not (str/includes? query "'")) (pr-str query)))))
+
+(deftest column-name-is-one-quoted-identifier-test
+ (testing "a name SQL would otherwise read as a qualifier is quoted whole, not split on the dot"
+ ;; `a.b` as a bare identifier would compile to `\"a\".\"b\"` — a reference to some other table's
+ ;; column rather than to the column the author named.
+ (let [{:keys [query]} (rows-query :postgres
+ [{:name "a.b" :cast_type "INTEGER"}]
+ ["a.b"]
+ [{"a.b" 1}])]
+ (is (str/includes? query "AS \"a.b\"") (pr-str query))
+ (is (not (str/includes? query "\"a\".\"b\"")) (pr-str query))))
+ (testing "a quote in the name is escaped by doubling rather than closing the identifier"
+ (let [{:keys [query]} (rows-query :postgres
+ [{:name "a\"b" :cast_type "INTEGER"}]
+ ["a\"b"]
+ [{"a\"b" 1}])]
+ (is (str/includes? query "AS \"a\"\"b\"") (pr-str query)))))
+
+(deftest database-type-that-is-not-a-type-name-is-quoted-test
+ (testing "a cast target that could escape the cast is quoted as an identifier instead of spliced"
+ ;; The database then refuses it as an unknown type, which is the point: the text never becomes
+ ;; SQL of its own.
+ (let [{:keys [query]} (rows-query :postgres
+ [{:name "a" :cast_type "INT) FROM secrets; --"}]
+ ["A"]
+ [{"a" 1}])]
+ (is (str/includes? query "CAST(? AS \"INT) FROM secrets; --\")") (pr-str query))
+ (is (not (str/includes? query "AS INT) FROM secrets")) (pr-str query)))))
+
+(deftest alias-comes-from-sql-names-value-from-declared-name-test
+ (testing "the alias is the warehouse's spelling; the cell is looked up by the author's spelling"
+ ;; The two differ whenever the engine folded the identifier — upper on H2 and Snowflake, lower on
+ ;; Postgres. A row carrying both spellings pins which one the lookup uses.
+ (let [{:keys [query params]} (rows-query :postgres
+ [{:name "name" :cast_type "VARCHAR(255)"}]
+ ["NAME"]
+ [{"name" "by-declared-name"
+ "NAME" "by-sql-name"}])]
+ (is (= ["by-declared-name"] params))
+ (is (str/includes? query "\"NAME\"") (pr-str query))
+ (is (not (str/includes? query "\"name\"")) (pr-str query)))))
+
+(deftest nil-cell-is-sql-null-test
+ (testing "a nil cell renders as NULL rather than as a parameter"
+ (let [{:keys [query params]} (rows-query :postgres
+ [{:name "a" :cast_type "INTEGER"}]
+ ["A"]
+ [{"a" nil}])]
+ (is (str/includes? query "CAST(NULL AS INTEGER)") (pr-str query))
+ (is (= [] params))))
+ (testing "a column the row omits is NULL too — a short row is not an error"
+ (let [{:keys [query params]} (rows-query :postgres
+ [{:name "a" :cast_type "INTEGER"}
+ {:name "b" :cast_type "INTEGER"}]
+ ["A" "B"]
+ [{"a" 1}])]
+ (is (str/includes? query "CAST(NULL AS INTEGER)") (pr-str query))
+ (is (= [1] params)))))
+
+(deftest no-rows-is-an-empty-result-with-the-declared-columns-test
+ (testing "an empty rows list still names every column, and returns nothing"
+ (let [{:keys [query params]} (rows-query :postgres
+ [{:name "id" :cast_type "INTEGER"}
+ {:name "name" :cast_type "VARCHAR(255)"}]
+ ["ID" "NAME"]
+ [])]
+ (is (str/includes? query "CAST(NULL AS INTEGER) AS \"ID\"") (pr-str query))
+ (is (str/includes? query "CAST(NULL AS VARCHAR(255)) AS \"NAME\"") (pr-str query))
+ (is (str/includes? query "WHERE 1 = 0") (pr-str query))
+ (is (= [] params)))))
+
+(deftest several-rows-are-unioned-test
+ (testing "one SELECT per row, joined with UNION ALL, parameters in row-major order"
+ (let [{:keys [query params]} (rows-query :postgres
+ [{:name "id" :cast_type "INTEGER"}
+ {:name "name" :cast_type "VARCHAR(255)"}]
+ ["ID" "NAME"]
+ [{"id" 1 "name" "a"}
+ {"id" 2 "name" "b"}
+ {"id" 3 "name" "c"}])]
+ (is (= 2 (count (re-seq #"UNION ALL" query))) (pr-str query))
+ (is (= [1 "a" 2 "b" 3 "c"] params))))
+ (testing "a single row is one SELECT with no set operation"
+ (let [{:keys [query]} (rows-query :postgres
+ [{:name "id" :cast_type "INTEGER"}]
+ ["ID"]
+ [{"id" 1}])]
+ (is (not (str/includes? query "UNION")) (pr-str query)))))
+
+(deftest declared-database-type-is-the-cast-target-test
+ (testing "each cell is cast to the type its column declares"
+ (let [{:keys [query]} (rows-query :postgres
+ [{:name "id" :cast_type "INTEGER"}
+ {:name "price" :cast_type "DECIMAL(10, 2)"}
+ {:name "at" :cast_type "TIMESTAMP WITH TIME ZONE"}]
+ ["ID" "PRICE" "AT"]
+ [{"id" 1 "price" 2 "at" "2024-01-01"}])]
+ (is (str/includes? query "CAST(? AS INTEGER)") (pr-str query))
+ (is (str/includes? query "CAST(? AS DECIMAL(10, 2))") (pr-str query))
+ (is (str/includes? query "CAST(? AS TIMESTAMP WITH TIME ZONE)") (pr-str query)))))
+
+(deftest parameterization-does-not-depend-on-the-dialect-test
+ (testing "values are parameters on every driver; only the quoting differs"
+ (doseq [driver [:postgres :h2]]
+ (testing driver
+ (let [{:keys [query params]} (rows-query driver
+ [{:name "note" :cast_type "VARCHAR(255)"}]
+ ["NOTE"]
+ [{"note" "'); DROP TABLE users; --"}])]
+ (is (= ["'); DROP TABLE users; --"] params))
+ (is (str/includes? query "CAST(? AS VARCHAR(255))") (pr-str query))
+ (is (not (str/includes? query "DROP")) (pr-str query)))))))
+
+;;; ------------------------------------ Queries over the temp tables ------------------------------------
+
+(deftest comparison-query-compares-both-directions-test
+ (testing "rows are grouped over the declared columns and kept when the two sides disagree"
+ (let [{:keys [query params]} (transform-testing.compile/comparison-query :postgres "out" "exp" ["ID" "NAME"])]
+ (is (= [] params))
+ (is (str/includes? query "UNION ALL") (pr-str query))
+ (is (str/includes? query "GROUP BY \"ID\", \"NAME\"") (pr-str query))
+ (is (str/includes? query "HAVING SUM(\"__mb_src\") <> 0") (pr-str query))))
+ (testing "a column name the warehouse folded to something dotted stays one identifier"
+ (let [{:keys [query]} (transform-testing.compile/comparison-query :postgres "out" "exp" ["a.b"])]
+ (is (str/includes? query "GROUP BY \"a.b\"") (pr-str query))
+ (is (not (str/includes? query "\"a\".\"b\"")) (pr-str query)))))
+
+(deftest row-count-and-columns-queries-test
+ (testing "the row count is a plain COUNT over the table"
+ (let [{:keys [query params]} (transform-testing.compile/row-count-query :postgres "out")]
+ (is (= "SELECT COUNT(1) AS \"__mb_count\" FROM \"out\"" query))
+ (is (= [] params))))
+ (testing "the columns probe selects everything and returns nothing"
+ (let [{:keys [query params]} (transform-testing.compile/columns-query :postgres "out")]
+ (is (= "SELECT * FROM \"out\" WHERE 1 = 0" query))
+ (is (= [] params))))
+ (testing "a temp table name is quoted whole, `#` and all"
+ (is (str/includes? (:query (transform-testing.compile/columns-query :postgres "#mb_test_1"))
+ "\"#mb_test_1\""))))
+
+;;; ------------------------------------------- compile-input -------------------------------------------
+
+(deftest compile-input-sql-is-passed-through-test
+ (testing "a :sql input compiles to its own SQL with no parameters"
+ (is (= {:query "SELECT 1 AS x" :params []}
+ (transform-testing.compile/compile-input
+ :h2
+ {:table {:schema "PUBLIC" :name "PEOPLE"}
+ :format :sql
+ :sql "SELECT 1 AS x"})))))
+
+(deftest compile-input-rows-names-columns-as-declared-test
+ (testing "a :rows input names each column exactly as the author declared it"
+ (let [{:keys [query params]} (transform-testing.compile/compile-input
+ :postgres
+ {:table {:schema "public" :name "people"}
+ :format :rows
+ :columns [{:name "id" :cast_type "INTEGER"}]
+ :rows [{"id" 1}]})]
+ (is (str/includes? query "CAST(? AS INTEGER) AS \"id\"") (pr-str query))
+ (is (= [1] params)))))
+
+(deftest driver-keywords-need-no-connection-test
+ (testing "the drivers these tests format for load and initialize without a database"
+ (is (= :postgres (driver/the-initialized-driver :postgres)))
+ (is (= :h2 (driver/the-initialized-driver :h2)))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/errors_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/errors_test.clj
new file mode 100644
index 00000000000..6d4947a3d45
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/errors_test.clj
@@ -0,0 +1,235 @@
+(ns metabase-enterprise.transform-testing.errors-test
+ "The `:error-type` vocabulary, the `ex` constructor, and warehouse-message remapping.
+ Pure — no app-db, no warehouse, no driver."
+ (:require
+ [clojure.test :refer [deftest is testing]]
+ [metabase-enterprise.transform-testing.errors :as errors]))
+
+;;; -------------------------------------------- checked --------------------------------------------
+
+(deftest checked-accepts-every-declared-type-test
+ (testing "`checked` returns its argument for every member of `all`"
+ (doseq [error-type errors/all]
+ (testing (str error-type)
+ (is (= error-type (errors/checked error-type)))))))
+
+(deftest checked-rejects-undeclared-type-test
+ (testing "an undeclared keyword throws, carrying the offender in ex-data"
+ (is (thrown? clojure.lang.ExceptionInfo (errors/checked ::not-a-real-error)))
+ (is (= {:invalid-error-type ::not-a-real-error}
+ (try (errors/checked ::not-a-real-error)
+ (catch clojure.lang.ExceptionInfo e (ex-data e))))))
+ (testing "nil is not a declared error type"
+ (is (thrown? clojure.lang.ExceptionInfo (errors/checked nil))))
+ (testing "a same-named keyword in another namespace is not a member"
+ (is (thrown? clojure.lang.ExceptionInfo (errors/checked ::setup-failed)))))
+
+;;; ------------------------------------------ status-code ------------------------------------------
+
+(def ^:private expected-status
+ "The status every declared error type must map to. Exhaustive over `all` by assertion below, so a
+ new error type with no mapping fails here instead of being absorbed by the 500 fallback."
+ {::errors/unknown-column 400
+ ::errors/ambiguous-column 400
+ ::errors/missing-inputs 400
+ ::errors/unused-inputs 400
+ ::errors/duplicate-input-table 400
+ ::errors/unparseable-source 400
+ ::errors/unremapped-reference 400
+ ::errors/unsupported-transform 422
+ ::errors/unsupported-driver 422
+ ::errors/transform-failed 422
+ ::errors/setup-failed 422
+ ::errors/expectation-failed 422
+ ::errors/unsupported-format 501})
+
+(defn- types-with-status
+ "The members of `all` that `status-code` maps to `status`."
+ [status]
+ (into #{} (filter #(= status (errors/status-code %))) errors/all))
+
+(deftest status-code-is-exhaustive-over-all-test
+ (testing "every declared error type has an expected status, and no stale entry lingers here"
+ (is (= errors/all (set (keys expected-status)))))
+ (doseq [[error-type expected] expected-status]
+ (testing (str error-type)
+ (is (= expected (errors/status-code error-type))))))
+
+(deftest status-code-classes-test
+ (testing "authoring errors — the caller can fix the test — are 400"
+ (is (= #{::errors/unknown-column
+ ::errors/ambiguous-column
+ ::errors/missing-inputs
+ ::errors/unused-inputs
+ ::errors/duplicate-input-table
+ ::errors/unparseable-source
+ ::errors/unremapped-reference}
+ (types-with-status 400))))
+ (testing "environment errors — the transform or its database prevents a run here — are 422"
+ (is (= #{::errors/unsupported-transform
+ ::errors/unsupported-driver
+ ::errors/transform-failed
+ ::errors/setup-failed
+ ::errors/expectation-failed}
+ (types-with-status 422))))
+ (testing "a form that is not built yet is 501"
+ (is (= #{::errors/unsupported-format}
+ (types-with-status 501))))
+ (testing "nothing is declared 500"
+ ;; `api-exception-response` structures a body only for a non-500 status carrying `:error-code`,
+ ;; so a type declared 500 would lose its code on the wire.
+ (is (= #{} (types-with-status 500)))))
+
+(deftest status-code-falls-back-to-500-test
+ (testing "an unknown error type"
+ (is (= 500 (errors/status-code ::not-a-real-error))))
+ (testing "an untyped exception"
+ (is (= 500 (errors/status-code nil)))))
+
+;;; --------------------------------------------- code ----------------------------------------------
+
+(def ^:private expected-code
+ "The wire spelling every declared error type must publish. Exhaustive over `all` by assertion
+ below. Written out rather than derived: a test that recomputed the prefix would agree with any
+ rename, and the point of this one is that a client's vocabulary cannot change silently."
+ {::errors/unknown-column "transform-test.unknown-column"
+ ::errors/ambiguous-column "transform-test.ambiguous-column"
+ ::errors/missing-inputs "transform-test.missing-inputs"
+ ::errors/unused-inputs "transform-test.unused-inputs"
+ ::errors/duplicate-input-table "transform-test.duplicate-input-table"
+ ::errors/unparseable-source "transform-test.unparseable-source"
+ ::errors/unremapped-reference "transform-test.unremapped-reference"
+ ::errors/unsupported-transform "transform-test.unsupported-transform"
+ ::errors/unsupported-driver "transform-test.unsupported-driver"
+ ::errors/transform-failed "transform-test.transform-failed"
+ ::errors/setup-failed "transform-test.setup-failed"
+ ::errors/expectation-failed "transform-test.expectation-failed"
+ ::errors/unsupported-format "transform-test.unsupported-format"})
+
+(deftest code-is-exhaustive-over-all-test
+ (testing "every declared error type has a pinned wire spelling, and no stale entry lingers here"
+ (is (= errors/all (set (keys expected-code)))))
+ (doseq [[error-type expected] expected-code]
+ (testing (str error-type)
+ (is (= expected (errors/code error-type))))))
+
+(deftest code-carries-no-clojure-namespace-test
+ (testing "a code is the published vocabulary, not a var's source location"
+ (doseq [error-type errors/all]
+ (let [c (errors/code error-type)]
+ (testing (str error-type)
+ (is (string? c))
+ (is (not (re-find #"/" c)))
+ (is (not (re-find #"metabase" c))))))))
+
+(deftest code-does-not-throw-on-an-undeclared-type-test
+ (testing "an undeclared type still yields a well-formed code"
+ ;; Deliberately not routed through `checked`: this runs on the refusal path, where a throw
+ ;; would turn a typed refusal into a 500 and lose the very code it was raised to carry.
+ (is (= "transform-test.not-a-real-error" (errors/code ::not-a-real-error)))))
+
+(deftest code-is-injective-test
+ (testing "two error types cannot share a code"
+ (is (= (count errors/all) (count (into #{} (map errors/code) errors/all))))))
+
+;;; ----------------------------------------------- ex ----------------------------------------------
+
+(deftest ex-rejects-literal-undeclared-type-at-expansion-test
+ (testing "a literal undeclared keyword fails while expanding, not when the form runs"
+ ;; The 3-arity only re-emits a 4-arity call, so the check lands one expansion deeper: full
+ ;; `macroexpand` for the 3-arity, `macroexpand-1` for the 4-arity.
+ ;; A macro that throws while expanding surfaces as a CompilerException wrapping the real one,
+ ;; so the typed failure is the cause rather than the thrown class.
+ (doseq [form ['(metabase-enterprise.transform-testing.errors/ex ::undeclared "boom" {})
+ '(metabase-enterprise.transform-testing.errors/ex ::undeclared "boom" {} nil)]]
+ (let [outcome (try (macroexpand form)
+ (catch clojure.lang.Compiler$CompilerException e e))
+ cause (when (instance? clojure.lang.Compiler$CompilerException outcome)
+ (ex-cause outcome))]
+ (is (instance? clojure.lang.Compiler$CompilerException outcome)
+ "expanding an undeclared type must fail at macro-expansion, not at call time")
+ (is (instance? clojure.lang.ExceptionInfo cause))
+ (is (= ::undeclared (:invalid-error-type (ex-data cause))))))))
+
+(deftest ex-builds-typed-exception-info-test
+ (let [e (errors/ex ::errors/setup-failed "boom" {:transform-id 1})]
+ (is (instance? clojure.lang.ExceptionInfo e))
+ (is (= "boom" (ex-message e)))
+ (testing "the error type is assoc'd into the caller's data, which is otherwise preserved"
+ (is (= {:transform-id 1 :error-type ::errors/setup-failed} (ex-data e))))
+ (testing "the 3-arity leaves the cause nil"
+ (is (nil? (ex-cause e))))))
+
+(deftest ex-attaches-cause-test
+ (let [cause (Exception. "underlying")
+ e (errors/ex ::errors/transform-failed "boom" {} cause)]
+ (is (identical? cause (ex-cause e)))
+ (is (= ::errors/transform-failed (:error-type (ex-data e))))))
+
+(deftest ex-checks-computed-type-at-runtime-test
+ (testing "a non-literal error type compiles, and is checked when the form runs"
+ (is (thrown? clojure.lang.ExceptionInfo
+ (errors/ex (identity ::undeclared) "boom" {})))
+ (is (= {:invalid-error-type ::undeclared}
+ (try (errors/ex (identity ::undeclared) "boom" {})
+ (catch clojure.lang.ExceptionInfo e (ex-data e))))))
+ (testing "a declared computed type is accepted"
+ (is (= ::errors/setup-failed
+ (:error-type (ex-data (errors/ex (identity ::errors/setup-failed) "boom" {})))))))
+
+;;; ------------------------------------------ remap-message ----------------------------------------
+
+(deftest remap-message-replaces-temp-name-test
+ (is (= "Table PUBLIC.ORDERS not found"
+ (errors/remap-message "Table mb_test_abc not found" {"mb_test_abc" "PUBLIC.ORDERS"}))))
+
+(deftest remap-message-ignores-case-test
+ (testing "Snowflake and H2 upper-case the generated names; the map is keyed by the minted name"
+ (is (= "Table PUBLIC.ORDERS not found"
+ (errors/remap-message "Table MB_TEST_ABC not found" {"mb_test_abc" "PUBLIC.ORDERS"}))))
+ (testing "mixed case too"
+ (is (= "Table PUBLIC.ORDERS not found"
+ (errors/remap-message "Table Mb_Test_Abc not found" {"mb_test_abc" "PUBLIC.ORDERS"})))))
+
+(deftest remap-message-replaces-several-names-test
+ (is (= "cannot join PUBLIC.ORDERS to PUBLIC.PEOPLE"
+ (errors/remap-message "cannot join mb_test_abc to mb_test_def"
+ {"mb_test_abc" "PUBLIC.ORDERS"
+ "mb_test_def" "PUBLIC.PEOPLE"}))))
+
+(deftest remap-message-replaces-every-occurrence-test
+ (is (= "ORDERS, ORDERS and ORDERS"
+ (errors/remap-message "mb_test_abc, mb_test_abc and mb_test_abc"
+ {"mb_test_abc" "ORDERS"}))))
+
+(deftest remap-message-matches-whole-words-only-test
+ (testing "an identifier that merely starts with the temp name is left alone"
+ (is (= "Table mb_test_abc_extra not found"
+ (errors/remap-message "Table mb_test_abc_extra not found" {"mb_test_abc" "ORDERS"}))))
+ (testing "one that merely ends with it, likewise"
+ (is (= "Table x_mb_test_abc not found"
+ (errors/remap-message "Table x_mb_test_abc not found" {"mb_test_abc" "ORDERS"}))))
+ (testing "non-word delimiters are boundaries, so a quoted or schema-qualified name still matches"
+ (is (= "Table \"ORDERS\" not found"
+ (errors/remap-message "Table \"mb_test_abc\" not found" {"mb_test_abc" "ORDERS"})))
+ (is (= "Table PUBLIC.ORDERS not found"
+ (errors/remap-message "Table PUBLIC.mb_test_abc not found" {"mb_test_abc" "ORDERS"})))))
+
+(deftest remap-message-tolerates-nil-message-test
+ (testing "a driver exception with no message does not blow up the rethrow path"
+ ;; nil in, nil out: coercing to "" would be indistinguishable from a driver that really said
+ ;; nothing, leaving the caller unable to substitute something useful.
+ (is (nil? (errors/remap-message nil {"mb_test_abc" "ORDERS"})))))
+
+(deftest remap-message-inserts-logical-name-literally-test
+ (testing "$1 in the logical name is a backreference to Matcher/replaceAll unless quoted"
+ (is (= "Table PUBLIC.ORDERS$1 not found"
+ (errors/remap-message "Table mb_test_abc not found" {"mb_test_abc" "PUBLIC.ORDERS$1"}))))
+ (testing "a backslash in the logical name is an escape to Matcher/replaceAll unless quoted"
+ (is (= "Table PUBLIC.ORDERS\\X not found"
+ (errors/remap-message "Table mb_test_abc not found" {"mb_test_abc" "PUBLIC.ORDERS\\X"})))))
+
+(deftest remap-message-with-empty-map-test
+ (testing "nothing to remap returns the message unchanged"
+ (is (= "Table mb_test_abc not found"
+ (errors/remap-message "Table mb_test_abc not found" {})))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/expectations/empty_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/expectations/empty_test.clj
new file mode 100644
index 00000000000..05235c6f6cb
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/expectations/empty_test.clj
@@ -0,0 +1,117 @@
+(ns metabase-enterprise.transform-testing.expectations.empty-test
+ "The `empty` expectation: what it asks to run, and what it makes of the answer. Pure — no database
+ and no warehouse connection; `probes` only parses and rewrites SQL."
+ (:require
+ [clojure.string :as str]
+ [clojure.test :refer [deftest is testing use-fixtures]]
+ [metabase-enterprise.transform-testing.expectations.empty]
+ [metabase-enterprise.transform-testing.expectations.protocol :as expectations.protocol]
+ [metabase-enterprise.transform-testing.expectations.report :as expectations.report]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase.driver :as driver]
+ [metabase.lib.core :as lib]
+ [metabase.sql-tools.macaw.references]
+ [metabase.util :as u]))
+
+;; The expectation under test, and the namespace registering the schema its rewrite guard validates against.
+(comment
+ metabase.sql-tools.macaw.references/keep-me
+ metabase-enterprise.transform-testing.expectations.empty/keep-me)
+
+(set! *warn-on-reflection* true)
+
+;; Rewriting a table reference dispatches on an initialized driver. No connection is opened.
+(use-fixtures :once (fn [f] (driver/the-initialized-driver :h2) (f)))
+
+(defn- empty-expectation
+ "One `empty` record, normalized and validated the way the model's `:in` transform does it, rather
+ than built with the generated constructor."
+ [m]
+ (expectations.protocol/build (lib/normalize ::transform-testing.schema/expectation m)))
+
+(def ^:private an-expectation
+ (delay (empty-expectation {:type "empty"
+ :name "no orphan rows"
+ :sql "SELECT * FROM PEOPLE WHERE ID IS NULL"})))
+
+;;; --------------------------------------------- interpret ---------------------------------------------
+
+(def ^:private violation-columns
+ [{:name "ID" :database_type "INTEGER"}
+ {:name "NAME" :database_type "CHARACTER VARYING"}
+ {:name "AMOUNT" :database_type "NUMERIC"}])
+
+(defn- interpret-violations
+ "`interpret` over `rows`, as the runner delivers them: the probe's rows and its column metadata."
+ [rows]
+ (expectations.protocol/interpret @an-expectation
+ {}
+ {:violations {:rows rows :columns violation-columns}}))
+
+(deftest interpret-no-rows-test
+ (let [result (interpret-violations [])]
+ (is (= {:name "no orphan rows" :type :empty :status :passed} result))
+ (testing "a pass carries no sample: there is nothing to show"
+ (is (not (contains? result :sample)))
+ (is (not (contains? result :truncated))))
+ (testing "and no columns: they describe a sample that is not there"
+ (is (not (contains? result :columns)))))
+ (testing "no rows at all reads the same as an empty list"
+ (is (= {:name "no orphan rows" :type :empty :status :passed}
+ (interpret-violations nil)))))
+
+(deftest interpret-rows-test
+ (let [result (interpret-violations [[1 "a" nil] [2 "b" (java.math.BigDecimal. "1.50")]])]
+ (is (= :failed (:status result)))
+ (testing "the result always identifies itself by the author's name and its type"
+ (is (= "no orphan rows" (:name result)))
+ (is (= :empty (:type result))))
+ (testing "the sample names its cells, as an equals diff does"
+ (is (= [{"ID" 1 "NAME" "a" "AMOUNT" nil}
+ {"ID" 2 "NAME" "b" "AMOUNT" "1.50"}]
+ (:sample result))))
+ (testing "the columns carry the warehouse's own type for each"
+ (is (= violation-columns (:columns result))))
+ (is (= 0 (:truncated result)))))
+
+(deftest interpret-truncation-test
+ (let [rows (mapv (fn [i] [i nil nil]) (range (+ expectations.report/row-cap 4)))
+ result (interpret-violations rows)]
+ (is (= :failed (:status result)))
+ (is (= expectations.report/row-cap (count (:sample result))))
+ (is (= 4 (:truncated result)))
+ (testing "the sample is the first rows, not an arbitrary selection"
+ (is (= (mapv (fn [[i]] {"ID" i "NAME" nil "AMOUNT" nil})
+ (take expectations.report/row-cap rows))
+ (:sample result))))))
+
+;;; ----------------------------------------------- probes -----------------------------------------------
+
+(def ^:private replacements
+ "PEOPLE stands in for the run's input temp table; the shape is what `compile/table-replacements`
+ produces."
+ {{:table "PEOPLE"} {:db nil :schema nil :table "TMP_IN_1"}})
+
+(defn- probes []
+ (expectations.protocol/probes @an-expectation
+ {:driver :h2
+ :output-table "TMP_OUT"
+ :output-columns ["ID"]
+ :replacements replacements}))
+
+(deftest probes-shape-test
+ (let [ps (probes)]
+ (testing "one probe"
+ (is (= [:violations] (keys ps))))
+ (let [{:keys [params max-rows]} (:violations ps)]
+ (is (= [] params))
+ (testing "the cap is the report's, so a runaway failure cannot be read into memory whole"
+ (is (= expectations.report/row-cap max-rows))))))
+
+(deftest probes-query-is-the-authors-sql-rewritten-test
+ (let [query (u/lower-case-en (:query (:violations (probes))))]
+ (testing "the author's SQL is put through table replacement"
+ (is (str/includes? query "tmp_in_1"))
+ (is (not (str/includes? query "people"))))
+ (testing "the rest of the author's SQL survives the rewrite"
+ (is (str/includes? query "is null")))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/expectations/equals_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/expectations/equals_test.clj
new file mode 100644
index 00000000000..f03f7c1e617
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/expectations/equals_test.clj
@@ -0,0 +1,252 @@
+(ns metabase-enterprise.transform-testing.expectations.equals-test
+ "The `equals` expectation's two pure halves: `interpret`, over synthetic probe results, and
+ `resolve-columns`, over a synthetic list of output-table column names. No warehouse."
+ (:require
+ [clojure.test :refer [deftest is testing]]
+ [metabase-enterprise.transform-testing.errors :as transform-testing.errors]
+ [metabase-enterprise.transform-testing.expectations.equals :as expectations.equals]
+ [metabase-enterprise.transform-testing.expectations.protocol :as expectations.protocol]
+ [metabase-enterprise.transform-testing.expectations.report :as expectations.report]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase.lib.core :as lib])
+ (:import
+ (clojure.lang ExceptionInfo)))
+
+;;; -------------------------------------------- Harness --------------------------------------------
+
+(def ^:private id+name
+ ;; `cast_type` is raw SQL text — a cast target, not a Metabase base type.
+ [{:name "id" :cast_type "INTEGER"}
+ {:name "name" :cast_type "VARCHAR"}])
+
+(defn- build
+ "The record for `m`, normalized and validated the way the model's `:in` transform does it. The
+ generated constructors are forbidden: a record built around the schema is a value nothing checked."
+ [m]
+ (expectations.protocol/build (lib/normalize ::transform-testing.schema/expectation m)))
+
+(defn- equals-rows
+ "An `:equals`/`:rows` record."
+ ([columns rows] (equals-rows "output" columns rows))
+ ([expectation-name columns rows]
+ (build {:type :equals
+ :name expectation-name
+ :format :rows
+ :columns columns
+ :rows rows})))
+
+(def ^:private comparison-columns
+ "The comparison probe's own column metadata: the resolved output columns, then the delta. The
+ spellings are the warehouse's, which is the point — a report names columns as the author did."
+ [{:name "ID" :database_type "INTEGER"}
+ {:name "NAME" :database_type "CHARACTER VARYING"}
+ {:name "__mb_delta" :database_type "BIGINT"}])
+
+(def ^:private output-columns
+ "The output table's own columns, as `table-columns` reports them: no delta, warehouse spellings."
+ [{:name "ID" :database_type "INTEGER"}
+ {:name "NAME" :database_type "CHARACTER VARYING"}])
+
+(def ^:private reported-columns
+ "What `interpret` should report for [[id+name]]: the author's names, the warehouse's types."
+ [{:name "id" :database_type "INTEGER"}
+ {:name "name" :database_type "CHARACTER VARYING"}])
+
+(defn- probe-results
+ "Probe results as the runner delivers them: each probe's rows alongside its own column metadata."
+ ([comparison] (probe-results comparison [[1]]))
+ ([comparison actual-count] (probe-results comparison actual-count comparison-columns))
+ ([comparison actual-count columns]
+ (cond-> {:comparison {:rows comparison :columns columns}}
+ actual-count (assoc :actual-count {:rows actual-count
+ :columns [{:name "__mb_count" :database_type "BIGINT"}]}))))
+
+(defn- interpret-comparison
+ "`interpret` over a synthetic comparison probe: each row is the declared columns followed by the
+ signed multiplicity."
+ [comparison]
+ (expectations.protocol/interpret (equals-rows id+name [{"id" 1 "name" "abc"}])
+ {:output-columns output-columns}
+ (probe-results comparison)))
+
+(defn- resolve-cols
+ "`resolve-columns` over `output-columns`, written as bare names."
+ [columns output-columns]
+ (expectations.equals/resolve-columns (equals-rows "cols" columns [])
+ (mapv #(array-map :name % :database_type "INTEGER") output-columns)))
+
+(defn- caught
+ "The `ExceptionInfo` thrown by `f`."
+ [f]
+ (let [e (try (f) nil (catch ExceptionInfo e e))]
+ (is (some? e) "expected a throw")
+ e))
+
+;;; ------------------------------------------- interpret -------------------------------------------
+
+(deftest interpret-no-differences-passes-test
+ (testing "an empty comparison is a pass, with every key still present"
+ ;; Full-map equality, so this also pins that the shape does not change between pass and fail:
+ ;; a consumer never has to tell an absent key from an empty one.
+ (is (= {:name "output"
+ :type :equals
+ :status :passed
+ :columns reported-columns
+ :row-counts {:actual 1 :expected 1}
+ :extra-rows []
+ :missing-rows []
+ :truncated 0
+ :cell-mismatches []}
+ (interpret-comparison [])))))
+
+(deftest interpret-row-counts-test
+ (let [two-rows [{"id" 1 "name" "abc"} {"id" 2 "name" "def"}]
+ counts #(:row-counts (expectations.protocol/interpret (equals-rows id+name two-rows)
+ {:output-columns output-columns} %))]
+ (testing ":expected counts the declared rows; :actual comes from the row-count probe"
+ (is (= {:actual 7 :expected 2} (counts (probe-results [] [[7]])))))
+ (testing "a probe count is coerced to a long — JDBC may hand back a BigDecimal"
+ (is (= {:actual 7 :expected 2} (counts (probe-results [] [[7M]])))))
+ (testing "a missing or empty probe falls back to 0 rather than throwing"
+ (is (= {:actual 0 :expected 2} (counts (probe-results [] nil))))
+ (is (= {:actual 0 :expected 2} (counts (probe-results [] []))))
+ (is (= {:actual 0 :expected 2} (counts (probe-results [] [[]])))))))
+
+(deftest interpret-multiset-expansion-test
+ ;; Why the comparison is a multiset difference rather than EXCEPT: EXCEPT deduplicates, so an
+ ;; output holding a row three times passes against an expectation holding it once. The delta
+ ;; carries the multiplicity, and interpret expands it back to one reported row per occurrence.
+ (testing "a positive delta is that many rows the output has too many"
+ (let [result (interpret-comparison [[1 "abc" 3]])]
+ (is (= :failed (:status result)))
+ (is (= (repeat 3 {"id" 1 "name" "abc"}) (:extra-rows result)))
+ (is (= [] (:missing-rows result)))
+ (is (= 0 (:truncated result)))))
+ (testing "a negative delta is that many rows the output has too few"
+ (let [result (interpret-comparison [[2 "def" -2]])]
+ (is (= :failed (:status result)))
+ (is (= [] (:extra-rows result)))
+ (is (= (repeat 2 {"id" 2 "name" "def"}) (:missing-rows result))))))
+
+(deftest interpret-mixed-deltas-test
+ (testing "positive and negative deltas split between the two lists, in comparison order"
+ (let [result (interpret-comparison [[1 "abc" 2] [2 "def" -1] [3 "ghi" 1]])]
+ (is (= [{"id" 1 "name" "abc"} {"id" 1 "name" "abc"} {"id" 3 "name" "ghi"}]
+ (:extra-rows result)))
+ (is (= [{"id" 2 "name" "def"}] (:missing-rows result)))
+ (is (= 0 (:truncated result))))))
+
+(deftest interpret-cell-mismatches-test
+ ;; Pairing a missing row with an extra one is guesswork except when there is exactly one of each.
+ ;; Sorting both sides and pairing positionally for a larger diff reads as authoritative and is
+ ;; arbitrary, so nothing is reported instead.
+ (testing "one against one names only the columns whose values differ"
+ (let [result (interpret-comparison [[1 "abc" 1] [1 "xyz" -1]])]
+ (is (= [{:column "name" :expected "xyz" :actual "abc"}] (:cell-mismatches result)))))
+ ;; The key is always present; "not paired" means it is empty, never missing.
+ (testing "two against two is not paired"
+ (is (= [] (:cell-mismatches (interpret-comparison [[1 "abc" 2] [1 "xyz" -2]])))))
+ (testing "one missing and nothing extra is not paired"
+ (is (= [] (:cell-mismatches (interpret-comparison [[1 "xyz" -1]])))))
+ (testing "one extra and nothing missing is not paired"
+ (is (= [] (:cell-mismatches (interpret-comparison [[1 "abc" 1]])))))
+ (testing "one against one differing in no column reports nothing"
+ (let [result (interpret-comparison [[1 "abc" 1] [1 "abc" -1]])]
+ (is (= [] (:cell-mismatches result)))
+ (is (= :failed (:status result))))))
+
+(deftest interpret-truncation-test
+ (testing "each list is capped at row-cap; :truncated counts what both lists dropped"
+ (let [result (interpret-comparison [[1 "abc" (+ expectations.report/row-cap 3)]
+ [2 "def" (- (+ expectations.report/row-cap 2))]])]
+ (is (= expectations.report/row-cap (count (:extra-rows result))))
+ (is (= expectations.report/row-cap (count (:missing-rows result))))
+ (is (= 5 (:truncated result)))
+ (is (= #{{"id" 1 "name" "abc"}} (set (:extra-rows result))))
+ (is (= #{{"id" 2 "name" "def"}} (set (:missing-rows result)))))))
+
+(deftest interpret-reports-declared-column-names-test
+ ;; Resolution to the warehouse's spelling builds the SQL and nothing else: a failure names the
+ ;; column the way the author wrote it, which is the way they can find it in their own test.
+ (let [expectation (equals-rows id+name [{"id" 1 "name" "abc"}])]
+ (is (= ["ID" "NAME"] (resolve-cols id+name ["ID" "NAME"])))
+ (is (= [{"id" 1 "name" "abc"}]
+ (:extra-rows (expectations.protocol/interpret expectation {:output-columns output-columns}
+ (probe-results [[1 "abc" 1]])))))))
+
+(deftest interpret-cell-rendering-test
+ (testing "a BigDecimal keeps its scale"
+ (let [expectation (equals-rows [{:name "amount" :cast_type "DECIMAL(10,2)"}] [{"amount" 1.5}])
+ result (expectations.protocol/interpret
+ expectation
+ {:output-columns [{:name "AMOUNT" :database_type "NUMERIC"}]}
+ (probe-results [[1.50M 1] [1.5M -1]] [[1]]
+ [{:name "AMOUNT" :database_type "NUMERIC"}
+ {:name "__mb_delta" :database_type "BIGINT"}]))]
+ (is (= [{"amount" "1.50"}] (:extra-rows result)))
+ (is (= [{"amount" "1.5"}] (:missing-rows result)))
+ (is (= [{:column "amount" :expected "1.5" :actual "1.50"}] (:cell-mismatches result)))))
+ (testing "nil stays nil"
+ (is (= [{"id" 1 "name" nil}] (:extra-rows (interpret-comparison [[1 nil 1]]))))))
+
+;;; ---------------------------------------- resolve-columns ----------------------------------------
+
+(deftest resolve-columns-exact-match-test
+ (testing "an exact match wins over a case-insensitive one, whatever the order"
+ (is (= ["id"] (resolve-cols [{:name "id" :cast_type "INTEGER"}] ["ID" "id"])))
+ (is (= ["ID"] (resolve-cols [{:name "ID" :cast_type "INTEGER"}] ["ID" "id"])))))
+
+(deftest resolve-columns-case-insensitive-test
+ (testing "H2 and Snowflake fold an unquoted identifier up"
+ (is (= ["ID" "NAME"] (resolve-cols id+name ["ID" "NAME"]))))
+ (testing "Postgres folds it down"
+ (is (= ["id" "name"] (resolve-cols [{:name "ID" :cast_type "INTEGER"}
+ {:name "NAME" :cast_type "VARCHAR"}]
+ ["id" "name"])))))
+
+(deftest resolve-columns-preserves-declared-order-test
+ (testing "resolved names come back in the order the author declared, not the table's"
+ (is (= ["NAME" "ID"] (resolve-cols [{:name "name" :cast_type "VARCHAR"}
+ {:name "id" :cast_type "INTEGER"}]
+ ["ID" "NAME"])))))
+
+(deftest resolve-columns-unknown-column-test
+ (testing "a column the output does not have is refused, listing what it does have"
+ (let [e (caught #(resolve-cols [{:name "nope" :cast_type "INTEGER"}] ["id" "name"]))]
+ (is (= ::transform-testing.errors/unknown-column (:error-type (ex-data e))))
+ (is (= {:expectation "cols" :column "nope" :available ["id" "name"]}
+ (dissoc (ex-data e) :error-type)))
+ (is (re-find #"compares column \"nope\"" (ex-message e)))
+ (is (re-find #"It has: id, name" (ex-message e))))))
+
+(deftest resolve-columns-ambiguous-case-test
+ ;; The output has both `id` and `ID`; `Id` matches neither exactly and both case-insensitively.
+ ;; Ambiguity is its own refusal: reporting it as unknown-column would tell the author the output
+ ;; "does not have" a column it arguably has twice over, and send them hunting for a typo.
+ (testing "two case-insensitive matches refuse rather than pick one"
+ (let [e (caught #(resolve-cols [{:name "Id" :cast_type "INTEGER"}] ["id" "ID"]))]
+ (is (= ::transform-testing.errors/ambiguous-column (:error-type (ex-data e))))
+ (is (= {:expectation "cols" :column "Id" :candidates ["id" "ID"]}
+ (dissoc (ex-data e) :error-type)))
+ (is (re-find #"matches more than one output column: id, ID" (ex-message e))))))
+
+;;; ------------------------------------------- EqualsSql -------------------------------------------
+
+(deftest equals-sql-is-unimplemented-test
+ (testing "the sql form builds, then refuses to produce probes"
+ (let [expectation (build {:type :equals
+ :name "sql form"
+ :format :sql
+ :sql "SELECT 1"})
+ e (caught #(expectations.protocol/probes expectation
+ {:driver :h2
+ :output-table "t"
+ :output-columns ["id"]
+ :replacements {}}))]
+ ;; Its own type, not an authoring error: the expectation is well-formed, the feature is
+ ;; simply not built. The author is told which form to use instead.
+ (is (= ::transform-testing.errors/unsupported-format (:error-type (ex-data e))))
+ (is (= {:expectation "sql form" :format :sql} (dissoc (ex-data e) :error-type)))
+ (is (re-find #"not implemented yet" (ex-message e)))
+ (is (re-find #"columns" (ex-message e)))
+ (is (re-find #"rows" (ex-message e))))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/expectations/report_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/expectations/report_test.clj
new file mode 100644
index 00000000000..f52c1f2aee1
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/expectations/report_test.clj
@@ -0,0 +1,89 @@
+(ns metabase-enterprise.transform-testing.expectations.report-test
+ "Rendering warehouse values into a failure report, and capping how much of one is carried. Pure —
+ no database."
+ (:require
+ [clojure.string :as str]
+ [clojure.test :refer [deftest is testing]]
+ [metabase-enterprise.transform-testing.expectations.report :as expectations.report]
+ [metabase.util :as u])
+ (:import
+ (java.math BigDecimal)
+ (java.sql Timestamp)
+ (java.time LocalDate)))
+
+(set! *warn-on-reflection* true)
+
+;;; ------------------------------------------------ cell ------------------------------------------------
+
+(deftest cell-passthrough-test
+ (testing "nil stays nil — a NULL cell must not become the string \"null\""
+ (is (nil? (expectations.report/cell nil))))
+ (testing "what JSON already carries goes through untouched"
+ (is (= "abc" (expectations.report/cell "abc")))
+ (is (= "" (expectations.report/cell "")))
+ (is (= 7 (expectations.report/cell 7)))
+ (is (= 7 (expectations.report/cell (long 7))))
+ (is (= 1.5 (expectations.report/cell 1.5)))
+ (is (true? (expectations.report/cell true)))
+ (is (false? (expectations.report/cell false)))))
+
+(deftest cell-bigdecimal-keeps-scale-test
+ (testing "a BigDecimal renders as its plain string with its scale intact"
+ (is (= "1.50" (expectations.report/cell (BigDecimal. "1.50"))))
+ (is (= "1.5" (expectations.report/cell (BigDecimal. "1.5"))))
+ (is (= "0.00" (expectations.report/cell (BigDecimal. "0.00"))))
+ (is (= "-2.500" (expectations.report/cell (BigDecimal. "-2.500")))))
+ (testing "1.50 and 1.5 stay distinguishable"
+ (is (not= (expectations.report/cell (BigDecimal. "1.50"))
+ (expectations.report/cell (BigDecimal. "1.5"))))))
+
+(deftest cell-bigdecimal-is-plain-not-scientific-test
+ (testing "a BigDecimal whose toString would use an exponent renders in plain form"
+ (doseq [s ["1E+3" "1.0E-8" "1E-10" "123456789E+10"]]
+ (let [rendered (expectations.report/cell (BigDecimal. ^String s))]
+ (is (string? rendered))
+ (is (not (str/includes? (u/upper-case-en rendered) "E"))
+ (str s " rendered as " (pr-str rendered))))))
+ (testing "the plain form is the value, not an approximation of it"
+ (is (= "1000" (expectations.report/cell (BigDecimal. "1E+3"))))
+ (is (= "0.000000010" (expectations.report/cell (BigDecimal. "1.0E-8"))))))
+
+(deftest cell-temporal-test
+ (testing "a temporal value becomes a string rather than leaking a Java object into JSON"
+ (doseq [v [(Timestamp/valueOf "2024-01-31 12:34:56") (LocalDate/of 2024 1 31)]]
+ (let [rendered (expectations.report/cell v)]
+ (is (string? rendered) (str (class v)))
+ (is (= (str v) rendered)))))
+ (is (= "2024-01-31" (expectations.report/cell (LocalDate/of 2024 1 31)))))
+
+;;; ----------------------------------------------- capped -----------------------------------------------
+
+(defn- numbered-rows
+ "`n` distinguishable rows, so a cap can be checked for taking the first ones and not just `n` of them."
+ [n]
+ (mapv (fn [i] [i]) (range n)))
+
+(deftest capped-under-the-cap-test
+ (let [rows (numbered-rows (dec expectations.report/row-cap))]
+ (is (= [rows 0] (expectations.report/capped rows)))))
+
+(deftest capped-at-the-cap-test
+ (testing "exactly at the cap nothing is dropped — the boundary is inclusive"
+ (let [rows (numbered-rows expectations.report/row-cap)]
+ (is (= [rows 0] (expectations.report/capped rows))))))
+
+(deftest capped-over-the-cap-test
+ (let [rows (numbered-rows (+ expectations.report/row-cap 7))
+ [kept dropped] (expectations.report/capped rows)]
+ (is (= expectations.report/row-cap (count kept)))
+ (is (= 7 dropped))
+ (testing "the rows kept are the first ones, in order"
+ (is (= (vec (take expectations.report/row-cap rows)) kept)))))
+
+(deftest capped-empty-test
+ (is (= [[] 0] (expectations.report/capped [])))
+ (is (= [[] 0] (expectations.report/capped nil))))
+
+(deftest capped-returns-a-vector-test
+ (testing "the sample is a vector, so a lazy seq cannot escape into the report"
+ (is (vector? (first (expectations.report/capped (range 3)))))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/models_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/models_test.clj
new file mode 100644
index 00000000000..8d761af3a04
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/models_test.clj
@@ -0,0 +1,212 @@
+(ns metabase-enterprise.transform-testing.models-test
+ "The `:model/TransformTest` column transforms and delete hook. Both JSON columns normalize and
+ validate on the way in and normalize on the way out, so every reader gets values the schema has
+ passed — including a serdes import, which goes through Toucan and never touches an API endpoint."
+ (:require
+ [clojure.test :refer [deftest is testing]]
+ [metabase-enterprise.transform-testing.expectations.protocol :as expectations.protocol]
+ [metabase.events.core :as events]
+ [metabase.test :as mt]
+ [toucan2.core :as t2]))
+
+(set! *warn-on-reflection* true)
+
+;;; ------------------------------------------------ Fixtures ------------------------------------------------
+
+(def ^:private inputs
+ [{:table {:schema "PUBLIC" :name "PEOPLE"}
+ :format "rows"
+ :columns [{:name "ID" :cast_type "INTEGER"}]
+ :rows [{"ID" 1}]}
+ {:table {:schema "PUBLIC" :name "ORDERS"}
+ :format "sql"
+ :sql "SELECT 1 AS ID"}])
+
+(def ^:private equals-expectation
+ ;; A warehouse column can be named 2024 or order-id. Keywordizing the row keys on the way back
+ ;; out would corrupt the data, so both spellings are here on purpose.
+ {:type "equals"
+ :name "output matches the fixture"
+ :format "rows"
+ :columns [{:name "ID" :cast_type "INTEGER"}
+ {:name "2024" :cast_type "VARCHAR(255)"}
+ {:name "order-id" :cast_type "BIGINT"}]
+ :rows [{"ID" 1 "2024" "jan" "order-id" 7}
+ {"ID" 2 "2024" nil "order-id" 8}]})
+
+(def ^:private empty-expectation
+ {:type "empty" :name "no orphan rows" :sql "SELECT * FROM OUT WHERE ID IS NULL"})
+
+(defn- saved-with
+ "Call `f` with a TransformTest carrying `expectations`, selected back through the model so its
+ `:out` transforms have run."
+ [expectations f]
+ (mt/with-temp [:model/Transform {transform-id :id} {}
+ :model/TransformTest {test-id :id} {:transform_id transform-id
+ :inputs inputs
+ :expectations expectations}]
+ (f (t2/select-one :model/TransformTest :id test-id))))
+
+;;; ------------------------------------------ Normalized, not raw ------------------------------------------
+
+(deftest expectations-read-back-normalized-test
+ (saved-with
+ [equals-expectation empty-expectation]
+ (fn [transform-test]
+ (let [expectations (:expectations transform-test)]
+ (is (= 2 (count expectations)))
+ (testing "a reader gets plain normalized data; the runner is what turns it into records"
+ (doseq [e expectations]
+ (is (map? e) (pr-str e))
+ (is (not (satisfies? expectations.protocol/Expectation e)) (pr-str e))
+ (is (keyword? (:type e)))))
+ (testing "and every one of them builds into a record that can run"
+ (doseq [e expectations]
+ (is (satisfies? expectations.protocol/Expectation (expectations.protocol/build e)))))))))
+
+;;; ------------------------------------------- Round-trip fidelity -------------------------------------------
+
+(deftest equals-expectation-round-trip-test
+ (saved-with
+ [equals-expectation]
+ (fn [transform-test]
+ (let [[e] (:expectations transform-test)]
+ (is (= "output matches the fixture" (:name e)))
+ (is (= :equals (:type e)))
+ (is (= :rows (:format e)))
+ (testing "columns keep their raw SQL type names"
+ (is (= [{:name "ID" :cast_type "INTEGER"}
+ {:name "2024" :cast_type "VARCHAR(255)"}
+ {:name "order-id" :cast_type "BIGINT"}]
+ (:columns e))))
+ (testing "row keys are still strings after the JSON round trip"
+ (is (= [{"ID" 1 "2024" "jan" "order-id" 7}
+ {"ID" 2 "2024" nil "order-id" 8}]
+ (:rows e)))
+ (is (every? string? (mapcat keys (:rows e)))))
+ (testing "a NULL cell survives as nil rather than being dropped"
+ (is (contains? (second (:rows e)) "2024"))
+ (is (nil? (get (second (:rows e)) "2024"))))))))
+
+(deftest empty-expectation-round-trip-test
+ (saved-with
+ [empty-expectation]
+ (fn [transform-test]
+ (let [[e] (:expectations transform-test)]
+ (is (= "no orphan rows" (:name e)))
+ (is (= :empty (:type e)))
+ (is (= "SELECT * FROM OUT WHERE ID IS NULL" (:sql e)))))))
+
+(deftest expectation-order-is-preserved-test
+ (testing "expectations come back in declared order — a failure report leads with the name"
+ (saved-with
+ [empty-expectation equals-expectation]
+ (fn [transform-test]
+ (is (= ["no orphan rows" "output matches the fixture"]
+ (mapv :name (:expectations transform-test))))))))
+
+(deftest inputs-round-trip-test
+ (testing ":inputs uses the plain json-column, so it reads back as data — normalized, not records"
+ (saved-with
+ [empty-expectation]
+ (fn [transform-test]
+ (let [[rows-input sql-input] (:inputs transform-test)]
+ (is (= {:table {:schema "PUBLIC" :name "PEOPLE"}
+ :format :rows
+ :columns [{:name "ID" :cast_type "INTEGER"}]
+ :rows [{"ID" 1}]}
+ rows-input))
+ (is (= {:table {:schema "PUBLIC" :name "ORDERS"} :format :sql :sql "SELECT 1 AS ID"}
+ sql-input))
+ (testing "an input's row keys are strings too"
+ (is (every? string? (mapcat keys (:rows rows-input)))))
+ (is (not (satisfies? expectations.protocol/Expectation rows-input))))))))
+
+;;; ----------------------------------------------- Rejection -----------------------------------------------
+
+(defn- caught
+ "The Throwable `thunk` threw, or nil."
+ [thunk]
+ (try (thunk) nil (catch Throwable t t)))
+
+(defn- causes
+ "`t` and everything it wraps. Toucan rebuilds a throw from a column transform to add its own
+ context, so match anywhere in the chain rather than on one exception."
+ [t]
+ (take-while some? (iterate ex-cause t)))
+
+(defn- valid-row [transform-id expectations]
+ {:transform_id transform-id
+ :creator_id (mt/user->id :rasta)
+ :name "a test"
+ :inputs []
+ :expectations expectations})
+
+(deftest schema-invalid-expectation-is-rejected-on-write-test
+ (testing "the :in transform validates, so a malformed expectation never reaches the column"
+ (mt/with-temp [:model/Transform {transform-id :id} {}]
+ (try
+ (doseq [[label bad]
+ [["no :sql on an empty expectation" {:type "empty" :name "x"}]
+ ["no :name" {:type "empty" :sql "SELECT 1"}]
+ ["a blank :name" {:type "empty" :name "" :sql "SELECT 1"}]
+ ["an undeclared key" {:type "empty" :name "x" :sql "SELECT 1" :nope 1}]
+ ["an unknown :type" {:type "nope" :name "x" :sql "SELECT 1"}]]]
+ (testing label
+ (is (some? (caught #(t2/insert! :model/TransformTest (valid-row transform-id [bad])))))))
+ (finally
+ ;; Raw: the model's before-delete hook reads the row back, which is the very thing under
+ ;; test here. Nothing should have been written, but a leftover row would break the
+ ;; Transform's own cleanup on its FK.
+ (t2/query {:delete-from :transform_test :where [:= :transform_id transform-id]}))))))
+
+(deftest duplicate-expectation-names-test
+ ;; Uniqueness is a property of the vector rather than of one expectation, so `::expectations`
+ ;; carries it as a predicate over the whole sequence.
+ (mt/with-temp [:model/Transform {transform-id :id} {}]
+ (try
+ (let [dupes [{:type "empty" :name "dup" :sql "SELECT 1"}
+ {:type "empty" :name "dup" :sql "SELECT 2"}]
+ e (caught #(t2/insert! :model/TransformTest (valid-row transform-id dupes)))]
+ (is (some? e) "two expectations sharing a name must not be storable")
+ (is (re-find #"unique" (pr-str (keep (comp :error ex-data) (causes e))))
+ (pr-str (mapv ex-message (causes e)))))
+ (finally
+ (t2/query {:delete-from :transform_test :where [:= :transform_id transform-id]})))))
+
+(deftest update-is-validated-too-test
+ (testing "an update goes through the same :in transform as an insert"
+ (mt/with-temp [:model/Transform {transform-id :id} {}
+ :model/TransformTest {test-id :id} {:transform_id transform-id
+ :inputs inputs
+ :expectations [empty-expectation]}]
+ (is (some? (caught #(t2/update! :model/TransformTest test-id {:expectations [{:type "empty" :name "x"}]}))))
+ (testing "and the stored value is unchanged"
+ (is (= ["no orphan rows"]
+ (mapv :name (:expectations (t2/select-one :model/TransformTest :id test-id)))))))))
+
+;;; ------------------------------------------------ Deletion ------------------------------------------------
+
+(deftest deleting-a-transform-deletes-its-tests-through-toucan-test
+ (testing "Deleting a transform takes its tests with it, through their own hook"
+ (let [published (atom [])]
+ (mt/with-temp [:model/Transform {transform-id :id} {:name "Orders Summary"}
+ :model/TransformTest {test-id :id} {:transform_id transform-id
+ :name "No nulls"
+ :inputs []
+ :expectations []}
+ :model/TransformTest {other-id :id} {:transform_id transform-id
+ :name "No dupes"
+ :inputs []
+ :expectations []}]
+ (with-redefs [events/publish-event! (fn [topic event] (swap! published conj [topic event]))]
+ (t2/delete! :model/Transform transform-id))
+ (testing "the rows are gone"
+ (is (false? (t2/exists? :model/TransformTest :id test-id)))
+ (is (false? (t2/exists? :model/TransformTest :id other-id))))
+ (testing "and each test published its own delete event on the way out"
+ (is (= #{test-id other-id}
+ (into #{}
+ (comp (filter (comp #{:event/transform-test-delete} first))
+ (map (comp :id :object second)))
+ @published))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/remapping_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/remapping_test.clj
new file mode 100644
index 00000000000..df16818c056
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/remapping_test.clj
@@ -0,0 +1,332 @@
+(ns ^:mb/driver-tests metabase-enterprise.transform-testing.remapping-test
+ (:require
+ [clojure.string :as str]
+ [clojure.test :refer :all]
+ [metabase-enterprise.transform-testing.compile :as transform-testing.compile]
+ [metabase-enterprise.transform-testing.runner :as transform-testing.runner]
+ [metabase-enterprise.transform-testing.validator :as transform-testing.validator]
+ [metabase.driver :as driver]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
+ [metabase.sql-tools.core :as sql-tools]
+ [metabase.sql-tools.macaw.references]
+ [metabase.test :as mt]
+ [metabase.util :as u])
+ (:import
+ (clojure.lang ExceptionInfo)))
+
+;; Guard B parses the rewritten SQL through the namespace that registers its result schema.
+(comment metabase.sql-tools.macaw.references/keep-me)
+
+;; `sql.normalize/default-schema`, which decides whether a bare table key is registered, dispatches
+;; on an initialized driver. These tests parse and rewrite SQL only — no connection is needed.
+(use-fixtures :once (fn [f] (driver/the-initialized-driver :h2) (f)))
+
+;;; -------------------------------------------- Harness --------------------------------------------
+
+(defn- rewrite
+ "`sql` with `replacements` applied, through the same call a test run makes."
+ [sql replacements]
+ (transform-testing.compile/replace-tables :h2 sql replacements))
+
+(defn- table-refs
+ "The tables `sql` reads, case-folded, as `{:schema :table}` tuples. Once a query has been
+ rewritten, every table in this set should be one of the run's temp tables."
+ [sql]
+ (into #{}
+ (map (fn [{:keys [schema table]}]
+ {:schema (some-> schema u/lower-case-en)
+ :table (u/lower-case-en table)}))
+ (sql-tools/referenced-tables-raw :h2 sql)))
+
+(defn- declared-refs
+ "The tables `sql` reads, uncased, as `{:schema :name}` maps — the shape `missing-inputs` compares
+ declared inputs against."
+ [sql]
+ (into #{}
+ (map (fn [{:keys [schema table]}] {:schema schema :name table}))
+ (sql-tools/referenced-tables-raw :h2 sql)))
+
+(defn- transform-under-test
+ "A transform whose target name appears in none of the SQL below, so its own replacement entry
+ never interferes with an input's."
+ []
+ {:source {:type "query"}
+ :target {:type "table" :schema nil :name "UNREFERENCED_TARGET"}})
+
+(defn- declared-input
+ "A declared input of `:format :sql` standing in for the table `schema`.`table-name`."
+ [schema table-name]
+ {:table {:schema schema :name table-name}
+ :format :sql
+ :sql "SELECT 1 AS x"})
+
+(defn- replacements-for
+ "The replacement map for `inputs`, whose temp tables are named TMP_IN_1, TMP_IN_2, … and whose
+ transform output is TMP_OUT."
+ [inputs]
+ (transform-testing.compile/table-replacements
+ (transform-under-test)
+ (into {} (map-indexed (fn [i input] [input (str "TMP_IN_" (inc i))])) inputs)
+ "TMP_OUT" "PUBLIC"))
+
+(defn- only-temp-tables?
+ "True iff every table in `refs` is one of the temp tables named by [[replacements-for]]."
+ [refs]
+ (every? #(re-matches #"tmp_(in_\d+|out)" (:table %)) refs))
+
+(defn- missing-inputs
+ "The tables `sql` reads that `inputs` does not declare."
+ [inputs sql]
+ (#'transform-testing.validator/missing-inputs inputs (declared-refs sql) "PUBLIC"))
+
+;;; ------------------------------------- Rewrite completeness -------------------------------------
+
+(deftest declared-input-is-redirected-to-its-temp-table-test
+ (let [inputs [(declared-input "PUBLIC" "PEOPLE")]
+ rewritten (rewrite "SELECT ID FROM PEOPLE" (replacements-for inputs))]
+ (is (= #{{:schema nil :table "tmp_in_1"}}
+ (table-refs rewritten)))))
+
+(def ^:private coverage-cases
+ "Ways a query can name a declared input, paired with the declaration it should satisfy."
+ [{:desc "exact-case match, both qualified"
+ :declared {:schema "PUBLIC" :name "PEOPLE"}
+ :sql "SELECT ID FROM PUBLIC.PEOPLE"}
+ {:desc "lowercase reference against an uppercase declaration"
+ :declared {:schema "PUBLIC" :name "PEOPLE"}
+ :sql "select id from people"}
+ {:desc "declared schema differing in case from the reference"
+ :declared {:schema "public" :name "PEOPLE"}
+ :sql "SELECT ID FROM PUBLIC.PEOPLE"}
+ {:desc "bare reference against a declaration in the default schema"
+ :declared {:schema "PUBLIC" :name "PEOPLE"}
+ :sql "SELECT ID FROM PEOPLE"}
+ {:desc "qualified reference against a bare declaration"
+ :declared {:schema nil :name "PEOPLE"}
+ :sql "SELECT ID FROM PUBLIC.PEOPLE"}])
+
+(deftest accepted-input-coverage-implies-a-complete-rewrite-test
+ (testing "a run whose inputs validate as complete must not go on to read a real table"
+ (doseq [{:keys [desc declared sql]} coverage-cases]
+ (let [{:keys [schema name]} declared
+ inputs [(declared-input schema name)]
+ rewritten (rewrite sql (replacements-for inputs))
+ accepted? (empty? (missing-inputs inputs sql))
+ rewrote? (only-temp-tables? (table-refs rewritten))]
+ (is (or (not accepted?) rewrote?)
+ (str desc ": inputs validated as complete, but the rewritten query still reads "
+ (pr-str (remove #(re-matches #"tmp_(in_\d+|out)" (:table %)) (table-refs rewritten)))))))))
+
+(deftest undeclared-table-is-reported-missing-test
+ (testing "a table the transform reads with no declared input is reported, so the run can be refused"
+ (let [sql "SELECT P.ID FROM PEOPLE P JOIN ORDERS O ON O.USER_ID = P.ID"
+ inputs [(declared-input "PUBLIC" "PEOPLE")]
+ missing (missing-inputs inputs sql)]
+ (is (some #(= "ORDERS" (:name %)) missing)
+ (str "ORDERS is undeclared and must be reported; missing=" (pr-str missing))))))
+
+;;; ---------------------------------------- Identifier case ----------------------------------------
+
+(deftest case-folded-table-reference-test
+ (testing "an unquoted reference resolves to the declared table regardless of the case it is written in"
+ ;; Unquoted `people` names the table PEOPLE on H2.
+ (let [sql "select id from people"
+ inputs [(declared-input "PUBLIC" "PEOPLE")]
+ rewritten (rewrite sql (replacements-for inputs))]
+ (is (= #{{:schema nil :table "tmp_in_1"}} (table-refs rewritten))
+ "the reference should have been redirected to the input's temp table")
+ (is (empty? (missing-inputs inputs sql))
+ "the reference should have counted as covered by the declared input"))))
+
+(deftest case-folded-schema-in-declaration-test
+ (testing "a declaration whose schema differs in case from the reference still matches it"
+ (let [sql "SELECT ID FROM PUBLIC.PEOPLE"
+ inputs [(declared-input "public" "PEOPLE")]
+ rewritten (rewrite sql (replacements-for inputs))]
+ (is (= #{{:schema nil :table "tmp_in_1"}} (table-refs rewritten))
+ "the reference should have been redirected to the input's temp table")
+ (is (empty? (missing-inputs inputs sql))
+ "the reference should have counted as covered by the declared input"))))
+
+;;; --------------------------- References the rewrite does not account for ---------------------------
+
+(deftest cte-named-after-a-declared-input-test
+ (testing "a CTE keeps its own name: a query reading it must not be redirected to a temp table"
+ ;; Table-reference parsing resolves scopes and so excludes CTE names, but the rewrite walks
+ ;; every table node. A CTE sharing a declared input's name is therefore orphaned — the query
+ ;; reads the fixture instead of the CTE, and nothing upstream can see that it happened.
+ (let [sql "WITH PEOPLE AS (SELECT 1 AS ID) SELECT ID FROM PEOPLE"
+ inputs [(declared-input "PUBLIC" "PEOPLE")]
+ rewritten (rewrite sql (replacements-for inputs))]
+ (is (empty? (table-refs rewritten))
+ (str "the only PEOPLE here is the CTE, so the query should read no table at all; "
+ "rewritten=" (pr-str rewritten))))))
+
+(deftest table-qualified-column-reference-test
+ (testing "a column qualified by table name is redirected along with the FROM clause"
+ ;; Table-reference parsing reports FROM and JOIN sources only, so a qualifier left behind is
+ ;; invisible to it; the rewritten query names a table that is no longer in scope.
+ (let [sql "SELECT PEOPLE.ID FROM PEOPLE"
+ inputs [(declared-input "PUBLIC" "PEOPLE")]
+ rewritten (rewrite sql (replacements-for inputs))
+ errors (:errors (sql-tools/field-references :h2 rewritten))]
+ (is (not-any? #(= :missing-table-alias (:type %)) errors)
+ (str "rewritten=" (pr-str rewritten) " errors=" (pr-str errors))))))
+
+(defn- validate
+ "Run [[transform-testing.validator/validate]] over a test declaring `inputs` and `expectations` against a transform
+ reading `sql`. Returns nil when it accepts the test, and throws its refusal when it does not."
+ [inputs expectations sql]
+ (let [replacements (replacements-for inputs)]
+ (transform-testing.validator/validate
+ :h2
+ {:inputs inputs
+ :expectations expectations
+ :referenced-tables (declared-refs sql)
+ :rewritten-transform (rewrite sql replacements)
+ :replacements replacements
+ :default-schema "PUBLIC"})))
+
+(deftest expectation-sql-reads-only-temp-tables-test
+ (let [inputs [(declared-input "PUBLIC" "PEOPLE")]
+ source "SELECT ID FROM PUBLIC.PEOPLE"]
+ (testing "an expectation naming a table the test declares no input for is refused, naming both"
+ ;; Input completeness is checked against the transform's references, never an expectation's, so
+ ;; nothing stands in for ORDERS. Guard B covers the expectation's own SQL for exactly this.
+ (is (thrown-with-msg?
+ ExceptionInfo #"(?i)Expectation \"leaks\" reads table\(s\) this test does not stand in for: orders"
+ (validate inputs [{:type :empty :name "leaks" :sql "SELECT * FROM ORDERS"}] source))))
+ (testing "an expectation reading the transform's output or a declared input is accepted"
+ (is (nil? (validate inputs
+ [{:type :empty :name "output" :sql "SELECT * FROM UNREFERENCED_TARGET"}
+ {:type :empty :name "input" :sql "SELECT * FROM PUBLIC.PEOPLE"}]
+ source))))
+ (testing "an expectation whose SQL cannot be parsed is refused as an authoring error, not a parser escape"
+ (is (thrown-with-msg?
+ ExceptionInfo #"Expectation \"broken\" has SQL that could not be parsed"
+ (validate inputs [{:type :empty :name "broken" :sql "SELECT * FROM WHERE ("}] source))))
+ (testing "an expectation with no SQL of its own is nothing to check"
+ (is (nil? (validate inputs
+ [{:type :equals :name "rows" :format :rows
+ :columns [{:name "ID" :cast_type "INTEGER"}]
+ :rows [{"ID" 1}]}]
+ source))))))
+
+;;; ----------------------------------- Replacement-map construction -----------------------------------
+
+(deftest same-table-name-in-two-schemas-test
+ (let [inputs [(declared-input "SALES" "ORDERS") (declared-input "ARCHIVE" "ORDERS")]
+ replacements (replacements-for inputs)]
+ (testing "an unqualified reference is ambiguous and must not be resolved to either schema"
+ (let [refs (table-refs (rewrite "SELECT * FROM ORDERS" replacements))]
+ (is (not (contains? refs {:schema nil :table "tmp_in_1"})) (str "refs=" (pr-str refs)))
+ (is (not (contains? refs {:schema nil :table "tmp_in_2"})) (str "refs=" (pr-str refs)))))
+ (testing "a qualified reference resolves to its own schema's temp table"
+ (is (= #{{:schema nil :table "tmp_in_1"}}
+ (table-refs (rewrite "SELECT * FROM SALES.ORDERS" replacements))))
+ (is (= #{{:schema nil :table "tmp_in_2"}}
+ (table-refs (rewrite "SELECT * FROM ARCHIVE.ORDERS" replacements)))))))
+
+(deftest catalog-qualified-reference-test
+ (testing "a catalog-qualified reference loses its catalog along with its schema"
+ ;; A replacement carries a table and a schema but no catalog, so a three-part reference keeps
+ ;; the catalog it came with and ends up naming a table that exists under no such catalog.
+ (let [inputs [(declared-input "PUBLIC" "ORDERS")]
+ rewritten (rewrite "SELECT * FROM MYDB.PUBLIC.ORDERS" (replacements-for inputs))]
+ (is (not (str/includes? (u/lower-case-en rewritten) "mydb"))
+ (str "rewritten=" (pr-str rewritten))))))
+
+(deftest colliding-replacement-keys-test
+ (testing "two declarations a reference cannot tell apart are refused, rather than one winning"
+ ;; With PUBLIC the default schema, a bare declaration and an explicit-PUBLIC declaration of the
+ ;; same table both register the key {:table "ORDERS"}. Whichever lost the collision would keep
+ ;; its fixture loaded but unreferenced, and the query would read the other one's data.
+ (let [inputs [(declared-input nil "ORDERS") (declared-input "PUBLIC" "ORDERS")]]
+ (is (= ["ORDERS" "PUBLIC.ORDERS"]
+ (#'transform-testing.validator/colliding-inputs inputs "PUBLIC")))))
+ (testing "two declarations differing only in case are the same table to a reference, so they collide"
+ (let [inputs [(declared-input "public" "orders") (declared-input "PUBLIC" "ORDERS")]]
+ (is (= ["public.orders" "PUBLIC.ORDERS"]
+ (#'transform-testing.validator/colliding-inputs inputs "PUBLIC")))))
+ (testing "the same table in two schemas is not a collision: a qualified reference tells them apart"
+ (let [inputs [(declared-input "SALES" "ORDERS") (declared-input "ARCHIVE" "ORDERS")]]
+ (is (= [] (#'transform-testing.validator/colliding-inputs inputs "PUBLIC"))))))
+
+;;; ------------------------------- Occurrences that are not references -------------------------------
+
+(deftest non-reference-occurrences-are-left-alone-test
+ (testing "a declared table's name in a column, literal or alias is not a reference and must survive"
+ ;; Table and column names collide routinely — status, type, source, state. A rewrite that
+ ;; matched names rather than references would corrupt all of these.
+ (let [inputs [(declared-input "PUBLIC" "PEOPLE") (declared-input "PUBLIC" "ORDERS")]
+ replacements (replacements-for inputs)]
+ (doseq [[desc sql] [["a column named like another declared table" "SELECT PEOPLE FROM ORDERS"]
+ ["a column whose name contains one" "SELECT PEOPLE_COUNT FROM ORDERS"]
+ ["a string literal" "SELECT * FROM ORDERS WHERE SRC = 'PEOPLE'"]
+ ["an alias" "SELECT X AS PEOPLE FROM ORDERS"]]]
+ (testing desc
+ (let [rewritten (rewrite sql replacements)]
+ (is (= #{{:schema nil :table "tmp_in_2"}} (table-refs rewritten))
+ (str "only ORDERS is a reference here; rewritten=" (pr-str rewritten)))
+ (is (str/includes? rewritten "PEOPLE")
+ (str "PEOPLE is not a reference here and should survive; rewritten="
+ (pr-str rewritten)))))))))
+
+;;; ------------------------------------------- End to end -------------------------------------------
+
+(deftest case-mismatched-run-must-not-report-on-real-data-test
+ (testing "a run whose rewrite misses must refuse rather than report a result read from real tables"
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (let [mp (mt/metadata-provider)
+ {schema :schema, table :name} (lib.metadata/table mp (mt/id :people))
+ target "mb_remapping_probe_case_mismatch"]
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query
+ mp (str "SELECT id, name FROM " schema "." (u/lower-case-en table)))}
+ :target {:type "table"
+ :schema schema
+ :name target
+ :database (mt/id)}}]
+ (mt/with-temp [:model/TransformTest transform-test
+ {:transform_id transform-id
+ :inputs [{:table {:schema schema :name table}
+ :format :sql
+ :sql "SELECT -1 AS id, 'fixture' AS name"}]
+ :expectations [{:type :empty
+ :name "the output holds only the fixture row"
+ :sql (str "SELECT * FROM " schema "." target " WHERE id <> -1")}]}]
+ ;; The declared input names the table as sync reports it; the transform names it in
+ ;; lower case. Refusing the run is a fine answer. Reporting :failed is not — it means
+ ;; the fixture was ignored and the expectation was measured against real rows.
+ (let [outcome (try
+ (transform-testing.runner/run-transform-test! transform-test)
+ (catch Exception e
+ {:refused (ex-message e)}))]
+ (is (not= {:status :failed} outcome)
+ (str "driver=" driver/*driver* " outcome=" (pr-str outcome))))))))))
+
+(deftest target-table-is-never-created-test
+ (testing "a run leaves no trace in the warehouse: the transform's target is never materialized"
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (let [mp (mt/metadata-provider)
+ {schema :schema, table :name} (lib.metadata/table mp (mt/id :people))
+ target "mb_remapping_probe_target"]
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query mp (str "SELECT id, name FROM " schema "." table))}
+ :target {:type "table"
+ :schema schema
+ :name target
+ :database (mt/id)}}
+ :model/TransformTest transform-test
+ {:transform_id transform-id
+ :inputs [{:table {:schema schema :name table}
+ :format :sql
+ :sql "SELECT -1 AS id, 'fixture' AS name"}]
+ :expectations [{:type :empty
+ :name "the output holds only the fixture row"
+ :sql (str "SELECT * FROM " schema "." target " WHERE id <> -1")}]}]
+ (is (= :passed (:status (transform-testing.runner/run-transform-test! transform-test))))
+ (is (not (driver/table-exists? driver/*driver* (mt/db) {:schema schema :name target}))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/reported_type_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/reported_type_test.clj
new file mode 100644
index 00000000000..c544e67430b
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/reported_type_test.clj
@@ -0,0 +1,69 @@
+(ns ^:mb/driver-tests metabase-enterprise.transform-testing.reported-type-test
+ "The `database_type` a run reports is the transform output table's own, not the comparison query's
+ and not the cast target the author declared."
+ (:require
+ [clojure.test :refer :all]
+ [metabase-enterprise.transform-testing.executor :as transform-testing.executor]
+ [metabase-enterprise.transform-testing.runner :as transform-testing.runner]
+ [metabase.driver :as driver]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
+ [metabase.test :as mt]))
+
+(def ^:private cases
+ "Per driver: the input fixture, the type the author declares for the output column, and the two
+ type the transform's output column really has. The declared one is a cast target and is not a column
+ type: `SIGNED` is not a type name MySQL ever reports, and `Nullable(Int32)` is not what the
+ transform produced."
+ {:mysql {:input-sql "SELECT 1 AS id"
+ :declared "SIGNED"
+ :produced "INTEGER"}
+ :clickhouse {:input-sql "SELECT CAST(1 AS Int64) AS id"
+ :declared "Nullable(Int32)"
+ :produced "Int64"}})
+
+(defn- output-column-type!
+ "The type the transform's output column really has, materialized the way the runner materializes it."
+ [driver sql]
+ (driver/do-with-test-connection
+ driver
+ (mt/db)
+ (fn [conn]
+ (let [table (driver/temp-table-name driver)]
+ (try
+ (transform-testing.executor/create-temp-table! driver conn table {:query sql :params []})
+ (:database_type (first (transform-testing.executor/table-columns driver conn table)))
+ (finally
+ (transform-testing.executor/drop-temp-table! driver conn table)))))))
+
+(deftest reported-type-is-the-output-tables-test
+ (mt/test-drivers (set (keys cases))
+ (mt/with-premium-features #{:transforms-basic :transforms-testing}
+ (let [{:keys [input-sql declared produced]} (get cases driver/*driver*)
+ mp (mt/metadata-provider)
+ {schema :schema, table :name} (lib.metadata/table mp (mt/id :people))
+ transform-sql (str "SELECT id AS total FROM " schema "." table)]
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query mp transform-sql)}
+ :target {:type "table" :schema schema :name "people_total" :database (mt/id)}}
+ :model/TransformTest transform-test
+ {:transform_id transform-id
+ :inputs [{:table {:schema schema :name table} :format :sql :sql input-sql}]
+ :expectations [{:type :equals
+ :name "total"
+ :format :rows
+ :columns [{:name "total" :cast_type declared}]
+ :rows [{"total" 1}]}]}]
+ (let [result (transform-testing.runner/run-transform-test! transform-test)]
+ (testing "the run passes, so nothing here reaches the author as a problem"
+ (is (= :passed (:status result))))
+ (testing "the transform's output column is not the type the author declared"
+ (is (= produced (output-column-type!
+ driver/*driver*
+ (str "SELECT id AS total FROM (" input-sql ") mb_src"))))
+ (is (not= declared produced)))
+ (testing "and the run reports the output table's type, not the comparison's"
+ (let [actual (-> result :expectations first :columns first :database_type)]
+ (is (= produced actual))
+ (is (not= declared actual))))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/runner_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/runner_test.clj
new file mode 100644
index 00000000000..590ad93cb91
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/runner_test.clj
@@ -0,0 +1,315 @@
+(ns ^:mb/driver-tests metabase-enterprise.transform-testing.runner-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase-enterprise.transform-testing.errors :as transform-testing.errors]
+ [metabase-enterprise.transform-testing.runner :as transform-testing.runner]
+ [metabase-enterprise.transform-testing.test-util :as transform-testing.test-util]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
+ [metabase.sql-tools.settings :as sql-tools.settings]
+ [metabase.test :as mt])
+ (:import
+ (clojure.lang ExceptionInfo)))
+
+(defn- table-ref
+ "The `{:schema :name}` of a test-data table, as the transform's compiled SQL will reference it."
+ [table-key]
+ (let [{:keys [schema name]} (lib.metadata/table (mt/metadata-provider) (mt/id table-key))]
+ {:schema schema :name name}))
+
+(defn- with-people-transform
+ "Call `f` with the id of a transform selecting `id, name` from the people table, whose target is
+ `people_summary` in the same schema."
+ [f]
+ (let [mp (mt/metadata-provider)
+ {schema :schema, table :name} (lib.metadata/table mp (mt/id :people))]
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query mp (str "SELECT id, name FROM " schema "." table))}
+ :target {:type "table"
+ :schema schema
+ :name "people_summary"
+ :database (mt/id)}}]
+ (f schema table transform-id))))
+
+(defn- run-test!
+ "Run a transform test over `transform-id` with one SQL input standing in for `schema`.`table`."
+ [schema table transform-id input-sql expectations]
+ (mt/with-temp [:model/TransformTest transform-test
+ {:transform_id transform-id
+ :inputs [{:table {:schema schema :name table}
+ :format :sql
+ :sql input-sql}]
+ :expectations expectations}]
+ (transform-testing.runner/run-transform-test! transform-test)))
+
+(defn- drivers-resolving-bare-tables
+ "The test drivers that resolve an unqualified table reference to the schema the test data lives in. Redshift reports
+ no default schema, so a read of `people` cannot be matched to the input declared for `sha__….people`."
+ []
+ (disj (mt/normal-drivers-with-feature :transforms/testing) :redshift))
+
+(def ^:private one-row "SELECT 1 AS id, 'abc' AS name")
+
+(deftest run-transform-test-rows-input-test
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (with-people-transform
+ (fn [schema table transform-id]
+ (let [mp (mt/metadata-provider)
+ id-name (:name (lib.metadata/field mp (mt/id :people :id)))
+ name-name (:name (lib.metadata/field mp (mt/id :people :name)))
+ [int-type text-type] (transform-testing.test-util/cast-types)
+ output (str schema ".people_summary")
+ run (fn [rows sql]
+ (mt/with-temp [:model/TransformTest transform-test
+ {:transform_id transform-id
+ :inputs [{:table {:schema schema :name table}
+ :format :rows
+ :columns [{:name id-name :cast_type int-type}
+ {:name name-name :cast_type text-type}]
+ :rows rows}]
+ :expectations [{:type :empty :name "check" :sql sql}]}]
+ (:status (transform-testing.runner/run-transform-test! transform-test))))
+ two-rows [{id-name 1 name-name "abc"} {id-name 2 name-name "def"}]]
+ (testing "the transform reads the literal rows"
+ (is (= :passed (run two-rows (str "SELECT * FROM " output " WHERE id NOT IN (1, 2) OR name IS NULL"))))
+ (is (= :failed (run two-rows (str "SELECT * FROM " output " WHERE id = 2")))))
+ (testing "no rows stand in for an empty table"
+ (is (= :passed (run [] (str "SELECT * FROM " output))))))))))
+
+(defn- id+name
+ "The `id` and `name` columns of the output, typed as the current driver spells those types."
+ []
+ (let [[int-type text-type] (transform-testing.test-util/cast-types)]
+ [{:name "id" :cast_type int-type}
+ {:name "name" :cast_type text-type}]))
+
+(deftest run-transform-test-empty-expectation-test
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (with-people-transform
+ (fn [schema table transform-id]
+ (doseq [[desc where expected] [["passes when the query returns no rows"
+ "name IS NULL"
+ :passed]
+ ["reads the input test data instead of the input table"
+ "id <> 1"
+ :passed]
+ ["fails when the query returns rows"
+ "name = 'abc'"
+ :failed]]]
+ (testing desc
+ (let [result (run-test! schema table transform-id one-row
+ [{:type :empty
+ :name desc
+ :sql (str "SELECT * FROM " schema ".people_summary WHERE " where)}])]
+ (is (= expected (:status result)))
+ (is (= [{:name desc :type :empty :status expected}]
+ (mapv #(select-keys % [:name :type :status]) (:expectations result)))))))))))
+
+(deftest run-transform-test-equals-passes-test
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (with-people-transform
+ (fn [schema table transform-id]
+ (testing "passes when the output matches the expected rows"
+ (let [result (run-test! schema table transform-id one-row
+ [{:type :equals :name "output" :format :rows
+ :columns (id+name)
+ :rows [{"id" 1 "name" "abc"}]}])]
+ (is (= :passed (:status result)))
+ (is (= [:passed] (mapv :status (:expectations result))))))))))
+
+(deftest run-transform-test-equals-cell-mismatch-test
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (with-people-transform
+ (fn [schema table transform-id]
+ (testing "a single differing cell is reported by column name"
+ (let [result (run-test! schema table transform-id one-row
+ [{:type :equals :name "output" :format :rows
+ :columns (id+name)
+ :rows [{"id" 1 "name" "xyz"}]}])
+ [expect] (:expectations result)]
+ (is (= :failed (:status result)))
+ (is (= :failed (:status expect)))
+ (is (= {:actual 1 :expected 1} (:row-counts expect)))
+ (is (= [{:column "name" :expected "xyz" :actual "abc"}]
+ (:cell-mismatches expect)))))))))
+
+(deftest run-transform-test-equals-ignores-undeclared-columns-test
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (with-people-transform
+ (fn [schema table transform-id]
+ (testing "a column the expectation does not declare is not compared"
+ (let [result (run-test! schema table transform-id one-row
+ [{:type :equals :name "id only" :format :rows
+ :columns [{:name "id" :cast_type (first (transform-testing.test-util/cast-types))}]
+ :rows [{"id" 1}]}])]
+ (is (= :passed (:status result)))))))))
+
+(deftest run-transform-test-equals-is-multiset-test
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (with-people-transform
+ (fn [schema table transform-id]
+ (testing "a duplicated output row fails against a single expected row"
+ ;; Set difference (a plain EXCEPT) passes here; multiset difference must not.
+ (let [result (run-test! schema table transform-id
+ (str "SELECT id, name FROM (" one-row " UNION ALL " one-row ") AS t")
+ [{:type :equals :name "output" :format :rows
+ :columns (id+name)
+ :rows [{"id" 1 "name" "abc"}]}])
+ [expect] (:expectations result)]
+ (is (= :failed (:status result)))
+ (is (= {:actual 2 :expected 1} (:row-counts expect)))
+ (is (= [{"id" 1 "name" "abc"}] (:extra-rows expect)))
+ (is (= [] (:missing-rows expect)))))))))
+
+(deftest run-transform-test-equals-matches-null-cells-test
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (with-people-transform
+ (fn [schema table transform-id]
+ (testing "a NULL cell in the output cancels against a nil in the expectation"
+ ;; `GROUP BY` folds NULLs together. An equality-based difference leaves the row
+ ;; uncancelled, reporting it as both missing and extra.
+ (let [result (run-test! schema table transform-id
+ (str "SELECT 1 AS id, CAST(NULL AS " (second (transform-testing.test-util/cast-types)) ") AS name")
+ [{:type :equals :name "output" :format :rows
+ :columns (id+name)
+ :rows [{"id" 1 "name" nil}]}])]
+ (is (= :passed (:status result)))))))))
+
+(deftest run-transform-test-rejects-undeclared-input-test
+ (testing "Guard A: a transform reading a table with no declared input is refused,"
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ ;; the transform reads people, but the test declares no input for it — running would leave
+ ;; people pointing at the real table (read production, false-green). Must reject before running.
+ (let [mp (mt/metadata-provider)
+ {schema :schema, table :name} (lib.metadata/table mp (mt/id :people))]
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query mp (str "SELECT id, name FROM " schema "." table))}
+ :target {:type "table" :schema schema :name "people_summary" :database (mt/id)}}
+ :model/TransformTest transform-test
+ {:transform_id transform-id
+ :inputs []
+ :expectations [{:type :empty :name "all" :sql (str "SELECT * FROM " schema ".people_summary")}]}]
+ (let [ex (try (transform-testing.runner/run-transform-test! transform-test)
+ nil
+ (catch ExceptionInfo e e))]
+ (testing "throws"
+ (is (some? ex)))
+ (testing "with a typed refusal that names the undeclared table"
+ (is (= ::transform-testing.errors/missing-inputs (:error-type (ex-data ex))))
+ (is (re-find (re-pattern (str "(?i)" table)) (ex-message ex))))))))))
+
+(deftest run-transform-test-rejects-partially-declared-inputs-test
+ (testing "Guard A: a join declaring only some of its input tables is rejected, naming the gap,"
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ ;; reads orders and people; declares only orders → people would fall through to the real table.
+ (let [mp (mt/metadata-provider)
+ {pschema :schema, people :name} (table-ref :people)
+ {oschema :schema, orders :name} (table-ref :orders)]
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query
+ mp (str "SELECT o.id, p.name"
+ " FROM " oschema "." orders " o"
+ " JOIN " pschema "." people " p ON o.user_id = p.id"))}
+ :target {:type "table" :schema oschema :name "order_owners" :database (mt/id)}}
+ :model/TransformTest transform-test
+ {:transform_id transform-id
+ :inputs [{:table {:schema oschema :name orders} :format :sql
+ :sql "SELECT 1 AS id, 1 AS user_id"}]
+ :expectations [{:type :empty :name "all" :sql (str "SELECT * FROM " oschema ".order_owners")}]}]
+ (let [ex (try (transform-testing.runner/run-transform-test! transform-test)
+ nil
+ (catch ExceptionInfo e e))]
+ (is (= ::transform-testing.errors/missing-inputs (:error-type (ex-data ex))))
+ (testing "names the undeclared people, not the declared orders"
+ (is (re-find (re-pattern (str "(?i)" people)) (ex-message ex)))
+ (is (not (re-find (re-pattern (str "(?i)" orders)) (ex-message ex)))))))))))
+
+(deftest run-transform-test-rejects-unused-input-test
+ (testing "Guard A: a declared input for a table the transform does not read is refused,"
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ ;; reads only people, but declares an input for orders too — a stale/mistyped fake.
+ (let [mp (mt/metadata-provider)
+ {schema :schema, people :name} (lib.metadata/table mp (mt/id :people))
+ orders (:name (lib.metadata/table mp (mt/id :orders)))]
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query mp (str "SELECT id, name FROM " schema "." people))}
+ :target {:type "table" :schema schema :name "people_summary" :database (mt/id)}}
+ :model/TransformTest transform-test
+ {:transform_id transform-id
+ :inputs [{:table (table-ref :people) :format :sql
+ :sql "SELECT 1 AS id, 'x' AS name"}
+ {:table (table-ref :orders) :format :sql
+ :sql "SELECT 1 AS id"}]
+ :expectations [{:type :empty :name "all" :sql (str "SELECT * FROM " schema ".people_summary")}]}]
+ (let [ex (try (transform-testing.runner/run-transform-test! transform-test)
+ nil
+ (catch ExceptionInfo e e))]
+ (is (= ::transform-testing.errors/unused-inputs (:error-type (ex-data ex))))
+ (testing "names the unused orders, not the read people"
+ (is (re-find (re-pattern (str "(?i)" orders)) (ex-message ex)))
+ (is (not (re-find (re-pattern (str "(?i)" people)) (ex-message ex)))))))))))
+
+(deftest run-transform-test-rejects-duplicate-input-table-test
+ (testing "two inputs targeting the same table (even with different fixture SQL) are rejected 400,"
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ ;; both inputs fixture `people`, differing only in SQL — they would collide on one temp table
+ ;; and silently drop a fixture. Reject on :table, not on the whole (distinct) input map.
+ (let [mp (mt/metadata-provider)
+ {schema :schema, table :name} (lib.metadata/table mp (mt/id :people))]
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query mp (str "SELECT id, name FROM " schema "." table))}
+ :target {:type "table" :schema schema :name "people_summary" :database (mt/id)}}
+ :model/TransformTest transform-test
+ {:transform_id transform-id
+ :inputs [{:table (table-ref :people) :format :sql :sql "SELECT 1 AS id, 'a' AS name"}
+ {:table (table-ref :people) :format :sql :sql "SELECT 2 AS id, 'b' AS name"}]
+ :expectations [{:type :empty :name "all" :sql (str "SELECT * FROM " schema ".people_summary")}]}]
+ (let [ex (try (transform-testing.runner/run-transform-test! transform-test)
+ nil
+ (catch ExceptionInfo e e))]
+ (is (= ::transform-testing.errors/duplicate-input-table (:error-type (ex-data ex))))
+ (is (re-find #"(?i)duplicate" (ex-message ex)))))))))
+
+(deftest run-transform-test-rejects-unparseable-source-test
+ (testing "a SQLGlot parse failure cannot masquerade as a source with no table reads"
+ (binding [sql-tools.settings/*parser-backend-override* :sqlglot]
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (let [mp (mt/metadata-provider)]
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query mp "SELECT * FROM WHERE (")}
+ :target {:type "table" :schema "public" :name "parse_test" :database (mt/id)}}
+ :model/TransformTest transform-test
+ {:transform_id transform-id :inputs [] :expectations []}]
+ (let [e (try (transform-testing.runner/run-transform-test! transform-test)
+ (catch ExceptionInfo e e))]
+ (is (= ::transform-testing.errors/unparseable-source (:error-type (ex-data e))))
+ (testing "and the refusal reads as the parser's own diagnostic, less its internals"
+ (is (= (str "The transform source SQL could not be parsed for test input validation: "
+ "ParseError: Expected table name but got 'WHERE'. Line 1, Col: 19.\n"
+ " SELECT * FROM WHERE (\n"
+ " ^^^^^")
+ (ex-message e)))))))))))
+
+(deftest run-transform-test-table-qualified-columns-test
+ (testing "column qualifiers naming a replaced table are rewritten along with the table, so the transform reads the input"
+ (mt/test-drivers (drivers-resolving-bare-tables)
+ (let [mp (mt/metadata-provider)
+ {schema :schema, table :name} (lib.metadata/table mp (mt/id :people))]
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:source {:type "query"
+ :query (lib/native-query mp (str "SELECT " table ".id, " table ".name FROM " table))}
+ :target {:type "table" :schema schema :name "people_qualified" :database (mt/id)}}
+ :model/TransformTest transform-test
+ {:transform_id transform-id
+ :inputs [{:table (table-ref :people) :format :sql
+ :sql "SELECT 1 AS id, 'x' AS name"}]
+ :expectations [{:type :empty :name "only the input row"
+ :sql (str "SELECT * FROM " schema ".people_qualified WHERE id <> 1")}]}]
+ (is (=? {:status :passed}
+ (transform-testing.runner/run-transform-test! transform-test))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/schema_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/schema_test.clj
new file mode 100644
index 00000000000..789760e6c51
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/schema_test.clj
@@ -0,0 +1,238 @@
+(ns metabase-enterprise.transform-testing.schema-test
+ "The `::expectations` schema and the records built from what it passes. The schema is the only
+ gate: it normalizes the wire form, dispatches on `:type`, and refuses a repeated name. Pure — no
+ warehouse, no app DB."
+ (:require
+ [clojure.string :as str]
+ [clojure.test :refer [deftest is testing]]
+ [malli.core :as mc]
+ [metabase-enterprise.transform-testing.errors :as transform-testing.errors]
+ [metabase-enterprise.transform-testing.expectations.empty :as expectations.empty]
+ [metabase-enterprise.transform-testing.expectations.equals :as expectations.equals]
+ [metabase-enterprise.transform-testing.expectations.protocol :as expectations.protocol]
+ [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
+ [metabase.lib.core :as lib]
+ [metabase.util.malli :as mu]
+ [metabase.util.malli.registry :as mr]))
+
+(set! *warn-on-reflection* true)
+
+;;; -------------------------------------------- Harness --------------------------------------------
+
+(defn- equals-rows
+ "The wire form of an `equals`/`rows` expectation: string keys and string values, as JSON delivers it."
+ ([nm]
+ (equals-rows nm [{"name" "id" "cast_type" "INTEGER"}] [{"id" 1}]))
+ ([nm columns rows]
+ {"type" "equals" "name" nm "format" "rows" "columns" columns "rows" rows}))
+
+(defn- empty-sql
+ "The wire form of an `empty` expectation."
+ [nm]
+ {"type" "empty" "name" nm "sql" "SELECT 1 FROM out WHERE id IS NULL"})
+
+(defn- normalized
+ "`raw` normalized as the model's `:in` transform normalizes it."
+ [raw]
+ (lib/normalize ::transform-testing.schema/expectations raw))
+
+(defn- valid?
+ "Whether `raw` is an acceptable `:expectations` value once normalized."
+ [raw]
+ (mr/validate ::transform-testing.schema/expectations (normalized raw)))
+
+(defn- explained
+ "Every message the explanation of `value` against `schema` carries, wherever in the structure it sits, without
+ the value each one echoes back."
+ [schema value]
+ (into #{}
+ (comp (filter string?)
+ (map #(first (str/split % #", received: "))))
+ (tree-seq coll? seq (mu/explain schema value))))
+
+(defn- normalized-inputs
+ "`raw` normalized as the model's `:in` transform normalizes an `:inputs` value."
+ [raw]
+ (lib/normalize ::transform-testing.schema/inputs raw))
+
+(defn- records
+ "The expectation records `raw` builds into, as the runner builds them."
+ [raw]
+ (mapv expectations.protocol/build (normalized raw)))
+
+(defn- record-name
+ "The simple class name of `x` — how a test names the record type it expected."
+ [x]
+ (.getSimpleName (class x)))
+
+;;; ------------------------------------------- Normalization -------------------------------------------
+
+(deftest normalizes-the-wire-form-test
+ (testing "string keys and values become keywords where the schema says so"
+ (is (= [{:type :equals :name "n" :format :rows
+ :columns [{:name "id" :cast_type "INTEGER"}]
+ :rows [{"id" 1}]}]
+ (normalized [(equals-rows "n")]))))
+ (testing "row keys stay strings — a warehouse column can be named 2024 or order-id"
+ (let [[e] (normalized [(equals-rows "n" [{"name" "2024" "cast_type" "INTEGER"}] [{"2024" 1}])])]
+ (is (= [{"2024" 1}] (:rows e)))
+ (is (every? string? (mapcat keys (:rows e))))))
+ (testing "declared order is kept, across types"
+ (is (= ["one" "two" "three"]
+ (mapv :name (normalized [(equals-rows "one") (empty-sql "two") (equals-rows "three")]))))))
+
+;;; --------------------------------------------- What passes ---------------------------------------------
+
+(deftest accepts-every-known-type-test
+ (is (valid? [(equals-rows "a") (empty-sql "b")]))
+ (testing "no expectations at all is fine"
+ (is (valid? []))
+ (is (= [] (records []))))
+ (testing "an `equals` comparing against SQL is a shape the schema knows, though no run can use it"
+ (is (valid? [{"type" "equals" "name" "n" "format" "sql" "sql" "SELECT 1"}]))))
+
+(deftest builds-the-type-that-owns-each-branch-test
+ (is (= ["EqualsRows" "Empty"]
+ (mapv record-name (records [(equals-rows "a") (empty-sql "b")]))))
+ (is (= ["EqualsSql"]
+ (mapv record-name (records [{"type" "equals" "name" "n" "format" "sql" "sql" "SELECT 1"}])))))
+
+;;; --------------------------------------------- What it refuses ---------------------------------------------
+
+(deftest refuses-a-repeated-name-test
+ ;; Uniqueness is a property of the whole sequence rather than of one expectation, and a failure
+ ;; report leads with the name, so two expectations sharing one would be unreadable.
+ (testing "two expectations with the same name are refused, however they are typed"
+ (is (not (valid? [(equals-rows "dup") (empty-sql "dup")])))
+ (is (not (valid? [(empty-sql "dup") (empty-sql "other") (empty-sql "dup")]))))
+ (testing "names differing in case are different names"
+ (is (valid? [(empty-sql "dup") (empty-sql "DUP")]))))
+
+(deftest rows-must-carry-exactly-the-declared-columns-test
+ (testing "a row naming a column that was not declared is refused"
+ (is (not (valid? [(equals-rows "n" [{"name" "id" "cast_type" "INTEGER"}] [{"id" 1 "nope" 2}])])))
+ (is (= #{"every row must carry exactly the declared columns; not declared: \"nope\""}
+ (explained ::transform-testing.schema/expectations
+ (normalized [(equals-rows "n" [{"name" "id" "cast_type" "INTEGER"}]
+ [{"id" 1 "nope" 2}])])))))
+ (testing "a row leaving a declared column out is refused, rather than becoming a null cell"
+ (is (= #{"every row must carry exactly the declared columns; missing \"name\""}
+ (explained ::transform-testing.schema/expectations
+ (normalized [(equals-rows "n"
+ [{"name" "id" "cast_type" "INTEGER"}
+ {"name" "name" "cast_type" "VARCHAR(5)"}]
+ [{"id" 1}])])))))
+ (testing "the message names what one offending row got wrong, both ways at once"
+ (is (= #{"every row must carry exactly the declared columns; missing \"name\"; not declared: \"nope\""}
+ (explained ::transform-testing.schema/expectations
+ (normalized [(equals-rows "n"
+ [{"name" "id" "cast_type" "INTEGER"}
+ {"name" "name" "cast_type" "VARCHAR(5)"}]
+ [{"id" 1 "nope" 2}])])))))
+ (testing "a declared column holding a null is carried by the row, not left out"
+ (is (valid? [(equals-rows "n" [{"name" "id" "cast_type" "INTEGER"}] [{"id" nil}])])))
+ (testing "no rows at all declares columns and stands for an empty table"
+ (is (valid? [(equals-rows "n" [{"name" "id" "cast_type" "INTEGER"}] [])])))
+ (testing "and an input's rows answer to its columns the same way"
+ (is (mr/validate ::transform-testing.schema/inputs
+ (normalized-inputs [{"table" {"name" "PEOPLE"} "format" "rows"
+ "columns" [{"name" "ID" "cast_type" "INTEGER"}]
+ "rows" [{"ID" 1}]}])))
+ (is (= #{"every row must carry exactly the declared columns; not declared: \"NOPE\""}
+ (explained ::transform-testing.schema/inputs
+ (normalized-inputs [{"table" {"name" "PEOPLE"} "format" "rows"
+ "columns" [{"name" "ID" "cast_type" "INTEGER"}]
+ "rows" [{"ID" 1 "NOPE" 2}]}]))))))
+
+(deftest refuses-a-malformed-expectation-test
+ (testing "an unknown type matches no branch"
+ (is (not (valid? [{"type" "nope" "name" "n"}]))))
+ (testing "a missing or blank name"
+ (is (not (valid? [{"type" "empty" "sql" "SELECT 1"}])))
+ (is (not (valid? [{"type" "empty" "name" "" "sql" "SELECT 1"}]))))
+ (testing "a missing body, an undeclared key, and a non-map"
+ (is (not (valid? [{"type" "empty" "name" "n"}])))
+ (is (not (valid? [{"type" "empty" "name" "n" "sql" "SELECT 1" "nope" 1}])))
+ (is (not (valid? ["SELECT 1"])))))
+
+;;; ----------------------------------- Checks deferred to run time -----------------------------------
+
+(deftest equals-sql-is-accepted-but-cannot-run-test
+ ;; Passing the schema says the shape is storable, not that a run can use it. Refusing it at read
+ ;; time would instead make every already-stored test of this shape unreadable.
+ (testing "equals/sql builds, then refuses at probe time as not implemented"
+ (let [[r] (records [{"type" "equals" "name" "n" "format" "sql" "sql" "SELECT 1"}])]
+ (is (satisfies? expectations.protocol/Expectation r))
+ (is (= ::transform-testing.errors/unsupported-format
+ (try (expectations.protocol/probes r nil)
+ ::no-refusal
+ (catch clojure.lang.ExceptionInfo e (:error-type (ex-data e)))))))))
+
+(deftest database-type-and-column-name-are-not-checked-here-test
+ ;; Neither is checked anywhere: `compile/rows-query` writes a column name as one quoted identifier
+ ;; and quotes a cast target that is not a plain type name, leaving the database to refuse it.
+ (testing "a type that could escape its cast, and a name SQL could not carry unquoted, both store"
+ (is (valid? [(equals-rows "n" [{"name" "id" "cast_type" "INT) FROM x; --"}] [{"id" 1}])]))
+ (is (valid? [(equals-rows "n" [{"name" "a.b" "cast_type" "INTEGER"}] [{"a.b" 1}])]))))
+
+;;; ---------------------------------- Every type owns both of its schemas ----------------------------------
+
+(def ^:private expected-types
+ "The schemas every expectation type must own. Exhaustive over the aggregates by assertion below, so
+ a type added with no result schema fails here rather than reaching a client as an undescribed
+ result map."
+ {:equals {:schema ::transform-testing.schema/expectation.equals
+ :result ::expectations.equals/result}
+ :empty {:schema ::transform-testing.schema/expectation.empty
+ :result ::expectations.empty/result}})
+
+(defn- branches
+ "The dispatch values the `:multi` registered as `schema` has branches for, and the schema each names."
+ [schema]
+ (into {} (map (juxt first last)) (mc/children (mr/schema schema))))
+
+(deftest aggregates-are-exhaustive-over-the-types-test
+ (testing "the schema a client sends has a branch per known type, naming that type's own schema"
+ (is (= (update-vals expected-types :schema)
+ (update-vals (branches ::transform-testing.schema/expectation) mc/form))))
+ (testing "the aggregate a run reports too — a type with no result schema cannot ship"
+ (is (= (update-vals expected-types :result)
+ (update-vals (branches ::transform-testing.schema/expectation-result) mc/form))))
+ (testing "and every one of those schemas is registered"
+ (doseq [[type {:keys [schema result]}] expected-types]
+ (testing (str type)
+ (is (some? (mr/registered-schema schema)))
+ (is (some? (mr/registered-schema result)))))))
+
+(deftest every-type-can-be-built-test
+ (testing "every branch of the aggregate has a `build` method, so nothing storable is unrunnable"
+ (is (= (conj (set (keys expected-types)) :default)
+ (set (keys (methods expectations.protocol/build)))))))
+
+(deftest every-build-validates-before-constructing-test
+ (testing "every known type's build method puts its value through the schema before constructing anything"
+ (doseq [type (keys expected-types)]
+ (testing type
+ (let [e (try (expectations.protocol/build {:type type}) nil (catch Exception e e))]
+ (is (some? e))
+ (testing "and the refusal is the schema's own explanation"
+ (is (some? (:error (ex-data e))))))))))
+
+(deftest the-schema-explains-its-own-refusals-test
+ (testing "an unknown type names the types there are"
+ (is (= #{"unknown type \"nope\", must be one of: empty, equals"}
+ (explained ::transform-testing.schema/expectations (normalized [{"type" "nope" "name" "n"}])))))
+ (testing "an unknown format names the formats there are"
+ (is (= #{"unknown format \"nope\", must be one of: rows, sql"}
+ (explained ::transform-testing.schema/expectations
+ (normalized [{"type" "equals" "name" "n" "format" "nope"}])))))
+ (testing "something that is not a map at all"
+ (is (= #{"must be a map whose type is one of: empty, equals"}
+ (explained ::transform-testing.schema/expectations (normalized ["SELECT 1"])))))
+ (testing "a repeated name explains itself over the whole sequence"
+ (is (= #{"expectation names must be unique within a test"}
+ (explained ::transform-testing.schema/expectations
+ (normalized [(empty-sql "dup") (empty-sql "dup")])))))
+ (testing "and an input's format, which is the same kind of dispatch"
+ (is (= #{"unknown format :nope, must be one of: rows, sql"}
+ (explained ::transform-testing.schema/inputs [{:table {:name "PEOPLE"} :format :nope}])))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/test_util.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/test_util.clj
new file mode 100644
index 00000000000..82f992958bc
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/test_util.clj
@@ -0,0 +1,17 @@
+(ns metabase-enterprise.transform-testing.test-util
+ "Helpers shared by the transform-testing tests that run against a real warehouse."
+ (:require
+ [metabase.driver :as driver]))
+
+(defn cast-types
+ "An integer and a text `cast_type` the current driver accepts as a `CAST` target.
+
+ A test's declared types are raw SQL, so a fixture has to spell them the way the warehouse under test does."
+ []
+ (case driver/*driver*
+ :mysql ["SIGNED" "CHAR(50)"]
+ :sqlserver ["int" "nvarchar(50)"]
+ ;; ClickHouse casts NULL only to a nullable type
+ :clickhouse ["Nullable(Int32)" "Nullable(String)"]
+ :bigquery-cloud-sdk ["INT64" "STRING"]
+ ["INTEGER" "VARCHAR(50)"]))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/validator_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/validator_test.clj
new file mode 100644
index 00000000000..6eb1ff88448
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/validator_test.clj
@@ -0,0 +1,84 @@
+(ns metabase-enterprise.transform-testing.validator-test
+ "Guard A: every table the transform reads must have a declared input. Pure — no warehouse."
+ (:require
+ [clojure.test :refer [deftest is testing]]
+ [metabase-enterprise.transform-testing.validator :as validator]))
+
+(defn- sql-input [schema name]
+ {:table {:schema schema :name name} :format :sql :sql "SELECT 1"})
+
+(deftest missing-inputs-complete-test
+ (testing "no missing inputs when every referenced table is declared"
+ (is (= [] (#'validator/missing-inputs
+ [(sql-input "PUBLIC" "PEOPLE") (sql-input "PUBLIC" "ORDERS")]
+ #{{:schema "PUBLIC" :name "PEOPLE"} {:schema "PUBLIC" :name "ORDERS"}} "PUBLIC")))))
+
+(deftest missing-inputs-undeclared-test
+ (testing "an undeclared referenced table is reported"
+ (is (= [{:schema "PUBLIC" :name "ORDERS"}]
+ (#'validator/missing-inputs
+ [(sql-input "PUBLIC" "PEOPLE")]
+ #{{:schema "PUBLIC" :name "PEOPLE"} {:schema "PUBLIC" :name "ORDERS"}} "PUBLIC"))))
+ (testing "with nothing declared, every referenced table is reported"
+ (is (= #{{:schema nil :name "PEOPLE"} {:schema nil :name "ORDERS"}}
+ (set (#'validator/missing-inputs [] #{{:schema nil :name "PEOPLE"} {:schema nil :name "ORDERS"}} "PUBLIC"))))))
+
+(deftest missing-inputs-schema-defaulting-test
+ (testing "a bare reference is covered by a declared input in the driver's default schema"
+ ;; H2 default-schema is PUBLIC: bare PEOPLE ⇔ PUBLIC.PEOPLE. This is the case Alex's runner
+ ;; test hits (transform reads bare PEOPLE, input declares {:schema PUBLIC}).
+ (is (= [] (#'validator/missing-inputs [(sql-input "PUBLIC" "PEOPLE")] #{{:schema nil :name "PEOPLE"}} "PUBLIC"))))
+ (testing "a bare reference is NOT covered by a declared input in a non-default schema"
+ (is (= [{:schema nil :name "PEOPLE"}]
+ (#'validator/missing-inputs [(sql-input "OTHER" "PEOPLE")] #{{:schema nil :name "PEOPLE"}} "PUBLIC"))))
+ (testing "an explicitly-schemaed reference requires an exact schema match"
+ (is (= [{:schema "ANALYTICS" :name "PEOPLE"}]
+ (#'validator/missing-inputs [(sql-input "PUBLIC" "PEOPLE")] #{{:schema "ANALYTICS" :name "PEOPLE"}} "PUBLIC")))))
+
+(deftest unused-inputs-test
+ (testing "no unused inputs when every declared input is read"
+ (is (= [] (#'validator/unused-inputs [(sql-input "PUBLIC" "PEOPLE")] #{{:schema nil :name "PEOPLE"}} "PUBLIC"))))
+ (testing "a declared input the transform does not read is reported (stale/typo'd fake)"
+ (is (= [{:schema "PUBLIC" :name "ORDERZ"}]
+ (#'validator/unused-inputs [(sql-input "PUBLIC" "PEOPLE") (sql-input "PUBLIC" "ORDERZ")]
+ #{{:schema nil :name "PEOPLE"}} "PUBLIC"))))
+ (testing "matching is one rule shared with missing-inputs: bare read ⇔ default-schema input"
+ (is (= [] (#'validator/unused-inputs [(sql-input "PUBLIC" "PEOPLE")] #{{:schema nil :name "PEOPLE"}} "PUBLIC")))))
+
+(deftest surviving-references-test
+ (testing "Guard B: empty when every rewritten reference is a temp table and nothing dangles"
+ (is (= [] (#'validator/surviving-references
+ #{{:schema nil :name "MB_T1"} {:schema nil :name "MB_T2"}}
+ #{"MB_T1" "MB_T2"} #{}))))
+ (testing "a real table the rewrite missed is reported, labelled schema.name"
+ (is (= ["github_mart.stg_x"]
+ (#'validator/surviving-references
+ #{{:schema nil :name "MB_T1"} {:schema "github_mart" :name "stg_x"}}
+ #{"MB_T1"} #{}))))
+ (testing "a dangling column-qualifier (missing-table-alias) is reported"
+ (is (= ["people"]
+ (#'validator/surviving-references #{{:schema nil :name "MB_T1"}} #{"MB_T1"} #{"people"}))))
+ (testing "both survivor kinds combine, deduped and sorted"
+ (is (= ["a_table" "people"]
+ (#'validator/surviving-references
+ #{{:schema nil :name "MB_T1"} {:schema nil :name "a_table"}}
+ #{"MB_T1"} #{"people" "a_table"})))))
+
+(deftest table-label-test
+ (testing "table-label renders schema.name, or bare name when schema unknown — never a raw map"
+ (is (= "PEOPLE" (validator/table-label {:schema nil :name "PEOPLE"})))
+ (is (= "PUBLIC.PEOPLE" (validator/table-label {:schema "PUBLIC" :name "PEOPLE"})))))
+
+(deftest missing-inputs-schema-direction-test
+ (testing "a qualified reference matches a declared input in the same explicit schema"
+ (is (= [] (#'validator/missing-inputs [(sql-input "PUBLIC" "PEOPLE")] #{{:schema "PUBLIC" :name "PEOPLE"}} "PUBLIC"))))
+ (testing "schema defaulting is one-directional and fails closed: a DEFAULT-SCHEMA-qualified
+ reference is NOT covered by a bare-declared input — the author must qualify the input.
+ Safe (errs toward rejection, never a false pass); strict by design for LLM authors."
+ (is (= [{:schema "PUBLIC" :name "PEOPLE"}]
+ (#'validator/missing-inputs [(sql-input nil "PEOPLE")] #{{:schema "PUBLIC" :name "PEOPLE"}} "PUBLIC")))))
+
+(deftest missing-inputs-name-mismatch-test
+ (testing "same schema, different name is not a match"
+ (is (= [{:schema "PUBLIC" :name "ORDERS"}]
+ (#'validator/missing-inputs [(sql-input "PUBLIC" "PEOPLE")] #{{:schema "PUBLIC" :name "ORDERS"}} "PUBLIC")))))
diff --git a/frontend/src/metabase-types/api/mocks/settings.ts b/frontend/src/metabase-types/api/mocks/settings.ts
index 9bcf0773082..d17e19d9e8c 100644
--- a/frontend/src/metabase-types/api/mocks/settings.ts
+++ b/frontend/src/metabase-types/api/mocks/settings.ts
@@ -146,6 +146,7 @@ export const createMockTokenFeatures = (
"data-apps": false,
"transforms-basic": false,
"transforms-python": false,
+ "transforms-testing": false,
library: false,
library_retrieval: false,
"support-users": false,
diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.ts
index f1f47ae6b81..9933aed42b4 100644
--- a/frontend/src/metabase-types/api/settings.ts
+++ b/frontend/src/metabase-types/api/settings.ts
@@ -294,6 +294,7 @@ const tokenStatusFeatures = [
"sso",
"transforms-basic",
"transforms-python",
+ "transforms-testing",
"upload-management",
"whitelabel",
] as const;
@@ -377,6 +378,7 @@ export const tokenFeatures = [
"semantic_search",
"transforms-python",
"transforms-basic",
+ "transforms-testing",
"library",
"library_retrieval",
"support-users",
diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
index dec2ea46fbf..ccfc0ea72d6 100644
--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
@@ -45,6 +45,7 @@
BigQueryException
BigQueryOptions
Clustering
+ ConnectionProperty
Dataset
DatasetId
Field
@@ -1134,7 +1135,8 @@
;; statements by using a different driver-native API for affected-row counts.
:transforms/accurate-rows-affected false
:transforms/python true
- :transforms/table true}]
+ :transforms/table true
+ :transforms/testing false}]
(defmethod driver/database-supports? [:bigquery-cloud-sdk feature] [_driver _feature _db] supported?))
(defmethod driver/qualified-name-components :bigquery-cloud-sdk
@@ -1303,6 +1305,78 @@
(let [table-str (get-table-str table)]
[(str "DROP TABLE IF EXISTS " table-str)]))
+(defn- session-query-config
+ "The configuration of a job running the `[sql params]` query in the session `session-id`, or creating a new session
+ when `session-id` is nil."
+ ^QueryJobConfiguration [^String session-id [^String sql params]]
+ (let [builder (doto (QueryJobConfiguration/newBuilder sql)
+ (bigquery.params/set-parameters! params)
+ (.setUseLegacySql false))]
+ (if session-id
+ (.setConnectionProperties builder [(-> (ConnectionProperty/newBuilder)
+ (.setKey "session_id")
+ (.setValue session-id)
+ (.build))])
+ (.setCreateSession builder true))
+ (.build builder)))
+
+(defn- run-session-job!
+ "Runs the `[sql params]` query in the session `session-id`, or in a new session when `session-id` is nil, and returns
+ its finished job."
+ ^Job [^BigQuery client session-id query]
+ (let [job (.create client (JobInfo/of (session-query-config session-id query)) (u/varargs BigQuery$JobOption))]
+ (.getQueryResults job (u/varargs BigQuery$QueryResultsOption))
+ job))
+
+(defmethod driver/do-with-test-connection :bigquery-cloud-sdk
+ [driver database f]
+ (let [details (driver/connection-spec driver database)
+ client (database-details->client details)
+ _ (driver.conn/track-connection-acquisition! details)
+ job (.reload (run-session-job! client nil ["SELECT 1" nil]) (u/varargs BigQuery$JobOption))
+ session-id (.getSessionId (.getSessionInfo (.getStatistics job)))]
+ (try
+ (f {:client client, :session-id session-id})
+ (finally
+ (try
+ (run-session-job! client session-id ["CALL BQ.ABORT_SESSION()" nil])
+ (catch Exception e
+ (log/warnf "Failed to abort BigQuery transform test session: %s" (ex-message e))))))))
+
+(defmethod driver/execute-on-connection! :bigquery-cloud-sdk
+ [_driver {:keys [client session-id]} query]
+ (run-session-job! client session-id query)
+ {:rows-affected 0})
+
+(defmethod driver/query-on-connection :bigquery-cloud-sdk
+ [_driver {:keys [client session-id]} query {:keys [max-rows]}]
+ ;; Every scalar arrives as a String, and a NULL as an Object (TBD-1592), so the values go through the same per-field
+ ;; parsers the query processor reads results with rather than straight off the cell.
+ (let [^TableResult result (.getQueryResults (run-session-job! client session-id query)
+ (u/varargs BigQuery$QueryResultsOption))
+ ^Schema schema (.getSchema result)
+ parsers (get-field-parsers schema)]
+ {:columns (perf/mapv (fn [^Field field]
+ {:name (.getName field)
+ :database_type (.. field getType name)})
+ (.getFields schema))
+ :rows (into []
+ (comp (map (fn [^FieldValueList row]
+ (perf/mapv parse-field-value row parsers)))
+ (if max-rows (take max-rows) identity))
+ (.iterateAll result))}))
+
+(defmethod driver/compile-create-temp-table :bigquery-cloud-sdk
+ [_driver {:keys [table query]}]
+ (let [{sql-query :query sql-params :params} query]
+ [(format "CREATE TEMP TABLE %s AS %s" (get-table-str (keyword table)) sql-query)
+ sql-params]))
+
+(defmethod driver/compile-drop-temp-table :bigquery-cloud-sdk
+ [_driver table]
+ [(str "DROP TABLE IF EXISTS " (get-table-str (keyword table)))
+ []])
+
(defmethod driver/create-table! :bigquery-cloud-sdk
[driver database-id table-name column-definitions & {:keys [primary-key indexes]}]
(let [base (#'driver.sql-jdbc/create-table!-sql driver table-name column-definitions :primary-key primary-key)
@@ -1425,7 +1499,7 @@
(driver.conn/effective-details database))
(defmethod driver.sql/default-schema :bigquery-cloud-sdk
- [_]
+ [_driver _database]
nil)
(defmethod driver/create-schema-if-needed! :bigquery-cloud-sdk
diff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
index a41b335f8c9..29af85f395d 100644
--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
@@ -81,6 +81,7 @@
:test/time-type false
:transforms/python true
:transforms/table true
+ :transforms/testing true
:upload-with-auto-pk false
:window-functions/cumulative (not driver-api/is-test?)
:window-functions/offset true}]
@@ -135,6 +136,17 @@
(remove str/blank?)
first))
+(defmethod driver.sql/default-schema :clickhouse
+ [_driver database]
+ ;; ClickHouse opens a database where other engines have a default schema, so an unqualified reference resolves to
+ ;; the one this connection opened rather than to anything the driver could name on its own. `:db` is the older
+ ;; spelling of `:dbname`, and both are still in the wild.
+ (let [details (:details database)]
+ (or (first-db-name (:dbname details))
+ (first-db-name (:db details))
+ ;; Details carrying neither still connect, to the database `default-connection-details` names.
+ (first-db-name (:dbname default-connection-details)))))
+
(defmethod sql-jdbc.conn/connection-details->spec :clickhouse
[_ details]
(let [;; ensure defaults merge on top of nils
@@ -525,6 +537,33 @@
sql (str/join " " (map first pieces))]
(into [sql] (mapcat rest) pieces)))
+(defmethod driver/do-with-test-connection :clickhouse
+ [driver database f]
+ ((get-method driver/do-with-test-connection :sql-jdbc)
+ driver
+ database
+ (fn [^java.sql.Connection conn]
+ (let [^com.clickhouse.jdbc.ConnectionImpl clickhouse-conn (.unwrap conn com.clickhouse.jdbc.ConnectionImpl)
+ ^QuerySettings query-settings (.getDefaultQuerySettings clickhouse-conn)]
+ (.setDefaultQuerySettings clickhouse-conn (doto (QuerySettings. (.getAllSettings query-settings))
+ (.serverSetting "session_id" (str (random-uuid)))))
+ (try
+ (f conn)
+ (finally
+ (.setDefaultQuerySettings clickhouse-conn query-settings)))))))
+
+(defmethod driver/compile-create-temp-table :clickhouse
+ [driver {:keys [table query]}]
+ (let [{sql-query :query sql-params :params} query]
+ [(first (sql.qp/format-honeysql driver [:raw ["CREATE TEMPORARY TABLE " [:inline (keyword table)]
+ " ENGINE = Memory AS " sql-query]]))
+ sql-params]))
+
+(defmethod driver/compile-drop-temp-table :clickhouse
+ [driver table]
+ [(first (sql.qp/format-honeysql driver [:raw ["DROP TEMPORARY TABLE IF EXISTS " [:inline (keyword table)]]]))
+ []])
+
(defmethod driver/compile-insert :clickhouse
[driver {:keys [query output-table]}]
(let [{sql-query :query sql-params :params} query]
diff --git a/modules/drivers/redshift/src/metabase/driver/redshift.clj b/modules/drivers/redshift/src/metabase/driver/redshift.clj
index c548bcd7d17..ec4acc8e193 100644
--- a/modules/drivers/redshift/src/metabase/driver/redshift.clj
+++ b/modules/drivers/redshift/src/metabase/driver/redshift.clj
@@ -79,6 +79,7 @@
:transforms/accurate-rows-affected false
:transforms/python true
:transforms/table true
+ :transforms/testing true
:transforms/index-ddl false
:uuid-type false}]
(defmethod driver/database-supports? [:redshift feature] [_driver _feat _db] supported?))
diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
index 3dbbf3d9aa1..c41239b613a 100644
--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
@@ -92,7 +92,8 @@
:regex/lookaheads-and-lookbehinds false
:transforms/accurate-rows-affected false
:transforms/python true
- :transforms/table true}]
+ :transforms/table true
+ :transforms/testing false}]
(defmethod driver/database-supports? [:snowflake feature] [_driver _feature _db] supported?))
(mu/defn- quote-schema ^String [s :- :string] (sql.u/quote-name :snowflake :schema s))
@@ -119,9 +120,13 @@
:sunday)
(defmethod driver.sql/default-schema :snowflake
- [_]
+ [_driver _database]
"PUBLIC")
+(defmethod driver/temp-table-name :snowflake
+ [_driver]
+ (str "MB_TEST_" (u/upper-case-en (str/replace (str (random-uuid)) "-" ""))))
+
(defn- start-of-week-setting->snowflake-offset
"Value to use for the `WEEK_START` connection parameter -- see
https://docs.snowflake.com/en/sql-reference/parameters.html#label-week-start -- based on
diff --git a/modules/drivers/sqlserver/deps.edn b/modules/drivers/sqlserver/deps.edn
index e445aa48b39..b315856af31 100644
--- a/modules/drivers/sqlserver/deps.edn
+++ b/modules/drivers/sqlserver/deps.edn
@@ -2,4 +2,4 @@
["src" "resources"]
:deps
- {com.microsoft.sqlserver/mssql-jdbc {:mvn/version "13.2.1.jre11"}}}
+ {com.microsoft.sqlserver/mssql-jdbc {:mvn/version "13.4.0.jre11"}}}
diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
index fc870ecfeee..e7b611c08e6 100644
--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
@@ -39,6 +39,7 @@
[metabase.util.performance :as perf :refer [empty? mapv get-in not-empty]]
[next.jdbc :as next.jdbc])
(:import
+ (com.microsoft.sqlserver.jdbc ISQLServerConnection)
(java.sql Connection DatabaseMetaData PreparedStatement ResultSet Time)
(java.time LocalDate LocalDateTime LocalTime OffsetDateTime OffsetTime ZonedDateTime)
(java.time.format DateTimeFormatter)
@@ -77,6 +78,7 @@
:transforms/python true
:transforms/table true
:transforms/index-ddl true
+ :transforms/testing true
:jdbc/statements false
:describe-default-expr true
:describe-is-nullable true
@@ -111,7 +113,7 @@
:sunday)
(defmethod driver.sql/default-schema :sqlserver
- [_]
+ [_driver _database]
"dbo")
(defn- quote-schema [s] (sql.u/quote-name :sqlserver :schema s))
@@ -1190,6 +1192,30 @@
modified-sql (sql-tools/add-into-clause driver sql-query table-name)]
[modified-sql sql-params]))
+(defmethod driver/temp-table-name :sqlserver
+ [_driver]
+ (str "#mb_test_" (str/replace (str (random-uuid)) "-" "")))
+
+(defmethod driver/compile-create-temp-table :sqlserver
+ [driver {:keys [table query]}]
+ (let [{sql-query :query sql-params :params} query
+ ^String table-name (first (sql.qp/format-honeysql driver (keyword table)))]
+ [(sql-tools/add-into-clause driver sql-query table-name) sql-params]))
+
+(defmethod driver/do-with-test-connection :sqlserver
+ [driver database f]
+ ((get-method driver/do-with-test-connection :sql-jdbc)
+ driver
+ database
+ (fn [^Connection conn]
+ (let [^ISQLServerConnection sqlserver-conn (.unwrap conn ISQLServerConnection)
+ prepare-method (.getPrepareMethod sqlserver-conn)]
+ (.setPrepareMethod sqlserver-conn "scopeTempTablesToConnection")
+ (try
+ (f conn)
+ (finally
+ (.setPrepareMethod sqlserver-conn prepare-method)))))))
+
(defmethod driver/compile-insert :sqlserver
[driver {:keys [query output-table]}]
(let [{sql-query :query sql-params :params} query
diff --git a/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj b/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
index 89d90e69ac8..a698b407879 100644
--- a/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
+++ b/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
@@ -1271,3 +1271,47 @@
(is (= 4 (count (rows :venues 4))))
(testing "and a later query reading every row still succeeds"
(is (= 1000 (count (rows :checkins nil)))))))))
+
+(defn- temp-table-rows [conn table]
+ (driver/query-on-connection :sqlserver conn [(str "SELECT * FROM " table) []] {:max-rows 10}))
+
+(defn- create-temp-table! [conn table sql params]
+ (driver/execute-on-connection! :sqlserver conn
+ (driver/compile-create-temp-table :sqlserver {:table table
+ :query {:query sql :params params}})))
+
+(deftest transform-testing-temp-tables-are-session-local-test
+ (mt/test-driver :sqlserver
+ (testing "a transform test's temp table is visible to its own session only"
+ (let [table (driver/temp-table-name :sqlserver)]
+ (driver/do-with-test-connection
+ :sqlserver (mt/db)
+ (fn [conn]
+ (create-temp-table! conn table "SELECT 1 AS id" [])
+ (is (= [[1]] (:rows (temp-table-rows conn table))))
+ (driver/do-with-test-connection
+ :sqlserver (mt/db)
+ (fn [other-conn]
+ (is (thrown-with-msg? Exception #"Invalid object name"
+ (temp-table-rows other-conn table)))))))))))
+
+(deftest transform-testing-temp-table-from-parameterized-query-test
+ (mt/test-driver :sqlserver
+ (testing "a temp table created by a query with bound parameters outlives the statement that created it"
+ (let [table (driver/temp-table-name :sqlserver)]
+ (driver/do-with-test-connection
+ :sqlserver (mt/db)
+ (fn [conn]
+ (create-temp-table! conn table "SELECT CAST(? AS nvarchar(50)) AS name, CAST(? AS decimal(10,2)) AS price"
+ ["abc" 1.5M])
+ (is (=? {:rows [["abc" 1.50M]]} (temp-table-rows conn table)))))))
+ (testing "the test connection runs temp table statements as batches, and gets its prepare method back afterwards"
+ (let [physical (atom nil)]
+ (driver/do-with-test-connection
+ :sqlserver (mt/db)
+ (fn [^java.sql.Connection conn]
+ (let [sqlserver-conn (.unwrap conn com.microsoft.sqlserver.jdbc.ISQLServerConnection)]
+ (reset! physical sqlserver-conn)
+ (is (= "scopeTempTablesToConnection"
+ (.getPrepareMethod ^com.microsoft.sqlserver.jdbc.ISQLServerConnection sqlserver-conn))))))
+ (is (= "prepexec" (.getPrepareMethod ^com.microsoft.sqlserver.jdbc.ISQLServerConnection @physical)))))))
diff --git a/resources/migrations/065/20260915_transform_test.yaml b/resources/migrations/065/20260915_transform_test.yaml
new file mode 100644
index 00000000000..be8a181ec79
--- /dev/null
+++ b/resources/migrations/065/20260915_transform_test.yaml
@@ -0,0 +1,127 @@
+## Adds transform_test: a test for one transform, replacing some of its input tables with fixture data and
+## checking its output against expectations. The inputs and expectations are JSON documents validated by
+## metabase-enterprise.transform-testing.schema.
+
+databaseChangeLog:
+ - changeSet:
+ id: v65.2026-09-15T00:00:00
+ author: ranquild
+ comment: Add transform_test
+ changes:
+ - createTable:
+ tableName: transform_test
+ remarks: A test for a transform, with fixture inputs and expectations on its output
+ columns:
+ - column:
+ name: id
+ type: int
+ remarks: Unique ID
+ autoIncrement: true
+ constraints:
+ primaryKey: true
+ nullable: false
+ - column:
+ name: entity_id
+ type: char(21)
+ remarks: NanoID for serdes
+ constraints:
+ nullable: false
+ unique: true
+ - column:
+ name: transform_id
+ type: int
+ remarks: The transform under test
+ constraints:
+ nullable: false
+ - column:
+ name: creator_id
+ type: int
+ remarks: The user who created the test suite
+ constraints:
+ nullable: false
+ - column:
+ name: name
+ type: ${text.type}
+ remarks: The name of the test suite
+ constraints:
+ nullable: false
+ - column:
+ name: description
+ type: ${text.type}
+ remarks: A description of the test suite
+ - column:
+ name: inputs
+ type: ${text.type}
+ remarks: JSON array of the input tables replaced with fixture data
+ constraints:
+ nullable: false
+ - column:
+ name: expectations
+ type: ${text.type}
+ remarks: JSON array of the expectations checked against the transform output
+ constraints:
+ nullable: false
+ - column:
+ name: created_at
+ type: ${timestamp_type}
+ remarks: The timestamp this test suite was created
+ defaultValueComputed: current_timestamp
+ constraints:
+ nullable: false
+ - column:
+ name: updated_at
+ type: ${timestamp_type}
+ remarks: The timestamp this test suite was last updated
+ defaultValueComputed: current_timestamp
+ constraints:
+ nullable: false
+
+ - changeSet:
+ id: v65.2026-09-15T00:00:01
+ author: ranquild
+ comment: Index transform_test.transform_id
+ changes:
+ - createIndex:
+ tableName: transform_test
+ indexName: idx_transform_test_transform_id
+ columns:
+ - column:
+ name: transform_id
+
+ - changeSet:
+ id: v65.2026-09-15T00:00:02
+ author: ranquild
+ comment: Add FK constraint on transform_test.transform_id
+ changes:
+ - addForeignKeyConstraint:
+ baseTableName: transform_test
+ baseColumnNames: transform_id
+ referencedTableName: transform
+ referencedColumnNames: id
+ constraintName: fk_transform_test_transform_id
+ onDelete: CASCADE
+
+ - changeSet:
+ id: v65.2026-09-15T00:00:03
+ author: ranquild
+ comment: Index transform_test.creator_id
+ changes:
+ - createIndex:
+ tableName: transform_test
+ indexName: idx_transform_test_creator_id
+ columns:
+ - column:
+ name: creator_id
+
+ - changeSet:
+ id: v65.2026-09-15T00:00:04
+ author: ranquild
+ comment: Add FK constraint on transform_test.creator_id
+ changes:
+ - addForeignKeyConstraint:
+ baseTableName: transform_test
+ baseColumnNames: creator_id
+ referencedTableName: core_user
+ referencedColumnNames: id
+ constraintName: fk_transform_test_creator_id
+ onDelete: CASCADE
diff --git a/resources/python-sources/sql_tools.py b/resources/python-sources/sql_tools.py
index ca4c52e60f5..40a811485f1 100644
--- a/resources/python-sources/sql_tools.py
+++ b/resources/python-sources/sql_tools.py
@@ -74,6 +74,20 @@ def unquote_identifier(name: str, dialect: str = None):
pass
return (name, False)
+def temp_table_prefix(table):
+ """
+ The T-SQL temp table prefix of a table expression: "##" for a global temp table, "#" for a local one, and "" for
+ any other table. SQLGlot keeps the prefix as a flag on the identifier rather than in its name.
+ """
+ identifier = table.this
+ if not isinstance(identifier, exp.Identifier):
+ return ""
+ if identifier.args.get("global_"):
+ return "##"
+ if identifier.args.get("temporary"):
+ return "#"
+ return ""
+
def table_parts(table):
"""
Extract (catalog, schema, table) 3-tuple from a table expression.
@@ -82,13 +96,58 @@ def table_parts(table):
SQLGlot naming:
- table.catalog → SQL catalog (e.g., BigQuery project, Snowflake database)
- table.db → SQL schema (e.g., BigQuery dataset, Postgres schema)
- - table.name → table name
+ - table.name → table name, to which the T-SQL temp table prefix is added (see `temp_table_prefix`)
"""
name = table.name
if not isinstance(name, str) or not name:
# UDTFs and other function-based sources don't have traditional table names
return None
- return (table.catalog or None, table.db or None, name)
+ return (table.catalog or None, table.db or None, temp_table_prefix(table) + name)
+
+def shadowing_cte_names(node):
+ """The CTE names that shadow a table at `node`: those declared by a query `node` sits inside."""
+ names = set()
+ own = set()
+ current = node
+ while current is not None:
+ # A CTE does not shadow the table its own body reads: `WITH orders AS (SELECT * FROM orders)` selects
+ # from the real table. A recursive CTE is the exception -- reading its own name is what recursion is.
+ if isinstance(current, exp.CTE) and current.alias:
+ declaration = current.parent
+ if not (isinstance(declaration, exp.With) and declaration.args.get("recursive")):
+ own.add(current.alias.lower())
+ # Found by type rather than by arg name, which sqlglot spells `with_` in some versions and `with` in
+ # others.
+ for value in current.args.values():
+ if isinstance(value, exp.With):
+ names.update(cte.alias.lower() for cte in value.expressions if cte.alias)
+ current = current.parent
+ return names - own
+
+
+def statements(sql, dialect=None):
+ """Every statement in `sql`, without the empty ones a stray semicolon leaves behind.
+
+ A caller asking what a query reads, or rewriting what it reads, is asking about the whole string it was handed:
+ reading only the first statement answers a question nobody asked, quietly.
+ """
+ return [ast for ast in sqlglot.parse(sql, read=dialect) if ast is not None]
+
+
+def table_references(ast):
+ """Every table the statement `ast` names, as sqlglot Table nodes.
+
+ A CTE reference, a derived table, a VALUES clause and a table function are not tables and are left out; the
+ table a statement writes -- an INSERT, UPDATE, DELETE or MERGE target -- is one, and is included. This is the
+ walk `replace_names` rewrites by, so what a caller is told a query reads is what a rewrite would replace.
+ """
+ for table in ast.find_all(exp.Table):
+ if table_parts(table) is None:
+ continue
+ if not table.db and not table.catalog and table.name.lower() in shadowing_cte_names(table):
+ continue
+ yield table
+
def referenced_tables(sql: str, dialect: str = "postgres") -> str:
"""
@@ -112,17 +171,9 @@ def referenced_tables(sql: str, dialect: str = "postgres") -> str:
referenced_tables("SELECT * FROM myproject.analytics.events", "bigquery")
=> '[["myproject", "analytics", "events"]]'
"""
- ast = sqlglot.parse_one(sql, read=dialect)
- root_scope = optimizer.build_scope(ast)
-
- tables = set()
- for scope in root_scope.traverse():
- for source in scope.sources.values():
- if isinstance(source, exp.Table):
- parts = table_parts(source)
- if parts is not None:
- tables.add(parts)
-
+ tables = {table_parts(table)
+ for ast in statements(sql, dialect)
+ for table in table_references(ast)}
# Sort for deterministic output (nulls sort first via empty string)
return json.dumps(sorted(tables, key=lambda x: (x[0] or "", x[1] or "", x[2])))
@@ -430,7 +481,7 @@ def add_into_clause(sql: str, table_name: str, dialect: str = None) -> str:
8101. Wrapping each base table in a self-UNION breaks that IDENTITY lineage -- the UNION's second
branch is always empty and gets elided by SQL Server's contradiction detection, so each table is
still scanned once -- without touching filters, ORDER BY, TOP, or query params. CTEs, derived
- tables, and table-valued functions are left untouched.
+ tables, table-valued functions, and T-SQL `#`/`##` temp tables are left untouched.
A table with no explicit alias is wrapped under an alias equal to its bare name, and any column
in the same scope that referenced it via a schema/catalog-qualified name (e.g. `dbo.products.id`,
@@ -462,6 +513,10 @@ def add_into_clause(sql: str, table_name: str, dialect: str = None) -> str:
table_name_only = source.name
if not table_name_only or table_name_only in cte_names or alias in cte_names:
continue
+ # A T-SQL `#`/`##` temp table has no IDENTITY lineage worth breaking, and its derived-table alias would lose
+ # the prefix that qualified column references keep.
+ if temp_table_prefix(source):
+ continue
if id(source) in seen_ids:
continue
seen_ids.add(id(source))
@@ -548,6 +603,29 @@ def returned_columns_lineage(dialect, sql, default_table_schema, sqlglot_schema_
return json.dumps(dependencies)
+def fold_keys(mapping):
+ """
+ Case-insensitive view of a replacement map, for matching an identifier written in another case.
+
+ An identifier names the same thing however it is cased (Metabase treats every database as case-agnostic, see
+ `macaw-options`), so `FROM people` must match a key of `PEOPLE`. Keys that differ only in case have no single
+ answer, so they are dropped rather than resolved arbitrarily: an exact match still finds them.
+ """
+ folded = {}
+ ambiguous = set()
+ for key, value in mapping.items():
+ folded_key = tuple(part.lower() if isinstance(part, str) else part for part in key)
+ if folded_key in folded and folded[folded_key] != value:
+ ambiguous.add(folded_key)
+ folded[folded_key] = value
+ return {key: value for key, value in folded.items() if key not in ambiguous}
+
+
+def fold_lookup(folded, key):
+ """The value `key` maps to in a [[fold_keys]] map, ignoring the case of every part."""
+ return folded.get(tuple(part.lower() if isinstance(part, str) else part for part in key))
+
+
def replace_names(sql: str, replacements_json: str, dialect: str = None) -> str:
"""
Replace schema, table, and column names in a SQL query.
@@ -577,133 +655,138 @@ def replace_names(sql: str, replacements_json: str, dialect: str = None) -> str:
"""
replacements = json.loads(replacements_json)
schemas = replacements.get("schemas") or {}
- tables = replacements.get("tables") or [] # List of [key, value] pairs
- columns = replacements.get("columns") or [] # List of [key, value] pairs
-
- # Convert list-of-pairs to lookup dicts for O(1) matching
- # Tables: (db, schema, table) -> new_name. `db` is the catalog (BigQuery project,
- # ClickHouse database, etc). Keys with no :db default to None for back-compat.
- table_map = {}
- for item in tables:
- key, new_name = item
- db = key.get("db") # may be None
- schema = key.get("schema") # may be None
- table = key["table"]
- table_map[(db, schema, table)] = new_name
-
- # Columns: (schema, table, column) -> new_name
- column_map = {}
- for item in columns:
- key, new_name = item
- schema = key.get("schema")
- table = key.get("table") # may be None for unqualified
- column = key["column"]
- column_map[(schema, table, column)] = new_name
-
- ast = sqlglot.parse_one(sql, read=dialect)
-
- def rename_fn(node):
- # Schema rename (appears in Table.db)
- if isinstance(node, exp.Table):
- # Capture original values BEFORE any modifications (important for lookup).
- # SQLGlot stores 3-part identifiers as catalog.db.this -> catalog=BigQuery
- # project / ClickHouse db, db=schema, this=table.
- original_db = node.catalog or None
- original_schema = node.db
- original_table = node.name
- # Preserve original quoting status for renamed identifiers.
- # Some Table nodes have non-Identifier children (e.g., ExplodingGenerateSeries,
- # Anonymous) that lack a `quoted` attribute — default to False for those.
- db_node = node.args.get("db")
- catalog_node = node.args.get("catalog")
- original_schema_quoted = db_node.quoted if isinstance(db_node, exp.Identifier) else False
- original_db_quoted = catalog_node.quoted if isinstance(catalog_node, exp.Identifier) else False
- original_table_quoted = node.this.quoted if isinstance(node.this, exp.Identifier) else False
-
- # Rename schema if present
- if original_schema and original_schema in schemas:
- raw_schema, was_quoted = unquote_identifier(schemas[original_schema], dialect)
- schema_quoted = original_schema_quoted or was_quoted or needs_quoting(raw_schema, dialect)
- node.set("db", exp.Identifier(this=raw_schema, quoted=schema_quoted))
-
- # Rename table - try most-specific match first, falling back to less-qualified keys.
- # Order: (db, schema, table), (None, schema, table), (None, None, table).
- # This lets a remapping omit :db when it doesn't matter (Postgres-style),
- # and still match a 3-part SQL reference, while a remapping that does include :db
- # only matches references that have the matching catalog.
- new_table = (table_map.get((original_db, original_schema, original_table)) or
- table_map.get((None, original_schema, original_table)) or
- table_map.get((None, None, original_table)))
- if new_table:
- if isinstance(new_table, dict):
- # New format: {db?, schema?, table?}
- if new_table.get("db"):
- raw_db, was_quoted = unquote_identifier(new_table["db"], dialect)
- db_quoted = original_db_quoted or was_quoted or needs_quoting(raw_db, dialect)
- node.set("catalog", exp.Identifier(this=raw_db, quoted=db_quoted))
- elif "db" in new_table and new_table["db"] is None:
- # Explicitly clear the catalog (database/project) qualifier.
- node.set("catalog", None)
- if new_table.get("schema"):
- # When injecting a new schema, quote if it contains special characters
- raw_schema, was_quoted = unquote_identifier(new_table["schema"], dialect)
- schema_quoted = original_schema_quoted or was_quoted or needs_quoting(raw_schema, dialect)
- node.set("db", exp.Identifier(this=raw_schema, quoted=schema_quoted))
- elif "schema" in new_table and new_table["schema"] is None:
- # Explicitly clear the schema from the AST node. This matches Macaw's
- # behavior: {:schema nil :table "x"} means "remove the schema qualifier",
- # turning e.g. `FROM public.orders` into `FROM x`.
- node.set("db", None)
- if new_table.get("table"):
- raw_table, was_quoted = unquote_identifier(new_table["table"], dialect)
- table_quoted = original_table_quoted or was_quoted or needs_quoting(raw_table, dialect)
- node.set("this", exp.Identifier(this=raw_table, quoted=table_quoted))
- else:
- # String: just the table name
- raw_name, was_quoted = unquote_identifier(new_table, dialect)
- table_quoted = original_table_quoted or was_quoted or needs_quoting(raw_name, dialect)
- node.set("this", exp.Identifier(this=raw_name, quoted=table_quoted))
-
- # Column rename
- elif isinstance(node, exp.Column):
- col_name = node.name
- col_table = node.table # May be None if column is unqualified (e.g., "SELECT id" not "SELECT t.id")
- # Preserve original quoting status
- original_col_quoted = node.this.quoted if isinstance(node.this, exp.Identifier) else False
-
- # Try to find a matching column rename.
- # The challenge: replacement key might be {:table "orders" :column "id"}
- # but the SQL column ref might just be "id" (unqualified).
- # We need to match flexibly:
- # - Exact match: (schema, table, column) all match
- # - Table match: column and table match, schema is None in key
- # - Column-only match: just column matches (when no table qualifier in SQL)
- new_col = None
-
- # Iterate through all column mappings and find best match
- for (key_schema, key_table, key_col), new_name in column_map.items():
- if key_col != col_name:
- continue
- # Column name matches, now check table qualifier
- # Note: SQLGlot uses empty string (not None) for missing table qualifier
- if col_table:
- # SQL has table qualifier - match if tables are equal
- if key_table == col_table:
- new_col = new_name
- break
- else:
- # SQL has no table qualifier - accept any table in key
- # (this is the common case: "SELECT id FROM orders" with key {:table "orders" :column "id"})
- new_col = new_name
- break
-
- if new_col:
- node.set("this", exp.Identifier(this=new_col, quoted=original_col_quoted))
-
- return node
+ # Tables: (db, schema, table) -> replacement. `db` is the catalog (BigQuery project, ClickHouse database, etc),
+ # None when the key doesn't name one.
+ table_map = {(key.get("db"), key.get("schema"), key["table"]): new_name
+ for key, new_name in replacements.get("tables") or []}
+ # Columns: (schema, table, column) -> new name. `table` is None for a key that matches any table.
+ column_map = {(key.get("schema"), key.get("table"), key["column"]): new_name
+ for key, new_name in replacements.get("columns") or []}
+ folded_schemas = fold_keys({(name,): new_name for name, new_name in schemas.items()})
+
+ def rewrite(ast):
+ """The statement `ast` with every replacement applied."""
+
+ # Names a bare column qualifier may refer to instead of a table: table and subquery aliases and CTE names. Folded,
+ # because a reference matches a name whatever its case.
+ aliases = {node.alias.lower() for node in ast.find_all(exp.Table, exp.Subquery, exp.CTE) if node.alias}
+
+ def part_matches(key_part, ref_part, quoted):
+ """Whether one part of a replacement key names the same thing as the reference's."""
+ if key_part is None or ref_part is None:
+ return key_part is None and ref_part is None
+ # A quoted reference is case-significant to the engine — Postgres `"Orders"` is not `orders` — so that part
+ # matches only as written. An unquoted one matches whatever its case.
+ return key_part == ref_part if quoted else key_part.lower() == ref_part.lower()
+
+ def find_table_replacement(db, schema, table, quoted):
+ # Most specific key first: (db, schema, table), then (None, schema, table), then (None, None, table).
+ # A key without a db matches a reference in any catalog; a key with one only matches that catalog.
+ # Each shape is tried as written before it is tried part by part, so an exact key always wins, and keys that
+ # differ only in the case of an unquoted part answer nothing rather than one of them arbitrarily.
+ shapes = ((db, schema, table), (None, schema, table), (None, None, table))
+ for key in shapes:
+ replacement = table_map.get(key)
+ if replacement:
+ return replacement
+ for key in shapes:
+ matched = [table_map[map_key]
+ for map_key in table_map
+ if all(part_matches(map_part, ref_part, was_quoted)
+ for map_part, ref_part, was_quoted in zip(map_key, key, quoted))]
+ if matched:
+ # Keys that differ only in the case of an unquoted part are ambiguous unless they all name the same
+ # replacement, and an ambiguous reference is left alone rather than pointed at one of them.
+ first = matched[0]
+ return first if all(other == first for other in matched) else None
+ return None
- transformed = ast.transform(rename_fn)
- return transformed.sql(dialect=dialect)
+ def quoted_parts(node, table_arg):
+ """Which parts of a table reference the query quoted, in replacement-key order."""
+ def quoted(arg):
+ value = node.args.get(arg)
+ return isinstance(value, exp.Identifier) and value.quoted
+
+ return (quoted("catalog"), quoted("db"), quoted(table_arg))
+
+ def set_identifier(node, arg, name):
+ # Sets `arg` of `node` to the identifier `name`, quoted if the identifier it replaces was, if `name` comes
+ # quoted, or if `name` needs quoting. Some nodes hold non-Identifier children (e.g. Anonymous), which count
+ # as unquoted.
+ original = node.args.get(arg)
+ original_quoted = isinstance(original, exp.Identifier) and original.quoted
+ raw_name, was_quoted = unquote_identifier(name, dialect)
+ quoted = original_quoted or was_quoted or needs_quoting(raw_name, dialect)
+ node.set(arg, exp.Identifier(this=raw_name, quoted=quoted))
+
+ def replace_table(node, replacement, catalog_arg, schema_arg, table_arg):
+ # Applies a table replacement to the name parts `node` keeps under the given args: a Table node keeps them in
+ # catalog/db/this, a Column node keeps its qualifier in catalog/db/table. A string replacement renames the
+ # table. A {db?, schema?, table?} replacement sets each part it names, and a nil db or schema clears that
+ # qualifier, so {:schema nil :table "x"} turns `public.orders` into `x` (matching Macaw).
+ if isinstance(replacement, str):
+ replacement = {"table": replacement}
+ for key, arg in (("db", catalog_arg), ("schema", schema_arg), ("table", table_arg)):
+ if replacement.get(key):
+ set_identifier(node, arg, replacement[key])
+ elif key != "table" and key in replacement and replacement[key] is None:
+ node.set(arg, None)
+
+ def rename_fn(node):
+ if isinstance(node, exp.Table):
+ # SQLGlot stores a 3-part name as catalog.db.this: catalog is the BigQuery project or ClickHouse database,
+ # db the schema. The replacement is looked up by the original parts, before the schema is renamed.
+ db, schema, table = node.catalog or None, node.db, node.name
+ new_schema = schemas.get(schema) or fold_lookup(folded_schemas, (schema,)) if schema else None
+ if new_schema:
+ set_identifier(node, "db", new_schema)
+ # A CTE name shadows a real table of the same name, so an unqualified reference to one declared in an
+ # enclosing query names the CTE and must be left alone — renaming it would point the query at the
+ # replacement table and silently skip the CTE.
+ if not db and not schema and table.lower() in shadowing_cte_names(node):
+ return node
+ replacement = find_table_replacement(db, schema, table, quoted_parts(node, "this"))
+ if replacement:
+ replace_table(node, replacement, "catalog", "db", "this")
+
+ elif isinstance(node, exp.Column):
+ # SQLGlot uses "" (not None) for a missing qualifier part.
+ column, table = node.name, node.table
+ # A qualifier naming a replaced table (e.g. `public.orders.id`) follows the table. A qualifier without a
+ # schema may name an alias instead, which is left alone.
+ if table and (node.db or table.lower() not in aliases):
+ replacement = find_table_replacement(
+ node.catalog or None, node.db, table, quoted_parts(node, "table")
+ )
+ if replacement:
+ replace_table(node, replacement, "catalog", "db", "table")
+ # A column key matches a column qualified by the key's table, or an unqualified column with any table
+ # (the common case: "SELECT id FROM orders" with key {:table "orders" :column "id"}).
+ def column_matches(key_table, key_column):
+ return (key_column.lower() == column.lower()
+ and (not table or (key_table or "").lower() == table.lower()))
+
+ new_column = next((new_name
+ for (_, key_table, key_column), new_name in column_map.items()
+ if column_matches(key_table, key_column)),
+ None)
+ if new_column:
+ column_quoted = isinstance(node.this, exp.Identifier) and node.this.quoted
+ node.set("this", exp.Identifier(this=new_column, quoted=column_quoted))
+
+ return node
+
+ # Mutated in place rather than through `ast.transform`, whose copy detaches a node from its parents while the
+ # function runs — and a CTE only shadows a table for the query that declares it, which is a question about
+ # ancestors.
+ for table_node in list(ast.find_all(exp.Table)):
+ rename_fn(table_node)
+ for column_node in list(ast.find_all(exp.Column)):
+ rename_fn(column_node)
+ return ast.sql(dialect=dialect)
+
+ rewritten = [rewrite(ast) for ast in statements(sql, dialect)]
+ return "; ".join(rewritten) if rewritten else sql
#############################################################################
diff --git a/src/metabase/analytics/stats.clj b/src/metabase/analytics/stats.clj
index b2717c3203f..b4d5886122e 100644
--- a/src/metabase/analytics/stats.clj
+++ b/src/metabase/analytics/stats.clj
@@ -966,6 +966,9 @@
{:name :transforms-python
:available (premium-features/enable-python-transforms?)
:enabled (premium-features/enable-python-transforms?)}
+ {:name :transforms-testing
+ :available (premium-features/enable-transform-testing?)
+ :enabled (premium-features/enable-transform-testing?)}
{:name :dependencies
:available (premium-features/enable-dependencies?)
:enabled (premium-features/enable-dependencies?)}
diff --git a/src/metabase/cmd/copy.clj b/src/metabase/cmd/copy.clj
index 834315840cd..2b9e416f98b 100644
--- a/src/metabase/cmd/copy.clj
+++ b/src/metabase/cmd/copy.clj
@@ -161,6 +161,8 @@
:model/TransformTransformTag
:model/TransformJob
:model/TransformJobTransformTag
+ ;; By table name: migrations create it on every edition, but its model is EE-only.
+ :transform_test
;; Serialization never exports run history; a whole-instance move keeps it.
;; A run still in flight at dump time arrives marked running, and the transform timeout job reaps it.
:model/TransformJobRun
diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj
index 412808a47d4..69625bb8353 100644
--- a/src/metabase/driver.clj
+++ b/src/metabase/driver.clj
@@ -956,6 +956,11 @@
;; Does this driver support executing python transforms?
:transforms/python
;;
+ ;; Does this driver support running transform test suites against temp tables? Drivers with this feature
+ ;; implement [[temp-table-name]], [[compile-create-temp-table]], [[compile-drop-temp-table]],
+ ;; [[do-with-test-connection]], [[execute-on-connection!]] and [[query-on-connection]].
+ :transforms/testing
+ ;;
;; Does this driver support creating an index (in the broad sense -- see the comment above
;; [[supported-index-methods]]) as a standalone statement after the transform target table already exists?
;; Drivers with this feature implement [[supported-index-methods]] and [[compile-create-index]]. Contrast with
@@ -1452,6 +1457,47 @@
dispatch-on-initialized-driver
:hierarchy #'hierarchy)
+(defmulti temp-table-name
+ "Returns a new unique name for a temp table created while running a transform test suite."
+ {:added "0.64.0", :arglists '([driver])}
+ dispatch-on-initialized-driver
+ :hierarchy #'hierarchy)
+
+(defmulti compile-create-temp-table
+ "Compiles the `[sql params]` statement creating the temp table `table` from the compiled `query`."
+ {:added "0.64.0", :arglists '([driver {:keys [table query]}])}
+ dispatch-on-initialized-driver
+ :hierarchy #'hierarchy)
+
+(defmulti compile-drop-temp-table
+ "Compiles the `[sql params]` statement dropping the temp table `table` if it exists."
+ {:added "0.64.0", :arglists '([driver table])}
+ dispatch-on-initialized-driver
+ :hierarchy #'hierarchy)
+
+(defmulti do-with-test-connection
+ "Calls `f` with a single connection to `database` that keeps its temp tables between statements and rolls back
+ everything it did afterwards."
+ {:added "0.64.0", :arglists '([driver database f])}
+ dispatch-on-initialized-driver
+ :hierarchy #'hierarchy)
+
+(defmulti execute-on-connection!
+ "Executes the `[sql params]` statement on a connection from [[do-with-test-connection]]."
+ {:added "0.64.0", :arglists '([driver conn query])}
+ dispatch-on-initialized-driver
+ :hierarchy #'hierarchy)
+
+(defmulti query-on-connection
+ "`{:rows :columns}` for the `[sql params]` `query` on a connection from [[do-with-test-connection]]:
+ at most `max-rows` rows as vectors, and one `{:name :database_type}` per column, in order.
+
+ Names and types are the engine's own, after whatever folding it applies to unquoted identifiers,
+ and are reported whether or not any row comes back."
+ {:added "0.64.0", :arglists '([driver conn query {:keys [max-rows]}])}
+ dispatch-on-initialized-driver
+ :hierarchy #'hierarchy)
+
(defmulti compile-drop-table
"Compiles the sql for a drop table statement for a given table."
{:added "0.57.0", :arglists '([driver table])}
diff --git a/src/metabase/driver/h2.clj b/src/metabase/driver/h2.clj
index 825292bdcf6..b148998da22 100644
--- a/src/metabase/driver/h2.clj
+++ b/src/metabase/driver/h2.clj
@@ -104,7 +104,8 @@
:describe-is-generated true
:describe-is-nullable true
:describe-default-expr true
- :metadata/table-existence-check true}]
+ :metadata/table-existence-check true
+ :transforms/testing true}]
(defmethod driver/database-supports? [:h2 feature]
[_driver _feature _database]
supported?))
@@ -735,8 +736,19 @@
(u/upper-case-en name-str))
(defmethod sql/default-schema :h2
- [_]
+ [_driver _database]
"PUBLIC")
+(defmethod driver/temp-table-name :h2
+ [_driver]
+ (str "MB_TEST_" (u/upper-case-en (str/replace (str (random-uuid)) "-" ""))))
+
+(defmethod driver/compile-create-temp-table :h2
+ [driver {:keys [table query]}]
+ (let [{sql-query :query sql-params :params} query]
+ [(first (sql.qp/format-honeysql driver [:raw ["CREATE LOCAL TEMPORARY TABLE " [:inline (keyword table)]
+ " TRANSACTIONAL AS " sql-query]]))
+ sql-params]))
+
(defmethod driver/llm-sql-dialect-resource :h2 [_]
"metabot/prompts/dialects/h2.md")
diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.clj
index 243877299fc..d67332c9562 100644
--- a/src/metabase/driver/mysql.clj
+++ b/src/metabase/driver/mysql.clj
@@ -101,6 +101,7 @@
:metadata/table-existence-check true
:transforms/python true
:transforms/table true
+ :transforms/testing true
:transforms/index-ddl true
:describe-default-expr true
:describe-is-nullable true
@@ -1322,7 +1323,7 @@
(= (sql-jdbc/get-sql-state e) "42S02"))
(defmethod driver.sql/default-schema :mysql
- [_]
+ [_driver _database]
nil)
;; Override db-type-name to handle tinyint(1) as boolean
@@ -1447,5 +1448,10 @@
(partition-by :index_name)
(mapv mysql-index-rows->index)))
+(defmethod driver/compile-drop-temp-table :mysql
+ [driver table]
+ [(first (sql.qp/format-honeysql driver [:raw ["DROP TEMPORARY TABLE IF EXISTS " [:inline (keyword table)]]]))
+ []])
+
(defmethod driver/llm-sql-dialect-resource :mysql [_]
"metabot/prompts/dialects/mysql.md")
diff --git a/src/metabase/driver/postgres.clj b/src/metabase/driver/postgres.clj
index d886bb91453..eed5749c4b4 100644
--- a/src/metabase/driver/postgres.clj
+++ b/src/metabase/driver/postgres.clj
@@ -99,6 +99,7 @@
:transforms/index-ddl true
:transforms/python true
:transforms/table true
+ :transforms/testing true
:uploads true
:uuid-type true}]
(defmethod driver/database-supports? [:postgres feature] [_driver _feature _db] supported?))
diff --git a/src/metabase/driver/sql/normalize.clj b/src/metabase/driver/sql/normalize.clj
index 414f57a78fc..ad43a8085a8 100644
--- a/src/metabase/driver/sql/normalize.clj
+++ b/src/metabase/driver/sql/normalize.clj
@@ -40,15 +40,19 @@
error))
(defmulti default-schema
- "Returns the default schema for a given database driver.
+ "The schema an unqualified table reference resolves to in `database`, or nil when the driver has none.
+
+ `database` may be nil when the caller has none in hand, and a driver whose default schema is a property of the
+ connection rather than of the driver — ClickHouse opens a database where other engines have a default schema —
+ answers nil for it.
Drivers that support any of the `:transforms/...` features must implement this method."
- {:added "0.57.0" :arglists '([driver])}
- driver/dispatch-on-initialized-driver
+ {:added "0.57.0" :arglists '([driver database])}
+ (fn [driver _database] (driver/dispatch-on-initialized-driver driver))
:hierarchy #'driver/hierarchy)
(defmethod default-schema :sql
- [_]
+ [_driver _database]
"public")
(defmulti reserved-literal
diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.clj
index 1cf04ff77f2..4a710d92eb8 100644
--- a/src/metabase/driver/sql/query_processor.clj
+++ b/src/metabase/driver/sql/query_processor.clj
@@ -2329,6 +2329,21 @@
{:insert-into [(keyword output-table) {:raw sql-query}]}))
sql-params]))
+(defmethod driver/temp-table-name :sql
+ [_driver]
+ (str "mb_test_" (str/replace (str (random-uuid)) "-" "")))
+
+(defmethod driver/compile-create-temp-table :sql
+ [driver {:keys [table query]}]
+ (let [{sql-query :query sql-params :params} query]
+ [(first (format-honeysql driver [:raw ["CREATE TEMPORARY TABLE " [:inline (keyword table)] " AS " sql-query]]))
+ sql-params]))
+
+(defmethod driver/compile-drop-temp-table :sql
+ [driver table]
+ [(first (format-honeysql driver [:raw ["DROP TABLE IF EXISTS " [:inline (keyword table)]]]))
+ []])
+
(defmethod driver/compile-drop-table :sql
[driver table]
(format-honeysql driver {:drop-table [:if-exists (keyword table)]}))
diff --git a/src/metabase/driver/sql_jdbc/execute.clj b/src/metabase/driver/sql_jdbc/execute.clj
index 480b8e5b00e..26b38f76a81 100644
--- a/src/metabase/driver/sql_jdbc/execute.clj
+++ b/src/metabase/driver/sql_jdbc/execute.clj
@@ -1000,6 +1000,61 @@
(.executeUpdate ^PreparedStatement stmt)
(.executeUpdate stmt sql))}))
+(defmethod driver/do-with-test-connection :sql-jdbc
+ [driver database f]
+ (do-with-connection-with-options
+ driver
+ database
+ {:write? true}
+ (fn [^Connection conn]
+ (.setAutoCommit conn false)
+ (try
+ (f conn)
+ (finally
+ ;; Neither may throw past the body's own exception, and the connection goes back to the pool either way:
+ ;; leaving it inside a transaction hands the next borrower a session that answers every statement with
+ ;; "current transaction is aborted" on Postgres and Redshift.
+ (try
+ (.rollback conn)
+ (catch Throwable e
+ (log/warnf "Failed to roll back the transform test transaction: %s" (ex-message e))))
+ (try
+ (.setAutoCommit conn true)
+ (catch Throwable e
+ (log/warnf "Failed to restore autoCommit after the transform test: %s" (ex-message e)))))))))
+
+(defmethod driver/execute-on-connection! :sql-jdbc
+ [driver conn [sql params]]
+ (create-and-execute-statement! driver conn sql params))
+
+(defmethod driver/query-on-connection :sql-jdbc
+ [driver conn [sql params] {:keys [max-rows]}]
+ (with-open [stmt (statement-or-prepared-statement driver conn sql params (driver-api/canceled-chan))]
+ (when (and max-rows (pos? max-rows))
+ ;; The cap is the statement's only bound. Every statement otherwise carries the streaming fetch size, and
+ ;; Redshift aborts the transaction when a row cap is asked of a cursor; `setMaxRows` bounds the memory a
+ ;; cursor would have bounded anyway.
+ (try
+ (.setFetchSize stmt 0)
+ (catch Throwable e
+ (log/debugf "Error clearing the statement fetch size: %s" (ex-message e))))
+ (.setMaxRows stmt (int max-rows)))
+ (with-open [^ResultSet rs (if (instance? PreparedStatement stmt)
+ (.executeQuery ^PreparedStatement stmt)
+ (.executeQuery stmt ^String sql))]
+ (let [md (.getMetaData rs)
+ column-count (.getColumnCount md)]
+ {:columns (mapv (fn [i]
+ {:name (.getColumnLabel md (int i))
+ :database_type (.getColumnTypeName md (int i))})
+ (range 1 (inc column-count)))
+ ;; `setMaxRows` reads 0 as unlimited, so the cap is enforced here rather than left to it.
+ :rows (loop [rows []]
+ (if (and (or (nil? max-rows) (< (count rows) max-rows))
+ (.next rs))
+ (recur (conj rows (mapv #(.getObject rs (int %)) (range 1 (inc column-count)))))
+ rows))}))))
+
(defmethod driver/execute-raw-queries! :sql-jdbc
[driver conn-spec queries]
(try
diff --git a/src/metabase/models/resolution.clj b/src/metabase/models/resolution.clj
index b914987f5ed..87a371e72ef 100644
--- a/src/metabase/models/resolution.clj
+++ b/src/metabase/models/resolution.clj
@@ -148,6 +148,7 @@
:model/TransformRun metabase.transforms.models.transform-run
:model/TransformRunCancelation metabase.transforms.models.transform-run-cancelation
:model/TransformTag metabase.transforms.models.transform-tag
+ :model/TransformTest metabase-enterprise.transform-testing.models
:model/TransformTransformTag metabase.transforms.models.transform-transform-tag
:model/Undo metabase-enterprise.action-v2.models.undo
:model/SourceDimensionDaily metabase.usage-metadata.models.source-dimension-daily
diff --git a/src/metabase/premium_features/core.clj b/src/metabase/premium_features/core.clj
index 47f95a6e0fd..9d9c23f58fa 100644
--- a/src/metabase/premium_features/core.clj
+++ b/src/metabase/premium_features/core.clj
@@ -91,6 +91,7 @@
enable-support-users?
enable-basic-transforms?
enable-python-transforms?
+ enable-transform-testing?
enable-upload-management?
enable-whitelabeling?
enable-writable-connection?
diff --git a/src/metabase/premium_features/settings.clj b/src/metabase/premium_features/settings.clj
index a0ce1743ee9..9d4303746c2 100644
--- a/src/metabase/premium_features/settings.clj
+++ b/src/metabase/premium_features/settings.clj
@@ -306,6 +306,10 @@
"Should we allow users to use Python transforms?"
:transforms-python)
+(define-premium-feature enable-transform-testing?
+ "Should we allow users to author and run tests against their transforms?"
+ :transforms-testing)
+
(define-premium-feature ^{:added "0.57.0"} enable-dependencies?
"Should we allow users to use dependency tracking?"
:dependencies)
@@ -437,6 +441,7 @@
:tenants (enable-tenants?)
:transforms-basic (enable-basic-transforms?)
:transforms-python (enable-python-transforms?)
+ :transforms-testing (enable-transform-testing?)
:upload_management (enable-upload-management?)
:whitelabel (enable-whitelabeling?)
:writable_connection (enable-writable-connection?)
diff --git a/src/metabase/remote_sync/events.clj b/src/metabase/remote_sync/events.clj
index 03180743328..266b8e49f93 100644
--- a/src/metabase/remote_sync/events.clj
+++ b/src/metabase/remote_sync/events.clj
@@ -40,6 +40,12 @@
(events/derive! :event/transform-tag-update ::transform-tag-event)
(events/derive! :event/transform-tag-delete ::transform-tag-event)
+;; Transform Test events
+(events/derive! ::transform-test-event :metabase/event)
+(events/derive! :event/transform-test-create ::transform-test-event)
+(events/derive! :event/transform-test-update ::transform-test-event)
+(events/derive! :event/transform-test-delete ::transform-test-event)
+
;; Transform Run events
(events/derive! ::transform-run-event :metabase/event)
(events/derive! :event/transform-run-complete ::transform-run-event)
diff --git a/src/metabase/sql_parsing/core.clj b/src/metabase/sql_parsing/core.clj
index f9fa32d9e40..23614b417bc 100644
--- a/src/metabase/sql_parsing/core.clj
+++ b/src/metabase/sql_parsing/core.clj
@@ -39,6 +39,57 @@
(boolean (and (:sql-parsing/error data)
(= "ParseError" (:sql-parsing/python-error-type data))))))
+(def ^:private ansi-escape
+ "A terminal escape sequence. sqlglot underlines the token it stopped at with one, which reads as an underline in
+ a terminal and as raw bytes everywhere else a diagnostic goes."
+ #"\u001b\[[0-9;]*[A-Za-z]")
+
+(def ^:private underline-start "\u001b[4m")
+(def ^:private underline-end "\u001b[0m")
+
+(defn- caret-line
+ "A line of carets under the token `snippet` underlines, or nil when it underlines none.
+
+ The offsets are into the text with the escapes taken out, which is the `snippet` its reader will see."
+ [snippet]
+ (when-let [start (str/index-of snippet underline-start)]
+ (let [plain (fn [s] (str/replace s ansi-escape ""))
+ after (subs snippet (+ start (count underline-start)))
+ token (plain (if-let [end (str/index-of after underline-end)]
+ (subs after 0 end)
+ after))]
+ (when (seq token)
+ (str (apply str (repeat (count (plain (subs snippet 0 start))) \space))
+ (apply str (repeat (count token) \^)))))))
+
+(defn- without-parser-internals
+ "`detail` with what the parser says about its own workings replaced by what it was reading."
+ [detail]
+ (-> detail
+ (str/replace-first #"^sqlglot call failed: " "")
+ (str/replace #" for <class '[^']*'>" "")
+ (str/replace #"<Token [^>]*?text: ([^,>]*),[^>]*>" "'$1'")
+ (str/replace #"but got None\b" "but got the end of the query")))
+
+(defn parse-error-message
+ "The parse failure `e` reports, as a diagnostic its reader can act on: what the parser could not read, where
+ (`Line`/`Col`), and the offending SQL with a caret under the token it stopped at.
+
+ What the parser says about its own workings comes out — the Python class it was building, the token object it
+ was holding, the escape codes it underlines with, the wrapper the transport adds — and the underline it drew
+ becomes the caret line, an underline being what an escape code is only in a terminal.
+
+ For handing a parse failure to whoever wrote the SQL, or to a model writing it. Returns nil when `e` carries no
+ message of its own."
+ [e]
+ (when-let [reported (or (some-> (ex-cause e) ex-message) (ex-message e))]
+ (let [[detail snippet] (str/split reported #"\n" 2)]
+ (->> [(without-parser-internals detail)
+ (some-> snippet (str/replace ansi-escape ""))
+ (some-> snippet caret-line)]
+ (remove nil?)
+ (str/join "\n")))))
+
;;; ------------------------------------- Large literal-list stripping -----------------------------------------
;; Large literal lists — VALUES clauses, IN lists (flat or tuple), and ARRAY literals with
diff --git a/src/metabase/sql_tools/common.clj b/src/metabase/sql_tools/common.clj
index a053bc0156a..345a4edfbad 100644
--- a/src/metabase/sql_tools/common.clj
+++ b/src/metabase/sql_tools/common.clj
@@ -42,10 +42,12 @@
(defn find-table-or-transform
"Given a table and schema that has been parsed out of a native query, finds either a matching table or a matching transform.
- It will return either {:table table-id} or {:transform transform-id}, or nil if neither is found."
- [driver tables transforms {search-table :table raw-schema :schema}]
+ It will return either {:table table-id} or {:transform transform-id}, or nil if neither is found.
+
+ An unqualified reference resolves to `database`'s default schema, so the database it was parsed against decides."
+ [driver database tables transforms {search-table :table raw-schema :schema}]
(let [search-schema (or raw-schema
- (sql.normalize/default-schema driver))
+ (sql.normalize/default-schema driver database))
normalize (partial normalize-name driver)
matches? (fn [db-table db-schema]
(and (= (normalize search-table) (normalize db-table))
@@ -94,7 +96,10 @@
[driver metadata-provider col-spec]
(or (some->> (:table col-spec)
(find-table-or-transform
- driver (lib.metadata/tables metadata-provider) (lib.metadata/transforms metadata-provider))
+ driver
+ (lib.metadata/database metadata-provider)
+ (lib.metadata/tables metadata-provider)
+ (lib.metadata/transforms metadata-provider))
:table
(lib.metadata/active-fields metadata-provider)
(map #(-> (assoc % :lib/desired-column-alias (:name %))
diff --git a/src/metabase/sql_tools/core.clj b/src/metabase/sql_tools/core.clj
index 64da3a1860f..d5454ce885e 100644
--- a/src/metabase/sql_tools/core.clj
+++ b/src/metabase/sql_tools/core.clj
@@ -158,12 +158,18 @@
(interface/replace-names-impl parser driver sql-string replacements opts)))))
(mu/defn referenced-tables-raw :- [:sequential ::table-spec]
- "Given a driver and sql string, returns a sequence of {:schema <name> :table <name>} maps."
- [driver :- :keyword
- sql-str :- [:maybe :string]]
- (let [parser (sql-tools.settings/current-parser-backend)]
- (metrics/with-operation-timing [parser "referenced-tables-raw"]
- (interface/referenced-tables-raw-impl parser driver sql-str))))
+ "Given a driver and SQL string, return table references. With `:fail-on-parse-error?`, propagate
+ SQLGlot parse errors instead of treating them as an empty set of references."
+ ([driver :- :keyword
+ sql-str :- [:maybe :string]]
+ (referenced-tables-raw driver sql-str {}))
+ ([driver :- :keyword
+ sql-str :- [:maybe :string]
+ opts :- [:map {:closed true}
+ [:fail-on-parse-error? {:optional true} :boolean]]]
+ (let [parser (sql-tools.settings/current-parser-backend)]
+ (metrics/with-operation-timing [parser "referenced-tables-raw"]
+ (interface/referenced-tables-raw-impl parser driver sql-str opts)))))
(mu/defn simple-query? :- ::simple-query-result
"Check if SQL string is a simple SELECT (no LIMIT, OFFSET, or CTEs).
@@ -202,8 +208,8 @@
(defn find-table-or-transform
"Given a table and schema parsed from a native query, find the matching table or transform.
Returns {:table table-id} or {:transform transform-id}, or nil."
- [driver tables transforms spec]
- (common/find-table-or-transform driver tables transforms spec))
+ [driver database tables transforms spec]
+ (common/find-table-or-transform driver database tables transforms spec))
(defn resolve-field
"Resolve a field reference to one or more actual database fields.
diff --git a/src/metabase/sql_tools/interface.clj b/src/metabase/sql_tools/interface.clj
index c4b17741113..673f813babd 100644
--- a/src/metabase/sql_tools/interface.clj
+++ b/src/metabase/sql_tools/interface.clj
@@ -46,7 +46,7 @@
(defmulti referenced-tables-raw-impl
"Parser specific implementation of [[metabase.sql-tools.core/referenced-tables-raw]]. Do not use directly."
- {:arglists '([parser driver sql-str])}
+ {:arglists '([parser driver sql-str opts])}
parser-driver-dispatch)
(defmulti simple-query?-impl
diff --git a/src/metabase/sql_tools/macaw/core.clj b/src/metabase/sql_tools/macaw/core.clj
index 85c784173d8..808c391b13d 100644
--- a/src/metabase/sql_tools/macaw/core.clj
+++ b/src/metabase/sql_tools/macaw/core.clj
@@ -108,7 +108,7 @@
db-tables (lib.metadata/bulk-metadata query :metadata/table table-ids)
db-transforms (lib.metadata/transforms query)]
(into #{}
- (keep #(sql-tools.common/find-table-or-transform driver db-tables db-transforms %))
+ (keep #(sql-tools.common/find-table-or-transform driver (lib.metadata/database query) db-tables db-transforms %))
specs)))
(defmethod sql-tools/referenced-tables-impl :macaw
@@ -141,7 +141,7 @@
(sql-tools.common/validate-query parser driver query))
(defmethod sql-tools/referenced-tables-raw-impl :macaw
- [_parser _driver sql-str]
+ [_parser _driver sql-str _opts]
(-> sql-str
(macaw/parsed-query)
(macaw/query->components {:strip-contexts? true})
diff --git a/src/metabase/sql_tools/sqlglot/core.clj b/src/metabase/sql_tools/sqlglot/core.clj
index 2ffe91bb9d1..00521757e0a 100644
--- a/src/metabase/sql_tools/sqlglot/core.clj
+++ b/src/metabase/sql_tools/sqlglot/core.clj
@@ -52,7 +52,7 @@
[driver query]
(try
(let [sql (lib/raw-native-query query)
- default-schema (sql.normalize/default-schema driver)
+ default-schema (sql.normalize/default-schema driver (lib.metadata/database query))
query-tables (sql-parsing/referenced-tables (driver->dialect driver) sql)
specs (map (fn [[_catalog table-schema table]]
(sql-tools.common/normalize-table-spec
@@ -63,7 +63,7 @@
db-tables (lib.metadata/bulk-metadata query :metadata/table table-ids)
db-transforms (lib.metadata/transforms query)]
(into #{}
- (keep #(sql-tools.common/find-table-or-transform driver db-tables db-transforms %))
+ (keep #(sql-tools.common/find-table-or-transform driver (lib.metadata/database query) db-tables db-transforms %))
specs))
(catch Exception e
;; Return empty sequence on parse error to follow the Macaw implementation behavior.
@@ -136,7 +136,7 @@
(sql-tools.common/returned-columns parser driver query))
(defmethod sql-tools/referenced-tables-raw-impl :sqlglot
- [_parser driver sql-str]
+ [_parser driver sql-str opts]
(try
(let [dialect (driver->dialect driver)
;; sql-parsing/referenced-tables returns [[catalog schema table] ...]
@@ -152,7 +152,7 @@
table-tuples))
(catch Exception e
;; Return empty sequence on parse error to follow the Macaw implementation behavior.
- (if (sql-parsing/parse-error? e)
+ (if (and (sql-parsing/parse-error? e) (not (:fail-on-parse-error? opts)))
[]
(throw e)))))
diff --git a/src/metabase/transforms/models/transform.clj b/src/metabase/transforms/models/transform.clj
index 04cc0e2d6ed..8bbc2946cfa 100644
--- a/src/metabase/transforms/models/transform.clj
+++ b/src/metabase/transforms/models/transform.clj
@@ -11,6 +11,7 @@
[metabase.models.interface :as mi]
[metabase.models.serialization :as serdes]
[metabase.permissions.core :as perms]
+ [metabase.premium-features.core :refer [defenterprise]]
[metabase.remote-sync.core :as remote-sync]
[metabase.search.core :as search.core]
[metabase.search.spec :as search.spec]
@@ -345,9 +346,20 @@
(events/publish-event! :event/update-transform {:object transform}))
transform)
+(defenterprise delete-transform-tests!
+ "Hook called from the `:model/Transform` before-delete: delete the tests of the transform `transform-id` through
+ Toucan, so that each one's own delete hook runs. The database would take them with the transform either way -- the
+ `transform_test.transform_id` FK is `ON DELETE CASCADE` -- but a cascade runs no hook, and remote sync learns a
+ test is gone only from the event that hook publishes, so it would go on serving a test whose transform no longer
+ exists. OSS is a no-op (no transform-testing module)."
+ metabase-enterprise.transform-testing.models
+ [_transform-id]
+ nil)
+
(t2/define-before-delete :model/Transform [transform]
(when-not mi/*deserializing?*
(events/publish-event! :event/delete-transform {:id (:id transform)}))
+ (delete-transform-tests! (:id transform))
(search.core/delete! :model/Transform [(str (:id transform))])
transform)
diff --git a/src/metabase/transforms_base/util.clj b/src/metabase/transforms_base/util.clj
index ed25394040f..0d612f46fc6 100644
--- a/src/metabase/transforms_base/util.clj
+++ b/src/metabase/transforms_base/util.clj
@@ -183,7 +183,7 @@
This handles the case where transforms create tables without explicit schema
but the driver needs a schema to find the table during sync."
[driver database table]
- (when-let [default-schema (try (sql.normalize/default-schema driver) (catch Exception _ nil))]
+ (when-let [default-schema (try (sql.normalize/default-schema driver database) (catch Exception _ nil))]
(when (driver/table-exists? driver database {:schema default-schema :name (:name table)})
default-schema)))
diff --git a/src/metabase/util/honey_sql_2.clj b/src/metabase/util/honey_sql_2.clj
index 031c2fe3f3a..12ae0a54e45 100644
--- a/src/metabase/util/honey_sql_2.clj
+++ b/src/metabase/util/honey_sql_2.clj
@@ -419,16 +419,51 @@
[:fn {:error/message "::h2x/typed Honey SQL form"} typed?]
[:sequential [:or [:ref ::honeysql-expr] ::honeysql-clause-opts]]])
-(def ^:private raw-cast-type-name-re
- #"(?i)[a-z][a-z0-9_ ]*(?:\(\d+(?:, ?\d+)?\))?")
+(def ^:private raw-type-name-regex
+ "The shape of a plain SQL type name, from the grammar the supported engines report their types in:
+
+ type ::= word ((' ')+ word | args)* `double precision`, `TIMESTAMP(6) WITH TIME ZONE`
+ args ::= '(' arg (',' arg)* ')' `varchar(10)`, `Decimal(38, 19)`, `Map(String, Int32)`
+ arg ::= type | number | quoted `Nullable(DateTime64(3, 'GMT0'))`
+ word ::= letter (letter | digit | '_')*
+ quoted ::= a single-quoted run holding no quote, backslash or line break
+ suffix ::= '[]'* `int[]`, `varchar(50)[]`
+
+ The pattern is built from the innermost type outwards, each turn letting a type take the previous one as an
+ argument; the innermost takes none, which bounds arguments at four deep. That covers what a warehouse reports
+ (`Array(Nullable(Tuple(String, Int32)))` is three); anything deeper is quoted as an identifier rather than
+ spliced.
+
+ A quoted argument is how a type carries a name it cannot spell as a token — the timezone of
+ `DateTime64(3, 'GMT0')`, which ClickHouse reports for an ordinary timestamp column and accepts back as a cast
+ target. Its content is a string to the engine, so nothing in it can end the expression the type sits in, so long
+ as the engine and this pattern agree on where it ends: a backslash is out because ClickHouse and MySQL read `\\'`
+ as a quote in the string rather than as its end, and the two readings would then part company over which
+ parentheses are structural."
+ (let [word "[A-Za-z][A-Za-z0-9_]*"
+ number "[0-9]+"
+ quoted "'[^'\\\\\\n]*'"]
+ (re-pattern
+ (str (reduce (fn [type _]
+ (let [arg (str "(?:" type "|" number "|" quoted ")")
+ args (str "\\(" arg "(?: *, *" arg ")*\\)")]
+ (str word "(?:(?: +" word ")|" args ")*")))
+ word
+ (range 4))
+ "(?:\\[\\])*"))))
(defn raw-type-name?
- "Whether `sql-type` is a plain SQL type name — letters, digits, underscores, and spaces with an optional precision
- suffix, e.g. `varchar(10)` or `double precision` — and is therefore safe to splice into SQL unquoted. Cast targets
- that don't match (e.g. a `database-type` coming from field metadata) must be quoted as identifiers or rejected
- instead of being emitted raw."
+ "Whether `sql-type` is a plain SQL type name — the shape [[raw-type-name-regex]] spells out — and is therefore safe
+ to splice into SQL unquoted.
+
+ Nothing a name of that shape holds can end the expression it is spliced into: it is words, arguments in balanced
+ parentheses, and an array suffix, so it carries no semicolon, dot or comment marker, no parenthesis that closes
+ one it did not open, and no comma or quote outside an argument list. Postgres, for one, emits `(…)::«type»` in a
+ select list, where `text, (SELECT secret FROM users)` would add a column. Cast targets that don't match (e.g. a
+ `database-type` coming from field metadata) must be quoted as identifiers or rejected instead of being emitted
+ raw."
[sql-type]
- (boolean (re-matches raw-cast-type-name-re (name sql-type))))
+ (boolean (re-matches raw-type-name-regex (name sql-type))))
(mu/defn cast :- TypedExpression
"Generate a statement like `cast(expr AS sql-type)`. Returns a typed HoneySQL form."
diff --git a/test/metabase/cmd/copy_test.clj b/test/metabase/cmd/copy_test.clj
index 229bab96ad4..198d40ffd52 100644
--- a/test/metabase/cmd/copy_test.clj
+++ b/test/metabase/cmd/copy_test.clj
@@ -119,6 +119,8 @@
:model/TableIndex
:model/TaskHistory
:model/TaskRun
+ ;; copied by table name instead, since its model is EE-only
+ :model/TransformTest
:model/Undo
:model/UserKeyValue})
diff --git a/test/metabase/driver_test.clj b/test/metabase/driver_test.clj
index 5b7974722a0..1a510e52843 100644
--- a/test/metabase/driver_test.clj
+++ b/test/metabase/driver_test.clj
@@ -479,6 +479,43 @@
(driver/register! ::mock-no-deps-driver, :abstract? true)
+(defn- select-all-on-connection [conn table max-rows]
+ (driver/query-on-connection driver/*driver* conn [(str "SELECT * FROM " table) []] {:max-rows max-rows}))
+
+(deftest transform-testing-temp-table-lifecycle-test
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/testing)
+ (let [table (driver/temp-table-name driver/*driver*)]
+ (driver/do-with-test-connection
+ driver/*driver*
+ (mt/db)
+ (fn [conn]
+ (driver/execute-on-connection! driver/*driver* conn
+ (driver/compile-create-temp-table
+ driver/*driver*
+ {:table table
+ :query {:query "SELECT id FROM (SELECT 1 AS id UNION ALL SELECT 2 AS id) AS t" :params []}}))
+ (testing "the temp table is readable on the connection that created it"
+ (is (= 2 (count (:rows (select-all-on-connection conn table 10))))))
+ (testing "max-rows caps the rows returned"
+ (is (= 1 (count (:rows (select-all-on-connection conn table 1))))))
+ (testing "the answer names its columns, each with the warehouse's own type"
+ ;; Engines differ on how they fold an unquoted identifier, so compare the name case-aside.
+ (let [columns (:columns (select-all-on-connection conn table 10))]
+ (is (= ["id"] (mapv (comp u/lower-case-en :name) columns)))
+ (is (every? (comp seq :database_type) columns) (pr-str columns))))
+ (testing "the columns are reported even when no row comes back"
+ (let [{:keys [rows columns]} (driver/query-on-connection
+ driver/*driver* conn
+ [(str "SELECT * FROM " table " WHERE 1 = 0") []] {:max-rows 1})]
+ (is (= [] rows))
+ (is (= ["id"] (mapv (comp u/lower-case-en :name) columns)))))
+ (driver/execute-on-connection! driver/*driver* conn (driver/compile-drop-temp-table driver/*driver* table))
+ (testing "dropping a temp table that no longer exists succeeds"
+ (is (some? (driver/execute-on-connection! driver/*driver* conn
+ (driver/compile-drop-temp-table driver/*driver* table)))))
+ (testing "the dropped temp table is gone"
+ (is (thrown? Exception (select-all-on-connection conn table 10)))))))))
+
(deftest ^:parallel deps-ignores-invalid-drivers-test
(is (= #{}
(driver/native-query-deps ::mock-no-deps-driver nil nil))))
diff --git a/test/metabase/sql_parsing/core_test.clj b/test/metabase/sql_parsing/core_test.clj
index eb0c18580cd..048dffc2f64 100644
--- a/test/metabase/sql_parsing/core_test.clj
+++ b/test/metabase/sql_parsing/core_test.clj
@@ -1,827 +1,180 @@
(ns metabase.sql-parsing.core-test
+ "Every function `metabase.sql-parsing.core` publishes, one test each, over the cases in `test_cases/`.
+
+ A case file is `{:cases [...]}` and each case carries its `:sql`, the `:dialect` to read it with, and what the
+ function is expected to answer."
(:require
+ [clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.test :refer :all]
- [metabase.analytics-interface.core :as analytics-interface]
[metabase.sql-parsing.core :as sql-parsing]
- [metabase.sql-parsing.graal :as graal]
- [metabase.test.util.dynamic-redefs :as dynamic-redefs]
- [metabase.util :as u])
- (:import
- (java.util.concurrent TimeoutException)))
+ [metabase.util :as u]))
(set! *warn-on-reflection* true)
-;;; ------------------------------------------ referenced-tables API Tests -----------------------------------------
-
-(deftest ^:parallel referenced-tables-basic-test
- (testing "Simple table extraction"
- (is (= [[nil nil "users"]]
- (sql-parsing/referenced-tables "postgres" "SELECT * FROM users"))))
- (testing "Multiple tables from JOIN"
- (is (= [[nil nil "orders"] [nil nil "users"]]
- (sql-parsing/referenced-tables
- "postgres"
- "SELECT * FROM users u LEFT JOIN orders o ON u.id = o.user_id"))))
- (testing "Schema-qualified tables are preserved"
- (is (= [[nil "other" "users"] [nil "public" "users"]]
- (sql-parsing/referenced-tables
- "postgres"
- "SELECT public.users.id, other.users.id
- FROM public.users u1
- LEFT JOIN other.users u2 ON u1.id = u2.id"))))
- (testing "CTE names are excluded"
- (is (= [[nil nil "users"]]
- (sql-parsing/referenced-tables
- "postgres"
- "WITH active AS (SELECT * FROM users WHERE active) SELECT * FROM active")))))
-
-(deftest ^:parallel cte-test
- (testing "CTE (WITH clause) parsing"
- (is (= [[nil nil "users"]]
- (sql-parsing/referenced-tables
- "postgres"
- "WITH active_users AS (SELECT * FROM users WHERE active)
- SELECT * FROM active_users")))))
-
-(deftest ^:parallel join-test
- (testing "JOIN parsing extracts all tables"
- (is (=? [[nil nil "orders"] [nil nil "users"]]
- (sql-parsing/referenced-tables
- "postgres"
- "SELECT u.name, o.total
- FROM users u
- JOIN orders o ON u.id = o.user_id")))))
-
-(deftest ^:parallel subquery-test
- (testing "Subquery table extraction"
- (is (=? [[nil nil "users"]]
- (sql-parsing/referenced-tables
- "postgres"
- "SELECT * FROM (SELECT id FROM users) AS sub")))))
-
-;;; ---------------------------------------- Catalog/Schema Extraction Tests ----------------------------------------
-;;; These tests verify the 3-tuple (tables) and 4-tuple (fields) API per the design doc:
-;;; - BigQuery: project.dataset.table → [project, dataset, table]
-;;; - Snowflake: database.schema.table → [database, schema, table]
-;;; - PostgreSQL: schema.table → [nil, schema, table]
-;;; - MySQL: database.table → [nil, database, table] (MySQL's "database" = schema level)
-
-(deftest ^:parallel catalog-schema-tables-bigquery-test
- (testing "BigQuery: project.dataset.table extracts all three levels"
- (is (= [["myproject" "analytics" "events"]]
- (sql-parsing/referenced-tables "bigquery" "SELECT * FROM myproject.analytics.events")))
- (is (= [["myproject" "analytics" "events"] ["myproject" "analytics" "users"]]
- (sql-parsing/referenced-tables "bigquery"
- "SELECT * FROM myproject.analytics.events e
- JOIN myproject.analytics.users u ON e.user_id = u.id")))))
-
-(deftest ^:parallel catalog-schema-tables-snowflake-test
- (testing "Snowflake: database.schema.table extracts all three levels"
- ;; Snowflake uppercases identifiers by default
- (let [result (sql-parsing/referenced-tables "snowflake" "SELECT * FROM mydb.public.orders")
- normalized (mapv (fn [[c s t]] [(some-> c u/lower-case-en)
- (some-> s u/lower-case-en)
- (u/lower-case-en t)])
- result)]
- (is (= [["mydb" "public" "orders"]] normalized)))))
-
-(deftest ^:parallel catalog-schema-tables-postgres-test
- (testing "PostgreSQL: schema.table (no cross-database queries)"
- (is (= [[nil "public" "users"]]
- (sql-parsing/referenced-tables "postgres" "SELECT * FROM public.users")))
- (is (= [[nil "other_schema" "accounts"] [nil "public" "users"]]
- (sql-parsing/referenced-tables "postgres"
- "SELECT * FROM public.users u
- JOIN other_schema.accounts a ON u.id = a.user_id")))))
-
-(deftest ^:parallel catalog-schema-tables-mysql-test
- (testing "MySQL: database.table (MySQL 'database' maps to schema slot)"
- ;; In MySQL, CREATE SCHEMA = CREATE DATABASE, so there's no separate schema level
- ;; SQLGlot maps MySQL's database to the schema slot (slot 2)
- (is (= [[nil "mydb" "users"]]
- (sql-parsing/referenced-tables "mysql" "SELECT * FROM mydb.users")))
- (is (= [[nil nil "users"]]
- (sql-parsing/referenced-tables "mysql" "SELECT * FROM users")))))
-
-(deftest ^:parallel catalog-schema-fields-bigquery-test
- (testing "BigQuery: fully-qualified field references"
- (is (= [["myproject" "analytics" "users" "email"]]
- (sql-parsing/referenced-fields "bigquery"
- "SELECT myproject.analytics.users.email
- FROM myproject.analytics.users")))))
-
-(deftest ^:parallel catalog-schema-fields-postgres-test
- (testing "PostgreSQL: schema-qualified field references"
- (is (= [[nil "public" "orders" "total"]]
- (sql-parsing/referenced-fields "postgres" "SELECT public.orders.total FROM public.orders")))))
-
-(deftest ^:parallel catalog-schema-fields-mysql-test
- (testing "MySQL: database-qualified field references"
- (is (= [[nil "mydb" "users" "id"]]
- (sql-parsing/referenced-fields "mysql" "SELECT mydb.users.id FROM mydb.users")))
- (is (= [[nil nil "users" "id"]]
- (sql-parsing/referenced-fields "mysql" "SELECT id FROM users")))))
-
-;;; -------------------------------------------- UDTF (Table Function) Tests -----------------------------------------
-
-;; Dialect-specific UDTF queries - each dialect may have slightly different syntax
-;; for table-valued functions. The queries should be semantically equivalent.
-;; We use a generic function name for dialects without well-known UDTFs to test
-;; that unknown functions are handled gracefully.
-(def udtf-queries
- {:postgres "SELECT * FROM generate_series(1, 10)"
- :mysql "SELECT * FROM my_table_func(1, 10)" ; MySQL's JSON_TABLE has complex syntax
- :snowflake "SELECT * FROM TABLE(FLATTEN(input => ARRAY_CONSTRUCT(1, 2, 3)))"
- :bigquery "SELECT * FROM UNNEST([1, 2, 3]) AS val"
- :redshift "SELECT * FROM generate_series(1, 10)"
- :duckdb "SELECT * FROM generate_series(1, 10)"})
-
-;; UDTF queries mixed with real tables - tests that we correctly identify real tables
-;; while gracefully handling table functions
-(def udtf-with-table-queries
- {:postgres "SELECT o.* FROM orders o, generate_series(1, 10) g"
- :mysql "SELECT o.* FROM orders o, my_table_func(1, 10) t"
- :snowflake "SELECT o.* FROM orders o, TABLE(FLATTEN(input => ARRAY_CONSTRUCT(1))) f"
- :bigquery "SELECT o.* FROM orders o, UNNEST([1]) AS val"
- :redshift "SELECT o.* FROM orders o, generate_series(1, 10) g"
- :duckdb "SELECT o.* FROM orders o, generate_series(1, 10) g"})
-
-(deftest ^:parallel udtf-referenced-tables-test
- (testing "UDTFs are handled by referenced-tables across dialects"
- (doseq [[dialect sql] udtf-queries]
- (testing (str "dialect: " dialect " - pure UDTF query")
- ;; Table functions shouldn't appear as referenced tables - they're not real tables
- (let [result (sql-parsing/referenced-tables (name dialect) sql)]
- (is (vector? result)
- (format "dialect %s: should return vector, got %s" dialect (type result)))
- (is (every? vector? result)
- (format "dialect %s: each element should be [catalog schema table] tuple" dialect)))))
- (doseq [[dialect sql] udtf-with-table-queries]
- (testing (str "dialect: " dialect " - UDTF mixed with real table")
- (let [result (sql-parsing/referenced-tables (name dialect) sql)]
- ;; Case-insensitive comparison since dialects like Snowflake uppercase identifiers
- ;; 3-tuple: [catalog schema table] - table is third element
- (is (some #(= "orders" (u/lower-case-en (nth % 2))) result)
- (format "dialect %s: should find 'orders' table in %s" dialect (pr-str result))))))))
-
-;;; ------------------------------------------ Set Operation Tests (UNION, INTERSECT, EXCEPT) ------------------------------------------
-
-;; Set operation queries by dialect - tests UNION, UNION ALL, INTERSECT, EXCEPT
-;; All queries use tables "a" and "b" for consistent expected results
-(def dialect->set-operation->query
- {:postgres
- {:union "SELECT id, name FROM a UNION SELECT id, name FROM b"
- :union-all "SELECT id, name FROM a UNION ALL SELECT id, name FROM b"
- :intersect "SELECT id FROM a INTERSECT SELECT id FROM b"
- :except "SELECT id FROM a EXCEPT SELECT id FROM b"
- :nested-union "SELECT * FROM (SELECT id FROM a UNION SELECT id FROM b) AS combined"
- :cte-with-union "WITH c AS (SELECT id FROM a UNION SELECT id FROM b) SELECT * FROM c"}
-
- :mysql
- {:union "SELECT id, name FROM a UNION SELECT id, name FROM b"
- :union-all "SELECT id, name FROM a UNION ALL SELECT id, name FROM b"
- :intersect "SELECT id FROM a INTERSECT SELECT id FROM b"
- :except "SELECT id FROM a EXCEPT SELECT id FROM b"
- :nested-union "SELECT * FROM (SELECT id FROM a UNION SELECT id FROM b) AS combined"
- :cte-with-union "WITH c AS (SELECT id FROM a UNION SELECT id FROM b) SELECT * FROM c"}
-
- :snowflake
- {:union "SELECT id, name FROM a UNION SELECT id, name FROM b"
- :union-all "SELECT id, name FROM a UNION ALL SELECT id, name FROM b"
- :intersect "SELECT id FROM a INTERSECT SELECT id FROM b"
- :except "SELECT id FROM a EXCEPT SELECT id FROM b"
- :nested-union "SELECT * FROM (SELECT id FROM a UNION SELECT id FROM b) AS combined"
- :cte-with-union "WITH c AS (SELECT id FROM a UNION SELECT id FROM b) SELECT * FROM c"}
-
- :bigquery
- {:union "SELECT id, name FROM a UNION DISTINCT SELECT id, name FROM b"
- :union-all "SELECT id, name FROM a UNION ALL SELECT id, name FROM b"
- :intersect "SELECT id FROM a INTERSECT DISTINCT SELECT id FROM b"
- :except "SELECT id FROM a EXCEPT DISTINCT SELECT id FROM b"
- :nested-union "SELECT * FROM (SELECT id FROM a UNION DISTINCT SELECT id FROM b) AS combined"
- :cte-with-union "WITH c AS (SELECT id FROM a UNION DISTINCT SELECT id FROM b) SELECT * FROM c"}
-
- :redshift
- {:union "SELECT id, name FROM a UNION SELECT id, name FROM b"
- :union-all "SELECT id, name FROM a UNION ALL SELECT id, name FROM b"
- :intersect "SELECT id FROM a INTERSECT SELECT id FROM b"
- :except "SELECT id FROM a EXCEPT SELECT id FROM b"
- :nested-union "SELECT * FROM (SELECT id FROM a UNION SELECT id FROM b) AS combined"
- :cte-with-union "WITH c AS (SELECT id FROM a UNION SELECT id FROM b) SELECT * FROM c"}
-
- :duckdb
- {:union "SELECT id, name FROM a UNION SELECT id, name FROM b"
- :union-all "SELECT id, name FROM a UNION ALL SELECT id, name FROM b"
- :intersect "SELECT id FROM a INTERSECT SELECT id FROM b"
- :except "SELECT id FROM a EXCEPT SELECT id FROM b"
- :nested-union "SELECT * FROM (SELECT id FROM a UNION SELECT id FROM b) AS combined"
- :cte-with-union "WITH c AS (SELECT id FROM a UNION SELECT id FROM b) SELECT * FROM c"}})
-
-(deftest ^:parallel set-operation-referenced-tables-test
- (testing "Set operations correctly identify all referenced tables across dialects"
- (doseq [[dialect ops] dialect->set-operation->query
- [op-type sql] ops]
- (testing (str "dialect: " (name dialect) " - " (name op-type))
- (let [result (sql-parsing/referenced-tables (name dialect) sql)
- ;; Normalize to lowercase for case-insensitive comparison (Snowflake uppercases)
- ;; 3-tuple: [catalog schema table]
- normalized (into #{} (map (fn [[catalog schema table]]
- [(some-> catalog u/lower-case-en)
- (some-> schema u/lower-case-en)
- (u/lower-case-en table)])
- result))]
- (is (= #{[nil nil "a"] [nil nil "b"]} normalized)
- (format "%s/%s should return [[nil nil a] [nil nil b]], got %s"
- (name dialect) (name op-type) normalized)))))))
-
-(def ^:private set-operation->returned-columns
- {:union ["id" "name"]
- :union-all ["id" "name"]
- :intersect ["id"]
- :except ["id"]
- :nested-union ["id"]
- :cte-with-union ["id"]})
-
-(deftest ^:parallel set-operation-validate-query-test
- (testing "Set operations validate correctly across dialects"
- (doseq [[dialect ops] dialect->set-operation->query
- [op-type sql] ops]
- (testing (str "dialect: " (name dialect) " - " (name op-type))
- (let [result (sql-parsing/validate-query (name dialect) sql "public" {})]
- (is (= "ok" (:status result))
- (format "%s/%s should validate OK, got %s"
- (name dialect) (name op-type) result)))))))
-
-(deftest ^:parallel set-operation-returned-columns-lineage-test
- (testing "Set operations return correct column lineage across dialects"
- (doseq [[dialect ops] dialect->set-operation->query
- [op-type sql] ops
- :let [expected (get set-operation->returned-columns op-type)]]
- (testing (str "dialect: " (name dialect) ", operation: " (name op-type))
- (let [result (sql-parsing/returned-columns-lineage (name dialect) sql "public" {})
- columns (sort (map (comp u/lower-case-en first) result))]
- (is (= expected columns)
- (format "%s %s should return %s, got %s"
- (name dialect) (name op-type) (pr-str expected) columns)))))))
-
-;;; -------------------------------------------- Schema Validation Tests (validate-query) ------------------------------------------------
-
-;; validate-query validates SQL against a provided schema, detecting:
-;; - Missing columns (column doesn't exist in any table)
-;; - Missing table aliases (table wildcard references non-existent source)
-;; - Unknown tables (table not in schema)
-
-(def ^:private test-schema
- "Test schema for validation tests.
- Structure: {schema-name {table-name {column-name type}}}"
- {"PUBLIC" {"PRODUCTS" {"ID" "INT"
- "TITLE" "VARCHAR"
- "CATEGORY" "VARCHAR"
- "PRICE" "DECIMAL"}
- "ORDERS" {"ID" "INT"
- "USER_ID" "INT"
- "PRODUCT_ID" "INT"
- "TOTAL" "DECIMAL"}
- "USERS" {"ID" "INT"
- "NAME" "VARCHAR"
- "EMAIL" "VARCHAR"}}})
-
-(deftest ^:parallel validate-query-valid-queries-test
- (testing "Valid queries against schema"
- (testing "simple select with existing columns"
- (is (= "ok" (:status (sql-parsing/validate-query nil "SELECT id, title FROM products" "PUBLIC" test-schema)))))
- (testing "wildcard select"
- (is (= "ok" (:status (sql-parsing/validate-query nil "SELECT * FROM products" "PUBLIC" test-schema)))))
- (testing "table-qualified columns"
- (is (= "ok" (:status (sql-parsing/validate-query nil "SELECT products.id, products.title FROM products" "PUBLIC" test-schema)))))
- (testing "join with valid columns"
- (is (= "ok" (:status (sql-parsing/validate-query nil
- "SELECT o.id, p.title FROM orders o JOIN products p ON o.product_id = p.id"
- "PUBLIC" test-schema)))))
- (testing "subquery"
- (is (= "ok" (:status (sql-parsing/validate-query nil
- "SELECT * FROM (SELECT id, title FROM products) AS sub"
- "PUBLIC" test-schema)))))))
-
-(deftest ^:parallel validate-query-missing-column-test
- (testing "Missing column errors"
- (testing "non-existent column"
- (let [result (sql-parsing/validate-query nil "SELECT bad_column FROM products" "PUBLIC" test-schema)]
- (is (= "error" (:status result)))
- (is (= "column_not_resolved" (:type result)))
- (is (re-find #"(?i)bad_column" (:column result)))))
- (testing "column from wrong table"
- (let [result (sql-parsing/validate-query nil "SELECT email FROM products" "PUBLIC" test-schema)]
- (is (= "error" (:status result)))
- (is (= "column_not_resolved" (:type result)))))))
-
-(deftest ^:parallel validate-query-missing-table-alias-test
- (testing "Missing table alias errors"
- (testing "table wildcard with non-existent table"
- (let [result (sql-parsing/validate-query nil "SELECT products.* FROM orders" "PUBLIC" test-schema)]
- (is (= "error" (:status result)))
- ;; SQLGlot reports this as unknown_table
- (is (contains? #{"unknown_table" "column_not_resolved"} (:type result)))))
- (testing "qualified column with non-existent alias"
- (let [result (sql-parsing/validate-query nil "SELECT p.id FROM products" "PUBLIC" test-schema)]
- (is (= "error" (:status result)))))))
-
-(deftest ^:parallel validate-query-unknown-table-test
- ;; SQLGlot's qualify.qualify() does NOT detect unknown tables - it just doesn't qualify them.
- ;; Unknown table detection happens at the sql-tools layer by matching referenced tables
- ;; against Metabase's database metadata.
- (testing "Unknown tables are not detected by SQLGlot (known limitation)"
- (let [result (sql-parsing/validate-query nil "SELECT * FROM nonexistent" "PUBLIC" test-schema)]
- (is (= "ok" (:status result))
- "SQLGlot passes unknown tables through without error"))))
-
-(deftest ^:parallel validate-query-syntax-error-test
- (testing "Syntax errors are caught"
- (let [result (sql-parsing/validate-query nil "SELECT * FORM products" "PUBLIC" test-schema)]
- (is (= "error" (:status result)))
- (is (= "invalid_expression" (:type result))))))
-
-(deftest ^:parallel lenient-parsing-incomplete-limit-test
- (testing "SQLGlot's lenient parsing treats 'SELECT 1 LIMIT' as 'SELECT 1 AS LIMIT'"
- ;; This documents a known SQLGlot behavior where incomplete LIMIT clauses
- ;; are parsed as column aliases.
- (let [result (sql-parsing/referenced-tables "postgres" "SELECT 1 LIMIT")]
- (is (= [] result)
- "No tables are referenced in 'SELECT 1 LIMIT'")))
- (testing "To reliably trigger SQL errors, use nonexistent tables instead"
- ;; This is the recommended pattern for tests that need to trigger SQL failures
- (let [result (sql-parsing/referenced-tables "postgres" "SELECT * FROM nonexistent_table_xyz")]
- (is (= [[nil nil "nonexistent_table_xyz"]] result)
- "Nonexistent table reference is parsed and will fail at execution time"))))
-
-(comment
- (def query-corpus-path "/Users/bcm/dv/mb/query_corpus/")
-
- (def sentinel (re-pattern "\n-----end-query-----\n"))
-
- (def drivers ["mysql"])
-
- (let [driver (first drivers)
- corpus (slurp (str query-corpus-path driver ".log"))
- queries (sort (distinct (str/split corpus sentinel)))]
- (frequencies
- (doall
- (for [q queries]
- (try
- (sql-parsing/referenced-tables q driver)
- true
- (catch Exception _ false))))))
-
- ;; Load and run validation tests interactively
- (load-validation-test-cases)
-
- ;; Test a single case
- (validate-test-case {:name "test"
- :sql "SELECT * FROM users"
- :dialect "postgres"
- :expected :valid})
-
- ;; Run all valid query tests
- (doseq [tc (:valid-queries (load-validation-test-cases))]
- (let [result (validate-test-case tc)]
- (when-not (:passed result)
- result)))
-
- ;; Run all invalid query tests
- (doseq [tc (:invalid-queries (load-validation-test-cases))]
- (let [result (validate-test-case tc)]
- (when-not (:passed result)
- result))))
-
-;;; -------------------------------------------- Referenced Fields Tests --------------------------------------------
-
-(defn- load-referenced-fields-test-cases
- "Load referenced fields test cases from EDN file."
- []
- (let [resource (io/resource "metabase/sql_parsing/referenced_fields_test_cases.edn")]
- (when-not resource
- (throw (ex-info "Could not find referenced_fields_test_cases.edn" {})))
- (read-string (slurp resource))))
+(defn- cases
+ "The cases in `test_cases/<file-name>.edn`, the file holding the cases for one function under test."
+ [file-name]
+ (let [path (str "metabase/sql_parsing/test_cases/" file-name ".edn")
+ edn (edn/read-string (slurp (or (io/resource path)
+ (throw (ex-info (str "No case file at " path) {:path path})))))]
+ (:cases edn)))
(defn- normalize-fields
- "Normalize field references for comparison (sort and ensure consistent format).
- Handles both 2-tuples (expected format in test cases) and 4-tuples (actual API format)."
- [fields]
- (vec (sort-by (fn [f]
- (if (= 4 (count f))
- ;; 4-tuple: [catalog schema table field]
- [(u/lower-case-en (nth f 2)) (u/lower-case-en (nth f 3))]
- ;; 2-tuple: [table field] (test case format)
- [(u/lower-case-en (first f)) (u/lower-case-en (second f))]))
- (map (fn [f]
- (if (= 4 (count f))
- ;; Convert 4-tuple to 2-tuple for comparison
- [(u/lower-case-en (nth f 2)) (u/lower-case-en (nth f 3))]
- ;; Already 2-tuple
- [(u/lower-case-en (first f)) (u/lower-case-en (second f))]))
- fields))))
-
-(defn- fields-match?
- "Check if actual fields match expected fields (case-insensitive, order-independent)."
- [expected actual]
- (= (normalize-fields expected)
- (normalize-fields actual)))
+ "Field references as `[table field]` or `[catalog schema table field]`, lower-cased and sorted, so one expectation
+ covers the dialects that fold identifiers. Compared at whatever arity the case was written in."
+ [arity fields]
+ (->> fields
+ (map (fn [f]
+ (let [f (mapv #(some-> % u/lower-case-en) f)]
+ (if (= 2 arity) [(nth f (- (count f) 2)) (last f)] f))))
+ sort
+ vec))
+
+;;; --------------------------------------------- parse-error-message ----------------------------------------------
+
+(defn- parse-failure
+ "The exception `sql` fails to parse with."
+ [dialect sql]
+ (try
+ (sql-parsing/referenced-tables dialect sql)
+ (is false (str "expected " (pr-str sql) " not to parse"))
+ (catch Exception e e)))
+
+(deftest ^:parallel parse-error-message-test
+ (testing "what a parse failure tells whoever wrote the SQL, once the parser's own workings are out of it"
+ (doseq [{:keys [name sql dialect internals diagnostic]} (cases "parse_error_message")]
+ (testing name
+ (let [e (parse-failure dialect sql)]
+ (is (sql-parsing/parse-error? e))
+ (testing "the parser still reports the internals the diagnostic is cleaned of"
+ (is (str/includes? (or (some-> (ex-cause e) ex-message) (ex-message e)) internals)))
+ (testing "and the caller reads the error, its place, and a caret under the token"
+ (is (= diagnostic (sql-parsing/parse-error-message e)))))))))
+
+(deftest ^:parallel parse-error-message-without-a-message-test
+ (testing "an exception carrying no message of its own has no diagnostic to give"
+ (is (nil? (sql-parsing/parse-error-message (ex-info nil {}))))))
+
+;;; ------------------------------------------------ referenced-tables ---------------------------------------------
+
+(defn- normalize-tables
+ "Table references lower-cased and sorted, so one expectation covers a dialect that folds identifiers up (Snowflake)
+ and one that folds them down (Postgres)."
+ [tables]
+ (sort (mapv (fn [[catalog schema table]]
+ [(some-> catalog u/lower-case-en) (some-> schema u/lower-case-en) (u/lower-case-en table)])
+ tables)))
+
+(deftest ^:parallel referenced-tables-test
+ (testing "the tables a query reads, which is what Guard A of a transform test asks for"
+ (doseq [{:keys [name sql dialect tables]} (cases "referenced_tables")]
+ (testing name
+ (is (= (normalize-tables tables)
+ (normalize-tables (sql-parsing/referenced-tables dialect sql)))
+ (pr-str sql))))))
+
+;;; ------------------------------------------------- replace-names ------------------------------------------------
+
+(deftest ^:parallel replace-names-test
+ (testing "the query rewritten to read other tables, which is how a transform test reads its fixtures"
+ (doseq [{:keys [name sql dialect replacements expected]} (cases "replace_names")]
+ (testing name
+ (is (= expected (sql-parsing/replace-names dialect sql replacements))
+ (pr-str sql))))))
+
+;;; ------------------------------------------------ field-references ----------------------------------------------
+
+(deftest ^:parallel field-references-test
+ (testing "the fields a query reads and returns, and the qualifiers that resolve to nothing"
+ (doseq [{:keys [name sql dialect errors returned]} (cases "field_references")]
+ (testing name
+ (let [result (sql-parsing/field-references dialect sql)]
+ (is (= (set errors) (set (:errors result))) (pr-str sql))
+ (is (= (sort returned)
+ (sort (keep :column (:returned-fields result))))
+ (pr-str sql)))))))
+
+;;; --------------------------------------------- is-single-stmt-of-type? ------------------------------------------
+
+(deftest ^:parallel is-single-stmt-of-type?-test
+ (testing "whether a query is one statement of the kind asked for, which is what tells a caller the rest is unread"
+ (doseq [{:keys [name sql dialect stmt-type expected]} (cases "single_stmt")]
+ (testing name
+ (is (=? expected (sql-parsing/is-single-stmt-of-type? dialect sql stmt-type))
+ (pr-str sql))))))
+
+;;; ------------------------------------------- returned-columns-lineage -------------------------------------------
+
+(deftest ^:parallel returned-columns-lineage-test
+ (testing "the columns a query returns, which is what an expectation compares against"
+ (doseq [{:keys [name sql dialect default-schema columns]} (cases "returned_columns")]
+ (testing name
+ (is (= columns
+ (sort (map (comp u/lower-case-en first)
+ (sql-parsing/returned-columns-lineage dialect sql default-schema {}))))
+ (pr-str sql))))))
+
+;;; ------------------------------------------------- validate-query -----------------------------------------------
+
+(deftest ^:parallel validate-query-test
+ (testing "a query checked against a schema, column by column"
+ (let [schemas (:schemas (edn/read-string (slurp (io/resource "metabase/sql_parsing/test_cases/validate_query.edn"))))]
+ (doseq [{:keys [name sql dialect default-schema schema expected]} (cases "validate_query")]
+ (testing name
+ (is (=? expected (sql-parsing/validate-query dialect sql default-schema (get schemas schema)))
+ (pr-str sql)))))))
+
+;;; ------------------------------------------------ referenced-fields ---------------------------------------------
(deftest ^:parallel referenced-fields-test
- (testing "Referenced fields extraction from SQL queries"
- (let [test-cases (:test-cases (load-referenced-fields-test-cases))]
- (doseq [test-case test-cases]
- (testing (:name test-case)
- (let [{:keys [sql dialect expected]} test-case
- actual (sql-parsing/referenced-fields dialect sql)]
- (is (fields-match? expected actual)
- (str "Test '" (:name test-case) "' failed"
- "\n SQL: " sql
- "\n Expected: " (pr-str (normalize-fields expected))
- "\n Actual: " (pr-str (normalize-fields actual))))))))))
-
-;;; -------------------------------------- Large literal-list stripping tests --------------------------------------
-
-(deftest ^:parallel strip-large-values-test
- (testing "Small VALUES clauses are preserved"
- (let [sql "SELECT * FROM (VALUES (1, 'a'), (2, 'b'), (3, 'c')) AS t(id, name)"]
- (is (= sql (sql-parsing/strip-large-literal-lists sql)))))
- (testing "No VALUES keyword returns SQL unchanged"
- (let [sql "SELECT * FROM users WHERE id = 1"]
- (is (= sql (sql-parsing/strip-large-literal-lists sql)))))
- (testing "Large VALUES clause is replaced with NULLs preserving column count"
- (let [tuples (str/join ", " (map #(format "(%d, '%s', %d)" % (str "name" %) (* % 10))
- (range 200)))
- sql (str "SELECT * FROM (VALUES " tuples ") AS t(id, name, score)")
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (str/includes? result "VALUES (NULL, NULL, NULL)"))
- (is (str/includes? result "AS t(id, name, score)"))
- (is (not (str/includes? result "name0")))))
- (testing "Multiple large VALUES clauses are all stripped"
- (let [tuples1 (str/join ", " (map #(format "(%d)" %) (range 200)))
- tuples2 (str/join ", " (map #(format "(%d, %d)" % (* % 2)) (range 200)))
- sql (str "WITH a AS (SELECT * FROM (VALUES " tuples1 ") AS t(x)), "
- "b AS (SELECT * FROM (VALUES " tuples2 ") AS t(y, z)) "
- "SELECT * FROM a JOIN b ON a.x = b.y")
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (str/includes? result "VALUES (NULL)"))
- (is (str/includes? result "VALUES (NULL, NULL)"))))
- (testing "VALUES keyword casing is preserved"
- (let [tuples (str/join ", " (map #(format "(%d)" %) (range 200)))
- sql (str "select * from (values " tuples ") as t(x)")
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (str/includes? result "values (NULL)"))))
- (testing "VALUES inside a string literal is not stripped"
- (let [sql "SELECT 'INSERT INTO foo VALUES (1,2,3)' AS example FROM bar"]
- (is (= sql (sql-parsing/strip-large-literal-lists sql)))))
- (testing "Column named values is not stripped"
- (let [sql "SELECT values FROM my_table WHERE values > 10"]
- (is (= sql (sql-parsing/strip-large-literal-lists sql)))))
- (testing "VALUES keyword not followed by paren is not stripped"
- (let [sql "SELECT * FROM t WHERE col IN (SELECT values FROM other)"]
- (is (= sql (sql-parsing/strip-large-literal-lists sql)))))
- (testing "INSERT INTO ... VALUES is stripped when large"
- (let [tuples (str/join ", " (map #(format "(%d, 'x')" %) (range 200)))
- sql (str "INSERT INTO foo VALUES " tuples)
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (str/includes? result "VALUES (NULL, NULL)"))
- (is (not (str/includes? result "(0, 'x')"))))))
-
-(deftest ^:parallel strip-large-in-lists-test
- (testing "Small IN lists are preserved"
- (let [sql "SELECT * FROM t WHERE id IN (1, 2, 3)"]
- (is (= sql (sql-parsing/strip-large-literal-lists sql)))))
- (testing "Large literal IN list is replaced with IN (NULL)"
- (let [sql (str "SELECT * FROM t WHERE id IN (" (str/join ", " (range 200)) ") AND active = true")
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (= "SELECT * FROM t WHERE id IN (NULL) AND active = true" result))))
- (testing "Large IN list of strings, negatives, and decimals is stripped"
- (let [items (concat (map #(format "'name %d'" %) (range 100))
- (map #(str "-" %) (range 50))
- ["1.5" "+2"])
- sql (str "SELECT * FROM t WHERE name IN (" (str/join ", " items) ")")
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (= "SELECT * FROM t WHERE name IN (NULL)" result))))
- (testing "IN keyword casing is preserved"
- (let [sql (str "SELECT * FROM t WHERE id in (" (str/join ", " (range 200)) ")")
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (str/includes? result "in (NULL)"))))
- (testing "NOT IN with a large literal list is stripped"
- (let [sql (str "SELECT * FROM t WHERE id NOT IN (" (str/join ", " (range 200)) ")")
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (= "SELECT * FROM t WHERE id NOT IN (NULL)" result))))
- (testing "Multiple large IN lists are all stripped"
- (let [in1 (str/join ", " (range 200))
- in2 (str/join ", " (map #(format "'x%d'" %) (range 200)))
- sql (str "SELECT * FROM t WHERE a IN (" in1 ") OR b IN (" in2 ")")
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (= "SELECT * FROM t WHERE a IN (NULL) OR b IN (NULL)" result))))
- (testing "IN with a subquery is not stripped"
- (let [sql "SELECT * FROM t WHERE id IN (SELECT id FROM other)"]
- (is (= sql (sql-parsing/strip-large-literal-lists sql)))))
- (testing "A large IN list nested inside an unstripped IN subquery is still stripped"
- (let [sql (str "SELECT * FROM t WHERE id IN (SELECT id FROM other WHERE x IN ("
- (str/join ", " (range 200)) "))")
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (= "SELECT * FROM t WHERE id IN (SELECT id FROM other WHERE x IN (NULL))" result))))
- (testing "Large IN lists containing non-literals are not stripped"
- (doseq [extra-item ["col_ref" "foo(1)" "?" "$1" "{{param}}" "1e5" "DATE '2024-01-01'" "NULL"]]
- (let [sql (str "SELECT * FROM t WHERE id IN (" (str/join ", " (concat (range 200) [extra-item])) ")")]
- (is (= sql (sql-parsing/strip-large-literal-lists sql))
- (str "list containing " (pr-str extra-item) " should not be stripped")))))
- (testing "Word boundaries: JOIN ( and INT( do not trigger IN matching"
- (let [sql "SELECT CAST(x AS INT) FROM a JOIN (SELECT * FROM b) c ON a.id = c.id"]
- (is (= sql (sql-parsing/strip-large-literal-lists sql)))))
- (testing "Large VALUES and IN in the same statement are both stripped"
- (let [tuples (str/join ", " (map #(format "(%d)" %) (range 200)))
- sql (str "WITH v AS (SELECT * FROM (VALUES " tuples ") AS t(x)) "
- "SELECT * FROM v WHERE x IN (" (str/join ", " (range 200)) ")")
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (= "WITH v AS (SELECT * FROM (VALUES (NULL)) AS t(x)) SELECT * FROM v WHERE x IN (NULL)" result)))))
-
-(deftest ^:parallel large-in-list-referenced-tables-test
- (testing "referenced-tables and referenced-fields work on queries with massive IN lists"
+ (testing "the fields a query reads, as the catalog/schema/table/field tuples a caller matches against metadata"
+ (doseq [{:keys [name sql dialect expected]} (cases "referenced_fields")]
+ (testing name
+ (let [arity (count (first expected))]
+ (is (= (normalize-fields arity expected)
+ (normalize-fields (or arity 4) (sql-parsing/referenced-fields dialect sql)))
+ (pr-str sql)))))))
+
+;;; ------------------------------------------- strip-large-literal-lists ------------------------------------------
+
+(defn- built
+ "The case's SQL: written out, or built from `:build` when the point of the case is a list too long to write."
+ [{:keys [sql build]}]
+ (or sql
+ (let [{:keys [prefix item count extra separator suffix]} build
+ items (cond-> (mapv #(format item % % %) (range count))
+ extra (conj extra))]
+ (str prefix (str/join (or separator ", ") items) suffix))))
+
+(deftest ^:parallel strip-large-literal-lists-test
+ (testing "the literal lists too big for the parser to hold, replaced by a single NULL before it sees them"
+ (doseq [{:keys [name expected] :as case} (cases "literal_stripping")]
+ (testing name
+ (let [sql (built case)
+ result (sql-parsing/strip-large-literal-lists sql)]
+ (cond
+ (= :unchanged expected)
+ (is (identical? sql result) (pr-str (subs sql 0 (min 60 (clojure.core/count sql)))))
+
+ (string? expected)
+ (is (= expected result) (pr-str (subs sql 0 (min 60 (clojure.core/count sql)))))
+
+ :else
+ (do (doseq [text (:includes expected)]
+ (is (str/includes? result text) (pr-str result)))
+ (doseq [text (:excludes expected)]
+ (is (not (str/includes? result text)) (pr-str result)))))
+ (testing "stripping what was stripped changes nothing more"
+ (is (= result (sql-parsing/strip-large-literal-lists result)))))))))
+
+(deftest ^:parallel stripping-is-invisible-to-the-caller-test
+ (testing "a query whose lists were stripped still reports the tables and fields it reads"
(let [sql (str "SELECT a.name FROM accounts a WHERE a.id IN (" (str/join ", " (range 20000)) ")")]
(is (= [[nil nil "accounts"]] (sql-parsing/referenced-tables "postgres" sql)))
(is (= [[nil nil "accounts" "id"] [nil nil "accounts" "name"]]
- (sort (sql-parsing/referenced-fields "postgres" sql)))))))
-
-(defn- in-list
- "Build `IN (0, 1, ..., n-1)` with `n` items."
- [n]
- (str "IN (" (str/join ", " (range n)) ")"))
-
-(deftest ^:parallel strip-threshold-boundary-test
- (testing "IN lists at or below the 100-item threshold are preserved, above it stripped"
- (doseq [n [1 2 50 99 100]]
- (let [sql (str "SELECT * FROM t WHERE id " (in-list n))]
- (is (= sql (sql-parsing/strip-large-literal-lists sql))
- (str n " items should not be stripped"))))
- (doseq [n [101 102 150 1000 10000]]
- (let [sql (str "SELECT * FROM t WHERE id " (in-list n))]
- (is (= "SELECT * FROM t WHERE id IN (NULL)" (sql-parsing/strip-large-literal-lists sql))
- (str n " items should be stripped")))))
- (testing "VALUES at or below the 100-tuple threshold are preserved, above it stripped"
- (doseq [[n stripped?] {1 false, 100 false, 101 true, 1000 true}]
- (let [tuples (str/join ", " (map #(format "(%d, %d)" % %) (range n)))
- sql (str "SELECT * FROM (VALUES " tuples ") AS t(x, y)")
- result (sql-parsing/strip-large-literal-lists sql)]
- (if stripped?
- (is (= "SELECT * FROM (VALUES (NULL, NULL)) AS t(x, y)" result)
- (str n " tuples should be stripped"))
- (is (= sql result)
- (str n " tuples should not be stripped")))))))
-
-(deftest ^:parallel strip-in-list-literal-shapes-test
- (testing "Lists of each literal shape are stripped"
- (doseq [[shape item-fn] {"integers" str
- "decimals" #(str % ".5")
- "negatives" #(str "-" %)
- "plus-signed" #(str "+" %)
- "single-quoted" #(format "'name %d'" %)
- "commas-inside-strings" #(format "'a,%d,b'" %)
- "escaped-quotes" #(format "'it''s %d'" %)}]
- (let [sql (str "SELECT * FROM t WHERE x IN (" (str/join ", " (map item-fn (range 150))) ")")]
- (is (= "SELECT * FROM t WHERE x IN (NULL)" (sql-parsing/strip-large-literal-lists sql))
- (str shape " should be stripped")))))
- (testing "Whitespace variants are stripped"
- (doseq [sql [(str "SELECT * FROM t WHERE x IN(" (str/join "," (range 150)) ")")
- (str "SELECT * FROM t WHERE x IN\n (" (str/join " ,\n" (range 150)) ")")
- (str "SELECT * FROM t WHERE x in (" (str/join ",\t" (range 150)) ")")]]
- (is (str/includes? (sql-parsing/strip-large-literal-lists sql) "(NULL)")
- (pr-str (subs sql 0 40))))))
-
-(deftest ^:parallel strip-in-list-disqualifiers-test
- (testing "One non-literal item anywhere in a large list prevents stripping"
- (doseq [bad ["col_ref" "some_column" "foo(1)" "?" "$1" "{{param}}" "%(name)s"
- "1e5" "NULL" "DATE '2024-01-01'" "TIMESTAMP '2024-01-01 00:00:00'"
- "1::int" "x.y" "CURRENT_DATE" "-- comment" "héllo" "N'abc'"
- "\"quoted_identifier\"" "null1"]
- position [:first :middle :last]]
- (let [items (map str (range 150))
- items (case position
- :first (cons bad items)
- :middle (concat (take 75 items) [bad] (drop 75 items))
- :last (concat items [bad]))
- sql (str "SELECT * FROM t WHERE x IN (" (str/join ", " items) ")")]
- (is (= sql (sql-parsing/strip-large-literal-lists sql))
- (str (pr-str bad) " at " position " should prevent stripping")))))
- (testing "Subqueries are never stripped regardless of size"
- (let [sql "SELECT * FROM t WHERE x IN (SELECT id FROM u WHERE y = 'a' AND z IN ('b', 'c'))"]
- (is (= sql (sql-parsing/strip-large-literal-lists sql)))))
- (testing "A large list of double-quoted identifiers is not stripped (they are column references
- in identifier-quoting dialects, not strings)"
- (let [sql (str "SELECT * FROM t WHERE x IN (" (str/join ", " (map #(format "\"col_%d\"" %) (range 150))) ")")]
- (is (identical? sql (sql-parsing/strip-large-literal-lists sql))))))
-
-(deftest ^:parallel strip-preserves-surroundings-test
- (testing "Everything around a stripped list is preserved byte-for-byte"
- (let [prefix "WITH cte AS (SELECT 1 AS x) SELECT t.a, cte.x FROM t JOIN cte ON TRUE WHERE t.id "
- suffix " AND t.status = 'active' ORDER BY t.a LIMIT 5"
- sql (str prefix (in-list 150) suffix)]
- (is (= (str prefix "IN (NULL)" suffix)
- (sql-parsing/strip-large-literal-lists sql)))))
- (testing "Several lists in one statement are stripped/kept independently"
- (let [sql (str "SELECT * FROM t WHERE a " (in-list 150)
- " OR b " (in-list 3)
- " OR c NOT " (in-list 200)
- " OR d IN (SELECT x FROM u)")]
- (is (= (str "SELECT * FROM t WHERE a IN (NULL)"
- " OR b " (in-list 3)
- " OR c NOT IN (NULL)"
- " OR d IN (SELECT x FROM u)")
- (sql-parsing/strip-large-literal-lists sql))))))
-
-(deftest ^:parallel strip-nesting-test
- (testing "A large IN list inside a small VALUES tuple is stripped"
- (let [sql (str "SELECT * FROM (VALUES ((SELECT count(*) FROM u WHERE u.id " (in-list 150) "))) t")]
- (is (= "SELECT * FROM (VALUES ((SELECT count(*) FROM u WHERE u.id IN (NULL)))) t"
- (sql-parsing/strip-large-literal-lists sql)))))
- (testing "A large IN list inside a stripped VALUES clause disappears with it"
- (let [tuples (str/join ", " (map #(format "(%d)" %) (range 150)))
- sql (str "SELECT * FROM (VALUES " tuples ", ((SELECT 1 WHERE x " (in-list 150) "))) t")
- result (sql-parsing/strip-large-literal-lists sql)]
- (is (= "SELECT * FROM (VALUES (NULL)) t" result))))
- (testing "IN subqueries nest arbitrarily deep and the innermost large list is still found"
- (let [sql (str "SELECT * FROM a WHERE x IN "
- "(SELECT x FROM b WHERE y IN "
- "(SELECT y FROM c WHERE z " (in-list 150) "))")]
- (is (= (str "SELECT * FROM a WHERE x IN "
- "(SELECT x FROM b WHERE y IN "
- "(SELECT y FROM c WHERE z IN (NULL)))")
- (sql-parsing/strip-large-literal-lists sql))))))
-
-(deftest ^:parallel strip-idempotency-and-identity-test
- (testing "Stripping is idempotent"
- (doseq [sql [(str "SELECT * FROM t WHERE id " (in-list 150))
- (str "INSERT INTO foo VALUES " (str/join ", " (map #(format "(%d)" %) (range 150))))]]
- (let [once (sql-parsing/strip-large-literal-lists sql)]
- (is (= once (sql-parsing/strip-large-literal-lists once))))))
- (testing "When nothing is stripped the original string instance is returned"
- (doseq [sql ["SELECT * FROM t"
- "SELECT * FROM t WHERE id IN (1, 2, 3)"
- "SELECT * FROM t WHERE id IN (SELECT id FROM u)"
- "SELECT * FROM (VALUES (1), (2)) AS t(x)"]]
- (is (identical? sql (sql-parsing/strip-large-literal-lists sql))))))
-
-(deftest ^:parallel strip-malformed-sql-test
- (testing "Malformed or degenerate input never throws and is left unchanged"
- (doseq [sql [""
- "IN ("
- "VALUES ("
- (str "SELECT * FROM t WHERE id IN (" (str/join ", " (range 150))) ; no closing paren
- (str "SELECT * FROM t WHERE id IN ('unterminated string, " (str/join ", " (range 150)) ")")
- "SELECT * FROM t WHERE id IN ()"
- "IN () VALUES ()"
- "in( )"]]
- (is (= sql (sql-parsing/strip-large-literal-lists sql))
- (pr-str (subs sql 0 (min 40 (count sql))))))))
-
-(deftest ^:parallel strip-keyword-false-positives-test
- (testing "Words containing 'in'/'values' and quoted identifiers are not treated as keywords"
- (doseq [sql ["SELECT margin FROM t WHERE margin > 10"
- "SELECT * FROM t JOIN (SELECT * FROM u) v ON t.id = v.id"
- "SELECT CAST(x AS INT) FROM t"
- "SELECT * FROM logins (1, 2, 3)"
- "SELECT \"in\" FROM t"
- "SELECT t.values FROM t WHERE t.values > 10"]]
- (is (identical? sql (sql-parsing/strip-large-literal-lists sql)) sql))))
-
-(deftest ^:parallel strip-tuple-in-list-test
- (testing "Tuple IN lists below the comma threshold are preserved, above it stripped"
- ;; n two-element tuples contain 2n-1 commas; the threshold is 100 commas, so 51 tuples strip
- (doseq [[n stripped?] {1 false, 50 false, 51 true, 1000 true}]
- (let [tuples (str/join ", " (map #(format "(%d, %d)" % (* % 2)) (range n)))
- sql (str "SELECT * FROM t WHERE (a, b) IN (" tuples ")")
- result (sql-parsing/strip-large-literal-lists sql)]
- (if stripped?
- (is (= "SELECT * FROM t WHERE (a, b) IN (NULL)" result)
- (str n " tuples should be stripped"))
- (is (identical? sql result)
- (str n " tuples should not be stripped"))))))
- (testing "NOT IN and string tuples"
- (let [tuples (str/join ", " (map #(format "(%d, '%d')" % %) (range 150)))
- sql (str "SELECT * FROM t WHERE (a, b) NOT IN (" tuples ")")]
- (is (= "SELECT * FROM t WHERE (a, b) NOT IN (NULL)"
- (sql-parsing/strip-large-literal-lists sql)))))
- (testing "Tuple lists containing non-simple tuples are not stripped"
- (doseq [bad ["(SELECT 1, 2)" "(x, 1)" "(foo(1), 2)" "(?, ?)"]]
- (let [tuples (str/join ", " (concat (map #(format "(%d, %d)" % %) (range 150)) [bad]))
- sql (str "SELECT * FROM t WHERE (a, b) IN (" tuples ")")]
- (is (identical? sql (sql-parsing/strip-large-literal-lists sql))
- (str "tuple list containing " (pr-str bad) " should not be stripped"))))))
-
-(defn- array-literal
- "Build `ARRAY[0, 1, ..., n-1]` with `n` items."
- [n]
- (str "ARRAY[" (str/join ", " (range n)) "]"))
-
-(deftest ^:parallel strip-array-literal-test
- (testing "ARRAY literals at or below the threshold are preserved, above it stripped"
- (doseq [n [1 100]]
- (let [sql (str "SELECT * FROM t WHERE id = ANY(" (array-literal n) ")")]
- (is (identical? sql (sql-parsing/strip-large-literal-lists sql))
- (str n " items should not be stripped"))))
- (doseq [n [101 1000]]
- (let [sql (str "SELECT * FROM t WHERE id = ANY(" (array-literal n) ")")]
- (is (= "SELECT * FROM t WHERE id = ANY(ARRAY[NULL])" (sql-parsing/strip-large-literal-lists sql))
- (str n " items should be stripped")))))
- (testing "Casing and string arrays"
- (let [sql (str "SELECT array['" (str/join "', '" (range 150)) "'] AS a FROM t")]
- (is (= "SELECT array[NULL] AS a FROM t" (sql-parsing/strip-large-literal-lists sql)))))
- (testing "Array literals containing non-literals are not stripped"
- (doseq [bad ["col_ref" "foo(1)" "ARRAY[1]" "1:5"]]
- (let [sql (str "SELECT ARRAY[" (str/join ", " (concat (range 150) [bad])) "] FROM t")]
- (is (identical? sql (sql-parsing/strip-large-literal-lists sql))
- (str "array containing " (pr-str bad) " should not be stripped")))))
- (testing "Array subscript access on a column named array is untouched"
- (doseq [sql ["SELECT array[1] FROM t"
- "SELECT arr[1] FROM t"
- "SELECT arr[1:5] FROM t"]]
- (is (identical? sql (sql-parsing/strip-large-literal-lists sql)) sql)))
- (testing "A large ARRAY nested inside an unstripped IN subquery is still stripped"
- (let [sql (str "SELECT * FROM t WHERE id IN (SELECT id FROM u WHERE tags && " (array-literal 150) ")")]
- (is (= "SELECT * FROM t WHERE id IN (SELECT id FROM u WHERE tags && ARRAY[NULL])"
- (sql-parsing/strip-large-literal-lists sql))))))
-
-(deftest ^:parallel stripped-sql-parses-e2e-test
- (testing "every analysis entry point tolerates SQL whose lists were stripped"
- (let [sql (str "SELECT a.name FROM accounts a WHERE a.id " (in-list 150))]
- (is (= [[nil nil "accounts"]] (sql-parsing/referenced-tables "postgres" sql)))
- (is (= {:is_simple true} (sql-parsing/simple-query? "postgres" sql)))
- (is (=? {:status "ok"} (sql-parsing/validate-query "postgres" sql nil)))
- (is (=? {:used-fields set? :returned-fields vector? :errors #{}}
- (sql-parsing/field-references "postgres" sql)))))
- (testing "tuple IN lists and ARRAY literals also parse after stripping"
- (doseq [sql [(str "SELECT a.name FROM accounts a WHERE (a.id, a.org_id) IN ("
- (str/join ", " (map #(format "(%d, %d)" % %) (range 20000))) ")")
- (str "SELECT a.name FROM accounts a WHERE a.id = ANY(" (array-literal 20000) ")")]]
- (is (= [[nil nil "accounts"]] (sql-parsing/referenced-tables "postgres" sql)))
- (is (=? {:errors #{}} (sql-parsing/field-references "postgres" sql))))))
-
-(deftest ^:parallel large-values-referenced-tables-test
- (testing "referenced-tables works on queries with massive VALUES clauses"
- (let [tuples (str/join ", " (map #(format "(%d, %d)" % (mod % 100)) (range 20000)))
- sql (str "WITH lookup AS (SELECT * FROM (VALUES " tuples
- ") AS v(id, score)) "
- "SELECT a.name, l.score FROM accounts a "
- "JOIN lookup l ON l.id = a.id")
- result (sql-parsing/referenced-tables "postgres" sql)]
- (is (= [[nil nil "accounts"]] result)))))
-
-(deftest ^:parallel referenced-fields-dialect-support-test
- (testing "Referenced fields works across different SQL dialects"
- (doseq [dialect ["postgres" "mysql" "snowflake" "bigquery" "redshift" "duckdb"]]
- (testing (str "dialect: " dialect)
- (let [result (sql-parsing/referenced-fields dialect "SELECT id, name FROM users WHERE active = true")]
- (is (seq result)
- (str "Should return fields for " dialect))
- (is (every? #(and (vector? %) (= 4 (count %))) result)
- (str "Each field should be [catalog schema table field] tuple for " dialect)))))))
-
-(deftest ^:parallel referenced-fields-wildcard-test
- (testing "Wildcard handling in referenced fields"
- (testing "Unqualified wildcard - single table"
- (let [result (sql-parsing/referenced-fields "postgres" "SELECT * FROM users")]
- (is (fields-match? [["users" "*"]] result))))
- (testing "Unqualified wildcard - multiple tables"
- (let [result (sql-parsing/referenced-fields "postgres" "SELECT * FROM users u LEFT JOIN orders o ON u.id = o.user_id")]
- (is (some #(= ["orders" "*"] %) (normalize-fields result))
- "Should include orders wildcard")
- (is (some #(= ["users" "*"] %) (normalize-fields result))
- "Should include users wildcard")))
- (testing "Qualified wildcard"
- (let [result (sql-parsing/referenced-fields "postgres" "SELECT u.* FROM users u")]
- (is (fields-match? [["users" "*"]] result))))
- (testing "Mixed wildcards and specific columns"
- (let [result (sql-parsing/referenced-fields "postgres" "SELECT u.*, t.total FROM users u, transactions t WHERE u.id = t.user_id")]
- (is (some #(= ["users" "*"] %) (normalize-fields result))
- "Should include users wildcard")
- (is (some #(= ["transactions" "total"] %) (normalize-fields result))
- "Should include transactions.total")))))
-
-;;; ------------------------------------------------ Parse errors --------------------------------------------------
-
-(deftest parse-error-test
- (testing "unparseable SQL surfaces as a transport-agnostic parse error"
- (let [e (try
- (sql-parsing/referenced-tables "postgres" "SELECT !!!")
- (catch Exception e e))]
- (is (sql-parsing/parse-error? e)))))
-
-;;; ------------------------------------------------ Timeout robustness --------------------------------------------
-
-(deftest timeout-surfaces-even-when-metric-inc-fails-test
- (testing (str "A fired Python-call timeout still surfaces as TimeoutException from the API even if "
- "the timeout-metric bump throws — a misconfigured analytics registry must not shadow "
- "the timeout (#77084).")
- (dynamic-redefs/with-dynamic-fn-redefs [;; force the timeout branch of do-with-python-context without waiting 30s
- graal/with-timeout* (fn [_ _] :metabase.sql-parsing.graal/timeout)
- ;; poison only the timeout metric; leave the acquisition inc! alone
- analytics-interface/inc! (fn [k & _]
- (when (= k :metabase-sql-parsing/context-timeouts)
- (throw (Exception. "boom"))))]
- (is (thrown? TimeoutException
- (sql-parsing/referenced-tables "postgres" "SELECT 1"))))))
+ (sort (sql-parsing/referenced-fields "postgres" sql))))))
+ (testing "and is one statement still"
+ (let [sql (str "SELECT * FROM t WHERE id IN (" (str/join ", " (range 20000)) ")")]
+ (is (=? {:is-single-stmt? true} (sql-parsing/is-single-stmt-of-type? "postgres" sql "read"))))))
diff --git a/test/metabase/sql_parsing/jar_python_fs_repro_test.clj b/test/metabase/sql_parsing/graal_test.clj
similarity index 67%
rename from test/metabase/sql_parsing/jar_python_fs_repro_test.clj
rename to test/metabase/sql_parsing/graal_test.clj
index 1380ffa923b..95b6eb6c928 100644
--- a/test/metabase/sql_parsing/jar_python_fs_repro_test.clj
+++ b/test/metabase/sql_parsing/graal_test.clj
@@ -1,4 +1,4 @@
-(ns metabase.sql-parsing.jar-python-fs-repro-test
+(ns metabase.sql-parsing.graal-test
"Regression for https://github.com/metabase/metabase/issues/73541.
GraalVM's bundled polyglot `FileSystem` wrapper routes `newByteChannel` through
@@ -8,7 +8,10 @@
`Files/newByteChannel`, which returns an in-memory `ByteArrayChannel` for compressed entries."
(:require
[clojure.test :refer :all]
+ [metabase.analytics-interface.core :as analytics-interface]
+ [metabase.sql-parsing.core :as sql-parsing]
[metabase.sql-parsing.graal :as graal]
+ [metabase.test.util.dynamic-redefs :as dynamic-redefs]
[metabase.util.files :as u.files])
(:import
(java.io FileOutputStream)
@@ -16,6 +19,7 @@
(java.nio.file Files StandardOpenOption)
(java.nio.file.attribute FileAttribute)
(java.util EnumSet)
+ (java.util.concurrent TimeoutException)
(java.util.zip ZipEntry ZipOutputStream)
(org.graalvm.polyglot.io FileSystem)))
@@ -46,3 +50,16 @@
"Channel must not be a FileChannel — that implies temp-file extraction beside the jar."))))
(finally
(Files/deleteIfExists zip-file))))))
+
+(deftest timeout-surfaces-even-when-metric-inc-fails-test
+ (testing (str "A fired Python-call timeout still surfaces as TimeoutException from the API even if "
+ "the timeout-metric bump throws — a misconfigured analytics registry must not shadow "
+ "the timeout (#77084).")
+ (dynamic-redefs/with-dynamic-fn-redefs [;; force the timeout branch of do-with-python-context without waiting 30s
+ graal/with-timeout* (fn [_ _] :metabase.sql-parsing.graal/timeout)
+ ;; poison only the timeout metric; leave the acquisition inc! alone
+ analytics-interface/inc! (fn [k & _]
+ (when (= k :metabase-sql-parsing/context-timeouts)
+ (throw (Exception. "boom"))))]
+ (is (thrown? TimeoutException
+ (sql-parsing/referenced-tables "postgres" "SELECT 1"))))))
diff --git a/test/metabase/sql_parsing/test_cases/field_references.edn b/test/metabase/sql_parsing/test_cases/field_references.edn
new file mode 100644
index 00000000000..e2fd26d5b46
--- /dev/null
+++ b/test/metabase/sql_parsing/test_cases/field_references.edn
@@ -0,0 +1,48 @@
+{:cases
+ [{:name "a query whose columns all resolve reports no errors"
+ :dialect "postgres"
+ :sql "SELECT id, name FROM people"
+ :errors []
+ :returned ["id" "name"]}
+
+ {:name "a column qualified by a table the query does not read is a dangling qualifier"
+ :dialect "postgres"
+ :sql "SELECT people.id FROM tmp_people"
+ :errors [{:type :missing-table-alias :name "people"}]
+ :returned []}
+
+ {:name "a column qualified by an alias is not dangling"
+ :dialect "postgres"
+ :sql "SELECT p.id FROM people p"
+ :errors []
+ :returned ["id"]}
+
+ {:name "a column qualified by the table's own name is not dangling"
+ :dialect "postgres"
+ :sql "SELECT people.id FROM people"
+ :errors []
+ :returned ["id"]}
+
+ {:name "a star is a star, not a column"
+ :dialect "postgres"
+ :sql "SELECT * FROM people"
+ :errors []
+ :returned []}
+
+ {:name "a qualifier left behind by a rewrite is what Guard B looks for"
+ :dialect "postgres"
+ :sql "SELECT people.id, tmp_people.name FROM tmp_people"
+ :errors [{:type :missing-table-alias :name "people"}]
+ :returned ["name"]}
+
+ {:name "a column qualified through a CTE name resolves"
+ :dialect "postgres"
+ :sql "WITH c AS (SELECT id FROM people) SELECT c.id FROM c"
+ :errors []
+ :returned ["id"]}
+
+ {:name "a syntax error is reported as one, with no fields"
+ :dialect "postgres"
+ :sql "SELECT * FROM WHERE ("
+ :errors [{:type :syntax-error}]
+ :returned []}]}
diff --git a/test/metabase/sql_parsing/test_cases/literal_stripping.edn b/test/metabase/sql_parsing/test_cases/literal_stripping.edn
new file mode 100644
index 00000000000..575ee15fe92
--- /dev/null
+++ b/test/metabase/sql_parsing/test_cases/literal_stripping.edn
@@ -0,0 +1,174 @@
+{:cases
+ [{:name "no list at all"
+ :sql "SELECT * FROM users WHERE id = 1"
+ :expected :unchanged}
+
+ {:name "a small VALUES clause"
+ :sql "SELECT * FROM (VALUES (1, 'a'), (2, 'b'), (3, 'c')) AS t(id, name)"
+ :expected :unchanged}
+
+ {:name "a small IN list"
+ :sql "SELECT * FROM t WHERE id IN (1, 2, 3)"
+ :expected :unchanged}
+
+ {:name "an IN list at the hundred-item threshold"
+ :build {:prefix "SELECT * FROM t WHERE id IN (" :item "%d" :count 100 :suffix ")"}
+ :expected :unchanged}
+
+ {:name "an IN list one item past the threshold"
+ :build {:prefix "SELECT * FROM t WHERE id IN (" :item "%d" :count 101 :suffix ")"}
+ :expected "SELECT * FROM t WHERE id IN (NULL)"}
+
+ {:name "a long IN list, with the rest of the predicate kept"
+ :build {:prefix "SELECT * FROM t WHERE id IN (" :item "%d" :count 200 :suffix ") AND active = true"}
+ :expected "SELECT * FROM t WHERE id IN (NULL) AND active = true"}
+
+ {:name "a long IN list of strings"
+ :build {:prefix "SELECT * FROM t WHERE name IN (" :item "'name %d'" :count 200 :suffix ")"}
+ :expected "SELECT * FROM t WHERE name IN (NULL)"}
+
+ {:name "the keyword's own casing is kept"
+ :build {:prefix "SELECT * FROM t WHERE id in (" :item "%d" :count 200 :suffix ")"}
+ :expected "SELECT * FROM t WHERE id in (NULL)"}
+
+ {:name "NOT IN"
+ :build {:prefix "SELECT * FROM t WHERE id NOT IN (" :item "%d" :count 200 :suffix ")"}
+ :expected "SELECT * FROM t WHERE id NOT IN (NULL)"}
+
+ {:name "a long list nested inside a subquery that is itself left alone"
+ :build {:prefix "SELECT * FROM t WHERE id IN (SELECT id FROM other WHERE x IN (" :item "%d" :count 200 :suffix "))"}
+ :expected "SELECT * FROM t WHERE id IN (SELECT id FROM other WHERE x IN (NULL))"}
+
+ {:name "an IN list holding a column reference"
+ :build {:prefix "SELECT * FROM t WHERE id IN (" :item "%d" :count 200 :extra "col_ref" :suffix ")"}
+ :expected :unchanged}
+
+ {:name "an IN list holding a function call"
+ :build {:prefix "SELECT * FROM t WHERE id IN (" :item "%d" :count 200 :extra "foo(1)" :suffix ")"}
+ :expected :unchanged}
+
+ {:name "an IN list holding a parameter"
+ :build {:prefix "SELECT * FROM t WHERE id IN (" :item "%d" :count 200 :extra "?" :suffix ")"}
+ :expected :unchanged}
+
+ {:name "an IN list holding a template tag"
+ :build {:prefix "SELECT * FROM t WHERE id IN (" :item "%d" :count 200 :extra "{{param}}" :suffix ")"}
+ :expected :unchanged}
+
+ {:name "an IN list holding a typed literal"
+ :build {:prefix "SELECT * FROM t WHERE id IN (" :item "%d" :count 200 :extra "DATE '2024-01-01'" :suffix ")"}
+ :expected :unchanged}
+
+ {:name "an IN list holding NULL"
+ :build {:prefix "SELECT * FROM t WHERE id IN (" :item "%d" :count 200 :extra "NULL" :suffix ")"}
+ :expected :unchanged}
+
+ {:name "an IN whose argument is a subquery"
+ :sql "SELECT * FROM t WHERE id IN (SELECT id FROM other)"
+ :expected :unchanged}
+
+ {:name "a long VALUES clause keeps its column count and its alias"
+ :build {:prefix "SELECT * FROM (VALUES " :item "(%d, 'name%d', %d)" :count 200 :suffix ") AS t(id, name, score)"}
+ :expected {:includes ["VALUES (NULL, NULL, NULL)" "AS t(id, name, score)"]
+ :excludes ["name0"]}}
+
+ {:name "a VALUES clause at the hundred-tuple threshold"
+ :build {:prefix "SELECT * FROM (VALUES " :item "(%d, %d)" :count 100 :suffix ") AS t(x, y)"}
+ :expected :unchanged}
+
+ {:name "a VALUES clause one tuple past it"
+ :build {:prefix "SELECT * FROM (VALUES " :item "(%d, %d)" :count 101 :suffix ") AS t(x, y)"}
+ :expected "SELECT * FROM (VALUES (NULL, NULL)) AS t(x, y)"}
+
+ {:name "the VALUES keyword's own casing is kept"
+ :build {:prefix "select * from (values " :item "(%d)" :count 200 :suffix ") as t(x)"}
+ :expected "select * from (values (NULL)) as t(x)"}
+
+ {:name "INSERT ... VALUES"
+ :build {:prefix "INSERT INTO foo VALUES " :item "(%d, 'x')" :count 200 :suffix ""}
+ :expected {:includes ["VALUES (NULL, NULL)"] :excludes ["(0, 'x')"]}}
+
+ {:name "VALUES inside a string literal"
+ :sql "SELECT 'INSERT INTO foo VALUES (1,2,3)' AS example FROM bar"
+ :expected :unchanged}
+
+ {:name "a column named values"
+ :sql "SELECT values FROM my_table WHERE values > 10"
+ :expected :unchanged}
+
+ {:name "the VALUES keyword not followed by a parenthesis"
+ :sql "SELECT * FROM t WHERE col IN (SELECT values FROM other)"
+ :expected :unchanged}
+
+ {:name "a tuple IN list under the comma threshold"
+ :build {:prefix "SELECT * FROM t WHERE (a, b) IN (" :item "(%d, %d)" :count 50 :suffix ")"}
+ :expected :unchanged}
+
+ {:name "a tuple IN list over it"
+ :build {:prefix "SELECT * FROM t WHERE (a, b) IN (" :item "(%d, %d)" :count 51 :suffix ")"}
+ :expected "SELECT * FROM t WHERE (a, b) IN (NULL)"}
+
+ {:name "a tuple NOT IN list"
+ :build {:prefix "SELECT * FROM t WHERE (a, b) NOT IN (" :item "(%d, '%d')" :count 150 :suffix ")"}
+ :expected "SELECT * FROM t WHERE (a, b) NOT IN (NULL)"}
+
+ {:name "a tuple list holding a tuple that is not simple"
+ :build {:prefix "SELECT * FROM t WHERE (a, b) IN (" :item "(%d, %d)" :count 150 :extra "(x, 1)" :suffix ")"}
+ :expected :unchanged}
+
+ {:name "an ARRAY literal at the threshold"
+ :build {:prefix "SELECT * FROM t WHERE id = ANY(ARRAY[" :item "%d" :count 100 :suffix "])"}
+ :expected :unchanged}
+
+ {:name "an ARRAY literal past it"
+ :build {:prefix "SELECT * FROM t WHERE id = ANY(ARRAY[" :item "%d" :count 101 :suffix "])"}
+ :expected "SELECT * FROM t WHERE id = ANY(ARRAY[NULL])"}
+
+ {:name "a lower-case array of strings"
+ :build {:prefix "SELECT array[" :item "'%d'" :count 150 :suffix "] AS a FROM t"}
+ :expected "SELECT array[NULL] AS a FROM t"}
+
+ {:name "an array holding a column reference"
+ :build {:prefix "SELECT ARRAY[" :item "%d" :count 150 :extra "col_ref" :suffix "] FROM t"}
+ :expected :unchanged}
+
+ {:name "an array subscript is not an array literal"
+ :sql "SELECT arr[1:5] FROM t"
+ :expected :unchanged}
+
+ {:name "a word ending in IN, and a cast to INT"
+ :sql "SELECT CAST(x AS INT) FROM a JOIN (SELECT * FROM b) c ON a.id = c.id"
+ :expected :unchanged}
+
+ {:name "a column named margin"
+ :sql "SELECT margin FROM t WHERE margin > 10"
+ :expected :unchanged}
+
+ {:name "a quoted identifier spelled like the keyword"
+ :sql "SELECT \"in\" FROM t"
+ :expected :unchanged}
+
+ {:name "an unterminated list"
+ :build {:prefix "SELECT * FROM t WHERE id IN (" :item "%d" :count 150 :suffix ""}
+ :expected :unchanged}
+
+ {:name "an unterminated string inside a list"
+ :build {:prefix "SELECT * FROM t WHERE id IN ('unterminated string, " :item "%d" :count 150 :suffix ")"}
+ :expected :unchanged}
+
+ {:name "an empty list"
+ :sql "SELECT * FROM t WHERE id IN ()"
+ :expected :unchanged}
+
+ {:name "nothing but keywords"
+ :sql "IN () VALUES ()"
+ :expected :unchanged}
+
+ {:name "an empty query"
+ :sql ""
+ :expected :unchanged}
+
+ {:name "a long list and a long VALUES in one statement"
+ :build {:prefix "WITH v AS (SELECT * FROM (VALUES " :item "(%d)" :count 200
+ :suffix ") AS t(x)) SELECT * FROM v WHERE x IN (1, 2, 3)"}
+ :expected "WITH v AS (SELECT * FROM (VALUES (NULL)) AS t(x)) SELECT * FROM v WHERE x IN (1, 2, 3)"}]}
diff --git a/test/metabase/sql_parsing/test_cases/parse_error_message.edn b/test/metabase/sql_parsing/test_cases/parse_error_message.edn
new file mode 100644
index 00000000000..514d2bccedf
--- /dev/null
+++ b/test/metabase/sql_parsing/test_cases/parse_error_message.edn
@@ -0,0 +1,24 @@
+{:cases
+ [{:name "a keyword the parser was still waiting for"
+ :dialect "postgres"
+ :sql "SELECT !!!"
+ :internals "<class 'sqlglot.expressions.Not'>"
+ :diagnostic "ParseError: Required keyword: 'this' missing. Line 1, Col: 10.\n SELECT !!!\n ^"}
+
+ {:name "a token where a table name belonged"
+ :dialect "postgres"
+ :sql "SELECT * FROM WHERE ("
+ :internals "<Token token_type: TokenType.WHERE, text: WHERE,"
+ :diagnostic "ParseError: Expected table name but got 'WHERE'. Line 1, Col: 19.\n SELECT * FROM WHERE (\n ^^^^^"}
+
+ {:name "the end of the query where a table name belonged"
+ :dialect "postgres"
+ :sql "SELECT * FROM a JOIN"
+ :internals "but got None"
+ :diagnostic "ParseError: Expected table name but got the end of the query. Line 1, Col: 20.\n SELECT * FROM a JOIN\n ^^^^"}
+
+ {:name "a query that is not SQL at all"
+ :dialect "postgres"
+ :sql "complete nonsense query"
+ :internals "["
+ :diagnostic "ParseError: Invalid expression / Unexpected token. Line 1, Col: 23.\n complete nonsense query\n ^^^^^"}]}
diff --git a/test/metabase/sql_parsing/referenced_fields_test_cases.edn b/test/metabase/sql_parsing/test_cases/referenced_fields.edn
similarity index 87%
rename from test/metabase/sql_parsing/referenced_fields_test_cases.edn
rename to test/metabase/sql_parsing/test_cases/referenced_fields.edn
index 6e35418a9d9..cce6ac25656 100644
--- a/test/metabase/sql_parsing/referenced_fields_test_cases.edn
+++ b/test/metabase/sql_parsing/test_cases/referenced_fields.edn
@@ -1,13 +1,5 @@
-;; Referenced Fields Test Cases
-;; Each test case has:
-;; - :name - descriptive name for the test
-;; - :sql - the SQL query to analyze
-;; - :dialect - SQL dialect (postgres, mysql, snowflake, bigquery, redshift, duckdb)
-;; - :expected - vector of [table field] pairs (sorted)
-
-{:test-cases
- [;; Basic SELECT queries
- {:name "simple-select-with-columns"
+{:cases
+ [{:name "simple-select-with-columns"
:sql "SELECT id, name FROM users"
:dialect "postgres"
:expected [["users" "id"] ["users" "name"]]}
@@ -32,7 +24,6 @@
:dialect "postgres"
:expected [["users" "id"] ["users" "name"]]}
- ;; Wildcards
{:name "unqualified-wildcard-single-table"
:sql "SELECT * FROM users"
:dialect "postgres"
@@ -53,7 +44,6 @@
:dialect "postgres"
:expected [["transactions" "total"] ["transactions" "user_id"] ["users" "*"] ["users" "id"]]}
- ;; JOINs
{:name "inner-join-simple"
:sql "SELECT u.id, o.total FROM users u INNER JOIN orders o ON u.id = o.user_id"
:dialect "postgres"
@@ -79,8 +69,6 @@
:dialect "postgres"
:expected [["transactions" "total"] ["transactions" "user_id"] ["users" "id"]]}
- ;; CTEs
- ;; Note: CTEs create scopes, and sqlglot attributes columns to the CTE scope in some cases
{:name "simple-cte"
:sql "WITH active_users AS (SELECT id FROM users WHERE active) SELECT * FROM active_users"
:dialect "postgres"
@@ -89,7 +77,6 @@
{:name "cte-with-join"
:sql "WITH active AS (SELECT id FROM users WHERE active = true) SELECT a.id, t.total FROM active a JOIN transactions t ON a.id = t.user_id"
:dialect "postgres"
- ;; Note: sqlglot sees columns from CTE join and attributes some to transactions scope
:expected [["transactions" "active"] ["transactions" "id"] ["transactions" "total"] ["transactions" "user_id"] ["users" "active"] ["users" "id"]]}
{:name "multiple-ctes"
@@ -97,7 +84,6 @@
:dialect "postgres"
:expected [["transactions" "created_at"] ["transactions" "total"] ["transactions" "user_id"] ["users" "active"] ["users" "id"]]}
- ;; Subqueries
{:name "subquery-in-from"
:sql "SELECT * FROM (SELECT id, name FROM users WHERE active = true) AS active_users"
:dialect "postgres"
@@ -106,7 +92,6 @@
{:name "subquery-in-where"
:sql "SELECT id, name FROM users WHERE id IN (SELECT user_id FROM transactions WHERE total > 100)"
:dialect "postgres"
- ;; Note: sqlglot attributes some subquery columns to outer scope
:expected [["transactions" "total"] ["transactions" "user_id"] ["users" "id"] ["users" "name"] ["users" "total"] ["users" "user_id"]]}
{:name "correlated-subquery"
@@ -114,8 +99,6 @@
:dialect "postgres"
:expected [["transactions" "user_id"] ["users" "id"] ["users" "name"]]}
- ;; Aggregations
- ;; Note: The * in COUNT(*) is not a column reference, it's part of the aggregate function syntax
{:name "count-aggregate"
:sql "SELECT COUNT(*) FROM users"
:dialect "postgres"
@@ -136,7 +119,6 @@
:dialect "postgres"
:expected [["employees" "department"] ["employees" "salary"]]}
- ;; Window functions
{:name "window-function-simple"
:sql "SELECT name, salary, RANK() OVER (ORDER BY salary DESC) FROM employees"
:dialect "postgres"
@@ -147,7 +129,6 @@
:dialect "postgres"
:expected [["employees" "department"] ["employees" "name"] ["employees" "salary"]]}
- ;; CASE expressions
{:name "case-expression"
:sql "SELECT name, CASE WHEN age < 18 THEN 'minor' ELSE 'adult' END AS age_group FROM users"
:dialect "postgres"
@@ -158,7 +139,6 @@
:dialect "postgres"
:expected [["users" "active"] ["users" "age"] ["users" "name"]]}
- ;; Set operations
{:name "union-simple"
:sql "SELECT id, name FROM users UNION SELECT id, name FROM archived_users"
:dialect "postgres"
@@ -169,7 +149,6 @@
:dialect "postgres"
:expected [["active_users" "id"] ["inactive_users" "id"]]}
- ;; Schema-qualified tables
{:name "schema-qualified-single"
:sql "SELECT id, name FROM public.users"
:dialect "postgres"
@@ -180,7 +159,6 @@
:dialect "postgres"
:expected [["orders" "total"] ["orders" "user_id"] ["users" "id"]]}
- ;; Complex queries
{:name "complex-with-cte-join-subquery"
:sql "WITH ranked AS (SELECT id, name, RANK() OVER (ORDER BY created_at DESC) as rank FROM users WHERE active = true) SELECT r.id, r.name, t.total FROM ranked r JOIN (SELECT user_id, SUM(amount) as total FROM transactions GROUP BY user_id) t ON r.id = t.user_id WHERE r.rank <= 10"
:dialect "postgres"
@@ -189,10 +167,8 @@
{:name "nested-subqueries"
:sql "SELECT id, name FROM users WHERE id IN (SELECT user_id FROM transactions WHERE total > (SELECT AVG(total) FROM transactions))"
:dialect "postgres"
- ;; Note: sqlglot attributes some subquery columns to outer scope
:expected [["transactions" "total"] ["transactions" "user_id"] ["users" "id"] ["users" "name"] ["users" "total"] ["users" "user_id"]]}
- ;; Edge cases
{:name "table-alias-same-as-table-name"
:sql "SELECT users.id, users.name FROM users users"
:dialect "postgres"
@@ -208,7 +184,6 @@
:dialect "postgres"
:expected []}
- ;; Different dialects
{:name "mysql-backticks"
:sql "SELECT `id`, `name` FROM `users` WHERE `active` = 1"
:dialect "mysql"
@@ -217,7 +192,6 @@
{:name "bigquery-struct"
:sql "SELECT id, name, address.city FROM users"
:dialect "bigquery"
- ;; Note: Struct field access (address.city) is not parsed as a column reference
:expected [["users" "id"] ["users" "name"]]}
{:name "snowflake-uppercase"
@@ -225,7 +199,6 @@
:dialect "snowflake"
:expected [["USERS" "ID"] ["USERS" "NAME"]]}
- ;; INSERT/UPDATE/DELETE (less common but valid)
{:name "insert-with-select"
:sql "INSERT INTO archive_users SELECT id, name, email FROM users WHERE active = false"
:dialect "postgres"
@@ -234,11 +207,9 @@
{:name "update-with-join"
:sql "UPDATE users SET last_purchase = t.max_date FROM (SELECT user_id, MAX(created_at) as max_date FROM transactions GROUP BY user_id) t WHERE users.id = t.user_id"
:dialect "postgres"
- ;; Note: UPDATE statements may not be fully parsed by sqlglot for field extraction
:expected []}
{:name "delete-with-subquery"
:sql "DELETE FROM users WHERE id IN (SELECT user_id FROM transactions WHERE total < 10)"
:dialect "postgres"
- ;; Note: DELETE statements don't include the target table's columns in references
:expected [["transactions" "total"] ["transactions" "user_id"]]}]}
diff --git a/test/metabase/sql_parsing/test_cases/referenced_tables.edn b/test/metabase/sql_parsing/test_cases/referenced_tables.edn
new file mode 100644
index 00000000000..00dc7a4868e
--- /dev/null
+++ b/test/metabase/sql_parsing/test_cases/referenced_tables.edn
@@ -0,0 +1,200 @@
+{:cases
+ [{:name "one table"
+ :dialect "postgres"
+ :sql "SELECT * FROM users"
+ :tables [[nil nil "users"]]}
+
+ {:name "a join names both tables"
+ :dialect "postgres"
+ :sql "SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id"
+ :tables [[nil nil "orders"] [nil nil "users"]]}
+
+ {:name "the same table name in two schemas is two tables"
+ :dialect "postgres"
+ :sql "SELECT public.users.id, other.users.id FROM public.users u1 JOIN other.users u2 ON u1.id = u2.id"
+ :tables [[nil "other" "users"] [nil "public" "users"]]}
+
+ {:name "a CTE is not a table, and the table it reads is"
+ :dialect "postgres"
+ :sql "WITH active AS (SELECT * FROM users WHERE active) SELECT * FROM active"
+ :tables [[nil nil "users"]]}
+
+ {:name "a CTE named after a real table shadows it"
+ :dialect "postgres"
+ :sql "WITH orders AS (SELECT 1 AS id) SELECT * FROM orders, people"
+ :tables [[nil nil "people"]]}
+
+ {:name "a CTE reading a CTE"
+ :dialect "postgres"
+ :sql "WITH a AS (SELECT id FROM people), b AS (SELECT id FROM a) SELECT * FROM b"
+ :tables [[nil nil "people"]]}
+
+ {:name "a recursive CTE"
+ :dialect "postgres"
+ :sql "WITH RECURSIVE r AS (SELECT id, parent_id FROM people UNION ALL SELECT p.id, p.parent_id FROM people p JOIN r ON p.parent_id = r.id) SELECT * FROM r"
+ :tables [[nil nil "people"]]}
+
+ {:name "a derived table is not a table, and the table inside it is"
+ :dialect "postgres"
+ :sql "SELECT * FROM (SELECT id FROM users) AS sub"
+ :tables [[nil nil "users"]]}
+
+ {:name "a correlated subquery in the predicate is read"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders o WHERE EXISTS (SELECT 1 FROM people p WHERE p.id = o.user_id)"
+ :tables [[nil nil "orders"] [nil nil "people"]]}
+
+ {:name "a table buried under several levels of subquery"
+ :dialect "postgres"
+ :sql "SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT id FROM secrets) a) b) c"
+ :tables [[nil nil "secrets"]]}
+
+ {:name "both arms of a union"
+ :dialect "postgres"
+ :sql "SELECT id, name FROM a UNION SELECT id, name FROM b"
+ :tables [[nil nil "a"] [nil nil "b"]]}
+
+ {:name "a union nested in a derived table"
+ :dialect "snowflake"
+ :sql "SELECT * FROM (SELECT id FROM a UNION SELECT id FROM b) AS combined"
+ :tables [[nil nil "a"] [nil nil "b"]]}
+
+ {:name "intersect and except read both sides"
+ :dialect "bigquery"
+ :sql "SELECT id FROM a INTERSECT DISTINCT SELECT id FROM b"
+ :tables [[nil nil "a"] [nil nil "b"]]}
+
+ {:name "a set operation inside a CTE"
+ :dialect "postgres"
+ :sql "WITH u AS (SELECT id FROM a UNION ALL SELECT id FROM b) SELECT * FROM u"
+ :tables [[nil nil "a"] [nil nil "b"]]}
+
+ {:name "a table function is not a table, and the table beside it is"
+ :dialect "postgres"
+ :sql "SELECT o.* FROM orders o, generate_series(1, 10) g"
+ :tables [[nil nil "orders"]]}
+
+ {:name "UNNEST is not a table either"
+ :dialect "bigquery"
+ :sql "SELECT o.* FROM orders o, UNNEST([1]) AS val"
+ :tables [[nil nil "orders"]]}
+
+ {:name "VALUES is not a table"
+ :dialect "postgres"
+ :sql "SELECT * FROM (VALUES (1), (2)) AS t(id) JOIN orders o ON o.id = t.id"
+ :tables [[nil nil "orders"]]}
+
+ {:name "a lateral join reads the table it is lateral to"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders o CROSS JOIN LATERAL (SELECT id FROM people p WHERE p.id = o.user_id) x"
+ :tables [[nil nil "orders"] [nil nil "people"]]}
+
+ {:name "a window function does not hide the table under it"
+ :dialect "postgres"
+ :sql "SELECT id, row_number() OVER (PARTITION BY user_id ORDER BY created_at) FROM orders"
+ :tables [[nil nil "orders"]]}
+
+ {:name "an alias shadowing a real table name is an alias"
+ :dialect "postgres"
+ :sql "SELECT people.id FROM orders AS people"
+ :tables [[nil nil "orders"]]}
+
+ {:name "the same table joined to itself twice"
+ :dialect "postgres"
+ :sql "SELECT a.id FROM people a JOIN people b ON a.parent_id = b.id"
+ :tables [[nil nil "people"]]}
+
+ {:name "BigQuery fills all three slots from project.dataset.table"
+ :dialect "bigquery"
+ :sql "SELECT * FROM myproject.analytics.events"
+ :tables [["myproject" "analytics" "events"]]}
+
+ {:name "Snowflake fills all three slots from database.schema.table"
+ :dialect "snowflake"
+ :sql "SELECT * FROM mydb.public.orders"
+ :tables [["mydb" "public" "orders"]]}
+
+ {:name "Postgres puts a schema in the middle slot and leaves the catalog empty"
+ :dialect "postgres"
+ :sql "SELECT * FROM public.users u JOIN other_schema.accounts a ON u.id = a.user_id"
+ :tables [[nil "other_schema" "accounts"] [nil "public" "users"]]}
+
+ {:name "MySQL puts a database in the schema slot, having no level between"
+ :dialect "mysql"
+ :sql "SELECT * FROM mydb.users"
+ :tables [[nil "mydb" "users"]]}
+
+ {:name "H2 is read with sqlglot's default dialect, as transform testing reads it"
+ :dialect nil
+ :sql "SELECT ID, NAME FROM PUBLIC.PEOPLE"
+ :tables [[nil "public" "people"]]}
+
+ {:name "SQL Server brackets quote an identifier"
+ :dialect "tsql"
+ :sql "SELECT * FROM [dbo].[orders]"
+ :tables [[nil "dbo" "orders"]]}
+
+ {:name "ClickHouse reads a two-part name as database.table"
+ :dialect "clickhouse"
+ :sql "SELECT * FROM analytics.events"
+ :tables [[nil "analytics" "events"]]}
+
+ {:name "a name in a comment is not a table"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders -- SELECT * FROM secrets"
+ :tables [[nil nil "orders"]]}
+
+ {:name "every statement is read, not only the first"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders; DELETE FROM people"
+ :tables [[nil nil "orders"] [nil nil "people"]]}
+
+ {:name "the table a DELETE writes is one of its tables"
+ :dialect "postgres"
+ :sql "DELETE FROM people WHERE id IN (SELECT id FROM orders)"
+ :tables [[nil nil "orders"] [nil nil "people"]]}
+
+ {:name "the table an INSERT writes, likewise"
+ :dialect "postgres"
+ :sql "INSERT INTO people (id) SELECT id FROM orders"
+ :tables [[nil nil "orders"] [nil nil "people"]]}
+
+ {:name "the table an UPDATE writes, likewise"
+ :dialect "postgres"
+ :sql "UPDATE people SET total = 1 WHERE id IN (SELECT id FROM orders)"
+ :tables [[nil nil "orders"] [nil nil "people"]]}
+
+ {:name "the table a CREATE TABLE AS SELECT writes, and the one it reads"
+ :dialect "postgres"
+ :sql "CREATE TABLE summary AS SELECT id FROM orders"
+ :tables [[nil nil "orders"] [nil nil "summary"]]}
+
+ {:name "the view a CREATE VIEW declares, and the table behind it"
+ :dialect "postgres"
+ :sql "CREATE VIEW v_orders AS SELECT id FROM orders"
+ :tables [[nil nil "orders"] [nil nil "v_orders"]]}
+
+ {:name "a name written as one quoted identifier holding a dot is one table"
+ :dialect nil
+ :sql "SELECT ssn FROM \"PUBLIC.SECRETS\""
+ :tables [[nil nil "public.secrets"]]}
+
+ {:name "both tables of a MERGE"
+ :dialect "postgres"
+ :sql "MERGE INTO people tt USING orders ss ON tt.id = ss.id WHEN MATCHED THEN UPDATE SET total = ss.total"
+ :tables [[nil nil "orders"] [nil nil "people"]]}
+
+ {:name "a view is the table it names, the tables behind it being the warehouse's business"
+ :dialect "postgres"
+ :sql "SELECT ssn FROM masked_view"
+ :tables [[nil nil "masked_view"]]}
+
+ {:name "SQL inside a string literal is a string"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders WHERE note = 'SELECT ssn FROM secrets'"
+ :tables [[nil nil "orders"]]}
+
+ {:name "a trailing comma the engine would refuse still names its table"
+ :dialect "clickhouse"
+ :sql "SELECT id, FROM customers"
+ :tables [[nil nil "customers"]]}]}
diff --git a/test/metabase/sql_parsing/test_cases/replace_names.edn b/test/metabase/sql_parsing/test_cases/replace_names.edn
new file mode 100644
index 00000000000..2e6679549ef
--- /dev/null
+++ b/test/metabase/sql_parsing/test_cases/replace_names.edn
@@ -0,0 +1,102 @@
+{:cases
+ [{:name "a table is replaced wherever the query names it"
+ :dialect "postgres"
+ :sql "SELECT orders.id FROM orders WHERE orders.total > 0"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]]}
+ :expected "SELECT tmp_orders.id FROM tmp_orders WHERE tmp_orders.total > 0"}
+
+ {:name "several tables in one call"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders o JOIN people p ON o.user_id = p.id"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]
+ [{:table "people"} {:db nil :schema nil :table "tmp_people"}]]}
+ :expected "SELECT * FROM tmp_orders AS o JOIN tmp_people AS p ON o.user_id = p.id"}
+
+ {:name "a qualified reference loses its schema when the replacement has none"
+ :dialect "postgres"
+ :sql "SELECT * FROM public.orders"
+ :replacements {:tables [[{:schema "public" :table "orders"} {:db nil :schema nil :table "tmp_orders"}]]}
+ :expected "SELECT * FROM tmp_orders"}
+
+ {:name "a reference written in another case is still replaced"
+ :dialect "postgres"
+ :sql "SELECT * FROM ORDERS"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]]}
+ :expected "SELECT * FROM tmp_orders"}
+
+ {:name "a CTE named after a replaced table keeps its own name"
+ :dialect "postgres"
+ :sql "WITH orders AS (SELECT 1 AS id) SELECT * FROM orders, people"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]
+ [{:table "people"} {:db nil :schema nil :table "tmp_people"}]]}
+ :expected "WITH orders AS (SELECT 1 AS id) SELECT * FROM orders, tmp_people"}
+
+ {:name "an alias named after a replaced table keeps its own name"
+ :dialect "postgres"
+ :sql "SELECT people.id FROM orders AS people"
+ :replacements {:tables [[{:table "people"} {:db nil :schema nil :table "tmp_people"}]]}
+ :expected "SELECT people.id FROM orders AS people"}
+
+ {:name "both arms of a union are replaced"
+ :dialect "postgres"
+ :sql "SELECT id FROM orders UNION ALL SELECT id FROM people"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]
+ [{:table "people"} {:db nil :schema nil :table "tmp_people"}]]}
+ :expected "SELECT id FROM tmp_orders UNION ALL SELECT id FROM tmp_people"}
+
+ {:name "a table buried in a subquery is replaced"
+ :dialect "postgres"
+ :sql "SELECT * FROM (SELECT * FROM (SELECT id FROM orders) a) b"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]]}
+ :expected "SELECT * FROM (SELECT * FROM (SELECT id FROM tmp_orders) AS a) AS b"}
+
+ {:name "a name in a string literal is left alone"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders WHERE note = 'orders'"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]]}
+ :expected "SELECT * FROM tmp_orders WHERE note = 'orders'"}
+
+ {:name "a table with no replacement is left alone"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders JOIN secrets ON TRUE"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]]}
+ :expected "SELECT * FROM tmp_orders JOIN secrets ON TRUE"}
+
+ {:name "a DML target is replaced when a replacement names it"
+ :dialect "postgres"
+ :sql "DELETE FROM people WHERE id IN (SELECT id FROM orders)"
+ :replacements {:tables [[{:table "people"} {:db nil :schema nil :table "tmp_people"}]
+ [{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]]}
+ :expected "DELETE FROM tmp_people WHERE id IN (SELECT id FROM tmp_orders)"}
+
+ {:name "a DML target with no replacement stays the real table"
+ :dialect "postgres"
+ :sql "DELETE FROM people WHERE id IN (SELECT id FROM orders)"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]]}
+ :expected "DELETE FROM people WHERE id IN (SELECT id FROM tmp_orders)"}
+
+ {:name "every statement is rewritten, not only the first"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders; DELETE FROM people"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]
+ [{:table "people"} {:db nil :schema nil :table "tmp_people"}]]}
+ :expected "SELECT * FROM tmp_orders; DELETE FROM tmp_people"}
+
+ {:name "a MERGE has both its tables replaced"
+ :dialect "postgres"
+ :sql "MERGE INTO people tt USING orders ss ON tt.id = ss.id WHEN MATCHED THEN UPDATE SET total = ss.total"
+ :replacements {:tables [[{:table "people"} {:db nil :schema nil :table "tmp_people"}]
+ [{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]]}
+ :expected "MERGE INTO tmp_people AS tt USING tmp_orders AS ss ON tt.id = ss.id WHEN MATCHED THEN UPDATE SET total = ss.total"}
+
+ {:name "a line comment is rewritten into a block comment"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders -- keep me"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]]}
+ :expected "SELECT * FROM tmp_orders /* keep me */"}
+
+ {:name "invalid SQL the parser reads is printed back valid"
+ :dialect "postgres"
+ :sql "SELECT id, FROM orders"
+ :replacements {:tables [[{:table "orders"} {:db nil :schema nil :table "tmp_orders"}]]}
+ :expected "SELECT id FROM tmp_orders"}]}
diff --git a/test/metabase/sql_parsing/test_cases/returned_columns.edn b/test/metabase/sql_parsing/test_cases/returned_columns.edn
new file mode 100644
index 00000000000..1e5641b1117
--- /dev/null
+++ b/test/metabase/sql_parsing/test_cases/returned_columns.edn
@@ -0,0 +1,48 @@
+{:cases
+ [{:name "a select's own columns"
+ :dialect "postgres"
+ :sql "SELECT id, name FROM a"
+ :default-schema "public"
+ :columns ["id" "name"]}
+
+ {:name "a union returns the columns both arms agree on"
+ :dialect "postgres"
+ :sql "SELECT id, name FROM a UNION SELECT id, name FROM b"
+ :default-schema "public"
+ :columns ["id" "name"]}
+
+ {:name "union all, likewise"
+ :dialect "snowflake"
+ :sql "SELECT id, name FROM a UNION ALL SELECT id, name FROM b"
+ :default-schema "public"
+ :columns ["id" "name"]}
+
+ {:name "intersect returns the one column it selects"
+ :dialect "postgres"
+ :sql "SELECT id FROM a INTERSECT SELECT id FROM b"
+ :default-schema "public"
+ :columns ["id"]}
+
+ {:name "except, likewise"
+ :dialect "postgres"
+ :sql "SELECT id FROM a EXCEPT SELECT id FROM b"
+ :default-schema "public"
+ :columns ["id"]}
+
+ {:name "a union nested in a derived table"
+ :dialect "postgres"
+ :sql "SELECT * FROM (SELECT id FROM a UNION SELECT id FROM b) AS combined"
+ :default-schema "public"
+ :columns ["id"]}
+
+ {:name "a union inside a CTE"
+ :dialect "postgres"
+ :sql "WITH u AS (SELECT id FROM a UNION SELECT id FROM b) SELECT * FROM u"
+ :default-schema "public"
+ :columns ["id"]}
+
+ {:name "an aliased expression is returned under its alias"
+ :dialect "postgres"
+ :sql "SELECT count(*) AS n, max(total) AS biggest FROM orders"
+ :default-schema "public"
+ :columns ["biggest" "n"]}]}
diff --git a/test/metabase/sql_parsing/test_cases/single_stmt.edn b/test/metabase/sql_parsing/test_cases/single_stmt.edn
new file mode 100644
index 00000000000..3d2166e5c08
--- /dev/null
+++ b/test/metabase/sql_parsing/test_cases/single_stmt.edn
@@ -0,0 +1,42 @@
+{:cases
+ [{:name "one select is a single read"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders"
+ :stmt-type "read"
+ :expected {:is-single-stmt? true :allowed-stmt-type? true}}
+
+ {:name "two statements are not a single statement"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders; DELETE FROM people"
+ :stmt-type "read"
+ :expected {:is-single-stmt? false :allowed-stmt-type? false}}
+
+ {:name "a trailing semicolon does not make a second statement"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders;"
+ :stmt-type "read"
+ :expected {:is-single-stmt? true :allowed-stmt-type? true}}
+
+ {:name "a semicolon inside a literal is not a statement boundary"
+ :dialect "postgres"
+ :sql "SELECT * FROM orders WHERE note = 'a; b'"
+ :stmt-type "read"
+ :expected {:is-single-stmt? true :allowed-stmt-type? true}}
+
+ {:name "a write is a single statement, but not a read"
+ :dialect "postgres"
+ :sql "DELETE FROM people WHERE id = 1"
+ :stmt-type "read"
+ :expected {:is-single-stmt? true :allowed-stmt-type? false}}
+
+ {:name "the same write, asked for as a write"
+ :dialect "postgres"
+ :sql "DELETE FROM people WHERE id = 1"
+ :stmt-type "write"
+ :expected {:is-single-stmt? true :allowed-stmt-type? true}}
+
+ {:name "a CTE ahead of a select is still one read"
+ :dialect "postgres"
+ :sql "WITH c AS (SELECT id FROM people) SELECT * FROM c"
+ :stmt-type "read"
+ :expected {:is-single-stmt? true :allowed-stmt-type? true}}]}
diff --git a/test/metabase/sql_parsing/test_cases/validate_query.edn b/test/metabase/sql_parsing/test_cases/validate_query.edn
new file mode 100644
index 00000000000..5aaad5f9d21
--- /dev/null
+++ b/test/metabase/sql_parsing/test_cases/validate_query.edn
@@ -0,0 +1,71 @@
+{:schemas
+ {:products {"PUBLIC" {"PRODUCTS" {"ID" "INT" "TITLE" "VARCHAR" "CATEGORY" "VARCHAR" "PRICE" "DECIMAL"}
+ "ORDERS" {"ID" "INT" "USER_ID" "INT" "PRODUCT_ID" "INT" "TOTAL" "DECIMAL"}
+ "USERS" {"ID" "INT" "NAME" "VARCHAR" "EMAIL" "VARCHAR"}}}}
+
+ :cases
+ [{:name "columns that exist"
+ :sql "SELECT id, title FROM products"
+ :default-schema "PUBLIC"
+ :schema :products
+ :expected {:status "ok"}}
+
+ {:name "a wildcard"
+ :sql "SELECT * FROM products"
+ :default-schema "PUBLIC"
+ :schema :products
+ :expected {:status "ok"}}
+
+ {:name "table-qualified columns"
+ :sql "SELECT products.id, products.title FROM products"
+ :default-schema "PUBLIC"
+ :schema :products
+ :expected {:status "ok"}}
+
+ {:name "a join whose columns all exist"
+ :sql "SELECT o.id, p.title FROM orders o JOIN products p ON o.product_id = p.id"
+ :default-schema "PUBLIC"
+ :schema :products
+ :expected {:status "ok"}}
+
+ {:name "a subquery"
+ :sql "SELECT * FROM (SELECT id, title FROM products) AS sub"
+ :default-schema "PUBLIC"
+ :schema :products
+ :expected {:status "ok"}}
+
+ {:name "a union"
+ :sql "SELECT id FROM products UNION SELECT id FROM orders"
+ :default-schema "PUBLIC"
+ :schema :products
+ :expected {:status "ok"}}
+
+ {:name "a column no table has"
+ :sql "SELECT bad_column FROM products"
+ :default-schema "PUBLIC"
+ :schema :products
+ :expected {:status "error" :type "column_not_resolved" :column "bad_column"}}
+
+ {:name "a column belonging to another table"
+ :sql "SELECT email FROM products"
+ :default-schema "PUBLIC"
+ :schema :products
+ :expected {:status "error" :type "column_not_resolved"}}
+
+ {:name "a qualifier naming no source"
+ :sql "SELECT p.id FROM products"
+ :default-schema "PUBLIC"
+ :schema :products
+ :expected {:status "error"}}
+
+ {:name "a syntax error"
+ :sql "SELECT * FORM products"
+ :default-schema "PUBLIC"
+ :schema :products
+ :expected {:status "error" :type "invalid_expression"}}
+
+ {:name "a table the schema does not have is left to the layer that knows the database"
+ :sql "SELECT * FROM nonexistent"
+ :default-schema "PUBLIC"
+ :schema :products
+ :expected {:status "ok"}}]}
diff --git a/test/metabase/sql_tools/core_test.clj b/test/metabase/sql_tools/core_test.clj
index 47fdcdf1afd..14d83f58740 100644
--- a/test/metabase/sql_tools/core_test.clj
+++ b/test/metabase/sql_tools/core_test.clj
@@ -10,6 +10,7 @@
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.query-processor.compile :as qp.compile]
+ [metabase.sql-parsing.core :as sql-parsing]
[metabase.sql-tools.core :as sql-tools]
[metabase.sql-tools.settings :as sql-tools.settings]
[metabase.sql-tools.test-util :as sql-tools.tu]
@@ -115,6 +116,16 @@
(is (= [{:schema "public" :table "orders"}]
(sql-tools/referenced-tables-raw :postgres "SELECT * FROM public.orders"))))))
+(deftest ^:parallel referenced-tables-raw-strict-parse-error-test
+ (binding [sql-tools.settings/*parser-backend-override* :sqlglot]
+ (testing "ordinary callers retain the fail-soft behavior"
+ (is (= [] (sql-tools/referenced-tables-raw :postgres "SELECT !!!"))))
+ (testing "Guard A can distinguish a parse failure from a query with no table reads"
+ (let [e (try
+ (sql-tools/referenced-tables-raw :postgres "SELECT !!!" {:fail-on-parse-error? true})
+ (catch Exception e e))]
+ (is (sql-parsing/parse-error? e))))))
+
;;; -------------------------------------------- transpile-sql ---------------------------------------------
;; transpile-sql is only implemented for the :sqlglot backend, so these tests bind it directly
;; rather than using test-parser-backends.
diff --git a/test/metabase/sql_tools/macaw/core_test.clj b/test/metabase/sql_tools/macaw/core_test.clj
index 134247ce372..b66443db9e6 100644
--- a/test/metabase/sql_tools/macaw/core_test.clj
+++ b/test/metabase/sql_tools/macaw/core_test.clj
@@ -28,12 +28,13 @@
(testing "Macaw referenced-tables-raw splits BigQuery-style dotted table names"
(is (= [{:schema "analytics" :table "events"}]
(sql-tools/referenced-tables-raw-impl :macaw :bigquery-cloud-sdk
- "SELECT * FROM analytics.events")))
+ "SELECT * FROM analytics.events" {})))
(is (= #{{:schema "analytics" :table "events"}
{:schema "analytics" :table "users"}}
(set (sql-tools/referenced-tables-raw-impl :macaw :bigquery-cloud-sdk
- "SELECT * FROM analytics.events JOIN analytics.users ON events.user_id = users.id")))))
+ "SELECT * FROM analytics.events JOIN analytics.users ON events.user_id = users.id"
+ {})))))
(testing "simple table names are unaffected"
(is (= [{:table "orders"}]
(sql-tools/referenced-tables-raw-impl :macaw :postgres
- "SELECT * FROM orders")))))
+ "SELECT * FROM orders" {})))))
diff --git a/test/metabase/sql_tools/sqlglot/replace_names_test.clj b/test/metabase/sql_tools/sqlglot/replace_names_test.clj
index 137a0794fda..cd56e1d7eef 100644
--- a/test/metabase/sql_tools/sqlglot/replace_names_test.clj
+++ b/test/metabase/sql_tools/sqlglot/replace_names_test.clj
@@ -88,18 +88,116 @@
"SELECT * FROM (SELECT id FROM people) sub"
{:tables {{:table "people"} "users"}})))))
-(deftest ^:parallel case-sensitive-match-test
- (testing "Case-sensitive matching - must match exact case"
- ;; Lower case matches lower case
+(deftest ^:parallel cte-shadows-a-table-test
+ (testing "a bare reference to a CTE names the CTE, not the table the key renames"
+ (is (= "WITH people AS (SELECT 1 AS id) SELECT id FROM people"
+ (replace-names :postgres
+ "WITH people AS (SELECT 1 AS id) SELECT id FROM people"
+ {:tables {{:table "people"} "users"}}))))
+ (testing "a real table of another name is still renamed alongside the CTE"
+ (is (= "WITH other AS (SELECT 1 AS id) SELECT id FROM users"
+ (replace-names :postgres
+ "WITH other AS (SELECT 1 AS id) SELECT id FROM people"
+ {:tables {{:table "people"} "users"}}))))
+ (testing "a qualified reference names a real table, which a CTE cannot shadow"
+ (is (= "WITH people AS (SELECT 1 AS id) SELECT id FROM public.users"
+ (replace-names :postgres
+ "WITH people AS (SELECT 1 AS id) SELECT id FROM public.people"
+ {:tables {{:schema "public" :table "people"} "users"}})))))
+
+(deftest ^:parallel cte-shadows-a-table-in-any-case-test
+ (testing "a reference to a CTE names the CTE however either is cased"
+ (is (= "WITH orders AS (SELECT 1 AS id) SELECT * FROM ORDERS"
+ (replace-names :postgres
+ "WITH orders AS (SELECT 1 AS id) SELECT * FROM ORDERS"
+ {:tables {{:table "orders"} "users"}}))))
+ (testing "a CTE shadows only inside the query that declares it"
+ ;; The outer `orders` is the real table: the CTE belongs to the subquery, and renaming stops at its edge.
+ (is (= (str "SELECT * FROM users UNION ALL "
+ "SELECT * FROM (WITH orders AS (SELECT 1 AS id) SELECT id FROM orders) AS t")
+ (replace-names :postgres
+ (str "SELECT * FROM orders UNION ALL "
+ "SELECT * FROM (WITH orders AS (SELECT 1 AS id) SELECT id FROM orders) t")
+ {:tables {{:table "orders"} "users"}})))))
+
+(deftest ^:parallel alias-shadows-a-qualifier-in-any-case-test
+ (testing "a column qualifier naming a FROM alias is left alone however either is cased"
+ (is (= "SELECT People.id FROM users AS PEOPLE"
+ (replace-names :postgres
+ "SELECT People.id FROM people PEOPLE"
+ {:tables {{:table "people"} "users"}})))))
+
+(deftest ^:parallel quoting-is-judged-per-part-test
+ (testing "a quoted schema leaves the unquoted table free to match in any case"
+ (is (= "SELECT * FROM \"public\".users"
+ (replace-names :postgres
+ "SELECT * FROM \"public\".ORDERS"
+ {:tables {{:schema "public" :table "orders"} "users"}})))))
+
+(deftest ^:parallel cte-does-not-shadow-its-own-body-test
+ (testing "a CTE reading the table it is named after reads the real table"
+ (is (= "WITH orders AS (SELECT * FROM users) SELECT * FROM orders"
+ (replace-names :postgres
+ "WITH orders AS (SELECT * FROM orders) SELECT * FROM orders"
+ {:tables {{:table "orders"} "users"}})))))
+
+(deftest ^:parallel quoted-reference-is-case-significant-test
+ (testing "a quoted reference matches only a key of the same case: the engine reads it literally"
+ (is (= "SELECT * FROM \"Orders\""
+ (replace-names :postgres
+ "SELECT * FROM \"Orders\""
+ {:tables {{:table "orders"} "users"}}))))
+ (testing "and still matches a key spelled the same way"
+ (is (= "SELECT * FROM \"users\""
+ (replace-names :postgres
+ "SELECT * FROM \"Orders\""
+ {:tables {{:table "Orders"} "users"}})))))
+
+(deftest ^:parallel case-agnostic-match-test
+ (testing "a key matches a reference written in any case"
+ ;; Metabase treats every database as case-agnostic (see `macaw-options`): unquoted identifiers are
+ ;; case-insensitive per SQL-92, and where they are not (MySQL, SQL Server) it depends on the file
+ ;; system or collation rather than on the query. A key that only matched one spelling would leave
+ ;; the other pointing at the real table.
(is (= "SELECT * FROM users"
(replace-names :postgres
"SELECT * FROM people"
{:tables {{:table "people"} "users"}})))
- ;; Upper case needs upper case key
(is (= "SELECT * FROM users"
(replace-names :postgres
"SELECT * FROM PEOPLE"
- {:tables {{:table "PEOPLE"} "users"}})))))
+ {:tables {{:table "people"} "users"}})))
+ (is (= "SELECT * FROM users"
+ (replace-names :postgres
+ "SELECT * FROM people"
+ {:tables {{:table "PEOPLE"} "users"}}))))
+ (testing "the schema may differ in case too"
+ ;; A string replacement renames the table and leaves the schema where it was, whatever its case.
+ (is (= "SELECT * FROM PUBLIC.users"
+ (replace-names :postgres
+ "SELECT * FROM PUBLIC.PEOPLE"
+ {:tables {{:schema "public" :table "people"} "users"}})))
+ (is (= "SELECT * FROM public.users"
+ (replace-names :postgres
+ "SELECT * FROM public.people"
+ {:tables {{:schema "public" :table "people"} "users"}}))))
+ (testing "a key matching as written wins over one matching only by case"
+ (is (= "SELECT * FROM exact"
+ (replace-names :postgres
+ "SELECT * FROM people"
+ {:tables {{:table "people"} "exact"
+ {:table "PEOPLE"} "folded"}}))))
+ (testing "keys differing only in case answer nothing, rather than one of them arbitrarily"
+ (is (= "SELECT * FROM People"
+ (replace-names :postgres
+ "SELECT * FROM People"
+ {:tables {{:table "people"} "one"
+ {:table "PEOPLE"} "two"}}))))
+ (testing "a column key matches its column and qualifier in any case"
+ (is (= "SELECT user_id FROM people"
+ (replace-names :postgres
+ "SELECT ID FROM people"
+ {:columns {{:table "PEOPLE" :column "id"} "user_id"}})))))
(deftest ^:parallel table-rename-with-schema-map-value-test
(testing "Table rename using map value with schema and table (schema relocation pattern)"
@@ -136,3 +234,37 @@
(replace-names :clickhouse
"SELECT * FROM `zz`.`src` WHERE `id` = {uid: UInt32}"
{:tables {{:schema "zz" :table "src"} "iso__src"}})))))
+
+(deftest ^:parallel table-qualified-column-follows-table-rename-test
+ (testing "A column qualified by a replaced table is qualified by its replacement"
+ (is (= "SELECT users.id, users.name FROM users"
+ (replace-names :postgres
+ "SELECT people.id, people.name FROM people"
+ {:tables {{:table "people"} "users"}})))))
+
+(deftest ^:parallel schema-qualified-column-follows-table-rename-test
+ (testing "A schema-qualified column loses the schema along with its table"
+ (is (= "SELECT tmp_people.id FROM tmp_people WHERE tmp_people.name IS NULL"
+ (replace-names :postgres
+ "SELECT public.people.id FROM public.people WHERE public.people.name IS NULL"
+ {:tables {{:schema "public" :table "people"} {:schema nil :table "tmp_people"}}}))))
+ (testing "Quoted qualifiers stay quoted"
+ (is (= "SELECT \"tmp_people\".\"ID\" FROM \"tmp_people\""
+ (replace-names :postgres
+ "SELECT \"PUBLIC\".\"PEOPLE\".\"ID\" FROM \"PUBLIC\".\"PEOPLE\""
+ {:tables {{:schema "PUBLIC" :table "PEOPLE"} {:schema nil :table "tmp_people"}}})))))
+
+(deftest ^:parallel alias-qualified-column-is-not-renamed-test
+ (testing "A qualifier naming an alias is left alone, even when a replaced table has the same name"
+ (is (= "SELECT people.id FROM orders AS people"
+ (replace-names :postgres
+ "SELECT people.id FROM orders AS people"
+ {:tables {{:table "people"} "users"}})))))
+
+(deftest ^:parallel qualified-column-rename-with-table-rename-test
+ (testing "A column rename still applies when its table qualifier is renamed"
+ (is (= "SELECT transactions.amount FROM transactions"
+ (replace-names :postgres
+ "SELECT orders.total FROM orders"
+ {:tables {{:table "orders"} "transactions"}
+ :columns {{:table "orders" :column "total"} "amount"}})))))
diff --git a/test/metabase/test/util.clj b/test/metabase/test/util.clj
index 3640e024a83..1e45d7cfe11 100644
--- a/test/metabase/test/util.clj
+++ b/test/metabase/test/util.clj
@@ -406,6 +406,14 @@
:schedule "0 0 * * * ?"
:ui_display_type :cron/raw}))
+ :model/TransformTest
+ (fn [_]
+ (default-timestamped
+ {:creator_id (rasta-id)
+ :name (str "Test Transform Test " (u/generate-nano-id))
+ :inputs []
+ :expectations []}))
+
:model/TransformRun
(fn [_]
{:status "succeeded"
diff --git a/test/metabase/transforms/test_util.clj b/test/metabase/transforms/test_util.clj
index 60c91fe4d6c..67ea47db92d 100644
--- a/test/metabase/transforms/test_util.clj
+++ b/test/metabase/transforms/test_util.clj
@@ -68,7 +68,7 @@
;; assume this is just a plain table name
{:type :table, :name target})
(and (nil? (:schema target)) (isa? driver/hierarchy driver/*driver* :sql))
- (assoc :schema (driver.sql/default-schema driver)))]
+ (assoc :schema (driver.sql/default-schema driver/*driver* (mt/db))))]
(binding [api/*is-superuser?* true
api/*current-user-id* (mt/user->id :crowberto)]
;; Drop the actual table/view from the database
@@ -252,7 +252,7 @@
Useful for tests that need to create tables with a schema matching transform targets."
[]
(or (when (get-method driver.sql/default-schema driver/*driver*)
- (driver.sql/default-schema driver/*driver*))
+ (driver.sql/default-schema driver/*driver* (mt/db)))
(if (= :bigquery-cloud-sdk driver/*driver*)
(t2/select-one-fn :schema :model/Table :db_id (mt/id))
"public")))
diff --git a/test/metabase/transforms_base/ordering_test.clj b/test/metabase/transforms_base/ordering_test.clj
index 08439cc8dd4..36669ab54a1 100644
--- a/test/metabase/transforms_base/ordering_test.clj
+++ b/test/metabase/transforms_base/ordering_test.clj
@@ -13,7 +13,7 @@
(defn- default-schema-or-public [& [fallback-driver]]
(let [driver (or driver/*driver* fallback-driver)]
- (or (and driver (driver.sql/default-schema driver)) "public")))
+ (or (and driver (driver.sql/default-schema driver (mt/db))) "public")))
(defn- make-transform [query & [name schema]]
(let [name (or name (mt/random-name))
diff --git a/test/metabase/util/honey_sql_2_test.clj b/test/metabase/util/honey_sql_2_test.clj
index ce253c485b0..4a838b809c4 100644
--- a/test/metabase/util/honey_sql_2_test.clj
+++ b/test/metabase/util/honey_sql_2_test.clj
@@ -153,6 +153,10 @@
(testing "a sane bare type-name token is emitted raw, since most dialects reject a quoted type name in a CAST"
(is (= ["SELECT CAST(? AS date)" "toucan"]
(sql/format {:select [[(h2x/cast "date" "toucan")]]} {:quoted true, :dialect :ansi}))))
+ (testing "a type name carrying a quoted argument is emitted raw, which is the only form ClickHouse accepts"
+ (is (= ["SELECT CAST(? AS Nullable(DateTime64(3, 'GMT0')))" "toucan"]
+ (sql/format {:select [[(h2x/cast "Nullable(DateTime64(3, 'GMT0'))" "toucan")]]}
+ {:quoted true, :dialect :ansi}))))
(testing "a type name that isn't a bare token is quoted, so a hostile type cannot inject SQL"
(is (= ["SELECT CAST(? AS \"date) UNION SELECT 1 --\")" "toucan"]
(sql/format {:select [[(h2x/cast "date) UNION SELECT 1 --" "toucan")]]}
@@ -168,7 +172,69 @@
false "integer); select 1 --"
false "\"quoted\""
false "schema.type"
- false "decimal(10, 2); --"))
+ false "decimal(10, 2); --")
+ (testing "a type whose argument is itself a type, as ClickHouse writes them"
+ (are [sql-type] (h2x/raw-type-name? sql-type)
+ "Nullable(String)"
+ "LowCardinality(Nullable(String))"
+ "Map(String, Nullable(Int32))"
+ "Variant(Decimal(38, 19), Float64)"
+ "Array(Nullable(Tuple(String, Int32)))"))
+ (testing "a quoted argument, which is how ClickHouse writes the timezone of a timestamp column"
+ (are [sql-type] (h2x/raw-type-name? sql-type)
+ "DateTime64(3, 'GMT0')"
+ "Nullable(DateTime64(9, 'UTC'))"
+ "DateTime('Europe/Amsterdam')"
+ "Nullable('a')"
+ "T('(')"))
+ (testing "an argument list nested deeper than the rule goes is quoted rather than spliced"
+ (is (h2x/raw-type-name? "A(B(C(D(String))))"))
+ (is (not (h2x/raw-type-name? "A(B(C(D(E(String)))))"))))
+ (testing "an array suffix, and a type name that continues after its precision"
+ (are [sql-type] (h2x/raw-type-name? sql-type)
+ "int[]"
+ "varchar(50)[]"
+ "TIMESTAMP(6) WITH TIME ZONE"))
+ (testing "nothing that could end the expression the type is spliced into"
+ (are [sql-type] (not (h2x/raw-type-name? sql-type))
+ ;; Postgres splices `(…)::«type»` into a select list, where a top-level comma would add a column
+ "text, (SELECT secret FROM users)"
+ ;; a parenthesis this name did not open
+ "int) UNION SELECT 1 --"
+ "(int)"
+ "Nullable(String"
+ "date' AND 1 = 1"
+ "T('a') 'b'"
+ "DateTime64(3, 'GMT0)"
+ "DateTime64(3, 'GM\\'T0')"
+ "T('(')) , (SELECT secret FROM users"
+ "T('((')) , 1"
+ ;; comment markers stay out whatever the parentheses do
+ "int/*x*/"
+ "int-1"
+ ;; `[]` is an array suffix, not a subscript
+ "int[1]"
+ "int]"
+ ""
+ "1int")))
+
+(deftest ^:parallel cast-contains-a-hostile-type-name-test
+ (testing "a name made only of type-name characters cannot reach past the CAST it sits in"
+ ;; Type names are words, so words are all a type name can be made of: `bigint` and
+ ;; `UNION SELECT` are the same shape. What keeps that inert is the parentheses it is spliced
+ ;; inside — it can add no parenthesis of its own that closes one it did not open.
+ (are [sql-type] (= [(str "SELECT CAST(\"x\" AS " sql-type ")")]
+ (sql/format {:select [[(h2x/cast sql-type :x)]]} {:quoted true, :dialect :ansi}))
+ "bigint UNION SELECT secret FROM users"
+ "Map(String, Nullable(Int32))"
+ "T('(') UNION ALL SELECT password FROM users"
+ "Nullable(DateTime64(3, 'GMT0'))"
+ "int[]"))
+ (testing "a type name that would close the CAST early is quoted as an identifier instead"
+ (are [sql-type] (= [(str "SELECT CAST(\"x\" AS \"" sql-type "\")")]
+ (sql/format {:select [[(h2x/cast sql-type :x)]]} {:quoted true, :dialect :ansi}))
+ "T('(')) , (SELECT secret FROM users"
+ "date' AND 1 = 1")))
(defn- ->sql [expr]
(sql/format {:select [[expr]]} {:quoted false}))