@@ -2813,43 +2813,40 @@ fn literal_to_array_ref(
2813
2813
}
2814
2814
DataType :: List ( ref f) => {
2815
2815
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
+ }
2833
2833
}
2834
2834
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 ( ) ;
2838
2838
arrow:: compute:: concat ( & child_refs) ?
2839
2839
} 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)
2844
2842
} ;
2845
2843
2846
2844
// Create and return the parent ListArray
2847
2845
Ok ( Arc :: new ( ListArray :: new (
2848
- // Field: item type matches the concatenated child's type
2849
2846
FieldRef :: from ( Field :: new ( "item" , output_array. data_type ( ) . clone ( ) , true ) ) ,
2850
2847
OffsetBuffer :: new ( offsets. into ( ) ) ,
2851
2848
output_array,
2852
- Some ( NullBuffer :: from ( list_literal. null_mask ) ) ,
2849
+ Some ( NullBuffer :: from ( list_literal. null_mask . clone ( ) ) ) ,
2853
2850
) ) )
2854
2851
}
2855
2852
dt => Err ( GeneralError ( format ! (
@@ -3672,12 +3669,14 @@ mod tests {
3672
3669
[1, 2, 3],
3673
3670
[4, 5, 6],
3674
3671
[7, 8, 9, null],
3672
+ [],
3675
3673
null
3676
3674
],
3677
3675
[
3678
3676
[10, null, 12]
3679
3677
],
3680
- null
3678
+ null,
3679
+ []
3681
3680
]
3682
3681
*/
3683
3682
let data = ListLiteral {
@@ -3718,11 +3717,14 @@ mod tests {
3718
3717
null_mask: vec![ true ] ,
3719
3718
..Default :: default ( )
3720
3719
} ,
3721
- // ListLiteral {
3722
- // ..Default::default()
3723
- // },
3720
+ ListLiteral {
3721
+ ..Default :: default ( )
3722
+ } ,
3723
+ ListLiteral {
3724
+ ..Default :: default ( )
3725
+ } ,
3724
3726
] ,
3725
- null_mask : vec ! [ true , true ] ,
3727
+ null_mask : vec ! [ true , true , false , true ] ,
3726
3728
..Default :: default ( )
3727
3729
} ;
3728
3730
@@ -3746,38 +3748,14 @@ mod tests {
3746
3748
true , // outer list nullable
3747
3749
) ) ) ;
3748
3750
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
-
3770
3751
let array = literal_to_array_ref ( nested_type, data) ?;
3771
3752
3772
3753
dbg ! ( & array) ;
3773
3754
dbg ! ( & array. nulls( ) ) ;
3774
3755
3775
-
3776
-
3777
-
3778
3756
// Top-level should be ListArray<ListArray<Int32>>
3779
3757
let list_outer = array. as_any ( ) . downcast_ref :: < ListArray > ( ) . unwrap ( ) ;
3780
- assert_eq ! ( list_outer. len( ) , 2 ) ;
3758
+ assert_eq ! ( list_outer. len( ) , 4 ) ;
3781
3759
3782
3760
// First outer element: ListArray<Int32>
3783
3761
let first_elem = list_outer. value ( 0 ) ;
0 commit comments