You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/08/16 18:56:14 UTC

[GitHub] [arrow] andygrove commented on a change in pull request #7967: ARROW-9751: [Rust] [DataFusion] Allow UDFs to accept multiple data types per argument

andygrove commented on a change in pull request #7967:
URL: https://github.com/apache/arrow/pull/7967#discussion_r471146144



##########
File path: rust/datafusion/src/execution/physical_plan/math_expressions.rs
##########
@@ -20,36 +20,62 @@
 use crate::error::ExecutionError;
 use crate::execution::physical_plan::udf::ScalarFunction;
 
-use arrow::array::{Array, ArrayRef, Float64Array, Float64Builder};
-use arrow::datatypes::{DataType, Field};
+use arrow::array::{Array, ArrayRef};
+use arrow::array::{Float32Array, Float64Array};
+use arrow::datatypes::DataType;
 
 use std::sync::Arc;
 
+macro_rules! compute_op {
+    ($ARRAY:expr, $FUNC:ident, $TYPE:ident) => {{
+        let mut builder = <$TYPE>::builder($ARRAY.len());
+        for i in 0..$ARRAY.len() {
+            if $ARRAY.is_null(i) {
+                builder.append_null()?;
+            } else {
+                builder.append_value($ARRAY.value(i).$FUNC())?;
+            }
+        }
+        Ok(Arc::new(builder.finish()))
+    }};
+}
+
+macro_rules! downcast_compute_op {
+    ($ARRAY:expr, $NAME:expr, $FUNC:ident, $TYPE:ident) => {{
+        let n = $ARRAY.as_any().downcast_ref::<$TYPE>();
+        match n {
+            Some(array) => compute_op!(array, $FUNC, $TYPE),
+            _ => Err(ExecutionError::General(format!(
+                "Invalid data type for {}",
+                $NAME
+            ))),
+        }
+    }};
+}
+
+macro_rules! unary_primitive_array_op {
+    ($ARRAY:expr, $NAME:expr, $FUNC:ident) => {{
+        match ($ARRAY).data_type() {
+            DataType::Float32 => downcast_compute_op!($ARRAY, $NAME, $FUNC, Float32Array),
+            DataType::Float64 => downcast_compute_op!($ARRAY, $NAME, $FUNC, Float64Array),
+            other => Err(ExecutionError::General(format!(
+                "Unsupported data type {:?} for function {}",
+                other, $NAME,
+            ))),
+        }
+    }};
+}
+
 macro_rules! math_unary_function {
     ($NAME:expr, $FUNC:ident) => {
         ScalarFunction::new(
             $NAME,
-            vec![Field::new("n", DataType::Float64, true)],
+            // order: from faster to slower
+            vec![vec![DataType::Float32], vec![DataType::Float64]],
             DataType::Float64,

Review comment:
       Should there be a relationship between argument type and return type? should `sqrt(f32)` return `f32`, and `sqrt(f64)` return `f64` for example?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org