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