datafusion_functions/crypto/
md5.rs1use crate::crypto::basic::md5;
19use arrow::datatypes::DataType;
20use datafusion_common::{
21 Result,
22 types::{logical_binary, logical_string},
23};
24use datafusion_expr::{
25 ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
26 TypeSignature, Volatility,
27};
28use datafusion_expr_common::signature::{Coercion, TypeSignatureClass};
29use datafusion_macros::user_doc;
30use std::any::Any;
31
32#[user_doc(
33 doc_section(label = "Hashing Functions"),
34 description = "Computes an MD5 128-bit checksum for a string expression.",
35 syntax_example = "md5(expression)",
36 sql_example = r#"```sql
37> select md5('foo');
38+----------------------------------+
39| md5(Utf8("foo")) |
40+----------------------------------+
41| acbd18db4cc2f85cedef654fccc4a4d8 |
42+----------------------------------+
43```"#,
44 standard_argument(name = "expression", prefix = "String")
45)]
46#[derive(Debug, PartialEq, Eq, Hash)]
47pub struct Md5Func {
48 signature: Signature,
49}
50
51impl Default for Md5Func {
52 fn default() -> Self {
53 Self::new()
54 }
55}
56
57impl Md5Func {
58 pub fn new() -> Self {
59 Self {
60 signature: Signature::one_of(
61 vec![
62 TypeSignature::Coercible(vec![Coercion::new_exact(
63 TypeSignatureClass::Native(logical_string()),
64 )]),
65 TypeSignature::Coercible(vec![Coercion::new_exact(
66 TypeSignatureClass::Native(logical_binary()),
67 )]),
68 ],
69 Volatility::Immutable,
70 ),
71 }
72 }
73}
74
75impl ScalarUDFImpl for Md5Func {
76 fn as_any(&self) -> &dyn Any {
77 self
78 }
79
80 fn name(&self) -> &str {
81 "md5"
82 }
83
84 fn signature(&self) -> &Signature {
85 &self.signature
86 }
87
88 fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
89 Ok(DataType::Utf8View)
90 }
91
92 fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
93 md5(&args.args)
94 }
95
96 fn documentation(&self) -> Option<&Documentation> {
97 self.doc()
98 }
99}