Skip to content

Commit 4c7bb08

Browse files
authored
Use ListArray nullability instead of offsets for array_element, array_any_value. (#21672)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #. #21671 ## Rationale for this change Align the implementation of `array_element`, `array_any_value` with Arrow's spec concerning Null rows. Specifically allow the below from [Arrow's spec](https://arrow.apache.org/docs/format/Columnar.html#variable-size-binary-layout) > It should be noted that a null value may have a positive slot length. That is, a null value may occupy a non-empty memory space in the data buffer. When this is true, the content of the corresponding memory space is undefined. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## Are these changes tested? Unit tests included. <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? -->
1 parent 8b47f45 commit 4c7bb08

1 file changed

Lines changed: 56 additions & 7 deletions

File tree

datafusion/functions-nested/src/extract.rs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ where
257257
let len = end - start;
258258

259259
// array is null
260-
if len == O::usize_as(0) {
260+
if array.is_null(row_index) {
261261
mutable.extend_nulls(1);
262262
continue;
263263
}
@@ -1064,11 +1064,9 @@ where
10641064

10651065
for (row_index, offset_window) in array.offsets().windows(2).enumerate() {
10661066
let start = offset_window[0];
1067-
let end = offset_window[1];
1068-
let len = end - start;
10691067

10701068
// array is null
1071-
if len == O::usize_as(0) {
1069+
if array.is_null(row_index) {
10721070
mutable.extend_nulls(1);
10731071
continue;
10741072
}
@@ -1101,14 +1099,18 @@ where
11011099

11021100
#[cfg(test)]
11031101
mod tests {
1104-
use super::{array_element_udf, general_list_view_array_slice};
1102+
use super::{
1103+
array_element_udf, general_array_any_value, general_array_element,
1104+
general_list_view_array_slice,
1105+
};
11051106
use arrow::array::{
11061107
Array, ArrayRef, GenericListViewArray, Int32Array, Int64Array, ListViewArray,
11071108
cast::AsArray,
11081109
};
1109-
use arrow::buffer::ScalarBuffer;
1110+
use arrow::array::{ListArray, RecordBatch};
1111+
use arrow::buffer::{NullBuffer, OffsetBuffer, ScalarBuffer};
11101112
use arrow::datatypes::{DataType, Field};
1111-
use datafusion_common::{Column, DFSchema, Result};
1113+
use datafusion_common::{Column, DFSchema, Result, assert_batches_eq};
11121114
use datafusion_expr::expr::ScalarFunction;
11131115
use datafusion_expr::{Expr, ExprSchemable};
11141116
use std::collections::HashMap;
@@ -1169,6 +1171,53 @@ mod tests {
11691171
);
11701172
}
11711173

1174+
#[test]
1175+
fn test_array_element_null_handling() -> Result<()> {
1176+
let values = Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5]));
1177+
let offsets = OffsetBuffer::new(ScalarBuffer::from(vec![0, 3, 4, 5]));
1178+
let nulls = NullBuffer::from(vec![true, false, true]);
1179+
let field = Arc::new(Field::new("item", DataType::Int32, true));
1180+
1181+
let list_array = ListArray::new(field, offsets, values, Some(nulls));
1182+
let indexes = Int64Array::from(vec![1, 1, 1]);
1183+
1184+
let result = general_array_element(&list_array, &indexes)?;
1185+
1186+
let expected = [
1187+
"+--------+",
1188+
"| result |",
1189+
"+--------+",
1190+
"| 1 |",
1191+
"| |",
1192+
"| 5 |",
1193+
"+--------+",
1194+
];
1195+
1196+
let batch = RecordBatch::try_from_iter([("result", result)])?;
1197+
1198+
assert_batches_eq!(expected, &[batch]);
1199+
1200+
Ok(())
1201+
}
1202+
1203+
#[test]
1204+
fn test_array_any_null_handling() -> Result<()> {
1205+
let values: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5]));
1206+
let offsets = OffsetBuffer::new(ScalarBuffer::from(vec![0, 3, 4, 5]));
1207+
let nulls = NullBuffer::from(vec![true, false, true]);
1208+
let field = Arc::new(Field::new("item", DataType::Int32, true));
1209+
1210+
let list_array = ListArray::new(field, offsets, values, Some(nulls));
1211+
1212+
let result = general_array_any_value(&list_array)?;
1213+
1214+
assert!(!result.is_null(0));
1215+
assert!(result.is_null(1));
1216+
assert!(!result.is_null(2));
1217+
1218+
Ok(())
1219+
}
1220+
11721221
#[test]
11731222
fn test_array_slice_list_view_basic() -> Result<()> {
11741223
let values: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5]));

0 commit comments

Comments
 (0)