Skip to content

Comments

fix: handle divide-by-zero in Spark mod/pmod with ANSI mode support#20461

Open
davidlghellin wants to merge 1 commit intoapache:mainfrom
davidlghellin:fix/mod
Open

fix: handle divide-by-zero in Spark mod/pmod with ANSI mode support#20461
davidlghellin wants to merge 1 commit intoapache:mainfrom
davidlghellin:fix/mod

Conversation

@davidlghellin
Copy link
Contributor

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 Feb 21, 2026
@davidlghellin davidlghellin marked this pull request as draft February 21, 2026 12:38
@davidlghellin davidlghellin marked this pull request as ready for review February 21, 2026 12:54
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.

1 participant