The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Apply temporal bucketing on expressions (#72567)
Riley Thompson430df2c3895b
diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.cljindex 5580de87561..37fc74a2012 100644--- a/src/metabase/driver/sql/query_processor.clj+++ b/src/metabase/driver/sql/query_processor.clj@@ -708,28 +708,36 @@(isa? (or effective-type base-type):type/Text))))+(defn apply-temporal-bucketing+ "Apply temporal bucketing for the `:temporal-unit` in the options of a `:field` clause; return a new HoneySQL form that+ buckets `honeysql-form` appropriately."+ [driver {:keys [temporal-unit]} honeysql-form]+ (date driver temporal-unit honeysql-form))+(defmethod ->honeysql [:sql :expression][driver [_ expression-name opts :as _clause]](let [source-table (get opts driver-api/qp.add.source-table)source-alias (get opts driver-api/qp.add.source-alias)expression-definition (driver-api/expression-with-name *inner-query* expression-name)]- (->honeysql driver (cond (= source-table driver-api/qp.add.source)- (apply h2x/identifier :field source-query-alias source-alias)-- (literal-text-value? expression-definition)- [::expression-literal-text-value expression-definition]-- ;; Handle raw string literals (not wrapped in :value) - needed for- ;; expression definitions that are just string literals, e.g. from- ;; custom columns like `"fixed literal string"`. Without this,- ;; the string becomes a parameter placeholder without type info,- ;; which some databases (like H2) can't handle.- (string? expression-definition)- [::expression-literal-text-value- [:value expression-definition {:base_type :type/Text}]]-- :else- expression-definition))))+ (cond->>+ (->honeysql driver (cond (= source-table driver-api/qp.add.source)+ (apply h2x/identifier :field source-query-alias source-alias)++ (literal-text-value? expression-definition)+ [::expression-literal-text-value expression-definition]++ ;; Handle raw string literals (not wrapped in :value) - needed for+ ;; expression definitions that are just string literals, e.g. from+ ;; custom columns like `"fixed literal string"`. Without this,+ ;; the string becomes a parameter placeholder without type info,+ ;; which some databases (like H2) can't handle.+ (string? expression-definition)+ [::expression-literal-text-value+ [:value expression-definition {:base_type :type/Text}]]++ :else+ expression-definition))+ (:temporal-unit opts) (apply-temporal-bucketing driver opts))))(defmethod ->honeysql [:sql :now][driver _clause]@@ -829,12 +837,6 @@[_driver identifier]identifier)-(defn apply-temporal-bucketing- "Apply temporal bucketing for the `:temporal-unit` in the options of a `:field` clause; return a new HoneySQL form that- buckets `honeysql-form` appropriately."- [driver {:keys [temporal-unit]} honeysql-form]- (date driver temporal-unit honeysql-form))-(defn apply-binning"Apply `:binning` options from a `:field` clause; return a new HoneySQL form that bins `honeysql-form`appropriately."diff --git a/test/metabase/query_processor/expressions_test.clj b/test/metabase/query_processor/expressions_test.cljindex b3725f8a56e..835ffea6235 100644--- a/test/metabase/query_processor/expressions_test.clj+++ b/test/metabase/query_processor/expressions_test.clj@@ -5,6 +5,8 @@[java-time.api :as t][medley.core :as m][metabase.driver :as driver]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata][metabase.query-processor.test :as qp][metabase.test :as mt][metabase.util :as u]@@ -1185,3 +1187,33 @@(mt/formatted-rows[u.date/temporal-str->iso8601-str int int](qp/process-query query))))))))))++(deftest ^:parallel expression-as-bucketed-join-key-test+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql :+features [:expressions :left-join]})+ (testing "can join a custom expression that uses temporal bucketing (#59614)"+ (let [mp (mt/metadata-provider)+ orders-table (lib.metadata/table mp (mt/id :orders))+ products-table (lib.metadata/table mp (mt/id :products))+ orders-id (lib.metadata/field mp (mt/id :orders :id))+ orders-created (lib.metadata/field mp (mt/id :orders :created_at))+ products-id (lib.metadata/field mp (mt/id :products :id))+ products-created (lib.metadata/field mp (mt/id :products :created_at))+ q (-> (lib/query mp orders-table)+ (lib/expression "X" (lib/datetime-add orders-created 1 :day)))+ x-month (-> (lib/expression-ref q "X")+ (lib/with-temporal-bucket :month))+ products-month (lib/with-temporal-bucket products-created :month)+ query (-> q+ (lib/join (-> (lib/join-clause products-table+ [(lib/= x-month products-month)])+ (lib/with-join-fields [products-id products-created])))+ (lib/with-fields [orders-id (lib/expression-ref q "X")])+ (lib/order-by orders-id :asc)+ (lib/order-by products-id :asc)+ (lib/limit 3))]+ (is (= [[1 "2019-02-12T21:40:27Z" 9 "2019-02-07T08:26:25Z"]+ [1 "2019-02-12T21:40:27Z" 137 "2019-02-16T22:36:43Z"]+ [1 "2019-02-12T21:40:27Z" 188 "2019-02-07T19:03:43Z"]]+ (mt/formatted-rows+ [int u.date/temporal-str->iso8601-str int u.date/temporal-str->iso8601-str]+ (qp/process-query query))))))))