Skip to content

Commit 40af786

Browse files
authored
fix take on empty struct array (#7224)
1 parent 82c2d5f commit 40af786

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

arrow-select/src/take.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,12 @@ fn take_impl<IndexType: ArrowPrimitiveType>(
254254
})
255255
.collect();
256256

257-
Ok(Arc::new(StructArray::from((fields, is_valid))) as ArrayRef)
257+
if fields.is_empty() {
258+
let nulls = NullBuffer::new(BooleanBuffer::new(is_valid, 0, indices.len()));
259+
Ok(Arc::new(StructArray::new_empty_fields(indices.len(), Some(nulls))))
260+
} else {
261+
Ok(Arc::new(StructArray::from((fields, is_valid))) as ArrayRef)
262+
}
258263
}
259264
DataType::Dictionary(_, _) => downcast_dictionary_array! {
260265
values => Ok(Arc::new(take_dict(values, indices)?)),
@@ -1968,6 +1973,15 @@ mod tests {
19681973
]);
19691974

19701975
assert_eq!(&expected, actual);
1976+
1977+
let nulls = NullBuffer::from(&[false, true, false, true, false, true]);
1978+
let empty_struct_arr = StructArray::new_empty_fields(6, Some(nulls));
1979+
let index = UInt32Array::from(vec![0, 2, 1, 4]);
1980+
let actual = take(&empty_struct_arr, &index, None).unwrap();
1981+
1982+
let expected_nulls = NullBuffer::from(&[false, false, true, false]);
1983+
let expected_struct_arr = StructArray::new_empty_fields(4, Some(expected_nulls));
1984+
assert_eq!(&expected_struct_arr, actual.as_struct());
19711985
}
19721986

19731987
#[test]

0 commit comments

Comments
 (0)