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::Result;
22use datafusion_expr::{
23    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
24    Volatility,
25};
26use datafusion_macros::user_doc;
27use std::any::Any;
28
29#[user_doc(
30    doc_section(label = "Hashing Functions"),
31    description = "Computes the SHA-224 hash of a binary string.",
32    syntax_example = "sha224(expression)",
33    sql_example = r#"```sql
34> select sha224('foo');
35+------------------------------------------+
36| sha224(Utf8("foo"))                      |
37+------------------------------------------+
38| <sha224_hash_result>                     |
39+------------------------------------------+
40```"#,
41    standard_argument(name = "expression", prefix = "String")
42)]
43#[derive(Debug)]
44pub struct SHA224Func {
45    signature: Signature,
46}
47
48impl Default for SHA224Func {
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54impl SHA224Func {
55    pub fn new() -> Self {
56        use DataType::*;
57        Self {
58            signature: Signature::uniform(
59                1,
60                vec![Utf8View, Utf8, LargeUtf8, Binary, LargeBinary],
61                Volatility::Immutable,
62            ),
63        }
64    }
65}
66
67impl ScalarUDFImpl for SHA224Func {
68    fn as_any(&self) -> &dyn Any {
69        self
70    }
71
72    fn name(&self) -> &str {
73        "sha224"
74    }
75
76    fn signature(&self) -> &Signature {
77        &self.signature
78    }
79
80    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
81        utf8_or_binary_to_binary_type(&arg_types[0], self.name())
82    }
83
84    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
85        sha224(&args.args)
86    }
87
88    fn documentation(&self) -> Option<&Documentation> {
89        self.doc()
90    }
91}