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
Minor `lib.convert` fixes (#63401)
Cam Saul5f5b2f6d076e
diff --git a/src/metabase/legacy_mbql/schema.cljc b/src/metabase/legacy_mbql/schema.cljcindex a6a63537082..ccc14c840d3 100644--- a/src/metabase/legacy_mbql/schema.cljc+++ b/src/metabase/legacy_mbql/schema.cljc@@ -1688,6 +1688,8 @@"Schema for a valid dimension clause."(with-meta [:ref ::dimension] {:clause-name :dimension}))+(defmethod options-style-method :dimension [_tag] ::options-style.last-unless-empty)+(defclause variabletarget template-tag)diff --git a/src/metabase/lib/convert.cljc b/src/metabase/lib/convert.cljcindex 3b072a4ea75..0fdc2ee1021 100644--- a/src/metabase/lib/convert.cljc+++ b/src/metabase/lib/convert.cljc@@ -125,15 +125,24 @@lib.dispatch/dispatch-value:hierarchy lib.hierarchy/hierarchy)-(defn- default-MBQL-clause->pMBQL [mbql-clause]- (let [last-elem (peek mbql-clause)- last-elem-option? (map? last-elem)- [clause-type & args] (cond-> mbql-clause- last-elem-option? pop)- options (if last-elem-option?- last-elem- {})]- (lib.options/ensure-uuid (into [clause-type options] (map ->pMBQL) args))))+(defn- default-MBQL-clause->pMBQL [[tag & args :as clause]]+ (if (map? (first args))+ ;; already MBQL 5+ clause+ ;; decode from legacy MBQL+ (let [[tag options & args] (case (mbql.s/options-style tag)+ ::mbql.s/options-style.none (list* tag nil args)+ ::mbql.s/options-style.mbql5 clause+ (::mbql.s/options-style.last-always+ ::mbql.s/options-style.last-always.snake_case) (list* tag (or (last args) {}) (butlast args))+ ::mbql.s/options-style.last-unless-empty (if (map? (last args))+ (list* tag (last args) (butlast args))+ (list* tag {} args))+ ::mbql.s/options-style.π¨πππ (cond+ (> (count args) 2) clause+ (map? (last args)) (list* tag (last args) (butlast args))+ :else (list* tag {} args)))]+ (lib.options/ensure-uuid (into [tag options] (map ->pMBQL) args)))))(defmethod ->pMBQL :default[x]@@ -634,10 +643,14 @@(defmethod ->legacy-MBQL :field [[_ opts id]];; Fields are not like the normal clauses - they need that options field even if it's null.;; TODO: Sometimes the given field is in the legacy order - that seems wrong.- (let [[opts id] (if ((some-fn nil? map?) opts)- [opts id]- [id opts])]- (clause-with-options->legacy-MBQL [:field opts id])))+ (let [[opts id] (if ((some-fn nil? map?) opts)+ [opts id]+ [id opts])+ ensure-base-type (fn [[tag field-name legacy-opts, :as _legacy-ref]]+ [tag field-name (merge (select-keys opts [:base-type]) legacy-opts)])+ legacy-ref (clause-with-options->legacy-MBQL [:field opts id])]+ (cond-> legacy-ref+ (string? id) ensure-base-type)))(defn- update-list->legacy-boolean-expression[m pMBQL-key legacy-key]diff --git a/test/metabase/lib/convert_test.cljc b/test/metabase/lib/convert_test.cljcindex e21163fd41a..57e328b26e1 100644--- a/test/metabase/lib/convert_test.cljc+++ b/test/metabase/lib/convert_test.cljc@@ -54,13 +54,30 @@:query {:source-query {:source-table 1}:fields [[:field 2 nil][:field 3 {:temporal-unit :month}]]- :aggregation [[:count]]}}))))+ :aggregation [[:count]]}})))))++(deftest ^:parallel ->pMBQL-idempotency-test-2(testing ":field clause"(are [clause expected] (=? expected(lib.convert/->pMBQL (lib.convert/->pMBQL clause)))[:field 2 nil] [:field {:lib/uuid string?} 2][:field 3 {:temporal-unit :month}] [:field {:lib/uuid string?, :temporal-unit :month} 3])))+(deftest ^:parallel ->pMBQL-idempotency-test-3+ (testing "Calling ->pMBQL on something already MBQL 5 should no-op instead of adding duplicate options maps"+ (let [clause [[:=+ {:lib/uuid "1cb124b0-757f-4717-b8ee-9cf12a7c3f62"}+ [:field+ {:lib/uuid "a2eb96a0-420b-4465-817d-f3c9f789eff4"}+ (meta/id :users :id)]+ [:field+ {:base-type :type/Integer+ :join-alias "checkins_by_user"+ :lib/uuid "b23a769d-774a-4eb5-8fb8-1f6a33c9a8d5"}+ "USER_ID"]]]]+ (is (= clause+ (lib.convert/->pMBQL clause))))))+(deftest ^:parallel ->pMBQL-joins-test(is (=? {:lib/type :mbql/query:database (meta/id)@@ -1619,3 +1636,18 @@{:case-sensitive false, :lib/uuid "92c8ee03-6bc6-45c9-865c-57d0c5d53e23"}[:field {:base-type :type/Text, :lib/uuid "044811f6-9e30-4c42-b5b6-6afde5031675", :effective-type :type/Text} 243]"C"]])))))++(deftest ^:parallel field-name-ref-to-legacy-never-remove-base-type-test+ (testing "never remove :base-type from a :field name ref regardless of :metabase.lib.query/transformation-added-base-type"+ (is (= [:field "USER_ID" {:base-type :type/Integer, :join-alias "ord1"}]+ (lib.convert/->legacy-MBQL [:field {:lib/uuid "47afe974-396c-4213-8736-859399ba5e7e"+ :base-type :type/Integer+ :join-alias "ord1"+ :metabase.lib.query/transformation-added-base-type true}+ "USER_ID"])))))++(deftest ^:parallel dimension->mbql5-test+ (is (=? [:dimension+ {:stage-number 0, :lib/uuid string?}+ [:field {:base-type :type/BigInteger, :lib/uuid string?} 49]]+ (lib.convert/->pMBQL [:dimension [:field 49 {:base-type :type/BigInteger}] {:stage-number 0}]))))