Skip to content

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

Report datetime insights even if columns is PK or FK (#67536)

metamben905c5dde9934
diff --git a/src/metabase/analyze/fingerprint/insights.clj b/src/metabase/analyze/fingerprint/insights.clj
index 781a5afff85..9125ae797f5 100644
--- a/src/metabase/analyze/fingerprint/insights.clj
+++ b/src/metabase/analyze/fingerprint/insights.clj
@@ -302,11 +302,11 @@
lib-source :lib/source
lib-breakout? :lib/breakout?}]
(cond
- (isa? semantic-type :Relation/*) :others
-
- ;; Only count datetime columns from breakouts/dimensions, not aggregations
- ;; Aggregations of datetime values (like max(created_at)) are computed values,
- ;; not datetime dimensions for the X-axis (#62069)
+ ;; Only count datetime columns from breakouts/dimensions, not aggregations
+ ;; Aggregations of datetime values (like max(created_at)) are computed values,
+ ;; not datetime dimensions for the X-axis (#62069)
+ ;; Datetime columns with FK/PK semantic types should still be recognized as
+ ;; datetimes for timeseries insights (#35281)
(and
(or (u.date/truncate-units unit)
(isa? (or effective-type base-type) :type/Temporal))
@@ -317,7 +317,11 @@
:datetimes
(u.date/extract-units unit) :numbers
- (isa? base-type :type/Number) :numbers
+ ;; Don't treat numeric FK/PK columns as numbers - they are identifiers, not
+ ;; values to compute insights over
+ (and (isa? base-type :type/Number)
+ (not (isa? semantic-type :Relation/*)))
+ :numbers
:else :others))))]
(cond
(timeseries? cols-by-type) (timeseries-insight cols-by-type)
diff --git a/test/metabase/analyze/fingerprint/insights_test.clj b/test/metabase/analyze/fingerprint/insights_test.clj
index daa03a0686e..62add072d89 100644
--- a/test/metabase/analyze/fingerprint/insights_test.clj
+++ b/test/metabase/analyze/fingerprint/insights_test.clj
@@ -252,3 +252,42 @@
:source :aggregation}])
[["2024-08-09" 10.0 "2024-08-01"]
["2024-08-10" 20.0 "2024-08-02"]]))))))
+
+(deftest ^:parallel timeseries-with-relation-semantic-type-test
+ (testing "A datetime column with Entity Key or Foreign Key semantic type should still work for timeseries (#35281)"
+ (testing "Should recognize timeseries when datetime breakout has :type/FK semantic type"
+ (is (some?
+ (transduce identity
+ (insights/insights [{:base_type :type/DateTime
+ :semantic_type :type/FK
+ :source :breakout}
+ {:base_type :type/Number}])
+ [["2024-08-09" 10.0]
+ ["2024-08-10" 20.0]]))))
+ (testing "Should recognize timeseries when datetime breakout has :type/PK semantic type"
+ (is (some?
+ (transduce identity
+ (insights/insights [{:base_type :type/DateTime
+ :semantic_type :type/PK
+ :source :breakout}
+ {:base_type :type/Number}])
+ [["2024-08-09" 10.0]
+ ["2024-08-10" 20.0]]))))
+ (testing "Non-datetime FK/PK columns should not be treated as datetimes"
+ (is (nil?
+ (transduce identity
+ (insights/insights [{:base_type :type/Integer
+ :semantic_type :type/FK
+ :source :breakout}
+ {:base_type :type/Number}])
+ [[1 10.0]
+ [2 20.0]]))))
+ (testing "Integer FK/PK columns should not be treated as numbers (they are identifiers)"
+ (is (nil?
+ (transduce identity
+ (insights/insights [{:base_type :type/DateTime
+ :source :breakout}
+ {:base_type :type/Integer
+ :semantic_type :type/FK}])
+ [["2024-08-09" 1]
+ ["2024-08-10" 2]]))))))