Skip to content

feat: support fixed size list for array reverse #16423

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

Merged
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
50 changes: 47 additions & 3 deletions datafusion/functions-nested/src/reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@

use crate::utils::make_scalar_function;
use arrow::array::{
Array, ArrayRef, Capacities, GenericListArray, MutableArrayData, OffsetSizeTrait,
Array, ArrayRef, Capacities, FixedSizeListArray, GenericListArray, MutableArrayData,
OffsetSizeTrait,
};
use arrow::buffer::OffsetBuffer;
use arrow::datatypes::DataType::{LargeList, List, Null};
use arrow::datatypes::DataType::{FixedSizeList, LargeList, List, Null};
use arrow::datatypes::{DataType, FieldRef};
use datafusion_common::cast::{as_large_list_array, as_list_array};
use datafusion_common::cast::{
as_fixed_size_list_array, as_large_list_array, as_list_array,
};
use datafusion_common::{exec_err, utils::take_function_args, Result};
use datafusion_expr::{
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
Expand Down Expand Up @@ -125,6 +128,10 @@ pub fn array_reverse_inner(arg: &[ArrayRef]) -> Result<ArrayRef> {
let array = as_large_list_array(input_array)?;
general_array_reverse::<i64>(array, field)
}
FixedSizeList(field, _) => {
let array = as_fixed_size_list_array(input_array)?;
fixed_size_array_reverse(array, field)
}
Null => Ok(Arc::clone(input_array)),
array_type => exec_err!("array_reverse does not support type '{array_type:?}'."),
}
Expand Down Expand Up @@ -175,3 +182,40 @@ fn general_array_reverse<O: OffsetSizeTrait + TryFrom<i64>>(
Some(nulls.into()),
)?))
}

fn fixed_size_array_reverse(
array: &FixedSizeListArray,
field: &FieldRef,
) -> Result<ArrayRef> {
let values = array.values();
let original_data = values.to_data();
let capacity = Capacities::Array(original_data.len());
let mut nulls = vec![];
let mut mutable =
MutableArrayData::with_capacities(vec![&original_data], false, capacity);
let value_length = array.value_length() as usize;

for row_index in 0..array.len() {
// skip the null value
if array.is_null(row_index) {
nulls.push(false);
mutable.extend(0, 0, 1);
continue;
} else {
nulls.push(true);
}
let start = row_index * value_length;
let end = start + value_length;
for idx in (start..end).rev() {
mutable.extend(0, idx, idx + 1);
}
}

let data = mutable.freeze();
Ok(Arc::new(FixedSizeListArray::try_new(
Arc::clone(field),
array.value_length(),
arrow::array::make_array(data),
Some(nulls.into()),
)?))
}
12 changes: 7 additions & 5 deletions datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -7853,11 +7853,13 @@ select array_reverse(arrow_cast(make_array(1, 2, 3), 'LargeList(Int64)')), array
----
[3, 2, 1] [1]

#TODO: support after FixedSizeList type coercion
#query ??
#select array_reverse(arrow_cast(make_array(1, 2, 3), 'FixedSizeList(3, Int64)')), array_reverse(arrow_cast(make_array(1), 'FixedSizeList(1, Int64)'));
#----
#[3, 2, 1] [1]
query ????
select array_reverse(arrow_cast(make_array(1, 2, 3), 'FixedSizeList(3, Int64)')),
array_reverse(arrow_cast(make_array(1), 'FixedSizeList(1, Int64)')),
array_reverse(arrow_cast(make_array(1, NULL, 3), 'FixedSizeList(3, Int64)')),
array_reverse(arrow_cast(make_array(NULL, NULL, NULL), 'FixedSizeList(3, Int64)'));
----
[3, 2, 1] [1] [3, NULL, 1] [NULL, NULL, NULL]

Copy link
Contributor

Choose a reason for hiding this comment

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

Lets add a test for null values as well

query ??
select array_reverse(NULL), array_reverse([]);
Expand Down