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 2021/01/09 12:57:49 UTC

[GitHub] [arrow] ovr commented on a change in pull request #9139: ARROW-11188: [Rust] Support crypto functions from PostgreSQL dialect …

ovr commented on a change in pull request #9139:
URL: https://github.com/apache/arrow/pull/9139#discussion_r554424637



##########
File path: rust/datafusion/src/physical_plan/crypto_expressions.rs
##########
@@ -0,0 +1,114 @@
+// 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.
+
+//! Crypto expressions
+
+use md5::Md5;
+use sha2::{
+    digest::Output as SHA2DigestOutput, Digest as SHA2Digest, Sha224, Sha256, Sha384,
+    Sha512,
+};
+
+use crate::error::{DataFusionError, Result};
+use arrow::array::{
+    ArrayRef, BinaryBuilder, GenericBinaryArray, GenericStringArray,
+    StringOffsetSizeTrait,
+};
+
+fn md5_process(input: &str) -> String {
+    let mut digest = Md5::default();
+    digest.update(&input);
+
+    let mut result = String::new();
+
+    for byte in &digest.finalize() {
+        result.push_str(&format!("{:02x}", byte));
+    }
+
+    result
+}
+
+// It's not possible to return &[u8], because trait in trait without short lifetime
+fn sha_process<D: SHA2Digest + Default>(input: &str) -> SHA2DigestOutput<D> {
+    let mut digest = D::default();
+    digest.update(&input);
+
+    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)),
+                )));
+            }
+
+            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| $FUNC(x))).collect())
+        }
+    };
+}
+
+macro_rules! crypto_unary_binary_function {
+    ($NAME:ident, $FUNC:expr) => {
+        /// crypto function that accepts Utf8 or LargeUtf8 and returns Binary
+        pub fn $NAME<T: StringOffsetSizeTrait>(
+            args: &[ArrayRef],
+        ) -> Result<GenericBinaryArray<i32>> {
+            if args.len() != 1 {
+                return Err(DataFusionError::Internal(format!(
+                    "{:?} args were supplied but {} takes exactly one argument",
+                    args.len(),
+                    String::from(stringify!($NAME)),
+                )));
+            }
+
+            let array = args[0]
+                .as_any()
+                .downcast_ref::<GenericStringArray<T>>()
+                .unwrap();
+
+            let mut builder = BinaryBuilder::new(args.len());
+
+            for value in array.iter() {
+                builder
+                    .append_value($FUNC(value.unwrap()).as_slice())
+                    .unwrap();
+            }

Review comment:
       Yes, it will crash.
   
   Replaced this with `Ok(array.iter().map(|x| x.map(|x| $FUNC(x))).collect())` without as_slice
   And it works. Weird, Where `SHA2DigestOutput<D>` converted to slice.
   
   




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