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