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

   ### What changes were proposed in this pull request?
   
   Under ANSI mode, dividing the minimum ANSI interval by `-1` overflows. 
`DivideYMInterval` and `DivideDTInterval` detect that case in both of their 
evaluation paths, but they raise different errors:
   
   - **Interpreted** (`IntervalDivide.checkDivideOverflow`): 
`INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION`, SQLSTATE 22015, suggesting 
`try_divide`. SPARK-50226 changed this path to the interval error class.
   - **Generated code** (`doGenCode`): `ARITHMETIC_OVERFLOW`, SQLSTATE 22003, 
"Overflow in integral divide", through 
`QueryExecutionErrors.overflowInIntegralDivideError`. SPARK-50226 did not 
change this path.
   
   This PR makes the generated code of both expressions raise 
`QueryExecutionErrors.withSuggestionIntervalArithmeticOverflowError("try_divide",
 ...)`, the same error as the interpreted path, with the same query context.
   
   ### Why are the changes needed?
   
   The error class and SQLSTATE a user sees should not depend on whether 
whole-stage codegen ran. On Spark 4.2.0, with `spark.sql.ansi.enabled=true`:
   
   ```sql
   CREATE TABLE t USING parquet AS SELECT INTERVAL '-106751991 04:00:54.775808' 
DAY TO SECOND AS i;
   SELECT i / -1 FROM t;
   -- codegen:     [ARITHMETIC_OVERFLOW] Overflow in integral divide. ... 
SQLSTATE: 22003
   -- interpreted: [INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION] Integer 
overflow while operating
   --              with intervals. Use `try_divide` to tolerate overflow and 
return NULL instead.
   --              SQLSTATE: 22015
   ```
   
   The interpreted path was run with `spark.sql.codegen.wholeStage=false` and 
`spark.sql.codegen.factoryMode=NO_CODEGEN`. A literal operand is 
constant-folded through the interpreted path, so the SQL golden files already 
show the interval error and do not change.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. In generated code, dividing the minimum year-month or day-time interval 
by `-1` now raises `INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION` (SQLSTATE 
22015) instead of `ARITHMETIC_OVERFLOW` (SQLSTATE 22003). This matches what the 
interpreted path, and therefore constant-folded queries, already raise.
   
   ### How was this patch tested?
   
   A new test in `IntervalExpressionsSuite` checks the error class and its 
parameter in both `CODEGEN_ONLY` and `NO_CODEGEN` mode. It covers 
`DivideYMInterval` and `DivideDTInterval` with a `-1` of each integral type. It 
failed before this change and passes after it, and the whole suite passes:
   
   ```
   build/sbt 'catalyst/testOnly *IntervalExpressionsSuite'
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (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