Skip to content

feat: add Spark-compatible hypot function#23774

Open
KarpagamKarthikeyan wants to merge 3 commits into
apache:mainfrom
KarpagamKarthikeyan:feat/spark-hypot
Open

feat: add Spark-compatible hypot function#23774
KarpagamKarthikeyan wants to merge 3 commits into
apache:mainfrom
KarpagamKarthikeyan:feat/spark-hypot

Conversation

@KarpagamKarthikeyan

Copy link
Copy Markdown

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 labels Jul 21, 2026
@KarpagamKarthikeyan

Copy link
Copy Markdown
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!

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.

Implement Spark-compatible hypot function

1 participant