Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ recursive = "0.1.1"
regex = "1.12"
rstest = "0.26.1"
serde_json = "1"
sha2 = "^0.10.9"
sha2 = "^0.11.0"
sqlparser = { version = "0.61.0", default-features = false, features = ["std", "visitor"] }
strum = "0.28.0"
strum_macros = "0.28.0"
Expand Down
2 changes: 1 addition & 1 deletion datafusion/functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ datafusion-macros = { workspace = true }
hex = { workspace = true, optional = true }
itertools = { workspace = true }
log = { workspace = true }
md-5 = { version = "^0.10.0", optional = true }
md-5 = { version = "^0.11.0", optional = true }
memchr = { workspace = true }
num-traits = { workspace = true }
rand = { workspace = true }
Expand Down
52 changes: 28 additions & 24 deletions datafusion/functions/src/crypto/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use arrow::array::{Array, ArrayRef, AsArray, BinaryArray, BinaryArrayType};
use arrow::datatypes::DataType;
use blake2::{Blake2b512, Blake2s256, Digest};
use blake2::{Blake2b512, Blake2s256};
use blake3::Hasher as Blake3;

use arrow::compute::StringArrayType;
Expand Down Expand Up @@ -85,7 +85,8 @@ impl fmt::Display for DigestAlgorithm {
}

macro_rules! digest_to_array {
($METHOD:ident, $INPUT:expr) => {{
($MODULE:ident, $METHOD:ident, $INPUT:expr) => {{
use $MODULE::Digest;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

This is a reasonable way to handle the mixed digest versions. Another readability option would be to use UFCS, e.g. <$METHOD as $MODULE::Digest>::digest(...), so the trait version is explicit at the call site. Not necessary for this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a nice idea, though I think it makes it a bit more verbose, e.g.

ScalarValue::Binary(
    $INPUT.map(|v| <$METHOD as $MODULE::Digest>::digest(v).as_slice().to_vec()),
)

let binary_array: BinaryArray = $INPUT
.iter()
.map(|x| x.map(|x| $METHOD::digest(x)))
Expand All @@ -95,20 +96,23 @@ macro_rules! digest_to_array {
}

macro_rules! digest_to_scalar {
($METHOD: ident, $INPUT:expr) => {{ ScalarValue::Binary($INPUT.map(|v| $METHOD::digest(v).as_slice().to_vec())) }};
($MODULE: ident, $METHOD: ident, $INPUT:expr) => {{
use $MODULE::Digest;
ScalarValue::Binary($INPUT.map(|v| $METHOD::digest(v).as_slice().to_vec()))
}};
}

impl DigestAlgorithm {
/// digest an optional string to its hash value, null values are returned as is
fn digest_scalar(self, value: Option<&[u8]>) -> ColumnarValue {
ColumnarValue::Scalar(match self {
Self::Md5 => digest_to_scalar!(Md5, value),
Self::Sha224 => digest_to_scalar!(Sha224, value),
Self::Sha256 => digest_to_scalar!(Sha256, value),
Self::Sha384 => digest_to_scalar!(Sha384, value),
Self::Sha512 => digest_to_scalar!(Sha512, value),
Self::Blake2b => digest_to_scalar!(Blake2b512, value),
Self::Blake2s => digest_to_scalar!(Blake2s256, value),
Self::Md5 => digest_to_scalar!(md5, Md5, value),
Self::Sha224 => digest_to_scalar!(sha2, Sha224, value),
Self::Sha256 => digest_to_scalar!(sha2, Sha256, value),
Self::Sha384 => digest_to_scalar!(sha2, Sha384, value),
Self::Sha512 => digest_to_scalar!(sha2, Sha512, value),
Self::Blake2b => digest_to_scalar!(blake2, Blake2b512, value),
Self::Blake2s => digest_to_scalar!(blake2, Blake2s256, value),
Self::Blake3 => ScalarValue::Binary(value.map(|v| {
let mut digest = Blake3::default();
digest.update(v);
Expand All @@ -125,13 +129,13 @@ impl DigestAlgorithm {
StringArrType: StringArrayType<'a>,
{
match self {
Self::Md5 => digest_to_array!(Md5, input_value),
Self::Sha224 => digest_to_array!(Sha224, input_value),
Self::Sha256 => digest_to_array!(Sha256, input_value),
Self::Sha384 => digest_to_array!(Sha384, input_value),
Self::Sha512 => digest_to_array!(Sha512, input_value),
Self::Blake2b => digest_to_array!(Blake2b512, input_value),
Self::Blake2s => digest_to_array!(Blake2s256, input_value),
Self::Md5 => digest_to_array!(md5, Md5, input_value),
Self::Sha224 => digest_to_array!(sha2, Sha224, input_value),
Self::Sha256 => digest_to_array!(sha2, Sha256, input_value),
Self::Sha384 => digest_to_array!(sha2, Sha384, input_value),
Self::Sha512 => digest_to_array!(sha2, Sha512, input_value),
Self::Blake2b => digest_to_array!(blake2, Blake2b512, input_value),
Self::Blake2s => digest_to_array!(blake2, Blake2s256, input_value),
Self::Blake3 => {
let binary_array: BinaryArray = input_value
.iter()
Expand All @@ -156,13 +160,13 @@ impl DigestAlgorithm {
BinaryArrType: BinaryArrayType<'a>,
{
match self {
Self::Md5 => digest_to_array!(Md5, input_value),
Self::Sha224 => digest_to_array!(Sha224, input_value),
Self::Sha256 => digest_to_array!(Sha256, input_value),
Self::Sha384 => digest_to_array!(Sha384, input_value),
Self::Sha512 => digest_to_array!(Sha512, input_value),
Self::Blake2b => digest_to_array!(Blake2b512, input_value),
Self::Blake2s => digest_to_array!(Blake2s256, input_value),
Self::Md5 => digest_to_array!(md5, Md5, input_value),
Self::Sha224 => digest_to_array!(sha2, Sha224, input_value),
Self::Sha256 => digest_to_array!(sha2, Sha256, input_value),
Self::Sha384 => digest_to_array!(sha2, Sha384, input_value),
Self::Sha512 => digest_to_array!(sha2, Sha512, input_value),
Self::Blake2b => digest_to_array!(blake2, Blake2b512, input_value),
Self::Blake2s => digest_to_array!(blake2, Blake2s256, input_value),
Self::Blake3 => {
let binary_array: BinaryArray = input_value
.iter()
Expand Down
Loading