@@ -122,7 +122,8 @@ pub enum TypeSignature {
122122 /// is `OneOf(vec![Any(0), VariadicAny])`.
123123 OneOf ( Vec < TypeSignature > ) ,
124124 /// Specifies Signatures for array functions
125- ArraySignature ( ArrayFunctionSignature ) ,
125+ /// Boolean value specifies whether null type coercion is allowed
126+ ArraySignature ( ArrayFunctionSignature , bool ) ,
126127}
127128
128129#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
@@ -144,13 +145,19 @@ pub enum ArrayFunctionSignature {
144145}
145146
146147impl ArrayFunctionSignature {
148+ /// Arguments to ArrayFunctionSignature
149+ /// `current_types` - The data types of the arguments
150+ /// `coercion` - Whether null type coercion is allowed
151+ /// Returns the valid types for the function signature
147152 pub fn get_type_signature (
148153 & self ,
149154 current_types : & [ DataType ] ,
155+ allow_null_coercion : bool ,
150156 ) -> Result < Vec < Vec < DataType > > > {
151157 fn array_append_or_prepend_valid_types (
152158 current_types : & [ DataType ] ,
153159 is_append : bool ,
160+ allow_null_coercion : bool ,
154161 ) -> Result < Vec < Vec < DataType > > > {
155162 if current_types. len ( ) != 2 {
156163 return Ok ( vec ! [ vec![ ] ] ) ;
@@ -163,7 +170,7 @@ impl ArrayFunctionSignature {
163170 } ;
164171
165172 // We follow Postgres on `array_append(Null, T)`, which is not valid.
166- if array_type. eq ( & DataType :: Null ) {
173+ if array_type. eq ( & DataType :: Null ) && !allow_null_coercion {
167174 return Ok ( vec ! [ vec![ ] ] ) ;
168175 }
169176
@@ -215,8 +222,13 @@ impl ArrayFunctionSignature {
215222 _ => Ok ( vec ! [ vec![ ] ] ) ,
216223 }
217224 }
218- fn array ( current_types : & [ DataType ] ) -> Result < Vec < Vec < DataType > > > {
219- if current_types. len ( ) != 1 {
225+ fn array (
226+ current_types : & [ DataType ] ,
227+ allow_null_coercion : bool ,
228+ ) -> Result < Vec < Vec < DataType > > > {
229+ if current_types. len ( ) != 1
230+ || ( current_types[ 0 ] . is_null ( ) && !allow_null_coercion)
231+ {
220232 return Ok ( vec ! [ vec![ ] ] ) ;
221233 }
222234
@@ -229,7 +241,6 @@ impl ArrayFunctionSignature {
229241 let array_type = coerced_fixed_size_list_to_list ( array_type) ;
230242 Ok ( vec ! [ vec![ array_type] ] )
231243 }
232- DataType :: Null => Ok ( vec ! [ vec![ array_type. to_owned( ) ] ] ) ,
233244 _ => Ok ( vec ! [ vec![ DataType :: List ( Arc :: new( Field :: new(
234245 "item" ,
235246 array_type. to_owned( ) ,
@@ -239,13 +250,21 @@ impl ArrayFunctionSignature {
239250 }
240251 match self {
241252 ArrayFunctionSignature :: ArrayAndElement => {
242- array_append_or_prepend_valid_types ( current_types, true )
253+ array_append_or_prepend_valid_types (
254+ current_types,
255+ true ,
256+ allow_null_coercion,
257+ )
243258 }
244259 ArrayFunctionSignature :: ElementAndArray => {
245- array_append_or_prepend_valid_types ( current_types, false )
260+ array_append_or_prepend_valid_types (
261+ current_types,
262+ false ,
263+ allow_null_coercion,
264+ )
246265 }
247266 ArrayFunctionSignature :: ArrayAndIndex => array_and_index ( current_types) ,
248- ArrayFunctionSignature :: Array => array ( current_types) ,
267+ ArrayFunctionSignature :: Array => array ( current_types, allow_null_coercion ) ,
249268 }
250269 }
251270}
@@ -297,7 +316,7 @@ impl TypeSignature {
297316 TypeSignature :: OneOf ( sigs) => {
298317 sigs. iter ( ) . flat_map ( |s| s. to_string_repr ( ) ) . collect ( )
299318 }
300- TypeSignature :: ArraySignature ( array_signature) => {
319+ TypeSignature :: ArraySignature ( array_signature, _ ) => {
301320 vec ! [ array_signature. to_string( ) ]
302321 }
303322 }
@@ -402,36 +421,42 @@ impl Signature {
402421 }
403422 }
404423 /// Specialized Signature for ArrayAppend and similar functions
405- pub fn array_and_element ( volatility : Volatility ) -> Self {
424+ pub fn array_and_element ( allow_null_coercion : bool , volatility : Volatility ) -> Self {
406425 Signature {
407426 type_signature : TypeSignature :: ArraySignature (
408427 ArrayFunctionSignature :: ArrayAndElement ,
428+ allow_null_coercion,
409429 ) ,
410430 volatility,
411431 }
412432 }
413433 /// Specialized Signature for ArrayPrepend and similar functions
414- pub fn element_and_array ( volatility : Volatility ) -> Self {
434+ pub fn element_and_array ( allow_null_coercion : bool , volatility : Volatility ) -> Self {
415435 Signature {
416436 type_signature : TypeSignature :: ArraySignature (
417437 ArrayFunctionSignature :: ElementAndArray ,
438+ allow_null_coercion,
418439 ) ,
419440 volatility,
420441 }
421442 }
422443 /// Specialized Signature for ArrayElement and similar functions
423- pub fn array_and_index ( volatility : Volatility ) -> Self {
444+ pub fn array_and_index ( allow_null_coercion : bool , volatility : Volatility ) -> Self {
424445 Signature {
425446 type_signature : TypeSignature :: ArraySignature (
426447 ArrayFunctionSignature :: ArrayAndIndex ,
448+ allow_null_coercion,
427449 ) ,
428450 volatility,
429451 }
430452 }
431453 /// Specialized Signature for ArrayEmpty and similar functions
432- pub fn array ( volatility : Volatility ) -> Self {
454+ pub fn array ( allow_null_coercion : bool , volatility : Volatility ) -> Self {
433455 Signature {
434- type_signature : TypeSignature :: ArraySignature ( ArrayFunctionSignature :: Array ) ,
456+ type_signature : TypeSignature :: ArraySignature (
457+ ArrayFunctionSignature :: Array ,
458+ allow_null_coercion,
459+ ) ,
435460 volatility,
436461 }
437462 }
0 commit comments