alamb commented on code in PR #27732:
URL: https://github.com/apache/datafusion/pull/27732#discussion_r4760691067


##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -1262,6 +1270,121 @@ fn truncates_in_input_unit(granularity: DatePart, tz: 
Option<&Tz>) -> bool {
         || (tz.is_none() && granularity.is_fine_granularity_utc())
 }
 
+/// Whether truncating to `granularity` in the local time of `tz` never 
reverses
+/// two instants. Computed once per timezone name.
+fn local_truncation_preserves_order(
+    tz_name: &str,
+    tz: Tz,
+    granularity: DatePart,
+) -> bool {
+    static CACHE: LazyLock<Mutex<HashMap<String, LocalTruncationOrder>>> =
+        LazyLock::new(Default::default);
+    let mut cache = CACHE.lock().unwrap_or_else(PoisonError::into_inner);

Review Comment:
   Also, this global along with the proposed global state / cache in 
https://github.com/apache/datafusion/pull/27883 makes me think we should 
perhaps centralize the timezone related functionality.
   
   Perhaps we can put it in datafusion-common as a timezone module 
`src/timezone.rs`
   
   That way we could have a cached per-zone transition table and share between 
this PR and https://github.com/apache/datafusion/pull/27883
   
   



##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -1262,6 +1270,121 @@ fn truncates_in_input_unit(granularity: DatePart, tz: 
Option<&Tz>) -> bool {
         || (tz.is_none() && granularity.is_fine_granularity_utc())
 }
 
+/// Whether truncating to `granularity` in the local time of `tz` never 
reverses
+/// two instants. Computed once per timezone name.
+fn local_truncation_preserves_order(
+    tz_name: &str,
+    tz: Tz,
+    granularity: DatePart,
+) -> bool {
+    static CACHE: LazyLock<Mutex<HashMap<String, LocalTruncationOrder>>> =
+        LazyLock::new(Default::default);
+    let mut cache = CACHE.lock().unwrap_or_else(PoisonError::into_inner);
+    let order = match cache.get(tz_name) {
+        Some(order) => *order,
+        None => {
+            let order = LocalTruncationOrder::new(tz);
+            cache.insert(tz_name.to_owned(), order);
+            order
+        }
+    };
+    order.preserves(granularity)
+}
+
+/// Granularities coarser than minute whose local-time truncation keeps the 
order
+/// in one zone. Day and longer buckets outlast any offset change, so only the
+/// instants around each transition can go backwards; `hour` needs no 
transitions.

Review Comment:
   Also I had my agent test all the chrono timezones (597 of them) and all 
their transitions are more than a day
   
   FWIW it seems jiff has a API to enumerate the timezone transitions -- so if 
we ever moved that library we could remove this assumption and just enumemerate 
the transitions directly
   
   - https://docs.rs/jiff/0.2.24/jiff/tz/struct.TimeZone.html#method.following
   - https://docs.rs/jiff/0.2.24/jiff/tz/struct.TimeZone.html#method.preceding
   



##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -1262,6 +1270,121 @@ fn truncates_in_input_unit(granularity: DatePart, tz: 
Option<&Tz>) -> bool {
         || (tz.is_none() && granularity.is_fine_granularity_utc())
 }
 
+/// Whether truncating to `granularity` in the local time of `tz` never 
reverses
+/// two instants. Computed once per timezone name.
+fn local_truncation_preserves_order(
+    tz_name: &str,
+    tz: Tz,
+    granularity: DatePart,
+) -> bool {
+    static CACHE: LazyLock<Mutex<HashMap<String, LocalTruncationOrder>>> =

Review Comment:
   I think we could special case UTC and fixed timezone offsets here (`+00:00` 
etc) as they have no transitions and thus preserve granularity



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to