Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions datafusion/functions-nested/src/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,23 @@ impl ScalarUDFImpl for ArrayConcat {
}

fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
let base_type = base_type(&self.return_type(arg_types)?);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized something funny the existing implementation does; it calls return_type within coerce_types even though the docstring for return_type states that the input types are already coerced (i.e. coerce_types would have already been called) 😅

let return_type = self.return_type(arg_types)?;
let base_type = base_type(&return_type);
let coercion = Some(&ListCoercion::FixedSizedListToList);
// When the return type is a `LargeList`, the outer container of every
// input must be widened to `LargeList` as well. Otherwise
// `array_concat_inner` would later try to downcast a `List` argument
// to `GenericListArray<i64>` and fail.
let promote_to_large_list = matches!(return_type, DataType::LargeList(_));
let arg_types = arg_types.iter().map(|arg_type| {
coerced_type_with_base_type_only(arg_type, &base_type, coercion)
let coerced =
coerced_type_with_base_type_only(arg_type, &base_type, coercion);
match coerced {
DataType::List(field) if promote_to_large_list => {
DataType::LargeList(field)
}
other => other,
}
});

Ok(arg_types.collect())
Expand Down
32 changes: 32 additions & 0 deletions datafusion/sqllogictest/test_files/array/array_concat.slt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,38 @@ select
----
[1, 2, 3] List(Utf8View)

# Concatenating mixed list and large list — return type widens to LargeList
query ?T
select
array_concat(make_array(1, 2), arrow_cast([3, 4], 'LargeList(Int64)')),
arrow_typeof(array_concat(make_array(1, 2), arrow_cast([3, 4], 'LargeList(Int64)')));
----
[1, 2, 3, 4] LargeList(Int64)

# Reverse argument order: LargeList first, plain list second
query ?T
select
array_concat(arrow_cast([1, 2], 'LargeList(Int64)'), make_array(3, 4)),
arrow_typeof(array_concat(arrow_cast([1, 2], 'LargeList(Int64)'), make_array(3, 4)));
----
[1, 2, 3, 4] LargeList(Int64)

# FixedSizeList combined with LargeList — also widens to LargeList
query ?T
select
array_concat(arrow_cast([1, 2], 'FixedSizeList(2, Int64)'), arrow_cast([3, 4], 'LargeList(Int64)')),
arrow_typeof(array_concat(arrow_cast([1, 2], 'FixedSizeList(2, Int64)'), arrow_cast([3, 4], 'LargeList(Int64)')));
----
[1, 2, 3, 4] LargeList(Int64)

# Three-way mix: List, LargeList, List
query ?T
select
array_concat(make_array(1, 2), arrow_cast([3], 'LargeList(Int64)'), make_array(4, 5)),
arrow_typeof(array_concat(make_array(1, 2), arrow_cast([3], 'LargeList(Int64)'), make_array(4, 5)));
----
[1, 2, 3, 4, 5] LargeList(Int64)

# array_concat with NULL elements inside arrays
query ?
select array_concat([1, NULL, 3], [NULL, 5]);
Expand Down
Loading