@@ -20,12 +20,11 @@ use std::collections::VecDeque;
2020use std:: sync:: Arc ;
2121
2222use arrow:: array:: { Array , ArrayData , ArrayRef , MapArray , StructArray } ;
23- use arrow:: compute:: concat;
2423use arrow:: datatypes:: { DataType , Field , SchemaBuilder } ;
2524use arrow_buffer:: { Buffer , ToByteSlice } ;
2625
27- use datafusion_common:: { exec_err , internal_err , ScalarValue } ;
28- use datafusion_common:: { not_impl_err , Result } ;
26+ use datafusion_common:: Result ;
27+ use datafusion_common:: { exec_err , ScalarValue } ;
2928use datafusion_expr:: { ColumnarValue , ScalarUDFImpl , Signature , Volatility } ;
3029
3130/// Check if we can evaluate the expr to constant directly.
@@ -40,41 +39,6 @@ fn can_evaluate_to_const(args: &[ColumnarValue]) -> bool {
4039 . all ( |arg| matches ! ( arg, ColumnarValue :: Scalar ( _) ) )
4140}
4241
43- fn make_map ( args : & [ ColumnarValue ] ) -> Result < ColumnarValue > {
44- let can_evaluate_to_const = can_evaluate_to_const ( args) ;
45-
46- let ( key, value) : ( Vec < _ > , Vec < _ > ) = args
47- . chunks_exact ( 2 )
48- . map ( |chunk| {
49- if let ColumnarValue :: Array ( _) = chunk[ 0 ] {
50- return not_impl_err ! ( "make_map does not support array keys" ) ;
51- }
52- if let ColumnarValue :: Array ( _) = chunk[ 1 ] {
53- return not_impl_err ! ( "make_map does not support array values" ) ;
54- }
55- Ok ( ( chunk[ 0 ] . clone ( ) , chunk[ 1 ] . clone ( ) ) )
56- } )
57- . collect :: < Result < Vec < _ > > > ( ) ?
58- . into_iter ( )
59- . unzip ( ) ;
60-
61- let keys = ColumnarValue :: values_to_arrays ( & key) ?;
62- let values = ColumnarValue :: values_to_arrays ( & value) ?;
63-
64- let keys: Vec < _ > = keys. iter ( ) . map ( |k| k. as_ref ( ) ) . collect ( ) ;
65- let values: Vec < _ > = values. iter ( ) . map ( |v| v. as_ref ( ) ) . collect ( ) ;
66-
67- let key = match concat ( & keys) {
68- Ok ( key) => key,
69- Err ( e) => return internal_err ! ( "Error concatenating keys: {}" , e) ,
70- } ;
71- let value = match concat ( & values) {
72- Ok ( value) => value,
73- Err ( e) => return internal_err ! ( "Error concatenating values: {}" , e) ,
74- } ;
75- make_map_batch_internal ( key, value, can_evaluate_to_const)
76- }
77-
7842fn make_map_batch ( args : & [ ColumnarValue ] ) -> Result < ColumnarValue > {
7943 if args. len ( ) != 2 {
8044 return exec_err ! (
@@ -154,115 +118,6 @@ fn make_map_batch_internal(
154118 } )
155119}
156120
157- #[ derive( Debug ) ]
158- pub struct MakeMap {
159- signature : Signature ,
160- }
161-
162- impl Default for MakeMap {
163- fn default ( ) -> Self {
164- Self :: new ( )
165- }
166- }
167-
168- impl MakeMap {
169- pub fn new ( ) -> Self {
170- Self {
171- signature : Signature :: user_defined ( Volatility :: Immutable ) ,
172- }
173- }
174- }
175-
176- impl ScalarUDFImpl for MakeMap {
177- fn as_any ( & self ) -> & dyn Any {
178- self
179- }
180-
181- fn name ( & self ) -> & str {
182- "make_map"
183- }
184-
185- fn signature ( & self ) -> & Signature {
186- & self . signature
187- }
188-
189- fn coerce_types ( & self , arg_types : & [ DataType ] ) -> Result < Vec < DataType > > {
190- if arg_types. is_empty ( ) {
191- return exec_err ! (
192- "make_map requires at least one pair of arguments, got 0 instead"
193- ) ;
194- }
195- if arg_types. len ( ) % 2 != 0 {
196- return exec_err ! (
197- "make_map requires an even number of arguments, got {} instead" ,
198- arg_types. len( )
199- ) ;
200- }
201-
202- let key_type = & arg_types[ 0 ] ;
203- let mut value_type = & arg_types[ 1 ] ;
204-
205- for ( i, chunk) in arg_types. chunks_exact ( 2 ) . enumerate ( ) {
206- if chunk[ 0 ] . is_null ( ) {
207- return exec_err ! ( "make_map key cannot be null at position {}" , i) ;
208- }
209- if & chunk[ 0 ] != key_type {
210- return exec_err ! (
211- "make_map requires all keys to have the same type {}, got {} instead at position {}" ,
212- key_type,
213- chunk[ 0 ] ,
214- i
215- ) ;
216- }
217-
218- if !chunk[ 1 ] . is_null ( ) {
219- if value_type. is_null ( ) {
220- value_type = & chunk[ 1 ] ;
221- } else if & chunk[ 1 ] != value_type {
222- return exec_err ! (
223- "map requires all values to have the same type {}, got {} instead at position {}" ,
224- value_type,
225- & chunk[ 1 ] ,
226- i
227- ) ;
228- }
229- }
230- }
231-
232- let mut result = Vec :: new ( ) ;
233- for _ in 0 ..arg_types. len ( ) / 2 {
234- result. push ( key_type. clone ( ) ) ;
235- result. push ( value_type. clone ( ) ) ;
236- }
237-
238- Ok ( result)
239- }
240-
241- fn return_type ( & self , arg_types : & [ DataType ] ) -> Result < DataType > {
242- let key_type = & arg_types[ 0 ] ;
243- let mut value_type = & arg_types[ 1 ] ;
244-
245- for chunk in arg_types. chunks_exact ( 2 ) {
246- if !chunk[ 1 ] . is_null ( ) && value_type. is_null ( ) {
247- value_type = & chunk[ 1 ] ;
248- }
249- }
250-
251- let mut builder = SchemaBuilder :: new ( ) ;
252- builder. push ( Field :: new ( "key" , key_type. clone ( ) , false ) ) ;
253- builder. push ( Field :: new ( "value" , value_type. clone ( ) , true ) ) ;
254- let fields = builder. finish ( ) . fields ;
255- Ok ( DataType :: Map (
256- Arc :: new ( Field :: new ( "entries" , DataType :: Struct ( fields) , false ) ) ,
257- false ,
258- ) )
259- }
260-
261- fn invoke ( & self , args : & [ ColumnarValue ] ) -> Result < ColumnarValue > {
262- make_map ( args)
263- }
264- }
265-
266121#[ derive( Debug ) ]
267122pub struct MapFunc {
268123 signature : Signature ,
0 commit comments