Skip to content

[SPARK-61352][SQL][4.3] Keep the query context of interval errors after serialization and rewrites - #60847

Closed
MaxGekk wants to merge 2 commits into
apache:branch-4.3from
MaxGekk:SPARK-61352-4.3
Closed

MaxGekk wants to merge 2 commits into
apache:branch-4.3from
MaxGekk:SPARK-61352-4.3

Conversation

@MaxGekk

@MaxGekk MaxGekk commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

This is a backport of #60285 (SPARK-61352) to branch-4.3, adapted because branch-4.3 doesn't have SPARK-60746 (#59956), on which the change on master builds. The interval divisions there still have their own checks instead of the IntervalMathUtils.divide overloads, so only the part of the fix that applies to that code is ported:

  • IntervalDivide (DivideYMInterval, DivideDTInterval) now mixes in SupportQueryContext, with initQueryContext() = Some(origin.context), set in every mode since the division raises in every mode.
  • The interpreted nullSafeEval of both divisions calls getContextOrNull(), and their doGenCode calls getContextOrNullCode(ctx), in place of origin.context. The same for the interpreted paths of MakeYMInterval and MakeDTInterval, which were already SupportQueryContext.
  • SupportQueryContext.copyTagsFrom copies the context of the node it replaces only when that node has one with an origin (SupportQueryContext.hasOrigin), as on master, so that a replaced non-ANSI Divide or cast, which has none, no longer leaves the division without a context.

What differs from master: on this branch, an interval at its minimum divided by an integral -1 raises ARITHMETIC_OVERFLOW in generated code and INTERVAL_ARITHMETIC_OVERFLOW in the interpreted path, and an overflow with a decimal or double divisor raises a plain ArithmeticException without a context. Both are unchanged here: unifying them is SPARK-60746, a behavior change that is not backported. The tests expect the errors of this branch.

Not fixed here, as on master: copyTagsFrom sets the context of the instance a rule returns in place, and that instance can be shared with another Dataset or a temp view, so a division shared that way can report the context of another query that removed a cast over it. This affects the other SupportQueryContext expressions too, and is tracked in SPARK-61467.

Why are the changes needed?

TreeNode does not serialize origin, so a deserialized interval division on an executor has an empty one. Any projection built there, e.g. with spark.sql.codegen.wholeStage=false, raises INTERVAL_DIVIDED_BY_ZERO or the overflow error with an empty query context, without the == SQL == block. For example, with ANSI on:

SET spark.sql.codegen.wholeStage=false;
SELECT CAST(id + 1 AS INTERVAL DAY) / CAST(id AS INT) FROM range(1);

Does this PR introduce any user-facing change?

Yes. Interval division keeps its SQL query context when it divides by zero, or overflows with an integral divisor, in a projection built on the executor; before, those errors had an empty context there. make_ym_interval and make_dt_interval keep their context in interpreted evaluation too. In non-ANSI mode, these expressions under a cast to their own type, which SimplifyCasts removes, keep a context instead of losing it. Under ANSI, an interval division under such a CAST now reports the whole CAST(...) as its fragment, as the other ANSI arithmetic does, where with whole-stage codegen it reported the division; a same-type try_cast does the same in both modes, although it does not suppress the error.

How was this patch tested?

The tests of #60285, adapted to this branch:

  • IntervalExpressionsSuite, "SPARK-61352: an interval error keeps its query context": year-month MinValue / -1 and day-time MinValue / -1L, divisions by an integral, a decimal and a double zero, make_ym_interval(2147483647, 0) and make_dt_interval(2147483647), each as built, under an ANSI, a TRY and a LEGACY same-type cast removed by SimplifyCasts, under an ANSI cast built without an origin, and after a makeCopy of a deserialized copy. The decimal and double MinValue / -1 cases of master are left out, since they raise without a context on this branch. The test fails without the change.
  • QueryExecutionAnsiErrorsSuite and QueryExecutionErrorsSuite, "SPARK-61352: ...": the divisions and make_*_interval end to end, bare and under a CAST and a try_cast to their own type, with whole-stage codegen on and off, and the Column API division with a DataFrameQueryContext. The year-month division expects ARITHMETIC_OVERFLOW, which generated code raises on this branch.
build/sbt 'catalyst/testOnly *IntervalExpressionsSuite *ArithmeticExpressionSuite *SimplifyCastsSuite *CastWithAnsiOnSuite *CastWithAnsiOffSuite *TryCastSuite' \
  'sql/testOnly *QueryExecutionErrorsSuite *QueryExecutionAnsiErrorsSuite' \
  'sql/testOnly org.apache.spark.sql.SQLQuerySuite -- -z "Query context"' \
  'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z interval'

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)

🤖 Generated with Claude Code

MaxGekk and others added 2 commits November 24, 2026 14:50
…rialization and rewrites

`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`.
- `SupportQueryContext.copyTagsFrom`, shared by every expression with a query context, now copies the context of the node it replaces only when that node has one that comes from an origin (`SupportQueryContext.hasOrigin`: a SQL context with a text and a start index, or any other context such as a `DataFrameQueryContext`). Until now it also copied `None`, and a context built from an empty `Origin`. `TreeNode.transformDown`/`transformUp` call `afterRule.copyTagsFrom(this)` whenever a rule replaces a node, and the replaced node can have no context: the non-ANSI `Divide` that the analyzer (`BinaryArithmeticWithDatetimeResolver`) turns into `DivideYMInterval` / `DivideDTInterval` has a `queryContext` of `None`, and so does a non-ANSI cast that `SimplifyCasts` replaces with `make_ym_interval(...)`. Copying it left the division or `make_*_interval`, which raise in every mode, with no context. Today the division still has one, because it reads `origin.context`; once it reads the field, copying `None` alone would make `select interval 2147483647 month / 0.5` in `nonansi/interval.sql` lose it.

  The copy still does what SPARK-39166 added it for. On an executor the deserialized expression has an empty `origin`, and a transform there (`BindReferences` while a projection is built) rebuilds the node, which then takes the serialized context. A replaced node with a context still gives it to its replacement, so the other ANSI expressions, the DataFrame API and the body of a SQL function keep their contexts as before. Under ANSI, `SimplifyCasts` dropping `CAST(<ym> / -1 AS INTERVAL YEAR TO MONTH)` now leaves the division with the cast's context too, as ANSI `Add` gets under a same-type cast. So does a same-type `try_cast` in every mode, since `Cast.ansiEnabled` holds for `EvalMode.TRY` unless it casts a string to a fractional or datetime type, so an interval-to-interval `try_cast` has a context even with `spark.sql.ansi.enabled=false`. Before, the division read `origin.context`, so with whole-stage codegen it reported its own fragment there.

  Effect on other expressions: only a `SupportQueryContext` expression with a context of its own that a rule returns in place of a node without one now keeps its context instead of losing it. Such a node is a non-ANSI or `try_*` form, or one built outside an origin, whose replacement has a context in that mode, such as the interval expressions here. Everything else behaves as before.
- 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()`.

Not fixed here: `copyTagsFrom` sets the context of the expression a rule returns in place, and that instance can be shared with a temp view or a parent Dataset, so a division shared that way can report the context of another query that removed a cast over it. This predates the PR for the other `SupportQueryContext` expressions, such as ANSI `Add`, and is tracked in SPARK-61467.

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

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`). In non-ANSI mode, an interval division or `make_ym_interval` / `make_dt_interval` under a cast to its own type, which `SimplifyCasts` removes, keeps a context instead of losing it. Under ANSI, an interval division under such a cast now reports the whole `CAST(...)` as its fragment, as the other ANSI arithmetic does, where before, with whole-stage codegen, it reported the division. The same holds for a same-type `try_cast` in either mode: the query still fails, since `try_cast` does not catch an error of its child, and the fragment is the whole `try_cast(...)`. The error condition, SQLSTATE and message parameters do not change.

New tests:

- `IntervalExpressionsSuite`, "SPARK-61352: an interval error keeps its query context": 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, double and decimal), year-month `/ 0`, day-time `MinValue / -1` (long, decimal and double), day-time `/ 0`, so that each interpreted division branch is reached, `make_ym_interval(2147483647, 0)`, `make_dt_interval(2147483647)`. Each case is checked in three forms: as built; after `SimplifyCasts`, run on a one-column `Project`, removes a same-type `Cast` around it: with an ANSI or a TRY cast, whose context the expression takes, with a non-ANSI one, which has none, and with an ANSI cast built outside `withOrigin`, whose context has no origin, so the expression keeps its own in the last two; and after a `makeCopy` of a deserialized copy, which rebuilds the node with an empty `origin` as a transform on an executor does. Each form is built from a fresh instance, since `copyTagsFrom` changes the instance a rule returns.
- `QueryExecutionAnsiErrorsSuite`, "SPARK-61352: an interval division error keeps its query context": runs `CAST(CAST(id - 2147483648 AS INT) AS INTERVAL MONTH) / -1` and `CAST(id + 1 AS INTERVAL DAY) / CAST(id AS INT)` end to end, each bare and inside a `CAST` and a `try_cast` to its own type (`INTERVAL YEAR TO MONTH`, `INTERVAL DAY TO SECOND`), with whole-stage codegen on and off, from one case table, and checks the condition, parameters and the `ExpectedContext` fragment: the division, or the whole `CAST(...)` / `try_cast(...)`, whose context the division takes. It also runs `spark.range(1).select(expr("CAST(id + 1 AS INTERVAL DAY)") / $"id".cast("int"))` in both modes and checks the `DataFrameQueryContext`: fragment `div` and the call site. The operands read a column, so they are not constant-folded on the driver.
- `QueryExecutionErrorsSuite`, "SPARK-61352: an interval error keeps its query context in non-ANSI mode": the same two divisions plus `make_ym_interval(CAST(id AS INT) + 178956971, 0)` and `make_dt_interval(CAST(id AS INT) + 106751992)`, bare and inside a `CAST` to their own type, whose context they keep, and inside a `try_cast`, whose context they take, under `spark.sql.ansi.enabled=false`, with whole-stage codegen on and off.

With `copyTagsFrom` copying `None` again, the unit test fails on the non-ANSI cast form: the expression under the cast reports no context. Without the `hasOrigin` check, it fails on the ANSI cast built outside `withOrigin`. The two suites share the queries and the expected-context helper through `IntervalErrorContextTestUtils`.

Existing suites pass: `IntervalExpressionsSuite`, `ArithmeticExpressionSuite`, `DecimalExpressionSuite`, `SimplifyCastsSuite`, `CastWithAnsiOnSuite`, `CastWithAnsiOffSuite`, `TryCastSuite`, `QueryExecutionErrorsSuite`, `QueryExecutionAnsiErrorsSuite`, the "Query context" tests of `SQLQuerySuite` (SPARK-39166 and its follow-ups, which raise from a projection built on the executor), and the whole `SQLQueryTestSuite` with no golden file changes. Its one failure locally, `udtf/udtf.sql - Python UDTFs` query apache#29, is the local Python's `ValueError` wording for `list.index` (`list.index(x): x not in list` against the expected `'input' is not in list`), unrelated to this change. `catalyst/scalastyle`, `catalyst/Test/scalastyle` and `sql/Test/scalastyle` pass.

```
build/sbt 'catalyst/testOnly *IntervalExpressionsSuite *ArithmeticExpressionSuite *DecimalExpressionSuite *SimplifyCastsSuite *CastWithAnsiOnSuite *CastWithAnsiOffSuite *TryCastSuite' \
  'sql/testOnly *QueryExecutionErrorsSuite *QueryExecutionAnsiErrorsSuite' \
  'sql/testOnly org.apache.spark.sql.SQLQuerySuite -- -z "Query context"' \
  'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite'
```

Generated-by: Claude Code (Opus 5.5)

Closes apache#60285 from MaxGekk/SPARK-61352-interval-divide-context.

Lead-authored-by: Maxim Gekk <max.gekk@gmail.com>
Co-authored-by: t <t@example.com>
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>

skip-checks: true
(cherry picked from commit 8621489)
On branch-4.3, generated code raises ARITHMETIC_OVERFLOW for an interval divided by an integral
-1 at its minimum, so the error suites expect it, with the same query context. A zero decimal or
double divisor still raises INTERVAL_DIVIDED_BY_ZERO with a context, so the unit test covers it,
and the doc of IntervalDivide no longer claims a context for every overflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@dongjoon-hyun dongjoon-hyun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, LGTM (Pending CIs).

@MaxGekk

MaxGekk commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Merging to 4.3. Thank you, @dongjoon-hyun for review.

MaxGekk added a commit that referenced this pull request Nov 24, 2026
…er serialization and rewrites

### What changes were proposed in this pull request?

This is a backport of #60285 (SPARK-61352) to `branch-4.3`, adapted because `branch-4.3` doesn't have SPARK-60746 (#59956), on which the change on master builds. The interval divisions there still have their own checks instead of the `IntervalMathUtils.divide` overloads, so only the part of the fix that applies to that code is ported:

- `IntervalDivide` (`DivideYMInterval`, `DivideDTInterval`) now mixes in `SupportQueryContext`, with `initQueryContext() = Some(origin.context)`, set in every mode since the division raises in every mode.
- The interpreted `nullSafeEval` of both divisions calls `getContextOrNull()`, and their `doGenCode` calls `getContextOrNullCode(ctx)`, in place of `origin.context`. The same for the interpreted paths of `MakeYMInterval` and `MakeDTInterval`, which were already `SupportQueryContext`.
- `SupportQueryContext.copyTagsFrom` copies the context of the node it replaces only when that node has one with an origin (`SupportQueryContext.hasOrigin`), as on master, so that a replaced non-ANSI `Divide` or cast, which has none, no longer leaves the division without a context.

What differs from master: on this branch, an interval at its minimum divided by an integral `-1` raises `ARITHMETIC_OVERFLOW` in generated code and `INTERVAL_ARITHMETIC_OVERFLOW` in the interpreted path, and an overflow with a decimal or double divisor raises a plain `ArithmeticException` without a context. Both are unchanged here: unifying them is SPARK-60746, a behavior change that is not backported. The tests expect the errors of this branch.

Not fixed here, as on master: `copyTagsFrom` sets the context of the instance a rule returns in place, and that instance can be shared with another Dataset or a temp view, so a division shared that way can report the context of another query that removed a cast over it. This affects the other `SupportQueryContext` expressions too, and is tracked in SPARK-61467.

### Why are the changes needed?

`TreeNode` does not serialize `origin`, so a deserialized interval division on an executor has an empty one. Any projection built there, e.g. with `spark.sql.codegen.wholeStage=false`, raises `INTERVAL_DIVIDED_BY_ZERO` or the overflow error with an empty query context, without the `== SQL ==` block. For example, with ANSI on:

```sql
SET spark.sql.codegen.wholeStage=false;
SELECT CAST(id + 1 AS INTERVAL DAY) / CAST(id AS INT) FROM range(1);
```

### Does this PR introduce _any_ user-facing change?

Yes. Interval division keeps its SQL query context when it divides by zero, or overflows with an integral divisor, in a projection built on the executor; before, those errors had an empty context there. `make_ym_interval` and `make_dt_interval` keep their context in interpreted evaluation too. In non-ANSI mode, these expressions under a cast to their own type, which `SimplifyCasts` removes, keep a context instead of losing it. Under ANSI, an interval division under such a `CAST` now reports the whole `CAST(...)` as its fragment, as the other ANSI arithmetic does, where with whole-stage codegen it reported the division; a same-type `try_cast` does the same in both modes, although it does not suppress the error.

### How was this patch tested?

The tests of #60285, adapted to this branch:

- `IntervalExpressionsSuite`, "SPARK-61352: an interval error keeps its query context": year-month `MinValue / -1` and day-time `MinValue / -1L`, divisions by an integral, a decimal and a double zero, `make_ym_interval(2147483647, 0)` and `make_dt_interval(2147483647)`, each as built, under an ANSI, a TRY and a LEGACY same-type cast removed by `SimplifyCasts`, under an ANSI cast built without an origin, and after a `makeCopy` of a deserialized copy. The decimal and double `MinValue / -1` cases of master are left out, since they raise without a context on this branch. The test fails without the change.
- `QueryExecutionAnsiErrorsSuite` and `QueryExecutionErrorsSuite`, "SPARK-61352: ...": the divisions and `make_*_interval` end to end, bare and under a `CAST` and a `try_cast` to their own type, with whole-stage codegen on and off, and the Column API division with a `DataFrameQueryContext`. The year-month division expects `ARITHMETIC_OVERFLOW`, which generated code raises on this branch.

```
build/sbt 'catalyst/testOnly *IntervalExpressionsSuite *ArithmeticExpressionSuite *SimplifyCastsSuite *CastWithAnsiOnSuite *CastWithAnsiOffSuite *TryCastSuite' \
  'sql/testOnly *QueryExecutionErrorsSuite *QueryExecutionAnsiErrorsSuite' \
  'sql/testOnly org.apache.spark.sql.SQLQuerySuite -- -z "Query context"' \
  'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z interval'
```

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Closes #60847 from MaxGekk/SPARK-61352-4.3.

Authored-by: Max Gekk <max.gekk@gmail.com>
Signed-off-by: Max Gekk <max.gekk@gmail.com>
@MaxGekk MaxGekk closed this Nov 24, 2026
@MaxGekk

MaxGekk commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Merge Summary:

Posted by merge_spark_pr.py

MaxGekk added a commit that referenced this pull request Nov 24, 2026
…er serialization and rewrites

### What changes were proposed in this pull request?

This is a backport of #60285 (SPARK-61352) to `branch-4.2`, the same as #60847 for `branch-4.3`, adapted because `branch-4.2` doesn't have SPARK-60746 (#59956), on which the change on master builds. The interval divisions there still have their own checks instead of the `IntervalMathUtils.divide` overloads, so only the part of the fix that applies to that code is ported:

