Skip to content

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

Only rewrite temporal unit on order bys and breakouts (#80124)

Riley Thompsone58b5a46fb57
diff --git a/src/metabase/query_processor/middleware/parameters/mbql.clj b/src/metabase/query_processor/middleware/parameters/mbql.clj
index 40675856a8e..7d3b3a4be40 100644
--- a/src/metabase/query_processor/middleware/parameters/mbql.clj
+++ b/src/metabase/query_processor/middleware/parameters/mbql.clj
@@ -116,11 +116,19 @@
target-column :- [:or ::lib.schema.id/field :string]
temporal-unit :- ::lib.schema.temporal-bucketing/unit
new-unit :- ::lib.schema.temporal-bucketing/unit]
- (match/replace stage
- [#{:field :expression}
- (opts :guard (= (:temporal-unit opts) temporal-unit))
- (id-or-name :guard (= id-or-name target-column))]
- (lib/with-temporal-bucket &match new-unit)))
+ ;; only rewrite clauses in :breakout and :order-by, clauses elsewhere
+ ;; (like a :join) should keep their original bucketing (#80098)
+ (let [update-clauses (fn [clauses]
+ (match/replace clauses
+ [#{:field :expression}
+ (opts :guard (= (:temporal-unit opts) temporal-unit))
+ (id-or-name :guard (= id-or-name target-column))]
+ (lib/with-temporal-bucket &match new-unit)))]
+ (reduce (fn [stage k]
+ (cond-> stage
+ (seq (get stage k)) (update k update-clauses)))
+ stage
+ [:breakout :order-by])))
(mu/defn- update-breakout-unit :- ::lib.schema/stage
[metadata-providerable :- ::lib.schema.metadata/metadata-providerable
diff --git a/test/metabase/query_processor/middleware/parameters/mbql_test.clj b/test/metabase/query_processor/middleware/parameters/mbql_test.clj
index 78fea95b062..a2571c45ae1 100644
--- a/test/metabase/query_processor/middleware/parameters/mbql_test.clj
+++ b/test/metabase/query_processor/middleware/parameters/mbql_test.clj
@@ -599,6 +599,29 @@
[:field {:temporal-unit :week} (meta/id :orders :created-at)]]}]}
(expand-parameters query))))))
+(deftest ^:parallel temporal-unit-param-does-not-rewrite-join-condition-test
+ (testing "a temporal-unit param only updates breakouts and order bys, not join conditions (#80098)"
+ (let [mp meta/metadata-provider
+ created-at (meta/field-metadata :orders :created-at)
+ query (-> (lib/query mp (meta/table-metadata :orders))
+ (lib/join (lib/join-clause (meta/table-metadata :products)
+ [(lib/= (lib/with-temporal-bucket created-at :month)
+ (lib/with-temporal-bucket (meta/field-metadata :products :created-at) :month))]))
+ (lib/aggregate (lib/count))
+ (lib/breakout (lib/with-temporal-bucket created-at :month))
+ (lib/update-query-stage
+ 0 assoc :parameters
+ [{:type :temporal-unit
+ :value "year"
+ :target [:dimension
+ (lib.convert/->legacy-MBQL (lib/ref (lib/with-temporal-bucket created-at :month)))
+ {}]}]))]
+ (is (=? {:stages [{:breakout [[:field {:temporal-unit :year} (meta/id :orders :created-at)]]
+ :joins [{:conditions [[:= {}
+ [:field {:temporal-unit :month} (meta/id :orders :created-at)]
+ [:field {:temporal-unit :month} (meta/id :products :created-at)]]]}]}]}
+ (expand-parameters query))))))
+
(deftest ^:parallel bigint-number-operators-test
(testing "number operator params preserve type/BigInteger values without collapsing to a double (#5816)"
(let [mp meta/metadata-provider