datafusion_functions/core/
arrowtypeof.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::{utils::take_function_args, Result, ScalarValue};
20use datafusion_expr::{ColumnarValue, Documentation, ScalarFunctionArgs};
21use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
22use datafusion_macros::user_doc;
23use std::any::Any;
24
25#[user_doc(
26    doc_section(label = "Other Functions"),
27    description = "Returns the name of the underlying [Arrow data type](https://docs.rs/arrow/latest/arrow/datatypes/enum.DataType.html) of the expression.",
28    syntax_example = "arrow_typeof(expression)",
29    sql_example = r#"```sql
30> select arrow_typeof('foo'), arrow_typeof(1);
31+---------------------------+------------------------+
32| arrow_typeof(Utf8("foo")) | arrow_typeof(Int64(1)) |
33+---------------------------+------------------------+
34| Utf8                      | Int64                  |
35+---------------------------+------------------------+
36```
37"#,
38    argument(
39        name = "expression",
40        description = "Expression to evaluate. The expression can be a constant, column, or function, and any combination of operators."
41    )
42)]
43#[derive(Debug, PartialEq, Eq, Hash)]
44pub struct ArrowTypeOfFunc {
45    signature: Signature,
46}
47
48impl Default for ArrowTypeOfFunc {
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54impl ArrowTypeOfFunc {
55    pub fn new() -> Self {
56        Self {
57            signature: Signature::any(1, Volatility::Immutable),
58        }
59    }
60}
61
62impl ScalarUDFImpl for ArrowTypeOfFunc {
63    fn as_any(&self) -> &dyn Any {
64        self
65    }
66    fn name(&self) -> &str {
67        "arrow_typeof"
68    }
69
70    fn signature(&self) -> &Signature {
71        &self.signature
72    }
73
74    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
75        Ok(DataType::Utf8)
76    }
77
78    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
79        let [arg] = take_function_args(self.name(), args.args)?;
80        let input_data_type = arg.data_type();
81        Ok(ColumnarValue::Scalar(ScalarValue::from(format!(
82            "{input_data_type}"
83        ))))
84    }
85
86    fn documentation(&self) -> Option<&Documentation> {
87        self.doc()
88    }
89}