datafusion_functions/crypto/
sha224.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
18//! "crypto" DataFusion functions
19use super::basic::{sha224, utf8_or_binary_to_binary_type};
20use arrow::datatypes::DataType;
21use datafusion_common::{
22    types::{logical_binary, logical_string, NativeType},
23    Result,
24};
25use datafusion_expr::{
26    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
27    TypeSignature, Volatility,
28};
29use datafusion_expr_common::signature::{Coercion, TypeSignatureClass};
30use datafusion_macros::user_doc;
31use std::any::Any;
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| <sha224_hash_result>                     |
43+------------------------------------------+
44```"#,
45    standard_argument(name = "expression", prefix = "String")
46)]
47#[derive(Debug)]
48pub struct SHA224Func {
49    signature: Signature,
50}
51
52impl Default for SHA224Func {
53    fn default() -> Self {
54        Self::new()
55    }
56}
57
58impl SHA224Func {
59    pub fn new() -> Self {
60        Self {
61            signature: Signature::one_of(
62                vec![
63                    TypeSignature::Coercible(vec![Coercion::new_implicit(
64                        TypeSignatureClass::Native(logical_binary()),
65                        vec![TypeSignatureClass::Native(logical_string())],
66                        NativeType::String,
67                    )]),
68                    TypeSignature::Coercible(vec![Coercion::new_implicit(
69                        TypeSignatureClass::Native(logical_binary()),
70                        vec![TypeSignatureClass::Native(logical_binary())],
71                        NativeType::Binary,
72                    )]),
73                ],
74                Volatility::Immutable,
75            ),
76        }
77    }
78}
79
80impl ScalarUDFImpl for SHA224Func {
81    fn as_any(&self) -> &dyn Any {
82        self
83    }
84
85    fn name(&self) -> &str {
86        "sha224"
87    }
88
89    fn signature(&self) -> &Signature {
90        &self.signature
91    }
92
93    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
94        utf8_or_binary_to_binary_type(&arg_types[0], self.name())
95    }
96
97    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
98        sha224(&args.args)
99    }
100
101    fn documentation(&self) -> Option<&Documentation> {
102        self.doc()
103    }
104}