Skip to content

Commit

Permalink
support LargeList in array_repeat (#8725)
Browse files Browse the repository at this point in the history
  • Loading branch information
Weijun-H authored Jan 3, 2024
1 parent 881d03f commit ca260d9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
16 changes: 10 additions & 6 deletions datafusion/physical-expr/src/array_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,11 @@ pub fn array_repeat(args: &[ArrayRef]) -> Result<ArrayRef> {
match element.data_type() {
DataType::List(_) => {
let list_array = as_list_array(element)?;
general_list_repeat(list_array, count_array)
general_list_repeat::<i32>(list_array, count_array)
}
DataType::LargeList(_) => {
let list_array = as_large_list_array(element)?;
general_list_repeat::<i64>(list_array, count_array)
}
_ => general_repeat(element, count_array),
}
Expand Down Expand Up @@ -1302,8 +1306,8 @@ fn general_repeat(array: &ArrayRef, count_array: &Int64Array) -> Result<ArrayRef
/// [[1, 2, 3], [4, 5], [6]], [2, 0, 1] => [[[1, 2, 3], [1, 2, 3]], [], [[6]]]
/// )
/// ```
fn general_list_repeat(
list_array: &ListArray,
fn general_list_repeat<O: OffsetSizeTrait>(
list_array: &GenericListArray<O>,
count_array: &Int64Array,
) -> Result<ArrayRef> {
let data_type = list_array.data_type();
Expand Down Expand Up @@ -1335,9 +1339,9 @@ fn general_list_repeat(
let data = mutable.freeze();
let repeated_array = arrow_array::make_array(data);

let list_arr = ListArray::try_new(
let list_arr = GenericListArray::<O>::try_new(
Arc::new(Field::new("item", value_type.clone(), true)),
OffsetBuffer::from_lengths(vec![original_data.len(); count]),
OffsetBuffer::<O>::from_lengths(vec![original_data.len(); count]),
repeated_array,
None,
)?;
Expand All @@ -1354,7 +1358,7 @@ fn general_list_repeat(

Ok(Arc::new(ListArray::try_new(
Arc::new(Field::new("item", data_type.to_owned(), true)),
OffsetBuffer::from_lengths(lengths),
OffsetBuffer::<i32>::from_lengths(lengths),
values,
None,
)?))
Expand Down
37 changes: 37 additions & 0 deletions datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,15 @@ select
----
[[1], [1], [1], [1], [1]] [[1.1, 2.2, 3.3], [1.1, 2.2, 3.3], [1.1, 2.2, 3.3]] [[, ], [, ], [, ]] [[[1, 2], [3, 4]], [[1, 2], [3, 4]]]

query ????
select
array_repeat(arrow_cast([1], 'LargeList(Int64)'), 5),
array_repeat(arrow_cast([1.1, 2.2, 3.3], 'LargeList(Float64)'), 3),
array_repeat(arrow_cast([null, null], 'LargeList(Null)'), 3),
array_repeat(arrow_cast([[1, 2], [3, 4]], 'LargeList(List(Int64))'), 2);
----
[[1], [1], [1], [1], [1]] [[1.1, 2.2, 3.3], [1.1, 2.2, 3.3], [1.1, 2.2, 3.3]] [[, ], [, ], [, ]] [[[1, 2], [3, 4]], [[1, 2], [3, 4]]]

# array_repeat with columns #1

statement ok
Expand All @@ -1967,6 +1976,16 @@ AS VALUES
(3, 2, 2.2, 'rust', make_array(7)),
(0, 3, 3.3, 'datafusion', make_array(8, 9));

statement ok
CREATE TABLE large_array_repeat_table
AS SELECT
column1,
column2,
column3,
column4,
arrow_cast(column5, 'LargeList(Int64)') as column5
FROM array_repeat_table;

query ??????
select
array_repeat(column2, column1),
Expand All @@ -1982,9 +2001,27 @@ from array_repeat_table;
[2, 2, 2] [2.2, 2.2, 2.2] [rust, rust, rust] [[7], [7], [7]] [2, 2, 2] [[1], [1], [1]]
[] [] [] [] [3, 3, 3] []

query ??????
select
array_repeat(column2, column1),
array_repeat(column3, column1),
array_repeat(column4, column1),
array_repeat(column5, column1),
array_repeat(column2, 3),
array_repeat(make_array(1), column1)
from large_array_repeat_table;
----
[1] [1.1] [a] [[4, 5, 6]] [1, 1, 1] [[1]]
[, ] [, ] [, ] [, ] [, , ] [[1], [1]]
[2, 2, 2] [2.2, 2.2, 2.2] [rust, rust, rust] [[7], [7], [7]] [2, 2, 2] [[1], [1], [1]]
[] [] [] [] [3, 3, 3] []

statement ok
drop table array_repeat_table;

statement ok
drop table large_array_repeat_table;

## array_concat (aliases: `array_cat`, `list_concat`, `list_cat`)

# array_concat error
Expand Down

0 comments on commit ca260d9

Please sign in to comment.