Skip to content

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

Use locale independent formatting to get the day of the week (#71620)

metamben5f5d472fe704
diff --git a/modules/drivers/oracle/src/metabase/driver/oracle.clj b/modules/drivers/oracle/src/metabase/driver/oracle.clj
index 9fcb0a808b2..13d2ed21b35 100644
--- a/modules/drivers/oracle/src/metabase/driver/oracle.clj
+++ b/modules/drivers/oracle/src/metabase/driver/oracle.clj
@@ -283,12 +283,14 @@
2)
3))
-;; subtract number of days between today and first day of week, then add one since first day of week = 1
+;; Use locale-independent Julian day arithmetic instead of TO_CHAR(date, 'D') which depends on NLS_TERRITORY (#57794).
+;; MOD(TO_NUMBER(TO_CHAR(date, 'J')), 7) gives: Mon=0, Tue=1, Wed=2, Thu=3, Fri=4, Sat=5, Sun=6
+;; We add 1 and remap so Sunday=1 to match the AMERICA convention that db-start-of-week :sunday expects.
(defmethod sql.qp/date [:oracle :day-of-week]
[driver _ v]
(sql.qp/adjust-day-of-week
driver
- (h2x/->integer [:to_char v (h2x/literal :d)])
+ (h2x/+ [::mod (h2x/+ [:to_number [:to_char v (h2x/literal :J)]] [:inline 1]) [:inline 7]] [:inline 1])
(driver.common/start-of-week-offset driver)
(fn mod-fn [& args]
(into [::mod] args))))
diff --git a/modules/drivers/oracle/test/metabase/driver/oracle_test.clj b/modules/drivers/oracle/test/metabase/driver/oracle_test.clj
index 9cf0c3d4fa1..317f9a64858 100644
--- a/modules/drivers/oracle/test/metabase/driver/oracle_test.clj
+++ b/modules/drivers/oracle/test/metabase/driver/oracle_test.clj
@@ -11,12 +11,14 @@
[metabase.driver :as driver]
[metabase.driver.oracle :as oracle]
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
+ [metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]
[metabase.driver.sql-jdbc.sync :as sql-jdbc.sync]
[metabase.driver.sql.query-processor :as sql.qp]
[metabase.driver.util :as driver.u]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.test-util :as lib.tu]
+ [metabase.lib.test-util.notebook-helpers :as lib.tu.notebook]
[metabase.premium-features.core :as premium-features]
[metabase.query-processor.compile :as qp.compile]
[metabase.query-processor.order-by-test :as qp-test.order-by-test]
@@ -687,3 +689,49 @@
(lib/breakout (lib/with-temporal-bucket orders-created-at :year))
(lib/breakout products-category))]
(is (= 20 (count (mt/rows (qp/process-query query))))))))
+
+(defn- do-with-nls-territory
+ "Execute `thunk` with all Oracle connections using the given `nls-territory` (e.g. \"ARGENTINA\").
+ Wraps `do-with-connection-with-options` to run ALTER SESSION on each connection."
+ [nls-territory thunk]
+ (let [orig-method (get-method sql-jdbc.execute/do-with-connection-with-options :oracle)]
+ (try
+ (defmethod sql-jdbc.execute/do-with-connection-with-options :oracle
+ [driver db-or-id-or-spec options f]
+ (orig-method driver db-or-id-or-spec options
+ (fn [^java.sql.Connection conn]
+ (.execute (.createStatement conn)
+ (str "ALTER SESSION SET NLS_TERRITORY = '" nls-territory "'"))
+ (f conn))))
+ (thunk)
+ (finally
+ (defmethod sql-jdbc.execute/do-with-connection-with-options :oracle
+ [driver db-or-id-or-spec options f]
+ (orig-method driver db-or-id-or-spec options f))))))
+
+(deftest day-of-week-nls-territory-test
+ (testing "day-of-week extraction should respect NLS_TERRITORY setting (#57794)"
+ (mt/test-driver :oracle
+ (mt/dataset date-cols-with-datetime-values
+ (do-with-nls-territory
+ "ARGENTINA"
+ (fn []
+ ;; 2024-11-05 is a Tuesday.
+ ;; With start-of-week = sunday, Tuesday should be day-of-week 3.
+ ;; The bug: Oracle's TO_CHAR(date, 'D') returns day numbers relative to NLS_TERRITORY.
+ ;; With ARGENTINA (Monday=1), TO_CHAR returns 2 for Tuesday, but the driver assumes
+ ;; Sunday=1 (AMERICA convention), so it incorrectly reports Tuesday as day 2 instead of 3.
+ (mt/with-temporary-setting-values [start-of-week :sunday]
+ (let [mp (mt/metadata-provider)
+ base (lib/query mp (lib.metadata/table mp (mt/id :dates_with_time)))
+ date (lib.tu.notebook/find-col-with-spec base (lib/filterable-columns base)
+ {:is-main-group true} "Date With Time")
+ query (-> base
+ (lib/aggregate (lib/count))
+ (lib.tu.notebook/add-breakout
+ {:is-main-group true} "Date With Time"
+ {:col-fn #(lib/with-temporal-bucket % :day-of-week)})
+ (lib/filter (lib/= (lib/with-temporal-bucket date :day) "2024-11-05")))]
+ (mt/with-native-query-testing-context query
+ (is (= [[3 1]]
+ (mt/formatted-rows [int int] (qp/process-query query)))))))))))))