Skip to content

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

Remove lib UUID from joins (#72790)

Timothy S. Dean62bccb2a1876
diff --git a/src/metabase/models/serialization.clj b/src/metabase/models/serialization.clj
index f934f7ddda3..6dfaca0b3ac 100644
--- a/src/metabase/models/serialization.clj
+++ b/src/metabase/models/serialization.clj
@@ -1254,6 +1254,11 @@
[:field (opts :guard map?) (id :guard pos-int?)]
[:field (export-mbql-map opts) (*export-field-fk* id)]
+ ;; Field refs whose id is not a raw numeric field id — e.g. a source-card column name string
+ ;; or an already-exported FK vector — still need their opts walked so :lib/uuid is stripped.
+ [:field (opts :guard map?) id]
+ [:field (export-mbql-map opts) id]
+
;; legacy (MBQL 4) field refs are still supported in parameter targets and in result metadata `field_ref`...
[:field (id :guard pos-int?) (opts :guard (some-fn map? nil?))]
[:field (*export-field-fk* id) (export-mbql-map opts)]
@@ -1268,11 +1273,20 @@
[:metric opts (id :guard pos-int?)]
[:metric (export-mbql-map opts) (*export-fk* id 'Card)]
+ [:metric opts id]
+ [:metric (export-mbql-map opts) id]
+
[:segment opts (id :guard pos-int?)]
[:segment (export-mbql-map opts) (*export-fk* id 'Segment)]
+ [:segment opts id]
+ [:segment (export-mbql-map opts) id]
+
[:measure opts (id :guard pos-int?)]
- [:measure (export-mbql-map opts) (*export-fk* id 'Measure)]))
+ [:measure (export-mbql-map opts) (*export-fk* id 'Measure)]
+
+ [:measure opts id]
+ [:measure (export-mbql-map opts) id]))
(defn export-mbql
"Given an MBQL expression, convert it to an EDN structure and turn the non-portable Database, Table and Field IDs
diff --git a/test/metabase/models/serialization_test.clj b/test/metabase/models/serialization_test.clj
index 6895d3f1de8..cb93ca6605a 100644
--- a/test/metabase/models/serialization_test.clj
+++ b/test/metabase/models/serialization_test.clj
@@ -5,23 +5,52 @@
[metabase.lib.test-metadata :as meta]
[metabase.models.serialization :as serdes]))
+(defn- fake-uuid
+ "Deterministic placeholder `:lib/uuid` for tests, e.g. `(fake-uuid 1)` => \"00000000-0000-0000-0000-000000000001\"."
+ [n]
+ (format "00000000-0000-0000-0000-%012d" n))
+
(deftest ^:parallel drop-mbql-5-uuids-on-export-test
(binding [serdes/*export-field-fk* (constantly ::field-id)]
(is (= [:field {} :metabase.models.serialization-test/field-id]
- (#'serdes/export-mbql-ref [:field {:lib/uuid "00000000-0000-0000-0000-000000000001"} 1])))
- (binding [serdes/*required-lib-uuids-for-export* #{"00000000-0000-0000-0000-000000000001"}]
- (is (= [:field {:lib/uuid "00000000-0000-0000-0000-000000000001"} :metabase.models.serialization-test/field-id]
- (#'serdes/export-mbql-ref [:field {:lib/uuid "00000000-0000-0000-0000-000000000001"} 1])))
+ (#'serdes/export-mbql-ref [:field {:lib/uuid (fake-uuid 1)} 1])))
+ (binding [serdes/*required-lib-uuids-for-export* #{(fake-uuid 1)}]
+ (is (= [:field {:lib/uuid (fake-uuid 1)} :metabase.models.serialization-test/field-id]
+ (#'serdes/export-mbql-ref [:field {:lib/uuid (fake-uuid 1)} 1])))
(is (= [:field {} :metabase.models.serialization-test/field-id]
- (#'serdes/export-mbql-ref [:field {:lib/uuid "00000000-0000-0000-0000-000000000002"} 1]))))))
+ (#'serdes/export-mbql-ref [:field {:lib/uuid (fake-uuid 2)} 1]))))))
+
+(deftest ^:parallel drop-mbql-5-uuids-from-string-id-refs-test
+ (testing "field refs with string column names (e.g. source-card joins) should still have lib/uuid stripped"
+ (is (= [:field {} "name"]
+ (#'serdes/export-mbql-ref [:field {:lib/uuid (fake-uuid 1)} "name"])))
+ (testing "and other opts are preserved"
+ (is (= [:field {:base-type :type/Integer} "count"]
+ (#'serdes/export-mbql-ref [:field {:lib/uuid (fake-uuid 1)
+ :base-type :type/Integer} "count"])))))
+ (testing "metric/segment/measure refs with non-integer ids (e.g. after re-export) still strip lib/uuid"
+ (is (= [:metric {} "already-exported-eid"]
+ (#'serdes/export-mbql-ref [:metric {:lib/uuid (fake-uuid 1)}
+ "already-exported-eid"])))
+ (is (= [:segment {} "already-exported-eid"]
+ (#'serdes/export-mbql-ref [:segment {:lib/uuid (fake-uuid 1)}
+ "already-exported-eid"])))
+ (is (= [:measure {} "already-exported-eid"]
+ (#'serdes/export-mbql-ref [:measure {:lib/uuid (fake-uuid 1)}
+ "already-exported-eid"]))))
+ (testing "required lib/uuids are preserved even for string-id refs"
+ (binding [serdes/*required-lib-uuids-for-export* #{(fake-uuid 1)}]
+ (is (= [:field {:lib/uuid (fake-uuid 1)} "name"]
+ (#'serdes/export-mbql-ref [:field {:lib/uuid (fake-uuid 1)}
+ "name"]))))))
(deftest ^:parallel drop-mbql-5-uuids-on-export-test-2
(let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :venues))
(lib/filter (lib/= (meta/field-metadata :venues :id) 1))
(lib/aggregate (-> (lib/count)
- (lib/update-options assoc :lib/uuid "00000000-0000-0000-0000-000000000000")))
+ (lib/update-options assoc :lib/uuid (fake-uuid 0))))
(as-> $query (lib/order-by $query (first (lib/aggregations-metadata $query)))))]
- (is (= #{"00000000-0000-0000-0000-000000000000"}
+ (is (= #{(fake-uuid 0)}
(#'serdes/collect-required-lib-uuids query)))
(binding [serdes/*export-database-fk* (constantly "DATABASE")
serdes/*export-table-fk* (constantly ["DATABASE" "SCHEMA" "TABLE"])
@@ -36,14 +65,14 @@
{:effective-type :type/BigInteger, :base-type :type/BigInteger}
["DATABASE" "SCHEMA" "TABLE" "FIELD"]]
1]]
- :aggregation [[:count {:lib/uuid "00000000-0000-0000-0000-000000000000"}]]
+ :aggregation [[:count {:lib/uuid (fake-uuid 0)}]]
:order-by [[:asc
{}
[:aggregation
{:base-type :type/Integer
:effective-type :type/Integer
:lib/source-name "count"}
- "00000000-0000-0000-0000-000000000000"]]]}]}
+ (fake-uuid 0)]]]}]}
(serdes/export-mbql query))))))
(deftest ^:parallel hydrate-mbql-5-uuids-on-import-test
@@ -58,13 +87,13 @@
{:effective-type "type/BigInteger", :base-type "type/BigInteger"}
["DATABASE" "SCHEMA" "TABLE" "FIELD"]]
1]]
- :aggregation [["count" {:lib/uuid "00000000-0000-0000-0000-000000000000"}]]
+ :aggregation [["count" {:lib/uuid (fake-uuid 0)}]]
:order-by [["asc"
{}
[:aggregation {:base-type "type/Integer"
:effective-type "type/Integer"
:lib/source-name "count"}
- "00000000-0000-0000-0000-000000000000"]]]}]}]
+ (fake-uuid 0)]]]}]}]
(binding [serdes/*import-database-fk* (constantly 1)
serdes/*import-table-fk* (constantly 2)
serdes/*import-field-fk* (constantly 3)]
@@ -80,14 +109,14 @@
:base-type :type/BigInteger}
3]
1]]
- :aggregation [[:count {:lib/uuid "00000000-0000-0000-0000-000000000000"}]]
+ :aggregation [[:count {:lib/uuid (fake-uuid 0)}]]
:order-by [[:asc
{:lib/uuid string?}
[:aggregation
{:base-type :type/Integer
:effective-type :type/Integer
:lib/source-name "count"}
- "00000000-0000-0000-0000-000000000000"]]]}]}
+ (fake-uuid 0)]]]}]}
(serdes/import-mbql query))))))
(deftest ^:parallel hydrate-mbql-5-uuids-on-import-test-2
diff --git a/test_resources/serialization_baseline/collections/main/queries/nested_query.yaml b/test_resources/serialization_baseline/collections/main/queries/nested_query.yaml
index c9daaf97101..9040d966df4 100644
--- a/test_resources/serialization_baseline/collections/main/queries/nested_query.yaml
+++ b/test_resources/serialization_baseline/collections/main/queries/nested_query.yaml
@@ -37,7 +37,6 @@ dataset_query:
- {}
- - field
- base-type: type/Integer
- lib/uuid: 95bea6bf-64d4-4603-b47d-437d22bf6cab
- count
- 5
limit: 20
@@ -46,7 +45,6 @@ dataset_query:
- {}
- - field
- base-type: type/Float
- lib/uuid: 4726a0dd-12b3-4721-8941-c2ec9510dac1
- sum
lib/type: mbql.stage/mbql
lib/type: mbql/query
diff --git a/test_resources/serialization_baseline/collections/main/queries/source_card_reference.yaml b/test_resources/serialization_baseline/collections/main/queries/source_card_reference.yaml
index 731b1a17928..1b4f57a9505 100644
--- a/test_resources/serialization_baseline/collections/main/queries/source_card_reference.yaml
+++ b/test_resources/serialization_baseline/collections/main/queries/source_card_reference.yaml
@@ -14,7 +14,6 @@ dataset_query:
- {}
- - field
- base-type: type/Integer
- lib/uuid: d32f8053-f5b8-4e94-8768-32eb7b5a576f
- count
- 100
order-by:
@@ -22,7 +21,6 @@ dataset_query:
- {}
- - field
- base-type: type/Integer
- lib/uuid: c7c318b1-34bb-488a-bc9a-8e8ccd476336
- count
source-card: h5F2EjHsRd73Dqqh8sAtd
lib/type: mbql.stage/mbql