jorgecarleitao commented on a change in pull request #9376:
URL: https://github.com/apache/arrow/pull/9376#discussion_r574621673



##########
File path: rust/datafusion/src/physical_plan/crypto_expressions.rs
##########
@@ -49,58 +58,142 @@ fn sha_process<D: SHA2Digest + Default>(input: &str) -> 
SHA2DigestOutput<D> {
     digest.finalize()
 }
 
-macro_rules! crypto_unary_string_function {
-    ($NAME:ident, $FUNC:expr) => {
-        /// crypto function that accepts Utf8 or LargeUtf8 and returns Utf8 
string
-        pub fn $NAME<T: StringOffsetSizeTrait>(
-            args: &[ArrayRef],
-        ) -> Result<GenericStringArray<i32>> {
-            if args.len() != 1 {
-                return Err(DataFusionError::Internal(format!(
-                    "{:?} args were supplied but {} takes exactly one 
argument",
-                    args.len(),
-                    String::from(stringify!($NAME)),
-                )));
-            }
+/// # Errors
+/// This function errors when:
+/// * the number of arguments is not 1
+/// * the first argument is not castable to a `GenericStringArray`
+fn unary_binary_function<T, R, F>(
+    args: &[&dyn Array],
+    op: F,
+    name: &str,
+) -> Result<BinaryArray>
+where
+    R: AsRef<[u8]>,
+    T: StringOffsetSizeTrait,
+    F: Fn(&str) -> R,
+{
+    if args.len() != 1 {
+        return Err(DataFusionError::Internal(format!(
+            "{:?} args were supplied but {} takes exactly one argument",
+            args.len(),
+            name,
+        )));
+    }
+
+    let array = args[0]
+        .as_any()
+        .downcast_ref::<GenericStringArray<T>>()
+        .ok_or_else(|| {
+            DataFusionError::Internal("failed to downcast to 
string".to_string())
+        })?;
 
-            let array = args[0]
-                .as_any()
-                .downcast_ref::<GenericStringArray<T>>()
-                .unwrap();
+    // first map is the iterator, second is for the `Option<_>`
+    Ok(array.iter().map(|x| x.map(|x| op(x))).collect())

Review comment:
       Good idea, though, I also though it would work. However, because the 
functions have different signatures, a deref is needed and thus we need to 
write it explicitly. Same for `md5_process`.




----------------------------------------------------------------
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:
[email protected]


Reply via email to