Skip to content

Commit 24d953e

Browse files
authored
fix: array_resize null fix (#13209)
* array_resize null fix * comment * clippy * fixes
1 parent 752561a commit 24d953e

File tree

2 files changed

+80
-5
lines changed

2 files changed

+80
-5
lines changed

datafusion/functions-nested/src/resize.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
2020
use crate::utils::make_scalar_function;
2121
use arrow::array::{Capacities, MutableArrayData};
22-
use arrow_array::{ArrayRef, GenericListArray, Int64Array, OffsetSizeTrait};
23-
use arrow_buffer::{ArrowNativeType, OffsetBuffer};
22+
use arrow_array::{
23+
new_null_array, Array, ArrayRef, GenericListArray, Int64Array, OffsetSizeTrait,
24+
};
25+
use arrow_buffer::{ArrowNativeType, BooleanBufferBuilder, NullBuffer, OffsetBuffer};
2426
use arrow_schema::DataType::{FixedSizeList, LargeList, List};
2527
use arrow_schema::{DataType, FieldRef};
2628
use datafusion_common::cast::{as_int64_array, as_large_list_array, as_list_array};
@@ -134,6 +136,23 @@ pub(crate) fn array_resize_inner(arg: &[ArrayRef]) -> Result<ArrayRef> {
134136
return exec_err!("array_resize needs two or three arguments");
135137
}
136138

139+
let array = &arg[0];
140+
141+
// Checks if entire array is null
142+
if array.null_count() == array.len() {
143+
let return_type = match array.data_type() {
144+
List(field) => List(Arc::clone(field)),
145+
LargeList(field) => LargeList(Arc::clone(field)),
146+
_ => {
147+
return exec_err!(
148+
"array_resize does not support type '{:?}'.",
149+
array.data_type()
150+
)
151+
}
152+
};
153+
return Ok(new_null_array(&return_type, array.len()));
154+
}
155+
137156
let new_len = as_int64_array(&arg[1])?;
138157
let new_element = if arg.len() == 3 {
139158
Some(Arc::clone(&arg[2]))
@@ -184,7 +203,16 @@ fn general_list_resize<O: OffsetSizeTrait + TryInto<i64>>(
184203
capacity,
185204
);
186205

206+
let mut null_builder = BooleanBufferBuilder::new(array.len());
207+
187208
for (row_index, offset_window) in array.offsets().windows(2).enumerate() {
209+
if array.is_null(row_index) {
210+
null_builder.append(false);
211+
offsets.push(offsets[row_index]);
212+
continue;
213+
}
214+
null_builder.append(true);
215+
188216
let count = count_array.value(row_index).to_usize().ok_or_else(|| {
189217
internal_datafusion_err!("array_resize: failed to convert size to usize")
190218
})?;
@@ -211,10 +239,12 @@ fn general_list_resize<O: OffsetSizeTrait + TryInto<i64>>(
211239
}
212240

213241
let data = mutable.freeze();
242+
let null_bit_buffer: NullBuffer = null_builder.finish().into();
243+
214244
Ok(Arc::new(GenericListArray::<O>::try_new(
215245
Arc::clone(field),
216246
OffsetBuffer::<O>::new(offsets.into()),
217247
arrow_array::make_array(data),
218-
None,
248+
Some(null_bit_buffer),
219249
)?))
220250
}

datafusion/sqllogictest/test_files/array.slt

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6985,7 +6985,7 @@ select array_resize(column1, column2, column3) from arrays_values;
69856985
[11, 12, 13, 14, 15, 16, 17, 18, , 20, 2, 2]
69866986
[21, 22, 23, , 25, 26, 27, 28, 29, 30, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
69876987
[31, 32, 33, 34, 35, , 37, 38, 39, 40, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
6988-
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
6988+
NULL
69896989
[]
69906990
[51, 52, , 54, 55, 56, 57, 58, 59, 60, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ]
69916991
[61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]
@@ -6997,7 +6997,7 @@ select array_resize(arrow_cast(column1, 'LargeList(Int64)'), column2, column3) f
69976997
[11, 12, 13, 14, 15, 16, 17, 18, , 20, 2, 2]
69986998
[21, 22, 23, , 25, 26, 27, 28, 29, 30, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
69996999
[31, 32, 33, 34, 35, , 37, 38, 39, 40, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
7000-
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
7000+
NULL
70017001
[]
70027002
[51, 52, , 54, 55, 56, 57, 58, 59, 60, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ]
70037003
[61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]
@@ -7013,6 +7013,51 @@ select array_resize(arrow_cast([[1], [2], [3]], 'LargeList(List(Int64))'), 10, [
70137013
----
70147014
[[1], [2], [3], [5], [5], [5], [5], [5], [5], [5]]
70157015

7016+
# array_resize null value
7017+
query ?
7018+
select array_resize(arrow_cast(NULL, 'List(Int8)'), 1);
7019+
----
7020+
NULL
7021+
7022+
statement ok
7023+
CREATE TABLE array_resize_values
7024+
AS VALUES
7025+
(make_array(1, NULL, 3, 4, 5, 6, 7, 8, 9, 10), 2, 1),
7026+
(make_array(11, 12, NULL, 14, 15, 16, 17, 18, 19, 20), 5, 2),
7027+
(make_array(21, 22, 23, 24, NULL, 26, 27, 28, 29, 30), 8, 3),
7028+
(make_array(31, 32, 33, 34, 35, 36, NULL, 38, 39, 40), 12, 4),
7029+
(NULL, 3, 0),
7030+
(make_array(41, 42, 43, 44, 45, 46, 47, 48, 49, 50), NULL, 6),
7031+
(make_array(51, 52, 53, 54, 55, NULL, 57, 58, 59, 60), 13, NULL),
7032+
(make_array(61, 62, 63, 64, 65, 66, 67, 68, 69, 70), 15, 7)
7033+
;
7034+
7035+
# array_resize columnar test #1
7036+
query ?
7037+
select array_resize(column1, column2, column3) from array_resize_values;
7038+
----
7039+
[1, ]
7040+
[11, 12, , 14, 15]
7041+
[21, 22, 23, 24, , 26, 27, 28]
7042+
[31, 32, 33, 34, 35, 36, , 38, 39, 40, 4, 4]
7043+
NULL
7044+
[]
7045+
[51, 52, 53, 54, 55, , 57, 58, 59, 60, , , ]
7046+
[61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 7, 7, 7, 7, 7]
7047+
7048+
# array_resize columnar test #2
7049+
query ?
7050+
select array_resize(arrow_cast(column1, 'LargeList(Int64)'), column2, column3) from array_resize_values;
7051+
----
7052+
[1, ]
7053+
[11, 12, , 14, 15]
7054+
[21, 22, 23, 24, , 26, 27, 28]
7055+
[31, 32, 33, 34, 35, 36, , 38, 39, 40, 4, 4]
7056+
NULL
7057+
[]
7058+
[51, 52, 53, 54, 55, , 57, 58, 59, 60, , , ]
7059+
[61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 7, 7, 7, 7, 7]
7060+
70167061
## array_reverse
70177062
query ??
70187063
select array_reverse(make_array(1, 2, 3)), array_reverse(make_array(1));

0 commit comments

Comments
 (0)