spark-test commented on code in PR #60130:
URL: https://github.com/apache/spark/pull/60130#discussion_r4510196068


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala:
##########
@@ -575,7 +591,9 @@ object DateTimeUtils extends SparkDateTimeUtils {
    */
   def truncDate(days: Int, level: Int): Int = {
     level match {
-      case TRUNC_TO_WEEK => getNextDateForDayOfWeek(days - 7, MONDAY)
+      // `days - 7L` so the subtraction does not wrap. Near the minimum DATE, 
trunc still wraps
+      // (this `.toInt`, and the Int arithmetic of the other levels); that is 
a trunc follow-up.
+      case TRUNC_TO_WEEK => nextDateForDayOfWeek(days - 7L, MONDAY).toInt

Review Comment:
   **Non-blocking (P2):** This narrows the `Long` week start without the range 
check that `getNextDateForDayOfWeek` now applies. For `-5877641-06-23` through 
`-5877641-06-28`, the Monday of the week is `Int.MinValue - 1`, so `.toInt` 
wraps it to `Int.MaxValue`, and `trunc(date'-5877641-06-23', 'WEEK')` silently 
returns `+5881580-07-11` in both ANSI modes. The `MONTH` and `YEAR` levels 
below still subtract in `Int` and wrap the same way whenever the month or year 
starts before the minimum `DATE`: `trunc(date'-5877641-06-23', 'MONTH')` 
returns `+5881580-06-20`, and `trunc(d, 'YEAR')` returns `+5881580-01-21` for 
every date in year `-5877641`. `QUARTER` already fails there with 
`ARITHMETIC_OVERFLOW`, and `date_trunc` on timestamps never reaches these day 
numbers.
   
   So after this PR, `next_day` and `last_day` fail with `DATETIME_OVERFLOW` on 
an unrepresentable result, while `trunc` still returns a wrong date at the 
other end of the range. Since this change already fixes `trunc` to `WEEK` from 
`-5877641-06-29` on, it could cover these inputs too rather than leave them to 
a follow-up: compute the `WEEK`, `MONTH`, and `YEAR` results in `Long`, throw a 
new `DATETIME_OVERFLOW` builder (for example, `truncate DATE '-5877641-06-23' 
to WEEK`) when the result is below `Int.MinValue`, mirroring the check in 
`getLastDayOfMonth`, and mention `trunc` in the migration-guide entry and its 
usage text. The new `trunc` test asserts only `Int.MinValue + 6` and 
`Int.MinValue + 10`, so nothing pins the six earlier dates today.
   
   **Verification:**
   - **Regression:** Show that trunc fails with DATETIME_OVERFLOW, in both ANSI 
modes and with codegen on and off, whenever the truncated date would be earlier 
than the minimum DATE: WEEK for each of the six dates from -5877641-06-23 to 
-5877641-06-28, MONTH for each date from -5877641-06-23 to -5877641-06-30, and 
YEAR for every date from -5877641-06-23 to -5877641-12-31.
   - **Behavior:** Show that trunc still returns the correct result wherever 
the truncated date is representable: the Monday -5877641-06-29 for that day and 
the later days of its week, -5877641-07-01 for MONTH of that day and the rest 
of its month, -5877640-01-01 for YEAR of any date in year -5877640, and the 
existing results for ordinary dates at every level.
   - **Compatibility:** Show that the SQL results for these trunc inputs are 
the same in the ANSI, non-ANSI, and TIMESTAMP_NTZ configurations, and that 
date_trunc on timestamps, which shares the truncation code, keeps its existing 
results.



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