stevomitric opened a new pull request, #60130:
URL: https://github.com/apache/spark/pull/60130

   ### What changes were proposed in this pull request?
   
   `DateTimeUtils.getNextDateForDayOfWeek` (behind `next_day`) and 
`DateTimeUtils.getLastDayOfMonth` (behind `last_day`) now compute in `Long` and 
throw `SparkArithmeticException` with `DATETIME_OVERFLOW` when the result is 
later than the maximum `DATE`, `+5881580-07-11`, regardless of 
`spark.sql.ansi.enabled`. Previously both used plain `Int` arithmetic: the 
final sum wrapped around near `Int.MaxValue`, and in `getNextDateForDayOfWeek` 
the `dayOfWeek - 1 - startDay` term also overflowed near `Int.MinValue`, which 
returned the wrong weekday for the first six days of the `DATE` range.
   
   - Two `QueryExecutionErrors` builders in the style of the other 
`DATETIME_OVERFLOW` builders: `find the next MONDAY after DATE 
'+5881580-07-11'` and `find the last day of the month of DATE '+5881580-07-11'`.
   - The non-ANSI `next_day` paths (eval and codegen) already catch only 
`SparkIllegalArgumentException` for an invalid day of week, so the overflow 
error propagates in both modes. `NextDay` and `LastDay` are unchanged apart 
from their descriptions.
   - `trunc(d, 'WEEK')` shares the helper. It now calls the `Long` core with 
`days - 7L`, so the first week of the `DATE` range no longer wraps and the 
result is correct from the first Monday, `-5877641-06-29`, on. For the six days 
before it, whose Monday is before the minimum `DATE`, the result still wraps; 
failing there, and the `MONTH`/`QUARTER`/`YEAR` levels near the minimum, are 
left for a `trunc` follow-up.
   - `ExpressionDescription` of `next_day` and `last_day`, and a 
migration-guide entry under "Upgrading from Spark SQL 4.3 to 4.4".
   
   This is the same bug class as #59135 (SPARK-59859) for `date_add` / 
`date_sub`, which does not touch these two functions. The two PRs are 
independent; their migration-guide entries and error builders sit next to each 
other, so whichever lands second needs a trivial rebase.
   
   ### Why are the changes needed?
   
   `next_day` and `last_day` silently return a wrong date near the bounds of 
the `DATE` range, in both ANSI modes and never with an error:
   
   ```sql
   SELECT last_day(DATE'+5881580-07-11');        -- -5877641-07-12 (wrapped); 
+5881580-07-31 is past the maximum DATE
   SELECT next_day(DATE'+5881580-07-11', 'MO');  -- -5877641-06-25 (wrapped)
   SELECT next_day(DATE'-5877641-06-23', 'MO');  -- -5877641-06-25, a Thursday; 
the Monday is -5877641-06-29
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, for the last 11 days of the `DATE` range in `last_day`, the last 7 days 
in `next_day`, and the first 6 days in `next_day`. Ordinary dates are 
unchanged, and so is the ANSI behavior for an invalid day-of-week string.
   
   Before:
   
   ```
   spark-sql> SELECT last_day(DATE'+5881580-07-11'), 
next_day(DATE'+5881580-07-11', 'MO'), next_day(DATE'-5877641-06-23', 'MO');
   -5877641-07-12       -5877641-06-25  -5877641-06-25
   ```
   
   After:
   
   ```
   spark-sql> SELECT last_day(DATE'+5881580-07-11');
   [DATETIME_OVERFLOW] Datetime operation overflow: find the last day of the 
month of DATE '+5881580-07-11'. SQLSTATE: 22008
   spark-sql> SELECT next_day(DATE'+5881580-07-11', 'MO');
   [DATETIME_OVERFLOW] Datetime operation overflow: find the next MONDAY after 
DATE '+5881580-07-11'. SQLSTATE: 22008
   spark-sql> SELECT next_day(DATE'-5877641-06-23', 'MO');
   -5877641-06-29
   ```
   
   ### How was this patch tested?
   
   - New tests in `DateTimeUtilsSuite` (both helpers and `trunc` to `WEEK` at 
the bounds) and `DateExpressionsSuite` (`last_day`; `next_day` with 
`failOnError` on and off and a foldable and a non-foldable day of week, 
interpreted and codegen). All five fail without the fix.
   - New `date.sql` golden cases for the corrected minimum-side values and the 
overflows, regenerated for ANSI and non-ANSI.
   - `catalyst/testOnly *DateTimeUtilsSuite *DateExpressionsSuite`, 
`sql/testOnly *SQLQueryTestSuite -- -z date.sql`, `sql/testOnly 
*DateFunctionsSuite`, and scalastyle pass.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Fable 5.1)
   


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