@@ -29,30 +29,31 @@ fn array_struct(args: &[ArrayRef]) -> Result<ArrayRef> {
29
29
return exec_err ! ( "struct requires at least one argument" ) ;
30
30
}
31
31
32
- let vec : Vec < _ > = args
32
+ let fields = args
33
33
. iter ( )
34
34
. enumerate ( )
35
35
. map ( |( i, arg) | {
36
36
let field_name = format ! ( "c{i}" ) ;
37
- Ok ( (
38
- Arc :: new ( Field :: new (
39
- field_name. as_str ( ) ,
40
- arg. data_type ( ) . clone ( ) ,
41
- true ,
42
- ) ) ,
43
- Arc :: clone ( arg) ,
44
- ) )
37
+ Ok ( Arc :: new ( Field :: new (
38
+ field_name. as_str ( ) ,
39
+ arg. data_type ( ) . clone ( ) ,
40
+ true ,
41
+ ) ) )
45
42
} )
46
- . collect :: < Result < Vec < _ > > > ( ) ?;
43
+ . collect :: < Result < Vec < _ > > > ( ) ?
44
+ . into ( ) ;
47
45
48
- Ok ( Arc :: new ( StructArray :: from ( vec) ) )
46
+ let arrays = args. to_vec ( ) ;
47
+
48
+ Ok ( Arc :: new ( StructArray :: new ( fields, arrays, None ) ) )
49
49
}
50
50
51
51
/// put values in a struct array.
52
52
fn struct_expr ( args : & [ ColumnarValue ] ) -> Result < ColumnarValue > {
53
53
let arrays = ColumnarValue :: values_to_arrays ( args) ?;
54
54
Ok ( ColumnarValue :: Array ( array_struct ( arrays. as_slice ( ) ) ?) )
55
55
}
56
+
56
57
#[ derive( Debug ) ]
57
58
pub struct StructFunc {
58
59
signature : Signature ,
@@ -97,48 +98,3 @@ impl ScalarUDFImpl for StructFunc {
97
98
struct_expr ( args)
98
99
}
99
100
}
100
-
101
- #[ cfg( test) ]
102
- mod tests {
103
- use super :: * ;
104
- use arrow:: array:: Int64Array ;
105
- use datafusion_common:: cast:: as_struct_array;
106
- use datafusion_common:: ScalarValue ;
107
-
108
- #[ test]
109
- fn test_struct ( ) {
110
- // struct(1, 2, 3) = {"c0": 1, "c1": 2, "c2": 3}
111
- let args = [
112
- ColumnarValue :: Scalar ( ScalarValue :: Int64 ( Some ( 1 ) ) ) ,
113
- ColumnarValue :: Scalar ( ScalarValue :: Int64 ( Some ( 2 ) ) ) ,
114
- ColumnarValue :: Scalar ( ScalarValue :: Int64 ( Some ( 3 ) ) ) ,
115
- ] ;
116
- let struc = struct_expr ( & args)
117
- . expect ( "failed to initialize function struct" )
118
- . into_array ( 1 )
119
- . expect ( "Failed to convert to array" ) ;
120
- let result =
121
- as_struct_array ( & struc) . expect ( "failed to initialize function struct" ) ;
122
- assert_eq ! (
123
- & Int64Array :: from( vec![ 1 ] ) ,
124
- Arc :: clone( result. column_by_name( "c0" ) . unwrap( ) )
125
- . as_any( )
126
- . downcast_ref:: <Int64Array >( )
127
- . unwrap( )
128
- ) ;
129
- assert_eq ! (
130
- & Int64Array :: from( vec![ 2 ] ) ,
131
- Arc :: clone( result. column_by_name( "c1" ) . unwrap( ) )
132
- . as_any( )
133
- . downcast_ref:: <Int64Array >( )
134
- . unwrap( )
135
- ) ;
136
- assert_eq ! (
137
- & Int64Array :: from( vec![ 3 ] ) ,
138
- Arc :: clone( result. column_by_name( "c2" ) . unwrap( ) )
139
- . as_any( )
140
- . downcast_ref:: <Int64Array >( )
141
- . unwrap( )
142
- ) ;
143
- }
144
- }
0 commit comments