Skip to content

fix sqrt(-1.0::float8) should error, not return NaN#22308

Merged
kosiew merged 2 commits into
apache:mainfrom
xiedeyantu:sqrt
May 25, 2026
Merged

fix sqrt(-1.0::float8) should error, not return NaN#22308
kosiew merged 2 commits into
apache:mainfrom
xiedeyantu:sqrt

Conversation

@xiedeyantu

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

DataFusion previously returned NaN for sqrt on negative floating-point inputs, for example sqrt((-1.0)::float8). This differs from PostgreSQL semantics, which raise an error for square root of a negative number.

This change makes sqrt return an execution error for out-of-domain negative inputs so its behavior is closer to PostgreSQL and avoids silently producing NaN for invalid inputs.

What changes are included in this PR?

  • Updated the unary math UDF helper to support an optional validator callback for runtime input validation.
  • Switched sqrt to use a named validator helper instead of inline predicate and error-string arguments.
  • Added runtime validation for sqrt so negative inputs now raise cannot take square root of a negative number.
  • Updated sqllogictests for sqrt:
    • negative literal inputs now expect an error
    • negative column inputs now expect an error
    • positive column coverage was retained using in-domain inputs

Are these changes tested?

Yes.

The change is covered by existing SQL logic tests and targeted validation runs:

  • cargo test -p datafusion-functions sqrt
  • cargo test -p datafusion-sqllogictest --test sqllogictests scalar

Are there any user-facing changes?

Yes.

sqrt now raises an execution error for negative inputs instead of returning NaN. This changes user-visible query behavior to better align with PostgreSQL semantics.

Co-authored-by: Copilot <copilot@github.com>
@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels May 17, 2026
Co-authored-by: Copilot <copilot@github.com>
@xiedeyantu

Copy link
Copy Markdown
Member Author

@Dandandan Could you help me take a look?

@kosiew kosiew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@xiedeyantu
Looks 👍 to me

@xiedeyantu

Copy link
Copy Markdown
Member Author

@xiedeyantu Looks 👍 to me

Thanks!

@Dandandan Dandandan added this pull request to the merge queue May 23, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 23, 2026
@xiedeyantu

Copy link
Copy Markdown
Member Author

@Dandandan CI appears to be malfunctioning.

@kosiew kosiew added this pull request to the merge queue May 25, 2026
Merged via the queue into apache:main with commit 7bcb613 May 25, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL compatibility: sqrt(-1.0::float8) should error, not return NaN

3 participants