Skip to content

[branch-53] fix(spark): handle divide-by-zero in Spark mod/pmod with ANSI mode support (#20461)#20896

Open
alamb wants to merge 1 commit intoapache:branch-53from
alamb:alamb/backport_20461_branch53
Open

[branch-53] fix(spark): handle divide-by-zero in Spark mod/pmod with ANSI mode support (#20461)#20896
alamb wants to merge 1 commit intoapache:branch-53from
alamb:alamb/backport_20461_branch53

Conversation

@alamb
Copy link
Contributor

@alamb alamb commented Mar 12, 2026

…e support (apache#20461)

## Which issue does this PR close?

- NA.

## Rationale for this change

Spark's `mod` and `pmod` functions return `NULL` on integer division by
zero in legacy mode (ANSI off), but DataFusion's implementation always
threw a `DivideByZero` error regardless of the ANSI mode setting.
## What changes are included in this PR?

Add ANSI mode support to `spark_mod` and `spark_pmod` via
enable_ansi_mode config option
In legacy mode (ANSI off): division by zero returns `NULL` per-element
In ANSI mode (ANSI on): division by zero throws an error (unchanged
behavior)
Add `try_rem` helper that handles per-element zero-divisor masking for
integer arrays


## Are these changes tested?

Yes:

- 18 unit tests in modulus.rs (including new tests for both ANSI modes)
- Updated pmod.slt and mod.slt sqllogictests with ANSI on/off coverage

## Are there any user-facing changes?

Yes — mod(10, 0) and pmod(10, 0) now return NULL instead of erroring
when enable_ansi_mode = false (the default), matching Spark behavior.
@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) spark labels Mar 12, 2026
@coderfender
Copy link

Thank you @alamb . There is an open PR on the comet side to wire this function but it seems like that has to wait till v53 is out .

Comet PR : apache/datafusion-comet#3586

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

spark sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants