Skip to content
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

feat: support LargeList in array_dims #8592

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 22 additions & 5 deletions datafusion/physical-expr/src/array_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1925,12 +1925,29 @@ pub fn array_length(args: &[ArrayRef]) -> Result<ArrayRef> {

/// Array_dims SQL function
pub fn array_dims(args: &[ArrayRef]) -> Result<ArrayRef> {
let list_array = as_list_array(&args[0])?;
let data = match args[0].data_type() {
Copy link
Contributor

Choose a reason for hiding this comment

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

please check args not empty, especially if its pub function

Copy link
Member Author

Choose a reason for hiding this comment

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

I will address this later in the pull request as other functions may also lack this check.

DataType::List(_) => {
let array = as_list_array(&args[0])?;
array
.iter()
.map(compute_array_dims)
.collect::<Result<Vec<_>>>()?
}
DataType::LargeList(_) => {
let array = as_large_list_array(&args[0])?;
array
.iter()
.map(compute_array_dims)
.collect::<Result<Vec<_>>>()?
}
_ => {
return exec_err!(
"array_dims does not support type '{:?}'",
args[0].data_type()
);
}
};

let data = list_array
.iter()
.map(compute_array_dims)
.collect::<Result<Vec<_>>>()?;
let result = ListArray::from_iter_primitive::<UInt64Type, _, _>(data);

Ok(Arc::new(result) as ArrayRef)
Expand Down
40 changes: 38 additions & 2 deletions datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ AS VALUES
(make_array(make_array(15, 16),make_array(NULL, 18)), make_array(16.6, 17.7, 18.8), NULL)
;

statement ok
CREATE TABLE large_arrays
AS
SELECT
arrow_cast(column1, 'LargeList(List(Int64))') AS column1,
arrow_cast(column2, 'LargeList(Float64)') AS column2,
arrow_cast(column3, 'LargeList(Utf8)') AS column3
FROM arrays
;

statement ok
CREATE TABLE slices
AS VALUES
Expand Down Expand Up @@ -2820,8 +2830,7 @@ NULL 10
## array_dims (aliases: `list_dims`)

# array dims error
# TODO this is a separate bug
query error Internal error: could not cast value to arrow_array::array::list_array::GenericListArray<i32>\.
query error Execution error: array_dims does not support type 'Int64'
select array_dims(1);

# array_dims scalar function
Expand All @@ -2830,6 +2839,11 @@ select array_dims(make_array(1, 2, 3)), array_dims(make_array([1, 2], [3, 4])),
----
[3] [2, 2] [1, 1, 1, 2, 1]

query ???
select array_dims(arrow_cast(make_array(1, 2, 3), 'LargeList(Int64)')), array_dims(arrow_cast(make_array([1, 2], [3, 4]), 'LargeList(List(Int64))')), array_dims(arrow_cast(make_array([[[[1], [2]]]]), 'LargeList(List(List(List(List(Int64)))))'));
----
[3] [2, 2] [1, 1, 1, 2, 1]
Copy link
Contributor

Choose a reason for hiding this comment

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

👍


# array_dims scalar function #2
query ??
select array_dims(array_repeat(array_repeat(array_repeat(2, 3), 2), 1)), array_dims(array_repeat(array_repeat(array_repeat(3, 4), 5), 2));
Expand All @@ -2842,12 +2856,22 @@ select array_dims(make_array()), array_dims(make_array(make_array()))
----
NULL [1, 0]

query ??
select array_dims(arrow_cast(make_array(), 'LargeList(Null)')), array_dims(arrow_cast(make_array(make_array()), 'LargeList(List(Null))'))
----
NULL [1, 0]

# list_dims scalar function #4 (function alias `array_dims`)
query ???
select list_dims(make_array(1, 2, 3)), list_dims(make_array([1, 2], [3, 4])), list_dims(make_array([[[[1], [2]]]]));
----
[3] [2, 2] [1, 1, 1, 2, 1]

query ???
select list_dims(arrow_cast(make_array(1, 2, 3), 'LargeList(Int64)')), list_dims(arrow_cast(make_array([1, 2], [3, 4]), 'LargeList(List(Int64))')), list_dims(arrow_cast(make_array([[[[1], [2]]]]), 'LargeList(List(List(List(List(Int64)))))'));
----
[3] [2, 2] [1, 1, 1, 2, 1]

# array_dims with columns
query ???
select array_dims(column1), array_dims(column2), array_dims(column3) from arrays;
Expand All @@ -2860,6 +2884,18 @@ NULL [3] [4]
[2, 2] NULL [1]
[2, 2] [3] NULL

query ???
select array_dims(column1), array_dims(column2), array_dims(column3) from large_arrays;
----
[2, 2] [3] [5]
[2, 2] [3] [5]
[2, 2] [3] [5]
[2, 2] [3] [3]
NULL [3] [4]
[2, 2] NULL [1]
[2, 2] [3] NULL


## array_ndims (aliases: `list_ndims`)

# array_ndims scalar function #1
Expand Down