SEPURI-SAI-KRISHNA opened a new pull request, #59139:
URL: https://github.com/apache/spark/pull/59139

   ### What changes were proposed in this pull request?
   
   This PR makes `from_unixtime` fail with `DATETIME_OVERFLOW` when the number 
of seconds cannot be converted to microseconds without overflowing a `Long`, 
instead of silently wrapping around. It adds `DateTimeUtils.secondsToMicros`, 
which uses `Math.multiplyExact` and raises the same error as 
`timestamp_seconds`, and uses it in the interpreted path and in both code 
generation paths.
   
   ### Why are the changes needed?
   
   The conversion used a plain `Long` multiplication, so for large inputs it 
returned a wrong but plausible looking timestamp, with ANSI mode on and off 
(session time zone UTC):
   
   ```sql
   SELECT from_unixtime(9223372036854);         -- +294247-01-10 04:00:54 
(largest value that fits)
   SELECT from_unixtime(9223372036855);         -- -290308-12-21 19:59:05
   SELECT from_unixtime(9223372036854775807);   -- 1969-12-31 23:59:59
   ```
   
   `timestamp_seconds` performs the same conversion and already fails on these 
inputs:
   
   ```sql
   SELECT timestamp_seconds(9223372036855);
   -- [DATETIME_OVERFLOW] Datetime operation overflow: create a TIMESTAMP from 
9223372036855L seconds since the epoch. SQLSTATE: 22008
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `from_unixtime` now fails with the error above for inputs whose value 
in microseconds does not fit in a `Long`, instead of returning a wrong 
timestamp. Results for other inputs are unchanged.
   
   ### How was this patch tested?
   
   - New test in `DateExpressionsSuite` for the largest value that still fits 
and for overflowing values in both directions, with a literal and a non-literal 
format (the two code generation paths), in both interpreted and codegen mode. 
The new test fails without this change.
   - Ran the queries above on a table (not constant folded) with whole-stage 
codegen and interpreted mode, with ANSI mode on and off.
   - Ran `DateExpressionsSuite`, `DateTimeUtilsSuite`, `DateFunctionsSuite`, 
and the date, timestamp and unix tests in `SQLQueryTestSuite`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   


-- 
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