datafusion_spark/function/string/ascii.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 arrow::datatypes::DataType;
19use datafusion_common::Result;
20use datafusion_expr::ColumnarValue;
21use datafusion_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
22use datafusion_functions::string::ascii::ascii;
23use datafusion_functions::utils::make_scalar_function;
24use std::any::Any;
25
26/// Spark compatible version of the [ascii] function. Differs from the [default ascii function]
27/// in that it is more permissive of input types, for example casting numeric input to string
28/// before executing the function (default version doesn't allow numeric input).
29///
30/// [ascii]: https://spark.apache.org/docs/latest/api/sql/index.html#ascii
31/// [default ascii function]: datafusion_functions::string::ascii::AsciiFunc
32#[derive(Debug, PartialEq, Eq, Hash)]
33pub struct SparkAscii {
34 signature: Signature,
35}
36
37impl Default for SparkAscii {
38 fn default() -> Self {
39 Self::new()
40 }
41}
42
43impl SparkAscii {
44 pub fn new() -> Self {
45 Self {
46 signature: Signature::user_defined(Volatility::Immutable),
47 }
48 }
49}
50
51impl ScalarUDFImpl for SparkAscii {
52 fn as_any(&self) -> &dyn Any {
53 self
54 }
55
56 fn name(&self) -> &str {
57 "ascii"
58 }
59
60 fn signature(&self) -> &Signature {
61 &self.signature
62 }
63
64 fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
65 Ok(DataType::Int32)
66 }
67
68 fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
69 make_scalar_function(ascii, vec![])(&args.args)
70 }
71
72 fn coerce_types(&self, _arg_types: &[DataType]) -> Result<Vec<DataType>> {
73 Ok(vec![DataType::Utf8])
74 }
75}