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