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

   ### What changes were proposed in this pull request?
   
   `DivideYMInterval` and `DivideDTInterval` share the `IntervalDivide` trait. 
Both build the context for their errors 
(`INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION`, `INTERVAL_DIVIDED_BY_ZERO`) 
from `origin.context`, read at evaluation time in the interpreted path and at 
code-generation time in `doGenCode`. This PR makes the division keep its 
context the way the other arithmetic expressions do:
   
   - `IntervalDivide` now mixes in `SupportQueryContext`, with 
`initQueryContext() = Some(origin.context)`. The division raises in every mode, 
so the context is set unconditionally rather than only under ANSI as in 
`Divide`.
   - The interpreted `evalFunc` of both classes calls `getContextOrNull()`, and 
`doGenCode` calls `getContextOrNullCode(ctx)`, in place of `origin.context`.
   - `IntervalDivide` overrides `copyTagsFrom` to keep its own context when the 
node it replaces carries none. The analyzer 
(`BinaryArithmeticWithDatetimeResolver`) turns a `Divide` into 
`DivideYMInterval` / `DivideDTInterval`. Under non-ANSI that `Divide`'s 
`queryContext` is `None`, and `SupportQueryContext.copyTagsFrom` would copy 
that `None` over the division's own context. Without the override, the error of 
`select interval 2147483647 month / 0.5` in `nonansi/interval.sql` would lose 
the context it has today.
   - Same pattern in two siblings: the interpreted paths of `MakeYMInterval` 
and `MakeDTInterval` passed `origin.context`, although both classes are already 
`SupportQueryContext` and their generated code already uses the field. Both now 
call `getContextOrNull()`.
   
   ### Why are the changes needed?
   
   `TreeNode` does not implement `java.io.Serializable`, so Java serialization 
skips its fields. When an expression is deserialized, `TreeNode`'s constructor 
runs again and sets `origin` from `CurrentOrigin.get`, which is empty on an 
executor. `SupportQueryContext` exists for this reason (SPARK-39166): its 
context is a field of the concrete, serializable expression, so it survives the 
trip to the executor.
   
   The interval divisions never used it. Whole-stage codegen hides the problem, 
because it generates code on the driver and puts the context into the 
references array. Any projection built on the executor from the deserialized 
expression, though, raises the error with an empty context and no `== SQL ==` 
block. That includes `ProjectExec` and the other operators when 
`spark.sql.codegen.wholeStage=false`, and interpreted projections. For example:
   
   ```sql
   SET spark.sql.ansi.enabled=true;
   SET spark.sql.codegen.wholeStage=false;
   SELECT CAST(CAST(id - 2147483648 AS INT) AS INTERVAL MONTH) / -1 FROM 
range(1);
   ```
   
   raises `[INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION] ... SQLSTATE: 22015` 
with a query context whose fragment is empty and whose start and stop are -1. 
With whole-stage codegen on, the same query reports the fragment and the caret. 
`CAST(id + 1 AS INTERVAL DAY) / CAST(id AS INT)` gives 
`INTERVAL_DIVIDED_BY_ZERO` with the same gap. `make_ym_interval` / 
`make_dt_interval` overflows lose the context the same way, but only in 
interpreted evaluation, since their generated code already used the field. 
SPARK-60746 made both evaluation paths raise the same condition with a context. 
This PR closes the remaining gap, where that context is lost once the 
expression is serialized. The gap is older than SPARK-60746: released versions 
read `origin.context` in these classes too.
   
   The gap was found while comparing errors from a columnar engine's fallback 
projections, which are built on the executor, against Spark's own whole-stage 
code.
   
   A grep for `origin.context` across the non-test sources under `sql/` finds 
no other evaluation-time readers. The remaining hits are analysis-time error 
builders that run on the driver. SPARK-61196 (open) plans to pass a context to 
the overflow errors of interval add, subtract and negate. It should take it 
from a `SupportQueryContext` field as here, not from `origin.context`, or it 
will have the same gap.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Interval division by a number now reports its SQL query context (the 
`== SQL ==` block, and the fragment and position in the error's `queryContext`) 
when it overflows or divides by zero in a projection built on the executor, for 
example with `spark.sql.codegen.wholeStage=false`. Before, those errors had an 
empty context there. `make_ym_interval` and `make_dt_interval` already kept 
their context in generated code; they now keep it in interpreted evaluation too 
(the codegen fallback, or `spark.sql.codegen.factoryMode=NO_CODEGEN`). The 
error condition, SQLSTATE and message parameters do not change, and with 
whole-stage codegen errors are as before.
   
   ### How was this patch tested?
   
   New tests:
   
   - `IntervalExpressionsSuite`, "an interval error keeps its query context 
after serialization": builds each expression under `withOrigin` and evaluates 
it outside, so the serialization round trip in `ExpressionEvalHelper` cannot 
restore the origin from `CurrentOrigin`. It checks for the `== SQL (line 1, 
position 8) ==` block in the interpreted, mutable-projection and 
unsafe-projection modes. Cases: year-month `MinValue / -1` (integral and 
double), year-month `/ 0`, day-time `MinValue / -1` (long and decimal), 
day-time `/ 0`, `make_ym_interval(2147483647, 0)`, 
`make_dt_interval(2147483647)`. Before the fix every case fails, on the first 
mode checked (non-codegen).
   - `QueryExecutionAnsiErrorsSuite`, "an interval division error keeps its 
context without whole-stage codegen": runs the two queries above end to end 
with `spark.sql.codegen.wholeStage=false`, the year-month one under both ANSI 
and non-ANSI, and checks the condition, parameters and `ExpectedContext`. The 
operands read a column, so they are not constant-folded on the driver. Before 
the fix it fails with fragment `''` and start/stop -1.
   
   Existing suites pass unchanged: `IntervalExpressionsSuite`, 
`IntervalUtilsSuite`, `ArithmeticExpressionSuite`, 
`QueryExecutionAnsiErrorsSuite`, `DataFrameAggregateSuite`, 
`WholeStageCodegenSuite`, `ColumnExpressionSuite`, and `SQLQueryTestSuite` for 
`interval.sql`, `nonansi/interval.sql`, `try_aggregates.sql`, 
`nonansi/try_aggregates.sql`, the `datetime*` and `try_arithmetic` files. No 
golden file changes. Without the `copyTagsFrom` override, 
`nonansi/interval.sql` fails on query #6: the context of `interval 2147483647 
month / 0.5` goes missing. `catalyst/scalastyle`, `catalyst/Test/scalastyle` 
and `sql/Test/scalastyle` pass.
   
   ```
   build/sbt 'catalyst/testOnly *IntervalExpressionsSuite *IntervalUtilsSuite 
*ArithmeticExpressionSuite' \
     'sql/testOnly *QueryExecutionAnsiErrorsSuite *DataFrameAggregateSuite 
*WholeStageCodegenSuite *ColumnExpressionSuite' \
     'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z interval -z 
try_aggregates -z datetime -z try_arithmetic'
   ```
   
   ### 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