You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Jefffrey (via GitHub)" <gi...@apache.org> on 2025/11/13 06:27:21 UTC
[PR] Refactor crypto functions code [datafusion]
Jefffrey opened a new pull request, #18664:
URL: https://github.com/apache/datafusion/pull/18664
## 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.
-->
N/A
## 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.
-->
Deduplicate & simplify code in the crypto functions.
## 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.
-->
TODO
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
Existing tests.
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
Some public methods were removed, though I don't believe they were intended to be used outside of other DataFusion crates.
<!--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor crypto functions code [datafusion]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #18664:
URL: https://github.com/apache/datafusion/pull/18664#discussion_r2523425232
##########
datafusion/functions/src/crypto/md5.rs:
##########
@@ -89,30 +85,10 @@ impl ScalarUDFImpl for Md5Func {
&self.signature
}
- fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
- use DataType::*;
- Ok(match &arg_types[0] {
- LargeUtf8 | LargeBinary => Utf8View,
- Utf8View | Utf8 | Binary | BinaryView => Utf8View,
- Null => Null,
- Dictionary(_, t) => match **t {
- LargeUtf8 | LargeBinary => Utf8View,
- Utf8 | Binary | BinaryView => Utf8View,
- Null => Null,
- _ => {
- return plan_err!(
- "the md5 can only accept strings but got {:?}",
- **t
- );
- }
- },
- other => {
- return plan_err!(
- "The md5 function can only accept strings. Got {other}"
- );
- }
- })
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+ Ok(DataType::Utf8View)
Review Comment:
Why md5 returns Utf8View ?
digest and sha return Binary
##########
datafusion/functions/src/crypto/digest.rs:
##########
@@ -109,3 +114,20 @@ impl ScalarUDFImpl for DigestFunc {
self.doc()
}
}
+
+/// Digest computes a binary hash of the given data, accepts Utf8 or LargeUtf8 and returns a [`ColumnarValue`].
+/// Second argument is the algorithm to use.
+/// Standard algorithms are md5, sha1, sha224, sha256, sha384 and sha512.
Review Comment:
```suggestion
/// Standard algorithms are md5, sha224, sha256, sha384 and sha512.
```
sha1 is not supported in sha.rs and since it is too weak it should not be advertised
##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -17,20 +17,17 @@
//! "crypto" DataFusion functions
-use arrow::array::{
- Array, ArrayRef, BinaryArray, BinaryArrayType, BinaryViewArray, GenericBinaryArray,
- OffsetSizeTrait,
-};
-use arrow::array::{AsArray, GenericStringArray, StringViewArray};
+use arrow::array::{Array, ArrayRef, BinaryArray, BinaryArrayType};
+use arrow::array::{AsArray, StringViewArray};
Review Comment:
nit: The two lines above could be merged.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor crypto functions code [datafusion]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #18664:
URL: https://github.com/apache/datafusion/pull/18664#discussion_r2521921893
##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -84,18 +81,7 @@ define_digest_function!(
"computes blake3 hash digest of the given input"
);
-macro_rules! digest_to_scalar {
Review Comment:
Moved down, closer to where it's actually used
##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -107,23 +93,6 @@ pub enum DigestAlgorithm {
Blake3,
}
-/// Digest computes a binary hash of the given data, accepts Utf8 or LargeUtf8 and returns a [`ColumnarValue`].
-/// Second argument is the algorithm to use.
-/// Standard algorithms are md5, sha1, sha224, sha256, sha384 and sha512.
-pub fn digest(args: &[ColumnarValue]) -> Result<ColumnarValue> {
Review Comment:
Moved to `digest.rs` as that is the only place it is used
##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -251,49 +214,7 @@ impl DigestAlgorithm {
})
}
- /// digest a binary array to their hash values
- pub fn digest_binary_array<T>(self, value: &dyn Array) -> Result<ColumnarValue>
- where
- T: OffsetSizeTrait,
Review Comment:
Essentially inlined these, as didn't see much benefit and they were adding more indirection
##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -198,25 +167,7 @@ fn hex_encode<T: AsRef<[u8]>>(data: T) -> String {
}
s
}
-pub fn utf8_or_binary_to_binary_type(
Review Comment:
Refactored away, was only used for return types in sha/md5/digest (which are now simplified)
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor crypto functions code [datafusion]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #18664:
URL: https://github.com/apache/datafusion/pull/18664#discussion_r2563692386
##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -232,9 +184,21 @@ macro_rules! digest_to_array {
Arc::new(binary_array)
}};
}
+
+macro_rules! digest_to_scalar {
+ ($METHOD: ident, $INPUT:expr) => {{
+ ScalarValue::Binary($INPUT.as_ref().map(|v| {
+ let mut digest = $METHOD::default();
+ digest.update(v);
+ #[allow(deprecated)]
Review Comment:
Thanks for checking, fixed; I think last I tested one of the sha libs was causing a deprecation warning here, so I guess it's been updated since then 🎉
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor crypto functions code [datafusion]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #18664:
URL: https://github.com/apache/datafusion/pull/18664#discussion_r2525562313
##########
datafusion/functions/src/crypto/digest.rs:
##########
@@ -109,3 +114,20 @@ impl ScalarUDFImpl for DigestFunc {
self.doc()
}
}
+
+/// Digest computes a binary hash of the given data, accepts Utf8 or LargeUtf8 and returns a [`ColumnarValue`].
+/// Second argument is the algorithm to use.
+/// Standard algorithms are md5, sha1, sha224, sha256, sha384 and sha512.
Review Comment:
Fixed the docstring; we didn't even support `sha1` for this method anyway so seems it was outdated.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor crypto functions code [datafusion]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey merged PR #18664:
URL: https://github.com/apache/datafusion/pull/18664
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor crypto functions code [datafusion]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #18664:
URL: https://github.com/apache/datafusion/pull/18664#discussion_r2525563004
##########
datafusion/functions/src/crypto/md5.rs:
##########
@@ -89,30 +85,10 @@ impl ScalarUDFImpl for Md5Func {
&self.signature
}
- fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
- use DataType::*;
- Ok(match &arg_types[0] {
- LargeUtf8 | LargeBinary => Utf8View,
- Utf8View | Utf8 | Binary | BinaryView => Utf8View,
- Null => Null,
- Dictionary(_, t) => match **t {
- LargeUtf8 | LargeBinary => Utf8View,
- Utf8 | Binary | BinaryView => Utf8View,
- Null => Null,
- _ => {
- return plan_err!(
- "the md5 can only accept strings but got {:?}",
- **t
- );
- }
- },
- other => {
- return plan_err!(
- "The md5 function can only accept strings. Got {other}"
- );
- }
- })
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+ Ok(DataType::Utf8View)
Review Comment:
Looks like `md5` will naturally hex the output, thus it returns a string whereas the other digest functions return the binary output as is
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor crypto functions code [datafusion]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #18664:
URL: https://github.com/apache/datafusion/pull/18664#discussion_r2525561848
##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -17,20 +17,17 @@
//! "crypto" DataFusion functions
-use arrow::array::{
- Array, ArrayRef, BinaryArray, BinaryArrayType, BinaryViewArray, GenericBinaryArray,
- OffsetSizeTrait,
-};
-use arrow::array::{AsArray, GenericStringArray, StringViewArray};
+use arrow::array::{Array, ArrayRef, BinaryArray, BinaryArrayType};
+use arrow::array::{AsArray, StringViewArray};
Review Comment:
Fixed the imports (wish rustfmt could handle this for us 😔 )
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor crypto functions code [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #18664:
URL: https://github.com/apache/datafusion/pull/18664#discussion_r2561224760
##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -232,9 +184,21 @@ macro_rules! digest_to_array {
Arc::new(binary_array)
}};
}
+
+macro_rules! digest_to_scalar {
+ ($METHOD: ident, $INPUT:expr) => {{
+ ScalarValue::Binary($INPUT.as_ref().map(|v| {
+ let mut digest = $METHOD::default();
+ digest.update(v);
+ #[allow(deprecated)]
Review Comment:
I tried removing this `#[allow(deprecated)]` annotation locally and clippy still passed
##########
datafusion/functions/src/crypto/mod.rs:
##########
@@ -23,16 +23,13 @@ use std::sync::Arc;
pub mod basic;
pub mod digest;
pub mod md5;
-pub mod sha224;
-pub mod sha256;
-pub mod sha384;
-pub mod sha512;
+pub mod sha;
Review Comment:
i think technically this is a breaking API change, but not likely to cause major problems. I tagged this PR as an API change so it shows up in the release notes
##########
datafusion/functions/src/crypto/digest.rs:
##########
@@ -36,16 +38,16 @@ use std::any::Any;
syntax_example = "digest(expression, algorithm)",
sql_example = r#"```sql
> select digest('foo', 'sha256');
-+------------------------------------------+
-| digest(Utf8("foo"), Utf8("sha256")) |
-+------------------------------------------+
-| <binary_hash_result> |
-+------------------------------------------+
++------------------------------------------------------------------+
Review Comment:
I double checked this is indeed the output
```sql
DataFusion CLI v51.0.0
> select digest('foo', 'sha256');
+------------------------------------------------------------------+
| digest(Utf8("foo"),Utf8("sha256")) |
+------------------------------------------------------------------+
| 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae |
+------------------------------------------------------------------+
1 row(s) fetched.
Elapsed 0.030 seconds.
```
##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -232,9 +184,21 @@ macro_rules! digest_to_array {
Arc::new(binary_array)
}};
}
+
+macro_rules! digest_to_scalar {
+ ($METHOD: ident, $INPUT:expr) => {{
+ ScalarValue::Binary($INPUT.as_ref().map(|v| {
+ let mut digest = $METHOD::default();
+ digest.update(v);
+ #[allow(deprecated)]
Review Comment:
```suggestion
```
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org