You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "parthchandra (via GitHub)" <gi...@apache.org> on 2026/04/22 22:12:11 UTC

[PR] feat: Add TimestampNTZType support for unix_timestamp [datafusion-comet]

parthchandra opened a new pull request, #4039:
URL: https://github.com/apache/datafusion-comet/pull/4039

   ## Summary                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                      
     Add TimestampNTZ support for temporal expressions and fix DST offset calculation in `date_trunc`.                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                      
     **Key changes:**                                                                                                                                                                                                                                                                                                                                 
     - Add TimestampNTZ support to `unix_timestamp` (no timezone conversion — simply divides microseconds by 1,000,000)                                                                                                                                                                                                                               
     - Fix DST offset calculation in `as_micros_from_unix_epoch_utc` so `date_trunc` produces correct results when truncation crosses DST boundaries (e.g., December/PST to October/PDT)                                                                                                                                                              
     - Add TimestampNTZ tests for `hour`/`minute`/`second`, `date_trunc`, and `date_format`                                                                                                                                                                                                                                                           
     - Document chrono-tz limitation: DST calculations may be inaccurate for dates beyond ~2100                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                      
     **Not included in this PR:** TimestampNTZ cast support (to/from String, Date, Timestamp, Long) — that will come in a separate PR.                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                      
     ## Test plan                                                                                                                                                                                                                                                                                                                                     
                                                         
     - [ ] Run temporal expression tests: `mvn test -pl spark -Pspark-4.0 -Dsuites="org.apache.comet.CometTemporalExpressionSuite"`                                                                                                                                                                                                                   
     - [ ] Rust compilation: `cargo check -p datafusion-comet-spark-expr`
     - [ ] Full test suite                   


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: Add TimestampNTZType support for unix_timestamp [datafusion-comet]

Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on PR #4039:
URL: https://github.com/apache/datafusion-comet/pull/4039#issuecomment-4315881049

   Merged. Thank you @andygrove , @mbutrovich 


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: Add TimestampNTZType support for unix_timestamp [datafusion-comet]

Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on code in PR #4039:
URL: https://github.com/apache/datafusion-comet/pull/4039#discussion_r3134142797


##########
native/spark-expr/src/kernels/temporal.rs:
##########
@@ -153,10 +156,30 @@ where
     Ok(())
 }
 
