Skip to content
Open
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
97 changes: 4 additions & 93 deletions datafusion/functions/src/crypto/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,73 +17,22 @@

//! "crypto" DataFusion functions

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

use arrow::compute::StringArrayType;
use datafusion_common::{
DataFusionError, Result, ScalarValue, exec_err, internal_err, plan_err,
utils::take_function_args,
};
use datafusion_common::{DataFusionError, Result, ScalarValue, exec_err, plan_err};
use datafusion_expr::ColumnarValue;
use md5::Md5;
use sha2::{Sha224, Sha256, Sha384, Sha512};
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;

macro_rules! define_digest_function {
($NAME: ident, $METHOD: ident, $DOC: expr) => {
#[doc = $DOC]
pub fn $NAME(args: &[ColumnarValue]) -> Result<ColumnarValue> {
let [data] = take_function_args(&DigestAlgorithm::$METHOD.to_string(), args)?;
digest_process(data, DigestAlgorithm::$METHOD)
}
};
}
define_digest_function!(
sha224,
Sha224,
"computes sha224 hash digest of the given input"
);
Comment on lines -40 to -53
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are unused but part of our public API; I don't see a good reason to have them exposed so removed them

define_digest_function!(
sha256,
Sha256,
"computes sha256 hash digest of the given input"
);
define_digest_function!(
sha384,
Sha384,
"computes sha384 hash digest of the given input"
);
define_digest_function!(
sha512,
Sha512,
"computes sha512 hash digest of the given input"
);
define_digest_function!(
blake2b,
Blake2b,
"computes blake2b hash digest of the given input"
);
define_digest_function!(
blake2s,
Blake2s,
"computes blake2s hash digest of the given input"
);
define_digest_function!(
blake3,
Blake3,
"computes blake3 hash digest of the given input"
);

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum DigestAlgorithm {
pub(crate) enum DigestAlgorithm {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed from public API

Md5,
Sha224,
Sha256,
Expand Down Expand Up @@ -135,44 +84,6 @@ impl fmt::Display for DigestAlgorithm {
}
}

/// computes md5 hash digest of the given input
pub fn md5(args: &[ColumnarValue]) -> Result<ColumnarValue> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed from public API, also moved to md5 file which is the only place its used

let [data] = take_function_args("md5", args)?;
let value = digest_process(data, DigestAlgorithm::Md5)?;

// md5 requires special handling because of its unique utf8view return type
Ok(match value {
ColumnarValue::Array(array) => {
let binary_array = as_binary_array(&array)?;
let string_array: StringViewArray = binary_array
.iter()
.map(|opt| opt.map(hex_encode::<_>))
.collect();
ColumnarValue::Array(Arc::new(string_array))
}
ColumnarValue::Scalar(ScalarValue::Binary(opt)) => {
ColumnarValue::Scalar(ScalarValue::Utf8View(opt.map(hex_encode::<_>)))
}
_ => return internal_err!("Impossibly got invalid results from digest"),
})
}

/// Hex encoding lookup table for fast byte-to-hex conversion
const HEX_CHARS_LOWER: &[u8; 16] = b"0123456789abcdef";

/// Fast hex encoding using a lookup table instead of format strings.
/// This is significantly faster than using `write!("{:02x}")` for each byte.
#[inline]
fn hex_encode<T: AsRef<[u8]>>(data: T) -> String {
let bytes = data.as_ref();
let mut s = String::with_capacity(bytes.len() * 2);
for &b in bytes {
s.push(HEX_CHARS_LOWER[(b >> 4) as usize] as char);
s.push(HEX_CHARS_LOWER[(b & 0x0f) as usize] as char);
}
s
}

macro_rules! digest_to_array {
($METHOD:ident, $INPUT:expr) => {{
let binary_array: BinaryArray = $INPUT
Expand Down Expand Up @@ -269,7 +180,7 @@ impl DigestAlgorithm {
}
}

pub fn digest_process(
pub(crate) fn digest_process(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed from public API

value: &ColumnarValue,
digest_algorithm: DigestAlgorithm,
) -> Result<ColumnarValue> {
Expand Down
47 changes: 43 additions & 4 deletions datafusion/functions/src/crypto/md5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
// specific language governing permissions and limitations
// under the License.

use crate::crypto::basic::md5;
use arrow::datatypes::DataType;
use arrow::{array::StringViewArray, datatypes::DataType};
use datafusion_common::{
Result,
Result, ScalarValue,
cast::as_binary_array,
internal_err,
types::{logical_binary, logical_string},
utils::take_function_args,
};
use datafusion_expr::{
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
TypeSignature, Volatility,
};
use datafusion_expr_common::signature::{Coercion, TypeSignatureClass};
use datafusion_macros::user_doc;
use std::any::Any;
use std::{any::Any, sync::Arc};

use crate::crypto::basic::{DigestAlgorithm, digest_process};

#[user_doc(
doc_section(label = "Hashing Functions"),
Expand Down Expand Up @@ -97,3 +101,38 @@ impl ScalarUDFImpl for Md5Func {
self.doc()
}
}

/// Hex encoding lookup table for fast byte-to-hex conversion
const HEX_CHARS_LOWER: &[u8; 16] = b"0123456789abcdef";

/// Fast hex encoding using a lookup table instead of format strings.
/// This is significantly faster than using `write!("{:02x}")` for each byte.
#[inline]
fn hex_encode(data: impl AsRef<[u8]>) -> String {
let bytes = data.as_ref();
let mut s = String::with_capacity(bytes.len() * 2);
for &b in bytes {
s.push(HEX_CHARS_LOWER[(b >> 4) as usize] as char);
s.push(HEX_CHARS_LOWER[(b & 0x0f) as usize] as char);
}
s
}

fn md5(args: &[ColumnarValue]) -> Result<ColumnarValue> {
let [data] = take_function_args("md5", args)?;
let value = digest_process(data, DigestAlgorithm::Md5)?;

// md5 requires special handling because of its unique utf8view return type
Ok(match value {
ColumnarValue::Array(array) => {
let binary_array = as_binary_array(&array)?;
let string_array: StringViewArray =
binary_array.iter().map(|opt| opt.map(hex_encode)).collect();
ColumnarValue::Array(Arc::new(string_array))
}
ColumnarValue::Scalar(ScalarValue::Binary(opt)) => {
ColumnarValue::Scalar(ScalarValue::Utf8View(opt.map(hex_encode)))
}
_ => return internal_err!("Impossibly got invalid results from digest"),
})
}