Skip to content

Commit 9dac47c

Browse files
committed
null fix
1 parent 8f00a65 commit 9dac47c

File tree

1 file changed

+34
-56
lines changed

1 file changed

+34
-56
lines changed

native/core/src/execution/planner.rs

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,43 +2813,40 @@ fn literal_to_array_ref(
28132813
}
28142814
DataType::List(ref f) => {
28152815
let dt = f.data_type().clone();
2816-
let child_arrays: Vec<ArrayRef> = list_literal
2817-
.list_values
2818-
.iter()
2819-
.map(|c| literal_to_array_ref(dt.clone(), c.clone()).unwrap())
2820-
.collect();
2821-
2822-
// Convert Vec<ArrayRef> into Vec<&dyn Array> for concat()
2823-
let child_refs: Vec<&dyn Array> =
2824-
child_arrays.iter().map(|a| a.as_ref()).collect();
2825-
2826-
// --- Build offsets for the parent list ---
2827-
let mut offsets = Vec::with_capacity(child_arrays.len() + 1);
2828-
offsets.push(0); // the first list always starts at 0
2829-
let mut sum = 0;
2830-
for arr in &child_arrays {
2831-
sum += arr.len() as i32; // each child's length adds to the total
2832-
offsets.push(sum); // store cumulative sum as the next offset
2816+
2817+
// Build offsets and collect non-null child arrays
2818+
let mut offsets = Vec::with_capacity(list_literal.list_values.len() + 1);
2819+
offsets.push(0i32);
2820+
let mut child_arrays: Vec<ArrayRef> = Vec::new();
2821+
2822+
for (i, child_literal) in list_literal.list_values.iter().enumerate() {
2823+
if list_literal.null_mask[i] {
2824+
// Non-null entry: process the child array
2825+
let child_array = literal_to_array_ref(dt.clone(), child_literal.clone())?;
2826+
let len = child_array.len() as i32;
2827+
offsets.push(offsets.last().unwrap() + len);
2828+
child_arrays.push(child_array);
2829+
} else {
2830+
// Null entry: just repeat the last offset (empty slot)
2831+
offsets.push(*offsets.last().unwrap());
2832+
}
28332833
}
28342834

2835-
// Concatenate all child arrays' *values* into one array
2836-
// Example: [[1,2,3], [4,5,6]] → values = [1,2,3,4,5,6]
2837-
let output_array = if !child_refs.is_empty() {
2835+
// Concatenate all non-null child arrays' values into one array
2836+
let output_array = if !child_arrays.is_empty() {
2837+
let child_refs: Vec<&dyn Array> = child_arrays.iter().map(|a| a.as_ref()).collect();
28382838
arrow::compute::concat(&child_refs)?
28392839
} else {
2840-
let x = new_empty_array(&dt.clone());
2841-
offsets = vec![x.offset() as i32];
2842-
dbg!(&offsets);
2843-
x
2840+
// All entries are null or the list is empty
2841+
new_empty_array(&dt)
28442842
};
28452843

28462844
// Create and return the parent ListArray
28472845
Ok(Arc::new(ListArray::new(
2848-
// Field: item type matches the concatenated child's type
28492846
FieldRef::from(Field::new("item", output_array.data_type().clone(), true)),
28502847
OffsetBuffer::new(offsets.into()),
28512848
output_array,
2852-
Some(NullBuffer::from(list_literal.null_mask)),
2849+
Some(NullBuffer::from(list_literal.null_mask.clone())),
28532850
)))
28542851
}
28552852
dt => Err(GeneralError(format!(
@@ -3672,12 +3669,14 @@ mod tests {
36723669
[1, 2, 3],
36733670
[4, 5, 6],
36743671
[7, 8, 9, null],
3672+
[],
36753673
null
36763674
],
36773675
[
36783676
[10, null, 12]
36793677
],
3680-
null
3678+
null,
3679+
[]
36813680
]
36823681
*/
36833682
let data = ListLiteral {
@@ -3718,11 +3717,14 @@ mod tests {
37183717
null_mask: vec![true],
37193718
..Default::default()
37203719
},
3721-
// ListLiteral {
3722-
// ..Default::default()
3723-
// },
3720+
ListLiteral {
3721+
..Default::default()
3722+
},
3723+
ListLiteral {
3724+
..Default::default()
3725+
},
37243726
],
3725-
null_mask: vec![true, true],
3727+
null_mask: vec![true, true, false, true],
37263728
..Default::default()
37273729
};
37283730

@@ -3746,38 +3748,14 @@ mod tests {
37463748
true, // outer list nullable
37473749
)));
37483750

3749-
// let data = ListLiteral {
3750-
// list_values: vec![
3751-
// ListLiteral {
3752-
// int_values: vec![1, 2],
3753-
// null_mask: vec![true, true],
3754-
// ..Default::default()
3755-
// },
3756-
// ListLiteral {
3757-
// ..Default::default()
3758-
// }
3759-
// ],
3760-
// null_mask: vec![true, false],
3761-
// ..Default::default()
3762-
// };
3763-
//
3764-
// let nested_type = DataType::List(FieldRef::from(Field::new(
3765-
// "item",
3766-
// DataType::List(
3767-
// Field::new("item", DataType::Int32, true).into(),
3768-
// ), true)));
3769-
37703751
let array = literal_to_array_ref(nested_type, data)?;
37713752

37723753
dbg!(&array);
37733754
dbg!(&array.nulls());
37743755

3775-
3776-
3777-
37783756
// Top-level should be ListArray<ListArray<Int32>>
37793757
let list_outer = array.as_any().downcast_ref::<ListArray>().unwrap();
3780-
assert_eq!(list_outer.len(), 2);
3758+
assert_eq!(list_outer.len(), 4);
37813759

37823760
// First outer element: ListArray<Int32>
37833761
let first_elem = list_outer.value(0);

0 commit comments

Comments
 (0)