LiaCastaneda commented on code in PR #21895: URL: https://github.com/apache/datafusion/pull/21895#discussion_r3161306990
########## datafusion/functions-nested/src/array_filter.rs: ########## @@ -0,0 +1,486 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! [`HigherOrderUDF`] definitions for array_filter function. + +use arrow::{ + array::{ + Array, ArrayRef, AsArray, BooleanArray, BooleanBufferBuilder, LargeListArray, + ListArray, OffsetSizeTrait, new_empty_array, + }, + buffer::{OffsetBuffer, ScalarBuffer}, + compute::filter as arrow_filter, + datatypes::{DataType, Field, FieldRef}, +}; +use datafusion_common::{ + Result, ScalarValue, exec_err, plan_err, utils::adjust_offsets_for_slice, +}; +use datafusion_expr::{ + ColumnarValue, Documentation, HigherOrderFunctionArgs, HigherOrderReturnFieldArgs, + HigherOrderSignature, HigherOrderUDF, LambdaParametersProgress, ValueOrLambda, + Volatility, +}; +use datafusion_macros::user_doc; +use std::sync::Arc; + +make_higher_order_function_expr_and_func!( + ArrayFilter, + array_filter, + array lambda, + "filters the values of an array using a boolean lambda", + array_filter_higher_order_function +); + +#[user_doc( + doc_section(label = "Array Functions"), + description = "filters the values of an array using a boolean lambda", + syntax_example = "array_filter(array, x -> x > 2)", + sql_example = r#"```sql +> select array_filter([1, 2, 3, 4, 5], x -> x > 2); ++--------------------------------------------+ +| array_filter([1, 2, 3, 4, 5], x -> x > 2) | ++--------------------------------------------+ +| [3, 4, 5] | ++--------------------------------------------+ +```"#, + argument( + name = "array", + description = "Array expression. Can be a constant, column, or function, and any combination of array operators." + ), + argument( + name = "lambda", + description = "Lambda that returns a boolean. Elements for which the lambda returns true are kept." + ) +)] +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct ArrayFilter { + signature: HigherOrderSignature, + aliases: Vec<String>, +} + +impl Default for ArrayFilter { + fn default() -> Self { + Self::new() + } +} + +impl ArrayFilter { + pub fn new() -> Self { + Self { + signature: HigherOrderSignature::user_defined(Volatility::Immutable), Review Comment: I plan to open a PR soon so we can be more specific about the Lambda signature we want (e.g. `exact` types) so all the validation can be hidden into the planner (and potentially be able to remove `value_lambda_pair`) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
