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
Preserve nulls in day-of-week calculation (#76180)
metamben630fc8df9390
diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.cljindex 9958478c592..6124bb908e4 100644--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj@@ -294,13 +294,12 @@;; with something using the new system;; Issue: https://github.com/metabase/metabase/issues/39386(defn- weekday- "Wrapper around (date-part :weekday ...) to account for potentially varying @@DATEFIRST"+ "Wrapper around (date-part :weekday ...) to account for potentially varying @@DATEFIRST.+ `((dow + @@DATEFIRST - 1) % 7) + 1` shifts the day-of-week into the range [1, 7] and propagates NULL."[expr]- [:coalesce- [:nullif- (h2x/mod (h2x/+ (date-part :weekday expr) [:raw "@@DATEFIRST"]) [:inline 7])- [:inline 0]]- [:inline 7]])+ (h2x/+ (h2x/mod (h2x/- (h2x/+ (date-part :weekday expr) [:raw "@@DATEFIRST"]) [:inline 1])+ [:inline 7])+ [:inline 1]))(defmethod sql.qp/date [:sqlserver :day][_driver _unit expr]diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.cljindex 507a4553e8c..734f463e492 100644--- a/src/metabase/driver/sql/query_processor.clj+++ b/src/metabase/driver/sql/query_processor.clj@@ -528,13 +528,13 @@(inline? offset) (recur driver day-of-week-honeysql-expr (second offset) mod-fn)(zero? offset) day-of-week-honeysql-expr(neg? offset) (recur driver day-of-week-honeysql-expr (+ offset 7) mod-fn)- :else (-> [:coalesce- [:nullif- (mod-fn (h2x/+ day-of-week-honeysql-expr offset) (inline-num 7))- [:inline 0]]- [:inline 7]]- (h2x/with-database-type-info (or (h2x/database-type day-of-week-honeysql-expr)- "integer"))))))+ ;; `((dow + offset - 1) % 7) + 1` shifts the day-of-week into the range [1, 7] and propagates NULL.+ :else (let [shifted (if (= offset 1)+ day-of-week-honeysql-expr+ (h2x/+ day-of-week-honeysql-expr (dec offset)))]+ (-> (h2x/+ (mod-fn shifted (inline-num 7)) (inline-num 1))+ (h2x/with-database-type-info (or (h2x/database-type day-of-week-honeysql-expr)+ "integer")))))))(defmulti quote-style"Return the dialect that should be used by Honey SQL 2 when building a SQL statement. Defaults to `:ansi`, but otherdiff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.cljindex 01d4c3f4209..95e47ac1298 100644--- a/test/metabase/driver/mysql_test.clj+++ b/test/metabase/driver/mysql_test.clj@@ -155,10 +155,7 @@" ("" ("" DAYOFYEAR(weeks.d) - ("- " 8 - COALESCE("- " NULLIF((DAYOFWEEK(MAKEDATE(YEAR(weeks.d), 1)) + 5) % 7, 0),"- " 7"- " )"+ " 8 - (((DAYOFWEEK(MAKEDATE(YEAR(weeks.d), 1)) + 4) % 7) + 1)"" )"" ) / 7.0"" )"diff --git a/test/metabase/query_processor/date_time_zone_functions_test.clj b/test/metabase/query_processor/date_time_zone_functions_test.cljindex 34d3a907929..5442ea4b84f 100644--- a/test/metabase/query_processor/date_time_zone_functions_test.clj+++ b/test/metabase/query_processor/date_time_zone_functions_test.clj@@ -373,6 +373,37 @@(is (= [1 1 2 2 2 2 2 2 2 1](test-extract-week (mt/id :weeks :d) :instance)))))))))+(deftest day-of-week-preserves-null-test+ (testing "day-of-week breakout should preserve NULL inputs, not bucket them into day 7 (#76107)"+ ;; The bug only manifests when the offset between `start-of-week` and the driver's `db-start-of-week`+ ;; is non-zero. Iterating over all 7 settings guarantees we hit a non-zero offset on every driver.+ (mt/test-drivers (mt/normal-drivers-with-feature :temporal-extract :expressions)+ (mt/dataset daily-bird-counts+ (let [mp (mt/metadata-provider)+ bird-count (lib.metadata/table mp (mt/id :bird-count))+ date-field (lib.metadata/field mp (mt/id :bird-count :date))+ count-field (lib.metadata/field mp (mt/id :bird-count :count))+ ;; `nullable_date` = `date` when `count` is not NULL, otherwise NULL.+ ;; The `daily-bird-counts` dataset has 5 rows with NULL `count`.+ base (-> (lib/query mp bird-count)+ (lib/expression "nullable_date"+ (lib/case [[(lib/not-null count-field) date-field]])))+ expr-col (->> (lib/breakoutable-columns base)+ (filter (comp #{:source/expressions} :lib/source))+ first)+ query (-> base+ (lib/aggregate (lib/count))+ (lib/breakout (lib/with-temporal-bucket expr-col :day-of-week)))]+ (doseq [start-of-week [:sunday :monday :tuesday :wednesday :thursday :friday :saturday]]+ (mt/with-temporary-setting-values [start-of-week start-of-week]+ (testing (str "start-of-week = " start-of-week)+ (let [rows (mt/formatted-rows [identity int] (qp/process-query query))]+ (testing (str "rows = " (pr-str rows))+ (is (some (comp nil? first) rows)+ "NULL date should produce a NULL day-of-week bucket")+ (is (= 5 (some (fn [[dow cnt]] (when (nil? dow) cnt)) rows))+ "the NULL day-of-week bucket should hold the 5 rows whose date is NULL")))))))))))+;;; +----------------------------------------------------------------------------------------------------------------+;;; | Date arithmetics tests |;;; +----------------------------------------------------------------------------------------------------------------+