Skip to content
Snippets Groups Projects
Unverified Commit 9c2cf97a authored by Braden Shepherdson's avatar Braden Shepherdson Committed by GitHub
Browse files

[MLv2] Resolve a string `:fk-field-id` to a numeric ID if possible (#37079)

Progress towards #37067, might not fix it entirely. There may be other
bugs stemming from the string `:fk-field-id`, but I can't reproduce it
locally.
parent 1c06fac7
No related branches found
No related tags found
No related merge requests found
(ns metabase.lib.column-group
(:require
[metabase.lib.card :as lib.card]
[metabase.lib.equality :as lib.equality]
[metabase.lib.join :as lib.join]
[metabase.lib.join.util :as lib.join.util]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.metadata.calculation :as lib.metadata.calculation]
[metabase.lib.options :as lib.options]
[metabase.lib.schema.common :as lib.schema.common]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.lib.util :as lib.util]
......@@ -45,7 +47,8 @@
(>= (count (keys (select-keys m [:join-alias :table-id :card-id]))) 1))]]]
[:group-type/join.implicit
[:map
[:fk-field-id [:ref ::lib.schema.id/field]]]]]])
;; TODO: Strings are allowed here to work around a QP bug; see #37067.
[:fk-field-id [:or [:ref ::lib.schema.id/field] ::lib.schema.common/non-blank-string]]]]]])
(defmethod lib.metadata.calculation/metadata-method :metadata/column-group
[_query _stage-number column-group]
......@@ -94,7 +97,18 @@
(defmethod display-info-for-group-method :group-type/join.implicit
[query stage-number {:keys [fk-field-id], :as _column-group}]
(merge
(when-let [field (lib.metadata/field query fk-field-id)]
(when-let [field (if (string? fk-field-id)
;; TODO: This is probably working around a QP bug. See #37067 - the `data.cols` from the API has
;; fk_field_id: "customer_id" but I think that's wrong and it should not have dropped the real
;; field ID somewhere in the QP.
(some->> (lib.util/query-stage query stage-number)
(lib.metadata.calculation/visible-columns query stage-number)
(lib.equality/find-matching-column
query stage-number
(lib.options/ensure-uuid [:field {:base-type :type/*} fk-field-id]))
:id
(lib.metadata/field query))
(lib.metadata/field query fk-field-id))]
(let [field-info (lib.metadata.calculation/display-info query stage-number field)]
;; Implicitly joined column pickers don't use the target table's name, they use the FK field's name with
;; "ID" dropped instead.
......
......@@ -412,3 +412,25 @@
{:lib/desired-column-alias "PEOPLE__via__USER_ID__LATITUDE_2", :fk-join-alias "Mock orders card"}
{:lib/desired-column-alias "PEOPLE__via__USER_ID__CREATED_AT_2", :fk-join-alias "Mock orders card"}]
(::lib.column-group/columns product-2))))))
(deftest ^:parallel string-fk-id-test
(testing "models sometimes result in a string FK ID - those should be correctly looked up (#37067)"
(let [query (lib/query meta/metadata-provider (meta/table-metadata :orders))
columns (filter #(= (:table-id %) (meta/id :products)) (lib/visible-columns query))
group {:lib/type :metadata/column-group
::lib.column-group/group-type :group-type/join.implicit
::lib.column-group/columns columns
:fk-field-id "PRODUCT_ID"}]
(is (=? {:display-name "Product ID"
:name "PRODUCT_ID"
:long-display-name "Product ID"
:fk-reference-name "Product"
:is-from-join false
:is-implicitly-joinable true
:semantic-type :type/FK
:effective-type :type/Integer
:table {:display-name "Orders"
:long-display-name "Orders"
:is-source-table true
:name "ORDERS"}}
(lib/display-info query 0 group))))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment