Skip to content

fix: preserve TRY overflow semantics in nested integer arithmetic - #7379

Merged
sunchao merged 2 commits into
apache:mainfrom
sunchao:fix/try-arithmetic-eval-mode
Nov 2, 2026
Merged

sunchao merged 2 commits into
apache:mainfrom
sunchao:fix/try-arithmetic-eval-mode

Conversation

@sunchao

@sunchao sunchao commented Oct 31, 2026

Copy link
Copy Markdown
Member

Which issue does this PR close?

Closes #7378.

Rationale for this change

With ANSI mode disabled, Spark returns NULL for try_add(a, b) + b when the inner addition overflows. Comet flattened both additions and rebuilt them using the outer operation's wrapping overflow behavior. The same problem affected try_multiply inside a multiplication.

What changes are included in this PR?

Only flatten a nested Add or Multiply when its evaluation mode matches the parent. This keeps the inner overflow check in the native expression and still balances chains of legacy integer operations.

How are these changes tested?

Added one query to the existing arithmetic SQL fixture covering both operators. With a = 2147483647 and b = 1, the query produced the following results on Spark 4.1.3.

Execution try_add(a, b) + b try_multiply(a, a) * b
Spark NULL NULL
Comet before -2147483647 1
Comet after NULL NULL

The full expressions/math/arithmetic.sql fixture passed on Spark 3.5.9 and 4.1.3. The existing deep integral Add/Multiply chain test also passed on Spark 4.1.3. These runs compiled the changed serializer against cached Apache Comet dependencies and used the existing native library. Spotless passed.

@sunchao sunchao added the run-spark-4.1-tests Run the Spark 4.1 SQL tests on this pull request instead of waiting for the merge queue label Oct 31, 2026
@github-actions github-actions Bot added bug Something isn't working area:expressions Expression evaluation labels Oct 31, 2026

@viirya viirya 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.

LGTM. Comparing against expr.evalMode rather than only TRY also covers an ANSI Add that ends up under a LEGACY one after CollapseProject, e.g. from a view created with ANSI enabled.

@sunchao
sunchao enabled auto-merge November 2, 2026 05:41
@sunchao
sunchao added this pull request to the merge queue Nov 2, 2026
Merged via the queue into apache:main with commit 268ede5 Nov 2, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:expressions Expression evaluation bug Something isn't working run-spark-4.1-tests Run the Spark 4.1 SQL tests on this pull request instead of waiting for the merge queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested try_add and try_multiply can return wrapped integers instead of NULL

2 participants