datafusion_functions/crypto/
md5.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use 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}