The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Deprecate legacy MBQL utils (#64595)
Cam Saul8ad785fdbb41
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.ednindex 0a0d847f10a..f356a2e396e 100644--- a/.clj-kondo/config.edn+++ b/.clj-kondo/config.edn@@ -25,6 +25,7 @@:reduce-without-init {:level :warning}:redundant-call {:level :warning}:redundant-fn-wrapper {:level :warning}+ :redundant-str-call {:level :warning}:self-requiring-namespace {:level :warning}:single-key-in {:level :warning}:unsorted-imports {:level :warning}@@ -717,6 +718,8 @@:name metabase-lib}{:pattern "^metabase\\.lib\\..*":name metabase-lib-excluding-legacy-mbql}+ {:pattern "^metabase\\.legacy-mbql\\..*"+ :name legacy-mbql-namespaces};;;; QP and driver source namespaces: `metabase.query-processor.*`, `metabase-enterprise.sandbox.query-processor.*`,;; and `metabase.driver.*`, excluding `-test` namespaces.@@ -731,7 +734,7 @@;;;; driver namespaces, including tests;;- {:pattern "metabase\\.driver.*$"+ {:pattern "metabase\\.driver(?:-api)?.*$":name driver-namespaces};;;; Perf-sensitive namespaces are currently the union of metabase.lib.*, driver namespaces, and QP (without tests).@@ -777,7 +780,14 @@clojure.core/eval {:message "Don't use eval outside of tests"}}:discouraged-namespace- {metabase.query-processor.store {:message "Please do not use the QP Store outside of legacy QP or driver code. Use MBQL 5/Lib or lib.metadata.jvm/applicaton-database-metadata-provider instead."}}}}+ {metabase.legacy-mbql.jvm-util {:message "Avoid legacy MBQL utils, use Lib instead"}+ metabase.legacy-mbql.normalize {:message "Avoid legacy MBQL normalization, use MBQL 5 + lib/normalize instead"}+ metabase.legacy-mbql.predicates {:message "Avoid legacy MBQL utils, use Lib instead"}+ metabase.legacy-mbql.schema {:message "Avoid the legacy MBQL schema, use MBQL 5 + lib.schema instead"}+ metabase.legacy-mbql.schema.helpers {:message "Avoid the legacy MBQL schema, use MBQL 5 + lib.schema instead"}+ metabase.legacy-mbql.schema.macros {:message "Avoid the legacy MBQL schema, use MBQL 5 + lib.schema instead"}+ metabase.legacy-mbql.util {:message "Avoid legacy MBQL utils, use Lib instead"}+ metabase.query-processor.store {:message "Please do not use the QP Store outside of legacy QP or driver code. Use MBQL 5/Lib or lib.metadata.jvm/applicaton-database-metadata-provider instead."}}}}printable-namespaces{:linters@@ -819,20 +829,70 @@toucan2.core {:message "Don't use application database inside Lib code"}}}}metabase-lib-excluding-legacy-mbql- {:linters- {:discouraged-var- {metabase.legacy-mbql.util/unique-name-generator {:message "Use metabase.lib.util/unique-name-generator or non-truncating-unique-name-generator"}}}+ {}- :discouraged-namespace- {metabase.legacy-mbql.js {:message "Avoid legacy MBQL utils in Lib; move things into Lib if needded."}- metabase.legacy-mbql.jvm-util {:message "Avoid legacy MBQL utils in Lib; move things into Lib if needded."}- metabase.legacy-mbql.predicates {:message "Avoid legacy MBQL utils in Lib; move things into Lib if needded."}- metabase.legacy-mbql.util {:message "Avoid legacy MBQL utils in Lib; move things into Lib if needded."}}}+ legacy-mbql-namespaces+ {:linters+ {:discouraged-namespace+ {metabase.legacy-mbql.jvm-util {:level :off}+ metabase.legacy-mbql.normalize {:level :off}+ metabase.legacy-mbql.predicates {:level :off}+ metabase.legacy-mbql.schema {:level :off}+ metabase.legacy-mbql.schema.helpers {:level :off}+ metabase.legacy-mbql.schema.macros {:level :off}+ metabase.legacy-mbql.util {:level :off}+ metabase.query-processor.store {:level :off}}++ ;; the `metabase.legacy-mbql` stuff is all deprecated, so we don't need to warn inside the module about other stuff+ ;; in the module being marked deprecated or discouraged.+ :deprecated-namespace {:level :off}+ :deprecated-var {:level :off}}}driver-namespaces{:linters{:discouraged-var- {metabase.driver/database-supports? {:level :off}}}}+ {metabase.driver/database-supports? {:level :off}}++ ;; don't warn about the legacy MBQL namespaces being deprecated or discouraged in driver code now, we don't have a+ ;; real alternative YET so it's not a helpful warning.+ :discouraged-namespace+ {metabase.legacy-mbql.jvm-util {:level :off}+ metabase.legacy-mbql.normalize {:level :off}+ metabase.legacy-mbql.predicates {:level :off}+ metabase.legacy-mbql.schema {:level :off}+ metabase.legacy-mbql.schema.helpers {:level :off}+ metabase.legacy-mbql.schema.macros {:level :off}+ metabase.legacy-mbql.util {:level :off}+ metabase.query-processor.store {:level :off}}++ :deprecated-namespace+ {:exclude+ [metabase.driver.common.parameters+ metabase.driver.common.parameters.dates+ metabase.driver.common.parameters.operators+ metabase.driver.common.parameters.parse+ metabase.driver.common.parameters.values+ metabase.legacy-mbql.jvm-util+ metabase.legacy-mbql.normalize+ metabase.legacy-mbql.predicates+ metabase.legacy-mbql.schema+ metabase.legacy-mbql.schema.helpers+ metabase.legacy-mbql.schema.macros+ metabase.legacy-mbql.util+ metabase.query-processor.store]}++ :deprecated-var+ {:exclude+ {metabase.legacy-mbql.schema.helpers/is-clause? {:namespaces ["metabase\\.driver.*"]}+ metabase.legacy-mbql.schema.helpers/mbql-clause? {:namespaces ["metabase\\.driver.*"]}+ metabase.legacy-mbql.util/query->source-table-id {:namespaces ["metabase\\.driver.*"]}+ metabase.legacy-mbql.util/aggregation-at-index {:namespaces ["metabase\\.driver.*"]}+ metabase.legacy-mbql.util/assoc-field-options {:namespaces ["metabase\\.driver.*"]}+ metabase.legacy-mbql.util/expression-with-name {:namespaces ["metabase\\.driver.*"]}+ metabase.legacy-mbql.util/field-options {:namespaces ["metabase\\.driver.*"]}+ metabase.legacy-mbql.util/negate-filter-clause {:namespaces ["metabase\\.driver.*"]}+ metabase.legacy-mbql.util/normalize-token {:namespaces ["metabase\\.driver.*"]}+ metabase.legacy-mbql.util/update-field-options {:namespaces ["metabase\\.driver.*"]}}}}}performance-sensitive{:linters@@ -867,16 +927,7 @@metabase.lib.core/->legacy-MBQL {:level :off}}}}qp-source-namespaces- {:linters- {:discouraged-namespace- {metabase.legacy-mbql.js {:message "Avoid legacy MBQL namespaces in the QP, use Lib instead"}- metabase.legacy-mbql.jvm-util {:message "Avoid legacy MBQL namespaces in the QP, use Lib instead"}- metabase.legacy-mbql.normalize {:message "Avoid legacy MBQL namespaces in the QP, use Lib instead"}- metabase.legacy-mbql.predicates {:message "Avoid legacy MBQL namespaces in the QP, use Lib instead"}- metabase.legacy-mbql.schema {:message "Avoid legacy MBQL namespaces in the QP, use Lib instead"}- metabase.legacy-mbql.schema.helpers {:message "Avoid legacy MBQL namespaces in the QP, use Lib instead"}- metabase.legacy-mbql.schema.macros {:message "Avoid legacy MBQL namespaces in the QP, use Lib instead"}- metabase.legacy-mbql.util {:message "Avoid legacy MBQL namespaces in the QP, use Lib instead"}}}}+ {}general-qp-and-driver-test-namespaces{:lintersdiff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 96be9193836..a05aa61e32d 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -119,9 +119,9 @@metabase.analyze.query-results}:uses #{configdriver- legacy-mbqllibmodels+ query-processorsyncutil}}@@ -594,7 +594,6 @@metabase.legacy-mbql.schemametabase.legacy-mbql.util}:uses #{lib- modelstypesutil}}@@ -709,8 +708,7 @@models{:team "DevEx"- :api #{metabase.models.dispatch- metabase.models.humanization+ :api #{metabase.models.humanizationmetabase.models.initmetabase.models.interfacemetabase.models.resolution@@ -783,7 +781,6 @@:uses #{apiapp-dbclassloader- legacy-mbqlliblib-bemodels@@ -921,7 +918,6 @@eid-translationembeddingevents- legacy-mbqlliblib-bemodels@@ -1323,7 +1319,6 @@collectionsdriverevents- legacy-mbqllibmodel-persistencemodelsdiff --git a/.github/workflows/claude-code-review.yaml b/.github/workflows/claude-code-review.yamlindex aacbfe6af93..e5ae1bc1a49 100644--- a/.github/workflows/claude-code-review.yaml+++ b/.github/workflows/claude-code-review.yaml@@ -2,18 +2,14 @@ name: "Claude Code Review"on:pull_request:- types: [opened, synchronize, reopened, labeled, unlabeled]concurrency:group: ${{ github.workflow }}-${{ github.head_ref && github.ref || github.run_id }}-claude-code-reviewcancel-in-progress: true-env:- EXCLUDED_PR_AUTHORS: '["kamilmielnik", "romeovs"]'-jobs:review:- if: github.event.pull_request.draft == false && !contains(fromJSON(env.EXCLUDED_PR_AUTHORS), github.event.pull_request.user.login) }}+ if: github.event.pull_request.draft == falseruns-on: ubuntu-latestpermissions:contents: readdiff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/dummy_tools.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/dummy_tools.cljindex 29de408b4ae..c7ba47cbe27 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/dummy_tools.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/dummy_tools.clj@@ -4,7 +4,8 @@[metabase-enterprise.documents.core :as documents][metabase-enterprise.metabot-v3.tools.util :as metabot-v3.tools.u][metabase.api.common :as api]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ;; TODO (Cam 10/10/25) -- update MetaBot to use Lib + MBQL 5+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata]diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/api.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/api.cljindex 1e045d86cc9..b33a2ed4555 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/api.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/api.clj@@ -23,7 +23,8 @@[metabase.api.macros :as api.macros][metabase.api.response :as api.response][metabase.api.routes.common :as api.routes.common]- [metabase.legacy-mbql.schema :as mbql.s]+ ;; TODO (Cam 10/10/25) -- update MetaBot to use Lib + MBQL 5+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s][metabase.lib.schema.temporal-bucketing :as lib.schema.temporal-bucketing][metabase.request.core :as request][metabase.util :as u]diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/models/sandbox.clj b/enterprise/backend/src/metabase_enterprise/sandbox/models/sandbox.cljindex 823374e043d..4a6da2f2a81 100644--- a/enterprise/backend/src/metabase_enterprise/sandbox/models/sandbox.clj+++ b/enterprise/backend/src/metabase_enterprise/sandbox/models/sandbox.clj@@ -8,14 +8,15 @@(:require[medley.core :as m][metabase.audit-app.core :as audit]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.schema :as mbql.s]+ ;; legacy usage -- do not use in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.schema.id :as lib.schema.id][metabase.lib.schema.metadata :as lib.schema.metadata][metabase.models.interface :as mi][metabase.permissions.core :as perms][metabase.premium-features.core :refer [defenterprise]][metabase.query-processor.error-type :as qp.error-type]+ [metabase.query-processor.schema :as qp.schema][metabase.request.core :as request][metabase.util :as u][metabase.util.i18n :refer [tru]]@@ -58,10 +59,10 @@from the original Table being sandboxed."[col :- [:or::lib.schema.metadata/column- ::mbql.s/legacy-column-metadata]+ ::qp.schema/result-metadata.column]table-col :- [:maybe [:or::lib.schema.metadata/column- ::mbql.s/legacy-column-metadata]]]+ ::qp.schema/result-metadata.column]]];; These errors might get triggered by API endpoints or by the QP (this code is used in the;; `sandboxing` middleware). So include `:type` and `:status-code` information in the ExceptionInfo;; data so it can be passed along if applicable.diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.cljindex b3c644846a6..543e83853fc 100644--- a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj+++ b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj@@ -214,9 +214,7 @@;; TODO (Cam 9/9/25) -- we should switch to saving Lib-style metadata in the app DB instead of legacy;; style in the near future(t2/update! :model/Card card-id {:result_metadata cols}))- (assoc-in query [:stages 0 :lib/stage-metadata] (->> cols- lib.util/->stage-metadata- (lib/normalize ::lib.schema.metadata/stage)))))))+ (lib/update-query-stage query 0 assoc :lib/stage-metadata (lib/->normalized-stage-metadata cols))))))query))(mu/defn- sandbox->query :- ::lib.schema/querydiff --git a/enterprise/backend/test/metabase_enterprise/cache/task/refresh_cache_configs_test.clj b/enterprise/backend/test/metabase_enterprise/cache/task/refresh_cache_configs_test.cljindex dfb5c96e75c..7bb3de10893 100644--- a/enterprise/backend/test/metabase_enterprise/cache/task/refresh_cache_configs_test.clj+++ b/enterprise/backend/test/metabase_enterprise/cache/task/refresh_cache_configs_test.clj@@ -106,16 +106,18 @@(testing "Given a card, we rerun a limited number of variations of the card's query"(binding [qp.util/*execute-async?* falsetask.cache/*run-cache-refresh-async* false]- (mt/with-temp [:model/Card {card-id :id} {:name "Cached card"+ (mt/with-temp [:model/Card {card-id :id} {:name "Cached card":dataset_query (parameterized-native-query)}](let [param-val-1 "2024-12-01"- params-1 [{:type :text+ params-1 [{:id "_DATE_"+ :type :text:target [:variable [:template-tag "date"]]- :value param-val-1}]+ :value param-val-1}]param-val-2 "2024-12-02"- params-2 [{:type :text+ params-2 [{:id "_DATE_"+ :type :text:target [:variable [:template-tag "date"]]- :value param-val-2}]+ :value param-val-2}]to-rerun #(@#'task.cache/scheduled-queries-to-rerun card-id (t/minus (t/offset-date-time) (t/minutes 10)))param-vals #(-> % :parameters first :value)];; Sanity check that the query actually runs@@ -240,21 +242,23 @@(testing "We refresh expired :duration caches for queries that were run at least once in the last caching duration"(binding [qp.util/*execute-async?* falsetask.cache/*run-cache-refresh-async* false]- (mt/with-temp [:model/Card {card-id :id} {:name "Cached card"+ (mt/with-temp [:model/Card {card-id :id} {:name "Cached card":dataset_query (parameterized-native-query)}- :model/CacheConfig _ {:model "question"- :model_id card-id- :strategy :duration+ :model/CacheConfig _ {:model "question"+ :model_id card-id+ :strategy :duration:refresh_automatically true- :config {:unit "hours" :duration 1}}]+ :config {:unit "hours" :duration 1}}](let [param-val-1 "2024-12-01"- params-1 [{:type :text+ params-1 [{:id "_DATE_"+ :type :text:target [:variable [:template-tag "date"]]- :value param-val-1}]+ :value param-val-1}]param-val-2 "2024-12-02"- params-2 [{:type :text+ params-2 [{:id "_DATE_"+ :type :text:target [:variable [:template-tag "date"]]- :value param-val-2}]+ :value param-val-2}]to-rerun (fn [card-id](let [queries (@#'task.cache/duration-queries-to-rerun)](filter #(= (:card-id %) card-id) queries)))@@ -518,7 +522,8 @@:result [[1000]]}{:label "parameterized native query":query (parameterized-native-query)- :parameters [{:type :text+ :parameters [{:id "_DATE_"+ :type :text:target [:variable [:template-tag "date"]]:value "2014-05-31"}]:result [[2]]}diff --git a/frontend/test/metabase-lib/lib/normalize.unit.spec.js b/frontend/test/metabase-lib/lib/normalize.unit.spec.jsdeleted file mode 100644index 47b34821647..00000000000--- a/frontend/test/metabase-lib/lib/normalize.unit.spec.js+++ /dev/null@@ -1,29 +0,0 @@-import { normalize } from "cljs/metabase.legacy_mbql.js";--// This test is here mostly to make sure the shared CLJS lib works correctly.--describe("normalize", () => {- it("should normalize an MBQL query", () => {- const query = {- DATABASE: 1000,- tYpE: "qUeRY",- QUeRY: {- source_TABLE: 200,- aggREGATION: ["count"],- filter: ["AND", ["=", 1, ["field-id", 2]]],- },- };-- const normalized = {- database: 1000,- type: "query",- query: {- "source-table": 200,- aggregation: [["count"]],- filter: ["=", ["field", 1, null], ["field", 2, null]],- },- };-- expect(normalize(query)).toEqual(normalized);- });-});diff --git a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/query_processor_test.clj b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/query_processor_test.cljindex 4dbd8757d9e..fa602a62c64 100644--- a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/query_processor_test.clj+++ b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/query_processor_test.clj@@ -7,8 +7,7 @@[metabase.driver :as driver][metabase.driver.bigquery-cloud-sdk :as bigquery][metabase.driver.bigquery-cloud-sdk.query-processor :as bigquery.qp]- [metabase.driver.bigquery-cloud-sdk.query-processor-test.reconciliation-test-util- :as bigquery.qp.reconciliation-tu]+ [metabase.driver.bigquery-cloud-sdk.query-processor-test.reconciliation-test-util :as bigquery.qp.reconciliation-tu][metabase.driver.sql.query-processor :as sql.qp][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata]@@ -16,6 +15,7 @@[metabase.lib.test-util :as lib.tu][metabase.query-processor :as qp][metabase.query-processor.compile :as qp.compile]+ [metabase.query-processor.preprocess :as qp.preprocess]^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store][metabase.query-processor.util.add-alias-info :as add][metabase.sync.core :as sync]@@ -939,26 +939,37 @@[:datetime_tz true true true true true true true true true true true true true true true true]])(defn- can-breakout?! [field unit report-timezone]- (try- (mt/test-driver :bigquery-cloud-sdk- (mt/dataset attempted-murders- (mt/with-report-timezone-id! report-timezone- (mt/run-mbql-query attempts- {:aggregation [[:count]]- :breakout [[:field (mt/id :attempts field) {:temporal-unit unit}]]}))))- true- (catch Throwable _- false)))+ (mt/test-driver :bigquery-cloud-sdk+ (mt/dataset attempted-murders+ (mt/with-report-timezone-id! report-timezone+ (let [query (mt/mbql-query attempts+ {:aggregation [[:count]]+ :breakout [[:field (mt/id :attempts field) {:temporal-unit unit}]]})+ bucket (lib/raw-temporal-bucket (first (lib/breakouts (qp.preprocess/preprocess query))))]+ ;; if the bucket was removed automatically by normalization or swapped with something correct (see for+ ;; example [[metabase.lib.schema.ref/valid-temporal-unit-for-base-type?]]) or somewhere else in+ ;; preprocessing then we don't even need to run the query to know that that sort of breakout isn't+ ;; supported.+ (if (or (nil? bucket)+ (not= bucket unit))+ false+ (try+ (qp/process-query query)+ true+ (catch Throwable _+ false))))))))(defn- run-breakout-test-table-tests-for-field- :when (= (:row test-case) field)- :let [unit (:col test-case)]]- (if (:expected test-case)- (is (can-breakout?! field unit report-timezone))- (is (not (can-breakout?! field unit report-timezone)))))))+ (mt/test-driver :bigquery-cloud-sdk+ (doseq [test-case (test-table-seq breakout-test-table)+ :when (= (:row test-case) field)+ :let [unit (:col test-case)]]+ (testing (pr-str test-case)+ (if (:expected test-case)+ (is (can-breakout?! field unit report-timezone))+ (is (not (can-breakout?! field unit report-timezone)))))))))(deftest breakout-by-bucketed-datetimes-e2e-time-test (run-breakout-test-table-tests-for-field! :time nil))(deftest breakout-by-bucketed-datetimes-e2e-datetime-test (run-breakout-test-table-tests-for-field! :datetime nil))@@ -979,8 +990,11 @@[int](mt/run-mbql-query venues{:aggregation [[:count]]- :filter [:= $name "x\\\\' OR 1 = 1 -- "]})))))+ :filter [:= $name "x\\\\' OR 1 = 1 -- "]}))))))))+(deftest ^:parallel string-escape-test-2+ (mt/test-driver :bigquery-cloud-sdk+ (testing "Make sure single quotes in parameters are escaped properly to prevent SQL injection\n"(testing "native query"(is (= [[0]](mt/formatted-rowsdiff --git a/modules/drivers/mongo/src/metabase/driver/mongo/parameters.clj b/modules/drivers/mongo/src/metabase/driver/mongo/parameters.cljindex cd5ea534be6..5ae37fa6a6e 100644--- a/modules/drivers/mongo/src/metabase/driver/mongo/parameters.clj+++ b/modules/drivers/mongo/src/metabase/driver/mongo/parameters.clj@@ -3,11 +3,11 @@[clojure.string :as str][java-time.api :as t][metabase.driver-api.core :as driver-api]- ^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.driver.common.parameters :as params]- ^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.driver.common.parameters.dates :as params.dates]- ^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.driver.common.parameters.operators :as params.ops]- ^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.driver.common.parameters.parse :as params.parse]- ^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.driver.common.parameters.values :as params.values]+ [metabase.driver.common.parameters :as params]+ [metabase.driver.common.parameters.dates :as params.dates]+ [metabase.driver.common.parameters.operators :as params.ops]+ [metabase.driver.common.parameters.parse :as params.parse]+ [metabase.driver.common.parameters.values :as params.values][metabase.driver.mongo.query-processor :as mongo.qp][metabase.util :as u][metabase.util.date-2 :as u.date]diff --git a/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj b/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.cljindex 4f0cafdb430..86d4abe0300 100644--- a/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj+++ b/modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj@@ -1782,7 +1782,7 @@ function(bin) {(let [query (update query :query preprocess)](binding [*query* query*next-alias-index* (volatile! 0)]- (let [source-table-name (if-let [source-table-id #_{:clj-kondo/ignore [:deprecated-var]} (driver-api/query->source-table-id query)]+ (let [source-table-name (if-let [source-table-id (driver-api/query->source-table-id query)](:name (driver-api/table (driver-api/metadata-provider) source-table-id))(query->collection-name query))compiled (mbql->native-rec (:query query))]diff --git a/shadow-cljs.edn b/shadow-cljs.ednindex cce514bffb5..892a5c2e4d1 100644--- a/shadow-cljs.edn+++ b/shadow-cljs.edn@@ -19,7 +19,6 @@:warnings {:fn-deprecated false}}:closure-defines {goog.debug.LOGGING_ENABLED true}:entries [metabase.dashboards.constants- metabase.legacy-mbql.jsmetabase.lib.column-groupmetabase.lib.jsmetabase.lib.limit@@ -41,4 +40,5 @@{:target :node-test:output-to "target/node-tests.js":ns-regexp "-test$"- :compiler-options {:reader-features #{:cljs-test}}}}}+ :compiler-options {:reader-features #{:cljs-test}+ :warnings {:fn-deprecated false}}}}}diff --git a/src/metabase/actions/args.clj b/src/metabase/actions/args.cljindex 1593d0dc19a..98b8bf6442d 100644--- a/src/metabase/actions/args.clj+++ b/src/metabase/actions/args.clj@@ -1,6 +1,7 @@(ns ^:instrument/always metabase.actions.args(:require- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ;; legacy usage, do not use this in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.schema.id :as lib.schema.id][metabase.util :as u][metabase.util.log :as log]diff --git a/src/metabase/actions/execution.clj b/src/metabase/actions/execution.cljindex 46fc6322c93..e07a5f3f8eb 100644--- a/src/metabase/actions/execution.clj+++ b/src/metabase/actions/execution.clj@@ -8,10 +8,12 @@[metabase.actions.models :as action][metabase.analytics.core :as analytics][metabase.api.common :as api]- [metabase.legacy-mbql.schema :as mbql.s]+ ;; legacy usage, do not use this in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s][metabase.lib.schema :as lib.schema][metabase.lib.schema.id :as lib.schema.id][metabase.model-persistence.core :as model-persistence]+ [metabase.parameters.schema :as parameters.schema][metabase.queries.models.query :as query][metabase.query-processor :as qp][metabase.query-processor.card :as qp.card]@@ -110,13 +112,10 @@(mu/defn- build-implicit-query :- [:map[:query ::mbql.s/Query][:row-parameters ::actions.args/row]- ;; TODO -- the schema for these should probably be- ;; `:metabase.lib.schema.parameter/parameter` instead of `:any`, but I'm not- ;; 100% sure about that.- [:prefetch-parameters {:optional true} [:tuple :any]]]+ [:prefetch-parameters {:optional true} [:maybe ::parameters.schema/parameters]]][{:keys [model_id parameters] :as _action} implicit-action request-parameters](let [{database-id :db_id- table-id :id :as table} (implicit-action-table model_id)+ table-id :id :as table} (implicit-action-table model_id)table-fields (:fields table)pk-fields (filterv #(isa? (:semantic_type %) :type/PK) table-fields)slug->field-name (->> table-fields@@ -155,9 +154,12 @@[:= [:field (:id pk-field) nil] (get simple-parameters pk-field-name)])requires-pk?- (assoc :prefetch-parameters [{:target [:dimension [:field (:id pk-field) nil]]- :type "id"- :value [(get simple-parameters pk-field-name)]}]))))+ (assoc :prefetch-parameters [{;; parameter ID here is not really important but let's generate something+ ;; consistent rather than random to make this easier to test+ :id "metabase.actions.execution/prefetch-parameters-pk"+ :target [:dimension [:field (:id pk-field) nil]]+ :type :id+ :value [(get simple-parameters pk-field-name)]}]))))(defn- parse-implicit-action [action-instance](let [k (keyword (:kind action-instance))]diff --git a/src/metabase/analyze/fingerprint/insights.clj b/src/metabase/analyze/fingerprint/insights.cljindex 312569b27eb..cb122ba83cf 100644--- a/src/metabase/analyze/fingerprint/insights.clj+++ b/src/metabase/analyze/fingerprint/insights.clj@@ -6,7 +6,6 @@[kixi.stats.math :as math][medley.core :as m][metabase.analyze.fingerprint.fingerprinters :as fingerprinters]- [metabase.legacy-mbql.util :as mbql.u][metabase.models.interface :as mi][metabase.sync.util :as sync-util][metabase.util :as u]@@ -257,7 +256,7 @@(simple-linear-regression xfn yfn :linear :linear)(best-fit xfn yfn))))(fn [[[y-previous y-current] [x-previous x-current] [offset slope] best-fit-equation]]- (let [unit (let [unit (some-> datetime :unit mbql.u/normalize-token)]+ (let [unit (let [unit (some-> datetime :unit keyword)](if (or (nil? unit)(= unit :default))(infer-unit x-previous x-current)diff --git a/src/metabase/analyze/query_results.clj b/src/metabase/analyze/query_results.cljindex 4b8ed652915..836e4e0bdf0 100644--- a/src/metabase/analyze/query_results.clj+++ b/src/metabase/analyze/query_results.clj@@ -7,7 +7,7 @@[metabase.analyze.classifiers.name :as classifiers.name][metabase.analyze.fingerprint.fingerprinters :as fingerprinters][metabase.analyze.fingerprint.insights :as insights]- [metabase.legacy-mbql.schema :as mbql.s]+ [metabase.query-processor.schema :as query-processor.schema][metabase.util.i18n :as i18n][metabase.util.log :as log][metabase.util.malli :as mu]@@ -16,7 +16,7 @@(def ^:private ResultColumnMetadata"Result metadata for a single column"- [:ref ::mbql.s/legacy-column-metadata])+ [:ref ::query-processor.schema/result-metadata.column])(mr/def ::ResultsMetadata(mu/with-api-error-messagediff --git a/src/metabase/driver/common/parameters/dates.clj b/src/metabase/driver/common/parameters/dates.cljindex b02f43fa634..550d6197e6c 100644--- a/src/metabase/driver/common/parameters/dates.clj+++ b/src/metabase/driver/common/parameters/dates.clj@@ -211,7 +211,8 @@"Takes a string description of a *date* (not datetime) range such as 'lastmonth' or '2016-07-15~2016-08-6', oran absolute date *or datetime* string, and returns a corresponding MBQL filter clause for a given field reference."[date-string :- :string- field :- [:or ::lib.schema.id/field mbql.s/Field]]+ field :- [:or ::lib.schema.id/field mbql.s/FieldOrExpressionRef]]+ #_{:clj-kondo/ignore [:deprecated-var]}(or (#'qp.parameters.dates/execute-decoders all-date-string-decoders :filter (mbql.u/wrap-field-id-if-needed field) date-string)(throw (ex-info (tru "Don''t know how to parse date string {0}" (pr-str date-string)){:type qp.error-type/invalid-parameterdiff --git a/src/metabase/driver/common/parameters/operators.clj b/src/metabase/driver/common/parameters/operators.cljindex f55e95f7fd4..7a4b507ee66 100644--- a/src/metabase/driver/common/parameters/operators.clj+++ b/src/metabase/driver/common/parameters/operators.clj@@ -69,6 +69,7 @@[param :- ::lib.schema.parameter/parameter](let [{param-type :type, [a b :as param-value] :value, [_ field] :target, options :options} (#'qp.params.ops/normalize-param param)](verify-type-and-arity field param-type param-value)+ #_{:clj-kondo/ignore [:deprecated-var]}(let [field' (mbql.u/wrap-field-id-if-needed field)opts-fn (operator-options-fn param-type)](case (#'qp.params.ops/operator-arity param-type)diff --git a/src/metabase/driver/impl.clj b/src/metabase/driver/impl.cljindex b99b32dda91..96bfd4f083c 100644--- a/src/metabase/driver/impl.clj+++ b/src/metabase/driver/impl.clj@@ -3,7 +3,7 @@clutter in [[metabase.driver]] itself."(:require[metabase.classloader.core :as classloader]- [metabase.lib.util :as lib.util]+ [metabase.lib.core :as lib][metabase.util :as u][metabase.util.i18n :refer [trs tru]][metabase.util.log :as log]@@ -233,4 +233,4 @@(truncate-alias s default-alias-max-length-bytes))(^String [^String s max-length-bytes]- (lib.util/truncate-alias s max-length-bytes)))+ (lib/truncate-alias s max-length-bytes)))diff --git a/src/metabase/driver/sql/parameters/substitution.clj b/src/metabase/driver/sql/parameters/substitution.cljindex af5855b2a2d..04a5534e621 100644--- a/src/metabase/driver/sql/parameters/substitution.clj+++ b/src/metabase/driver/sql/parameters/substitution.clj@@ -261,14 +261,16 @@{:replacement-snippet snippet:prepared-statement-args args}))-(defn- field->clause+(mu/defn- field->clause :- driver-api/mbql.schema.field[field other-opts]- [:field- (:id field)- (merge {:base-type (:base-type field)- driver-api/qp.add.source-table (:table-id field)- ::compiling-field-filter? true}- other-opts)])+ (driver-api/normalize+ driver-api/mbql.schema.field+ [:field+ (:id field)+ (merge {:base-type (:base-type field)+ driver-api/qp.add.source-table (:table-id field)+ ::compiling-field-filter? true}+ other-opts)]))(mu/defn- field->field-filter-clause :- driver-api/mbql.schema.field[driver :- :keyworddiff --git a/src/metabase/driver_api/core.clj b/src/metabase/driver_api/core.cljindex 360ed392f33..da29b92cb50 100644--- a/src/metabase/driver_api/core.clj+++ b/src/metabase/driver_api/core.clj@@ -1,10 +1,6 @@(ns metabase.driver-api.core- {:clj-kondo/config '{:linters- ;; this is actually ok here since this is a drivers namespace- {:discouraged-namespace {metabase.query-processor.store {:level :off}}- ;; this is also ok here since this is a drivers namespace- :discouraged-var {metabase.lib.core/->legacy-MBQL {:level :off}}- :missing-docstring {:level :off}}}}+ ;; missing docstring warnings are false positives because of Potemkin+ {:clj-kondo/config '{:linters {:missing-docstring {:level :off}}}}(:refer-clojure :exclude [replace compile])(:require[metabase.actions.core :as actions]@@ -32,7 +28,6 @@[metabase.lib.schema.parameter :as lib.schema.parameter][metabase.lib.schema.temporal-bucketing :as lib.schema.temporal-bucketing][metabase.lib.types.isa :as lib.types.isa]- [metabase.lib.util :as lib.util][metabase.lib.util.match :as lib.util.match][metabase.logger.core :as logger][metabase.models.interface :as mi]@@ -49,7 +44,7 @@[metabase.query-processor.preprocess :as qp.preprocess][metabase.query-processor.reducible :as qp.reducible][metabase.query-processor.setup :as qp.setup]- ^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store]+ [metabase.query-processor.store :as qp.store][metabase.query-processor.timezone :as qp.timezone][metabase.query-processor.util :as qp.util][metabase.query-processor.util.add-alias-info :as add]@@ -113,9 +108,10 @@lib.util.match/matchlib.util.match/match-onelib.util.match/replace- lib.util/truncate-alias+ lib/truncate-aliaslib/->legacy-MBQLlib/->metadata-provider+ lib/normalizelib/order-by-clauselib/query-from-legacy-inner-querylib/raw-native-query@@ -127,7 +123,6 @@mbql.u/aggregation-at-indexmbql.u/assoc-field-optionsmbql.u/desugar-filter-clause- mbql.u/dispatch-by-clause-name-or-classmbql.u/expression-with-namembql.u/field-optionsmbql.u/is-clause?@@ -137,7 +132,6 @@mbql.u/query->max-rows-limitmbql.u/query->source-table-idmbql.u/simplify-compound-filter- mbql.u/unique-name-generatormbql.u/update-field-optionsmdb/clob->strmdb/data-source@@ -145,6 +139,7 @@mdb/query-canceled-exception?mdb/specmetabase.driver-api.impl/cached+ metabase.driver-api.impl/dispatch-by-clause-name-or-classmi/instance-of?nest-query/nest-expressionspremium-features/is-hosted?@@ -196,6 +191,8 @@(p/import-fn secrets/value-as-file! secret-value-as-file!)(p/import-fn table/database table->database)+(p/import-fn lib/unique-name-generator-with-options unique-name-generator)+(p/import-def qp.error-type/db qp.error-type.db)(p/import-def qp.error-type/driver qp.error-type.driver)(p/import-def qp.error-type/invalid-parameter qp.error-type.invalid-parameter)@@ -274,7 +271,7 @@(def mbql.schema.field"mbql.s/field"- mbql.s/field)+ ::mbql.s/field)(def mbql.schema.FieldOrExpressionDef"::mbql.s/FieldOrExpressionDef"diff --git a/src/metabase/driver_api/impl.clj b/src/metabase/driver_api/impl.cljindex 35a31e3f9f7..3436d25c384 100644--- a/src/metabase/driver_api/impl.clj+++ b/src/metabase/driver_api/impl.clj@@ -3,9 +3,11 @@;; this is actually ok here since this is a drivers namespace{:discouraged-namespace {metabase.query-processor.store {:level :off}}}}}(:require+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.util :as mbql.u][metabase.lib.metadata :as lib.metadata]^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store]- [metabase.util :as u]))+ [metabase.util :as u]+ [toucan2.core :as t2]));;; replacement for the old [[metabase.query-processor.store/cached]] macro, deprecated in 57;;;@@ -31,3 +33,23 @@(qp.store/metadata-provider)~ks(fn [] ~@body))))++(defn dispatch-by-clause-name-or-class+ "Dispatch function perfect for use with multimethods that dispatch off elements of an MBQL query. If `x` is an MBQL+ clause, dispatches off the clause name; otherwise dispatches off `x`'s class."+ ([x]+ (letfn [(clause-type [x]+ (when (mbql.u/mbql-clause? x)+ (first x)))+ (mbql5-lib-type [x]+ (when (map? x)+ (:lib/type x)))+ (model-type [x]+ (t2/model x))]+ (or+ (clause-type x)+ (mbql5-lib-type x)+ (model-type x)+ (type x))))+ ([x _]+ (dispatch-by-clause-name-or-class x)))diff --git a/src/metabase/embedding/api/common.clj b/src/metabase/embedding/api/common.cljindex 45fddada4a3..f27165e0690 100644--- a/src/metabase/embedding/api/common.clj+++ b/src/metabase/embedding/api/common.clj@@ -280,7 +280,7 @@(u/one-or-many value)value)]:when (contains? slug->value slug)]- (assoc (select-keys param [:type :target :slug])+ (assoc (select-keys param [:type :target :slug :id]):value value))));;; ---------------------------- Card Fns used by both /api/embed and /api/preview_embed -----------------------------diff --git a/src/metabase/indexed_entities/api.clj b/src/metabase/indexed_entities/api.cljindex 8a42aa9b7a7..ea4947be06e 100644--- a/src/metabase/indexed_entities/api.clj+++ b/src/metabase/indexed_entities/api.clj@@ -5,7 +5,8 @@[metabase.api.macros :as api.macros][metabase.indexed-entities.models.model-index :as model-index][metabase.indexed-entities.task.index-values :as task.index-values]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ;; legacy usage, do not use this in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.util.i18n :refer [tru]][metabase.util.malli.schema :as ms][toucan2.core :as t2]))@@ -13,7 +14,9 @@(defn- ensure-type"Ensure that the ref exists and is of type required for indexing."[t ref metadata]- (if-let [field (some (fn [f] (when ((comp #{(mbql.normalize/normalize-field-ref ref)} :field_ref) f)+ (if-let [field (some (fn [f] (when ((comp #{#_{:clj-kondo/ignore [:deprecated-var]} (mbql.normalize/normalize-field-ref ref)}+ :field_ref)+ f)f))metadata)](let [type-slot (case tdiff --git a/src/metabase/indexed_entities/models/model_index.clj b/src/metabase/indexed_entities/models/model_index.cljindex 693eee04f21..5fac45d8041 100644--- a/src/metabase/indexed_entities/models/model_index.clj+++ b/src/metabase/indexed_entities/models/model_index.clj@@ -2,8 +2,9 @@(:require[clojure.set :as set][clojure.string :as str]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.schema :as mbql.s]+ ;; legacy usage, do not use this in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s][metabase.lib.schema.common :as lib.schema.common][metabase.lib.schema.id :as lib.schema.id][metabase.models.interface :as mi]@@ -51,13 +52,13 @@"Filter function for valid tuples for indexing: an id and a value."[[id v]] (and id v))-(mu/defn- fix-expression-refs :- mbql.s/Field+(mu/defn- fix-expression-refs :- mbql.s/FieldOrExpressionRef"Convert expression ref into a field ref.Expression refs (`[:expression \"full-name\"]`) are how the _query_ refers to a custom column. But nested queriesdon't, (and shouldn't) care that those are expressions. They are just another field. The field type is always`:type/Text` enforced by the endpoint to create model indexes."- [field-ref :- mbql.s/Field+ [field-ref :- mbql.s/FieldOrExpressionRefbase-type :- ::lib.schema.common/base-type](case (first field-ref):field field-ref@@ -80,7 +81,7 @@(let [model (t2/select-one :model/Card :id (:model_id model-index))fix (mu/fn [field-ref :- some?base-type :- ::lib.schema.common/base-type]- (-> field-ref mbql.normalize/normalize-field-ref (fix-expression-refs base-type)))+ (-> field-ref #_{:clj-kondo/ignore [:deprecated-var]} mbql.normalize/normalize-field-ref (fix-expression-refs base-type)));; :type/Text and :type/Integer are ensured at creation time on the api.value-ref (-> model-index :value_ref (fix :type/Text))pk-ref (-> model-index :pk_ref (fix :type/Integer))]diff --git a/src/metabase/legacy_mbql/js.cljs b/src/metabase/legacy_mbql/js.cljsdeleted file mode 100644index 7ec74ca867f..00000000000--- a/src/metabase/legacy_mbql/js.cljs+++ /dev/null@@ -1,33 +0,0 @@-(ns metabase.legacy-mbql.js- "JavaScript-friendly interface to metabase.legacy-mbql util functions."- (:require- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.util :as u]))--(defn unwrap- "Sometimes JS queries are passed in with a `Join` or `Aggregation` clause object instead of a simple Array.- These clauses `extend Array` so `Array.isArray(x)` is true, but they're treated as opaque by `js->clj`.- This recurses over the whole query, unwrapping these values to their `.raw()` form."- [x]- (cond- ;; (object? x) only matches for things that are plain objects. eg. `(object? (js/Date.))` is false.- ;; This matches anything that descends from `Object`, like `Join` clause, and has a `.raw()` method.- (and (instance? js/Object x)- (fn? (.-raw x))) (-> x (.raw) js->clj unwrap)- (map? x) (update-vals x unwrap)- (sequential? x) (mapv unwrap x)- :else x))--(defn normalize-cljs- "Normalize an MBQL query, and convert it to the latest and greatest version of MBQL.-- Returns the CLJS form of the normalized query. Use [[normalize]] for the JS form."- [query]- (-> query js->clj unwrap mbql.normalize/normalize))--(defn ^:export normalize- "Normalize an MBQL query, and convert it to the latest and greatest version of MBQL.-- Returns the JS form of the normalized query. Use [[normalize-cljs]] for the CLJS form."- [query]- (-> query normalize-cljs (clj->js :keyword-fn u/qualified-name)))diff --git a/src/metabase/legacy_mbql/jvm_util.clj b/src/metabase/legacy_mbql/jvm_util.cljindex 5a51bbd87e4..f5cb195ce74 100644--- a/src/metabase/legacy_mbql/jvm_util.clj+++ b/src/metabase/legacy_mbql/jvm_util.clj@@ -1,6 +1,7 @@(ns metabase.legacy-mbql.jvm-util"This namespace contains functionality that is not compatible with js, hence can not be stored in correspodingcljc ns, ie. [[metabase.legacy-mbql.util]]."+ {:deprecated "0.57.0"}(:require[metabase.lib.filter.desugar.jvm :as lib.filter.desugar.jvm][metabase.lib.util.match :as lib.util.match]))diff --git a/src/metabase/legacy_mbql/normalize.cljc b/src/metabase/legacy_mbql/normalize.cljcindex f7a14243d39..3340c061619 100644--- a/src/metabase/legacy_mbql/normalize.cljc+++ b/src/metabase/legacy_mbql/normalize.cljc@@ -38,7 +38,6 @@[metabase.legacy-mbql.util :as mbql.u][metabase.lib.normalize :as lib.normalize][metabase.lib.schema.expression.temporal :as lib.schema.expression.temporal]- [metabase.lib.schema.metadata.fingerprint :as lib.schema.metadata.fingerprint][metabase.lib.util.match :as lib.util.match][metabase.util :as u][metabase.util.i18n :as i18n]@@ -392,41 +391,19 @@(defn normalize-field-ref"Normalize the field ref. Ensure it's well-formed mbql, not just json."+ {:deprecated "0.57.0"}[clause](-> clause normalize-tokens canonicalize-mbql-clauses))-(mu/defn- normalize-fingerprint :- [:maybe ::lib.schema.metadata.fingerprint/fingerprint]- [fingerprint :- [:maybe :map]]- (when fingerprint- (lib.normalize/normalize ::lib.schema.metadata.fingerprint/fingerprint fingerprint)))+(mu/defn- normalize-source-metadata+ "Normalize source/results metadata for a single column.-(mu/defn normalize-source-metadata- "Normalize source/results metadata for a single column."+ DEPRECATED: Use [[metabase.lib-be.core/instance->metadata]]+ or [[metabase.lib.core/->normalized-stage-metadata]] (for Lib-style metadata) or [[metabase.lib.core/normalize]]+ with `:metabase.query-processor.schema/result-metadata.column` (for QP-style metadata) going forward."[metadata :- :map]{:pre [(map? metadata)]}- (into (empty metadata)- (comp (remove (fn [[k _v]]- (= k :ident))) ; ignore legacy `:ident` key- (map (fn [[k v]]- (let [k (keyword k)- k ((if (simple-keyword? k)- u/->snake_case_en- u/->kebab-case-en) k)- v (case k- (:semantic_type- :visibility_type- :source- :unit- :lib/source) (keyword v)- (:effective_type- :base_type) (or (keyword v) :type/*)- :field_ref (normalize-field-ref v)- :fingerprint (normalize-fingerprint v)- :binning_info (m/update-existing v :binning_strategy keyword)- #_else- v)]- [k v]))))- metadata))+ (lib.normalize/normalize ::mbql.s/legacy-column-metadata metadata))(mu/defn- normalize-native-query :- [:maybe :map]"For native queries, normalize the top-level keys, and template tags, but nothing else."@@ -484,7 +461,10 @@In some cases, dealing with the path isn't desirable, but we don't want to accidentally trigger normalizationfunctions (such as accidentally normalizing the `:type` key in something other than the top-level of the query), so- by convention please pass `nil` to avoid accidentally triggering special path functions."+ by convention please pass `nil` to avoid accidentally triggering special path functions.++ DEPRECATED: Use [[metabase.lib.core/normalize]] to normalize things going forward."+ {:deprecated "0.57.0"}([x] (normalize-tokens x path->special-token-normalization-fn))([x special-fns](let [special-fn (when (and special-fns (fn? special-fns))diff --git a/src/metabase/legacy_mbql/predicates.cljc b/src/metabase/legacy_mbql/predicates.cljcindex d14d42bb1fb..ef3c4f1d2ba 100644--- a/src/metabase/legacy_mbql/predicates.cljc+++ b/src/metabase/legacy_mbql/predicates.cljc@@ -1,5 +1,6 @@(ns metabase.legacy-mbql.predicates"Predicate functions for checking whether something is a valid instance of a given MBQL clause."+ {:deprecated "0.57.0"}(:require[metabase.legacy-mbql.schema :as mbql.s][metabase.util.malli.registry :as mr]))diff --git a/src/metabase/legacy_mbql/schema.cljc b/src/metabase/legacy_mbql/schema.cljcindex 336d20b293d..54a674750b3 100644--- a/src/metabase/legacy_mbql/schema.cljc+++ b/src/metabase/legacy_mbql/schema.cljc@@ -1,7 +1,7 @@(ns metabase.legacy-mbql.schema"Schema for validating a *normalized* MBQL query. This is also the definitive grammar for MBQL, wow!"(:refer-clojure :exclude [count distinct min max + - / * and or not not-empty = < > <= >= time case concat replace- abs float every? select-keys #?(:clj doseq)])+ abs float every? select-keys #?(:clj doseq) some])(:require[clojure.core :as core][clojure.set :as set]@@ -23,12 +23,13 @@[metabase.lib.schema.metadata.fingerprint :as lib.schema.metadata.fingerprint][metabase.lib.schema.middleware-options :as lib.schema.middleware-options][metabase.lib.schema.parameter :as lib.schema.parameter]+ [metabase.lib.schema.ref :as lib.schema.ref][metabase.lib.schema.settings :as lib.schema.settings][metabase.lib.schema.template-tag :as lib.schema.template-tag][metabase.lib.schema.temporal-bucketing :as lib.schema.temporal-bucketing][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]- [metabase.util.performance :refer [every? select-keys #?(:clj doseq)]]))+ [metabase.util.performance :as perf :refer [every? select-keys #?(:clj doseq) some]]));; A NOTE ABOUT METADATA:;;@@ -173,7 +174,6 @@;; [:= [:field 10 {:temporal-unit :day}] [:absolute-datetime #inst "2018-10-02" :day]](mr/def ::absolute-datetime[:multi {:error/message "valid :absolute-datetime clause"- :doc/title [:span [:code ":absolute-datetime"] " clause"]:dispatch (fn [x](cond(core/not (is-clause? :absolute-datetime x)) :invalid@@ -266,112 +266,65 @@;; [:+ [:field 1 nil] [:field 2 nil]];;;; As of 0.42.0 `:expression` references can have an optional options map-(defclause ^{:requires-features #{:expressions}} expression- expression-name ::lib.schema.common/non-blank-string- options (optional :map))--(defmethod options-style-method :expression [_tag] ::options-style.last-unless-empty)--(defn valid-temporal-unit-for-base-type?- "Whether `temporal-unit` (e.g. `:day`) is valid for the given `base-type` (e.g. `:type/Date`). If either is `nil` this- will return truthy. Accepts either map of `field-options` or `base-type` and `temporal-unit` passed separately."- ([{:keys [base-type temporal-unit] :as _field-options}]- (valid-temporal-unit-for-base-type? base-type temporal-unit))-- ([base-type temporal-unit]- (if-let [units (when (core/and temporal-unit base-type)- (condp #(isa? %2 %1) base-type- :type/Date date-bucketing-units- :type/Time time-bucketing-units- :type/DateTime datetime-bucketing-units- nil))]- (contains? units temporal-unit)- true)))--(mr/def ::validate-temporal-unit- ;; TODO - consider breaking this out into separate constraints for the three different types so we can generate more- ;; specific error messages- [:fn- {:error/message "Invalid :temporal-unit for the specified :base-type."}- valid-temporal-unit-for-base-type?])--(mr/def ::no-binning-options-at-top-level- (lib.schema.common/disallowed-keys- {:strategy ":binning keys like :strategy are not allowed at the top level of :field options."}))--;;; TODO (Cam 9/12/25) -- aren't these basically the same as MBQL 5, except Lib UUID isn't required? Maybe we should-;;; unify these schemas.-(mr/def ::FieldOptions+(mr/def ::expression.options+ "Options for a legacy `:expression` ref in MBQL 4 are the same as in MBQL 5, except that `:lib/uuid` is optional and+ it cannot be empty."[:and- [:map- {:error/message "field options"}- [:base-type {:optional true} [:maybe ::lib.schema.common/base-type]]-- ;; Following option conveys temporal unit that was set on a ref in previous stages. For details refer to- ;; [:metabase.lib.schema.ref/field.options] schema.- [:inherited-temporal-unit {:optional true} [:maybe ::DateTimeUnit]]-- [:source-field- {:optional true- :description- "Replaces `fk->`.-- `:source-field` is used to refer to a FieldOrExpression from a different Table you would like IMPLICITLY JOINED to- the source table.-- If both `:source-field` and `:join-alias` are supplied, `:join-alias` should be used to perform the join;- `:source-field` should be for information purposes only."} ::lib.schema.id/field]-- [:source-field-name- {:optional true :description "The name or desired alias of the field used for an implicit join."}- ::lib.schema.common/non-blank-string]-- [:source-field-join-alias- {:optional true :description "The join alias of the source field used for an implicit join."}- ::lib.schema.common/non-blank-string]-- [:temporal-unit- {:optional true- :description- "`:temporal-unit` is used to specify DATE BUCKETING for a FieldOrExpression that represents a moment in time of- some sort.-- There is no requirement that all `:type/Temporal` derived FieldOrExpressions specify a `:temporal-unit`, but for- legacy reasons `:field` clauses that refer to `:type/DateTime` FieldOrExpressions will be automatically \"bucketed\"- in the `:breakout` and `:filter` clauses, but nowhere else. Auto-bucketing only applies to `:filter` clauses when- values for comparison are `yyyy-MM-dd` date strings. See the `auto-bucket-datetimes` middleware for more details.- `:field` clauses elsewhere will not be automatically bucketed, so drivers still need to make sure they do any- special datetime handling for plain `:field` clauses when their FieldOrExpression derives from `:type/DateTime`."}- [:maybe ::DateTimeUnit]]-- [:join-alias- {:optional true- :description- "Replaces `joined-field`.-- `:join-alias` is used to refer to a FieldOrExpression from a different Table/nested query that you are EXPLICITLY- JOINING against."}- [:maybe ::lib.schema.join/alias]]-- [:binning- {:optional true- :description- "Replaces `binning-strategy`.+ [:merge+ {:decode/normalize (fn [m]+ (when-let [m (lib.schema.ref/normalize-expression-options m)]+ (dissoc m :lib/uuid)))}+ ::lib.schema.ref/expression.options+ [:map+ [:lib/uuid {:optional true} ::lib.schema.common/uuid]]]+ (lib.schema.common/disallowed-keys {:lib/uuid "MBQL 4 refs should not have :lib/uuid"})+ [:fn+ {:error/message "MBQL 4 :expression options should not be empty, use a nil map instead"+ :decode/normalize perf/not-empty}+ seq]])- Using binning requires the driver to support the `:binning` feature."}- [:maybe [:ref ::lib.schema.binning/binning]]]]+(mr/def ::expression+ [:and+ (helpers/clause+ :expression+ "expression-name" ::lib.schema.common/non-blank-string+ "options" [:optional ::expression.options])+ [:fn+ {:error/message "Expression should not have empty opts"+ :decode/normalize (fn [[tag expression-name opts]]+ (if (empty? opts)+ [tag expression-name]+ [tag expression-name opts]))}+ (fn [[_tag _expression-name opts :as expression-ref]]+ (core/or (core/= (core/count expression-ref) 2)+ (seq opts)))]])++(def expression+ "Schema for an MBQL 4 :expression reference."+ ^{:clause-name :expression} [:ref ::expression])- ;; additional validation- [:ref- {:description "If `:base-type` is specified, the `:temporal-unit` must make sense, e.g. no bucketing by `:year`for- a `:type/Time` column."}- ::validate-temporal-unit]+(defmethod options-style-method :expression [_tag] ::options-style.last-unless-empty)- [:ref- {:description "You cannot use `:binning` keys like `:strategy` in the top level."}- ::no-binning-options-at-top-level]])+(mr/def ::FieldOptions+ "Options for an MBQL 4 `:field` ref are the same as MBQL 5, except that `:lib/uuid` is not required and it cannot be+ empty."+ [:maybe+ [:and+ [:merge+ {:decode/normalize (fn [m]+ (when-let [m (lib.schema.ref/normalize-field-options-map m)]+ (dissoc m :lib/uuid)))}+ [:ref ::lib.schema.ref/field.options]+ [:map+ [:lib/uuid {:optional true} ::lib.schema.common/uuid]]]+ (lib.schema.common/disallowed-keys {:lib/uuid "MBQL 4 refs should not have :lib/uuid"})+ [:fn+ {:error/message "MBQL 4 :field ref options should not be empty, use nil instead"+ :decode/normalize perf/not-empty}+ seq]]])(mr/def ::require-base-type-for-field-name+ "Fields using names rather than integer IDs are required to specify `:base-type`."[:fn{:error/message ":field clauses using a string field name must specify :base-type."}(fn [[_ id-or-name {:keys [base-type]}]]@@ -383,25 +336,20 @@(mr/def ::field[:and- {:doc/title [:span [:code ":field"] " clause"]}(helpers/clause:field"id-or-name" [:or ::lib.schema.id/field :string]"options" [:maybe [:ref ::FieldOptions]])- [:ref- {:description "Fields using names rather than integer IDs are required to specify `:base-type`."}- ::require-base-type-for-field-name]])+ ::require-base-type-for-field-name])(def ^{:added "0.39.0"} field"Schema for a `:field` clause."^{:clause-name :field} [:ref ::field])(mr/def ::field-or-expression-ref- [:schema- {:doc/title "`:field` or `:expression` ref"}- (one-of expression field)])+ (one-of expression field))-(def Field+(def FieldOrExpressionRef"Schema for either a `:field` clause (reference to a Field) or an `:expression` clause (reference to an expression)."[:ref ::field-or-expression-ref])@@ -469,7 +417,7 @@[:string :string][:string-expression StringExpression][:value value]- [:else Field]])+ [:else FieldOrExpressionRef]])(def ^:private StringExpressionArg[:ref ::StringExpressionArg])@@ -541,7 +489,7 @@[:aggregation Aggregation][:value value][:datetime-expression DatetimeExpression]- [:else [:or [:ref ::DateOrDatetimeLiteral] Field]]])+ [:else [:or [:ref ::DateOrDatetimeLiteral] FieldOrExpressionRef]]])(def ^:private DateTimeExpressionArg[:ref ::DateTimeExpressionArg])@@ -570,7 +518,7 @@[:string :string][:string-expression StringExpression][:value value]- [:else Field]])+ [:else FieldOrExpressionRef]])(def ^:private ExpressionArg[:ref ::ExpressionArg])@@ -815,7 +763,7 @@:relative-datetime:else))}[:relative-datetime relative-datetime]- [:else Field]])+ [:else FieldOrExpressionRef]])(mr/def ::EqualityComparable[:maybe@@ -890,11 +838,11 @@lon-max OrderComparable);; SUGAR CLAUSES: These are rewritten as `[:= <field> nil]` and `[:not= <field> nil]` respectively-(defclause ^:sugar is-null, field Field)-(defclause ^:sugar not-null, field Field)+(defclause ^:sugar is-null, field FieldOrExpressionRef)+(defclause ^:sugar not-null, field FieldOrExpressionRef)(mr/def ::Emptyable- [:or StringExpressionArg Field])+ [:or StringExpressionArg FieldOrExpressionRef])(def Emptyable"Schema for a valid is-empty or not-empty argument."@@ -964,7 +912,7 @@;;;; SUGAR: This is automatically rewritten as a filter clause with a relative-datetime value(defclause ^:sugar time-interval- field Field+ field FieldOrExpressionRefn [:or:int[:enum :current :last :next]]@@ -974,12 +922,12 @@(defmethod options-style-method :time-interval [_tag] ::options-style.last-unless-empty)(defclause ^:sugar during- field Field+ field FieldOrExpressionRefvalue [:or ::lib.schema.literal/date ::lib.schema.literal/datetime]unit ::DateTimeUnit)(defclause ^:sugar relative-time-interval- col Field+ col FieldOrExpressionRefvalue :intbucket [:ref ::RelativeDatetimeUnit]offset-value :int@@ -1021,7 +969,7 @@[:boolean BooleanExpression][:value value][:segment segment]- [:else Field]])+ [:else FieldOrExpressionRef]])(def ^:private CaseClause[:tuple {:error/message ":case subclause"} Filter ExpressionArg])@@ -1059,7 +1007,6 @@`:+` clause or a `:field` or `:value` clause."[:multi{:error/message ":field or :expression reference or expression"- :doc/title "expression definition":dispatch (fn [x](cond(is-clause? numeric-functions x) :numeric@@ -1079,7 +1026,7 @@[:if case:if][:offset offset][:value value]- [:else Field]])+ [:else FieldOrExpressionRef]]);;; -------------------------------------------------- Aggregations --------------------------------------------------@@ -1087,8 +1034,8 @@;; cum-sum and cum-count are SUGAR because they're implemented in middleware. The clauses are swapped out with;; `count` and `sum` aggregations respectively and summation is done in Clojure-land-(defclause ^{:requires-features #{:basic-aggregations}} ^:sugar count, field (optional Field))-(defclause ^{:requires-features #{:basic-aggregations}} ^:sugar cum-count, field (optional Field))+(defclause ^{:requires-features #{:basic-aggregations}} ^:sugar count, field (optional FieldOrExpressionRef))+(defclause ^{:requires-features #{:basic-aggregations}} ^:sugar cum-count, field (optional FieldOrExpressionRef));; technically aggregations besides count can also accept expressions as args, e.g.;;@@ -1422,31 +1369,98 @@"Normalize legacy column metadata when using [[metabase.lib.normalize/normalize]]."[m](when (map? m)- (let [m (lib.schema.common/normalize-map-no-kebab-case m)]- ;; remove deprecated `:ident` key.- (dissoc m :ident))))+ (-> m+ lib.schema.common/normalize-map-no-kebab-case+ ;; remove deprecated `:ident` key.+ (dissoc :ident)+ ;; set `display_name` to `name` if it's unset.+ (as-> $m (cond-> $m+ (core/and (:name $m)+ (core/not (contains? $m :display_name)))+ (assoc :display_name (:name $m)))))))++(mr/def ::legacy-column-metadata.binning-info+ [:and+ [:map+ {:decode/normalize (fn [m]+ (when (map? m)+ (let [m (lib.schema.common/normalize-map-no-kebab-case m)]+ (cond-> m+ (core/and (:binning_strategy m)+ (core/not (:strategy m)))+ (assoc :strategy (:binning_strategy m))))))}+ [:strategy [:ref ::lib.schema.binning/strategy]]+ [:binning_strategy {:optional true} [:ref ::lib.schema.binning/strategy]]+ [:bin_width {:optional true} [:ref ::lib.schema.binning/bin-width]]+ [:num_bins {:optional true} [:ref ::lib.schema.binning/num-bins]]]+ [:fn+ {:error/message "bin_width is a required key when strategy is bin-width"}+ (fn [m]+ (if (core/= (:strategy m) :bin-width)+ (contains? m :bin_width)+ true))]+ [:fn+ {:error/message "num_bins is a required key when strategy is num-bins"}+ (fn [m]+ (if (core/= (:strategy m) :num-bins)+ (contains? m :num_bins)+ true))]+ [:fn+ {:error/message "binning_strategy, if present, must be equal to strategy"+ :decode/normalize (fn [m]+ (cond-> m+ (:binning_strategy m)+ (assoc :binning_strategy (:strategy m))))}+ (fn [m]+ (if (:binning_strategy m)+ (core/= (:binning_strategy m) (:strategy m))+ true))]])++(defn- legacy-column-metadata-qualified-keys-schema+ "In 56+ legacy column metadata can optionally include qualified keys from Lib-style metadata. Walk the Lib column+ metadata schema and build a `:map` schema out of those keys."+ []+ (let [schema (mr/resolve-schema ::lib.schema.metadata/column)+ find-map-schema (fn find-map-schema [schema]+ (if (core/= (mc/type schema) :map)+ schema+ (some find-map-schema (mc/children schema))))+ map-schema (find-map-schema schema)]+ (into [:map]+ (keep (fn [[k opts schema]]+ (when (qualified-keyword? k)+ [k (assoc opts :optional true) schema])))+ (mc/children map-schema))))++;;; TODO (Cam 10/20/25) -- it would be nice to come up with a way to automatically rebuild this if+;;; `::lib.schema.metadata/column` changes+(mr/def ::legacy-column-metadata.qualified-keys+ (legacy-column-metadata-qualified-keys-schema))(mr/def ::legacy-column-metadata"Schema for a single legacy metadata column. This is the pre-Lib equivalent of`:metabase.lib.schema.metadata/column`."[:and- [:map- ;; this schema is allowed for Card `result_metadata` in Lib so `:decode/normalize` is used for those Lib use cases.- {:decode/normalize lib-normalize-legacy-column}- [:base_type ::lib.schema.common/base-type]- [:display_name :string]- [:name :string]- [:effective_type {:optional true} ::lib.schema.common/base-type]- [:converted_timezone {:optional true} [:maybe [:ref ::lib.schema.expression.temporal/timezone-id]]]- [:field_ref {:optional true} [:maybe [:ref ::Reference]]]- ;; Fingerprint is required in order to use BINNING- [:fingerprint {:optional true} [:maybe [:ref ::lib.schema.metadata.fingerprint/fingerprint]]]- [:id {:optional true} [:maybe ::lib.schema.id/field]]- ;; name is allowed to be empty in some databases like SQL Server.- [:semantic_type {:optional true} [:maybe ::lib.schema.common/semantic-or-relation-type]]- [:source {:optional true} [:maybe [:ref ::lib.schema.metadata/column.legacy-source]]]- [:unit {:optional true} [:maybe [:ref ::lib.schema.temporal-bucketing/unit]]]- [:visibility_type {:optional true} [:maybe [:ref ::lib.schema.metadata/column.visibility-type]]]]+ [:merge+ [:map+ ;; this schema is allowed for Card `result_metadata` in Lib so `:decode/normalize` is used for those Lib use cases.+ {:decode/normalize lib-normalize-legacy-column}+ [:base_type {:default :type/*} ::lib.schema.common/base-type]+ [:display_name :string]+ [:name :string]+ [:binning_info {:optional true} [:maybe [:ref ::legacy-column-metadata.binning-info]]]+ [:effective_type {:optional true} ::lib.schema.common/base-type]+ [:converted_timezone {:optional true} [:maybe [:ref ::lib.schema.expression.temporal/timezone-id]]]+ [:field_ref {:optional true} [:maybe [:ref ::Reference]]]+ ;; Fingerprint is required in order to use BINNING+ [:fingerprint {:optional true} [:maybe [:ref ::lib.schema.metadata.fingerprint/fingerprint]]]+ [:id {:optional true} [:maybe ::lib.schema.id/field]]+ ;; name is allowed to be empty in some databases like SQL Server.+ [:semantic_type {:optional true} [:maybe ::lib.schema.common/semantic-or-relation-type]]+ [:source {:optional true} [:maybe [:ref ::lib.schema.metadata/column.legacy-source]]]+ [:unit {:optional true} [:maybe [:ref ::lib.schema.temporal-bucketing/unit]]]+ [:visibility_type {:optional true} [:maybe [:ref ::lib.schema.metadata/column.visibility-type]]]]+ [:ref ::legacy-column-metadata.qualified-keys]](lib.schema.common/disallowed-keys{:lib/type "Legacy results metadata should not have :lib/type, use :metabase.lib.schema.metadata/column for Lib metadata"})(letfn [(disallowed-key? [k]@@ -1609,7 +1623,7 @@(mr/def ::Fields[:schema{:error/message "Distinct, non-empty sequence of Field clauses"}- (helpers/distinct [:sequential {:min 1} Field])])+ (helpers/distinct [:sequential {:min 1} FieldOrExpressionRef])])(mr/def ::OrderBys(helpers/distinct [:sequential {:min 1} [:ref ::OrderBy]]))@@ -1620,7 +1634,7 @@[:source-query {:optional true} SourceQuery][:source-table {:optional true} SourceTable][:aggregation {:optional true} [:sequential {:min 1} Aggregation]]- [:breakout {:optional true} [:sequential {:min 1} Field]]+ [:breakout {:optional true} [:sequential {:min 1} FieldOrExpressionRef]][:expressions {:optional true} [:map-of ::lib.schema.common/non-blank-string [:ref ::FieldOrExpressionDef]]][:fields {:optional true} Fields][:filter {:optional true} Filter]@@ -1628,7 +1642,6 @@[:order-by {:optional true} [:ref ::OrderBys]][:page {:optional true} [:ref :metabase.lib.schema/page]][:joins {:optional true} [:ref ::Joins]]-[:source-metadata{:optional true:description "Info about the columns of the source query. Added in automatically by middleware. This metadata is@@ -1665,7 +1678,6 @@(mr/def ::dimension[:and- {:doc/title [:span [:code ":dimension"] " clause"]}[:fn {:error/message "must be a `:dimension` clause"} (partial helpers/is-clause? :dimension)][:catn[:tag [:= :dimension]]diff --git a/src/metabase/legacy_mbql/schema/helpers.cljc b/src/metabase/legacy_mbql/schema/helpers.cljcindex 15710035366..1b1e19a62cf 100644--- a/src/metabase/legacy_mbql/schema/helpers.cljc+++ b/src/metabase/legacy_mbql/schema/helpers.cljc@@ -3,13 +3,17 @@(:require#?(:clj [metabase.util.performance :refer [for]])[clojure.string :as str]+ [metabase.lib.schema.common :as lib.schema.common][metabase.types.core][metabase.util.malli.registry :as mr]))(comment metabase.types.core/keep-me)(defn mbql-clause?- "True if `x` is an MBQL clause (a sequence with a keyword as its first arg)."+ "True if `x` is an MBQL clause (a sequence with a keyword as its first arg).++ Deprecated: Use [[metabase.lib.core/clause?]] going forward."+ {:deprecated "0.57.0"}[x](and (sequential? x)(not (map-entry? x))@@ -19,7 +23,10 @@"If `x` is an MBQL clause, and an instance of clauses defined by keyword(s) `k-or-ks`?(is-clause? :count [:count 10]) ; -> true- (is-clause? #{:+ :- :* :/} [:+ 10 20]) ; -> true"+ (is-clause? #{:+ :- :* :/} [:+ 10 20]) ; -> true++ Deprecated: use [[metabase.lib.core/clause-of-type?]] going forward."+ {:deprecated "0.57.0"}[k-or-ks x](and(mbql-clause? x)@@ -32,7 +39,10 @@(check-clause :count [:count 10]) ; => [:count 10](check-clause? #{:+ :- :* :/} [:+ 10 20]) ; -> [:+ 10 20]- (check-clause :sum [:count 10]) ; => nil"+ (check-clause :sum [:count 10]) ; => nil++ DEPRECATED: use [[metabase.lib.core/clause-of-type?]] going forward"+ {:deprecated "0.57.0"}[k-or-ks x](when (is-clause? k-or-ks x)x))@@ -58,11 +68,15 @@[:and{:description (str "schema for a valid legacy MBQL " :tag " clause")}[:fn- {:error/message (str "must be a `" tag "` clause")}+ {:error/message (str "must be a `" tag "` clause")+ :decode/normalize (fn [x]+ (when (and (sequential? x)+ ((some-fn keyword? string?) (first x)))+ (update (vec x) 0 keyword)))}(partial is-clause? tag)](into[:catn- ["tag" [:= tag]]]+ ["tag" [:= {:decode/normalize lib.schema.common/normalize-keyword} tag]]](for [[arg-name arg-schema] (partition 2 arg-schemas)][arg-name (clause-arg-schema arg-schema)]))])diff --git a/src/metabase/legacy_mbql/util.cljc b/src/metabase/legacy_mbql/util.cljcindex 0e96dc91c2b..5800087d4c8 100644--- a/src/metabase/legacy_mbql/util.cljc+++ b/src/metabase/legacy_mbql/util.cljc@@ -1,16 +1,19 @@(ns metabase.legacy-mbql.util- "Utilitiy functions for working with MBQL queries."+ "Utilitiy functions for working with MBQL queries.++ DEPRECATED: Use [[metabase.lib.core]] for MBQL manipulation in all new code."+ {:deprecated "0.57.0"}(:refer-clojure :exclude [replace some mapv every? #?(:clj for)])(:require#?@(:clj- [[metabase.legacy-mbql.jvm-util :as mbql.jvm-u]- [metabase.models.dispatch :as models.dispatch]])+ [[metabase.legacy-mbql.jvm-util :as mbql.jvm-u]])[clojure.string :as str][metabase.legacy-mbql.predicates :as mbql.preds][metabase.legacy-mbql.schema :as mbql.s][metabase.legacy-mbql.schema.helpers :as schema.helpers][metabase.lib.schema.common :as lib.schema.common][metabase.lib.schema.id :as lib.schema.id]+ [metabase.lib.schema.ref :as lib.schema.ref][metabase.lib.util.match :as lib.util.match][metabase.util :as u][metabase.util.i18n :as i18n]@@ -28,7 +31,10 @@(mu/defn normalize-token :- [:or :keyword :string]"Convert a string or keyword in various cases (`lisp-case`, `snake_case`, or `SCREAMING_SNAKE_CASE`) to a lisp-cased- keyword."+ keyword.++ DEPRECATED: use [[metabase.lib.normalize]] going forward to normalize things."+ {:deprecated "0.57.0"}[token :- [:or :keyword :string]](let [s (u/qualified-name token)](if (str/starts-with? s "type/")@@ -478,7 +484,7 @@maybe-desugar-expression))(defmulti ^:private negate*- {:arglists '([mbql-clause])}+ {:arglists '([mbql-clause]), :deprecated "0.57.0"}first)(defmethod negate* :not [[_ subclause]] subclause)@@ -502,7 +508,10 @@(mu/defn negate-filter-clause :- mbql.s/Filter"Return the logical compliment of an MBQL filter clause, generally without using `:not` (except for the stringfilter clause types). Useful for generating highly optimized filter clauses and for drivers that do not support- top-level `:not` filter clauses."+ top-level `:not` filter clauses.++ Deprecated: use [[metabase.lib.core/negate-boolean-expression]] going forward."+ {:deprecated "0.57.0"}[filter-clause :- mbql.s/Filter]#_{:clj-kondo/ignore [:deprecated-var]}(-> filter-clause desugar-filter-clause negate* simplify-compound-filter))@@ -542,29 +551,9 @@:elsesource-table-id))-(defn dispatch-by-clause-name-or-class- "Dispatch function perfect for use with multimethods that dispatch off elements of an MBQL query. If `x` is an MBQL- clause, dispatches off the clause name; otherwise dispatches off `x`'s class."- ([x]- (letfn [(clause-type [x]- (when (mbql-clause? x)- (first x)))- (mlv2-lib-type [x]- (when (map? x)- (:lib/type x)))- (model-type [#?(:clj x :cljs _x)]- #?(:clj (models.dispatch/model x)- :cljs nil))]- (or- (clause-type x)- (mlv2-lib-type x)- (model-type x)- (type x))))- ([x _]- (dispatch-by-clause-name-or-class x)))-(mu/defn expression-with-name :- ::mbql.s/FieldOrExpressionDef"Return the expression referenced by a given `expression-name`."+ {:deprecated "0.57.0"}[inner-query expression-name :- ::lib.schema.common/non-blank-string](loop [{:keys [expressions source-query]} inner-query, found #{}](when (seq expressions)@@ -590,6 +579,7 @@This also handles nested queries, which could be potentially ambiguous if multiple levels had aggregations. Tosupport nested queries, you'll need to keep tract of how many `:source-query`s deep you've traveled; pass in thisnumber to as optional arg `nesting-level` to make sure you reference aggregations at the right level of nesting."+ {:deprecated "0.57.0"}([query index](aggregation-at-index query index 0))@@ -602,116 +592,6 @@;; keep recursing deeper into the query until we get to the same level the aggregation reference was defined at(recur {:query (get-in query [:query :source-query])} index (dec nesting-level)))))-(defn unique-name-generator- "Return a function that can be used to uniquify string names. Function maintains an internal counter that will suffix- any names passed to it as needed so all results will be unique.-- (let [unique-name (unique-name-generator)]- [(unique-name \"A\")- (unique-name \"B\")- (unique-name \"A\")])- ;; -> [\"A\" \"B\" \"A_2\"]-- By default, unique aliases are generated for each unique `[id original-name]` key pair. By default, a unique `id` is- generated for every call, meaning repeated calls to [[unique-name-generator]] with the same `original-name` will- return different unique aliases. If idempotence is desired, the function returned by the generator also has a 2- airity version with the signature-- (unique-name-fn id original-name)-- for example:-- (let [unique-name (unique-name-generator)]- [(unique-name :x \"A\")- (unique-name :x \"B\")- (unique-name :x \"A\")- (unique-name :y \"A\")])- ;; -> [\"A\" \"B\" \"A\" \"A_2\"]-- Finally, [[unique-name-generator]] accepts the following options to further customize behavior:-- ### `:name-key-fn`-- Generated aliases are unique by the value of `[id (name-key-fn original-name)]`; the default is `identity`, so by- default aliases are unique by `[id name-key-fn]`. Specify something custom here if you want to make the unique- aliases unique by some other value, for example to make them unique without regards to case:-- (let [f (unique-name-generator :name-key-fn str/lower-case)]- [(f \"x\")- (f \"X\")- (f \"X\")])- ;; -> [\"x\" \"X_2\" \"X_3\"]-- This is useful for databases that treat column aliases as case-insensitive (see #19618 for some examples of this).-- ### `:unique-alias-fn`-- The function used to generate a potentially-unique alias given an original alias and unique suffix with the signature-- (unique-alias-fn original suffix)-- By default, combines them like `original_suffix`, but you can supply a custom function if you need to change this- behavior:-- (let [f (unique-name-generator :unique-alias-fn (fn [x y] (format \"%s~~%s\" y x)))]- [(f \"x\")- (f \"x\")])- ;; -> [\"x\" \"2~~x\"]-- This is useful if you need to constrain the generated suffix in some way, for example by limiting its length or- escaping characters disallowed in a column alias.-- Values generated by this function are recursively checked for uniqueness, and will keep trying values a unique value- is generated; for this reason the function *must* return a unique value for every unique input. Use caution when- limiting the length of the identifier generated (consider appending a hash in cases like these)."- [& {:keys [name-key-fn unique-alias-fn]- :or {name-key-fn identity- unique-alias-fn (fn [original suffix]- (str original \_ suffix))}}]- (let [id+original->unique (atom {}) ; map of [id original-alias] -> unique-alias- original->count (atom {})] ; map of original-alias -> count- (fn generate-name- ([an-alias]- (generate-name (gensym) an-alias))-- ([id original]- (let [name-key (name-key-fn original)]- (or- ;; if we already have generated an alias for this key (e.g. `[id original]`), return it as-is.- (@id+original->unique [id name-key])- ;; otherwise generate a new unique alias.- ;; see if we're the first to try to use this candidate alias. Update the usage count in `original->count`- (let [total-count (get (swap! original->count update name-key (fnil inc 0)) name-key)]- (if (= total-count 1)- ;; if we are the first to do it, record it in `id+original->unique` and return it.- (do- (swap! id+original->unique assoc [id name-key] original)- original)- ;; otherwise prefix the alias by the current total count (e.g. `id` becomes `id_2`) and recur. If `id_2`- ;; is unused, it will get returned. Otherwise we'll recursively try `id_2_2`, and so forth.- (let [candidate (unique-alias-fn original (str total-count))]- ;; double-check that `unique-alias-fn` isn't doing something silly like truncating the generated alias- ;; to aggressively or forgetting to include the `suffix` -- otherwise we could end up with an infinite- ;; loop- (assert (not= candidate original)- (str "unique-alias-fn must return a different string than its input. Input: "- (pr-str candidate)))- (swap! id+original->unique assoc [id name-key] candidate)- (recur id candidate))))))))))--(mu/defn uniquify-names :- [:and- [:sequential :string]- [:fn- {:error/message "sequence of unique strings"}- distinct?]]- "Make the names in a sequence of string names unique by adding suffixes such as `_2`.-- (uniquify-names [\"count\" \"sum\" \"count\" \"count_2\"])- ;; -> [\"count\" \"sum\" \"count_2\" \"count_2_2\"]"- {:deprecated "0.57.0"}- [names :- [:sequential :string]]- (map (unique-name-generator) names))-(defn query->max-rows-limit"Calculate the absolute maximum number of results that should be returned by this query (MBQL or native), useful fordoing the equivalent of@@ -758,13 +638,18 @@x))(defn field-options- "Returns options in a `:field`, `:expression`, or `:aggregation` clause."+ "Returns options in a `:field`, `:expression`, or `:aggregation` clause.++ DEPRECATED: Use MBQL 5 + [[metabase.lib.core/options]] going forward."+ {:deprecated "0.57.0"}[[_ _ opts]]opts)(mu/defn update-field-options :- mbql.s/Reference- "Like [[clojure.core/update]], but for the options in a `:field`, `:expression`, or `:aggregation` clause."- {:arglists '([field-or-ag-ref-or-expression-ref f & args])}+ "Like [[clojure.core/update]], but for the options in a `:field`, `:expression`, or `:aggregation` clause.++ DEPRECATED: Use MBQL 5 + [[metabase.lib.core/update-options]] going forward."+ {:arglists '([field-or-ag-ref-or-expression-ref f & args]), :deprecated "0.57.0"}[[clause-type id-or-name opts] :- mbql.s/Reference f & args](let [opts (not-empty (remove-empty (apply f opts args)))];; `:field` clauses should have a `nil` options map if there are no options. `:aggregation` and `:expression`@@ -776,7 +661,10 @@:else [clause-type id-or-name])))(defn assoc-field-options- "Like [[clojure.core/assoc]], but for the options in a `:field`, `:expression`, or `:aggregation` clause."+ "Like [[clojure.core/assoc]], but for the options in a `:field`, `:expression`, or `:aggregation` clause.++ DEPRECATED: Use MBQL 5 + [[metabase.lib.core/update-options]] going forward."+ {:deprecated "0.57.0"}[clause & kvs](apply update-field-options clause assoc kvs))@@ -789,7 +677,7 @@;; it doesn't make sense to call this on an `:expression` or `:aggregation`.(assert (is-clause? :field clause))(if (or (not base-type)- (mbql.s/valid-temporal-unit-for-base-type? base-type unit))+ (lib.schema.ref/valid-temporal-unit-for-base-type? base-type unit))(assoc-field-options clause :temporal-unit unit)(do(log/warnf "%s is not a valid temporal unit for %s; not adding to clause %s" unit base-type (pr-str clause))@@ -810,34 +698,11 @@[:field (id :guard integer?) opts][id (:source-field opts)]))))-(defn pred-matches-form?- "Check if `form` or any of its children forms match `pred`. This function is used for validation; during normal- operation it will never match, so calling this function before `matching-locations` is more efficient."- [form pred]- (cond- (pred form) true- (map? form) (reduce-kv (fn [b _ v] (or b (pred-matches-form? v pred))) false form)- (sequential? form) (reduce (fn [b x] (or b (pred-matches-form? x pred))) false form)- :else false))--(defn matching-locations- "Find the forms matching pred, returns a list of tuples of location (as used in get-in) and the match."- [form pred]- ;; Surprisingly enough, a list works better as a stack here than a vector.- (loop [stack (list [[] form]), matches []]- (if-let [[loc form :as top] (peek stack)]- (let [stack (pop stack)- map-onto-stack #(transduce (map (fn [[k v]] [(conj loc k) v])) conj stack %)- seq-onto-stack #(transduce (map-indexed (fn [i v] [(conj loc i) v])) conj stack %)]- (cond- (pred form) (recur stack (conj matches top))- (map? form) (recur (map-onto-stack form) matches)- (sequential? form) (recur (seq-onto-stack form) matches)- :else (recur stack matches)))- matches)))-(defn wrap-field-id-if-needed- "Wrap a raw Field ID in a `:field` clause if needed."+ "Wrap a raw Field ID in a `:field` clause if needed.++ DEPRECATED: this operates on legacy MBQL. Use Lib for MBQL manipulation going forward."+ {:deprecated "0.57.0"}[field-id-or-form](cond(mbql-clause? field-id-or-form)diff --git a/src/metabase/lib/binning.cljc b/src/metabase/lib/binning.cljcindex c79e59f1e25..d4f0b000c1b 100644--- a/src/metabase/lib/binning.cljc+++ b/src/metabase/lib/binning.cljc@@ -234,6 +234,7 @@;;; belongs in Lib in its current shape.(defn ensure-binning-in-display-name"Update results column so binning is contained in its display_name."+ {:deprecated "0.57.0"}[column](if (:binning_info column)(update column :display_name #(ensure-ends-with-binning %1diff --git a/src/metabase/lib/card.cljc b/src/metabase/lib/card.cljcindex 43b9130032e..1a9b28ad144 100644--- a/src/metabase/lib/card.cljc+++ b/src/metabase/lib/card.cljc@@ -15,6 +15,7 @@[metabase.lib.schema.metadata :as lib.schema.metadata][metabase.lib.temporal-bucket :as lib.temporal-bucket][metabase.lib.util :as lib.util]+ [metabase.lib.util.unique-name-generator :as lib.util.unique-name-generator][metabase.util :as u][metabase.util.humanization :as u.humanization][metabase.util.i18n :as i18n]@@ -232,7 +233,7 @@;; do not truncate the desired column aliases coming back in card metadata, if the query returns a;; 'crazy long' column name then we need to use that in the next stage.;; See [[metabase.lib.card-test/propagate-crazy-long-identifiers-from-card-metadata-test]]- (lib.field.util/add-source-and-desired-aliases-xform metadata-providerable (lib.util/non-truncating-unique-name-generator))+ (lib.field.util/add-source-and-desired-aliases-xform metadata-providerable (lib.util.unique-name-generator/non-truncating-unique-name-generator))(cond-> result-cols(seq model-cols) (merge-model-metadata model-cols))))))))@@ -290,6 +291,4 @@(let [mp (lib.metadata/->metadata-provider metadata-providerable){card-query :dataset-query, result-metadata :result-metadata} card](cond-> (lib.query/query mp card-query)- result-metadata (lib.util/update-query-stage -1 (fn [stage]- (->> (assoc stage :lib/stage-metadata (lib.util/->stage-metadata result-metadata))- (lib.normalize/normalize ::lib.schema/stage)))))))+ result-metadata (lib.util/update-query-stage -1 assoc :lib/stage-metadata (lib.normalize/->normalized-stage-metadata result-metadata)))))diff --git a/src/metabase/lib/convert.cljc b/src/metabase/lib/convert.cljcindex 3cb369bfdcd..f6155bc5d17 100644--- a/src/metabase/lib/convert.cljc+++ b/src/metabase/lib/convert.cljc@@ -6,8 +6,8 @@[clojure.string :as str][malli.error :as me][medley.core :as m]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.schema :as mbql.s]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s][metabase.lib.convert.metadata-to-legacy :as lib.convert.metadata-to-legacy][metabase.lib.dispatch :as lib.dispatch][metabase.lib.hierarchy :as lib.hierarchy]@@ -17,6 +17,7 @@[metabase.lib.schema.expression :as lib.schema.expression][metabase.lib.schema.ref :as lib.schema.ref][metabase.lib.util :as lib.util]+ [metabase.lib.util.unique-name-generator :as lib.util.unique-name-generator][metabase.util :as u][metabase.util.log :as log][metabase.util.malli :as mu]@@ -173,7 +174,7 @@Only deduplicate the default `__join` aliases; we don't want the [[lib.util/unique-name-generator]] to touch otheraliases and truncate them or anything like that."[joins]- (let [unique-name-fn (lib.util/unique-name-generator)]+ (let [unique-name-fn (lib.util.unique-name-generator/unique-name-generator)](mapv (fn [join](cond-> join(= (:alias join) legacy-default-join-alias) (update :alias unique-name-fn)))@@ -767,6 +768,7 @@stage-number :- :intlegacy-ref :- some?](let [legacy-ref (->> #?(:clj legacy-ref :cljs (js->clj legacy-ref :keywordize-keys true))+ #_{:clj-kondo/ignore [:deprecated-var]}mbql.normalize/normalize-field-ref){aggregations :aggregation} (lib.util/query-stage query stage-number)](with-aggregation-list aggregationsdiff --git a/src/metabase/lib/convert/metadata_to_legacy.cljc b/src/metabase/lib/convert/metadata_to_legacy.cljcindex 41c0ad6250b..fc13b592f5e 100644--- a/src/metabase/lib/convert/metadata_to_legacy.cljc+++ b/src/metabase/lib/convert/metadata_to_legacy.cljc@@ -1,6 +1,8 @@(ns metabase.lib.convert.metadata-to-legacy(:require[medley.core :as m]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema]+ [metabase.lib.normalize :as lib.normalize][metabase.util :as u][metabase.util.malli :as mu][metabase.util.memoize :as u.memo]))@@ -17,7 +19,7 @@[k](f (keyword k))))-(mu/defn lib-metadata-column->legacy-metadata-column :- :map+(mu/defn lib-metadata-column->legacy-metadata-column :- :metabase.legacy-mbql.schema/legacy-column-metadata"Convert a kebab-case Lib results metadata column to a mixed-case legacy results metadata column:* Convert from all-kebab-case keys to legacy casing (simple keywords use snake case while@@ -31,4 +33,5 @@(-> col(update-keys lib-metadata-column-key->legacy-metadata-column-key)(m/update-existing :binning_info update-keys lib-metadata-column-key->legacy-metadata-column-key)- (dissoc :lib/type)))+ (dissoc :lib/type)+ (->> (lib.normalize/normalize :metabase.legacy-mbql.schema/legacy-column-metadata))))diff --git a/src/metabase/lib/core.cljc b/src/metabase/lib/core.cljcindex a2b2127f6db..43d0e5054ad 100644--- a/src/metabase/lib/core.cljc+++ b/src/metabase/lib/core.cljc@@ -41,6 +41,7 @@[metabase.lib.options][metabase.lib.order-by :as lib.order-by][metabase.lib.page]+ [metabase.lib.parameters][metabase.lib.parse :as lib.parse][metabase.lib.query :as lib.query][metabase.lib.ref :as lib.ref]@@ -55,6 +56,7 @@[metabase.lib.template-tags :as lib.template-tags][metabase.lib.temporal-bucket :as lib.temporal-bucket][metabase.lib.util :as lib.util]+ [metabase.lib.util.unique-name-generator][metabase.lib.validate :as lib.validate][metabase.lib.walk.util][metabase.util.namespaces :as shared.ns]))@@ -95,6 +97,7 @@lib.normalize/keep-memetabase.lib.options/keep-melib.order-by/keep-me+ metabase.lib.parameters/keep-melib.parse/keep-melib.query/keep-melib.ref/keep-me@@ -109,6 +112,7 @@lib.template-tags/keep-melib.temporal-bucket/keep-melib.util/keep-me+ metabase.lib.util.unique-name-generator/keep-memetabase.lib.walk.util/keep-me)(shared.ns/import-fns@@ -282,8 +286,6 @@filter-clausefilter-operatorfilter-parts- find-filter-for-legacy-filter- find-filterable-column-for-legacy-refandornot@@ -391,10 +393,23 @@order-bysorderable-columns][lib.normalize- normalize]+ normalize+ ->normalized-stage-metadata][metabase.lib.pagecurrent-pagewith-page]+ [metabase.lib.parameters+ parameter-target-dimension-options+ parameter-target-expression-name+ parameter-target-expression-options+ parameter-target-expression-ref+ parameter-target-field-id+ parameter-target-field-name+ parameter-target-field-options+ parameter-target-field-ref+ parameter-target-is-dimension?+ parameter-target-template-tag-name+ update-parameter-target-dimension-options][lib.parseparse][lib.query@@ -413,6 +428,8 @@with-wrapped-native-querywrap-native-query-with-mbql][lib.ref+ field-ref-id+ field-ref-nameref][lib.remove-replaceremove-clause@@ -462,6 +479,11 @@source-table-idsource-card-idupdate-query-stage]+ [metabase.lib.util.unique-name-generator+ non-truncating-unique-name-generator+ truncate-alias+ unique-name-generator+ unique-name-generator-with-options][lib.validatefind-bad-refs][metabase.lib.walk.utildiff --git a/src/metabase/lib/drill_thru/zoom_in_timeseries.cljc b/src/metabase/lib/drill_thru/zoom_in_timeseries.cljcindex 1873021cbfc..5e4998b9550 100644--- a/src/metabase/lib/drill_thru/zoom_in_timeseries.cljc+++ b/src/metabase/lib/drill_thru/zoom_in_timeseries.cljc@@ -34,7 +34,7 @@Question transformation:- Set default display"- (:refer-clojure :exclude [for])+ (:refer-clojure :exclude [some])(:require[metabase.lib.breakout :as lib.breakout][metabase.lib.drill-thru.common :as lib.drill-thru.common]diff --git a/src/metabase/lib/extraction.cljc b/src/metabase/lib/extraction.cljcindex c83d6240186..3bf0bfa97f0 100644--- a/src/metabase/lib/extraction.cljc+++ b/src/metabase/lib/extraction.cljc@@ -1,7 +1,8 @@(ns metabase.lib.extraction#?(:clj (:refer-clojure :exclude [for]))(:require- #?(:clj [metabase.util.performance :refer [for]])+ #?@(:clj+ ([metabase.util.performance :refer [for]]))[metabase.lib.expression :as lib.expression][metabase.lib.metadata :as lib.metadata][metabase.lib.metadata.calculation :as lib.metadata.calculation]@@ -12,6 +13,7 @@[metabase.lib.temporal-bucket :as lib.temporal-bucket][metabase.lib.types.isa :as lib.types.isa][metabase.lib.util :as lib.util]+ [metabase.lib.util.unique-name-generator :as lib.util.unique-name-generator][metabase.util.i18n :as i18n][metabase.util.malli :as mu]))@@ -112,7 +114,7 @@(let [unique-name-fn (->> (lib.util/query-stage query stage-number)(lib.metadata.calculation/returned-columns query stage-number)(map :name)- (lib.util/unique-name-generator))]+ (lib.util.unique-name-generator/unique-name-generator))](lib.expression/expressionquerystage-numberdiff --git a/src/metabase/lib/field/util.cljc b/src/metabase/lib/field/util.cljcindex 4a54d4efeed..fea32ce26cc 100644--- a/src/metabase/lib/field/util.cljc+++ b/src/metabase/lib/field/util.cljc@@ -8,6 +8,7 @@[metabase.lib.schema :as lib.schema][metabase.lib.schema.metadata :as lib.schema.metadata][metabase.lib.util :as lib.util]+ [metabase.lib.util.unique-name-generator :as lib.util.unique-name-generator][metabase.util.malli :as mu]))(mu/defn inherited-column? :- :boolean@@ -52,7 +53,7 @@The zero arity is a transducer version."([]- (let [deduplicated-name-fn (lib.util/non-truncating-unique-name-generator)]+ (let [deduplicated-name-fn (lib.util.unique-name-generator/non-truncating-unique-name-generator)](map (fn [col](assoc col:lib/original-name ((some-fn :lib/original-name :name) col)@@ -86,12 +87,12 @@;; | x | x_2 |;;(let [unique-name-generator (if (lib.util/native-stage? query -1)- (lib.util/non-truncating-unique-name-generator)- (lib.util/unique-name-generator))]+ (lib.util.unique-name-generator/non-truncating-unique-name-generator)+ (lib.util.unique-name-generator/unique-name-generator))](add-source-and-desired-aliases-xform query unique-name-generator)))([metadata-providerable :- ::lib.metadata.protocols/metadata-providerable- unique-name-fn :- ::lib.util/unique-name-generator]+ unique-name-fn :- :metabase.lib.util.unique-name-generator/unique-name-generator](comp (add-deduplicated-names)(map (fn [col](let [source-alias ((some-fn :lib/source-column-alias :lib/original-name :name) col)diff --git a/src/metabase/lib/filter.cljc b/src/metabase/lib/filter.cljcindex 77d8e25b4e8..f98d282341f 100644--- a/src/metabase/lib/filter.cljc+++ b/src/metabase/lib/filter.cljc@@ -3,9 +3,7 @@(:require[inflections.core :as inflections][medley.core :as m]- [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.common :as lib.common]- [metabase.lib.convert :as lib.convert][metabase.lib.dispatch :as lib.dispatch][metabase.lib.equality :as lib.equality][metabase.lib.filter.operator :as lib.filter.operator]@@ -521,44 +519,6 @@(lib.filter.operator/filter-operators col))(lib.filter.operator/operator-def op)))))-(mu/defn find-filter-for-legacy-filter :- [:maybe ::lib.schema.expression/boolean]- "Return the filter clause in `query` at stage `stage-number` matching the legacy- filter clause `legacy-filter`, if any."- ([query :- ::lib.schema/query- legacy-filter]- (find-filter-for-legacy-filter query -1 legacy-filter))-- ([query :- ::lib.schema/query- stage-number :- :int- legacy-filter :- some?]- (let [legacy-filter #_{:clj-kondo/ignore [:deprecated-var]} (mbql.normalize/normalize-fragment [:query :filter] legacy-filter)- query-filters (vec (filters query stage-number))- matching-filters (clojure.core/filter #(clojure.core/= #_{:clj-kondo/ignore [:deprecated-var]}- (mbql.normalize/normalize-fragment- [:query :filter]- (lib.convert/->legacy-MBQL %))- legacy-filter)- query-filters)]- (when (seq matching-filters)- (if (next matching-filters)- (throw (ex-info "Multiple matching filters found" {:legacy-filter legacy-filter- :query-filters query-filters- :matching-filters matching-filters}))- (first matching-filters))))))--;; TODO: Refactor this away - handle legacy refs in `lib.js` and call `lib.equality` from there.-(mu/defn find-filterable-column-for-legacy-ref :- [:maybe ColumnWithOperators]- "Given a legacy `:field` reference, return the filterable [[ColumnWithOperators]] that best fits it."- ([query legacy-ref]- (find-filterable-column-for-legacy-ref query -1 legacy-ref))-- ([query :- ::lib.schema/query- stage-number :- :int- legacy-ref :- some?]- (let [a-ref (lib.convert/legacy-ref->pMBQL query stage-number legacy-ref)- columns (filterable-columns query stage-number)]- (lib.equality/find-matching-column a-ref columns))))-(def ^:private FilterParts[:map[:lib/type [:= :mbql/filter-parts]]diff --git a/src/metabase/lib/join.cljc b/src/metabase/lib/join.cljcindex 73216982833..33a1b82aa15 100644--- a/src/metabase/lib/join.cljc+++ b/src/metabase/lib/join.cljc@@ -31,6 +31,7 @@[metabase.lib.types.isa :as lib.types.isa][metabase.lib.util :as lib.util][metabase.lib.util.match :as lib.util.match]+ [metabase.lib.util.unique-name-generator :as lib.util.unique-name-generator][metabase.util :as u][metabase.util.i18n :as i18n][metabase.util.log :as log]@@ -611,7 +612,7 @@condition)))(defn- generate-unique-name [base-name taken-names]- (let [generator (lib.util/unique-name-generator)]+ (let [generator (lib.util.unique-name-generator/unique-name-generator)](run! generator taken-names)(generator base-name)))diff --git a/src/metabase/lib/join/util.cljc b/src/metabase/lib/join/util.cljcindex 354505ae1cd..157e4f636b9 100644--- a/src/metabase/lib/join/util.cljc+++ b/src/metabase/lib/join/util.cljc@@ -56,7 +56,7 @@MyJoin__my_fieldYou should pass the results thru a unique name function e.g. one returned- by [[metabase.lib.util/unique-name-generator]]."+ by [[metabase.lib.util.unique-name-generator/unique-name-generator]]."[join-alias :- ::lib.schema.common/non-blank-stringfield-name :- ::lib.schema.common/non-blank-string](lib.util/format "%s__%s" join-alias field-name))@@ -71,9 +71,9 @@CATEGORIES__via__CATEGORY_ID__via__CATEGORIESYou should make sure this gets ran thru a unique-name fn e.g. one returned- by [[metabase.lib.util/unique-name-generator]]."- [table-name :- ::lib.schema.common/non-blank-string- fk-field-name :- ::lib.schema.common/non-blank-string+ by [[metabase.lib.util.unique-name-generator/unique-name-generator]]."+ [table-name :- ::lib.schema.common/non-blank-string+ fk-field-name :- ::lib.schema.common/non-blank-stringfk-join-alias :- [:maybe ::lib.schema.common/non-blank-string]](if fk-join-alias(lib.util/format "%s__via__%s__via__%s" table-name fk-field-name fk-join-alias)diff --git a/src/metabase/lib/js.cljs b/src/metabase/lib/js.cljsindex ab66804338b..3c964f8b08f 100644--- a/src/metabase/lib/js.cljs+++ b/src/metabase/lib/js.cljs@@ -59,8 +59,7 @@[clojure.string :as str][goog.object :as gobject][medley.core :as m]- [metabase.legacy-mbql.js :as mbql.js]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.cache :as lib.cache][metabase.lib.convert :as lib.convert][metabase.lib.core :as lib.core]@@ -80,6 +79,7 @@[metabase.lib.schema.ref :as lib.schema.ref][metabase.lib.types.isa :as lib.types.isa][metabase.lib.util :as lib.util]+ [metabase.lib.util.unique-name-generator :as lib.util.unique-name-generator][metabase.util :as u][metabase.util.log :as log][metabase.util.memoize :as memoize]@@ -635,8 +635,22 @@(lib.core/normalize (js->clj source-clause :keywordize-keys true))(lib.core/normalize (js->clj target-clause :keywordize-keys true))))+(defn- -unwrap+ "Sometimes JS queries are passed in with a `Join` or `Aggregation` clause object instead of a simple Array.+ These clauses `extend Array` so `Array.isArray(x)` is true, but they're treated as opaque by `js->clj`.+ This recurses over the whole query, unwrapping these values to their `.raw()` form."+ [x]+ (cond+ ;; (object? x) only matches for things that are plain objects. eg. `(object? (js/Date.))` is false.+ ;; This matches anything that descends from `Object`, like `Join` clause, and has a `.raw()` method.+ (and (instance? js/Object x)+ (fn? (.-raw x))) (-> x (.raw) js->clj -unwrap)+ (map? x) (update-vals x -unwrap)+ (sequential? x) (mapv -unwrap x)+ :else x))+(defn- unwrap [a-query]- (let [a-query (mbql.js/unwrap a-query)]+ (let [a-query (-unwrap a-query)](cond-> a-query(map? a-query) (:dataset_query a-query))))@@ -965,10 +979,6 @@[a-query stage-number](to-array (lib.core/filters a-query stage-number)))-;; TODO: find-filter-for-legacy-filter is dead code and should be removed.--;; TODO: find-filterable-column-for-legacy-ref is dead code and should be removed.-;; # Expressions;; Custom expressions are parsed from a string by a TS library, which returns legacy MBQL clauses. That may get ported;; to Clojure someday, but perhaps not - it's quite standalone and there's no use case for that logic in the BE.@@ -1381,7 +1391,7 @@"Inner implementation for [[returned-columns]], which wraps this with caching."[a-query stage-number](let [stage (lib.util/query-stage a-query stage-number)- unique-name-fn (lib.util/unique-name-generator)]+ unique-name-fn (lib.util.unique-name-generator/unique-name-generator)](->> (lib.metadata.calculation/returned-columns a-query stage-number stage)(map #(-> %(assoc :selected? true)@@ -1473,6 +1483,7 @@(-> a-legacy-ref(js->clj :keywordize-keys true)(update 0 keyword)+ #_{:clj-kondo/ignore [:deprecated-var]}mbql.normalize/normalize-field-reflib.convert/->pMBQL(->> (lib.normalize/normalize ::lib.schema.ref/ref))))diff --git a/src/metabase/lib/metadata/result_metadata.cljc b/src/metabase/lib/metadata/result_metadata.cljcindex 59047fc642d..1857a34e939 100644--- a/src/metabase/lib/metadata/result_metadata.cljc+++ b/src/metabase/lib/metadata/result_metadata.cljc@@ -13,8 +13,8 @@[clojure.set :as set][clojure.string :as str][medley.core :as m]- [metabase.legacy-mbql.schema :as mbql.s]- [metabase.legacy-mbql.util :as mbql.u]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s]+ ^{:clj-kondo/ignore [:discouraged-namespace :deprecated-namespace]} [metabase.legacy-mbql.util :as mbql.u][metabase.lib.aggregation :as lib.aggregation][metabase.lib.card :as lib.card][metabase.lib.convert :as lib.convert]@@ -236,6 +236,7 @@used by individual pieces of middleware or driver implementations for tracking little bits of information thatshould not be considered relevant when comparing clauses for equality."[legacy-ref]+ ^{:clj-kondo/ignore [:deprecated-var]}(mbql.u/update-field-options legacy-ref (partial into {} (remove (fn [[k _]](qualified-keyword? k))))))diff --git a/src/metabase/lib/metric.cljc b/src/metabase/lib/metric.cljcindex cf1fa3c534e..c563694a457 100644--- a/src/metabase/lib/metric.cljc+++ b/src/metabase/lib/metric.cljc@@ -3,7 +3,7 @@stuff is TBH.)"(:refer-clojure :exclude [select-keys])(:require- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.aggregation :as lib.aggregation][metabase.lib.convert :as lib.convert][metabase.lib.join :as lib.join]diff --git a/src/metabase/lib/normalize.cljc b/src/metabase/lib/normalize.cljcindex 03aa9571d25..8bc101704d3 100644--- a/src/metabase/lib/normalize.cljc+++ b/src/metabase/lib/normalize.cljc@@ -7,9 +7,11 @@[metabase.lib.schema :as lib.schema][metabase.lib.schema.mbql-clause :as lib.schema.mbql-clause][metabase.lib.schema.metadata :as lib.schema.metadata]+ [metabase.lib.util :as lib.util][metabase.util :as u][metabase.util.i18n :as i18n][metabase.util.log :as log]+ [metabase.util.malli :as mu][metabase.util.malli.registry :as mr][metabase.util.performance :refer [some]]))@@ -94,3 +96,12 @@{:schema schema, :x x, :error error})))](thunk))(thunk)))))++(mu/defn ->normalized-stage-metadata :- ::lib.schema.metadata/stage+ "Take a sequence of legacy or Lib metadata maps, convert to Lib-style if needed, then normalize them.++ Note that this returns a map with `:columns` rather than a sequence of columns like the input."+ [cols :- [:sequential :map]]+ (->> cols+ lib.util/->stage-metadata+ (normalize ::lib.schema.metadata/stage)))diff --git a/src/metabase/lib/page.clj b/src/metabase/lib/page.cljdeleted file mode 100644index ad49c7e54c8..00000000000--- a/src/metabase/lib/page.clj+++ /dev/null@@ -1,25 +0,0 @@-(ns metabase.lib.page- (:require- [metabase.lib.schema :as lib.schema]- [metabase.lib.util :as lib.util]- [metabase.util :as u]- [metabase.util.malli :as mu]))--(mu/defn current-page :- [:maybe ::lib.schema/page]- "Return the `:page` in a query stage."- ([query]- (current-page query -1))-- ([query :- ::lib.schema/query- stage-number :- :int]- (:page (lib.util/query-stage query stage-number))))--(mu/defn with-page :- ::lib.schema/query- "Set or remove the `:page` in a query stage."- ([query page]- (with-page query -1 page))-- ([query :- ::lib.schema/query- stage-number :- :int- page :- [:maybe ::lib.schema/page]]- (lib.util/update-query-stage query stage-number u/assoc-dissoc :page page)))diff --git a/src/metabase/lib/parameters.cljc b/src/metabase/lib/parameters.cljcnew file mode 100644index 00000000000..41f038cbcca--- /dev/null+++ b/src/metabase/lib/parameters.cljc@@ -0,0 +1,121 @@+(ns metabase.lib.parameters+ "TODO (Cam 10/10/25) -- all this code is only necessary because the `:dimension` 'clause' in a parameter/parameter+ mapping `:target` does not use standard MBQL 5 clause syntax. I avoided the change to avoid breaking all the+ existing code that manipulates MBQL 4 by hand. Most of the BE code in question has been ported, except for drivers+ parameter code. The FE code still creates these by hand. One of these days we'll want to go clean all this stuff up.++ See https://metaboat.slack.com/archives/C0645JP1W81/p1760128892249879"+ (:require+ [metabase.lib.convert :as lib.convert]+ [metabase.lib.options :as lib.options]+ [metabase.lib.ref :as lib.ref]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.lib.schema.parameter :as lib.schema.parameter]+ [metabase.lib.schema.ref :as lib.schema.ref]+ [metabase.util.malli :as mu]))++(mu/defn- parameter-target-template-tag :- [:maybe ::lib.schema.parameter/template-tag]+ [target :- ::lib.schema.parameter/target]+ (case (first target)+ :dimension (let [[_tag dimension] target]+ (case (first dimension)+ :template-tag dimension+ nil))+ :variable (let [[_tag variable] target]+ (case (first variable)+ :template-tag variable+ nil))+ nil))++(mu/defn parameter-target-template-tag-name :- [:maybe :string]+ "If parameter `:target` is a `:template-tag` psuedo-clause, return the name of the template tag it references."+ [target :- ::lib.schema.parameter/target]+ (when-let [[_tag tag-name] (parameter-target-template-tag target)]+ (cond+ (string? tag-name) tag-name+ (map? tag-name) (:id tag-name))))++(mu/defn- parameter-target-legacy-field-ref :- [:maybe ::lib.schema.parameter/target.legacy-field-ref]+ [target :- ::lib.schema.parameter/target]+ (case (first target)+ :dimension (let [[_tag dimension] target]+ (case (first dimension)+ :field dimension+ nil))+ :variable (let [[_tag variable] target]+ (case (first variable)+ :field variable+ nil))+ :field target+ nil))++(mu/defn parameter-target-field-ref :- [:maybe :mbql.clause/field]+ "If a parameter `:target` wraps a legacy `:field` ref, find it, convert it to MBQL 5, and return it."+ [target :- ::lib.schema.parameter/target]+ (some-> target parameter-target-legacy-field-ref lib.convert/->pMBQL))++(mu/defn parameter-target-field-id :- [:maybe ::lib.schema.id/field]+ "If a parameter `:target` wraps a field ID ref return the Field ID."+ [target :- ::lib.schema.parameter/target]+ (some-> target parameter-target-field-ref lib.ref/field-ref-id))++(mu/defn parameter-target-field-name :- [:maybe :string]+ "If a parameter `:target` wraps a field name ref return the Field ID."+ [target :- ::lib.schema.parameter/target]+ (some-> target parameter-target-field-ref lib.ref/field-ref-name))++(mu/defn parameter-target-field-options :- [:maybe ::lib.schema.ref/field.options]+ "If a parameter `:target` wraps a `:field` ref return the ref options map."+ [target :- ::lib.schema.parameter/target]+ (some-> target parameter-target-field-ref lib.options/options))++(mu/defn- parameter-target-legacy-expression-ref :- [:maybe ::lib.schema.parameter/target.legacy-expression-ref]+ [target :- ::lib.schema.parameter/target]+ (case (first target)+ :dimension (let [[_tag dimension] target]+ (case (first dimension)+ :expression dimension+ nil))+ nil))++(mu/defn parameter-target-expression-ref :- [:maybe :mbql.clause/expression]+ "If a parameter `:target` wraps a legacy `:expression` ref, find it, convert it to MBQL 5, and return it."+ [target :- ::lib.schema.parameter/target]+ (some-> target parameter-target-legacy-expression-ref lib.convert/->pMBQL))++(mu/defn parameter-target-expression-name :- [:maybe :string]+ "If a parameter `:target` wraps an `:expression` ref return the expression name."+ [target :- ::lib.schema.parameter/target]+ (some-> target parameter-target-expression-ref lib.ref/expression-ref-name))++(mu/defn parameter-target-expression-options :- [:maybe ::lib.schema.ref/expression.options]+ "If a parameter `:target` wraps an `:expression` ref return the ref options map."+ [target :- ::lib.schema.parameter/target]+ (some-> target parameter-target-expression-ref lib.options/options))++(mu/defn parameter-target-is-dimension?+ "Whether a parameter `:target` is a `:dimension` psuedo-clause. (Generally, you don't want to check this because it's+ pretty meaningless) -- see thread https://metaboat.slack.com/archives/C0645JP1W81/p1760128892249879 for more+ details."+ [target :- ::lib.schema.parameter/target]+ (case (first target)+ :dimension true+ false))++(mu/defn parameter-target-dimension-options :- [:maybe ::lib.schema.parameter/dimension.options]+ "If parameter `:target` is a `:dimension` pseudo-clause, return the options map associated with it, if any."+ [target :- ::lib.schema.parameter/target]+ (case (first target)+ :dimension (let [[_tag _dimension opts] target]+ opts)+ nil))++(mu/defn update-parameter-target-dimension-options :- ::lib.schema.parameter/target+ "If parameter `:target` is a `:dimension` pseudo-clause, update the options map associated with it, if any. If it is+ not a `:dimension` psuedo-clause, this function no-ops."+ [target :- ::lib.schema.parameter/target+ f & args]+ (case (first target)+ :dimension (let [[_tag dimension opts] target]+ [:dimension dimension (apply f opts args)])+ target))diff --git a/src/metabase/lib/ref.cljc b/src/metabase/lib/ref.cljcindex 5ef091f912c..f725f22548c 100644--- a/src/metabase/lib/ref.cljc+++ b/src/metabase/lib/ref.cljc@@ -2,6 +2,7 @@(:refer-clojure :exclude [ref])(:require[metabase.lib.dispatch :as lib.dispatch]+ [metabase.lib.schema.id :as lib.schema.id][metabase.lib.schema.ref :as lib.schema.ref][metabase.util.malli :as mu]))@@ -24,3 +25,20 @@create a new UUID every time this is called."[x :- some?](ref-method x))++(mu/defn field-ref-id :- [:maybe ::lib.schema.id/field]+ "If a `:field` ref uses a Field ID, return that ID."+ [[_tag _opts id-or-name, :as _field-ref] :- :mbql.clause/field]+ (when (pos-int? id-or-name)+ id-or-name))++(mu/defn field-ref-name :- [:maybe :string]+ "If a `:field` ref uses a Field name, return that name."+ [[_tag _opts id-or-name, :as _field-ref] :- :mbql.clause/field]+ (when (string? id-or-name)+ id-or-name))++(mu/defn expression-ref-name :- [:maybe :string]+ "Return the expression name from an `:expression` ref."+ [[_tag _opts expression-name, :as _expression-ref] :- :mbql.clause/expression]+ expression-name)diff --git a/src/metabase/lib/remove_replace.cljc b/src/metabase/lib/remove_replace.cljcindex c38ddc5c03d..e250a88b9f7 100644--- a/src/metabase/lib/remove_replace.cljc+++ b/src/metabase/lib/remove_replace.cljc@@ -18,6 +18,7 @@[metabase.lib.schema.util :as lib.schema.util][metabase.lib.util :as lib.util][metabase.lib.util.match :as lib.util.match]+ [metabase.lib.util.unique-name-generator :as lib.util.unique-name-generator][metabase.util :as u][metabase.util.log :as log][metabase.util.malli :as mu]@@ -546,7 +547,7 @@[idx old-name] (when (< -1 idx (count the-joins))[idx (get-in the-joins [idx :alias])])](if (and idx (not= old-name new-name))- (let [unique-name-fn (lib.util/unique-name-generator)+ (let [unique-name-fn (lib.util.unique-name-generator/unique-name-generator)_ (run! unique-name-fn (map :alias the-joins))unique-name (unique-name-fn new-name)](-> stagediff --git a/src/metabase/lib/schema.cljc b/src/metabase/lib/schema.cljcindex c65ed669ff5..5d8a7b0bd64 100644--- a/src/metabase/lib/schema.cljc+++ b/src/metabase/lib/schema.cljc@@ -9,7 +9,6 @@(:refer-clojure :exclude [ref every? some select-keys])(:require[medley.core :as m]- [metabase.legacy-mbql.util :as mbql.u][metabase.lib.schema.actions :as actions][metabase.lib.schema.aggregation :as aggregation][metabase.lib.schema.common :as common]@@ -154,15 +153,15 @@form (-> (stage-with-joins-and-namespaced-keys-removed stage);; also ignore expression refs inside `:parameters` since they still use legacy syntax these days.(dissoc :parameters))]- (when (mbql.u/pred-matches-form? form pred)- (mbql.u/matching-locations form pred))))+ (when (lib.schema.util/pred-matches-form? form pred)+ (lib.schema.util/matching-locations form pred))))(defn- aggregation-ref-errors-for-stage [stage](let [uuids (into #{} (map (comp :lib/uuid second)) (:aggregation stage))pred #(bad-ref-clause? :aggregation uuids %)form (stage-with-joins-and-namespaced-keys-removed stage)]- (when (mbql.u/pred-matches-form? form pred)- (mbql.u/matching-locations form pred))))+ (when (lib.schema.util/pred-matches-form? form pred)+ (lib.schema.util/matching-locations form pred))))(defn ref-errors-for-stage"Return the locations and the clauses with dangling expression or aggregation references.diff --git a/src/metabase/lib/schema/id.cljc b/src/metabase/lib/schema/id.cljcindex b9b83dd283e..8112685b0f4 100644--- a/src/metabase/lib/schema/id.cljc+++ b/src/metabase/lib/schema/id.cljc@@ -83,7 +83,8 @@pos-int?)(mr/def ::transform- [:schema {:doc/title "Valid Transform ID"} pos-int?])+ "Valid Transform ID"+ pos-int?)(mr/def ::collection"Valid Collection ID"diff --git a/src/metabase/lib/schema/metadata.cljc b/src/metabase/lib/schema/metadata.cljcindex 88c65981b8f..ddbd03990c6 100644--- a/src/metabase/lib/schema/metadata.cljc+++ b/src/metabase/lib/schema/metadata.cljc@@ -326,7 +326,7 @@;; came from. Prefer one of the other name keys instead, only falling back to `:name` if they are not present.[:name :string];; TODO -- ignore `base_type` and make `effective_type` required; see #29707- [:base-type ::lib.schema.common/base-type]+ [:base-type {:default :type/*} ::lib.schema.common/base-type];; This is nillable because internal remap columns have `:id nil`.[:id {:optional true} [:maybe ::lib.schema.id/field]][:display-name {:optional true} [:maybe :string]]diff --git a/src/metabase/lib/schema/parameter.cljc b/src/metabase/lib/schema/parameter.cljcindex 8f274e2d0ed..ffb4a243376 100644--- a/src/metabase/lib/schema/parameter.cljc+++ b/src/metabase/lib/schema/parameter.cljc@@ -13,7 +13,7 @@what type of widget to display, and also tells us what types of parameters we should allow. Examples:`:date/all-options`, `:category`, etc.- One type is used in the [[Parameter]] list (`:parameters`):+ One type is used in the list (`:parameters`):3. Parameter `:type` -- specifies the type of the value being passed in. e.g. `:text` or `:string/!=`@@ -27,6 +27,7 @@[malli.core :as mc][metabase.lib.schema.common :as lib.schema.common][metabase.util :as u]+ [metabase.util.malli :as mu][metabase.util.malli.registry :as mr]))(defn- variadic-opts-first@@ -158,12 +159,28 @@(keys types)))(mr/def ::widget-type+ "The type of widget to display in the FE UI for the user to use to pick values for this parameter."(into [:enum- {:error/message "valid template tag widget type"+ {:error/message "valid parameter widget type":decode/normalize lib.schema.common/normalize-keyword}:none](keys types)))+(mu/defn parameter-type-and-widget-type-allowed-together?+ "Whether `parameter-type` (the `:type` of the value in an MBQL query `:parameters` list, e.g. `:text`) and+ `widget-type` (the declared type of the parameter in the Card/Dashboard definition, e.g. `:number`) are compatible."+ [parameter-type :- ::type+ widget-type :- ::widget-type]+ (when-let [allowed-template-tag-types (get-in types [parameter-type :allowed-for])]+ (contains? allowed-template-tag-types widget-type)))++(mu/defn allowed-parameter-types-for-template-tag-widget-type :- [:set ::type]+ "Set of allowed parameter types for a given `:widget-type`."+ [widget-type :- ::widget-type]+ (into #{} (for [[parameter-type {:keys [allowed-for]}] types+ :when (contains? allowed-for widget-type)]+ parameter-type)))+;; the next few clauses are used for parameter `:target`... this maps the parameter to an actual template tag in a;; native query or Field for MBQL queries.;;@@ -248,19 +265,22 @@[:target ::dimension.target][:options [:? [:maybe ::dimension.options]]]])+(mr/def ::template-tag.tag-name+ [:multi {:dispatch map?}+ [true [:map+ [:id ::lib.schema.common/non-blank-string]]]+ [false [:schema+ {:decode/normalize (fn [x]+ (cond-> x+ (keyword? x) u/qualified-name))}+ ::lib.schema.common/non-blank-string]]])+(mr/def ::template-tag"This is the reference like [:template-tag <whatever>], not the schema for native query template tags -- that livesin [[metabase.lib.schema.template-tag]]."[:tuple#_tag [:= {:decode/normalize lib.schema.common/normalize-keyword} :template-tag]- #_tag-name [:multi {:dispatch map?}- [true [:map- [:id ::lib.schema.common/non-blank-string]]]- [false [:schema- {:decode/normalize (fn [x]- (cond-> x- (keyword? x) u/qualified-name))}- ::lib.schema.common/non-blank-string]]]])+ #_tag-name ::template-tag.tag-name])(mr/def ::variable.target[:multi {:dispatch lib.schema.common/mbql-clause-tag@@ -285,7 +305,11 @@[:multi {:dispatch (fn [x](if (pos-int? x):field- (lib.schema.common/mbql-clause-tag x)))+ (let [tag (lib.schema.common/mbql-clause-tag x)]+ ;; MBQL 3 refs like `:field-id` should get normalized to `:field`+ (case tag+ (:field-id :field-literal :fk->) :field+ tag)))):error/fn (fn [{:keys [value]} _](str "Invalid parameter :target, must be either :field, :dimension, :variable, or :text-tag; got: "(pr-str value)))@@ -297,14 +321,10 @@x))};; TODO (Cam 9/12/25) -- the old legacy MBQL schema also said `:expression` refs where allowed here, but I don't;; know if we actually did allow that in practice.- [:dimension [:ref ::dimension]]- [:variable [:ref ::variable]]- [:text-tag [:ref ::text-tag]]- [:field [:ref ::target.legacy-field-ref]]- ;; MBQL 3 refs like `:field-id` should get normalized to `:field`- [:field-id [:ref ::target.legacy-field-ref]]- [:field-literal [:ref ::target.legacy-field-ref]]- [:fk-> [:ref ::target.legacy-field-ref]]])+ [:dimension [:ref ::dimension]]+ [:variable [:ref ::variable]]+ [:text-tag [:ref ::text-tag]]+ [:field [:ref ::target.legacy-field-ref]]])(defn- normalize-parameter[param]@@ -338,7 +358,10 @@(mr/def ::parameter"Schema for the *value* of a parameter (e.g. a Dashboard parameter or a native query template tag) as passed in as- part of the `:parameters` list in a query."+ part of the `:parameters` list in a query.++ Note that this is different from the parameter declarations that are saved as part of Dashboards and Cards; for THAT+ schema refer to `:metabase.parameters.schema/parameter`."[:and[:map{:decode/normalize #'normalize-parameter}@@ -348,7 +371,7 @@[:id {:optional true} [:ref ::id]][:target {:optional true} [:ref ::target]];; not specified if the param has no value. TODO - make this stricter; type of `:value` should be validated based- ;; on the [[ParameterType]]+ ;; on the `::type`[:value {:optional true} [:ref ::parameter.value]];; the name of the parameter we're trying to set -- this is actually required now I think, or at least needs to get;; merged in appropriatelydiff --git a/src/metabase/lib/schema/ref.cljc b/src/metabase/lib/schema/ref.cljcindex 7d6e8bc8f8d..7cd2d59e92c 100644--- a/src/metabase/lib/schema/ref.cljc+++ b/src/metabase/lib/schema/ref.cljc@@ -17,41 +17,109 @@(comment metabase.types.core/keep-me)+(defn valid-temporal-unit-for-base-type?+ "Whether `temporal-unit` (e.g. `:day`) is valid for the given `base-type` (e.g. `:type/Date`). If either is `nil` this+ will return truthy. Accepts either map of `field-options` or `base-type` and `temporal-unit` passed separately."+ ([{:keys [base-type temporal-unit] :as _field-options}]+ (valid-temporal-unit-for-base-type? base-type temporal-unit))++ ([base-type temporal-unit]+ (if-let [units (when (and temporal-unit+ (not= temporal-unit :default)+ base-type)+ (condp #(isa? %2 %1) base-type+ :type/Date temporal-bucketing/date-bucketing-units+ :type/Time temporal-bucketing/time-bucketing-units+ :type/DateTime temporal-bucketing/datetime-bucketing-units+ nil))]+ (contains? units temporal-unit)+ true)))++(defn normalize-field-options-map+ "Normalize a `:field` ref options map."+ [m]+ (when (map? m)+ (let [m (common/normalize-options-map m)]+ ;; remove nil values+ (reduce-kv+ (fn [m k v]+ (cond-> m+ (nil? v)+ (dissoc k)))+ m+ m))))+(mr/def ::field.options- [:merge- {:encode/serialize (fn [opts]- (m/filter-keys (fn [k]- (or (simple-keyword? k)- (= (namespace k) "lib")))- opts))}- ::common/options- [:map- [:join-alias {:optional true} [:ref ::lib.schema.join/alias]]- [:temporal-unit {:optional true} [:ref ::temporal-bucketing/unit]]- [:binning {:optional true} [:ref ::binning/binning]]- [:lib/original-binning {:optional true} [:ref ::binning/binning]]- [:metabase.lib.field/original-effective-type {:optional true} [:ref ::common/base-type]]- [:metabase.lib.field/original-temporal-unit {:optional true} [:ref ::temporal-bucketing/unit]]- ;;- ;; for implicitly joinable columns, this is the ID of the FK in `:source-table` (or the previous stage) used to- ;; perform the implicit join. E.g. if the query is against `ORDERS` and the field ref is for `CATEGORIES.NAME`- ;; then `:source-field` should be `ORDERS.CATEGORY_ID`. This column should have `:fk-target-field-id` that points- ;; to `CATEGORIES.ID`. This (ideally) should only be specified in the stage of the query the implicit join is to- ;; be done; subsequent stages should omit this and use field name refs instead e.g. `CATEGORIES__via__CATEGORY_ID`.- ;;- ;; You REALLY shouldn't be specifying this for a field name ref, since it makes resolution 10x harder. There's- ;; a 99.9% chance that using a field name ref with `:source-field` is a bad idea and broken, I even- ;; considered banning it at the schema level, but decided to let it be for now since we should still be- ;; able to resolve it.- [:source-field {:optional true} [:ref ::id/field]]- ;;- ;; Inherited temporal unit captures the temporal unit, that has been set on a ref, for next stages. It is attached- ;; _to a column_, which is created from this ref by means of `returned-columns`, ie. is visible [inherited temporal- ;; unit] in next stages only. This information is used eg. to help pick a default _temporal unit_ for columns that- ;; are bucketed -- if a column contains `:inherited-temporal-unit`, it was bucketed already in previous stages,- ;; so nil default picked to avoid another round of bucketing. Shall user bucket the column again, they have to- ;; select the bucketing explicitly in QB.- [:inherited-temporal-unit {:optional true} [:ref ::temporal-bucketing/unit]]]])+ [:and+ [:merge+ {:encode/serialize (fn [opts]+ (m/filter-keys (fn [k]+ (or (simple-keyword? k)+ (= (namespace k) "lib")))+ opts))+ :decode/normalize normalize-field-options-map}+ ::common/options+ [:map+ ;;+ ;; `:join-alias` is used to refer to a FieldOrExpression from a different Table/nested query that you are+ ;; EXPLICITLY JOINING against.+ [:join-alias {:optional true} [:ref ::lib.schema.join/alias]]+ ;;+ ;; `:temporal-unit` is used to specify DATE BUCKETING for a FieldOrExpression that represents a moment in time of some sort.+ ;;+ ;; There is no requirement that all `:type/Temporal` derived FieldOrExpressions specify a `:temporal-unit`, but+ ;; for legacy reasons `:field` clauses that refer to `:type/DateTime` FieldOrExpressions will be automatically+ ;; \"bucketed\" in the `:breakout` and `:filter` clauses, but nowhere else. Auto-bucketing only applies to+ ;; `:filter` clauses when values for comparison are `yyyy-MM-dd` date strings. See the `auto-bucket-datetimes`+ ;; middleware for more details. `:field` clauses elsewhere will not be automatically bucketed, so drivers still+ ;; need to make sure they do any special datetime handling for plain `:field` clauses when their+ ;; FieldOrExpression derives from `:type/DateTime`.+ [:temporal-unit {:optional true} [:ref ::temporal-bucketing/unit]]+ ;;+ ;; Using binning requires the driver to support the `:binning` feature.+ [:binning {:optional true} [:ref ::binning/binning]]+ [:lib/original-binning {:optional true} [:ref ::binning/binning]]+ [:metabase.lib.field/original-effective-type {:optional true} [:ref ::common/base-type]]+ [:metabase.lib.field/original-temporal-unit {:optional true} [:ref ::temporal-bucketing/unit]]+ ;;+ ;; for implicitly joinable columns, this is the ID of the FK in `:source-table` (or the previous stage) used to+ ;; perform the implicit join. E.g. if the query is against `ORDERS` and the field ref is for `CATEGORIES.NAME`+ ;; then `:source-field` should be `ORDERS.CATEGORY_ID`. This column should have `:fk-target-field-id` that points+ ;; to `CATEGORIES.ID`. This (ideally) should only be specified in the stage of the query the implicit join is to+ ;; be done; subsequent stages should omit this and use field name refs instead e.g. `CATEGORIES__via__CATEGORY_ID`.+ ;;+ ;; You REALLY shouldn't be specifying this for a field name ref, since it makes resolution 10x harder. There's+ ;; a 99.9% chance that using a field name ref with `:source-field` is a bad idea and broken, I even+ ;; considered banning it at the schema level, but decided to let it be for now since we should still be+ ;; able to resolve it.+ ;;+ ;; If both `:source-field` and `:join-alias` are supplied, `:join-alias` should be used to perform the join;+ ;; `:source-field` should be for information purposes only.+ [:source-field {:optional true} [:ref ::id/field]]+ ;;+ ;; The name or desired alias of the field used for an implicit join.+ [:source-field-name {:optional true} ::common/non-blank-string]+ ;;+ ;; The join alias of the source field used for an implicit join.+ [:source-field-join-alias {:optional true} ::common/non-blank-string]+ ;;+ ;; Inherited temporal unit captures the temporal unit, that has been set on a ref, for next stages. It is attached+ ;; _to a column_, which is created from this ref by means of `returned-columns`, ie. is visible [inherited temporal+ ;; unit] in next stages only. This information is used eg. to help pick a default _temporal unit_ for columns that+ ;; are bucketed -- if a column contains `:inherited-temporal-unit`, it was bucketed already in previous stages,+ ;; so nil default picked to avoid another round of bucketing. Shall user bucket the column again, they have to+ ;; select the bucketing explicitly in QB.+ [:inherited-temporal-unit {:optional true} [:ref ::temporal-bucketing/unit]]]]+ (common/disallowed-keys+ {:strategy ":binning keys like :strategy are not allowed at the top level of :field options."})+ ;; If `:base-type` is specified, the `:temporal-unit` must make sense, e.g. no bucketing by `:year`for a+ ;; `:type/Time` column.+ [:fn+ {:error/message "Invalid :temporal-unit for the specified :base-type."+ :decode/normalize (fn [m]+ (cond-> m+ (not (valid-temporal-unit-for-base-type? m)) (dissoc :temporal-unit)))}+ #'valid-temporal-unit-for-base-type?]])(mr/def ::field.literal.options[:merge@@ -110,11 +178,18 @@(or ((some-fn :effective-type :base-type) opts)::expression/type.unknown))+(defn normalize-expression-options+ "Normalize an expression options map."+ [m]+ (when (map? m)+ (normalize-field-options-map m)))+(mr/def ::expression.options[:merge+ {:decode/normalize normalize-expression-options}::common/options[:map- [:temporal-unit {:optional true} [:ref ::temporal-bucketing/unit]]]])+ [:temporal-unit {:optional true} [:ref ::temporal-bucketing/unit]]]])(mbql-clause/define-mbql-clause :expression[:tuplediff --git a/src/metabase/lib/schema/template_tag.cljc b/src/metabase/lib/schema/template_tag.cljcindex 18de5350fcf..75081968738 100644--- a/src/metabase/lib/schema/template_tag.cljc+++ b/src/metabase/lib/schema/template_tag.cljc@@ -8,16 +8,9 @@[metabase.util.malli.registry :as mr][metabase.util.performance :refer [every?]]))-;; Schema for valid values of `:widget-type` for a [[TemplateTag:FieldFilter]].(mr/def ::widget-type- (into- [:enum- ;; this will be a nicer error message than Malli trying to list every single possible allowed type.- {:decode/normalize common/normalize-keyword- :error/message "Valid template tag :widget-type"}- :none]- ;; TODO -- move this stuff into `metabase.lib`- (keys lib.schema.parameter/types)))+ "Schema for valid values of `:widget-type` for a `:metabase.lib.schema.template-tag/field-filter` template tag."+ [:ref ::lib.schema.parameter/widget-type])(mr/def ::type"Schema for valid values of template tag `:type`."diff --git a/src/metabase/lib/schema/util.cljc b/src/metabase/lib/schema/util.cljcindex b16f0914b44..df290a63d8c 100644--- a/src/metabase/lib/schema/util.cljc+++ b/src/metabase/lib/schema/util.cljc@@ -140,10 +140,28 @@order-bys))))stage))-(defn indexed-order-bys- "Convert all order-bys in a query to refer to aggregations by index instead of uuid. The result is- not a valid query, but avoiding random uuids is important during hashing."- [query]- (if (:stages query)- (update query :stages #(mapv indexed-order-bys-for-stage %))- query))+(defn pred-matches-form?+ "Check if `form` or any of its children forms match `pred`. This function is used for validation; during normal+ operation it will never match, so calling this function before `matching-locations` is more efficient."+ [form pred]+ (cond+ (pred form) true+ (map? form) (reduce-kv (fn [b _ v] (or b (pred-matches-form? v pred))) false form)+ (sequential? form) (reduce (fn [b x] (or b (pred-matches-form? x pred))) false form)+ :else false))++(defn matching-locations+ "Find the forms matching pred, returns a list of tuples of location (as used in get-in) and the match."+ [form pred]+ ;; Surprisingly enough, a list works better as a stack here than a vector.+ (loop [stack (list [[] form]), matches []]+ (if-let [[loc form :as top] (peek stack)]+ (let [stack (pop stack)+ map-onto-stack #(transduce (map (fn [[k v]] [(conj loc k) v])) conj stack %)+ seq-onto-stack #(transduce (map-indexed (fn [i v] [(conj loc i) v])) conj stack %)]+ (cond+ (pred form) (recur stack (conj matches top))+ (map? form) (recur (map-onto-stack form) matches)+ (sequential? form) (recur (seq-onto-stack form) matches)+ :else (recur stack matches)))+ matches)))diff --git a/src/metabase/lib/stage.cljc b/src/metabase/lib/stage.cljcindex fb8db3af74a..f9259f3fdc5 100644--- a/src/metabase/lib/stage.cljc+++ b/src/metabase/lib/stage.cljc@@ -20,6 +20,7 @@[metabase.lib.stage.util][metabase.lib.util :as lib.util][metabase.lib.util.match :as lib.util.match]+ [metabase.lib.util.unique-name-generator :as lib.util.unique-name-generator][metabase.util :as u][metabase.util.i18n :as i18n][metabase.util.malli :as mu]@@ -63,7 +64,7 @@;; do not truncate the desired column aliases coming back from a native query, because if a;; native query returns a 'crazy long' column name then we need to use that in the next stage.;; See [[metabase.lib.stage-test/propagate-crazy-long-native-identifiers-test]]- (lib.field.util/add-source-and-desired-aliases-xform query (lib.util/non-truncating-unique-name-generator)))+ (lib.field.util/add-source-and-desired-aliases-xform query (lib.util.unique-name-generator/non-truncating-unique-name-generator)))(:columns metadata))))))))(mu/defn- breakouts-columns :- [:maybe ::lib.metadata.calculation/visible-columns]diff --git a/src/metabase/lib/table.cljc b/src/metabase/lib/table.cljcindex 9d584c71796..59654f4ae29 100644--- a/src/metabase/lib/table.cljc+++ b/src/metabase/lib/table.cljc@@ -4,6 +4,7 @@[metabase.lib.metadata :as lib.metadata][metabase.lib.metadata.calculation :as lib.metadata.calculation][metabase.lib.util :as lib.util]+ [metabase.lib.util.unique-name-generator :as lib.util.unique-name-generator][metabase.util.humanization :as u.humanization][metabase.util.i18n :as i18n][metabase.util.malli :as mu]))@@ -36,5 +37,5 @@;; refer back to them using their original names anyway. Note that returned columns for a table ARE NOT;; equal to returned columns for a stage with this source table and nothing else... those SHOULD get;; truncated desired aliases when they stage returned columns are calculated.- (lib.field.util/add-source-and-desired-aliases-xform query (lib.util/non-truncating-unique-name-generator)))+ (lib.field.util/add-source-and-desired-aliases-xform query (lib.util.unique-name-generator/non-truncating-unique-name-generator)))(lib.metadata/active-fields query (:id table-metadata))))diff --git a/src/metabase/lib/temporal_bucket.cljc b/src/metabase/lib/temporal_bucket.cljcindex 4a285e17383..c15f645b5c9 100644--- a/src/metabase/lib/temporal_bucket.cljc+++ b/src/metabase/lib/temporal_bucket.cljc@@ -508,6 +508,7 @@This is expected to be called after `:unit` is added into column metadata, ie. in terms of annotate middleware, afterthe column metadata coming from a driver are merged with result of `column-info`."+ {:deprecated "0.57.0"}[column-metadata](if-some [temporal-unit (:unit column-metadata)](update column-metadata :display_name ensure-ends-with-temporal-unit temporal-unit)diff --git a/src/metabase/lib/util.cljc b/src/metabase/lib/util.cljcindex d20e411fbf3..323f8f4de3d 100644--- a/src/metabase/lib/util.cljc+++ b/src/metabase/lib/util.cljc@@ -1,22 +1,19 @@(ns metabase.lib.util- (:refer-clojure :exclude [format every? mapv select-keys update-keys some #?(:clj doseq) #?(:clj for)])+ (:refer-clojure :exclude [format every? mapv select-keys update-keys some #?(:clj for)])(:require#?@(:clj([potemkin :as p]):cljs- (["crc-32" :as CRC32]- [goog.string :as gstring]+ ([goog.string :as gstring][goog.string.format :as gstring.format]))[clojure.set :as set][clojure.string :as str][medley.core :as m]- [metabase.legacy-mbql.util :as mbql.u][metabase.lib.common :as lib.common][metabase.lib.dispatch :as lib.dispatch][metabase.lib.hierarchy :as lib.hierarchy][metabase.lib.options :as lib.options][metabase.lib.schema :as lib.schema]- [metabase.lib.schema.common :as lib.schema.common][metabase.lib.schema.expression :as lib.schema.expression][metabase.lib.schema.id :as lib.schema.id][metabase.lib.schema.ref :as lib.schema.ref]@@ -26,7 +23,7 @@[metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]- [metabase.util.performance :refer [every? mapv select-keys update-keys some #?(:clj doseq) #?(:clj for)]]))+ [metabase.util.performance :refer [every? mapv select-keys update-keys some #?(:clj for)]]))#?(:clj(set! *warn-on-reflection* true))@@ -73,18 +70,6 @@(and (clause? clause)(lib.hierarchy/isa? (first clause) ::lib.schema.ref/ref)))-(defn segment-clause?- "Returns true if this is a segment clause"- [clause]- (and (clause? clause)- (lib.hierarchy/isa? (first clause) ::lib.schema.ref/segment)))--(defn metric-clause?- "Returns true if this is a metric clause"- [clause]- (and (clause? clause)- (lib.hierarchy/isa? (first clause) ::lib.schema.ref/metric)))-;;; TODO (Cam 8/28/25) -- base type is the original effective type!!! We shouldn't need a separate;;; `:metabase.lib.field/original-effective-type` key.(defn original-isa?@@ -432,50 +417,6 @@conjunction(last coll)))))))-(def ^:private truncate-alias-max-length-bytes- "Length to truncate column and table identifiers to. See [[metabase.driver.impl/default-alias-max-length-bytes]] for- reasoning."- 60)--(def ^:private truncated-alias-hash-suffix-length- "Length of the hash suffixed to truncated strings by [[truncate-alias]]."- ;; 8 bytes for the CRC32 plus one for the underscore- 9)--(mu/defn- crc32-checksum :- [:string {:min 8, :max 8}]- "Return a 4-byte CRC-32 checksum of string `s`, encoded as an 8-character hex string."- [s :- :string]- (let [s #?(:clj (Long/toHexString (.getValue (doto (java.util.zip.CRC32.)- (.update (.getBytes ^String s "UTF-8")))))- :cljs (-> (CRC32/str s 0)- (unsigned-bit-shift-right 0) ; see https://github.com/SheetJS/js-crc32#signed-integers- (.toString 16)))]- ;; pad to 8 characters if needed. Might come out as less than 8 if the first byte is `00` or `0x` or something.- (loop [s s]- (if (< (count s) 8)- (recur (str \0 s))- s))))--(mu/defn truncate-alias :- [:string {:min 1, :max 60}]- "Truncate string `s` if it is longer than [[truncate-alias-max-length-bytes]] and append a hex-encoded CRC-32- checksum of the original string. Truncated string is truncated to [[truncate-alias-max-length-bytes]]- minus [[truncated-alias-hash-suffix-length]] characters so the resulting string is- exactly [[truncate-alias-max-length-bytes]]. The goal here is that two really long strings that only differ at the- end will still have different resulting values.-- (truncate-alias \"some_really_long_string\" 15) ; -> \"some_r_8e0f9bc2\"- (truncate-alias \"some_really_long_string_2\" 15) ; -> \"some_r_2a3c73eb\""- ([s]- (truncate-alias s truncate-alias-max-length-bytes))-- ([s :- ::lib.schema.common/non-blank-string- max-bytes :- [:int {:min 0}]]- (if (<= (u/string-byte-count s) max-bytes)- s- (let [checksum (crc32-checksum s)- truncated (u/truncate-string-to-byte-count s (- max-bytes truncated-alias-hash-suffix-length))]- (str truncated \_ checksum)))))-(mu/defn legacy-string-table-id->card-id :- [:maybe ::lib.schema.id/card]"If `table-id` is a legacy `card__<id>`-style string, parse the `<id>` part to an integer Card ID. Only for legacyqueries! You don't need to use this in MBQL 5 since this is converted automatically by [[metabase.lib.convert]] to@@ -495,114 +436,6 @@[query](-> query :stages first :source-card))-(mu/defn first-stage-type :- [:maybe [:enum :mbql.stage/mbql :mbql.stage/native]]- "Type of the first query stage."- [query :- :map]- (:lib/type (query-stage query 0)))--(mu/defn- unique-alias :- :string- [original :- :string- suffix :- :string]- (-> (str original \_ suffix)- (truncate-alias)))--(mr/def ::unique-name-generator- "Stateful function with the signature-- (f) => 'fresh' unique name generator- (f str) => unique-str- (f id str) => unique-str-- i.e. repeated calls with the same string should return different unique strings."- [:function- ;; (f) => generates a new instance of the unique name generator for recursive generation without 'poisoning the- ;; well'.- [:=>- [:cat]- [:ref ::unique-name-generator]]- ;; (f str) => unique-str- [:=>- [:cat :string]- ::lib.schema.common/non-blank-string]- ;; (f id str) => unique-str- [:=>- [:cat :any :string]- ::lib.schema.common/non-blank-string]])--(mu/defn- unique-name-generator-with-options :- ::unique-name-generator- [options :- :map]- ;; ok to use here because this is the one designated wrapper for it.- #_{:clj-kondo/ignore [:discouraged-var]}- (let [f (mbql.u/unique-name-generator options)- truncate* (if (::truncate? options)- truncate-alias- identity)]- ;; I know we could just use `comp` here but it gets really hard to figure out where it's coming from when you're- ;; debugging things; a named function like this makes it clear where this function came from- (fn unique-name-generator-fn- ([]- (unique-name-generator-with-options options))- ([s]- (->> s truncate* f))- ([id s]- (->> s truncate* (f id))))))--(mu/defn- unique-name-generator-factory :- [:function- [:=>- [:cat]- ::unique-name-generator]- [:=>- [:cat [:schema [:sequential :string]]]- ::unique-name-generator]]- [options :- :map]- (mu/fn :- ::unique-name-generator- ([]- (unique-name-generator-with-options options))- ([existing-names :- [:sequential :string]]- (let [f (unique-name-generator-with-options options)]- (doseq [existing existing-names]- (f existing))- f))))--(def ^{:arglists '([] [existing-names])} unique-name-generator- "Create a new function with the signature-- (f str) => str-- or-- (f id str) => str-- That takes any sort of string identifier (e.g. a column alias or table/join alias) and returns a guaranteed-unique- name truncated to 60 characters (actually 51 characters plus a hash).-- Optionally takes a list of names which are already defined, \"priming\" the generator with eg. all the column names- that currently exist on a stage of the query.-- The two-arity version of the returned function can be used for idempotence. See docstring- for [[metabase.legacy-mbql.util/unique-name-generator]] for more information.-- New!-- You can call-- (f)-- to get a new, fresh unique name generator for recursive usage without 'poisoning the well'."- ;; unique by lower-case name, e.g. `NAME` and `name` => `NAME` and `name_2`- ;;- ;; some databases treat aliases as case-insensitive so make sure the generated aliases are unique regardless of- ;; case- (unique-name-generator-factory- {::truncate? true- :name-key-fn u/lower-case-en- :unique-alias-fn unique-alias}))--(def ^{:arglists '([] [existing-names])} non-truncating-unique-name-generator- "This is the same as [[unique-name-generator]] but doesn't truncate names, matching the 'classic' behavior in QP- results metadata."- (unique-name-generator-factory {::truncate? false}))-(def ^:private strip-id-regex#?(:cljs (js/RegExp. " id$" "i");; `(?i)` is JVM-specific magic to turn on the `i` case-insensitive flag.@@ -678,36 +511,6 @@:elsex)))-(defn- replace-uuid-references- [x replacement-map]- (let [replacement (find replacement-map x)]- (cond- replacement- (val replacement)-- (sequential? x)- (into (empty x) (map #(replace-uuid-references % replacement-map)) x)-- (map? x)- (into- (empty x)- (map (fn [[k v]]- [k (cond-> v- (not= k :lib/uuid) (replace-uuid-references replacement-map))]))- x)-- :else- x)))--(defn fresh-query-instance- "Create an copy of `query` with fresh :lib/uuid values making sure that internal- uuid references are kept."- [query]- (let [v-replacement (volatile! (transient {}))- almost-query (fresh-uuids query #(vswap! v-replacement assoc! %1 %2))- replacement (persistent! @v-replacement)]- (replace-uuid-references almost-query replacement)))-(mu/defn normalized-query-type :- [:maybe [:enum #_MLv2 :mbql/query #_legacy :query :native #_audit :internal]]"Get the `:lib/type` or `:type` from `query`, even if it is not-yet normalized."[query :- [:maybe :map]]diff --git a/src/metabase/lib/util/unique_name_generator.cljc b/src/metabase/lib/util/unique_name_generator.cljcnew file mode 100644index 00000000000..fbb244b8db3--- /dev/null+++ b/src/metabase/lib/util/unique_name_generator.cljc@@ -0,0 +1,263 @@+(ns metabase.lib.util.unique-name-generator+ (:require+ #?@(:cljs+ (["crc-32" :as CRC32]))+ [metabase.lib.schema.common :as lib.schema.common]+ [metabase.util :as u]+ [metabase.util.malli :as mu]+ [metabase.util.malli.registry :as mr]))++(def ^:private truncate-alias-max-length-bytes+ "Length to truncate column and table identifiers to. See [[metabase.driver.impl/default-alias-max-length-bytes]] for+ reasoning."+ 60)++(def ^:private truncated-alias-hash-suffix-length+ "Length of the hash suffixed to truncated strings by [[truncate-alias]]."+ ;; 8 bytes for the CRC32 plus one for the underscore+ 9)++(mu/defn- crc32-checksum :- [:string {:min 8, :max 8}]+ "Return a 4-byte CRC-32 checksum of string `s`, encoded as an 8-character hex string."+ [s :- :string]+ (let [s #?(:clj (Long/toHexString (.getValue (doto (java.util.zip.CRC32.)+ (.update (.getBytes ^String s "UTF-8")))))+ :cljs (-> (CRC32/str s 0)+ (unsigned-bit-shift-right 0) ; see https://github.com/SheetJS/js-crc32#signed-integers+ (.toString 16)))]+ ;; pad to 8 characters if needed. Might come out as less than 8 if the first byte is `00` or `0x` or something.+ (loop [s s]+ (if (< (count s) 8)+ (recur (str \0 s))+ s))))++(mu/defn truncate-alias :- [:string {:min 1, :max 60}]+ "Truncate string `s` if it is longer than [[truncate-alias-max-length-bytes]] and append a hex-encoded CRC-32+ checksum of the original string. Truncated string is truncated to [[truncate-alias-max-length-bytes]]+ minus [[truncated-alias-hash-suffix-length]] characters so the resulting string is+ exactly [[truncate-alias-max-length-bytes]]. The goal here is that two really long strings that only differ at the+ end will still have different resulting values.++ (truncate-alias \"some_really_long_string\" 15) ; -> \"some_r_8e0f9bc2\"+ (truncate-alias \"some_really_long_string_2\" 15) ; -> \"some_r_2a3c73eb\""+ ([s]+ (truncate-alias s truncate-alias-max-length-bytes))++ ([s :- ::lib.schema.common/non-blank-string+ max-bytes :- [:int {:min 0}]]+ (if (<= (u/string-byte-count s) max-bytes)+ s+ (let [checksum (crc32-checksum s)+ truncated (u/truncate-string-to-byte-count s (- max-bytes truncated-alias-hash-suffix-length))]+ (str truncated \_ checksum)))))++(mr/def ::unique-name-generator+ "Stateful function with the signature++ (f) => 'fresh' unique name generator+ (f str) => unique-str+ (f id str) => unique-str++ i.e. repeated calls with the same string should return different unique strings."+ [:function+ ;; (f) => generates a new instance of the unique name generator for recursive generation without 'poisoning the+ ;; well'.+ [:=>+ [:cat]+ [:ref ::unique-name-generator]]+ ;; (f str) => unique-str+ [:=>+ [:cat :string]+ ::lib.schema.common/non-blank-string]+ ;; (f id str) => unique-str+ [:=>+ [:cat :any :string]+ ::lib.schema.common/non-blank-string]])++(mu/defn- untruncated-unique-alias :- :string+ [original :- :string+ suffix :- :string]+ (str original \_ suffix))++(mu/defn- truncated-unique-alias :- :string+ [original :- :string+ suffix :- :string]+ (-> (untruncated-unique-alias original suffix)+ truncate-alias))++(mr/def ::unique-name-generator.options+ [:map+ {:closed true}+ [:truncate-fn {:optional true, :default identity} [:=> [:cat :string] :string]]+ [:name-key-fn {:optional true, :default identity} [:=> [:cat :string] :string]]+ [:unique-alias-fn {:optional true, :default untruncated-unique-alias} [:=> [:cat :string :string] :string]]])++(defn- -unique-name-generator+ [& {:keys [name-key-fn unique-alias-fn]+ :or {name-key-fn identity+ unique-alias-fn untruncated-unique-alias}}]+ (let [id+original->unique (atom {}) ; map of [id original-alias] -> unique-alias+ original->count (atom {})] ; map of original-alias -> count+ (fn generate-name+ ([an-alias]+ (generate-name (gensym) an-alias))++ ([id original]+ (let [name-key (name-key-fn original)]+ (or+ ;; if we already have generated an alias for this key (e.g. `[id original]`), return it as-is.+ (@id+original->unique [id name-key])+ ;; otherwise generate a new unique alias.+ ;; see if we're the first to try to use this candidate alias. Update the usage count in `original->count`+ (let [total-count (get (swap! original->count update name-key (fnil inc 0)) name-key)]+ (if (= total-count 1)+ ;; if we are the first to do it, record it in `id+original->unique` and return it.+ (do+ (swap! id+original->unique assoc [id name-key] original)+ original)+ ;; otherwise prefix the alias by the current total count (e.g. `id` becomes `id_2`) and recur. If `id_2`+ ;; is unused, it will get returned. Otherwise we'll recursively try `id_2_2`, and so forth.+ (let [candidate (unique-alias-fn original (str total-count))]+ ;; double-check that `unique-alias-fn` isn't doing something silly like truncating the generated alias+ ;; to aggressively or forgetting to include the `suffix` -- otherwise we could end up with an infinite+ ;; loop+ (assert (not= candidate original)+ (str "unique-alias-fn must return a different string than its input. Input: "+ (pr-str candidate)))+ (swap! id+original->unique assoc [id name-key] candidate)+ (recur id candidate))))))))))++(mu/defn unique-name-generator-with-options :- ::unique-name-generator+ "Return a function that can be used to uniquify string names. Function maintains an internal counter that will suffix+ any names passed to it as needed so all results will be unique.++ (let [unique-name (unique-name-generator)]+ [(unique-name \"A\")+ (unique-name \"B\")+ (unique-name \"A\")])+ ;; -> [\"A\" \"B\" \"A_2\"]++ By default, unique aliases are generated for each unique `[id original-name]` key pair. By default, a unique `id` is+ generated for every call, meaning repeated calls to [[unique-name-generator]] with the same `original-name` will+ return different unique aliases. If idempotence is desired, the function returned by the generator also has a 2+ airity version with the signature++ (unique-name-fn id original-name)++ for example:++ (let [unique-name (unique-name-generator)]+ [(unique-name :x \"A\")+ (unique-name :x \"B\")+ (unique-name :x \"A\")+ (unique-name :y \"A\")])+ ;; -> [\"A\" \"B\" \"A\" \"A_2\"]++ Finally, [[unique-name-generator]] accepts the following options to further customize behavior:++ ### `:name-key-fn`++ Generated aliases are unique by the value of `[id (name-key-fn original-name)]`; the default is `identity`, so by+ default aliases are unique by `[id name-key-fn]`. Specify something custom here if you want to make the unique+ aliases unique by some other value, for example to make them unique without regards to case:++ (let [f (unique-name-generator :name-key-fn str/lower-case)]+ [(f \"x\")+ (f \"X\")+ (f \"X\")])+ ;; -> [\"x\" \"X_2\" \"X_3\"]++ This is useful for databases that treat column aliases as case-insensitive (see #19618 for some examples of this).++ ### `:unique-alias-fn`++ The function used to generate a potentially-unique alias given an original alias and unique suffix with the signature++ (unique-alias-fn original suffix)++ By default, combines them like `original_suffix`, but you can supply a custom function if you need to change this+ behavior:++ (let [f (unique-name-generator :unique-alias-fn (fn [x y] (format \"%s~~%s\" y x)))]+ [(f \"x\")+ (f \"x\")])+ ;; -> [\"x\" \"2~~x\"]++ This is useful if you need to constrain the generated suffix in some way, for example by limiting its length or+ escaping characters disallowed in a column alias.++ Values generated by this function are recursively checked for uniqueness, and will keep trying values a unique value+ is generated; for this reason the function *must* return a unique value for every unique input. Use caution when+ limiting the length of the identifier generated (consider appending a hash in cases like these).++ ### `:truncate-fn`++ Names are truncated with this function before passed to `unique-alias-fn`."+ [& {:keys [truncate-fn], :or {truncate-fn identity}, :as options} :- [:maybe ::unique-name-generator.options]]+ ;; ok to use here because this is the one designated wrapper for it.+ (let [f (-unique-name-generator options)]+ ;; I know we could just use `comp` here but it gets really hard to figure out where it's coming from when you're+ ;; debugging things; a named function like this makes it clear where this function came from+ (fn unique-name-generator-fn+ ([]+ (unique-name-generator-with-options options))+ ([s]+ (->> s truncate-fn f))+ ([id s]+ (->> s truncate-fn (f id))))))++(mu/defn- unique-name-generator-factory :- [:function+ [:=>+ [:cat]+ ::unique-name-generator]+ [:=>+ [:cat [:schema [:sequential :string]]]+ ::unique-name-generator]]+ [options :- ::unique-name-generator.options]+ (mu/fn :- ::unique-name-generator+ ([]+ (unique-name-generator-with-options options))+ ([existing-names :- [:sequential :string]]+ (let [f (unique-name-generator-with-options options)]+ (doseq [existing existing-names]+ (f existing))+ f))))++(def ^{:arglists '([] [existing-names])} unique-name-generator+ "Create a new function with the signature++ (f str) => str++ or++ (f id str) => str++ That takes any sort of string identifier (e.g. a column alias or table/join alias) and returns a guaranteed-unique+ name truncated to 60 characters (actually 51 characters plus a hash).++ Optionally takes a list of names which are already defined, \"priming\" the generator with eg. all the column names+ that currently exist on a stage of the query.++ The two-arity version of the returned function can be used for idempotence. See docstring+ for [[metabase.legacy-mbql.util/unique-name-generator]] for more information.++ New!++ You can call++ (f)++ to get a new, fresh unique name generator for recursive usage without 'poisoning the well'."+ ;; unique by lower-case name, e.g. `NAME` and `name` => `NAME` and `name_2`+ ;;+ ;; some databases treat aliases as case-insensitive so make sure the generated aliases are unique regardless of+ ;; case+ (unique-name-generator-factory+ {:truncate-fn truncate-alias+ :name-key-fn u/lower-case-en+ :unique-alias-fn truncated-unique-alias}))++(def ^{:arglists '([] [existing-names])} non-truncating-unique-name-generator+ "This is the same as [[unique-name-generator]] but doesn't truncate names, matching the 'classic' behavior in QP+ results metadata."+ (unique-name-generator-factory {}))diff --git a/src/metabase/models/interface.clj b/src/metabase/models/interface.cljindex 63055c20989..c0e32e5358a 100644--- a/src/metabase/models/interface.clj+++ b/src/metabase/models/interface.clj@@ -14,8 +14,9 @@[clojure.string :as str][clojure.walk :as walk][medley.core :as m]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.binning :as lib.binning]+ [metabase.lib.core :as lib][metabase.lib.normalize :as lib.normalize][metabase.lib.schema.metadata :as lib.schema.metadata][metabase.lib.temporal-bucket :as lib.temporal-bucket]@@ -213,13 +214,16 @@(def ^{:deprecated "0.57.0"} transform-legacy-field-ref"Transform field refs"{:in json-in- :out (comp (catch-normalization-exceptions mbql.normalize/normalize-field-ref) json-out-with-keywordization)})+ :out (comp (catch-normalization-exceptions #_{:clj-kondo/ignore [:deprecated-var]} mbql.normalize/normalize-field-ref)+ json-out-with-keywordization)})(defn- normalize-result-metadata-column [col](if (:lib/type col)(lib.normalize/normalize ::lib.schema.metadata/column col)+ ;; legacy usages -- do not use these going forward+ #_{:clj-kondo/ignore [:deprecated-var]}(-> col- mbql.normalize/normalize-source-metadata+ (->> (lib/normalize :metabase.query-processor.schema/result-metadata.column));; This is necessary, because in the wild, there may be cards created prior to this change.lib.temporal-bucket/ensure-temporal-unit-in-display-namelib.binning/ensure-binning-in-display-name)))diff --git a/src/metabase/models/serialization.clj b/src/metabase/models/serialization.cljindex 23221a2608b..94d038b2454 100644--- a/src/metabase/models/serialization.clj+++ b/src/metabase/models/serialization.clj@@ -62,7 +62,8 @@[malli.core :as mc][malli.transform :as mtx][medley.core :as m]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ;; legacy usages -- do not use in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.core :as lib][metabase.lib.schema.id :as lib.schema.id][metabase.lib.util.match :as lib.util.match]@@ -1113,6 +1114,8 @@(defn- mbql-id->fully-qualified-name[mbql](-> mbql+ ;; legacy usages -- do not use in new code+ #_{:clj-kondo/ignore [:deprecated-var]}mbql.normalize/normalize-tokens(lib.util.match/replace;; `integer?` guard is here to make the operation idempotentdiff --git a/src/metabase/models/visualization_settings.cljc b/src/metabase/models/visualization_settings.cljcindex f0505001f51..3cdd30f47e4 100644--- a/src/metabase/models/visualization_settings.cljc+++ b/src/metabase/models/visualization_settings.cljc@@ -24,7 +24,8 @@[clojure.set :as set][malli.core :as mc][medley.core :as m]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ;; legacy usage -- do not use in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.schema.id :as lib.schema.id][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]))@@ -379,6 +380,8 @@(if (dimension-param-mapping? v)(let [parsed-id (-> (if (keyword? k) (keyname k) k)parse-json-string+ ;; legacy usage -- do not use in new code+ #_{:clj-kondo/ignore [:deprecated-var]}mbql.normalize/normalize-tokens)][parsed-id (cond-> v(:source v) (assoc ::param-mapping-sourcediff --git a/src/metabase/parameters/custom_values.clj b/src/metabase/parameters/custom_values.cljindex 6f503d0dc23..157ede25675 100644--- a/src/metabase/parameters/custom_values.clj+++ b/src/metabase/parameters/custom_values.clj@@ -8,7 +8,6 @@(:require[clojure.string :as str][medley.core :as m]- [metabase.legacy-mbql.schema :as mbql.s][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata][metabase.lib.schema :as lib.schema]@@ -22,6 +21,7 @@[metabase.util.i18n :refer [tru]][metabase.util.log :as log][metabase.util.malli :as mu]+ [metabase.util.malli.registry :as mr][metabase.util.malli.schema :as ms][toucan2.core :as t2]))@@ -62,23 +62,29 @@Maybe we should lower it for the sake of displaying a parameter dropdown."1000)+(mr/def ::values-from-card-query.options+ [:map+ ;; despite this being called "query string" it can actually be any value because it just gets used in an `:=`+ ;; filter clause. :eyeroll:+ [:query-string {:optional true} :any]])+(mu/defn- values-from-card-query :- [:maybe ::lib.schema/query][{query :dataset_query, :keys [id], :as _card} :- [:and:metabase.queries.schema/card[:map[:id ::lib.schema.id/card]]]- legacy-field-ref :- ::mbql.s/field-or-expression-ref- {:keys [query-string] :as _opts}]+ field-ref :- [:or :mbql.clause/field :mbql.clause/expression]+ {:keys [query-string] :as _opts} :- [:maybe ::values-from-card-query.options]](when (seq query);; start a new query using this Card as a starting point(let [query (lib/query query (lib.metadata/card query id))](when-let [visible-columns (or (not-empty (lib/visible-columns query))(log/warnf "Cannot get values from Card %d: Card query has no visible columns"id))]- (when-let [value-column (or (lib/find-column-for-legacy-ref query legacy-field-ref visible-columns)- (log/warnf "Cannot get values from Card %d: failed to find column for legacy ref %s\nFound: %s"+ (when-let [value-column (or (lib/find-matching-column query -1 field-ref visible-columns)+ (log/warnf "Cannot get values from Card %d: failed to find column for ref %s\nFound: %s"id- (pr-str legacy-field-ref)+ (pr-str field-ref)(pr-str (map (some-fn :lib/source-column-alias :name) visible-columns))))](let [textual? (lib.types.isa/string? value-column)nonempty ((if textual? lib/not-empty lib/not-null) value-column)@@ -109,13 +115,13 @@{:values [[\"Red Medicine\"]]:has_more_values false}"- ([card value-field]- (values-from-card card value-field nil))+ ([card field-ref]+ (values-from-card card field-ref nil))- ([card :- :metabase.queries.schema/card- legacy-field-ref :- ::mbql.s/field-or-expression-ref- opts :- [:maybe :map]]- (let [mbql-query (values-from-card-query card legacy-field-ref opts)+ ([card :- :metabase.queries.schema/card+ field-ref :- [:or :mbql.clause/field :mbql.clause/expression]+ opts :- [:maybe ::values-from-card-query.options]]+ (let [mbql-query (values-from-card-query card field-ref opts)result (some-> mbql-query qp/process-query)values (get-in result [:data :rows])]{:values values@@ -129,7 +135,7 @@query-string :- [:maybe ms/NonBlankString]](let [card-id (:card_id config)card (t2/select-one :model/Card :id card-id)]- (values-from-card card (:value_field config) {:query-string query-string})))+ (values-from-card card (lib/->pMBQL (:value_field config)) {:query-string query-string})))(defn- can-get-card-values?[card value-field]diff --git a/src/metabase/parameters/dashboard.clj b/src/metabase/parameters/dashboard.cljindex db825a58c04..d64c0e0d381 100644--- a/src/metabase/parameters/dashboard.clj+++ b/src/metabase/parameters/dashboard.clj@@ -2,8 +2,7 @@(:require[medley.core :as m][metabase.api.common :as api]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.util :as mbql.u]+ [metabase.lib.core :as lib][metabase.lib.schema.parameter :as lib.schema.parameter][metabase.parameters.chain-filter :as chain-filter][metabase.parameters.custom-values :as custom-values]@@ -56,10 +55,11 @@(let [dashboard (t2/hydrate dashboard :resolved-params)param (get-in dashboard [:resolved-params param-key])results (for [{:keys [target] {:keys [card]} :dashcard} (:mappings param)- :let [[_dimension field-ref opts] (->> (mbql.normalize/normalize-tokens target :ignore-path)- (mbql.u/check-clause :dimension))]+ :let [field-ref ((some-fn lib/parameter-target-field-ref+ lib/parameter-target-expression-ref)+ target)]:when field-ref]- (custom-values/values-from-card card field-ref opts))]+ (custom-values/values-from-card card field-ref))](when-some [values (seq (distinct (mapcat :values results)))](let [has_more_values (boolean (some true? (map :has_more_values results)))]{:values (cond->> valuesdiff --git a/src/metabase/parameters/params.clj b/src/metabase/parameters/params.cljindex abd0e157257..d9ed63f74f5 100644--- a/src/metabase/parameters/params.clj+++ b/src/metabase/parameters/params.clj@@ -14,18 +14,13 @@[malli.error :as me][medley.core :as m][metabase.app-db.core :as app-db]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.schema :as mbql.s]- [metabase.legacy-mbql.util :as mbql.u][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata][metabase.lib.schema.id :as lib.schema.id][metabase.lib.schema.parameter :as lib.schema.parameter][metabase.lib.schema.template-tag :as lib.schema.template-tag]- [metabase.lib.util.match :as lib.util.match][metabase.parameters.schema :as parameters.schema]- [metabase.util :as u][metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]@@ -62,52 +57,37 @@checks (since there is no current User) and get *all* values."false)-(mu/defn- template-tag->field-ref :- [:maybe :mbql.clause/field]+(mu/defn- template-tag-name->field-id :- [:maybe ::lib.schema.id/field]"Fetch the `:field` clause from `dashcard` referenced by `template-tag`.(template-tag->field-form [:template-tag :company] some-dashcard) ; -> [:field 100 nil]"- [[_template-tag tag] :- ::lib.schema.parameter/template-tag- card :- :metabase.queries.schema/card]- (some-> card- :dataset_query- not-empty- lib-be/normalize-query- lib/all-template-tags-map- (get (u/qualified-name tag))- :dimension))+ [template-tag-name :- :string+ card :- :metabase.queries.schema/card]+ (or (some-> card+ :dataset_query+ not-empty+ lib-be/normalize-query+ lib/all-template-tags-map+ (get template-tag-name)+ :dimension+ lib/field-ref-id)+ (do+ (log/warnf "Could not find matching Field ID for target: %s" (pr-str template-tag-name))+ nil)))(mu/defn param-target->field-id :- [:maybe ::lib.schema.id/field]"Parse a Card parameter `target` form, which looks something like `[:dimension [:field-id 100]]`, and return the FieldID it references (if any)."- [target :- ::lib.schema.parameter/target+ [target;; TODO (Cam 9/25/25) -- `card` should actually be required but I don't have all day to fix broken tests from;; before I schematized this.card :- [:maybe :metabase.queries.schema/card]]- (let [target (mbql.normalize/normalize target)]- (when (mbql.u/is-clause? :dimension target)- ;; MBQL 5 `:parameters` still use legacy field refs (for now), while `:template-tags` use MBQL 5 field refs. So- ;; we have to handle either one depending on where the `:field` ref actually lives.- (let [[_ dimension] target- field-ref (cond- (mbql.u/is-clause? :template-tag dimension)- (when card- (template-tag->field-ref dimension card))-- (mbql.u/is-clause? :field dimension)- dimension)]- (cond- (not field-ref)- (log/errorf "Could not find matching field clause for target: %s" (pr-str target))-- ;; Being extra safe here since we've got many reports on this cause loading dashboard to fail- ;; for unknown reasons. See #8917- (not (mr/validate [:or :mbql.clause/field ::mbql.s/field] field-ref))- (log/errorf "Template tag :dimension is not a valid MBQL 5 or legacy field ref, got %s" (pr-str field-ref))-- :else- (lib.util.match/match-one field-ref- [:field _opts (id :guard pos-int?)] id- [:field (id :guard pos-int?) _opts] id))))))+ (let [target (lib/normalize ::lib.schema.parameter/target target)]+ (or+ (when card+ (when-let [template-tag-name (lib/parameter-target-template-tag-name target)]+ (template-tag-name->field-id template-tag-name card)))+ (lib/parameter-target-field-id target))))(defn- pk-fields"Return the `fields` that are PK Fields."@@ -228,15 +208,13 @@card-id (or (get-in param-dashcard-info [:param-mapping :card_id])(get-in param-dashcard-info [:dashcard :card :id]))filterable-columns (get-in ctx [:card-id->filterable-columns card-id stage-number])- [_ dimension] (->> (mbql.normalize/normalize-tokens param-target :ignore-path)- (mbql.u/check-clause :dimension))]+ param-target (lib/normalize ::lib.schema.parameter/target param-target)](if-some [field-id (when (seq filterable-columns)- (lib.util.match/match-one dimension+ (when (lib/parameter-target-field-name param-target);; TODO it's basically a workaround for ignoring non-dimension parameter targets such as SQL variables;; TODO code is misleading; let's check for :dimension and drop the match call here- [:field (field-name :guard string?) _](->> filterable-columns- (lib/find-matching-column (lib/->pMBQL &match))+ (lib/find-matching-column (lib/parameter-target-field-ref param-target)):id)))](-> ctx(update :param-id->field-ids #(merge {param-id #{}} %))diff --git a/src/metabase/parameters/schema.cljc b/src/metabase/parameters/schema.cljcindex 12115327435..63a85d0535d 100644--- a/src/metabase/parameters/schema.cljc+++ b/src/metabase/parameters/schema.cljc@@ -69,8 +69,9 @@[:enum {:decode/normalize lib.schema.common/normalize-keyword} :none :list :search])(mr/def ::parameter- "Schema for a valid Parameter. We're not using [[metabase.legacy-mbql.schema/Parameter]] here because this Parameter- is meant to be used for Parameters we store on dashboard/card, and it has some difference with Parameter in MBQL."+ "Schema for a valid stored parameter declaration saved as part of a Dashboard or Card. We're not using+ `::metabase.lib.schema.parameter/parameter` here because these differ a bit from the parameters attached to+ queries.";; TODO we could use :multi to dispatch values_source_type to the correct values_source_config[:map{:description "parameter must be a map with :id and :type keys"@@ -83,7 +84,7 @@[:sequential [:ref ::parameter-mapping]][:set [:ref ::parameter-mapping]]]]][:name {:optional true} :string]- ;; ok now I know you're trying to mess with me+ ;; ok now I know you're trying to mess with me with this camelCase key[:sectionId {:optional true} ::lib.schema.common/non-blank-string][:slug {:optional true} :string][:target {:optional true} [:ref ::lib.schema.parameter/target]]@@ -92,6 +93,7 @@[:values_query_type {:optional true} [:maybe ::values-query-type]][:values_source_config {:optional true} [:maybe ::values-source-config]][:values_source_type {:optional true} [:maybe ::values-source-type]]])+;;; TODO (Cam 10/20/25) -- does this need to include `widget-type` as well? Or is that only set for template tags?(mu/defn normalize-parameter :- ::parameter"Normalize `parameter` when coming out of the application database or in via an API request."@@ -106,6 +108,22 @@[parameters](lib/normalize ::parameters parameters))+(mr/def ::parameter-with-optional-type+ [:merge+ ::parameter+ [:map+ [:type {:optional true} [:ref ::lib.schema.parameter/type]]]])++(mr/def ::parameters-with-optional-types+ [:sequential ::parameter-with-optional-type])++(mu/defn normalize-parameters-without-adding-default-types :- ::parameters-with-optional-types+ "The same as [[normalize-parameters]], but does not add a default `:type` if it is missing. Needed in some cases+ where we infer the type based on the `:widget-type` in the saved parameter declarations inside a Card or Dashboard,+ e.g. when running an embedded Card with the Card QP."+ [parameters]+ (lib/normalize ::parameters-with-optional-types parameters))+#?(:clj(def transform-parameters"Toucan 2 transform for columns that are sequences of Card/Dashboard parameters."diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.cljindex de5e8a91ee8..2bd0967c25d 100644--- a/src/metabase/queries/models/card.clj+++ b/src/metabase/queries/models/card.clj@@ -669,31 +669,28 @@(update card :result_metadata (fn [cols](mapv #(dissoc % :ident :model/inner_ident) cols))))-(mu/defn- upgrade-card-schema-to-latest :- [:map- [:result_metadata {:optional true} [:maybe- [:sequential- ::lib.schema.metadata/lib-or-legacy-column]]]]- [card]- (if (and (:id card)- (or (:dataset_query card)- (:result_metadata card)- (:database_id card)- (:type card)))- ;; A plausible select to run the after-select logic on.- (if-not (:card_schema card)- ;; Plausible but no :card_schema - error.- (throw (ex-info "Cannot SELECT a Card without including :card_schema"- {:card-id (:id card)}))- ;; Plausible and has the schema, so run the upgrades over it.- (loop [card card]- (if (= (:card_schema card) current-schema-version)- card- (let [new-version (inc (:card_schema card))]- (recur (assoc (upgrade-card-schema-to card new-version)- :card_schema new-version))))))-- ;; Some sort of odd query like an aggregation over cards. Just return it as-is.- card))+(mu/defn- upgrade-card-schema-to-latest :- ::queries.schema/card+ [card :- :map]+ (-> (if (and (:id card)+ (or (:dataset_query card)+ (:result_metadata card)+ (:database_id card)+ (:type card)))+ ;; A plausible select to run the after-select logic on.+ (if-not (:card_schema card)+ ;; Plausible but no :card_schema - error.+ (throw (ex-info "Cannot SELECT a Card without including :card_schema"+ {:card-id (:id card)}))+ ;; Plausible and has the schema, so run the upgrades over it.+ (loop [card card]+ (if (= (:card_schema card) current-schema-version)+ card+ (let [new-version (inc (:card_schema card))]+ (recur (assoc (upgrade-card-schema-to card new-version)+ :card_schema new-version))))))+ ;; Some sort of odd query like an aggregation over cards. Just return it as-is.+ card)+ queries.schema/normalize-card))(t2/define-after-select :model/Card[card]diff --git a/src/metabase/queries/models/card/metadata.clj b/src/metabase/queries/models/card/metadata.cljindex 5ee01377b7c..284b684df54 100644--- a/src/metabase/queries/models/card/metadata.clj+++ b/src/metabase/queries/models/card/metadata.clj@@ -4,13 +4,14 @@[medley.core :as m][metabase.analyze.core :as analyze][metabase.api.common :as api]- [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib-be.core :as lib-be]+ [metabase.lib.core :as lib][metabase.lib.normalize :as lib.normalize][metabase.lib.schema.id :as lib.schema.id][metabase.lib.schema.metadata :as lib.schema.metadata][metabase.query-processor.metadata :as qp.metadata][metabase.query-processor.preprocess :as qp.preprocess]+ [metabase.query-processor.schema :as qp.schema][metabase.query-processor.util :as qp.util][metabase.request.core :as request][metabase.util :as u]@@ -51,7 +52,7 @@ saved later when it is ready."(let [futur (-> querylegacy-result-metadata-future)metadata' (if valid-metadata?- (map mbql.normalize/normalize-source-metadata metadata)+ (lib/normalize ::qp.schema/result-metadata.columns metadata)original-metadata)result (deref futur metadata-sync-wait-ms ::timed-out)combiner (fn [result]diff --git a/src/metabase/queries/schema.clj b/src/metabase/queries/schema.cljindex 24bfd096510..5176bb708ff 100644--- a/src/metabase/queries/schema.clj+++ b/src/metabase/queries/schema.clj@@ -49,7 +49,8 @@[:dataset_query {:optional true} [:maybe ::query]][:parameters {:optional true} [:maybe [:ref ::parameters.schema/parameters]]][:parameter_mappings {:optional true} [:maybe [:ref ::parameters.schema/parameter-mappings]]]- [:type {:optional true} [:maybe ::lib.schema.metadata/card.type]]])+ [:type {:optional true} [:maybe ::lib.schema.metadata/card.type]]+ [:result_metadata {:optional true} [:maybe [:sequential ::lib.schema.metadata/lib-or-legacy-column]]]])(mu/defn normalize-card :- [:maybe ::card]"Normalize a `card` so it satisfies the `::card` schema."diff --git a/src/metabase/query_permissions/impl.clj b/src/metabase/query_permissions/impl.cljindex fb40b80dacb..38a99ba951e 100644--- a/src/metabase/query_permissions/impl.clj+++ b/src/metabase/query_permissions/impl.clj@@ -8,7 +8,8 @@[clojure.set :as set][clojure.walk :as walk][metabase.api.common :as api]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ;; legacy usage -- do not use in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata]diff --git a/src/metabase/query_processor/card.clj b/src/metabase/query_processor/card.cljindex cf0bae16aa4..556f7a6a654 100644--- a/src/metabase/query_processor/card.clj+++ b/src/metabase/query_processor/card.clj@@ -7,10 +7,6 @@[metabase.api.common :as api][metabase.cache.core :as cache][metabase.events.core :as events]- ;; legacy usages -- don't use Legacy MBQL utils in QP code going forward, prefer Lib. This will be updated to use- ;; Lib soon- ^{:clj-kondo/ignore [:discouraged-namespace]}- [metabase.legacy-mbql.util :as mbql.u][metabase.lib.core :as lib][metabase.lib.schema :as lib.schema][metabase.lib.schema.id :as lib.schema.id]@@ -18,6 +14,7 @@[metabase.lib.schema.parameter :as lib.schema.parameter][metabase.lib.schema.template-tag :as lib.schema.template-tag][metabase.lib.util.match :as lib.util.match]+ [metabase.parameters.schema :as parameters.schema][metabase.premium-features.core :refer [defenterprise]][metabase.queries.core :as queries][metabase.queries.schema :as queries.schema]@@ -56,12 +53,11 @@(assoc strategy :avg-execution-ms (or et 0)))strategy))-(defn- explict-stage-references- [parameters]+(mu/defn- explict-stage-references :- [:set :int]+ [parameters :- [:maybe ::parameters.schema/parameters]](into #{}(keep (fn [{:keys [target]}]- (when (mbql.u/is-clause? :dimension target)- (get-in target [2 :stage-number]))))+ (some-> target lib/parameter-target-dimension-options :stage-number)))parameters))(defn- point-parameters-to-last-stage@@ -72,9 +68,8 @@[parameters](mapv (fn [{:keys [target], :as parameter}](cond-> parameter- (and (mbql.u/is-clause? :dimension target)- (some? (get-in target [2 :stage-number])))- (assoc-in [:target 2 :stage-number] -1)))+ (:stage-number (lib/parameter-target-dimension-options target))+ (update :target lib/update-parameter-target-dimension-options assoc :stage-number -1)))parameters))(mu/defn- last-stage-number@@ -87,20 +82,17 @@(mapv (fn [{param-type :type, :keys [target], :as parameter}](cond-> parameter(and (= param-type :temporal-unit)- (mbql.u/is-clause? :dimension target)- (nil? (get-in target [2 :stage-number])))- (assoc-in [:target 2 :stage-number] -2)))+ (lib/parameter-target-is-dimension? target)+ (nil? (:stage-number (lib/parameter-target-dimension-options target))))+ (update :target lib/update-parameter-target-dimension-options assoc :stage-number -2)))parameters))(mu/defn query-for-card :- [:maybe ::lib.schema/query]"Generate a query for a saved Card"[{dataset-query :dataset_querycard-type :type- :as card} :- [:map- [:dataset_query [:or- [:= {:description "empty map"} {}]- ::lib.schema/query]]]- parameters :- [:maybe [:sequential :map]]+ :as card} :- ::queries.schema/card+ parameters :- [:maybe ::parameters.schema/parameters]constraints :- [:maybe :map]middleware :- [:maybe :map]& [ids]]@@ -180,15 +172,6 @@[param-name tag-type])))(lib/all-template-tags-map query))))-(defn- allowed-parameter-type-for-template-tag-widget-type? [parameter-type widget-type]- (when-let [allowed-template-tag-types (get-in lib.schema.parameter/types [parameter-type :allowed-for])]- (contains? allowed-template-tag-types widget-type)))--(defn- allowed-parameter-types-for-template-tag-widget-type [widget-type]- (into #{} (for [[parameter-type {:keys [allowed-for]}] lib.schema.parameter/types- :when (contains? allowed-for widget-type)]- parameter-type)))-(mu/defn check-allowed-parameter-value-type"If a parameter (i.e., a template tag or Dashboard parameter) is specified with `widget-type` (e.g.`:date/all-options`), make sure a user is allowed to pass in parameters with value type `parameter-value-type` (e.g.@@ -199,14 +182,17 @@Background: some more-specific parameter types aren't allowed for certain types of parameters.See [[metabase.legacy-mbql.schema/parameter-types]] for details."- [parameter-name- widget-type :- ::lib.schema.template-tag/widget-type+ [parameter-name :- :string+ widget-type :- ::lib.schema.parameter/widget-typeparameter-value-type :- ::lib.schema.parameter/type]- (when-not (allowed-parameter-type-for-template-tag-widget-type? parameter-value-type widget-type)- (let [allowed-types (allowed-parameter-types-for-template-tag-widget-type widget-type)]- (throw (ex-info (tru "Invalid parameter type {0} for parameter {1}. Parameter type must be one of: {2}"+ (when-not (lib.schema.parameter/parameter-type-and-widget-type-allowed-together? parameter-value-type widget-type)+ (let [allowed-types (lib.schema.parameter/allowed-parameter-types-for-template-tag-widget-type widget-type)]+ ;; if we're running into errors where `parameter-value-type` is `:text`, that might be because it's the+ ;; `:default` value in the schema these days+ (throw (ex-info (tru "Invalid parameter value type {0} for parameter {1} with widget type {2}. Parameter value must be one of: {3}"parameter-value-type(pr-str parameter-name)+ widget-type(str/join ", " (sort allowed-types))){:type qp.error-type/invalid-parameter:invalid-parameter parameter-name@@ -256,7 +242,7 @@(qp.streaming/streaming-response [rff export-format (qp.streaming/safe-filename-prefix (:card-name info))](qp (update query :info merge info) rff))))-(mu/defn combined-parameters-and-template-tags+(mu/defn combined-parameters-and-template-tags :- [:maybe ::parameters.schema/parameters]"Enrich `card.parameters` to include parameters from template-tags.On native queries parameters exists in 2 forms:@@ -277,9 +263,10 @@id->parameterid->template-tags-parameter))))-(defn- enrich-parameters-from-card+(mu/defn- enrich-parameters-from-card :- ::parameters.schema/parameters"Allow the FE to omit type and target for parameters by adding them from the card."- [parameters card-parameters]+ [parameters :- [:maybe ::parameters.schema/parameters-with-optional-types]+ card-parameters :- [:maybe ::parameters.schema/parameters]](let [id->card-param (->> card-parameters(map #(select-keys % [:id :type :target]))(m/index-by :id))]@@ -289,22 +276,22 @@"The context to use for tracking the view. Return nil if the view should not be tracked"[{:keys [context]}](case context- :public-dashboard :dashboard- :public-question :question- :embedded-dashboard :dashboard- :embedded-question :question- :csv-download nil- :public-csv-download nil- :embedded-csv-download nil- :json-download nil- :public-json-download nil+ :public-dashboard :dashboard+ :public-question :question+ :embedded-dashboard :dashboard+ :embedded-question :question+ :csv-download nil+ :public-csv-download nil+ :embedded-csv-download nil+ :json-download nil+ :public-json-download nil:embedded-json-download nil- :xlsx-download nil- :public-xlsx-download nil+ :xlsx-download nil+ :public-xlsx-download nil:embedded-xlsx-download nil:dashboard-subscription nil- :pulse nil- :map-tiles nil+ :pulse nil+ :map-tiles nilcontext))(mu/defn process-query-for-card@@ -339,6 +326,7 @@:cache_invalidated_at :entity_id :created_at :card_schema:parameters]:id card-id))+ parameters (some-> parameters parameters.schema/normalize-parameters-without-adding-default-types)parameters (enrich-parameters-from-card parameters (combined-parameters-and-template-tags card))dash-viz (when (and (not= context :question)dashcard-id)diff --git a/src/metabase/query_processor/metadata.clj b/src/metabase/query_processor/metadata.cljindex f23db67639b..ecdd482035c 100644--- a/src/metabase/query_processor/metadata.clj+++ b/src/metabase/query_processor/metadata.clj@@ -8,12 +8,6 @@[metabase.analyze.core :as analyze][metabase.driver :as driver][metabase.driver.util :as driver.u]- ;; legacy usage -- don't use Legacy MBQL utils in QP code going forward, prefer Lib. This will be updated to use- ;; Lib soon- ^{:clj-kondo/ignore [:discouraged-namespace]}- [metabase.legacy-mbql.normalize :as mbql.normalize]- ^{:clj-kondo/ignore [:discouraged-namespace]}- [metabase.legacy-mbql.schema :as mbql.s][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib][metabase.lib.schema.id :as lib.schema.id]@@ -136,7 +130,8 @@(add-extra-column-metadata ::mlv2)))(result-metadata* query current-user-id))))-(mu/defn- ensure-legacy :- ::mbql.s/legacy-column-metadata+(mu/defn- ensure-legacy :- ::qp.schema/result-metadata.column+ {:deprecated "0.57.0"}[col :- :map](letfn [(ensure-field-ref [col];; HACK for backward compatibility with FE stuff -- ideally we would be able to remove this entirely but@@ -147,9 +142,9 @@lib/lib-metadata-column->legacy-metadata-column(add-extra-column-metadata ::legacy)ensure-field-ref- mbql.normalize/normalize-source-metadata)))+ (->> (lib/normalize ::qp.schema/result-metadata.column)))))-(mu/defn legacy-result-metadata :- [:maybe [:sequential ::mbql.s/legacy-column-metadata]]+(mu/defn legacy-result-metadata :- [:maybe ::qp.schema/result-metadata.columns]"Like [[result-metadata]], but return metadata in legacy format rather than MLv2 format. This should be considereddeprecated, as we're working on moving towards using MLv2-style metadata everywhere; avoid new usages of this functionif possible, and prefer [[result-metadata]] instead.@@ -160,5 +155,6 @@[query :- :mapcurrent-user-id :- [:maybe ::lib.schema.id/user]](mapv+ #_{:clj-kondo/ignore [:deprecated-var]}ensure-legacy(result-metadata* query current-user-id)))diff --git a/src/metabase/query_processor/middleware/add_remaps.clj b/src/metabase/query_processor/middleware/add_remaps.cljindex 09a1db8a964..b6535d9a994 100644--- a/src/metabase/query_processor/middleware/add_remaps.clj+++ b/src/metabase/query_processor/middleware/add_remaps.clj@@ -39,7 +39,6 @@[metabase.lib.schema.metadata :as lib.schema.metadata][metabase.lib.schema.order-by :as lib.schema.order-by][metabase.lib.schema.ref :as lib.schema.ref]- [metabase.lib.util :as lib.util][metabase.lib.walk :as lib.walk][metabase.query-processor.middleware.large-int :as large-int][metabase.query-processor.schema :as qp.schema]@@ -129,7 +128,7 @@;;;; TODO (Cam 7/23/25) -- this seems sorta busted, we should probably be using `:lib/desired-column-alias` here;; instead.- (let [name-generator (lib.util/unique-name-generator)+ (let [name-generator (lib/unique-name-generator)unique-name (fn [field-id](assert (pos-int? field-id) (str "Invalid Field ID: " (pr-str field-id)))(let [field (lib.metadata/field query field-id)]diff --git a/src/metabase/query_processor/middleware/metrics.clj b/src/metabase/query_processor/middleware/metrics.cljindex 6aee60066d1..21b7c307100 100644--- a/src/metabase/query_processor/middleware/metrics.clj+++ b/src/metabase/query_processor/middleware/metrics.clj@@ -255,7 +255,7 @@:aggregation(fetch-referenced-metrics query))](let [temp-query (temp-query-at-stage-path query path)- unique-name-fn (lib.util/unique-name-generator+ unique-name-fn (lib/unique-name-generator(map(comp :lib/expression-name second)(lib/expressions temp-query)))diff --git a/src/metabase/query_processor/pivot.clj b/src/metabase/query_processor/pivot.cljindex e72406dfc62..5582d218ccd 100644--- a/src/metabase/query_processor/pivot.clj+++ b/src/metabase/query_processor/pivot.clj@@ -385,7 +385,7 @@metadata-provider (or (:lib/metadata query)(lib-be/application-database-metadata-provider (:database query)))query (lib/query metadata-provider query)- unique-name-fn (lib.util/unique-name-generator)+ unique-name-fn (lib/unique-name-generator)returned-columns (->> (lib/returned-columns query)(mapv #(update % :name unique-name-fn)))aggregations (filter #(= (:lib/source %) :source/aggregations)diff --git a/src/metabase/query_processor/preprocess.clj b/src/metabase/query_processor/preprocess.cljindex d3c790fba7c..899af408faa 100644--- a/src/metabase/query_processor/preprocess.clj+++ b/src/metabase/query_processor/preprocess.clj@@ -1,10 +1,6 @@(ns metabase.query-processor.preprocess(:require[metabase.config.core :as config]- ;; legacy usage -- don't use Legacy MBQL utils in QP code going forward, prefer Lib. This is allowed for now- ;; because the QP still returns legacy-style metadata (for now)- ^{:clj-kondo/ignore [:discouraged-namespace]}- [metabase.legacy-mbql.schema :as mbql.s][metabase.lib.schema :as lib.schema][metabase.query-processor.debug :as qp.debug][metabase.query-processor.error-type :as qp.error-type]@@ -42,6 +38,7 @@[metabase.query-processor.middleware.validate :as validate][metabase.query-processor.middleware.validate-temporal-bucketing :as validate-temporal-bucketing][metabase.query-processor.middleware.wrap-value-literals :as qp.wrap-value-literals]+ [metabase.query-processor.schema :as qp.schema][metabase.query-processor.setup :as qp.setup][metabase.util :as u][metabase.util.i18n :as i18n]@@ -147,7 +144,7 @@querymiddleware)))-(mu/defn query->expected-cols :- [:maybe [:sequential ::mbql.s/legacy-column-metadata]]+(mu/defn query->expected-cols :- [:maybe ::qp.schema/result-metadata.columns]"Return the `:cols` you would normally see in MBQL query results by preprocessing the query and calling `annotate` onit. This only works for pure MBQL queries, since it does not actually run the queries. Native queries or MBQLqueries with native source queries won't work, since we don't need the results."diff --git a/src/metabase/query_processor/schema.clj b/src/metabase/query_processor/schema.cljindex a50496ca7ee..9ad75e19c3e 100644--- a/src/metabase/query_processor/schema.clj+++ b/src/metabase/query_processor/schema.clj@@ -1,5 +1,9 @@(ns metabase.query-processor.schema(:require+ ;; legacy usage -- don't use Legacy MBQL utils in QP code going forward, prefer Lib. This is allowed for now+ ;; because the QP still returns legacy-style metadata (for now)+ ^{:clj-kondo/ignore [:discouraged-namespace]}+ [metabase.legacy-mbql.schema :as mbql.s][metabase.lib.schema.id :as lib.schema.id][metabase.util :as u][metabase.util.malli.registry :as mr]@@ -67,3 +71,11 @@(into [:enum {:decode/json keyword:api/regex export-formats-regex}]export-formats))++(mr/def ::result-metadata.column+ "A single result metadata column as returned by the Query Processor."+ [:ref ::mbql.s/legacy-column-metadata])++(mr/def ::result-metadata.columns+ "A sequence of result metadata columns as returned by the Query Processor."+ [:sequential ::result-metadata.column])diff --git a/src/metabase/query_processor/streaming.clj b/src/metabase/query_processor/streaming.cljindex c17a1efc257..6d1532bb4ad 100644--- a/src/metabase/query_processor/streaming.clj+++ b/src/metabase/query_processor/streaming.clj@@ -1,11 +1,8 @@(ns metabase.query-processor.streaming- (:refer-clojure :exclude [every? some])+ (:refer-clojure :exclude [every? some mapv])(:require[clojure.string :as str]- ;; legacy usage -- don't use Legacy MBQL utils in QP code going forward, prefer Lib. This will be updated to use- ;; Lib soon- ^{:clj-kondo/ignore [:discouraged-namespace]}- [metabase.legacy-mbql.util :as mbql.u]+ [metabase.lib.core :as lib][metabase.lib.schema.common :as lib.schema.common][metabase.models.visualization-settings :as mb.viz][metabase.query-processor.pipeline :as qp.pipeline]@@ -18,7 +15,7 @@[metabase.util :as u][metabase.util.log :as log][metabase.util.malli :as mu]- [metabase.util.performance :refer [every? some]])+ [metabase.util.performance :refer [every? mapv some]])(:import(clojure.core.async.impl.channels ManyToManyChannel)(java.io OutputStream)@@ -51,14 +48,13 @@TODO (Cam 9/23/25) -- We should use [[metabase.lib.field.util/add-deduplicated-names]] to do this."[cols]- (map (fn [col unique-name]- (-> col- (cond-> (not (:display_name col)) (assoc :display_name (:name col)))- (assoc :name unique-name)))- cols- ;; existing usage -- do not use going forward- #_{:clj-kondo/ignore [:deprecated-var]}- (mbql.u/uniquify-names (map :name cols))))+ (mapv (let [unique-name-fn (lib/non-truncating-unique-name-generator)]+ (fn [col]+ (let [unique-name (unique-name-fn (:name col))]+ (-> col+ (cond-> (not (:display_name col)) (assoc :display_name (:name col)))+ (assoc :name unique-name)))))+ cols))(defn- validate-table-columns"Validate that all of the columns in `table-columns` correspond to actual columns in `cols`, correlating them bydiff --git a/src/metabase/query_processor/util/add_alias_info.clj b/src/metabase/query_processor/util/add_alias_info.cljindex 8df578546c5..3cffea3afba 100644--- a/src/metabase/query_processor/util/add_alias_info.clj+++ b/src/metabase/query_processor/util/add_alias_info.clj@@ -51,7 +51,6 @@[metabase.lib.schema :as lib.schema][metabase.lib.schema.join :as lib.schema.join][metabase.lib.schema.metadata :as lib.schema.metadata]- [metabase.lib.util :as lib.util][metabase.lib.util.match :as lib.util.match][metabase.lib.walk :as lib.walk][metabase.query-processor.error-type :as qp.error-type]@@ -80,7 +79,7 @@(defn- escape-fn []{:pre [(keyword? driver/*driver*)]}- (let [f (lib.util/unique-name-generator)+ (let [f (lib/unique-name-generator)driver driver/*driver*](fn [s](->> s@@ -440,8 +439,8 @@[query :- ::lib.schema/query{:keys [globally-unique-join-aliases?], :or {globally-unique-join-aliases? false}} :- [:maybe ::options]](let [make-join-alias-unique-name-generator (if globally-unique-join-aliases?- (constantly (lib.util/unique-name-generator))- lib.util/unique-name-generator)]+ (constantly (lib/unique-name-generator))+ lib/unique-name-generator)](lib.walk/walk-stagesquery(fn [_query _path stage]diff --git a/src/metabase/query_processor/util/nest_query.clj b/src/metabase/query_processor/util/nest_query.cljindex 8ecfe892be1..0c2f479c3bf 100644--- a/src/metabase/query_processor/util/nest_query.clj+++ b/src/metabase/query_processor/util/nest_query.clj@@ -13,7 +13,7 @@;; Lib only soon^{:clj-kondo/ignore [:discouraged-namespace]}[metabase.legacy-mbql.schema :as mbql.s]- ^{:clj-kondo/ignore [:discouraged-namespace]}+ ^{:clj-kondo/ignore [:discouraged-namespace :deprecated-namespace]}[metabase.legacy-mbql.util :as mbql.u][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata]@@ -239,7 +239,7 @@"Convert an `:expression` reference from a source query into an appropriate `:field` clause for use in the surroundingquery."[{:keys [source-query], :as query} [_ expression-name opts :as _clause]]- (let [expression-definition (mbql.u/expression-with-name query expression-name)+ (let [expression-definition #_{:clj-kondo/ignore [:deprecated-var]} (mbql.u/expression-with-name query expression-name)base-type (infer-expression-type query expression-definition){::add/keys [desired-alias]} (lib.util.match/match-one source-query[:expression (_ :guard (partial = expression-name)) source-opts]@@ -281,7 +281,8 @@;; In fact, we don't mark all Fields, only the ones we deem coercible. Marking all would make a bunch of tests;; fail, but it might still make sense. For example, #48721 would have been avoided by unconditional marking.(_ :guard coercible-field-ref?)- (recur (mbql.u/update-field-options &match assoc :qp/ignore-coercion true))+ (recur #_{:clj-kondo/ignore [:deprecated-var]}+ (mbql.u/update-field-options &match assoc :qp/ignore-coercion true))[:field id-or-name (opts :guard :join-alias)](let [{::add/keys [desired-alias]} (lib.util.match/match-one (:source-query query)diff --git a/src/metabase/segments/api.clj b/src/metabase/segments/api.cljindex 7a208259b93..7621858c81a 100644--- a/src/metabase/segments/api.clj+++ b/src/metabase/segments/api.clj@@ -1,10 +1,11 @@(ns metabase.segments.api"/api/segment endpoints."(:require+ ;; TODO (Cam 10/1/25) -- update segments to persist MBQL 5[metabase.api.common :as api][metabase.api.macros :as api.macros][metabase.events.core :as events]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.models.interface :as mi][metabase.util :as u][metabase.util.log :as log]diff --git a/src/metabase/segments/models/segment.clj b/src/metabase/segments/models/segment.cljindex 7f3d9ba63d1..b5a4753edfd 100644--- a/src/metabase/segments/models/segment.clj+++ b/src/metabase/segments/models/segment.clj@@ -5,9 +5,10 @@[clojure.set :as set][malli.error :as me][metabase.api.common :as api]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.schema :as mbql.s]- [metabase.legacy-mbql.util :as mbql.u]+ ;; existing usages, do not use legacy MBQL utils in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s]+ ^{:clj-kondo/ignore [:discouraged-namespace :deprecated-namespace]} [metabase.legacy-mbql.util :as mbql.u][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib][metabase.lib.metadata.protocols :as lib.metadata.protocols]diff --git a/src/metabase/tiles/api.clj b/src/metabase/tiles/api.cljindex 913a780c682..ded10c72ad9 100644--- a/src/metabase/tiles/api.clj+++ b/src/metabase/tiles/api.clj@@ -3,8 +3,9 @@(:require[metabase.api.common :as api][metabase.api.macros :as api.macros]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.schema :as mbql.s]+ ;; TODO (Cam 10/10/25) -- update the tile API to use MBQL 5+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib][metabase.lib.schema :as lib.schema]diff --git a/src/metabase/upload/impl.clj b/src/metabase/upload/impl.cljindex e44c64b6e52..175f3d560c1 100644--- a/src/metabase/upload/impl.clj+++ b/src/metabase/upload/impl.clj@@ -16,9 +16,7 @@[metabase.driver.sync :as driver.s][metabase.driver.util :as driver.u][metabase.events.core :as events]- [metabase.legacy-mbql.util :as mbql.u][metabase.lib.core :as lib]- [metabase.lib.util :as lib.util][metabase.model-persistence.core :as model-persistence][metabase.models.humanization :as humanization][metabase.models.interface :as mi]@@ -358,17 +356,17 @@(fn [base suffix](as-> (str base separator suffix) %(driver/escape-alias driver %)- (lib.util/truncate-alias % max-length)))))+ (lib/truncate-alias % max-length)))))(defn- derive-display-names [driver header]- (let [generator-fn (mbql.u/unique-name-generator :unique-alias-fn (unique-alias-fn driver " "))]+ (let [generator-fn (lib/unique-name-generator-with-options {:unique-alias-fn (unique-alias-fn driver " ")})](mapv generator-fn(for [h header](humanization/name->human-readable-name(normalize-display-name h))))))(defn- derive-column-names [driver header]- (let [generator-fn (mbql.u/unique-name-generator :unique-alias-fn (unique-alias-fn driver "_"))]+ (let [generator-fn (lib/unique-name-generator-with-options {:unique-alias-fn (unique-alias-fn driver "_")})](mapv (comp keyword generator-fn)(for [h header] (normalize-column-name driver h)))))diff --git a/src/metabase/util/malli.cljc b/src/metabase/util/malli.cljcindex 751b179dd02..16b8aee299b 100644--- a/src/metabase/util/malli.cljc+++ b/src/metabase/util/malli.cljc@@ -11,7 +11,6 @@[malli.destructure][malli.error :as me][malli.util :as mut]- [metabase.util :as u][metabase.util.i18n :as i18n][metabase.util.malli.registry :as mr])#?(:cljs (:require-macros [metabase.util.malli])))@@ -131,36 +130,6 @@{:error (explain schema-or-validator value)})))value))))-(core/defn require-all-keys- "Ensure maps has no optional keys, maybe is required."- [schema]- (mc/walk- schema- (mc/schema-walker- (core/fn [schema]- (case (mc/type schema)- :map- (mc/-set-children schema- (mapv (core/fn [[k p s]]- [k (dissoc p :optional) s]) (mc/children schema)))- :maybe- (first (mc/children schema))-- schema)))))--(core/defn snake-keyed-schema- "Ensure all maps has snake key schemas"- [schema]- (mc/walk- schema- (mc/schema-walker (core/fn [schema]- (if (= :map (mc/type schema))- (mc/-set-children schema- (mapv (core/fn [[k p s]]- [(u/->snake_case_en k) p s]) (mc/children schema)))-- schema)))))-(core/defn map-schema-keys"Return a set of keys specified in a map `schema`. Resolves refs in the registry and handles maps wrapped in `:and`or combined with `:merge`.diff --git a/src/metabase/xrays/automagic_dashboards/combination.clj b/src/metabase/xrays/automagic_dashboards/combination.cljindex c5d67a254b1..7a92eba6d3d 100644--- a/src/metabase/xrays/automagic_dashboards/combination.clj+++ b/src/metabase/xrays/automagic_dashboards/combination.clj@@ -21,7 +21,8 @@[clojure.walk :as walk][medley.core :as m][metabase.driver.util :as driver.u]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ;; legacy usage, do not use this in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.schema.id :as lib.schema.id][metabase.models.interface :as mi][metabase.queries.core :as queries]diff --git a/src/metabase/xrays/automagic_dashboards/comparison.clj b/src/metabase/xrays/automagic_dashboards/comparison.cljindex e775d20d446..519db963352 100644--- a/src/metabase/xrays/automagic_dashboards/comparison.clj+++ b/src/metabase/xrays/automagic_dashboards/comparison.clj@@ -2,7 +2,8 @@(:require[medley.core :as m][metabase.api.common :as api]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ;; legacy usage, do not use this in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.schema.id :as lib.schema.id][metabase.models.interface :as mi][metabase.util :as u]diff --git a/src/metabase/xrays/automagic_dashboards/core.clj b/src/metabase/xrays/automagic_dashboards/core.cljindex 28ebc84e053..68039936194 100644--- a/src/metabase/xrays/automagic_dashboards/core.clj+++ b/src/metabase/xrays/automagic_dashboards/core.clj@@ -148,8 +148,9 @@[kixi.stats.math :as math][medley.core :as m][metabase.analyze.core :as analyze]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.schema :as mbql.s]+ ;; legacy usage, do not use this in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s][metabase.lib.core :as lib][metabase.lib.schema.id :as lib.schema.id][metabase.models.interface :as mi]diff --git a/src/metabase/xrays/automagic_dashboards/filters.clj b/src/metabase/xrays/automagic_dashboards/filters.cljindex ff4e7ffdf2f..e498511e9ed 100644--- a/src/metabase/xrays/automagic_dashboards/filters.clj+++ b/src/metabase/xrays/automagic_dashboards/filters.clj@@ -1,7 +1,8 @@(ns metabase.xrays.automagic-dashboards.filters(:require- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.util :as mbql.u]+ ;; legacy usage, do not use this in new code+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:deprecated-namespace :discouraged-namespace]} [metabase.legacy-mbql.util :as mbql.u][metabase.util :as u][metabase.util.date-2 :as u.date][metabase.util.malli :as mu]diff --git a/src/metabase/xrays/automagic_dashboards/interesting.clj b/src/metabase/xrays/automagic_dashboards/interesting.cljindex e792df9b836..f6e0d771767 100644--- a/src/metabase/xrays/automagic_dashboards/interesting.clj+++ b/src/metabase/xrays/automagic_dashboards/interesting.clj@@ -44,9 +44,11 @@[clojure.walk :as walk][java-time.api :as t][medley.core :as m]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.schema :as mbql.s]- [metabase.legacy-mbql.util :as mbql.u]+ ;; legacy usages, do not use legacy MBQL stuff in new code.+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s]+ ^{:clj-kondo/ignore [:deprecated-namespace :discouraged-namespace]} [metabase.legacy-mbql.util :as mbql.u]+ [metabase.lib.schema.ref :as lib.schema.ref][metabase.models.interface :as mi][metabase.util :as u][metabase.util.date-2 :as u.date]@@ -75,7 +77,7 @@:type/DateTime((juxt :earliest :latest))(map u.date/parse))- can-use? #(mbql.s/valid-temporal-unit-for-base-type? (:base_type field) %)]+ can-use? #(lib.schema.ref/valid-temporal-unit-for-base-type? (:base_type field) %)](if (and earliest latest)(let [duration (u.date/period-duration earliest latest)less-than? #(u.date/greater-than-period-duration? % duration)]diff --git a/src/metabase/xrays/automagic_dashboards/names.clj b/src/metabase/xrays/automagic_dashboards/names.cljindex 82e045bcbc4..7ddce36d57f 100644--- a/src/metabase/xrays/automagic_dashboards/names.clj+++ b/src/metabase/xrays/automagic_dashboards/names.clj@@ -2,7 +2,8 @@(:require[clojure.string :as str][java-time.api :as t]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ;; legacy usages, do not use legacy MBQL stuff in new code.+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.util.match :as lib.util.match][metabase.query-processor.util :as qp.util][metabase.util.date-2 :as u.date]diff --git a/src/metabase/xrays/automagic_dashboards/schema.clj b/src/metabase/xrays/automagic_dashboards/schema.cljindex bbaaf3f3930..b7a58d6b0ee 100644--- a/src/metabase/xrays/automagic_dashboards/schema.clj+++ b/src/metabase/xrays/automagic_dashboards/schema.clj@@ -2,7 +2,8 @@(:require[malli.core :as mc][malli.util :as mut]- [metabase.legacy-mbql.schema :as mbql.s]+ ;; legacy usages, do not use legacy MBQL stuff in new code.+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s][metabase.lib.core :as lib][metabase.lib.schema :as lib.schema][metabase.lib.schema.common :as lib.schema.common]diff --git a/src/metabase/xrays/automagic_dashboards/util.clj b/src/metabase/xrays/automagic_dashboards/util.cljindex 74bfa0e2832..6b415930ed4 100644--- a/src/metabase/xrays/automagic_dashboards/util.clj+++ b/src/metabase/xrays/automagic_dashboards/util.clj@@ -4,8 +4,9 @@[clojure.string :as str][medley.core :as m][metabase.analyze.core :as analyze]- [metabase.legacy-mbql.schema :as mbql.s]- [metabase.legacy-mbql.util :as mbql.u]+ ;; legacy usages, do not use legacy MBQL stuff in new code.+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s]+ ^{:clj-kondo/ignore [:deprecated-namespace :discouraged-namespace]} [metabase.legacy-mbql.util :as mbql.u][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib][metabase.lib.schema :as lib.schema]@@ -50,10 +51,14 @@(def ^{:arglists '([metric])} saved-metric?"Is metric a saved (V2) metric? (Note that X-Rays do not currently know how to handle Saved V2 Metrics.)"+ ;; legacy usage, do not use legacy MBQL stuff in new code.+ #_{:clj-kondo/ignore [:deprecated-var]}(partial mbql.u/is-clause? :metric))(def ^{:arglists '([metric])} custom-expression?"Is this a custom expression?"+ ;; legacy usage, do not use legacy MBQL stuff in new code.+ #_{:clj-kondo/ignore [:deprecated-var]}(partial mbql.u/is-clause? :aggregation-options))(def ^{:arglists '([metric])} adhoc-metric?diff --git a/src/metabase/xrays/domain_entities/core.clj b/src/metabase/xrays/domain_entities/core.cljindex 961970c2288..aef4470ec67 100644--- a/src/metabase/xrays/domain_entities/core.clj+++ b/src/metabase/xrays/domain_entities/core.clj@@ -2,7 +2,8 @@(:require[clojure.string :as str][medley.core :as m]- [metabase.legacy-mbql.util :as mbql.u]+ ;; legacy usage, do not use legacy MBQL stuff in new code.+ ^{:clj-kondo/ignore [:deprecated-namespace :discouraged-namespace]} [metabase.legacy-mbql.util :as mbql.u][metabase.lib.schema.common :as lib.schema.common][metabase.lib.util.match :as lib.util.match][metabase.util :as u]@@ -49,6 +50,8 @@(let [[table-or-dimension maybe-dimension] (str/split dimension-reference #"\.")](if maybe-dimension(let [field-clause (get-in bindings [table-or-dimension :dimensions maybe-dimension])]+ ;; legacy usage, do not use legacy MBQL stuff in new code.+ #_{:clj-kondo/ignore [:deprecated-var]}(cond-> field-clause(not= source table-or-dimension) (mbql.u/assoc-field-options :join-alias table-or-dimension)))(get-in bindings [source :dimensions table-or-dimension]))))diff --git a/src/metabase/xrays/domain_entities/specs.clj b/src/metabase/xrays/domain_entities/specs.cljindex 236d72fcb6f..7aab35cc84f 100644--- a/src/metabase/xrays/domain_entities/specs.clj+++ b/src/metabase/xrays/domain_entities/specs.clj@@ -3,9 +3,9 @@[malli.core :as mc][malli.transform :as mtx][medley.core :as m]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.schema :as mbql.s]- [metabase.legacy-mbql.util :as mbql.u]+ ;; legacy usages, do not use legacy MBQL stuff in new code.+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s][metabase.lib.schema.id :as lib.schema.id][metabase.util.malli.registry :as mr][metabase.util.malli.schema :as ms]@@ -17,7 +17,8 @@{:decode/domain-entity-spec mbql.normalize/normalize:decode/transform-spec mbql.normalize/normalize:error/message "valid MBQL clause"}- mbql.u/mbql-clause?])+ ;;; TODO (Cam 10/10/25) -- update this to use [[metabase.lib.core/clause?]] once we convert X-Rays to MBQL 5+ #(and (vector? %) (keyword? (first %)))]);;; TODO (Cam 9/29/25) -- these decoding rules are BUSTED because it ends up creating totally nonsensical types like;;; `:type/AvgPrice` which is not a real type at all. We should probably just leave the field names as strings.diff --git a/src/metabase/xrays/related.clj b/src/metabase/xrays/related.cljindex 675f25ff213..b408e13ac00 100644--- a/src/metabase/xrays/related.clj+++ b/src/metabase/xrays/related.clj@@ -4,7 +4,8 @@[clojure.set :as set][medley.core :as m][metabase.api.common :as api]- [metabase.legacy-mbql.normalize :as mbql.normalize]+ ;; legacy usage, do not use legacy MBQL stuff in new code.+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.models.interface :as mi][metabase.query-processor.util :as qp.util][metabase.util.malli.registry :as mr]diff --git a/src/metabase/xrays/transforms/specs.clj b/src/metabase/xrays/transforms/specs.cljindex 1b9701d04b7..92f5514a512 100644--- a/src/metabase/xrays/transforms/specs.clj+++ b/src/metabase/xrays/transforms/specs.clj@@ -3,8 +3,9 @@[malli.core :as mc][malli.transform :as mtx][medley.core :as m]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.legacy-mbql.schema :as mbql.s]+ ;; legacy usages, do not use legacy MBQL stuff in new code.+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize]+ ^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.schema :as mbql.s][metabase.lib.util.match :as lib.util.match][metabase.util :as u][metabase.util.malli.registry :as mr]diff --git a/test/metabase/actions/execution_test.clj b/test/metabase/actions/execution_test.cljindex 774f4dd239b..090b7c3c128 100644--- a/test/metabase/actions/execution_test.clj+++ b/test/metabase/actions/execution_test.clj@@ -15,13 +15,14 @@(let [dataset-query (mt/mbql-query venues {:fields [$id $name]})query (assocdataset-query- :parameters [{:target [:dimension+ :parameters [{:id "metabase.actions.execution/prefetch-parameters-pk"+ :target [:dimension(-> dataset-query:query:fieldsfirst)]- :type "id"- :value [1]}]+ :type :id+ :value [1]}]:constraints nil:middleware nil:cache-strategy nil)]diff --git a/test/metabase/dashboards/api_test.clj b/test/metabase/dashboards/api_test.cljindex 7987a1649ae..271627e9aae 100644--- a/test/metabase/dashboards/api_test.clj+++ b/test/metabase/dashboards/api_test.clj@@ -728,10 +728,10 @@:parameter_mappings [{:parameter_id "_TEXT_":card_id card-id:target [:dimension [:template-tag "not-existed-filter"]]}]}]- (mt/with-log-messages-for-level [messages [metabase.parameters.params :error]]+ (mt/with-log-messages-for-level [messages [metabase.parameters.params :warn]](is (some? (mt/user-http-request :rasta :get 200 (str "dashboard/" dash-id))))- (is (=? [{:level :error- :message "Could not find matching field clause for target: [:dimension [:template-tag \"not-existed-filter\"]]"}]+ (is (=? [{:level :warn+ :message "Could not find matching Field ID for target: \"not-existed-filter\""}](messages)))))));;; +----------------------------------------------------------------------------------------------------------------+diff --git a/test/metabase/driver/common/parameters/values_test.clj b/test/metabase/driver/common/parameters/values_test.cljindex 02bb10f11e9..e6e2e7e5817 100644--- a/test/metabase/driver/common/parameters/values_test.clj+++ b/test/metabase/driver/common/parameters/values_test.clj@@ -781,7 +781,7 @@(mt/dataset test-data(qp.store/with-metadata-provider meta/metadata-provider(let [template-tags {"createdAt" {:type :dimension- :dimension [:field (meta/id :orders :created-at) {}]+ :dimension [:field (meta/id :orders :created-at) nil]:name "createdAt":id "4636d745-1467-4a70-ba20-2a08069d77ff":display-name "CreatedAt"diff --git a/test/metabase/driver/sql/parameters/substitution_test.clj b/test/metabase/driver/sql/parameters/substitution_test.cljindex 183788b621d..1829d8ef84f 100644--- a/test/metabase/driver/sql/parameters/substitution_test.clj+++ b/test/metabase/driver/sql/parameters/substitution_test.clj@@ -20,7 +20,6 @@(is (=? [:field(meta/id :venues :id){:base-type :type/BigInteger- :temporal-unit nil:metabase.query-processor.util.add-alias-info/source-table (meta/id :venues):metabase.driver.sql.parameters.substitution/compiling-field-filter? true}](#'sql.params.substitution/field->field-filter-clausediff --git a/test/metabase/legacy_mbql/js_test.cljs b/test/metabase/legacy_mbql/js_test.cljsdeleted file mode 100644index 724ef19acf5..00000000000--- a/test/metabase/legacy_mbql/js_test.cljs+++ /dev/null@@ -1,16 +0,0 @@-(ns metabase.legacy-mbql.js-test- (:require- [cljs.test :as t]- [metabase.legacy-mbql.js :as mbql.js]))--(t/deftest ^:parallel normalize-test- (t/testing "normalize should preserve keyword namespaces"- (let [js-query #js {"database" 1- "type" "query"- "query" #js {"source-table" 2- "fields" #js [#js ["field" "my_field" #js {"base-type" "type/Text"}]]}}]- (t/is (= {"database" 1- "type" "query"- "query" {"source-table" 2- "fields" [["field" "my_field" {"base-type" "type/Text"}]]}}- (-> js-query mbql.js/normalize js->clj))))))diff --git a/test/metabase/legacy_mbql/normalize_test.cljc b/test/metabase/legacy_mbql/normalize_test.cljcindex dfd6c3adaf5..733835c87e3 100644--- a/test/metabase/legacy_mbql/normalize_test.cljc+++ b/test/metabase/legacy_mbql/normalize_test.cljc@@ -4,8 +4,7 @@#?@(:cljs ([metabase.test-runner.assert-exprs.approximately-equal]))[clojure.set :as set][clojure.test :as t]- [metabase.legacy-mbql.normalize :as mbql.normalize]- [metabase.util.malli :as mu]))+ [metabase.legacy-mbql.normalize :as mbql.normalize]))#?(:cljs (comment metabase.test-runner.assert-exprs.approximately-equal/keep-me))@@ -1495,17 +1494,6 @@[:expression "expr" {:base-type :type/Date}][:field 66302 {:base-type :type/DateTime}]]))))-(t/deftest ^:parallel normalize-source-metadata-test- (t/testing "normalize-source-metadata"- (t/testing "should convert legacy field_refs to modern `:field` clauses"- (t/is (= {:field_ref [:field 1 {:temporal-unit :month}]}- (mbql.normalize/normalize-source-metadata- {:field_ref ["datetime-field" ["field-id" 1] "month"]}))))- (t/testing "should correctly keywordize Field options"- (t/is (= {:field_ref [:field 1 {:temporal-unit :month}]}- (mbql.normalize/normalize-source-metadata- {:field_ref ["field" 1 {:temporal-unit "month"}]}))))))-(t/deftest ^:parallel do-not-normalize-fingerprints-test(t/testing "Numbers in fingerprints shouldn't get normalized"(let [fingerprint {:global {:distinct-count 1, :nil% 0}@@ -1539,13 +1527,6 @@(t/is (= query(mbql.normalize/normalize query)))))))))-(t/deftest ^:parallel do-not-normalize-fingerprints-test-2- (let [col {:fingerprint {:global {:distinct-count 200, :nil% 0}- :type {:type/DateTime {:earliest "2016-04-26T19:29:55.147Z"- :latest "2019-04-15T13:34:19.931Z"}}}}]- (t/is (= col- (mbql.normalize/normalize-source-metadata col)))))-(t/deftest ^:parallel error-messages-test(t/testing "Normalization error messages should be sane"(let [bad-query {:database 1@@ -1666,18 +1647,3 @@(mbql.normalize/normalize-tokens [:datetime "" {:mode "iso"}])))(t/is (= [:datetime "" {:mode :iso}](mbql.normalize/normalize-tokens ["datetime" "" {"mode" "iso"}]))))--(t/deftest ^:parallel normalize-evil-source-metadata-test- (t/testing "Fix really messed up fingerprints with lower-cased type names (only in prod) (#63397)"- (mu/disable-enforcement- (t/is (= {:fingerprint- {:global {:distinct-count 418, :nil% 0.0},- :type- {:type/Text- {:percent-json 0.0, :percent-url 0.0, :percent-email 0.0, :percent-state 0.0, :average-length 13.26388888888889}}}}- (#'mbql.normalize/normalize-source-metadata- {:fingerprint- {:global {:distinct-count 418, :nil% 0.0},- :type- {:type/text- {:percent-json 0.0, :percent-url 0.0, :percent-email 0.0, :percent-state 0.0, :average-length 13.26388888888889}}}}))))))diff --git a/test/metabase/legacy_mbql/schema_test.cljc b/test/metabase/legacy_mbql/schema_test.cljcindex 2c68b81a776..5998fdf249e 100644--- a/test/metabase/legacy_mbql/schema_test.cljc+++ b/test/metabase/legacy_mbql/schema_test.cljc@@ -4,7 +4,9 @@[clojure.test :refer [are deftest is testing]][malli.error :as me][metabase.legacy-mbql.schema :as mbql.s]+ [metabase.lib.core :as lib][metabase.lib.schema.parameter :as lib.schema.parameter]+ [metabase.util.malli :as mu][metabase.util.malli.humanize :as mu.humanize][metabase.util.malli.registry :as mr]))@@ -45,7 +47,7 @@(deftest ^:parallel field-clause-test(testing "Make sure our schema validates `:field` clauses correctly"(doseq [[clause expected] {[:field 1 nil] true- [:field 1 {}] true+ [:field 1 {}] false[:field 1 {:x true}] true[:field 1 2] false[:field "wow" nil] false@@ -211,7 +213,7 @@::mbql.s/+ [:+ [:relative-datetime -1 :month] [:interval -2 :month]]))(deftest ^:parallel filter-test- (are [x] (not (me/humanize (mr/explain ::mbql.s/Filter x)))+ (are [x] (nil? (me/humanize (mr/explain ::mbql.s/Filter x)))[:value true nil][:value false nil][:expression "boolexpr"]@@ -250,3 +252,63 @@[:datetime "" {:mode :iso}][:datetime 10 {:mode :unix-seconds}]]](is (mr/validate mbql.s/datetime expr))))++(deftest ^:parallel normalize-source-metadata-test+ (testing "normalize-source-metadata"+ (testing "should convert legacy field_refs to modern `:field` clauses"+ (is (= {:field_ref [:field 1 {:temporal-unit :month}], :base_type :type/*}+ (lib/normalize ::mbql.s/legacy-column-metadata+ {:field_ref ["datetime-field" ["field-id" 1] "month"]}))))+ (testing "should correctly keywordize Field options"+ (is (= {:field_ref [:field 1 {:temporal-unit :month}], :base_type :type/*}+ (lib/normalize ::mbql.s/legacy-column-metadata+ {:field_ref ["field" 1 {:temporal-unit "month"}]}))))))++(deftest ^:parallel do-not-normalize-fingerprints-test+ (let [col {:fingerprint {:global {:distinct-count 200, :nil% 0}+ :type {:type/DateTime {:earliest "2016-04-26T19:29:55.147Z"+ :latest "2019-04-15T13:34:19.931Z"}}}}]+ (is (= (assoc col :base_type :type/*)+ (lib/normalize ::mbql.s/legacy-column-metadata col)))))++(deftest ^:parallel normalize-evil-source-metadata-test+ (testing "Fix really messed up fingerprints with lower-cased type names (only in prod) (#63397)"+ (mu/disable-enforcement+ (is (= {:base_type :type/*+ :fingerprint {:global {:distinct-count 418, :nil% 0.0},+ :type+ {:type/Text+ {:percent-json 0.0, :percent-url 0.0, :percent-email 0.0, :percent-state 0.0, :average-length 13.26388888888889}}}}+ (lib/normalize+ ::mbql.s/legacy-column-metadata+ {:fingerprint+ {:global {:distinct-count 418, :nil% 0.0},+ :type+ {:type/text+ {:percent-json 0.0, :percent-url 0.0, :percent-email 0.0, :percent-state 0.0, :average-length 13.26388888888889}}}}))))))++(deftest ^:parallel lib-normalize-legacy-field-ref-test+ (is (= [:field 100 nil]+ (lib/normalize ::mbql.s/field ["field" 100 {"temporal-unit" nil, "lib/uuid" "d01f4c83-0fe5-4329-80f3-2bbea1f27c3b"}]))))++(deftest ^:parallel lib-normalize-legacy-expression-ref-test+ (is (= [:expression "X"]+ (lib/normalize ::mbql.s/expression ["expression" "X" {"temporal-unit" nil, "lib/uuid" "d01f4c83-0fe5-4329-80f3-2bbea1f27c3b"}])))+ (is (= [:expression "X" {:temporal-unit :day}]+ (lib/normalize ::mbql.s/expression ["expression" "X" {"temporal-unit" "day", "lib/uuid" "d01f4c83-0fe5-4329-80f3-2bbea1f27c3b"}]))))++(deftest ^:parallel lib-normalize-legacy-binning-info-test+ (is (= {:base_type :type/*+ :binning_info {:binning_strategy :num-bins+ :num-bins 100+ :strategy :num-bins}+ :display_name "X"+ :name "X"}+ ;; populate `:strategy` if we only have `:binning_strategy`+ (lib/normalize ::mbql.s/legacy-column-metadata {:name "X"+ :binning_info {"binning_strategy" "num-bins", "num-bins" 100}})+ ;; prefer `:strategy` over `:binning_strategy` if they conflict+ (lib/normalize ::mbql.s/legacy-column-metadata {:name "X"+ :binning_info {"binning_strategy" "default"+ "strategy" "num-bins"+ "num-bins" 100}}))))diff --git a/test/metabase/legacy_mbql/util_test.cljc b/test/metabase/legacy_mbql/util_test.cljcindex 8b8b8597482..ee099c4c28c 100644--- a/test/metabase/legacy_mbql/util_test.cljc+++ b/test/metabase/legacy_mbql/util_test.cljc@@ -3,10 +3,10 @@(:require#?@(:clj ([metabase.test.util.i18n]):cljs ([metabase.test-runner.assert-exprs.approximately-equal]))- [clojure.string :as str][clojure.test :as t][metabase.legacy-mbql.util :as mbql.u]- [metabase.types.core]))+ [metabase.types.core]+ [metabase.util.malli :as mu]))(comment metabase.types.core/keep-me)@@ -613,59 +613,6 @@(t/is (= expected(apply mbql.u/aggregation-at-index query-with-some-nesting input))))))-(t/deftest ^:parallel uniquify-names- (t/testing "can we generate unique names?"- (t/is (= ["count" "sum" "count_2" "count_3"]- (mbql.u/uniquify-names ["count" "sum" "count" "count"]))))-- (t/testing "what if we try to trick it by using a name it would have generated?"- (t/is (= ["count" "count_2" "count_2_2"]- (mbql.u/uniquify-names ["count" "count" "count_2"]))))-- (t/testing (str "for wacky DBMSes like SQL Server that return blank column names sometimes let's make sure we handle "- "those without exploding")- (t/is (= ["" "_2"]- (mbql.u/uniquify-names ["" ""])))))--(t/deftest ^:parallel unique-name-generator-test- (t/testing "Can we get a simple unique name generator"- (t/is (= ["count" "sum" "count_2" "count_2_2"]- (map (mbql.u/unique-name-generator) ["count" "sum" "count" "count_2"])))))--(t/deftest ^:parallel unique-name-generator-test-2- (t/testing "Can we get an idempotent unique name generator"- (t/is (= ["count" "sum" "count" "count_2"]- (map (mbql.u/unique-name-generator) [:x :y :x :z] ["count" "sum" "count" "count_2"])))))--(t/deftest ^:parallel unique-name-generator-test-3- (t/testing "Can the same object have multiple aliases"- (t/is (= ["count" "sum" "count" "count_2"]- (map (mbql.u/unique-name-generator) [:x :y :x :x] ["count" "sum" "count" "count_2"])))))--(t/deftest ^:parallel unique-name-generator-idempotence-test- (t/testing "idempotence (2-arity calls to generated function) (#40994)"- (let [unique-name (mbql.u/unique-name-generator)]- (t/is (= ["A" "B" "A" "A_2" "A_2"]- [(unique-name :x "A")- (unique-name :x "B")- (unique-name :x "A")- (unique-name :y "A")- (unique-name :y "A")])))))--(t/deftest ^:parallel unique-name-generator-options-test- (t/testing "options"- (t/testing :name-key-fn- (let [f (mbql.u/unique-name-generator :name-key-fn #_{:clj-kondo/ignore [:discouraged-var]} str/lower-case)]- (t/is (= ["x" "X_2" "X_3"]- (map f ["x" "X" "X"])))))))--(t/deftest ^:parallel unique-name-generator-options-test-2- (t/testing "options"- (t/testing :unique-alias-fn- (let [f (mbql.u/unique-name-generator :unique-alias-fn (fn [x y] (str y "~~" x)))]- (t/is (= ["x" "2~~x"]- (map f ["x" "x"])))))))-;;; --------------------------------------------- query->max-rows-limit ----------------------------------------------#_{:clj-kondo/ignore [:deprecated-var]}@@ -756,20 +703,17 @@(t/deftest ^:parallel update-field-options-test(t/is (= [:field 1 {:wow true}](mbql.u/update-field-options [:field 1 nil] assoc :wow true)- (mbql.u/update-field-options [:field 1 {}] assoc :wow true)+ (mu/disable-enforcement+ (mbql.u/update-field-options [:field 1 {}] assoc :wow true))(mbql.u/update-field-options [:field 1 {:wow false}] assoc :wow true)))-(t/is (= [:field 1 {:a 1, :b 2}](mbql.u/update-field-options [:field 1 {:a 1}] assoc :b 2)))-(t/testing "Should remove empty options"(t/is (= [:field 1 nil](mbql.u/update-field-options [:field 1 {:a 1}] dissoc :a))))-(t/testing "Should normalize the clause"(t/is (= [:field 1 nil](mbql.u/update-field-options [:field 1 {:a {:b 1}}] assoc-in [:a :b] nil))))-(t/testing "Should work with `:expression` and `:aggregation` references as well"(t/is (= [:expression "wow" {:a 1}](mbql.u/update-field-options [:expression "wow"] assoc :a 1)))@@ -779,7 +723,6 @@(mbql.u/update-field-options [:aggregation 0] assoc :a 1)))(t/is (= [:aggregation 0 {:a 1, :b 2}](mbql.u/update-field-options [:aggregation 0 {:b 2}] assoc :a 1)))-;; in the future when we make the 3-arg version the normalized/"official" version we will probably want to stop;; doing this.(t/testing "Remove empty options entirely from `:expression` and `:aggregation` (for now)"@@ -881,3 +824,11 @@(t/testing "If this gets called incorrectly with a base type keyword then handle it gracefully"(t/is (= :type/Text(mbql.u/normalize-token "type/Text")))))++(t/deftest ^:parallel wrap-field-id-if-needed-test+ (doseq [[x expected] {10 [:field 10 nil]+ [:field 10 nil] [:field 10 nil]+ [:field "name" {:base-type :type/Text}] [:field "name" {:base-type :type/Text}]}]+ (t/testing x+ (t/is (= expected+ (mbql.u/wrap-field-id-if-needed x))))))diff --git a/test/metabase/lib/drill_thru/zoom_in_timeseries_test.cljc b/test/metabase/lib/drill_thru/zoom_in_timeseries_test.cljcindex 431e0712e3c..12973630b1a 100644--- a/test/metabase/lib/drill_thru/zoom_in_timeseries_test.cljc+++ b/test/metabase/lib/drill_thru/zoom_in_timeseries_test.cljc@@ -10,7 +10,8 @@[metabase.lib.metadata :as lib.metadata][metabase.lib.temporal-bucket :as lib.temporal-bucket][metabase.lib.test-metadata :as meta]- [metabase.lib.test-util.metadata-providers.merged-mock :as merged-mock]))+ [metabase.lib.test-util.metadata-providers.merged-mock :as merged-mock]+ [metabase.util.malli :as mu]))#?(:cljs (comment metabase.test-runner.assert-exprs.approximately-equal/keep-me))@@ -182,12 +183,15 @@;; and hence no zoom-in-timeseries drill thru should be returned.bucketing [:day :hour]](testing (str "aggregation = " aggregation ", bucketing = " bucketing)- (lib.drill-thru.tu/test-drill-not-returned- (zoom-in-timeseries-drill-for-orders-created-at- metadata-provider-with-orders-created-at-as-date- aggregation- bucketing- {"CREATED_AT" "2022-12-01"}))))))+ ;; it should not even be legacy to give this column `:hour` bucketing, so disable Malli enforcement to+ ;; simulate it having it anyway+ (mu/disable-enforcement+ (lib.drill-thru.tu/test-drill-not-returned+ (zoom-in-timeseries-drill-for-orders-created-at+ metadata-provider-with-orders-created-at-as-date+ aggregation+ bucketing+ {"CREATED_AT" "2022-12-01"})))))))(deftest ^:parallel returns-zoom-in-timeseries-for-multi-stage-query-test(lib.drill-thru.tu/test-returns-drilldiff --git a/test/metabase/lib/fe_util_test.cljc b/test/metabase/lib/fe_util_test.cljcindex 24710e61972..05f254b0ae5 100644--- a/test/metabase/lib/fe_util_test.cljc+++ b/test/metabase/lib/fe_util_test.cljc@@ -585,7 +585,8 @@(deftest ^:parallel specific-date-filter-parts-test(let [query (lib.tu/venues-query)- column (m/filter-vals some? (meta/field-metadata :checkins :date))]+ column (-> (m/filter-vals some? (meta/field-metadata :checkins :date))+ (assoc :base-type :type/DateTime :effective-type :type/DateTime))](testing "clause to parts roundtrip"(doseq [[clause parts] {(lib.filter/= column "2024-11-28"){:operator :=diff --git a/test/metabase/lib/field_test.cljc b/test/metabase/lib/field_test.cljcindex 27118ff8cad..33112b2c4cd 100644--- a/test/metabase/lib/field_test.cljc+++ b/test/metabase/lib/field_test.cljc@@ -4,6 +4,7 @@[clojure.test :refer [are deftest is testing]][medley.core :as m][metabase.lib.binning :as lib.binning]+ [metabase.lib.convert.metadata-to-legacy :as lib.convert.metadata-to-legacy][metabase.lib.core :as lib][metabase.lib.equality :as lib.equality][metabase.lib.field :as lib.field]@@ -1790,11 +1791,9 @@meta/metadata-provider(lib.tu.macros/mbql-query products{:joins [{:source-query q1- :source-metadata (for [col (lib/returned-columns- (lib/query meta/metadata-provider {:database (meta/id), :type :query, :query q1}))]- (-> col- (dissoc :lib/type)- (update-keys u/->snake_case_en)))+ :source-metadata (map lib.convert.metadata-to-legacy/lib-metadata-column->legacy-metadata-column+ (lib/returned-columns+ (lib/query meta/metadata-provider {:database (meta/id), :type :query, :query q1}))):alias "Question 54":condition [:= $id [:field %orders.id {:join-alias "Question 54"}]]:fields [[:field %orders.id {:join-alias "Question 54"}]diff --git a/test/metabase/lib/filter_test.cljc b/test/metabase/lib/filter_test.cljcindex cdc9e42797a..7b348a9b9cf 100644--- a/test/metabase/lib/filter_test.cljc+++ b/test/metabase/lib/filter_test.cljc@@ -3,7 +3,6 @@#?@(:cljs ([metabase.test-runner.assert-exprs.approximately-equal]))[clojure.test :refer [are deftest is testing]][medley.core :as m]- [metabase.lib.convert :as lib.convert][metabase.lib.core :as lib][metabase.lib.expression :as lib.expression][metabase.lib.field.util :as lib.field.util]@@ -13,8 +12,6 @@[metabase.lib.test-metadata :as meta][metabase.lib.test-util :as lib.tu][metabase.lib.test-util.matrix :as matrix]- [metabase.lib.util :as lib.util]- [metabase.util :as u][metabase.util.number :as u.number]))#?(:cljs (comment metabase.test-runner.assert-exprs.approximately-equal/keep-me))@@ -420,55 +417,6 @@(is (=? {:stages [{:filters [[:!= {} [:field {} (meta/id :users :id)] 515]]}]}query'))))))-(deftest ^:parallel find-filter-for-legacy-filter-test- (testing "existing clauses"- (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :users))- (lib/expression "expr" (lib/absolute-datetime "2020" :month)))- filterable-cols (lib/filterable-columns query)- [first-col] filterable-cols- expr-col (m/find-first #(= (:name %) "expr") (lib/filterable-columns query))- first-filter (lib/filter-clause- (first (lib/filterable-column-operators first-col))- first-col- 515)- query (-> query- (lib/filter first-filter)- (lib/filter (lib/filter-clause- (first (lib/filterable-column-operators expr-col))- expr-col- (meta/field-metadata :users :last-login))))- filter-clauses (lib/filters query)]- (testing "existing clauses"- (doseq [filter-clause filter-clauses- :let [legacy-clause (lib.convert/->legacy-MBQL filter-clause)]- legacy-clause [legacy-clause- (update legacy-clause 0 u/qualified-name)]]- (testing (pr-str legacy-clause)- (is (= filter-clause- (lib/find-filter-for-legacy-filter query legacy-clause))))))- (testing "missing clause"- (let [filter-clause (assoc first-filter 2 43)]- (is (nil? (lib/find-filter-for-legacy-filter query (lib.convert/->legacy-MBQL filter-clause))))))- (testing "ambiguous match"- ;; don't use lib/filter because it will ignore duplicate filters.- (let [query (lib.util/update-query-stage query -1 update :filters conj (lib.util/fresh-uuids first-filter))]- (is (thrown-with-msg?- #?(:clj Exception :cljs :default) #"Multiple matching filters found"- (lib/find-filter-for-legacy-filter query (lib.convert/->legacy-MBQL (first filter-clauses))))))))))--(deftest ^:parallel find-filterable-column-for-legacy-ref-test- (let [query (lib/filter (lib.tu/venues-query) (lib/= (meta/field-metadata :venues :name) "BBQ"))]- (are [legacy-ref] (=? {:name "NAME"- :id (meta/id :venues :name)- :operators seq}- (lib/find-filterable-column-for-legacy-ref query legacy-ref))- [:field (meta/id :venues :name) nil]- [:field (meta/id :venues :name) {}]- ["field" (meta/id :venues :name) nil]- #?@(:cljs- [#js ["field" (meta/id :venues :name) nil]- #js ["field" (meta/id :venues :name) #js {}]]))))-(def ^:private last-online-time(assoc (meta/field-metadata :people :birth-date):display-name "Last Online Time"diff --git a/test/metabase/lib/parameters_test.cljc b/test/metabase/lib/parameters_test.cljcnew file mode 100644index 00000000000..94a127a9a16--- /dev/null+++ b/test/metabase/lib/parameters_test.cljc@@ -0,0 +1,8 @@+(ns metabase.lib.parameters-test+ (:require+ [clojure.test :refer [deftest is]]+ [metabase.lib.core :as lib]))++(deftest ^:parallel parameter-target-field-id-test+ (is (= 256+ (lib/parameter-target-field-id [:dimension [:field 256 nil]]))))diff --git a/test/metabase/lib/query_test.cljc b/test/metabase/lib/query_test.cljcindex df7ddb08b73..a4f497b6005 100644--- a/test/metabase/lib/query_test.cljc+++ b/test/metabase/lib/query_test.cljc@@ -249,7 +249,7 @@(lib/breakout (lib/with-temporal-bucket (meta/field-metadata :people :birth-date) :year)))true :metric (-> (lib/query meta/metadata-provider (meta/table-metadata :people))(lib/aggregate (lib/count))- (lib/breakout (lib/with-temporal-bucket (meta/field-metadata :people :birth-date) :hour-of-day)))+ (lib/breakout (lib/with-temporal-bucket (meta/field-metadata :people :birth-date) :month-of-year)))false :metric (-> (lib/query meta/metadata-provider (meta/table-metadata :people))(lib/aggregate (lib/count))(lib/breakout (meta/field-metadata :people :created-at))@@ -285,7 +285,7 @@(lib/breakout (lib/with-temporal-bucket (meta/field-metadata :people :birth-date) :year)))true :metric (-> (lib/query meta/metadata-provider (meta/table-metadata :people))(lib/aggregate (lib/count))- (lib/breakout (lib/with-temporal-bucket (meta/field-metadata :people :birth-date) :hour-of-day)))+ (lib/breakout (lib/with-temporal-bucket (meta/field-metadata :people :birth-date) :month-of-year)))false :metric (-> (lib/query meta/metadata-provider (meta/table-metadata :people))(lib/aggregate (lib/count))(lib/breakout (meta/field-metadata :people :created-at))diff --git a/test/metabase/lib/schema/metadata/fingerprint_test.clj b/test/metabase/lib/schema/metadata/fingerprint_test.cljcsimilarity index 97%rename from test/metabase/lib/schema/metadata/fingerprint_test.cljrename to test/metabase/lib/schema/metadata/fingerprint_test.cljcindex 73217ecf264..2d194af8022 100644--- a/test/metabase/lib/schema/metadata/fingerprint_test.clj+++ b/test/metabase/lib/schema/metadata/fingerprint_test.cljc@@ -1,7 +1,7 @@(ns metabase.lib.schema.metadata.fingerprint-test"Basic tests to make sure the fingerprint generatation code is doing something that makes sense."(:require- [clojure.test :refer :all]+ [clojure.test :refer [deftest is testing]][malli.error :as me][metabase.lib.core :as lib][metabase.lib.schema.metadata.fingerprint :as lib.schema.metadata.fingerprint]diff --git a/test/metabase/lib/schema/ref_test.cljc b/test/metabase/lib/schema/ref_test.cljcindex 050ab0e35b1..537fed8abfc 100644--- a/test/metabase/lib/schema/ref_test.cljc+++ b/test/metabase/lib/schema/ref_test.cljc@@ -65,3 +65,7 @@"effective-type" "type/Float""lib/original-binning" {"strategy" "num-bins", "num-bins" 50}}"TOTAL_2"]))))++(deftest ^:parallel normalize-field-ref-remove-nil-values-test+ (is (= [:field {:lib/uuid "d01f4c83-0fe5-4329-80f3-2bbea1f27c3b"} 100]+ (lib/normalize ["field" {"temporal-unit" nil, "lib/uuid" "d01f4c83-0fe5-4329-80f3-2bbea1f27c3b"} 100]))))diff --git a/test/metabase/lib/util/unique_name_generator_test.cljc b/test/metabase/lib/util/unique_name_generator_test.cljcnew file mode 100644index 00000000000..4dcfb4416ae--- /dev/null+++ b/test/metabase/lib/util/unique_name_generator_test.cljc@@ -0,0 +1,151 @@+(ns metabase.lib.util.unique-name-generator-test+ (:require+ [clojure.string :as str]+ [clojure.test :refer [are deftest is testing]]+ [metabase.lib.util.unique-name-generator :as lib.util.unique-name-generator]+ [metabase.util :as u]))++(deftest ^:parallel crc32-checksum-test+ (are [s checksum] (= checksum+ (#'lib.util.unique-name-generator/crc32-checksum s))+ "YMRZFRTHUBOUZHPTZGPD" "2694651f"+ "MEBRXTJEPWOJJXVZIPDA" "048132cb"+ "UIOJOTPGUIROVRJYAFPO" "0085cacb"+ "UCVEWTGNBDANGMZPGNQC" "000e32a0"+ "ZAFVKSVXQKJNGANBQZMX" "0000d5b8"+ "NCTFDMQNUEQLJUMAGSYG" "000000ea"+ "YHQJXDIXGGQTSARGOQZZ" "000000c1"+ "0601246074" "00000001"+ "2915035893" "00000000"))++(deftest ^:parallel truncate-alias-test+ (letfn [(truncate-alias [s max-bytes]+ (let [truncated (lib.util.unique-name-generator/truncate-alias s max-bytes)]+ (is (<= (u/string-byte-count truncated) max-bytes))+ truncated))]+ (doseq [[s max-bytes->expected] {;; 20-character plain ASCII string+ "01234567890123456789"+ {12 "012_fc89bad5"+ 15 "012345_fc89bad5"+ 20 "01234567890123456789"}++ ;; two strings that only differ after the point they get truncated+ "0123456789abcde" {12 "012_1629bb92"}+ "0123456789abcdE" {12 "012_2d479b5a"}++ ;; Unicode string: 14 characters, 42 bytes+ "가나다라마바사아자차카타파하"+ {12 "가_b9c95392"+ 13 "가_b9c95392"+ 14 "가_b9c95392"+ 15 "가나_b9c95392"+ 20 "가나다_b9c95392"+ 30 "가나다라마바사_b9c95392"+ 40 "가나다라마바사아자차_b9c95392"+ 50 "가나다라마바사아자차카타파하"}++ ;; Mixed string: 17 characters, 33 bytes+ "a가b나c다d라e마f바g사h아i"+ {12 "a_99a0fe0c"+ 13 "a가_99a0fe0c"+ 14 "a가b_99a0fe0c"+ 15 "a가b_99a0fe0c"+ 20 "a가b나c_99a0fe0c"+ 30 "a가b나c다d라e마f_99a0fe0c"+ 40 "a가b나c다d라e마f바g사h아i"}}+ [max-bytes expected] max-bytes->expected]+ (testing (pr-str (list `lib.util.unique-name-generator/truncate-alias s max-bytes))+ (is (= expected+ (truncate-alias s max-bytes)))))))++(deftest ^:parallel unique-name-generator-test+ (let [unique-name-fn (lib.util.unique-name-generator/unique-name-generator)]+ (is (= "wow"+ (unique-name-fn "wow")))+ (is (= "wow_2"+ (unique-name-fn "wow")))+ (testing "should be case-insensitive distinct"+ (is (= "WOW_3"+ (unique-name-fn "WOW"))))+ (testing "should truncate long names"+ (is (= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY_2dc86ef1"+ (unique-name-fn "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")))+ (is (= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY_fc11882d"+ (unique-name-fn "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))))))++(deftest ^:parallel unique-name-generator-idempotence-test+ (testing "idempotence (2-arity calls to generated function)"+ (let [unique-name (lib.util.unique-name-generator/unique-name-generator)]+ (is (= ["A" "B" "A" "A_2" "A_2"]+ [(unique-name :x "A")+ (unique-name :x "B")+ (unique-name :x "A")+ (unique-name :y "A")+ (unique-name :y "A")])))))++(deftest ^:parallel unique-name-generator-zero-arity-test+ (let [f (lib.util.unique-name-generator/unique-name-generator)]+ (is (= ["A" "B" "A" "A_2" "A_2"]+ [(f :x "A")+ (f :x "B")+ (f :x "A")+ (f :y "A")+ (f :y "A")]))+ (let [f' (f)]+ (is (= ["A" "B" "A" "A_2" "A_2"]+ [(f' :x "A")+ (f' :x "B")+ (f' :x "A")+ (f' :y "A")+ (f' :y "A")]))+ (let [f'' (f')]+ (is (= ["A" "B" "A" "A_2" "A_2"]+ [(f'' :x "A")+ (f'' :x "B")+ (f'' :x "A")+ (f'' :y "A")+ (f'' :y "A")]))))))++(deftest ^:parallel non-truncating-unique-name-generator-test+ (let [f (lib.util.unique-name-generator/non-truncating-unique-name-generator)]+ (is (= "Total_number_of_people_from_each_state_separated_by_state_and_then_we_do_a_count"+ (f "Total_number_of_people_from_each_state_separated_by_state_and_then_we_do_a_count")))))++(deftest ^:parallel unique-name-generator-with-options-test+ (testing "Can we get a simple unique name generator"+ (is (= ["count" "sum" "count_2" "count_2_2"]+ (map (lib.util.unique-name-generator/unique-name-generator-with-options) ["count" "sum" "count" "count_2"])))))++(deftest ^:parallel unique-name-generator-with-options-test-2+ (testing "Can we get an idempotent unique name generator"+ (is (= ["count" "sum" "count" "count_2"]+ (map (lib.util.unique-name-generator/unique-name-generator-with-options) [:x :y :x :z] ["count" "sum" "count" "count_2"])))))++(deftest ^:parallel unique-name-generator-with-options-test-3+ (testing "Can the same object have multiple aliases"+ (is (= ["count" "sum" "count" "count_2"]+ (map (lib.util.unique-name-generator/unique-name-generator-with-options) [:x :y :x :x] ["count" "sum" "count" "count_2"])))))++(deftest ^:parallel unique-name-generator-with-options-idempotence-test+ (testing "idempotence (2-arity calls to generated function) (#40994)"+ (let [unique-name (lib.util.unique-name-generator/unique-name-generator-with-options)]+ (is (= ["A" "B" "A" "A_2" "A_2"]+ [(unique-name :x "A")+ (unique-name :x "B")+ (unique-name :x "A")+ (unique-name :y "A")+ (unique-name :y "A")])))))++(deftest ^:parallel unique-name-generator-options-test+ (testing "options"+ (testing :name-key-fn+ (let [f (lib.util.unique-name-generator/unique-name-generator-with-options :name-key-fn #_{:clj-kondo/ignore [:discouraged-var]} str/lower-case)]+ (is (= ["x" "X_2" "X_3"]+ (map f ["x" "X" "X"])))))))++(deftest ^:parallel unique-name-generator-options-test-2+ (testing "options"+ (testing :unique-alias-fn+ (let [f (lib.util.unique-name-generator/unique-name-generator-with-options :unique-alias-fn (fn [x y] (str y "~~" x)))]+ (is (= ["x" "2~~x"]+ (map f ["x" "x"])))))))diff --git a/test/metabase/lib/util_test.cljc b/test/metabase/lib/util_test.cljcindex cf61c9da6d6..bcc373f44a0 100644--- a/test/metabase/lib/util_test.cljc+++ b/test/metabase/lib/util_test.cljc@@ -3,11 +3,9 @@#?@(:cljs ([metabase.test-runner.assert-exprs.approximately-equal]))[clojure.test :refer [are deftest is testing]][metabase.lib.core :as lib]- [metabase.lib.equality :as lib.equality][metabase.lib.test-metadata :as meta][metabase.lib.test-util :as lib.tu]- [metabase.lib.util :as lib.util]- [metabase.util :as u]))+ [metabase.lib.util :as lib.util]))#?(:cljs(comment metabase.test-runner.assert-exprs.approximately-equal/keep-me))@@ -234,112 +232,6 @@["a" "b" "c"] "a, b, and c"["a" "b" "c" "d"] "a, b, c, and d"))-(deftest ^:parallel crc32-checksum-test- (are [s checksum] (= checksum- (#'lib.util/crc32-checksum s))- "YMRZFRTHUBOUZHPTZGPD" "2694651f"- "MEBRXTJEPWOJJXVZIPDA" "048132cb"- "UIOJOTPGUIROVRJYAFPO" "0085cacb"- "UCVEWTGNBDANGMZPGNQC" "000e32a0"- "ZAFVKSVXQKJNGANBQZMX" "0000d5b8"- "NCTFDMQNUEQLJUMAGSYG" "000000ea"- "YHQJXDIXGGQTSARGOQZZ" "000000c1"- "0601246074" "00000001"- "2915035893" "00000000"))--(deftest ^:parallel truncate-alias-test- (letfn [(truncate-alias [s max-bytes]- (let [truncated (lib.util/truncate-alias s max-bytes)]- (is (<= (u/string-byte-count truncated) max-bytes))- truncated))]- (doseq [[s max-bytes->expected] {;; 20-character plain ASCII string- "01234567890123456789"- {12 "012_fc89bad5"- 15 "012345_fc89bad5"- 20 "01234567890123456789"}-- ;; two strings that only differ after the point they get truncated- "0123456789abcde" {12 "012_1629bb92"}- "0123456789abcdE" {12 "012_2d479b5a"}-- ;; Unicode string: 14 characters, 42 bytes- "가나다라마바사아자차카타파하"- {12 "가_b9c95392"- 13 "가_b9c95392"- 14 "가_b9c95392"- 15 "가나_b9c95392"- 20 "가나다_b9c95392"- 30 "가나다라마바사_b9c95392"- 40 "가나다라마바사아자차_b9c95392"- 50 "가나다라마바사아자차카타파하"}-- ;; Mixed string: 17 characters, 33 bytes- "a가b나c다d라e마f바g사h아i"- {12 "a_99a0fe0c"- 13 "a가_99a0fe0c"- 14 "a가b_99a0fe0c"- 15 "a가b_99a0fe0c"- 20 "a가b나c_99a0fe0c"- 30 "a가b나c다d라e마f_99a0fe0c"- 40 "a가b나c다d라e마f바g사h아i"}}- [max-bytes expected] max-bytes->expected]- (testing (pr-str (list `lib.util/truncate-alias s max-bytes))- (is (= expected- (truncate-alias s max-bytes)))))))--(deftest ^:parallel unique-name-generator-test- (let [unique-name-fn (lib.util/unique-name-generator)]- (is (= "wow"- (unique-name-fn "wow")))- (is (= "wow_2"- (unique-name-fn "wow")))- (testing "should be case-insensitive distinct"- (is (= "WOW_3"- (unique-name-fn "WOW"))))- (testing "should truncate long names"- (is (= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY_2dc86ef1"- (unique-name-fn "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")))- (is (= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY_fc11882d"- (unique-name-fn "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))))))--(deftest ^:parallel unique-name-generator-idempotence-test- (testing "idempotence (2-arity calls to generated function)"- (let [unique-name (lib.util/unique-name-generator)]- (is (= ["A" "B" "A" "A_2" "A_2"]- [(unique-name :x "A")- (unique-name :x "B")- (unique-name :x "A")- (unique-name :y "A")- (unique-name :y "A")])))))--(deftest ^:parallel unique-name-generator-zero-arity-test- (let [f (lib.util/unique-name-generator)]- (is (= ["A" "B" "A" "A_2" "A_2"]- [(f :x "A")- (f :x "B")- (f :x "A")- (f :y "A")- (f :y "A")]))- (let [f' (f)]- (is (= ["A" "B" "A" "A_2" "A_2"]- [(f' :x "A")- (f' :x "B")- (f' :x "A")- (f' :y "A")- (f' :y "A")]))- (let [f'' (f')]- (is (= ["A" "B" "A" "A_2" "A_2"]- [(f'' :x "A")- (f'' :x "B")- (f'' :x "A")- (f'' :y "A")- (f'' :y "A")]))))))--(deftest ^:parallel non-truncating-unique-name-generator-test- (let [f (lib.util/non-truncating-unique-name-generator)]- (is (= "Total_number_of_people_from_each_state_separated_by_state_and_then_we_do_a_count"- (f "Total_number_of_people_from_each_state_separated_by_state_and_then_we_do_a_count")))))-(deftest ^:parallel strip-id-test(are [exp in] (= exp (lib.util/strip-id in))"foo" "foo"@@ -386,33 +278,6 @@[:field {:lib/uuid "36a2abff-e4ae-4752-b232-4885e08f52ea"} 5]"abc"]))))-(deftest ^:parallel fresh-query-instance-test- (let [query {:lib/type :mbql/query,- :stages- [{:lib/type :mbql.stage/mbql,- :breakout- [[:field {:base-type :type/DateTime, :temporal-unit :month,- :lib/uuid "7ec788fb-3eb2-4ed0-88fa-5f6b53a09094"}- 38]- [:field {:base-type :type/Text, :source-field 37,- :lib/uuid "65135c9c-fec5-4f51-b111-fadbb6af4522"}- 64]],- :aggregation [[:metric {:lib/uuid "aa83c834-9a7c-4d7b-b408-3e17668d5ecc"} 84]],- :order-by- [[:asc- #:lib{:uuid "2712fc42-d13e-4810-9ae9-a126d536376e"}- [:aggregation {:lib/uuid "a1d73928-05db-4cb7-bb05-5123d2dbc261"}- "aa83c834-9a7c-4d7b-b408-3e17668d5ecc"]]],- :source-card 84}],- :database 1}- fresh-query (lib.util/fresh-query-instance query)- aggregation-ref-path [:stages 0 :order-by 0 2 2]]- (is (= (get-in fresh-query [:stages 0 :aggregation 0 1 :lib/uuid])- (get-in fresh-query aggregation-ref-path)))- (is (not= (get-in query aggregation-ref-path)- (get-in fresh-query aggregation-ref-path)))- (is (lib.equality/= query fresh-query))))-(def ^:private single-stage-query(lib.tu/venues-query))diff --git a/test/metabase/parameters/custom_values_test.clj b/test/metabase/parameters/custom_values_test.cljindex 86aced8c1ba..4dfec47d4f5 100644--- a/test/metabase/parameters/custom_values_test.clj+++ b/test/metabase/parameters/custom_values_test.clj@@ -26,13 +26,13 @@:values [["20th Century Cafe"] ["25°"] ["33 Taps"]]}(custom-values/values-from-cardcard- (mt/$ids $venues.name)))))+ [:field {:lib/uuid "00000000-0000-0000-0000-000000000000"} (mt/id :venues :name)]))))(testing "case in-sensitve search test"(is (= {:has_more_values false:values [["Liguria Bakery"] ["Noe Valley Bakery"]]}(custom-values/values-from-cardcard- (mt/$ids $venues.name)+ [:field {:lib/uuid "00000000-0000-0000-0000-000000000000"} (mt/id :venues :name)]{:query-string "bakery"}))))))))))(deftest ^:parallel with-mbql-card-test-2@@ -52,28 +52,29 @@:values [["American"] ["Artisan"] ["Asian"]]}(custom-values/values-from-cardcard- [:field "NAME" {:base-type :type/Text}]))))+ [:field {:base-type :type/Text, :lib/uuid "00000000-0000-0000-0000-000000000000"} "NAME"]))))(testing "get values from aggregation column"(is (= {:has_more_values true:values [[1] [2] [3]]}(custom-values/values-from-cardcard- [:field "sum" {:base-type :type/Float}]))))+ [:field {:base-type :type/Float, :lib/uuid "00000000-0000-0000-0000-000000000000"} "sum"]))))(testing "can search on aggregation column"(is (= {:has_more_values false:values [[2]]}(custom-values/values-from-cardcard- [:field "sum" {:base-type :type/Float}]+ [:field {:base-type :type/Float, :lib/uuid "00000000-0000-0000-0000-000000000000"} "sum"]{:query-string 2}))))(testing "doing case in-sensitve search on breakout columns"(is (= {:has_more_values false:values [["Bakery"]]}(custom-values/values-from-cardcard- [:field "NAME" {:base-type :type/Text}]- {:query-string "bakery"}))))))))+ [:field {:base-type :type/Text, :lib/uuid "00000000-0000-0000-0000-000000000000"} "NAME"]+ {:query-string "bakery"})))))))))+(deftest ^:parallel with-mbql-card-test-2b(testing "source card is a question" ; Questions are transparent, so this can drop the aggregations and filter the original.(binding [custom-values/*max-rows* 3](testing "has aggregation column"@@ -90,13 +91,13 @@:values [["American"] ["Artisan"] ["Asian"]]}(custom-values/values-from-cardcard- [:field (mt/id :categories :name) {:source-field (mt/id :venues :category_id)}]))))+ [:field {:source-field (mt/id :venues :category_id), :lib/uuid "00000000-0000-0000-0000-000000000000"} (mt/id :categories :name)]))))(testing "doing case in-sensitve search on breakout columns"(is (= {:has_more_values false:values [["Bakery"]]}(custom-values/values-from-cardcard- [:field (mt/id :categories :name) {:source-field (mt/id :venues :category_id)}]+ [:field {:source-field (mt/id :venues :category_id), :lib/uuid "00000000-0000-0000-0000-000000000000"} (mt/id :categories :name)]{:query-string "bakery"})))))))))(deftest ^:parallel with-mbql-card-test-3@@ -117,13 +118,13 @@:values [[2] [3] [4]]}(custom-values/values-from-cardcard- (mt/$ids $venues.category_id)))))+ [:field {:lib/uuid "00000000-0000-0000-0000-000000000000"} (mt/id :venues :category_id)]))))(testing "search with the value, not remapped values"(is (= {:has_more_values false:values [[2]]}(custom-values/values-from-cardcard- (mt/$ids $venues.category_id)+ [:field {:lib/uuid "00000000-0000-0000-0000-000000000000"} (mt/id :venues :category_id)]{:query-string 2})))))))))))(deftest ^:parallel with-mbql-card-test-4-explicit-fields@@ -139,7 +140,7 @@:values [["Chez Jay"] ["Marlowe"] ["Musso & Frank Grill"]]}(custom-values/values-from-cardcard- (mt/$ids $venues.name)))))))))+ [:field {:lib/uuid "00000000-0000-0000-0000-000000000000"} (mt/id :venues :name)]))))))))(deftest ^:parallel with-mbql-card-test-5-explicit-fields-in-join(testing "source card with explicit :fields on a join, and no aggregations or breakouts"@@ -158,7 +159,7 @@:values [["Chez Jay"] ["Marlowe"] ["Musso & Frank Grill"]]}(custom-values/values-from-cardcard- (mt/$ids $venues.name)))))))))+ [:field {:lib/uuid "00000000-0000-0000-0000-000000000000"} (mt/id :venues :name)]))))))))(deftest ^:parallel with-filter-stage-test(binding [custom-values/*max-rows* 3]@@ -176,7 +177,7 @@:has_more_values true}(custom-values/values-from-cardcard- [:field "Categories__NAME" {:base-type :type/Text}]+ [:field {:base-type :type/Text, :lib/uuid "00000000-0000-0000-0000-000000000000"} "Categories__NAME"]{:stage-number 1}))))))))(deftest ^:parallel with-mbql-card-test-6-expressions@@ -191,7 +192,7 @@:has_more_values true}(custom-values/values-from-cardcard- [:expression "unit price" {:base_type :type/Float}]+ [:expression {:lib/uuid "00000000-0000-0000-0000-000000000000", :base-type :type/Float} "unit price"]{:stage-number 0})))))))(deftest ^:parallel with-native-card-test@@ -209,13 +210,13 @@:values [["Fred 62"] ["Red Medicine"]]}(custom-values/values-from-cardcard- [:field "NAME" {:base-type :type/Text}]))))+ [:field {:base-type :type/Text, :lib/uuid "00000000-0000-0000-0000-000000000000"} "NAME"]))))(testing "doing case in-sensitve search on breakout columns"(is (= {:has_more_values false:values [["Red Medicine"]]}(custom-values/values-from-cardcard- [:field "NAME" {:base-type :type/Text}]+ [:field {:base-type :type/Text, :lib/uuid "00000000-0000-0000-0000-000000000000"} "NAME"]{:query-string "medicine"})))))))))(deftest ^:parallel deduplicate-and-remove-non-empty-values-empty@@ -229,13 +230,13 @@:values [["Affiliate"] ["Facebook"] ["Google"] ["Organic"] ["Twitter"]]}(custom-values/values-from-cardcard- [:field "SOURCE" {:base-type :type/Text}]))))+ [:field {:base-type :type/Text, :lib/uuid "00000000-0000-0000-0000-000000000000"} "SOURCE"]))))(testing "doing case in-sensitve search on breakout columns"(is (= {:has_more_values false:values [["Facebook"] ["Google"]]}(custom-values/values-from-cardcard- [:field "SOURCE" {:base-type :type/Text}]+ [:field {:base-type :type/Text, :lib/uuid "00000000-0000-0000-0000-000000000000"} "SOURCE"]{:query-string "oo"})))))))))(deftest ^:parallel deduplicate-and-remove-non-empty-values-empty-2@@ -249,13 +250,13 @@:values [["Affiliate"] ["Facebook"] ["Google"] ["Organic"] ["Twitter"]]}(custom-values/values-from-cardcard- (mt/$ids $people.source)))))+ [:field {:lib/uuid "00000000-0000-0000-0000-000000000000"} (mt/id :people :source)]))))(testing "doing case in-sensitve search on breakout columns"(is (= {:has_more_values false:values [["Facebook"] ["Google"]]}(custom-values/values-from-cardcard- (mt/$ids $people.source)+ [:field {:lib/uuid "00000000-0000-0000-0000-000000000000"} (mt/id :people :source)]{:query-string "oo"})))))))))(deftest errors-test@@ -337,7 +338,7 @@:values [["Doohickey"] ["Gadget"] ["Gizmo"] ["Widget"]]}(custom-values/values-from-cardcard- (mt/$ids $products.category)))))))))+ [:field {:lib/uuid "00000000-0000-0000-0000-000000000000"} (mt/id :products :category)]))))))))(deftest pk-of-fk-pk-field-ids-test(testing "single group"diff --git a/test/metabase/parameters/params_test.clj b/test/metabase/parameters/params_test.cljindex 068d23a65a4..a529dba99e0 100644--- a/test/metabase/parameters/params_test.clj+++ b/test/metabase/parameters/params_test.clj@@ -2,23 +2,12 @@"Tests for the utility functions for dealing with parameters in `metabase.parameters.params`."(:require[clojure.test :refer :all]- [metabase.legacy-mbql.util :as mbql.u][metabase.parameters.params :as params][metabase.public-sharing.api-test :as public-test][metabase.test :as mt][metabase.util :as u][toucan2.core :as t2]))-(deftest ^:parallel wrap-field-id-if-needed-test- (doseq [[x expected] {10 [:field 10 nil]- [:field 10 nil] [:field 10 nil]- [:field "name" {:base-type :type/Text}] [:field "name" {:base-type :type/Text}]}]- (testing x- (is (= expected- (mbql.u/wrap-field-id-if-needed x))))))--;;; ---------------------------------------------- name_field hydration -----------------------------------------------(deftest ^:parallel hydrate-name-field-test(testing "make sure that we can hydrate the `name_field` property for PK Fields"(is (= {:name "ID"diff --git a/test/metabase/parameters/schema_test.cljc b/test/metabase/parameters/schema_test.cljcindex 7aea7c47543..27c0e7ffe5c 100644--- a/test/metabase/parameters/schema_test.cljc+++ b/test/metabase/parameters/schema_test.cljc@@ -17,3 +17,7 @@(is (= {:parameter_id "53bb6214", :target [:variable [:template-tag "id"]]}(parameters.schema/normalize-parameter-mapping{"parameter_id" "53bb6214", "card_id" -1, "target" ["variable" ["template-tag" "id"]]})))))++(deftest ^:paralell normalize-parameters-without-adding-default-type-test+ (is (= [{:id "x", :target [:dimension [:template-tag "y"]]}]+ (parameters.schema/normalize-parameters-without-adding-default-types [{"id" "x", "target" ["dimension" ["template-tag" "y"]]}]))))diff --git a/test/metabase/public_sharing/api_test.clj b/test/metabase/public_sharing/api_test.cljindex 7eb0f7e8ac0..e1cbcf45015 100644--- a/test/metabase/public_sharing/api_test.clj+++ b/test/metabase/public_sharing/api_test.clj@@ -398,7 +398,12 @@:value 2}]}}(client/client :get 202 (str "public/card/" uuid "/query"):parameters (json/encode [{:id "_VENUE_ID_"- :value 2}]))))))+ :type :number/=+ :value 2}])))))))))++(deftest execute-public-card-with-parameters-test-2+ (testing "JSON-encoded MBQL parameters passed as a query parameter should work (#17019)"+ (mt/with-temporary-setting-values [enable-public-sharing true];; see longer explanation in [[metabase.legacy-mbql.schema/parameter-types]](testing "If the FE client is incorrectly passing in the parameter as a `:category` type, allow it for now"(with-temp-public-card [{uuid :public_uuid} {:dataset_query {:database (mt/id)@@ -490,7 +495,8 @@(is (= [[22]](mt/rows(client/client :get 202 (str "public/card/" uuid "/query")- :parameters (json/encode [{:type :number+ :parameters (json/encode [{:id "_PRICE_"+ :type :number:target [:variable [:template-tag "price"]]:value 1}])))))))diff --git a/test/metabase/queries/api/card_test.clj b/test/metabase/queries/api/card_test.cljindex 0627414d49e..4348606babc 100644--- a/test/metabase/queries/api/card_test.clj+++ b/test/metabase/queries/api/card_test.clj@@ -194,7 +194,8 @@:data {:rows [[8]]}}(mt/user-http-request:rasta :post 202 (format "card/%d/query" card-id)- {:parameters [{:type :number+ {:parameters [{:id "_CATEGORY_"+ :type :number:target [:variable [:template-tag :category]]:value 2}]})))))))@@ -2045,8 +2046,9 @@;;; +----------------------------------------------------------------------------------------------------------------+;;; Test GET /api/card/:id/query/csv & GET /api/card/:id/json & GET /api/card/:id/query/xlsx **WITH PARAMETERS**-(def ^:private ^String test-params- [{:type :number+(def ^:private test-params+ [{:id "_CATEGORY_"+ :type :number:target [:variable [:template-tag :category]]:value 2}])@@ -3006,7 +3008,7 @@(mt/user-http-request :crowberto :put 200(str "card/" (u/the-id card)) card))](doseq [[query-type query modified-query] [["mbql" query modified-query]- #_["native" (to-native query) (to-native modified-query)]]]+ ["native" (to-native query) (to-native modified-query)]]](testing (str "For: " query-type)(mt/with-model-cleanup [:model/Card](let [{metadata :result_metadatadiff --git a/test/metabase/queries/models/card_test.clj b/test/metabase/queries/models/card_test.cljindex f43e5252a24..2768f4bd7df 100644--- a/test/metabase/queries/models/card_test.clj+++ b/test/metabase/queries/models/card_test.clj@@ -212,8 +212,8 @@(let [metadata (qp.preprocess/query->expected-cols (mt/mbql-query venues))](mt/with-temp [:model/Card {card-id :id} {:dataset_query (mt/mbql-query venues):result_metadata metadata}]- (is (= (mt/derecordize metadata)- (mt/derecordize (t2/select-one-fn :result_metadata :model/Card :id card-id))))))))+ (is (= metadata+ (t2/select-one-fn :result_metadata :model/Card :id card-id)))))))(deftest populate-result-metadata-if-needed-test(doseq [[creating-or-updating f]diff --git a/test/metabase/query_processor/api_test.clj b/test/metabase/query_processor/api_test.cljindex 79b92456294..302e73d7435 100644--- a/test/metabase/query_processor/api_test.clj+++ b/test/metabase/query_processor/api_test.clj@@ -18,7 +18,7 @@[metabase.lib.metadata :as lib.metadata][metabase.lib.schema.id :as lib.schema.id][metabase.lib.test-util :as lib.tu]- [metabase.lib.util :as lib.util]+ [metabase.lib.util.unique-name-generator][metabase.permissions.core :as perms][metabase.query-processor :as qp][metabase.query-processor.api :as api.dataset]@@ -34,6 +34,7 @@[metabase.test.http-client :as client][metabase.util :as u][metabase.util.json :as json]+ [metabase.util.malli :as mu][metabase.util.malli.schema :as ms][toucan2.core :as t2]))@@ -234,33 +235,34 @@(deftest native-query-with-long-column-alias(testing "nested native query with long column alias (#47584)"- (let [short-col-name "coun"- long-col-name "Total_number_of_people_from_each_state_separated_by_state_and_then_we_do_a_count"-+ (let [short-col-name "coun"+ long-col-name "Total_number_of_people_from_each_state_separated_by_state_and_then_we_do_a_count";; Lightly validate the native form that comes back. Resist the urge to check for exact equality.validate-native-form (fn [native-form-lines](and (some #(str/includes? % short-col-name) native-form-lines)(some #(str/includes? % long-col-name) native-form-lines)))-;; Disable truncate-alias when compiling the native query to ensure we don't truncate the column.;; We want to simulate a user-defined query where the column name is long, but valid for the driver.- native-sub-query (with-redefs [lib.util/truncate-alias- (fn mock-truncate-alias- [ss & _] ss)]- (-> (mt/mbql-query people- {:source-table $$people- :aggregation [[:aggregation-options [:count] {:name short-col-name}]]- :breakout [[:field %state {:name long-col-name}]]- :limit 5})- qp.compile/compile- :query))- native-query (mt/native-query {:query native-sub-query})-+ native-sub-query (with-redefs [metabase.lib.util.unique-name-generator/truncate-alias+ (fn mock-truncate-alias+ [ss & _] ss)]+ ;; make sure the schema checks don't fail for aliases > 60 characters+ (mu/disable-enforcement+ (-> (mt/mbql-query people+ {:source-table $$people+ :aggregation [[:aggregation-options [:count] {:name short-col-name}]]+ :breakout [[:field %state {:name long-col-name}]]+ :limit 5})+ qp.compile/compile+ :query)))+ _ (is (not (str/includes? native-sub-query "_00028d48"))+ "double-check that the native-sub-query was not truncated")+ native-query (mt/native-query {:query native-sub-query});; Let metadata-provider-with-cards-with-metadata-for-queries calculate the result-metadata.- metadata-provider (qp.test-util/metadata-provider-with-cards-with-metadata-for-queries [native-query])- metadata-card (lib.metadata/card metadata-provider 1)]+ metadata-provider (qp.test-util/metadata-provider-with-cards-with-metadata-for-queries [native-query])+ metadata-card (lib.metadata/card metadata-provider 1)](mt/with-temp- [:model/Card card {:dataset_query native-query+ [:model/Card card {:dataset_query native-query:entity_id (:entity-id metadata-card):result_metadata (:result-metadata metadata-card)}](let [card-query {:database (mt/id)diff --git a/test/metabase/query_processor/card_test.clj b/test/metabase/query_processor/card_test.cljindex 62a60d55d2a..109d6a11a60 100644--- a/test/metabase/query_processor/card_test.clj+++ b/test/metabase/query_processor/card_test.clj@@ -152,7 +152,7 @@(testing disallowed-type(is (thrown-with-msg?clojure.lang.ExceptionInfo- #"Invalid parameter type :[^\s]+ for parameter \"date\".*/"+ #"Invalid parameter value type :[^\s]+ for parameter \"date\".*/"(validate disallowed-type)))(testing "should be ignored if `*allow-arbitrary-mbql-parameters*` is enabled"(binding [qp.card/*allow-arbitrary-mbql-parameters* true]diff --git a/test/metabase/query_processor/dashboard_test.clj b/test/metabase/query_processor/dashboard_test.cljindex 615269a4bc9..9211fa9af00 100644--- a/test/metabase/query_processor/dashboard_test.clj+++ b/test/metabase/query_processor/dashboard_test.clj@@ -23,7 +23,7 @@(qp/process-query (assoc query :info info))))options)))-(deftest resolve-parameters-validation-test+(deftest ^:parallel resolve-parameters-validation-test(api.dashboard-test/with-chain-filter-fixtures [{{dashboard-id :id} :dashboard{card-id :id} :card{dashcard-id :id} :dashcard}]@@ -43,8 +43,10 @@(testing "Should error if parameter is of a different type"(is (thrown-with-msg?clojure.lang.ExceptionInfo- #"Invalid parameter type :number/!= for parameter \"_PRICE_\".*"- (resolve-params [{:id "_PRICE_", :value 4, :type :number/!=}]))))))+ #"Invalid parameter value type :number/!= for parameter \"_PRICE_\".*"+ (resolve-params [{:id "_PRICE_", :value 4, :type :number/!=}])))))))++(deftest ^:parallel resolve-parameters-validation-test-2(testing "Resolves new operator type arguments without error (#25031)"(mt/dataset test-data(let [query (mt/native-query {:query "select COUNT(*) from \"ORDERS\" where true [[AND quantity={{qty_locked}}]]"diff --git a/test/metabase/query_processor/middleware/optimize_temporal_filters_test.clj b/test/metabase/query_processor/middleware/optimize_temporal_filters_test.cljindex 63303b93c38..23f37507f78 100644--- a/test/metabase/query_processor/middleware/optimize_temporal_filters_test.clj+++ b/test/metabase/query_processor/middleware/optimize_temporal_filters_test.clj@@ -4,7 +4,6 @@[clojure.test :refer :all][java-time.api :as t][metabase.driver :as driver]- [metabase.legacy-mbql.util :as mbql.u][metabase.lib.core :as lib][metabase.lib.test-metadata :as meta][metabase.lib.test-util :as lib.tu]@@ -116,6 +115,11 @@:lower (u.date/parse "2019-01-01" "UTC"):upper (u.date/parse "2020-01-01" "UTC")}])+;;; TODO (Cam 10/10/25) -- rework these tests to use MBQL 5 so we can use lib tools for this stuff instead of icky+;;; manual legacy MBQL manipulation+(defn- assoc-field-options [legacy-ref & kvs]+ (apply update (vec legacy-ref) 2 assoc kvs))+(deftest ^:parallel optimize-temporal-filters-test(doseq [field-or-expr [[:field (meta/id :orders :created-at) {}][:expression "date" {}]]@@ -129,51 +133,51 @@(pr-str upper))(testing :=(is (= [:and- [:>= (mbql.u/assoc-field-options field-or-expr :temporal-unit :default) lower]- [:< (mbql.u/assoc-field-options field-or-expr :temporal-unit :default) upper]]+ [:>= (assoc-field-options field-or-expr :temporal-unit :default) lower]+ [:< (assoc-field-options field-or-expr :temporal-unit :default) upper]](optimize-filters[:=- (mbql.u/assoc-field-options field-or-expr :temporal-unit unit)+ (assoc-field-options field-or-expr :temporal-unit unit)[:absolute-datetime filter-value unit]]))))(testing :!=(is (= [:or- [:< (mbql.u/assoc-field-options field-or-expr :temporal-unit :default) lower]- [:>= (mbql.u/assoc-field-options field-or-expr :temporal-unit :default) upper]]+ [:< (assoc-field-options field-or-expr :temporal-unit :default) lower]+ [:>= (assoc-field-options field-or-expr :temporal-unit :default) upper]](optimize-filters[:!=- (mbql.u/assoc-field-options field-or-expr :temporal-unit unit)+ (assoc-field-options field-or-expr :temporal-unit unit)[:absolute-datetime filter-value unit]]))))- (testing :<- (is (= [:< (mbql.u/assoc-field-options field-or-expr :temporal-unit :default) lower]+ (testing :<n+ (is (= [:< (assoc-field-options field-or-expr :temporal-unit :default) lower](optimize-filters[:<- (mbql.u/assoc-field-options field-or-expr :temporal-unit unit)+ (assoc-field-options field-or-expr :temporal-unit unit)[:absolute-datetime filter-value unit]]))))(testing :<=- (is (= [:< (mbql.u/assoc-field-options field-or-expr :temporal-unit :default) upper]+ (is (= [:< (assoc-field-options field-or-expr :temporal-unit :default) upper](optimize-filters[:<=- (mbql.u/assoc-field-options field-or-expr :temporal-unit unit)+ (assoc-field-options field-or-expr :temporal-unit unit)[:absolute-datetime filter-value unit]]))))(testing :>- (is (= [:>= (mbql.u/assoc-field-options field-or-expr :temporal-unit :default) upper]+ (is (= [:>= (assoc-field-options field-or-expr :temporal-unit :default) upper](optimize-filters[:>- (mbql.u/assoc-field-options field-or-expr :temporal-unit unit)+ (assoc-field-options field-or-expr :temporal-unit unit)[:absolute-datetime filter-value unit]]))))(testing :>=- (is (= [:>= (mbql.u/assoc-field-options field-or-expr :temporal-unit :default) lower]+ (is (= [:>= (assoc-field-options field-or-expr :temporal-unit :default) lower](optimize-filters[:>=- (mbql.u/assoc-field-options field-or-expr :temporal-unit unit)+ (assoc-field-options field-or-expr :temporal-unit unit)[:absolute-datetime filter-value unit]]))))(testing :between(is (= [:and- [:>= (mbql.u/assoc-field-options field-or-expr :temporal-unit :default) lower]- [:< (mbql.u/assoc-field-options field-or-expr :temporal-unit :default) upper]]+ [:>= (assoc-field-options field-or-expr :temporal-unit :default) lower]+ [:< (assoc-field-options field-or-expr :temporal-unit :default) upper]](optimize-filters[:between- (mbql.u/assoc-field-options field-or-expr :temporal-unit unit)+ (assoc-field-options field-or-expr :temporal-unit unit)[:absolute-datetime filter-value unit][:absolute-datetime filter-value unit]]))))))))@@ -312,10 +316,10 @@(testing (format "last %s" unit)(is (=? {:query {:filter [:and[:>=- (mbql.u/assoc-field-options field-or-expr :temporal-unit :default)+ (assoc-field-options field-or-expr :temporal-unit :default)[:relative-datetime -1 unit]][:<- (mbql.u/assoc-field-options field-or-expr :temporal-unit :default)+ (assoc-field-options field-or-expr :temporal-unit :default)[:relative-datetime 0 unit]]]}}(optimize-querymp@@ -327,7 +331,7 @@(lib.tu.attempted-murders-metadata-provider/id :attempts :datetime)nil]}:filter [:=- (mbql.u/assoc-field-options field-or-expr :temporal-unit unit)+ (assoc-field-options field-or-expr :temporal-unit unit)[:relative-datetime -1 unit]]}}))))(testing (format "this %s" unit);; test the various different ways we might refer to 'now'@@ -337,10 +341,10 @@(testing (format "clause = %s" (pr-str clause))(is (=? {:query {:filter [:and[:>=- (mbql.u/assoc-field-options field-or-expr :temporal-unit :default)+ (assoc-field-options field-or-expr :temporal-unit :default)[:relative-datetime 0 unit]][:<- (mbql.u/assoc-field-options field-or-expr :temporal-unit :default)+ (assoc-field-options field-or-expr :temporal-unit :default)[:relative-datetime 1 unit]]]}}(optimize-querymp@@ -352,15 +356,15 @@(lib.tu.attempted-murders-metadata-provider/id :attempts :datetime)nil]}:filter [:=- (mbql.u/assoc-field-options field-or-expr :temporal-unit unit)+ (assoc-field-options field-or-expr :temporal-unit unit)clause]}}))))))(testing (format "next %s" unit)(is (=? {:query {:filter [:and[:>=- (mbql.u/assoc-field-options field-or-expr :temporal-unit :default)+ (assoc-field-options field-or-expr :temporal-unit :default)[:relative-datetime 1 unit]][:<- (mbql.u/assoc-field-options field-or-expr :temporal-unit :default)+ (assoc-field-options field-or-expr :temporal-unit :default)[:relative-datetime 2 unit]]]}}(optimize-querymp@@ -372,7 +376,7 @@(lib.tu.attempted-murders-metadata-provider/id :attempts :datetime)nil]}:filter [:=- (mbql.u/assoc-field-options field-or-expr :temporal-unit unit)+ (assoc-field-options field-or-expr :temporal-unit unit)[:relative-datetime 1 unit]]}}))))))))(deftest ^:parallel optimize-mixed-temporal-values-testdiff --git a/test/metabase/query_processor/middleware/validate_temporal_bucketing_test.clj b/test/metabase/query_processor/middleware/validate_temporal_bucketing_test.cljindex ca35d206101..094f8c94ced 100644--- a/test/metabase/query_processor/middleware/validate_temporal_bucketing_test.clj+++ b/test/metabase/query_processor/middleware/validate_temporal_bucketing_test.clj@@ -7,48 +7,51 @@[metabase.lib.test-util.attempted-murders-metadata-provider :as lib.tu.attempted-murders-metadata-provider][metabase.lib.test-util.sad-toucan-incidents-metadata-provider :as lib.tu.sad-toucan-incidents-metadata-provider][metabase.query-processor.middleware.validate-temporal-bucketing :as validate-temporal-bucketing]- [metabase.test :as mt]))+ [metabase.test :as mt]+ [metabase.util.malli :as mu]))(defn- validate [query](validate-temporal-bucketing/validate-temporal-bucketing query))(deftest ^:parallel validate-temporal-bucketing-test(let [mp lib.tu.attempted-murders-metadata-provider/metadata-provider]- (doseq [field-clause-type [:id :name]]- (testing (format "With %s clauses" field-clause-type)- (letfn [(query [field unit]- (-> (lib/query mp (lib.metadata/table mp (lib.tu.attempted-murders-metadata-provider/id :attempts)))- (lib/filter (lib/=- [:field- (merge- {:lib/uuid (str (random-uuid))- :temporal-unit unit}- (select-keys (lib.metadata/field mp (lib.tu.attempted-murders-metadata-provider/id :attempts field))- [:base-type :effective-type]))- (case field-clause-type- :id (lib.tu.attempted-murders-metadata-provider/id :attempts field)- :name (:name (lib.metadata/field mp (lib.tu.attempted-murders-metadata-provider/id :attempts field))))]- (lib/relative-datetime -1 unit)))))]- ;; I don't think we need to test every possible combination in the world here -- that will get tested by- ;; other stuff- (doseq [[field unit valid?] [[:date :day true]- [:date :default true]- [:time :day false]- [:datetime :day true]- [:datetime-tz :month true]- [:date :minute false]- [:time-tz :minute true]- [:datetime-tz :minute true]]]- (if valid?- (testing (format "Valid combinations (%s × %s) should return query as-is" field unit)- (is (=? (lib.schema.util/remove-lib-uuids (query field unit))- (validate (query field unit)))))- (testing (format "We should throw an Exception if you try to do something that makes no sense (bucketing %s by %s)"- field unit)- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Unsupported temporal bucketing: You can't bucket"- (validate (query field unit))))))))))))+ ;; you aren't even allowed to create these at a schema level these days so disable Malli enforcement+ (mu/disable-enforcement+ (doseq [field-clause-type [:id :name]]+ (testing (format "With %s clauses" field-clause-type)+ (letfn [(query [field unit]+ (-> (lib/query mp (lib.metadata/table mp (lib.tu.attempted-murders-metadata-provider/id :attempts)))+ (lib/filter (lib/=+ [:field+ (merge+ {:lib/uuid (str (random-uuid))+ :temporal-unit unit}+ (select-keys (lib.metadata/field mp (lib.tu.attempted-murders-metadata-provider/id :attempts field))+ [:base-type :effective-type]))+ (case field-clause-type+ :id (lib.tu.attempted-murders-metadata-provider/id :attempts field)+ :name (:name (lib.metadata/field mp (lib.tu.attempted-murders-metadata-provider/id :attempts field))))]+ (lib/relative-datetime -1 unit)))))]+ ;; I don't think we need to test every possible combination in the world here -- that will get tested by+ ;; other stuff+ (doseq [[field unit valid?] [[:date :day true]+ [:date :default true]+ [:time :day false]+ [:datetime :day true]+ [:datetime-tz :month true]+ [:date :minute false]+ [:time-tz :minute true]+ [:datetime-tz :minute true]]]+ (if valid?+ (testing (format "Valid combinations (%s × %s) should return query as-is" field unit)+ (is (=? (lib.schema.util/remove-lib-uuids (query field unit))+ (validate (query field unit)))))+ (testing (format "We should throw an Exception if you try to do something that makes no sense (bucketing %s by %s)"+ field unit)+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Unsupported temporal bucketing: You can't bucket"+ (validate (query field unit)))))))))))))(deftest ^:parallel unix-timestamp-test(testing "UNIX Timestamps should be bucketable by anything"@@ -63,10 +66,12 @@(deftest ^:parallel e2e-test(testing "We should throw an Exception if you try to do something that makes no sense, e.g. bucketing a DATE by MINUTE"- (mt/dataset attempted-murders- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Unsupported temporal bucketing: You can't bucket a :type/Date Field by :minute"- (mt/run-mbql-query attempts- {:aggregation [[:count]]- :filter [:time-interval $date :last :minute]}))))))+ ;; these days the Malli schema enforces this, so turn it off and see if the query still fails+ (mu/disable-enforcement+ (mt/dataset attempted-murders+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Unsupported temporal bucketing: You can't bucket a :type/Date Field by :minute"+ (mt/run-mbql-query attempts+ {:aggregation [[:count]]+ :filter [:time-interval $date :last :minute]})))))))diff --git a/test/metabase/query_processor/util/add_alias_info_test.clj b/test/metabase/query_processor/util/add_alias_info_test.cljindex 0a3c75f3897..6c92db1403d 100644--- a/test/metabase/query_processor/util/add_alias_info_test.clj+++ b/test/metabase/query_processor/util/add_alias_info_test.clj@@ -15,7 +15,6 @@[metabase.lib.test-util.macros :as lib.tu.macros][metabase.lib.test-util.metadata-providers.mock :as providers.mock][metabase.lib.test-util.uuid-dogs-metadata-provider :as lib.tu.uuid-dogs-metadata-provider]- [metabase.lib.util :as lib.util][metabase.lib.util.match :as lib.util.match][metabase.query-processor.preprocess :as qp.preprocess]^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store]@@ -1129,7 +1128,7 @@;; this is a basically the same as Oracle's behavior for truncating aliasesquery (binding [add/*escape-alias-fn* (fn [_driver s](let [s (str/replace s #"[\"\u0000]" "_")]- (lib.util/truncate-alias s 30)))]+ (lib/truncate-alias s 30)))](add-alias-info query))stage (-> query :stages first)stage-join (-> stage :joins first)diff --git a/test/metabase/query_processor_test/nested_queries_test.clj b/test/metabase/query_processor_test/nested_queries_test.cljindex c45000f7cbe..73ee8297eff 100644--- a/test/metabase/query_processor_test/nested_queries_test.clj+++ b/test/metabase/query_processor_test/nested_queries_test.clj@@ -16,7 +16,7 @@[metabase.lib.temporal-bucket :as lib.temporal-bucket][metabase.lib.test-metadata :as meta][metabase.lib.test-util :as lib.tu]- [metabase.lib.util :as lib.util]+ [metabase.lib.util.unique-name-generator][metabase.models.interface :as mi][metabase.permissions.core :as perms][metabase.query-permissions.core :as query-perms]@@ -30,6 +30,7 @@[metabase.test.data.interface :as tx][metabase.util :as u][metabase.util.date-2 :as u.date]+ [metabase.util.malli :as mu][toucan2.core :as t2]))(deftest ^:parallel basic-test@@ -364,17 +365,18 @@(count long-col-full-name)));; Disable truncate-alias when compiling the native query to ensure we don't further truncate the column.;; We want to simulate a user-defined query where the column name is long, but valid for the driver.- native-sub-query (with-redefs [lib.util/truncate-alias+ native-sub-query (with-redefs [metabase.lib.util.unique-name-generator/truncate-alias(fn mock-truncate-alias([ss] ss)([ss _] ss))]- (-> (mt/mbql-query people- {:source-table $$people- :aggregation [[:aggregation-options [:count] {:name coun-col-name}]]- :breakout [[:field %state {:name long-col-name}]]- :limit 5})- qp.compile/compile- :query))+ (mu/disable-enforcement+ (-> (mt/mbql-query people+ {:source-table $$people+ :aggregation [[:aggregation-options [:count] {:name coun-col-name}]]+ :breakout [[:field %state {:name long-col-name}]]+ :limit 5})+ qp.compile/compile+ :query)))query (query-with-source-card 1 lib.schema.id/saved-questions-virtual-database-id)](qp.store/with-metadata-provider (qp.test-util/metadata-provider-with-cards-with-metadata-for-queries[(mt/native-query {:query native-sub-query})])@@ -625,6 +627,7 @@:unit :day)(dissoc :semantic_type :coercion_strategy :table_id:id :settings :fingerprint :nfc_path)+ #_{:clj-kondo/ignore [:deprecated-var]}lib.temporal-bucket/ensure-temporal-unit-in-display-name)(qp.test-util/aggregate-col :count)](mt/colsdiff --git a/test/metabase/test/generate.clj b/test/metabase/test/generate.cljindex 4fa48e56d82..bba44a121a9 100644--- a/test/metabase/test/generate.clj+++ b/test/metabase/test/generate.clj@@ -5,7 +5,7 @@[clojure.test.check.generators :as gen][java-time.api :as t][medley.core :as m]- [metabase.legacy-mbql.util :as mbql.u]+ [metabase.lib.core :as lib][metabase.util.log :as log][reifyhealth.specmonstah.core :as rs][reifyhealth.specmonstah.spec-gen :as rsg]@@ -308,7 +308,7 @@[query](rsg/ent-db-spec-gen {:schema schema} query))-(def ^:private unique-name (mbql.u/unique-name-generator))+(def ^:private unique-name (lib/non-truncating-unique-name-generator))(defn- unique-email [^String email](let [at (.indexOf email "@")]diff --git a/test/metabase/upload/impl_test.clj b/test/metabase/upload/impl_test.cljindex 51f703777d3..e3027d3c7b8 100644--- a/test/metabase/upload/impl_test.clj+++ b/test/metabase/upload/impl_test.clj@@ -2583,9 +2583,9 @@(defmethod driver/column-name-length-limit ::short-column-test-driver [_] 10)(deftest unique-long-column-names-test- (let [original ["αbcdεf_αbcdεf" "αbcdεfg_αbcdεf" "αbc_2_etc_αbcdεf" "αbc_3_xyz_αbcdεf"]- expected [:%ce%b1bcd :%_a2ba0330 :%ce%b1bc_2 :%ce%b1bc_3]- displays ["αbcdεf" "αbcdεfg" "αbc 2 etc" "αbc 3 xyz"]]+ (let [original ["αbcdεf_αbcdεf" "αbcdεfg_αbcdεf" "αbc_2_etc_αbcdεf" "αbc_3_xyz_αbcdεf"]+ expected [:%ce%b1bcd :%_a2ba0330 :%ce%b1bc_2 :%ce%b1bc_3]+ displays ["αbcdεf" "αbcdεfg" "αbc 2 etc" "αbc 3 xyz"]](is (= expected (#'upload/derive-column-names ::short-column-test-driver original)))(mt/with-dynamic-fn-redefs [upload/max-bytes (constantly 10)](is (= displays