-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: rewrite concat(array, ...) to array_concat #21689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // 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. | ||
|
|
||
| //! [`ConcatArrayRewrite`] rewrites `concat(array, ...)` to `array_concat(array, ...)`. | ||
|
|
||
| use std::any::Any; | ||
|
|
||
| use arrow::datatypes::DataType; | ||
| use datafusion_common::config::ConfigOptions; | ||
| use datafusion_common::tree_node::Transformed; | ||
| use datafusion_common::{DFSchema, Result, plan_err}; | ||
| use datafusion_expr::Expr; | ||
| use datafusion_expr::ExprSchemable; | ||
| use datafusion_expr::expr::ScalarFunction; | ||
| use datafusion_expr::expr_rewriter::FunctionRewrite; | ||
| use datafusion_functions::string::concat::ConcatFunc; | ||
|
|
||
| use crate::concat::array_concat; | ||
|
|
||
| /// [`FunctionRewrite`] that turns `concat(array, ...)` into | ||
| /// `array_concat(array, ...)` at the analyzer phase. | ||
| /// | ||
| /// `concat` calls with only non-array arguments are left unchanged. | ||
| /// Mixed array and non-array arguments are rejected with a plan error. | ||
| #[derive(Debug, Default)] | ||
| pub struct ConcatArrayRewrite; | ||
|
|
||
| impl FunctionRewrite for ConcatArrayRewrite { | ||
| fn name(&self) -> &str { | ||
| "concat_array_rewrite" | ||
| } | ||
|
|
||
| fn rewrite( | ||
| &self, | ||
| expr: Expr, | ||
| schema: &DFSchema, | ||
| _config: &ConfigOptions, | ||
| ) -> Result<Transformed<Expr>> { | ||
| let Expr::ScalarFunction(ScalarFunction { func, args }) = &expr else { | ||
| return Ok(Transformed::no(expr)); | ||
| }; | ||
| if !(func.inner().as_ref() as &dyn Any).is::<ConcatFunc>() { | ||
| return Ok(Transformed::no(expr)); | ||
| } | ||
|
|
||
| let mut any_list = false; | ||
| let mut any_non_list = false; | ||
| for arg in args { | ||
| match arg.get_type(schema)? { | ||
| DataType::List(_) | ||
| | DataType::LargeList(_) | ||
| | DataType::FixedSizeList(_, _) => any_list = true, | ||
| DataType::Null => {} | ||
| _ => any_non_list = true, | ||
| } | ||
| } | ||
|
|
||
| if !any_list { | ||
| return Ok(Transformed::no(expr)); | ||
| } | ||
| if any_non_list { | ||
| return plan_err!( | ||
| "Cannot mix array and non-array arguments in concat function" | ||
| ); | ||
| } | ||
|
|
||
| let Expr::ScalarFunction(ScalarFunction { args, .. }) = expr else { | ||
| unreachable!("already matched above") | ||
| }; | ||
| Ok(Transformed::yes(array_concat(args))) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -388,4 +388,86 @@ select array_concat(make_array(column3), column1, column2) from arrays_values_v2 | |
| [NULL] | ||
|
|
||
|
|
||
| ## concat() delegates to array_concat when all arguments are arrays. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth adding tests for mixed list types? e.g.,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! I added FSL + List, but skipped List + LargeList, since |
||
| ## An analyzer-phase FunctionRewrite rewrites the call before execution, so | ||
| ## the string concat path never sees array inputs. | ||
|
|
||
| query ? | ||
| select concat(make_array(1, 2, 3), make_array(4, 5)); | ||
| ---- | ||
| [1, 2, 3, 4, 5] | ||
|
|
||
| query ? | ||
| select concat(make_array(1, 2), make_array(3, 4), make_array(5, 6)); | ||
| ---- | ||
| [1, 2, 3, 4, 5, 6] | ||
|
|
||
| query ? | ||
| select concat(make_array(1, NULL, 3), make_array(4)); | ||
| ---- | ||
| [1, NULL, 3, 4] | ||
|
|
||
| query ? | ||
| select concat(make_array('a', 'b'), make_array('c', 'd')); | ||
| ---- | ||
| [a, b, c, d] | ||
|
|
||
| query ? | ||
| select concat(NULL::integer[], make_array(1, 2)); | ||
| ---- | ||
| [1, 2] | ||
|
|
||
| query ? | ||
| select concat(make_array(1, 2), NULL::integer[]); | ||
| ---- | ||
| [1, 2] | ||
|
|
||
| query ? | ||
| select concat(column1, column2) from arrays_values_v2; | ||
| ---- | ||
| [NULL, 2, 3, 4, 5, NULL] | ||
| [7, NULL, 8] | ||
| [9, NULL, 10] | ||
| [NULL, 1, NULL, 21] | ||
| [11, 12] | ||
| NULL | ||
|
|
||
| query ? | ||
| select concat( | ||
| arrow_cast(['1', '2'], 'LargeList(Utf8)'), | ||
| arrow_cast(['3'], 'LargeList(Utf8)') | ||
| ); | ||
| ---- | ||
| [1, 2, 3] | ||
|
|
||
| query ? | ||
| select concat( | ||
| arrow_cast(['1', '2'], 'FixedSizeList(2, Utf8)'), | ||
| arrow_cast(['3'], 'FixedSizeList(1, Utf8)') | ||
| ); | ||
| ---- | ||
| [1, 2, 3] | ||
|
|
||
| query ? | ||
| select concat(NULL::integer[], NULL::integer[]); | ||
| ---- | ||
| NULL | ||
|
|
||
| # Mixed list variants are coerced by array_concat's own coerce_types | ||
| # rules (same result as calling array_concat directly). | ||
| query ?T | ||
| select | ||
| concat(arrow_cast([1, 2], 'FixedSizeList(2, Int64)'), make_array(3, 4)) as v, | ||
| arrow_typeof(concat(arrow_cast([1, 2], 'FixedSizeList(2, Int64)'), make_array(3, 4))) as t; | ||
| ---- | ||
| [1, 2, 3, 4] List(Int64) | ||
|
|
||
| # Mixed array + non-array arguments are rejected at plan time. | ||
| statement error Cannot mix array and non-array arguments in concat function | ||
| select concat(make_array(1), 'x'); | ||
|
|
||
| statement error Cannot mix array and non-array arguments in concat function | ||
| select concat('x', make_array(1)); | ||
|
|
||
|
|
||
| include ./cleanup.slt.part | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel we can achieve this using simplify in UDF instead