From f8caa7ac0d487c0088ea557226bbc9da61157b97 Mon Sep 17 00:00:00 2001
From: metamben <103100869+metamben@users.noreply.github.com>
Date: Mon, 5 Feb 2024 18:22:14 +0300
Subject: [PATCH] Prevent categorizing extraction of/truncation to year as
 temporal-extraction (#38358)

* Prevent categorizing extraction of/truncation to year as temporal-extraction

Fixes #37172.
---
 src/metabase/lib/metadata/calculation.cljc  |  6 ++++--
 src/metabase/lib/temporal_bucket.cljc       |  7 +++++--
 test/metabase/lib/temporal_bucket_test.cljc | 11 ++++++++++-
 3 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/src/metabase/lib/metadata/calculation.cljc b/src/metabase/lib/metadata/calculation.cljc
index bebc1cd9525..3d4ada086c6 100644
--- a/src/metabase/lib/metadata/calculation.cljc
+++ b/src/metabase/lib/metadata/calculation.cljc
@@ -370,7 +370,7 @@
        (when-let [inner-metadata (cond
                                    (integer? table-id) (lib.metadata/table query table-id)
                                    (string? table-id)  (lib.metadata/card
-                                                         query (lib.util/legacy-string-table-id->card-id table-id)))]
+                                                        query (lib.util/legacy-string-table-id->card-id table-id)))]
          {:table (display-info query stage-number inner-metadata)}))
      (when-let [source (:lib/source x-metadata)]
        {:is-from-previous-stage (= source :source/previous-stage)
@@ -382,7 +382,9 @@
      (when-some [selected (:selected? x-metadata)]
        {:selected selected})
      (when-let [temporal-unit ((some-fn :metabase.lib.field/temporal-unit :temporal-unit) x-metadata)]
-       {:is-temporal-extraction (contains? lib.schema.temporal-bucketing/datetime-extraction-units temporal-unit)})
+       {:is-temporal-extraction
+        (and (contains? lib.schema.temporal-bucketing/datetime-extraction-units temporal-unit)
+             (not (contains? lib.schema.temporal-bucketing/datetime-truncation-units temporal-unit)))})
      (select-keys x-metadata [:breakout-position :order-by-position :filter-positions]))))
 
 (defmethod display-info-method :default
diff --git a/src/metabase/lib/temporal_bucket.cljc b/src/metabase/lib/temporal_bucket.cljc
index 281d4d1dc2b..8726cacbc6e 100644
--- a/src/metabase/lib/temporal_bucket.cljc
+++ b/src/metabase/lib/temporal_bucket.cljc
@@ -199,8 +199,11 @@
   [query stage-number option]
   (merge {:display-name (lib.metadata.calculation/display-name query stage-number option)
           :short-name (u/qualified-name (raw-temporal-bucket option))
-          :is-temporal-extraction (contains? lib.schema.temporal-bucketing/datetime-extraction-units
-                                             (raw-temporal-bucket option))}
+          :is-temporal-extraction (let [bucket (raw-temporal-bucket option)]
+                                    (and (contains? lib.schema.temporal-bucketing/datetime-extraction-units
+                                                    bucket)
+                                         (not (contains? lib.schema.temporal-bucketing/datetime-truncation-units
+                                                         bucket))))}
          (select-keys option [:default :selected])))
 
 (defmulti available-temporal-buckets-method
diff --git a/test/metabase/lib/temporal_bucket_test.cljc b/test/metabase/lib/temporal_bucket_test.cljc
index 4d35a201142..fc63f226ac0 100644
--- a/test/metabase/lib/temporal_bucket_test.cljc
+++ b/test/metabase/lib/temporal_bucket_test.cljc
@@ -182,7 +182,7 @@
             "week"            false
             "month"           false
             "quarter"         false
-            "year"            true
+            "year"            false
             "minute-of-hour"  true
             "hour-of-day"     true
             "day-of-week"     true
@@ -195,3 +195,12 @@
                  (comp (map #(lib/display-info query -1 %))
                        (map (juxt :short-name :is-temporal-extraction)))
                  (lib.temporal-bucket/available-temporal-buckets query (meta/field-metadata :products :created-at)))))))
+
+(deftest ^:parallel source-card-temporal-extraction-test
+  (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))
+                  (lib/breakout (lib/with-temporal-bucket (meta/field-metadata :orders :created-at) :year)))]
+    (is (=? {:display-name "Created At: Year"
+             :is-temporal-extraction false}
+            (->> (lib/breakouts query)
+                 first
+                 (lib/display-info query -1))))))
-- 
GitLab