Skip to content

feat: add Spark-compatible hypot function - #23774

Merged
comphead merged 6 commits into
apache:mainfrom
KarpagamKarthikeyan:feat/spark-hypot
Jul 26, 2026
Merged

comphead merged 6 commits into
apache:mainfrom
KarpagamKarthikeyan:feat/spark-hypot

Conversation

@KarpagamKarthikeyan

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Spark provides hypot(expr1, expr2), which returns sqrt(expr1^2 + expr2^2) computed without intermediate overflow or underflow. It was not yet implemented in datafusion-spark — only an auto-generated test stub existed at spark/math/hypot.slt with its query commented out.

What changes are included in this PR?

  • Add SparkHypot (implementing ScalarUDFImpl) in datafusion/spark/src/function/math/hypot.rs, backed by Rust's f64::hypot — the same overflow-safe algorithm as Java/Spark's Math.hypot.
  • Register it in datafusion/spark/src/function/math/mod.rs.
  • Enable the hypot.slt sqllogictest.

The signature is exact(Float64, Float64) -> Float64, following the datafusion-spark convention of only accepting types Spark supports. Computation uses the Arrow binary kernel so NULL in either argument propagates to a NULL result, matching Spark.

Are these changes tested?

Yes — datafusion/sqllogictest/test_files/spark/math/hypot.slt covers:

  • scalar Pythagorean triples (hypot(3, 4) → 5, hypot(5, 12) → 13),
  • double inputs,
  • NULL propagation when either argument is NULL,
  • the array path (including a NULL row),
  • overflow-safety: hypot(3e200, 4e200) stays finite, whereas a naive sqrt(a^2 + b^2) would overflow to Infinity.

Are there any user-facing changes?

Yes — adds the Spark-compatible hypot scalar function to datafusion-spark. No breaking changes to public APIs.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) spark Changes to datafusion-spark crate labels Jul 21, 2026
@KarpagamKarthikeyan

Copy link
Copy Markdown
Contributor Author

cc @Jefffrey @andygrove — this is my first contribution, so CI is currently awaiting a committer's approval. Would really appreciate a trigger when you have a moment.

This adds a Spark-compatible hypot function to datafusion-spark, following the existing math function conventions (exact(Float64, Float64) -> Float64, invoke rather than simplify), backed by Rust's overflow-safe f64::hypot. Covered by sqllogictest cases (triples, doubles, NULL propagation, array path, and overflow-safety). Closes #23770.

Thanks!

@codecov-commenter

codecov-commenter commented Jul 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.37500% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.69%. Comparing base (16471ee) to head (ef114ed).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/spark/src/function/math/hypot.rs 83.87% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   apache/datafusion#23774      +/-   ##
==========================================
- Coverage   80.69%   80.69%   -0.01%     
==========================================
  Files        1090     1091       +1     
  Lines      370357   370389      +32     
  Branches   370357   370389      +32     
==========================================
+ Hits       298859   298878      +19     
- Misses      53684    53689       +5     
- Partials    17814    17822       +8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Agree with @Jefffrey
for doubles/floats please include test input with NaN, +0, -0, +Inf, -Inf

@comphead
comphead added this pull request to the merge queue Jul 26, 2026
Merged via the queue into apache:main with commit e660118 Jul 26, 2026
37 checks passed
kosiew pushed a commit to kosiew/datafusion that referenced this pull request Aug 12, 2026
## Which issue does this PR close?

- Closes apache#23770
- Part of #15914

## Rationale for this change

Spark provides [`hypot(expr1,
expr2)`](https://spark.apache.org/docs/latest/api/sql/#hypot), which
returns `sqrt(expr1^2 + expr2^2)` computed without intermediate overflow
or underflow. It was not yet implemented in `datafusion-spark` — only an
auto-generated test stub existed at `spark/math/hypot.slt` with its
query commented out.

## What changes are included in this PR?

- Add `SparkHypot` (implementing `ScalarUDFImpl`) in
`datafusion/spark/src/function/math/hypot.rs`, backed by Rust's
`f64::hypot` — the same overflow-safe algorithm as Java/Spark's
`Math.hypot`.
- Register it in `datafusion/spark/src/function/math/mod.rs`.
- Enable the `hypot.slt` sqllogictest.

The signature is `exact(Float64, Float64) -> Float64`, following the
`datafusion-spark` convention of only accepting types Spark supports.
Computation uses the Arrow `binary` kernel so NULL in either argument
propagates to a NULL result, matching Spark.

## Are these changes tested?

Yes — `datafusion/sqllogictest/test_files/spark/math/hypot.slt` covers:
- scalar Pythagorean triples (`hypot(3, 4)` → 5, `hypot(5, 12)` → 13),
- double inputs,
- NULL propagation when either argument is NULL,
- the array path (including a NULL row),
- overflow-safety: `hypot(3e200, 4e200)` stays finite, whereas a naive
`sqrt(a^2 + b^2)` would overflow to `Infinity`.

## Are there any user-facing changes?

Yes — adds the Spark-compatible `hypot` scalar function to
`datafusion-spark`. No breaking changes to public APIs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

spark Changes to datafusion-spark crate sqllogictest SQL Logic Tests (.slt) v55.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Spark-compatible hypot function

5 participants