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. ########## 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(); + } + + Ok(builder.finish()) + } + }; +} + +crypto_unary_string_function!(md5, md5_process); +crypto_unary_binary_function!(sha224, sha_process::<Sha224>); +crypto_unary_binary_function!(sha256, sha_process::<Sha256>); +crypto_unary_binary_function!(sha384, sha_process::<Sha384>); +crypto_unary_binary_function!(sha512, sha_process::<Sha512>); Review comment: Good catch, I added tests for nulls and empty strings by SQL execution. ########## 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 calling `as_slice()` directly on `SHA2DigestOutput<D>` and It works. Wierd... How is it possible?) ########## File path: rust/datafusion/tests/sql.rs ########## @@ -1859,19 +1859,33 @@ async fn crypto_expressions() -> Result<()> { let mut ctx = ExecutionContext::new(); let sql = "SELECT md5('tom') AS md5_tom, + md5('') AS md5_empty_str, + md5(null) AS md5_null, sha224('tom') AS sha224_tom, + sha224('') AS sha224_empty_str, + sha224(null) AS sha224_null, sha256('tom') AS sha256_tom, + sha256('') AS sha256_empty_str, sha384('tom') AS sha348_tom, - sha512('tom') AS sha512_tom + sha384('') AS sha384_empty_str, + sha512('tom') AS sha512_tom, + sha512('') AS sha512_empty_str "; let actual = execute(&mut ctx, sql).await; let expected = vec![vec![ "34b7da764b21d298ef307d04d8152dc5", - "BF6CB62649C42A9AE3876AB6F6D92AD36CB5414E495F8873292BE4D", - "E1608F75C5D7813F3D4031CB30BFB786507D98137538FF8E128A6FF74E84E643", - "96F5B68AA77848E4FDF5C1CB35DE2DBFAD6FFD7C25D9EA7C6C19B8A4D55A9187EB117C557883F58C16DFAC3E343", - "6E1B9B3FE84068E3751F7AD5E959D6F39AD0F8885D855166F55C659469D3C8B78118C44A2A49C72DDB481CD6D8731034E11CC0307BA843A9B3495CB8D3E" + "d41d8cd98f00b204e9800998ecf8427e", Review comment: Tested with real PostgreSQL. ```sql select md5('tom') AS md5_tom, md5('') AS md5_empty_str, md5(null) AS md5_null, encode(sha224('tom'), 'hex') AS sha224_tom, encode(sha224(''), 'hex') AS sha224_empty_str, sha224(null) AS sha224_null; ``` ```json [ { "md5_tom": "34b7da764b21d298ef307d04d8152dc5", "md5_empty_str": "d41d8cd98f00b204e9800998ecf8427e", "md5_null": null, "sha224_tom": "0bf6cb62649c42a9ae3876ab6f6d92ad36cb5414e495f8873292be4d", "sha224_empty_str": "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", "sha224_null": null } ] ``` ---------------------------------------------------------------- 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]
