Skip to content

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

Extract entity types (#82265)

Ben Grabow463fb9355b86
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 25ad333e89b..d9c3e1529e0 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -3002,6 +3002,7 @@
query-processor
segments
settings
+ types
util
warehouse-schema}
:model-imports #{:model/Card
diff --git a/src/metabase/types/core.cljc b/src/metabase/types/core.cljc
index f8795d81dae..b30a76b0fe7 100644
--- a/src/metabase/types/core.cljc
+++ b/src/metabase/types/core.cljc
@@ -60,13 +60,17 @@
;;; Table (entity) Types
-(derive :entity/GenericTable :entity/*)
-(derive :entity/UserTable :entity/GenericTable)
-(derive :entity/CompanyTable :entity/GenericTable)
-(derive :entity/TransactionTable :entity/GenericTable)
-(derive :entity/ProductTable :entity/GenericTable)
-(derive :entity/SubscriptionTable :entity/GenericTable)
-(derive :entity/EventTable :entity/GenericTable)
+(def entity-hierarchy
+ "Hierarchy of table entity types, i.e. a Table's `:entity_type`. Not part of Clojure's global hierarchy, so `isa?`
+ on these keywords must be passed this hierarchy explicitly."
+ (-> (make-hierarchy)
+ (derive :entity/GenericTable :entity/*)
+ (derive :entity/UserTable :entity/GenericTable)
+ (derive :entity/CompanyTable :entity/GenericTable)
+ (derive :entity/TransactionTable :entity/GenericTable)
+ (derive :entity/ProductTable :entity/GenericTable)
+ (derive :entity/SubscriptionTable :entity/GenericTable)
+ (derive :entity/EventTable :entity/GenericTable)))
;;; Modifier Types
diff --git a/src/metabase/util/malli/schema.clj b/src/metabase/util/malli/schema.clj
index 175eaca8d11..f792fcbb4dc 100644
--- a/src/metabase/util/malli/schema.clj
+++ b/src/metabase/util/malli/schema.clj
@@ -210,12 +210,6 @@
[:fn #(isa? (keyword %) :Coercion/*)]
(deferred-tru "value must be a valid coercion strategy (keyword or string).")))
-(def EntityTypeKeywordOrString
- "Validates entity type derivatives of `:entity/*`. Allows strings or keywords"
- (mu/with-api-error-message
- [:fn #(isa? (keyword %) :entity/*)]
- (deferred-tru "value must be a valid entity type (keyword or string).")))
-
(defn- open-map
"A map whose keys are deliberately not ours to declare. The marker property is what lets
[[metabase.api.macros.defendpoint.closed-schemas]] accept it; nothing outside this namespace should set it."
diff --git a/src/metabase/warehouse_schema_rest/api/table.clj b/src/metabase/warehouse_schema_rest/api/table.clj
index 82ca4498466..a5e5edc9dcf 100644
--- a/src/metabase/warehouse_schema_rest/api/table.clj
+++ b/src/metabase/warehouse_schema_rest/api/table.clj
@@ -22,6 +22,7 @@
[metabase.query-processor.streaming :as qp.streaming]
[metabase.request.core :as request]
[metabase.sync.core :as sync]
+ [metabase.types.core :as types]
[metabase.upload.core :as upload]
[metabase.util :as u]
[metabase.util.i18n :refer [deferred-tru tru]]
@@ -47,6 +48,12 @@
"Schema for a valid table field ordering."
(into [:enum] (map name table/field-orderings)))
+(def ^:private EntityType
+ "Schema for a valid table entity type, as either a keyword or a string."
+ (mu/with-api-error-message
+ [:fn #(isa? types/entity-hierarchy (keyword %) :entity/*)]
+ (deferred-tru "value must be a valid entity type (keyword or string).")))
+
(mr/def ::data-authority-write
"Schema for writing a valid table data authority."
(into [:enum {:decode/string keyword}] table/writable-data-authority-types))
@@ -246,7 +253,7 @@
_query-params
body :- [:map {:closed true}
[:display_name {:optional true} [:maybe ms/NonBlankString]]
- [:entity_type {:optional true} [:maybe ms/EntityTypeKeywordOrString]]
+ [:entity_type {:optional true} [:maybe EntityType]]
[:visibility_type {:optional true} [:maybe TableVisibilityType]]
[:description {:optional true} [:maybe :string]]
[:caveats {:optional true} [:maybe :string]]
@@ -274,7 +281,7 @@
{:keys [ids], :as body} :- [:map {:closed true}
[:ids [:sequential ms/PositiveInt]]
[:display_name {:optional true} [:maybe ms/NonBlankString]]
- [:entity_type {:optional true} [:maybe ms/EntityTypeKeywordOrString]]
+ [:entity_type {:optional true} [:maybe EntityType]]
[:visibility_type {:optional true} [:maybe TableVisibilityType]]
[:description {:optional true} [:maybe :string]]
[:caveats {:optional true} [:maybe :string]]
diff --git a/src/metabase/xrays/automagic_dashboards/core.clj b/src/metabase/xrays/automagic_dashboards/core.clj
index 681c041bf2a..d6788dc900f 100644
--- a/src/metabase/xrays/automagic_dashboards/core.clj
+++ b/src/metabase/xrays/automagic_dashboards/core.clj
@@ -156,6 +156,7 @@
[metabase.permissions.core :as perms]
[metabase.query-processor.util :as qp.util]
[metabase.segments.schema :as segments.schema]
+ [metabase.types.core :as types]
[metabase.util :as u]
[metabase.util.i18n :as i18n :refer [tru trun]]
[metabase.util.log :as log]
@@ -397,7 +398,7 @@
(->> dashboard-templates
(filter (fn [{:keys [applies_to]}]
(let [[entity-type field-type] applies_to]
- (and (isa? table-type entity-type)
+ (and (isa? types/entity-hierarchy table-type entity-type)
(or (nil? field-type)
(magic.util/field-isa? entity field-type))))))
(sort-by :specificity >))))
diff --git a/src/metabase/xrays/automagic_dashboards/dashboard_templates.clj b/src/metabase/xrays/automagic_dashboards/dashboard_templates.clj
index 5b826cf7796..eccc25a78ec 100644
--- a/src/metabase/xrays/automagic_dashboards/dashboard_templates.clj
+++ b/src/metabase/xrays/automagic_dashboards/dashboard_templates.clj
@@ -9,6 +9,7 @@
[malli.transform :as mtx]
[metabase.dashboards.constants :as dashboards.constants]
[metabase.query-processor.util :as qp.util]
+ [metabase.types.core :as types]
[metabase.util :as u]
[metabase.util.files :as u.files]
[metabase.util.i18n :as i18n]
@@ -16,7 +17,8 @@
[metabase.util.malli.registry :as mr]
[metabase.util.performance :as perf]
[metabase.util.yaml :as yaml]
- [metabase.xrays.automagic-dashboards.populate :as populate])
+ [metabase.xrays.automagic-dashboards.populate :as populate]
+ [metabase.xrays.automagic-dashboards.util :as magic.util])
(:import
(java.nio.file Files Path)))
@@ -106,7 +108,7 @@
(defn- table-type?
[t]
- (isa? t :entity/*))
+ (isa? types/entity-hierarchy t :entity/*))
(def ^:private TableType
[:and
@@ -334,7 +336,7 @@
(defn- specificity
[dashboard-template]
- (transduce (map (comp count ancestors)) + (:applies_to dashboard-template)))
+ (transduce (map magic.util/ancestor-count) + (:applies_to dashboard-template)))
(defn- ensure-default-card-sizes
"Given a card definition from a template, fill in the card template with default width and height
diff --git a/src/metabase/xrays/automagic_dashboards/interesting.clj b/src/metabase/xrays/automagic_dashboards/interesting.clj
index 83c28c9f2d1..7ed52e14b63 100644
--- a/src/metabase/xrays/automagic_dashboards/interesting.clj
+++ b/src/metabase/xrays/automagic_dashboards/interesting.clj
@@ -309,7 +309,7 @@
[candidate-binding-values]
(letfn [(score [a]
(let [[_ definition] a]
- [(reduce + (map (comp count ancestors) (:field_type definition)))
+ [(reduce + (map magic.util/ancestor-count (:field_type definition)))
(count definition)
(:score definition)]))]
(map (juxt (comp score first) identity) candidate-binding-values)))
diff --git a/src/metabase/xrays/automagic_dashboards/util.clj b/src/metabase/xrays/automagic_dashboards/util.clj
index f416b61c870..377cae70650 100644
--- a/src/metabase/xrays/automagic_dashboards/util.clj
+++ b/src/metabase/xrays/automagic_dashboards/util.clj
@@ -7,6 +7,7 @@
[metabase.lib.core :as lib]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.models.interface :as mi]
+ [metabase.types.core :as types]
[metabase.util :as u]
[metabase.util.json :as json]
[metabase.util.log :as log]
@@ -42,7 +43,15 @@
(mu/defn filter-tables :- [:sequential ::ads/source]
"filter `tables` by `tablespec`, which is just an entity type (eg. :entity/GenericTable)"
[tablespec tables :- [:maybe [:sequential ::ads/source]]]
- (filter #(-> % :entity_type (isa? tablespec)) tables))
+ (filter #(isa? types/entity-hierarchy (:entity_type %) tablespec) tables))
+
+(defn ancestor-count
+ "Number of ancestors of one entry of a template's `applies_to` / `field_type`, which mixes entity types and field
+ types. Entity types live in [[types/entity-hierarchy]]; field and semantic types are still in the global hierarchy."
+ [t]
+ (count (if (isa? types/entity-hierarchy t :entity/*)
+ (ancestors types/entity-hierarchy t)
+ (ancestors t))))
(defn saved-metric?
"Is this a saved aggregation clause? True for V2 Metrics and Measures."