Skip to content

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

Fix metric viewer for metrics based on native sql cards (#72404)

John Swanson05a70000b566
diff --git a/src/metabase/lib_metric/dimension/jvm.clj b/src/metabase/lib_metric/dimension/jvm.clj
index 8cc6a52c211..dc9536cab2a 100644
--- a/src/metabase/lib_metric/dimension/jvm.clj
+++ b/src/metabase/lib_metric/dimension/jvm.clj
@@ -82,14 +82,20 @@
is required for FK / implicitly-joinable column resolution.
For source-card queries (metrics based on models or saved questions), resolves
- the card's table_id and uses that to find the database provider."
+ the card's table_id and uses that to find the database provider. Native-SQL
+ models have no underlying table (`:table_id` is nil), so in that case we fall
+ back to the card's `:database_id` and look up the database provider directly."
[mp query-with-mp]
(when (satisfies? lib-metric.provider/MetricMetadataProvider mp)
(or (when-let [table-id (lib.util/source-table-id query-with-mp)]
(lib-metric.provider/database-provider-for-table mp table-id))
(when-let [card-id (lib.util/source-card-id query-with-mp)]
- (when-let [table-id (t2/select-one-fn :table_id :model/Card card-id)]
- (lib-metric.provider/database-provider-for-table mp table-id))))))
+ (when-let [{:keys [table_id database_id]}
+ (t2/select-one [:model/Card :table_id :database_id] card-id)]
+ (or (when table_id
+ (lib-metric.provider/database-provider-for-table mp table_id))
+ (when database_id
+ (lib-metric.provider/database-provider-for-database mp database_id))))))))
(defn- group-type->type
"Convert a column group's group-type to a dimension group type string."
diff --git a/src/metabase/lib_metric/metadata/provider.cljc b/src/metabase/lib_metric/metadata/provider.cljc
index c0dd3ef9e75..d619ae0c765 100644
--- a/src/metabase/lib_metric/metadata/provider.cljc
+++ b/src/metabase/lib_metric/metadata/provider.cljc
@@ -16,7 +16,10 @@
"Protocol extension for metric-specific operations on a MetadataProvider."
(database-provider-for-table [this table-id]
"Get the database-specific MetadataProvider for a given table-id.
- Returns nil if the table cannot be found."))
+ Returns nil if the table cannot be found.")
+ (database-provider-for-database [this db-id]
+ "Get the database-specific MetadataProvider for a given database-id.
+ Returns nil if the database cannot be found."))
(defn- route-table-metadata
"Route table metadata request to the appropriate database provider."
@@ -139,6 +142,9 @@
(when-let [db-id (table->db-fn table-id)]
(db-provider-fn db-id)))
+ (database-provider-for-database [_this db-id]
+ (db-provider-fn db-id))
+
#?(:clj Object :cljs IEquiv)
(#?(:clj equals :cljs -equiv) [_this another]
(and (instance? MetricContextMetadataProvider another)
diff --git a/test/metabase/lib_metric/dimension/jvm_test.clj b/test/metabase/lib_metric/dimension/jvm_test.clj
index af4dbeeb6b1..ea380cbfba3 100644
--- a/test/metabase/lib_metric/dimension/jvm_test.clj
+++ b/test/metabase/lib_metric/dimension/jvm_test.clj
@@ -53,6 +53,40 @@
(str "dimension " (get-in pair [:dimension :name])
" should have a valid :has-field-values value"))))))))
+(deftest compute-dimension-pairs-native-sql-source-card-test
+ (testing "source-card metrics built on native-SQL models resolve dimensions (UXW-3491)"
+ ;; Native-SQL model cards have `:table_id nil` (there's no underlying table), so the
+ ;; dimension-sync path must fall back to the card's `:database_id` to find a usable
+ ;; db-specific MetadataProvider. Prior to the fix, compute-dimension-pairs returned
+ ;; zero pairs for this shape because the metric-context provider can't resolve
+ ;; `:metadata/card` requests.
+ (mt/with-temp [:model/Card model {:type :model
+ :dataset_query (mt/native-query
+ {:query "SELECT ID, NAME, CATEGORY_ID FROM VENUES"})
+ :database_id (mt/id)
+ :result_metadata [{:name "ID"
+ :display_name "ID"
+ :base_type :type/BigInteger}
+ {:name "NAME"
+ :display_name "Name"
+ :base_type :type/Text}
+ {:name "CATEGORY_ID"
+ :display_name "Category ID"
+ :base_type :type/Integer}]}]
+ (is (nil? (:table_id model))
+ "sanity check: native-SQL model should have no :table_id")
+ (let [mp (lib-metric.metadata.jvm/metadata-provider)
+ db-mp (mt/metadata-provider)
+ card-meta (lib.metadata/card db-mp (:id model))
+ query (-> (lib/query db-mp card-meta)
+ (lib/aggregate (lib/count)))
+ pairs (dimension.jvm/compute-dimension-pairs mp query)]
+ (is (seq pairs)
+ "should resolve dimensions from the card's result_metadata")
+ (is (= #{"ID" "NAME" "CATEGORY_ID"}
+ (into #{} (map #(get-in % [:dimension :name])) pairs))
+ "dimension names should match the card's result_metadata columns")))))
+
(deftest compute-dimension-pairs-has-field-values-for-category-columns-test
(testing "CATEGORY_ID dimension has :has-field-values populated"
(let [mp (lib-metric.metadata.jvm/metadata-provider)