Skip to content

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

Keep Metabot's join labels out of column metadata (#83648)

Andrei Simionescu1d8ae79f566a
diff --git a/src/metabase/metabot/tools/entity_details.clj b/src/metabase/metabot/tools/entity_details.clj
index 060dadeda45..30cbdd137bf 100644
--- a/src/metabase/metabot/tools/entity_details.clj
+++ b/src/metabase/metabot/tools/entity_details.clj
@@ -298,7 +298,6 @@
(comp (filter #(and (= target-tid (:table-id %))
(not (base-reachable-ids (:id %)))
(not (key-ids (:id %)))))
- (map #(metabot.tools.u/add-table-reference defq %))
(map #(metabot.tools.u/->result-column defq %))
(map (fn [rc]
(cond-> rc
@@ -416,10 +415,7 @@
:field_id))
(and source-table with-queryable-dimensions?)
- (assoc :queryable-dimensions (into []
- (comp (map #(metabot.tools.u/add-table-reference base-query %))
- (map #(metabot.tools.u/->result-column metric-query %)))
- queryable-columns))
+ (assoc :queryable-dimensions (mapv #(metabot.tools.u/->result-column metric-query %) queryable-columns))
(seq join-required-dims)
(assoc :join-required-dimensions join-required-dims)
@@ -494,8 +490,7 @@
cols (when with-fields?
(->> (lib/visible-columns table-query -1 {:include-implicitly-joinable? false})
permission-filter-columns
- field-values-fn
- (map #(metabot.tools.u/add-table-reference table-query %))))
+ field-values-fn))
related (when with-related-tables?
(related-tables table-query with-fields? field-values-fn))]
(-> {:id id
diff --git a/src/metabase/metabot/tools/util.clj b/src/metabase/metabot/tools/util.clj
index be4e2d07f47..ca30efea668 100644
--- a/src/metabase/metabot/tools/util.clj
+++ b/src/metabase/metabot/tools/util.clj
@@ -41,15 +41,14 @@
(isa? (:effective-type column) :type/Time) :time
(lib.types.isa/temporal? column) :date)))
-(defn add-table-reference
- "Add table-reference to columns that have FK relationships."
+(defn- table-reference
+ "Return the name of the FK an implicitly joined `col` comes through, like \"User\" for `User ID`."
[query col]
- (cond-> col
- (and (:fk-field-id col)
- (:table-id col))
- (assoc :table-reference (->> (lib.metadata/field query (:fk-field-id col))
- (lib/display-name query)
- lib/display-name-without-id))))
+ (when (and (:fk-field-id col)
+ (:table-id col))
+ (some->> (lib.metadata/field query (:fk-field-id col))
+ (lib/display-name query)
+ lib/display-name-without-id)))
(defn- column-portable-fk
"Build the portable FK path `[db-name, schema-or-null, table-name, field-name …]` for a column
@@ -102,7 +101,7 @@
(-> {:field_id field-id
:name (or (:lib/desired-column-alias column)
(:lib/source-column-alias column))
- :display_name (lib/display-name query (dissoc column :table-reference))
+ :display_name (lib/display-name query column)
:type (convert-field-type column)}
(m/assoc-some :description (:description column)
:base_type base-type
@@ -113,7 +112,7 @@
:field_values (:field-values column)
:portable_fk portable-fk
:fk_target_portable_fk fk-target-fk
- :table_reference (:table-reference column)))))
+ :table_reference (table-reference query column)))))
(defn find-column-by-field-id
"Find a column in `columns` by its real field ID.
diff --git a/test/metabase/metabot/tools/field_stats_test.clj b/test/metabase/metabot/tools/field_stats_test.clj
index a165166f7b8..8f0066a24e9 100644
--- a/test/metabase/metabot/tools/field_stats_test.clj
+++ b/test/metabase/metabot/tools/field_stats_test.clj
@@ -150,7 +150,12 @@
{:distinct-count 2308
:percent-null 0.0
:earliest "1958-04-26"
- :latest "2000-04-03"}}))))))
+ :latest "2000-04-03"}})))
+ (testing "A metric field reached through an FK names that FK."
+ (mt/as-admin
+ (is (=? {:structured-output {:table_reference "User"}}
+ (metabot.tools.field-stats/field-values
+ {:entity-type "metric", :entity-id metric-id, :field-id birth-date-id, :limit 5}))))))))
(deftest field-values-unknown-entity-type-test
(testing "an unrecognized entity-type -- e.g. because the schema that constrains it isn't enforced in
diff --git a/test/metabase/metabot/tools/util_test.clj b/test/metabase/metabot/tools/util_test.clj
index c9963b182d7..6509a6af1cc 100644
--- a/test/metabase/metabot/tools/util_test.clj
+++ b/test/metabase/metabot/tools/util_test.clj
@@ -188,44 +188,17 @@
(is (contains? card-ids (:id open-model)))
(is (not (contains? card-ids (:id destination-model)))))))))
-(deftest add-table-reference-test
- (testing "add-table-reference function adds table-reference for FK fields"
- (mt/dataset test-data
- (mt/with-current-user (mt/user->id :crowberto)
- (let [test-db-id (mt/id)
- mp (lib-be/application-database-metadata-provider test-db-id)
- orders-query (lib/query mp (lib.metadata/table mp (mt/id :orders)))
- columns (lib/visible-columns orders-query)]
- (testing "adds table-reference for implicitly joined columns"
- (let [processed-columns (map #(metabot.tools.util/add-table-reference orders-query %) columns)
- user-name-column (first (filter #(and (= "NAME" (:name %))
- (:fk-field-id %)) processed-columns))]
- (is (some? user-name-column) "Expected to find implicitly joined User NAME column")
- (is (contains? user-name-column :table-reference))
- (is (string? (:table-reference user-name-column)))
- (is (seq (:table-reference user-name-column)))
- (is (= "User" (:table-reference user-name-column)))))
- (testing "does not add table-reference for direct table columns"
- (let [processed-columns (map #(metabot.tools.util/add-table-reference orders-query %) columns)
- id-column (first (filter #(and (= "ID" (:name %))
- (not (:fk-field-id %))) processed-columns))]
- (is (some? id-column) "Expected to find direct ORDERS ID column")
- (is (not (contains? id-column :table-reference)))))
- (testing "handles columns without fk-field-id or table-id gracefully"
- (let [mock-column {:name "test-column" :type :string}
- result (metabot.tools.util/add-table-reference orders-query mock-column)]
- (is (= mock-column result))
- (is (not (contains? result :table-reference)))))
- (testing "handles columns with fk-field-id but no table-id"
- (let [mock-column {:name "test-fk" :fk-field-id 123}
- result (metabot.tools.util/add-table-reference orders-query mock-column)]
- (is (= mock-column result))
- (is (not (contains? result :table-reference)))))
- (testing "handles columns with table-id but no fk-field-id"
- (let [mock-column {:name "test-field" :table-id (mt/id :orders)}
- result (metabot.tools.util/add-table-reference orders-query mock-column)]
- (is (= mock-column result))
- (is (not (contains? result :table-reference))))))))))
+(deftest ^:parallel result-column-table-reference-test
+ (testing "result columns name the FK that an implicitly joined column is reached through"
+ (let [mp (lib-be/application-database-metadata-provider (mt/id))
+ orders-query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]
+ (is (=? {(mt/id :people :name) "User"
+ (mt/id :products :title) "Product"
+ (mt/id :orders :total) nil}
+ (into {}
+ (map (comp (juxt :field_id :table_reference)
+ #(metabot.tools.util/->result-column orders-query %)))
+ (lib/visible-columns orders-query)))))))
(deftest metabot-verified-content-test
(testing "metabot-scope-query with verified content filtering"