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/10/09 01:50:41 UTC
[GitHub] [arrow-datafusion] Jimexist opened a new pull request #1090: add digest(utf8, method) function
Jimexist opened a new pull request #1090:
URL: https://github.com/apache/arrow-datafusion/pull/1090
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #.
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] houqp commented on a change in pull request #1090: add digest(utf8, method) function and refactor all current hash digest functions
Posted by GitBox <gi...@apache.org>.
houqp commented on a change in pull request #1090:
URL: https://github.com/apache/arrow-datafusion/pull/1090#discussion_r725518578
##########
File path: datafusion/src/physical_plan/crypto_expressions.rs
##########
@@ -16,183 +16,283 @@
// under the License.
//! Crypto expressions
-use std::sync::Arc;
-
-use md5::Md5;
-use sha2::{
- digest::Output as SHA2DigestOutput, Digest as SHA2Digest, Sha224, Sha256, Sha384,
- Sha512,
-};
-
+use super::ColumnarValue;
use crate::{
error::{DataFusionError, Result},
scalar::ScalarValue,
};
use arrow::{
- array::{Array, BinaryArray, GenericStringArray, StringOffsetSizeTrait},
+ array::{
+ Array, ArrayRef, BinaryArray, GenericStringArray, StringArray,
+ StringOffsetSizeTrait,
+ },
datatypes::DataType,
};
+use md5::Md5;
+use sha2::{Digest as SHA2Digest, Sha224, Sha256, Sha384, Sha512};
+use std::any::type_name;
+use std::sync::Arc;
+use std::{fmt, str::FromStr};
-use super::{string_expressions::unary_string_function, ColumnarValue};
-
-/// Computes the md5 of a string.
-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
+/// digest algorithm
+#[derive(Debug, Copy, Clone)]
+enum DigestAlgorithm {
+ Md5,
+ Sha224,
+ Sha256,
+ Sha384,
+ Sha512,
}
-// 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! downcast_string_arg {
+ ($ARG:expr, $NAME:expr, $T:ident) => {{
+ $ARG.as_any()
+ .downcast_ref::<GenericStringArray<T>>()
+ .ok_or_else(|| {
+ DataFusionError::Internal(format!(
+ "could not cast {} to {}",
+ $NAME,
+ type_name::<GenericStringArray<T>>()
+ ))
+ })?
+ }};
}
-/// # 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())
- })?;
-
- // first map is the iterator, second is for the `Option<_>`
- Ok(array.iter().map(|x| x.map(|x| op(x))).collect())
-}
-
-fn handle<F, R>(args: &[ColumnarValue], op: F, name: &str) -> Result<ColumnarValue>
-where
- R: AsRef<[u8]>,
- F: Fn(&str) -> R,
-{
- match &args[0] {
+fn digest_process(
+ value: &ColumnarValue,
+ digest_algorithm: DigestAlgorithm,
+ return_utf8: bool,
Review comment:
Given that we only have md5 as the special case for returning value as hex, I think the code would be simpler if we isolate the return hex logic into the old `pub fn md5(args: &[ColumnarValue]) -> Result<ColumnarValue> ` function. Then we can avoid this `return_utf8` flag passing and checking for all other functions. Efficiency wise, it'a also better to have the return type specified using the type system statically instead of switching behaviors at runtime.
##########
File path: datafusion/src/physical_plan/crypto_expressions.rs
##########
@@ -16,183 +16,283 @@
// under the License.
//! Crypto expressions
-use std::sync::Arc;
-
-use md5::Md5;
-use sha2::{
- digest::Output as SHA2DigestOutput, Digest as SHA2Digest, Sha224, Sha256, Sha384,
- Sha512,
-};
-
+use super::ColumnarValue;
use crate::{
error::{DataFusionError, Result},
scalar::ScalarValue,
};
use arrow::{
- array::{Array, BinaryArray, GenericStringArray, StringOffsetSizeTrait},
+ array::{
+ Array, ArrayRef, BinaryArray, GenericStringArray, StringArray,
+ StringOffsetSizeTrait,
+ },
datatypes::DataType,
};
+use md5::Md5;
+use sha2::{Digest as SHA2Digest, Sha224, Sha256, Sha384, Sha512};
+use std::any::type_name;
+use std::sync::Arc;
+use std::{fmt, str::FromStr};
-use super::{string_expressions::unary_string_function, ColumnarValue};
-
-/// Computes the md5 of a string.
-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
+/// digest algorithm
+#[derive(Debug, Copy, Clone)]
+enum DigestAlgorithm {
+ Md5,
+ Sha224,
+ Sha256,
+ Sha384,
+ Sha512,
}
-// 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! downcast_string_arg {
Review comment:
looks like this macro is only used once in `digest_array`, if so, it would be better to manually expand it in `digest_array` or turn it into an inline function. macros are usually harder to maintain and debug.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] Jimexist commented on pull request #1090: add digest(utf8, method) function and refactor all current hash digest functions
Posted by GitBox <gi...@apache.org>.
Jimexist commented on pull request #1090:
URL: https://github.com/apache/arrow-datafusion/pull/1090#issuecomment-939303503
please note that `md5` returns hex string, `digest` returns bytes
```
-[ RECORD 1 ]------------------------------------------------------------------------------------------------------------------------------
md5 | c4ca4238a0b923820dcc509a6f75849b
digest | \xc4ca4238a0b923820dcc509a6f75849b
sha512 | \x4dff4ea340f0a823f15d3f4f01ab62eae0e5da579ccb851f8db9dfe84c58b2b37b89903a740e1ee172da793a6e79d560e5f7f9bd058a12a280433ed6fa46510a
digest | \x4dff4ea340f0a823f15d3f4f01ab62eae0e5da579ccb851f8db9dfe84c58b2b37b89903a740e1ee172da793a6e79d560e5f7f9bd058a12a280433ed6fa46510a
```
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] Dandandan commented on pull request #1090: add digest(utf8, method) function and refactor all current hash digest functions
Posted by GitBox <gi...@apache.org>.
Dandandan commented on pull request #1090:
URL: https://github.com/apache/arrow-datafusion/pull/1090#issuecomment-939427654
Thanks @Jimexist
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] Dandandan commented on a change in pull request #1090: add digest(utf8, method) function and refactor all current hash digest functions
Posted by GitBox <gi...@apache.org>.
Dandandan commented on a change in pull request #1090:
URL: https://github.com/apache/arrow-datafusion/pull/1090#discussion_r725597492
##########
File path: datafusion/tests/sql.rs
##########
@@ -3997,32 +3997,59 @@ async fn test_boolean_expressions() -> Result<()> {
#[cfg_attr(not(feature = "crypto_expressions"), ignore)]
async fn test_crypto_expressions() -> Result<()> {
test_expression!("md5('tom')", "34b7da764b21d298ef307d04d8152dc5");
+ test_expression!("digest('tom','md5')", "34b7da764b21d298ef307d04d8152dc5");
test_expression!("md5('')", "d41d8cd98f00b204e9800998ecf8427e");
+ test_expression!("digest('','md5')", "d41d8cd98f00b204e9800998ecf8427e");
test_expression!("md5(NULL)", "NULL");
+ test_expression!("digest(NULL,'md5')", "NULL");
test_expression!(
"sha224('tom')",
"0bf6cb62649c42a9ae3876ab6f6d92ad36cb5414e495f8873292be4d"
);
+ test_expression!(
+ "digest('tom','sha224')",
+ "0bf6cb62649c42a9ae3876ab6f6d92ad36cb5414e495f8873292be4d"
+ );
test_expression!(
"sha224('')",
"d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"
);
+ test_expression!(
+ "digest('','sha224')",
+ "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"
+ );
test_expression!("sha224(NULL)", "NULL");
+ test_expression!("digest(NULL,'sha224')", "NULL");
test_expression!(
"sha256('tom')",
"e1608f75c5d7813f3d4031cb30bfb786507d98137538ff8e128a6ff74e84e643"
);
+ test_expression!(
+ "digest('tom','sha256')",
+ "e1608f75c5d7813f3d4031cb30bfb786507d98137538ff8e128a6ff74e84e643"
+ );
test_expression!(
"sha256('')",
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
);
+ test_expression!(
+ "digest('','sha256')",
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
+ );
test_expression!("sha256(NULL)", "NULL");
+ test_expression!("digest(NULL,'sha256')", "NULL");
test_expression!("sha384('tom')", "096f5b68aa77848e4fdf5c1c0b350de2dbfad60ffd7c25d9ea07c6c19b8a4d55a9187eb117c557883f58c16dfac3e343");
+ test_expression!("digest('tom','sha384')", "096f5b68aa77848e4fdf5c1c0b350de2dbfad60ffd7c25d9ea07c6c19b8a4d55a9187eb117c557883f58c16dfac3e343");
Review comment:
Nice
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] houqp commented on pull request #1090: add digest(utf8, method) function and refactor all current hash digest functions
Posted by GitBox <gi...@apache.org>.
houqp commented on pull request #1090:
URL: https://github.com/apache/arrow-datafusion/pull/1090#issuecomment-939338589
builtin `pgcrypto` without having to execute `CREATE EXTENSION IF NOT EXISTS pgcrypto;`, nice :+1:
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] Dandandan merged pull request #1090: add digest(utf8, method) function and refactor all current hash digest functions
Posted by GitBox <gi...@apache.org>.
Dandandan merged pull request #1090:
URL: https://github.com/apache/arrow-datafusion/pull/1090
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] Jimexist edited a comment on pull request #1090: add digest(utf8, method) function and refactor all current hash digest functions
Posted by GitBox <gi...@apache.org>.
Jimexist edited a comment on pull request #1090:
URL: https://github.com/apache/arrow-datafusion/pull/1090#issuecomment-939303503
please note that `md5` returns hex string, `digest` returns bytes
```
[postgres] # select md5('1'), digest('1', 'md5'), sha512('1'), digest('1', 'sha512');
-[ RECORD 1 ]------------------------------------------------------------------------------------------------------------------------------
md5 | c4ca4238a0b923820dcc509a6f75849b
digest | \xc4ca4238a0b923820dcc509a6f75849b
sha512 | \x4dff4ea340f0a823f15d3f4f01ab62eae0e5da579ccb851f8db9dfe84c58b2b37b89903a740e1ee172da793a6e79d560e5f7f9bd058a12a280433ed6fa46510a
digest | \x4dff4ea340f0a823f15d3f4f01ab62eae0e5da579ccb851f8db9dfe84c58b2b37b89903a740e1ee172da793a6e79d560e5f7f9bd058a12a280433ed6fa46510a
```
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] houqp commented on pull request #1090: add digest(utf8, method) function and refactor all current hash digest functions
Posted by GitBox <gi...@apache.org>.
houqp commented on pull request #1090:
URL: https://github.com/apache/arrow-datafusion/pull/1090#issuecomment-939233190
Looks good to me overall, but one of the tests is failing.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] houqp edited a comment on pull request #1090: add digest(utf8, method) function and refactor all current hash digest functions
Posted by GitBox <gi...@apache.org>.
houqp edited a comment on pull request #1090:
URL: https://github.com/apache/arrow-datafusion/pull/1090#issuecomment-939338589
builtin `pgcrypto` support without having to execute `CREATE EXTENSION IF NOT EXISTS pgcrypto;`, nice :+1:
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] Jimexist commented on pull request #1090: add digest(utf8, method) function and refactor all current hash digest functions
Posted by GitBox <gi...@apache.org>.
Jimexist commented on pull request #1090:
URL: https://github.com/apache/arrow-datafusion/pull/1090#issuecomment-939398669
@houqp all suggestions incorporated, PTAL
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org