datafusion_functions_nested/
lib.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#![doc(
19    html_logo_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg",
20    html_favicon_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg"
21)]
22#![cfg_attr(docsrs, feature(doc_auto_cfg))]
23// Make cheap clones clear: https://github.com/apache/datafusion/issues/11143
24#![deny(clippy::clone_on_ref_ptr)]
25
26//! Nested type Functions for [DataFusion].
27//!
28//! This crate contains a collection of nested type functions implemented using the
29//! extension API.
30//!
31//! [DataFusion]: https://crates.io/crates/datafusion
32//!
33//! You can register the functions in this crate using the [`register_all`] function.
34//!
35
36#[macro_use]
37pub mod macros;
38
39pub mod array_has;
40pub mod cardinality;
41pub mod concat;
42pub mod dimension;
43pub mod distance;
44pub mod empty;
45pub mod except;
46pub mod expr_ext;
47pub mod extract;
48pub mod flatten;
49pub mod length;
50pub mod make_array;
51pub mod map;
52pub mod map_extract;
53pub mod map_keys;
54pub mod map_values;
55pub mod planner;
56pub mod position;
57pub mod range;
58pub mod remove;
59pub mod repeat;
60pub mod replace;
61pub mod resize;
62pub mod reverse;
63pub mod set_ops;
64pub mod sort;
65pub mod string;
66pub mod utils;
67
68use datafusion_common::Result;
69use datafusion_execution::FunctionRegistry;
70use datafusion_expr::ScalarUDF;
71use log::debug;
72use std::sync::Arc;
73
74/// Fluent-style API for creating `Expr`s
75pub mod expr_fn {
76    pub use super::array_has::array_has;
77    pub use super::array_has::array_has_all;
78    pub use super::array_has::array_has_any;
79    pub use super::cardinality::cardinality;
80    pub use super::concat::array_append;
81    pub use super::concat::array_concat;
82    pub use super::concat::array_prepend;
83    pub use super::dimension::array_dims;
84    pub use super::dimension::array_ndims;
85    pub use super::distance::array_distance;
86    pub use super::empty::array_empty;
87    pub use super::except::array_except;
88    pub use super::extract::array_any_value;
89    pub use super::extract::array_element;
90    pub use super::extract::array_pop_back;
91    pub use super::extract::array_pop_front;
92    pub use super::extract::array_slice;
93    pub use super::flatten::flatten;
94    pub use super::length::array_length;
95    pub use super::make_array::make_array;
96    pub use super::map_extract::map_extract;
97    pub use super::map_keys::map_keys;
98    pub use super::map_values::map_values;
99    pub use super::position::array_position;
100    pub use super::position::array_positions;
101    pub use super::range::gen_series;
102    pub use super::range::range;
103    pub use super::remove::array_remove;
104    pub use super::remove::array_remove_all;
105    pub use super::remove::array_remove_n;
106    pub use super::repeat::array_repeat;
107    pub use super::replace::array_replace;
108    pub use super::replace::array_replace_all;
109    pub use super::replace::array_replace_n;
110    pub use super::resize::array_resize;
111    pub use super::reverse::array_reverse;
112    pub use super::set_ops::array_distinct;
113    pub use super::set_ops::array_intersect;
114    pub use super::set_ops::array_union;
115    pub use super::sort::array_sort;
116    pub use super::string::array_to_string;
117    pub use super::string::string_to_array;
118}
119
120/// Return all default nested type functions
121pub fn all_default_nested_functions() -> Vec<Arc<ScalarUDF>> {
122    vec![
123        string::array_to_string_udf(),
124        string::string_to_array_udf(),
125        range::range_udf(),
126        range::gen_series_udf(),
127        dimension::array_dims_udf(),
128        cardinality::cardinality_udf(),
129        dimension::array_ndims_udf(),
130        concat::array_append_udf(),
131        concat::array_prepend_udf(),
132        concat::array_concat_udf(),
133        except::array_except_udf(),
134        extract::array_element_udf(),
135        extract::array_pop_back_udf(),
136        extract::array_pop_front_udf(),
137        extract::array_slice_udf(),
138        extract::array_any_value_udf(),
139        make_array::make_array_udf(),
140        array_has::array_has_udf(),
141        array_has::array_has_all_udf(),
142        array_has::array_has_any_udf(),
143        empty::array_empty_udf(),
144        length::array_length_udf(),
145        distance::array_distance_udf(),
146        flatten::flatten_udf(),
147        sort::array_sort_udf(),
148        repeat::array_repeat_udf(),
149        resize::array_resize_udf(),
150        reverse::array_reverse_udf(),
151        set_ops::array_distinct_udf(),
152        set_ops::array_intersect_udf(),
153        set_ops::array_union_udf(),
154        position::array_position_udf(),
155        position::array_positions_udf(),
156        remove::array_remove_udf(),
157        remove::array_remove_all_udf(),
158        remove::array_remove_n_udf(),
159        replace::array_replace_n_udf(),
160        replace::array_replace_all_udf(),
161        replace::array_replace_udf(),
162        map::map_udf(),
163        map_extract::map_extract_udf(),
164        map_keys::map_keys_udf(),
165        map_values::map_values_udf(),
166    ]
167}
168
169/// Registers all enabled packages with a [`FunctionRegistry`]
170pub fn register_all(registry: &mut dyn FunctionRegistry) -> Result<()> {
171    let functions: Vec<Arc<ScalarUDF>> = all_default_nested_functions();
172    functions.into_iter().try_for_each(|udf| {
173        let existing_udf = registry.register_udf(udf)?;
174        if let Some(existing_udf) = existing_udf {
175            debug!("Overwrite existing UDF: {}", existing_udf.name());
176        }
177        Ok(()) as Result<()>
178    })?;
179
180    Ok(())
181}
182
183#[cfg(test)]
184mod tests {
185    use crate::all_default_nested_functions;
186    use datafusion_common::Result;
187    use std::collections::HashSet;
188
189    #[test]
190    fn test_no_duplicate_name() -> Result<()> {
191        let mut names = HashSet::new();
192        for func in all_default_nested_functions() {
193            assert!(
194                names.insert(func.name().to_string().to_lowercase()),
195                "duplicate function name: {}",
196                func.name()
197            );
198            for alias in func.aliases() {
199                assert!(
200                    names.insert(alias.to_string().to_lowercase()),
201                    "duplicate function name: {}",
202                    alias
203                );
204            }
205        }
206        Ok(())
207    }
208}