datafusion_functions/core/
mod.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//! "core" DataFusion functions
19
20use datafusion_expr::ScalarUDF;
21use std::sync::Arc;
22
23pub mod arrow_cast;
24pub mod arrowtypeof;
25pub mod coalesce;
26pub mod expr_ext;
27pub mod getfield;
28pub mod greatest;
29mod greatest_least_utils;
30pub mod least;
31pub mod named_struct;
32pub mod nullif;
33pub mod nvl;
34pub mod nvl2;
35pub mod planner;
36pub mod r#struct;
37pub mod union_extract;
38pub mod version;
39
40// create UDFs
41make_udf_function!(arrow_cast::ArrowCastFunc, arrow_cast);
42make_udf_function!(nullif::NullIfFunc, nullif);
43make_udf_function!(nvl::NVLFunc, nvl);
44make_udf_function!(nvl2::NVL2Func, nvl2);
45make_udf_function!(arrowtypeof::ArrowTypeOfFunc, arrow_typeof);
46make_udf_function!(r#struct::StructFunc, r#struct);
47make_udf_function!(named_struct::NamedStructFunc, named_struct);
48make_udf_function!(getfield::GetFieldFunc, get_field);
49make_udf_function!(coalesce::CoalesceFunc, coalesce);
50make_udf_function!(greatest::GreatestFunc, greatest);
51make_udf_function!(least::LeastFunc, least);
52make_udf_function!(union_extract::UnionExtractFun, union_extract);
53make_udf_function!(version::VersionFunc, version);
54
55pub mod expr_fn {
56    use datafusion_expr::{Expr, Literal};
57
58    export_functions!((
59        nullif,
60        "Returns NULL if value1 equals value2; otherwise it returns value1. This can be used to perform the inverse operation of the COALESCE expression",
61        arg1 arg2
62    ),(
63        arrow_cast,
64        "Returns value2 if value1 is NULL; otherwise it returns value1",
65        arg1 arg2
66    ),(
67        nvl,
68        "Returns value2 if value1 is NULL; otherwise it returns value1",
69        arg1 arg2
70    ),(
71        nvl2,
72        "Returns value2 if value1 is not NULL; otherwise, it returns value3.",
73        arg1 arg2 arg3
74    ),(
75        arrow_typeof,
76        "Returns the Arrow type of the input expression.",
77        arg1
78    ),(
79        r#struct,
80        "Returns a struct with the given arguments",
81        args,
82    ),(
83        named_struct,
84        "Returns a struct with the given names and arguments pairs",
85        args,
86    ),(
87        coalesce,
88        "Returns `coalesce(args...)`, which evaluates to the value of the first expr which is not NULL",
89        args,
90    ),(
91        greatest,
92        "Returns `greatest(args...)`, which evaluates to the greatest value in the list of expressions or NULL if all the expressions are NULL",
93        args,
94    ),(
95        least,
96        "Returns `least(args...)`, which evaluates to the smallest value in the list of expressions or NULL if all the expressions are NULL",
97        args,
98    ));
99
100    #[doc = "Returns the value of the field with the given name from the struct"]
101    pub fn get_field(arg1: Expr, arg2: impl Literal) -> Expr {
102        super::get_field().call(vec![arg1, arg2.lit()])
103    }
104
105    #[doc = "Returns the value of the field with the given name from the union when it's selected, or NULL otherwise"]
106    pub fn union_extract(arg1: Expr, arg2: impl Literal) -> Expr {
107        super::union_extract().call(vec![arg1, arg2.lit()])
108    }
109}
110
111/// Returns all DataFusion functions defined in this package
112pub fn functions() -> Vec<Arc<ScalarUDF>> {
113    vec![
114        nullif(),
115        arrow_cast(),
116        nvl(),
117        nvl2(),
118        arrow_typeof(),
119        named_struct(),
120        // Note: most users invoke `get_field` indirectly via field access
121        // syntax like `my_struct_col['field_name']`, which results in a call to
122        // `get_field(my_struct_col, "field_name")`.
123        //
124        // However, it is also exposed directly for use cases such as
125        // serializing / deserializing plans with the field access desugared to
126        // calls to [`get_field`]
127        get_field(),
128        coalesce(),
129        greatest(),
130        least(),
131        union_extract(),
132        version(),
133        r#struct(),
134    ]
135}