datafusion_functions/core/
arrowtypeof.rs1use 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}