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