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!

let [x, y] = take_function_args(self.name(), args.args)?;

// Broadcast scalars to arrays so one path covers every combination.
let x = x.to_array(num_rows)?;

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.

make_scalar_function() handles this for us

e.g.

fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
    make_scalar_function(hypot_array, vec![])(&args.args)
}

// outside impl

fn hypot_array(args: &[ArrayRef]) -> Result<ArrayRef> {
    let [x, y] = take_function_args("hypot", args)?;
    let x = x.as_primitive::<Float64Type>();
    let y = y.as_primitive::<Float64Type>();
    return Ok(Arc::new(binary(x, y, |a, b| a.hypot(b))))
}

## PySpark 3.5.5 Result: {'HYPOT(3, 4)': 5.0, 'typeof(HYPOT(3, 4))': 'double', 'typeof(3)': 'int', 'typeof(4)': 'int'}
#query
#SELECT hypot(3::int, 4::int);
# Scalar: classic Pythagorean triples (3-4-5, 5-12-13)

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.

could we add some more edge cases such as infinity inputs, nans, etc.

Comment on lines +49 to +50
// Spark only defines hypot over doubles; `exact` makes coercion
// guarantee both inputs are Float64 before `invoke` runs.

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.

Suggested change
// Spark only defines hypot over doubles; `exact` makes coercion
// guarantee both inputs are Float64 before `invoke` runs.
// Spark only defines hypot over doubles

extra detail unnecessary

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.78788% with 7 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@05b7e11). Learn more about missing BASE report.
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/spark/src/function/math/hypot.rs 78.12% 3 Missing and 4 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #23774   +/-   ##
=======================================
  Coverage        ?   80.71%           
=======================================
  Files           ?     1090           
  Lines           ?   368933           
  Branches        ?   368933           
=======================================
  Hits            ?   297789           
  Misses          ?    53381           
  Partials        ?    17763           

☔ 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.

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

3 participants