Skip to content

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

fix(search): index batch failure when function-attr throws (#73352)

Tyler Pludef8531f2301b2
diff --git a/src/metabase/search/ingestion.clj b/src/metabase/search/ingestion.clj
index 3de9bfc049e..078731e2fc0 100644
--- a/src/metabase/search/ingestion.clj
+++ b/src/metabase/search/ingestion.clj
@@ -112,15 +112,21 @@
"Execute all function attributes for a given spec and return computed values.
If a function returns a map, its entries are merged directly into the result —
this allows a single function to populate multiple document keys (e.g. :temporal-info
- returns both :has_temporal_dim and :non_temporal_dim_ids in one call)."
+ returns both :has_temporal_dim and :non_temporal_dim_ids in one call).
+
+ When a function attr declares `:provides`, the attr-key itself is not a column in
+ the index — it's a logical grouping whose `:provides` keys are the real columns.
+ In that case, on non-map results (e.g. when the function threw and `false` was
+ returned), skip writing instead of poisoning the document with an unknown column."
[spec record]
(reduce-kv
(fn [acc attr-key attr-def]
(if (search.spec/function-attr? attr-def)
(let [result (execute-function-attr attr-key attr-def record)]
- (if (map? result)
- (merge acc result)
- (assoc acc (keyword (u/->snake_case_en (name attr-key))) result)))
+ (cond
+ (map? result) (merge acc result)
+ (seq (search.spec/function-attr-provides attr-def)) acc
+ :else (assoc acc (keyword (u/->snake_case_en (name attr-key))) result)))
acc))
{}
(:attrs spec)))
diff --git a/test/metabase/search/ingestion_test.clj b/test/metabase/search/ingestion_test.clj
index 2ad2348b9f2..a200a795b0e 100644
--- a/test/metabase/search/ingestion_test.clj
+++ b/test/metabase/search/ingestion_test.clj
@@ -90,6 +90,23 @@
(#'search.ingestion/embeddable-text record))
"Transformation functions should not be applied to embeddable text for semantic search")))))
+(deftest execute-all-function-attrs-test
+ (testing "function-attr returning a map merges its keys into the document"
+ (let [spec {:attrs {:temporal-info {:fn (constantly {:has_temporal_dim true :non_temporal_dim_ids "[1 2]"})
+ :provides [:has-temporal-dim :non-temporal-dim-ids]}}}]
+ (is (= {:has_temporal_dim true :non_temporal_dim_ids "[1 2]"}
+ (#'search.ingestion/execute-all-function-attrs spec {})))))
+
+ (testing "function-attr without :provides falls back to writing snake_case attr-key on non-map results"
+ (let [spec {:attrs {:native-query {:fn (constantly "SELECT 1")}}}]
+ (is (= {:native_query "SELECT 1"}
+ (#'search.ingestion/execute-all-function-attrs spec {})))))
+
+ (testing "function-attr with :provides skips writing when result is not a map"
+ (let [spec {:attrs {:temporal-info {:fn (fn [_] (throw (ex-info "boom" {})))
+ :provides [:has-temporal-dim :non-temporal-dim-ids]}}}]
+ (is (= {} (#'search.ingestion/execute-all-function-attrs spec {}))))))
+
(deftest search-term-columns-test
(testing "search-term-columns with vector format"
(is (= #{:name :description}