datafusion_functions/crypto/
sha512.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::{sha512, 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-512 hash of a binary string.",
36    syntax_example = "sha512(expression)",
37    sql_example = r#"```sql
38> select sha512('foo');
39+-------------------------------------------+
40| sha512(Utf8("foo"))                       |
41+-------------------------------------------+
42| <sha512_hash_result>                      |
43+-------------------------------------------+
44```"#,
45    standard_argument(name = "expression", prefix = "String")
46)]
47#[derive(Debug)]
48pub struct SHA512Func {
49    signature: Signature,
50}
51impl Default for SHA512Func {
52    fn default() -> Self {
53        Self::new()
54    }
55}
56
57impl SHA512Func {
58    pub fn new() -> Self {
59        Self {
60            signature: Signature::one_of(
61                vec![
62                    TypeSignature::Coercible(vec![Coercion::new_implicit(
63                        TypeSignatureClass::Native(logical_binary()),
64                        vec![TypeSignatureClass::Native(logical_string())],
65                        NativeType::String,
66                    )]),
67                    TypeSignature::Coercible(vec![Coercion::new_implicit(
68                        TypeSignatureClass::Native(logical_binary()),
69                        vec![TypeSignatureClass::Native(logical_binary())],
70                        NativeType::Binary,
71                    )]),
72                ],
73                Volatility::Immutable,
74            ),
75        }
76    }
77}
78impl ScalarUDFImpl for SHA512Func {
79    fn as_any(&self) -> &dyn Any {
80        self
81    }
82
83    fn name(&self) -> &str {
84        "sha512"
85    }
86
87    fn signature(&self) -> &Signature {
88        &self.signature
89    }
90
91    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
92        utf8_or_binary_to_binary_type(&arg_types[0], self.name())
93    }
94
95    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
96        sha512(&args.args)
97    }
98
99    fn documentation(&self) -> Option<&Documentation> {
100        self.doc()
101    }
102}