Jefffrey commented on code in PR #23774:
URL: https://github.com/apache/datafusion/pull/23774#discussion_r3634982335


##########
datafusion/spark/src/function/math/hypot.rs:
##########
@@ -0,0 +1,90 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::sync::Arc;
+
+use arrow::array::{ArrayRef, AsArray, Float64Array};
+use arrow::compute::kernels::arity::binary;
+use arrow::datatypes::{DataType, Float64Type};
+use datafusion_common::Result;
+use datafusion_common::utils::take_function_args;
+use datafusion_expr::{
+    ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
+};
+
+/// Spark-compatible `hypot` function.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#hypot>
+///
+/// Returns `sqrt(expr1^2 + expr2^2)` computed without intermediate overflow or
+/// underflow, matching Spark's use of `java.lang.Math.hypot`.
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkHypot {
+    signature: Signature,
+}
+
+impl Default for SparkHypot {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkHypot {
+    pub fn new() -> Self {
+        Self {
+            // Spark only defines hypot over doubles; `exact` makes coercion
+            // guarantee both inputs are Float64 before `invoke` runs.
+            signature: Signature::exact(
+                vec![DataType::Float64, DataType::Float64],
+                Volatility::Immutable,
+            ),
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkHypot {
+    fn name(&self) -> &str {
+        "hypot"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+        Ok(DataType::Float64)
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        let num_rows = args.number_rows;
+        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)?;

Review Comment:
   `make_scalar_function()` handles this for us
   
   e.g.
   
   ```rust
   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))))
   }
   ```



##########
datafusion/spark/src/function/math/hypot.rs:
##########
@@ -0,0 +1,90 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::sync::Arc;
+
+use arrow::array::{ArrayRef, AsArray, Float64Array};
+use arrow::compute::kernels::arity::binary;
+use arrow::datatypes::{DataType, Float64Type};
+use datafusion_common::Result;
+use datafusion_common::utils::take_function_args;
+use datafusion_expr::{
+    ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
+};
+
+/// Spark-compatible `hypot` function.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#hypot>
+///
+/// Returns `sqrt(expr1^2 + expr2^2)` computed without intermediate overflow or
+/// underflow, matching Spark's use of `java.lang.Math.hypot`.
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkHypot {
+    signature: Signature,
+}
+
+impl Default for SparkHypot {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkHypot {
+    pub fn new() -> Self {
+        Self {
+            // Spark only defines hypot over doubles; `exact` makes coercion
+            // guarantee both inputs are Float64 before `invoke` runs.

Review Comment:
   ```suggestion
               // Spark only defines hypot over doubles
   ```
   
   extra detail unnecessary



##########
datafusion/sqllogictest/test_files/spark/math/hypot.slt:
##########
@@ -21,7 +21,45 @@
 # For more information, please see:
 #   https://github.com/apache/datafusion/issues/15914
 
-## Original Query: SELECT hypot(3, 4);
-## 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)

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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to