use super::basic::{sha512, utf8_or_binary_to_binary_type};
use arrow::datatypes::DataType;
use datafusion_common::Result;
use datafusion_expr::scalar_doc_sections::DOC_SECTION_HASHING;
use datafusion_expr::{
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
};
use std::any::Any;
use std::sync::OnceLock;
#[derive(Debug)]
pub struct SHA512Func {
signature: Signature,
}
impl Default for SHA512Func {
fn default() -> Self {
Self::new()
}
}
impl SHA512Func {
pub fn new() -> Self {
use DataType::*;
Self {
signature: Signature::uniform(
1,
vec![Utf8View, Utf8, LargeUtf8, Binary, LargeBinary],
Volatility::Immutable,
),
}
}
}
impl ScalarUDFImpl for SHA512Func {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"sha512"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
utf8_or_binary_to_binary_type(&arg_types[0], self.name())
}
fn invoke_batch(
&self,
args: &[ColumnarValue],
_number_rows: usize,
) -> Result<ColumnarValue> {
sha512(args)
}
fn documentation(&self) -> Option<&Documentation> {
Some(get_sha512_doc())
}
}
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
fn get_sha512_doc() -> &'static Documentation {
DOCUMENTATION.get_or_init(|| {
Documentation::builder(
DOC_SECTION_HASHING,
"Computes the SHA-512 hash of a binary string.",
"sha512(expression)",
)
.with_sql_example(
r#"```sql
> select sha512('foo');
+-------------------------------------------+
| sha512(Utf8("foo")) |
+-------------------------------------------+
| <sha512_hash_result> |
+-------------------------------------------+
```"#,
)
.with_argument("expression", "String")
.build()
})
}