@@ -25,21 +25,91 @@ use std::sync::Arc;
2525
2626use super :: ColumnarValue ;
2727
28- fn array_array ( arrays : & [ & dyn Array ] ) -> Result < FixedSizeListArray > {
28+ fn array_array ( arrays : & [ & dyn Array ] ) -> Result < ArrayRef > {
2929 assert ! ( !arrays. is_empty( ) ) ;
3030 let first = arrays[ 0 ] ;
3131 assert ! ( arrays. iter( ) . all( |x| x. len( ) == first. len( ) ) ) ;
3232 assert ! ( arrays. iter( ) . all( |x| x. data_type( ) == first. data_type( ) ) ) ;
3333
3434 let size = arrays. len ( ) ;
3535
36- let values = concat:: concatenate ( arrays) ?;
37- let data_type = FixedSizeListArray :: default_datatype ( first. data_type ( ) . clone ( ) , size) ;
38- Ok ( FixedSizeListArray :: from_data (
39- data_type,
40- values. into ( ) ,
41- None ,
42- ) )
36+ macro_rules! array {
37+ ( $PRIMITIVE: ty, $ARRAY: ty, $DATA_TYPE: path) => { {
38+ let array = MutablePrimitiveArray :: <$PRIMITIVE>:: with_capacity_from( first. len( ) * size, $DATA_TYPE) ;
39+ let mut array = MutableFixedSizeListArray :: new( array, size) ;
40+ // for each entry in the array
41+ for index in 0 ..first. len( ) {
42+ let values = array. mut_values( ) ;
43+ for arg in arrays {
44+ let arg = arg. as_any( ) . downcast_ref:: <$ARRAY>( ) . unwrap( ) ;
45+ if arg. is_null( index) {
46+ values. push( None ) ;
47+ } else {
48+ values. push( Some ( arg. value( index) ) ) ;
49+ }
50+ }
51+ }
52+ Ok ( array. as_arc( ) )
53+ } } ;
54+ }
55+
56+ macro_rules! array_string {
57+ ( $OFFSET: ty) => { {
58+ let array = MutableUtf8Array :: <$OFFSET>:: with_capacity( first. len( ) * size) ;
59+ let mut array = MutableFixedSizeListArray :: new( array, size) ;
60+ // for each entry in the array
61+ for index in 0 ..first. len( ) {
62+ let values = array. mut_values( ) ;
63+ for arg in arrays {
64+ let arg = arg. as_any( ) . downcast_ref:: <Utf8Array <$OFFSET>>( ) . unwrap( ) ;
65+ if arg. is_null( index) {
66+ values. push:: <& str >( None ) ;
67+ } else {
68+ values. push( Some ( arg. value( index) ) ) ;
69+ }
70+ }
71+ }
72+ Ok ( array. as_arc( ) )
73+ } } ;
74+ }
75+
76+
77+ match first. data_type ( ) {
78+ DataType :: Boolean => {
79+ let array = MutableBooleanArray :: with_capacity ( first. len ( ) * size) ;
80+ let mut array = MutableFixedSizeListArray :: new ( array, size) ;
81+ // for each entry in the array
82+ for index in 0 ..first. len ( ) {
83+ let values = array. mut_values ( ) ;
84+ for arg in arrays {
85+ let arg = arg. as_any ( ) . downcast_ref :: < BooleanArray > ( ) . unwrap ( ) ;
86+ if arg. is_null ( index) {
87+ values. push ( None ) ;
88+ } else {
89+ values. push ( Some ( arg. value ( index) ) ) ;
90+ }
91+ }
92+ }
93+ Ok ( array. as_arc ( ) )
94+ } ,
95+ DataType :: UInt8 => array ! ( u8 , PrimitiveArray <u8 >, DataType :: UInt8 ) ,
96+ DataType :: UInt16 => array ! ( u16 , PrimitiveArray <u16 >, DataType :: UInt16 ) ,
97+ DataType :: UInt32 => array ! ( u32 , PrimitiveArray <u32 >, DataType :: UInt32 ) ,
98+ DataType :: UInt64 => array ! ( u64 , PrimitiveArray <u64 >, DataType :: UInt64 ) ,
99+ DataType :: Int8 => array ! ( i8 , PrimitiveArray <i8 >, DataType :: Int8 ) ,
100+ DataType :: Int16 => array ! ( i16 , PrimitiveArray <i16 >, DataType :: Int16 ) ,
101+ DataType :: Int32 => array ! ( i32 , PrimitiveArray <i32 >, DataType :: Int32 ) ,
102+ DataType :: Int64 => array ! ( i64 , PrimitiveArray <i64 >, DataType :: Int64 ) ,
103+ DataType :: Float32 => array ! ( f32 , PrimitiveArray <f32 >, DataType :: Float32 ) ,
104+ DataType :: Float64 => array ! ( f64 , PrimitiveArray <f64 >, DataType :: Float64 ) ,
105+ DataType :: Utf8 => array_string ! ( i32 ) ,
106+ DataType :: LargeUtf8 => array_string ! ( i64 ) ,
107+ data_type => Err ( DataFusionError :: NotImplemented ( format ! (
108+ "Array is not implemented for type '{:?}'." ,
109+ data_type
110+ ) ) ) ,
111+ }
112+
43113}
44114
45115/// put values in an array.
@@ -57,7 +127,7 @@ pub fn array(values: &[ColumnarValue]) -> Result<ColumnarValue> {
57127 } )
58128 . collect :: < Result < _ > > ( ) ?;
59129
60- Ok ( ColumnarValue :: Array ( array_array ( & arrays) . map ( Arc :: new ) ?) )
130+ Ok ( ColumnarValue :: Array ( array_array ( & arrays) ?) )
61131}
62132
63133/// Currently supported types by the array function.
0 commit comments