Skip to content

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

Fix quadratic :is_personal hydration that made GET /api/collection ta… (#79073)

Alexander Polyankin60cbd1c85cb0
diff --git a/src/metabase/collections/models/collection.clj b/src/metabase/collections/models/collection.clj
index a1f3c19f425..9a945732fb6 100644
--- a/src/metabase/collections/models/collection.clj
+++ b/src/metabase/collections/models/collection.clj
@@ -740,10 +740,13 @@
;; root collection is nil
[collection]))
(let [personal-collection-ids (t2/select-pks-set :model/Collection :personal_owner_id [:not= nil])
+ ;; Personal Collections only ever live in the Root Collection, so a Collection is inside one exactly when
+ ;; the first ID of its location path is a Personal Collection. Testing that ID against the set beats
+ ;; scanning every personal collection per row: instances with thousands of each made this quadratic.
location-is-personal (fn [location]
(boolean
(and (string? location)
- (some #(str/starts-with? location (format "/%d/" %)) personal-collection-ids))))]
+ (personal-collection-ids (first (location-path->ids location))))))]
(map (fn [{:keys [location personal_owner_id] :as coll}]
(if (some? coll)
(assoc coll :is_personal (or (some? personal_owner_id)
diff --git a/test/metabase/collections/models/collection_test.clj b/test/metabase/collections/models/collection_test.clj
index 90adaf34e62..71dcc5a2c1c 100644
--- a/test/metabase/collections/models/collection_test.clj
+++ b/test/metabase/collections/models/collection_test.clj
@@ -1281,7 +1281,12 @@
:model/Collection {nested-personal-coll :id} {:location (format "/%d/" personal-coll)
:personal_owner_id nil}
:model/Collection {top-level-coll :id} {:location "/"}
- :model/Collection {nested-top-level-coll :id} {:location (format "/%d/" top-level-coll)}]
+ :model/Collection {nested-top-level-coll :id} {:location (format "/%d/" top-level-coll)}
+ ;; a grandchild of a Personal Collection: only the *first* ID in the location is the personal one
+ :model/Collection {deep-personal-coll :id} {:location (format "/%d/%d/" personal-coll nested-personal-coll)}
+ ;; Personal Collections only ever live in the Root Collection, so a personal ID appearing deeper in a
+ ;; location does not make that Collection personal
+ :model/Collection {sneaky-coll :id} {:location (format "/%d/%d/" top-level-coll personal-coll)}]
(let [check-is-personal (fn [id-or-ids]
(if (int? id-or-ids)
(-> (t2/select-one :model/Collection id-or-ids)
@@ -1293,7 +1298,11 @@
(testing "simple hydration and batched hydration should return correctly"
(is (= [true true false false]
(map check-is-personal [personal-coll nested-personal-coll top-level-coll nested-top-level-coll])
- (check-is-personal [personal-coll nested-personal-coll top-level-coll nested-top-level-coll]))))
+ (check-is-personal [personal-coll nested-personal-coll top-level-coll nested-top-level-coll])))
+ (testing "only the first ID of the location decides"
+ (is (= [true false]
+ (map check-is-personal [deep-personal-coll sneaky-coll])
+ (check-is-personal [deep-personal-coll sneaky-coll])))))
(testing "root collection shouldn't be hydrated"
(is (= nil (t2/hydrate nil :is_personal)))
(is (= [nil true] (map :is_personal (t2/hydrate [nil (t2/select-one :model/Collection personal-coll)] :is_personal)))))))))