@@ -2842,43 +2842,40 @@ fn literal_to_array_ref(
2842
2842
}
2843
2843
DataType :: List ( ref f) => {
2844
2844
let dt = f. data_type ( ) . clone ( ) ;
2845
- let child_arrays : Vec < ArrayRef > = list_literal
2846
- . list_values
2847
- . iter ( )
2848
- . map ( |c| literal_to_array_ref ( dt . clone ( ) , c . clone ( ) ) . unwrap ( ) )
2849
- . collect ( ) ;
2850
-
2851
- // Convert Vec<ArrayRef> into Vec<&dyn Array> for concat()
2852
- let child_refs : Vec < & dyn Array > =
2853
- child_arrays . iter ( ) . map ( |a| a . as_ref ( ) ) . collect ( ) ;
2854
-
2855
- // --- Build offsets for the parent list ---
2856
- let mut offsets = Vec :: with_capacity ( child_arrays . len ( ) + 1 ) ;
2857
- offsets . push ( 0 ) ; // the first list always starts at 0
2858
- let mut sum = 0 ;
2859
- for arr in & child_arrays {
2860
- sum += arr . len ( ) as i32 ; // each child's length adds to the total
2861
- offsets . push ( sum ) ; // store cumulative sum as the next offset
2845
+
2846
+ // Build offsets and collect non-null child arrays
2847
+ let mut offsets = Vec :: with_capacity ( list_literal . list_values . len ( ) + 1 ) ;
2848
+ offsets . push ( 0i32 ) ;
2849
+ let mut child_arrays : Vec < ArrayRef > = Vec :: new ( ) ;
2850
+
2851
+ for ( i , child_literal ) in list_literal . list_values . iter ( ) . enumerate ( ) {
2852
+ if list_literal . null_mask [ i ] {
2853
+ // Non-null entry: process the child array
2854
+ let child_array = literal_to_array_ref ( dt . clone ( ) , child_literal . clone ( ) ) ? ;
2855
+ let len = child_array . len ( ) as i32 ;
2856
+ offsets . push ( offsets . last ( ) . unwrap ( ) + len ) ;
2857
+ child_arrays . push ( child_array ) ;
2858
+ } else {
2859
+ // Null entry: just repeat the last offset (empty slot)
2860
+ offsets . push ( * offsets . last ( ) . unwrap ( ) ) ;
2861
+ }
2862
2862
}
2863
2863
2864
- // Concatenate all child arrays' * values* into one array
2865
- // Example: [[1,2,3], [4,5,6]] → values = [1,2,3,4,5,6]
2866
- let output_array = if ! child_refs. is_empty ( ) {
2864
+ // Concatenate all non-null child arrays' values into one array
2865
+ let output_array = if !child_arrays . is_empty ( ) {
2866
+ let child_refs: Vec < & dyn Array > = child_arrays . iter ( ) . map ( |a| a . as_ref ( ) ) . collect ( ) ;
2867
2867
arrow:: compute:: concat ( & child_refs) ?
2868
2868
} else {
2869
- let x = new_empty_array ( & dt. clone ( ) ) ;
2870
- offsets = vec ! [ x. offset( ) as i32 ] ;
2871
- dbg ! ( & offsets) ;
2872
- x
2869
+ // All entries are null or the list is empty
2870
+ new_empty_array ( & dt)
2873
2871
} ;
2874
2872
2875
2873
// Create and return the parent ListArray
2876
2874
Ok ( Arc :: new ( ListArray :: new (
2877
- // Field: item type matches the concatenated child's type
2878
2875
FieldRef :: from ( Field :: new ( "item" , output_array. data_type ( ) . clone ( ) , true ) ) ,
2879
2876
OffsetBuffer :: new ( offsets. into ( ) ) ,
2880
2877
output_array,
2881
- Some ( NullBuffer :: from ( list_literal. null_mask ) ) ,
2878
+ Some ( NullBuffer :: from ( list_literal. null_mask . clone ( ) ) ) ,
2882
2879
) ) )
2883
2880
}
2884
2881
dt => Err ( GeneralError ( format ! (
@@ -3704,12 +3701,14 @@ mod tests {
3704
3701
[1, 2, 3],
3705
3702
[4, 5, 6],
3706
3703
[7, 8, 9, null],
3704
+ [],
3707
3705
null
3708
3706
],
3709
3707
[
3710
3708
[10, null, 12]
3711
3709
],
3712
- null
3710
+ null,
3711
+ []
3713
3712
]
3714
3713
*/
3715
3714
let data = ListLiteral {
@@ -3750,11 +3749,14 @@ mod tests {
3750
3749
null_mask: vec![ true ] ,
3751
3750
..Default :: default ( )
3752
3751
} ,
3753
- // ListLiteral {
3754
- // ..Default::default()
3755
- // },
3752
+ ListLiteral {
3753
+ ..Default :: default ( )
3754
+ } ,
3755
+ ListLiteral {
3756
+ ..Default :: default ( )
3757
+ } ,
3756
3758
] ,
3757
- null_mask : vec ! [ true , true ] ,
3759
+ null_mask : vec ! [ true , true , false , true ] ,
3758
3760
..Default :: default ( )
3759
3761
} ;
3760
3762
@@ -3778,38 +3780,14 @@ mod tests {
3778
3780
true , // outer list nullable
3779
3781
) ) ) ;
3780
3782
3781
- // let data = ListLiteral {
3782
- // list_values: vec![
3783
- // ListLiteral {
3784
- // int_values: vec![1, 2],
3785
- // null_mask: vec![true, true],
3786
- // ..Default::default()
3787
- // },
3788
- // ListLiteral {
3789
- // ..Default::default()
3790
- // }
3791
- // ],
3792
- // null_mask: vec![true, false],
3793
- // ..Default::default()
3794
- // };
3795
- //
3796
- // let nested_type = DataType::List(FieldRef::from(Field::new(
3797
- // "item",
3798
- // DataType::List(
3799
- // Field::new("item", DataType::Int32, true).into(),
3800
- // ), true)));
3801
-
3802
3783
let array = literal_to_array_ref ( nested_type, data) ?;
3803
3784
3804
3785
dbg ! ( & array) ;
3805
3786
dbg ! ( & array. nulls( ) ) ;
3806
3787
3807
-
3808
-
3809
-
3810
3788
// Top-level should be ListArray<ListArray<Int32>>
3811
3789
let list_outer = array. as_any ( ) . downcast_ref :: < ListArray > ( ) . unwrap ( ) ;
3812
- assert_eq ! ( list_outer. len( ) , 2 ) ;
3790
+ assert_eq ! ( list_outer. len( ) , 4 ) ;
3813
3791
3814
3792
// First outer element: ListArray<Int32>
3815
3793
let first_elem = list_outer. value ( 0 ) ;
0 commit comments