datafusion_functions/crypto/
sha.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::{DigestAlgorithm, digest_process};
19
20use arrow::datatypes::DataType;
21use datafusion_common::{
22    Result,
23    types::{logical_binary, logical_string},
24    utils::take_function_args,
25};
26use datafusion_expr::{
27    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
28    TypeSignature, Volatility,
29};
30use datafusion_expr_common::signature::{Coercion, TypeSignatureClass};
31use datafusion_macros::user_doc;
32use std::any::Any;
33
34#[user_doc(
35    doc_section(label = "Hashing Functions"),
36    description = "Computes the SHA-224 hash of a binary string.",
37    syntax_example = "sha224(expression)",
38    sql_example = r#"```sql
39> select sha224('foo');
40+----------------------------------------------------------+
41| sha224(Utf8("foo"))                                      |
42+----------------------------------------------------------+
43| 0808f64e60d58979fcb676c96ec938270dea42445aeefcd3a4e6f8db |
44+----------------------------------------------------------+
45```"#,
46    standard_argument(name = "expression", prefix = "String")
47)]
48struct SHA224Doc;
49
50#[user_doc(
51    doc_section(label = "Hashing Functions"),
52    description = "Computes the SHA-256 hash of a binary string.",
53    syntax_example = "sha256(expression)",
54    sql_example = r#"```sql
55> select sha256('foo');
56+------------------------------------------------------------------+
57| sha256(Utf8("foo"))                                              |
58+------------------------------------------------------------------+
59| 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae |
60+------------------------------------------------------------------+
61```"#,
62    standard_argument(name = "expression", prefix = "String")
63)]
64struct SHA256Doc;
65
66#[user_doc(
67    doc_section(label = "Hashing Functions"),
68    description = "Computes the SHA-384 hash of a binary string.",
69    syntax_example = "sha384(expression)",
70    sql_example = r#"```sql
71> select sha384('foo');
72+--------------------------------------------------------------------------------------------------+
73| sha384(Utf8("foo"))                                                                              |
74+--------------------------------------------------------------------------------------------------+
75| 98c11ffdfdd540676b1a137cb1a22b2a70350c9a44171d6b1180c6be5cbb2ee3f79d532c8a1dd9ef2e8e08e752a3babb |
76+--------------------------------------------------------------------------------------------------+
77```"#,
78    standard_argument(name = "expression", prefix = "String")
79)]
80struct SHA384Doc;
81
82#[user_doc(
83    doc_section(label = "Hashing Functions"),
84    description = "Computes the SHA-512 hash of a binary string.",
85    syntax_example = "sha512(expression)",
86    sql_example = r#"```sql
87> select sha512('foo');
88+----------------------------------------------------------------------------------------------------------------------------------+
89| sha512(Utf8("foo"))                                                                                                              |
90+----------------------------------------------------------------------------------------------------------------------------------+
91| f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7 |
92+----------------------------------------------------------------------------------------------------------------------------------+
93```"#,
94    standard_argument(name = "expression", prefix = "String")
95)]
96struct SHA512Doc;
97
98#[derive(Debug, PartialEq, Eq, Hash)]
99pub struct SHAFunc {
100    signature: Signature,
101    name: &'static str,
102    algorithm: DigestAlgorithm,
103}
104
105impl SHAFunc {
106    pub fn sha224() -> Self {
107        Self::new("sha224", DigestAlgorithm::Sha224)
108    }
109
110    pub fn sha256() -> Self {
111        Self::new("sha256", DigestAlgorithm::Sha256)
112    }
113
114    pub fn sha384() -> Self {
115        Self::new("sha384", DigestAlgorithm::Sha384)
116    }
117
118    pub fn sha512() -> Self {
119        Self::new("sha512", DigestAlgorithm::Sha512)
120    }
121
122    fn new(name: &'static str, algorithm: DigestAlgorithm) -> Self {
123        Self {
124            signature: Signature::one_of(
125                vec![
126                    TypeSignature::Coercible(vec![Coercion::new_exact(
127                        TypeSignatureClass::Native(logical_string()),
128                    )]),
129                    TypeSignature::Coercible(vec![Coercion::new_exact(
130                        TypeSignatureClass::Native(logical_binary()),
131                    )]),
132                ],
133                Volatility::Immutable,
134            ),
135            name,
136            algorithm,
137        }
138    }
139}
140
141impl ScalarUDFImpl for SHAFunc {
142    fn as_any(&self) -> &dyn Any {
143        self
144    }
145
146    fn name(&self) -> &str {
147        self.name
148    }
149
150    fn signature(&self) -> &Signature {
151        &self.signature
152    }
153
154    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
155        Ok(DataType::Binary)
156    }
157
158    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
159        let [data] = take_function_args(self.name(), args.args)?;
160        digest_process(&data, self.algorithm)
161    }
162
163    fn documentation(&self) -> Option<&Documentation> {
164        match self.algorithm {
165            DigestAlgorithm::Sha224 => SHA224Doc {}.doc(),
166            DigestAlgorithm::Sha256 => SHA256Doc {}.doc(),
167            DigestAlgorithm::Sha384 => SHA384Doc {}.doc(),
168            DigestAlgorithm::Sha512 => SHA512Doc {}.doc(),
169            DigestAlgorithm::Md5
170            | DigestAlgorithm::Blake2s
171            | DigestAlgorithm::Blake2b
172            | DigestAlgorithm::Blake3 => unreachable!(),
173        }
174    }
175}