-// Apply the Tz to the Naive Date Time,,convert to UTC, and return as microseconds in Unix epoch
+// Apply the Tz to the Naive Date Time, convert to UTC, and return as microseconds in Unix epoch.
+// This function re-interprets the local datetime in the timezone to ensure the correct DST offset
+// is used for the target date (not the original date's offset). This is important when truncation
+// changes the date to a different DST period (e.g., from December/PST to October/PDT).
+//
+// Note: For far-future dates (approximately beyond year 2100), chrono-tz may not accurately
+// calculate DST transitions, which can result in incorrect offsets. See the compatibility
+// guide for more information.
 #[inline]
 fn as_micros_from_unix_epoch_utc(dt: Option<DateTime<Tz>>) -> i64 {

Review Comment:
   #4040 is merged. Rebased to make sure tests all pass



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: Add TimestampNTZType support for unix_timestamp [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #4039:
URL: https://github.com/apache/datafusion-comet/pull/4039#discussion_r3127477597


##########
spark/src/main/scala/org/apache/comet/serde/datetime.scala:
##########
@@ -295,11 +295,9 @@ object CometSecond extends CometExpressionSerde[Second] {
 object CometUnixTimestamp extends CometExpressionSerde[UnixTimestamp] {
 
   private def isSupportedInputType(expr: UnixTimestamp): Boolean = {
-    // Note: TimestampNTZType is not supported because Comet incorrectly applies
-    // timezone conversion to TimestampNTZ values. TimestampNTZ stores local time
-    // without timezone, so no conversion should be applied.
     expr.children.head.dataType match {
       case TimestampType | DateType => true
+      case dt if dt.typeName == "timestamp_ntz" => true

Review Comment:
   We can just just match on `TimestampNTZType` rather than use `typeName`. I know we had to do this in the past, but Spark 3.4 and later have `TimestampNTZType`



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: Add TimestampNTZType support for unix_timestamp [datafusion-comet]

Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on code in PR #4039:
URL: https://github.com/apache/datafusion-comet/pull/4039#discussion_r3134148694


##########
spark/src/main/scala/org/apache/comet/serde/datetime.scala:
##########
@@ -295,11 +295,9 @@ object CometSecond extends CometExpressionSerde[Second] {
 object CometUnixTimestamp extends CometExpressionSerde[UnixTimestamp] {
 
   private def isSupportedInputType(expr: UnixTimestamp): Boolean = {
-    // Note: TimestampNTZType is not supported because Comet incorrectly applies
-    // timezone conversion to TimestampNTZ values. TimestampNTZ stores local time
-    // without timezone, so no conversion should be applied.
     expr.children.head.dataType match {
       case TimestampType | DateType => true
+      case dt if dt.typeName == "timestamp_ntz" => true

Review Comment:
   Good suggestion. Changed.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: Add TimestampNTZType support for unix_timestamp [datafusion-comet]

Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra merged PR #4039:
URL: https://github.com/apache/datafusion-comet/pull/4039


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: Add TimestampNTZType support for unix_timestamp [datafusion-comet]

Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on PR #4039:
URL: https://github.com/apache/datafusion-comet/pull/4039#issuecomment-4308537247

   @andygrove rebased, updated, and ready for review.  (Original PR: https://github.com/apache/datafusion-comet/pull/3253)


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: Add TimestampNTZType support for unix_timestamp [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #4039:
URL: https://github.com/apache/datafusion-comet/pull/4039#discussion_r3127530731


##########
native/spark-expr/src/kernels/temporal.rs:
##########
@@ -153,10 +156,30 @@ where
     Ok(())
 }
 
-// Apply the Tz to the Naive Date Time,,convert to UTC, and return as microseconds in Unix epoch
+// Apply the Tz to the Naive Date Time, convert to UTC, and return as microseconds in Unix epoch.
+// This function re-interprets the local datetime in the timezone to ensure the correct DST offset
+// is used for the target date (not the original date's offset). This is important when truncation
+// changes the date to a different DST period (e.g., from December/PST to October/PDT).
+//
+// Note: For far-future dates (approximately beyond year 2100), chrono-tz may not accurately
+// calculate DST transitions, which can result in incorrect offsets. See the compatibility
+// guide for more information.
 #[inline]
 fn as_micros_from_unix_epoch_utc(dt: Option<DateTime<Tz>>) -> i64 {

Review Comment:
   I think the changes here may introduce a regression in DST handling. 
   
   Could we merge https://github.com/apache/datafusion-comet/pull/4040 first to add new regression test and make sure they still pass with this PR?



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: Add TimestampNTZType support for unix_timestamp [datafusion-comet]

Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on code in PR #4039:
URL: https://github.com/apache/datafusion-comet/pull/4039#discussion_r3127861269


##########
native/spark-expr/src/kernels/temporal.rs:
##########
@@ -153,10 +156,30 @@ where
     Ok(())
 }
 
-// Apply the Tz to the Naive Date Time,,convert to UTC, and return as microseconds in Unix epoch
+// Apply the Tz to the Naive Date Time, convert to UTC, and return as microseconds in Unix epoch.
+// This function re-interprets the local datetime in the timezone to ensure the correct DST offset
+// is used for the target date (not the original date's offset). This is important when truncation
+// changes the date to a different DST period (e.g., from December/PST to October/PDT).
+//
+// Note: For far-future dates (approximately beyond year 2100), chrono-tz may not accurately
+// calculate DST transitions, which can result in incorrect offsets. See the compatibility
+// guide for more information.
 #[inline]
 fn as_micros_from_unix_epoch_utc(dt: Option<DateTime<Tz>>) -> i64 {

Review Comment:
   Yes, let's merge #4040 first



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: Add TimestampNTZType support for unix_timestamp [datafusion-comet]

Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on PR #4039:
URL: https://github.com/apache/datafusion-comet/pull/4039#issuecomment-4314258448

   Thank you @mbutrovich. Excellent comments!
   > 
   > **`div_floor` inconsistency in `unix_timestamp`**
   > 
   > The NTZ path (and the existing Timestamp path) uses `micros / MICROS_PER_SECOND` for the no-null fast path but `div_floor(micros, MICROS_PER_SECOND)` for the nullable path. For negative timestamps (pre-epoch), these give different results: `-1_500_000 / 1_000_000 = -1` vs `div_floor = -2`. Spark uses `Math.floorDiv` in all cases. This is pre-existing but worth fixing while you're in here. Just use `div_floor` in both branches.
   > 
   
   Changed both the NTZ and Timestamp no-null fast paths in unix_timestamp.rs to use div_floor(micros, MICROS_PER_SECOND) instead of micros / MICROS_PER_SECOND, matching Spark's Math.floorDiv for pre-epoch negative timestamps.  
   
   
   > **Code duplication in `timestamp_trunc_array_fmt_helper`**
   > 
   > The NTZ branch in the macro repeats the same format-to-truncation-function mapping that `timestamp_trunc_ntz` already has. Could the macro's NTZ branch delegate to `timestamp_trunc_ntz` directly, or at least share a format lookup?
   
   Extracted ntz_trunc_fn_for_format() that resolves a format string to the corresponding NaiveDateTime truncation function. Both timestamp_trunc_ntz and the macro's NTZ branch now use it


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org