- `IntervalDivide` (`DivideYMInterval`, `DivideDTInterval`) now mixes in `SupportQueryContext`, with `initQueryContext() = Some(origin.context)`, set in every mode since the division raises in every mode.
- The interpreted `nullSafeEval` of both divisions calls `getContextOrNull()`, and their `doGenCode` calls `getContextOrNullCode(ctx)`, in place of `origin.context`. The same for the interpreted paths of `MakeYMInterval` and `MakeDTInterval`, which were already `SupportQueryContext`.
- `SupportQueryContext.copyTagsFrom` copies the context of the node it replaces only when that node has one with an origin (`SupportQueryContext.hasOrigin`), as on master, so that a replaced non-ANSI `Divide` or cast, which has none, no longer leaves the division without a context.

What differs from master: on this branch, an interval at its minimum divided by an integral `-1` raises `ARITHMETIC_OVERFLOW` in generated code and `INTERVAL_ARITHMETIC_OVERFLOW` in the interpreted path, and an overflow with a decimal or double divisor raises a plain `ArithmeticException` without a context. Both are unchanged here: unifying them is SPARK-60746, a behavior change that is not backported. The tests expect the errors of this branch.

Not fixed here, as on master: `copyTagsFrom` sets the context of the instance a rule returns in place, and that instance can be shared with another Dataset or a temp view, so a division shared that way can report the context of another query that removed a cast over it. This affects the other `SupportQueryContext` expressions too, and is tracked in SPARK-61467.

### Why are the changes needed?

`TreeNode` does not serialize `origin`, so a deserialized interval division on an executor has an empty one. Any projection built there, e.g. with `spark.sql.codegen.wholeStage=false`, raises `INTERVAL_DIVIDED_BY_ZERO` or the overflow error with an empty query context, without the `== SQL ==` block. For example, with ANSI on:

```sql
SET spark.sql.codegen.wholeStage=false;
SELECT CAST(id + 1 AS INTERVAL DAY) / CAST(id AS INT) FROM range(1);
```

### Does this PR introduce _any_ user-facing change?

Yes. Interval division keeps its SQL query context when it divides by zero, or overflows with an integral divisor, in a projection built on the executor; before, those errors had an empty context there. `make_ym_interval` and `make_dt_interval` keep their context in interpreted evaluation too. In non-ANSI mode, these expressions under a cast to their own type, which `SimplifyCasts` removes, keep a context instead of losing it. Under ANSI, an interval division under such a `CAST` now reports the whole `CAST(...)` as its fragment, as the other ANSI arithmetic does, where with whole-stage codegen it reported the division; a same-type `try_cast` does the same in both modes, although it does not suppress the error.

### How was this patch tested?

The tests of #60285, adapted to this branch:

- `IntervalExpressionsSuite`, "SPARK-61352: an interval error keeps its query context": year-month `MinValue / -1` and day-time `MinValue / -1L`, divisions by an integral, a decimal and a double zero, `make_ym_interval(2147483647, 0)` and `make_dt_interval(2147483647)`, each as built, under an ANSI, a TRY and a LEGACY same-type cast removed by `SimplifyCasts`, under an ANSI cast built without an origin, and after a `makeCopy` of a deserialized copy. The decimal and double `MinValue / -1` cases of master are left out, since they raise without a context on this branch. The test fails without the change.
- `QueryExecutionAnsiErrorsSuite` and `QueryExecutionErrorsSuite`, "SPARK-61352: ...": the divisions and `make_*_interval` end to end, bare and under a `CAST` and a `try_cast` to their own type, with whole-stage codegen on and off, and the Column API division with a `DataFrameQueryContext`. The year-month division expects `ARITHMETIC_OVERFLOW`, which generated code raises on this branch.

```
build/sbt 'catalyst/testOnly *IntervalExpressionsSuite *ArithmeticExpressionSuite *SimplifyCastsSuite *CastWithAnsiOnSuite *CastWithAnsiOffSuite *TryCastSuite' \
  'sql/testOnly *QueryExecutionErrorsSuite *QueryExecutionAnsiErrorsSuite' \
  'sql/testOnly org.apache.spark.sql.SQLQuerySuite -- -z "Query context"' \
  'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z interval'
```

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)


Closes #60848 from MaxGekk/SPARK-61352-4.2.

Authored-by: Max Gekk <max.gekk@gmail.com>
Signed-off-by: Max Gekk <max.gekk@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants