@@ -183,6 +183,7 @@ pub(crate) fn handle<O, F, S>(
183183 args : & [ ColumnarValue ] ,
184184 op : F ,
185185 name : & str ,
186+ safe : bool ,
186187) -> Result < ColumnarValue >
187188where
188189 O : ArrowPrimitiveType ,
@@ -195,26 +196,35 @@ where
195196 unary_string_to_primitive_function :: < & StringViewArray , O , _ > (
196197 a. as_ref ( ) . as_string_view ( ) ,
197198 op,
199+ safe,
198200 ) ?,
199201 ) ) ) ,
200202 DataType :: LargeUtf8 => Ok ( ColumnarValue :: Array ( Arc :: new (
201203 unary_string_to_primitive_function :: < & GenericStringArray < i64 > , O , _ > (
202204 a. as_ref ( ) . as_string :: < i64 > ( ) ,
203205 op,
206+ safe,
204207 ) ?,
205208 ) ) ) ,
206209 DataType :: Utf8 => Ok ( ColumnarValue :: Array ( Arc :: new (
207210 unary_string_to_primitive_function :: < & GenericStringArray < i32 > , O , _ > (
208211 a. as_ref ( ) . as_string :: < i32 > ( ) ,
209212 op,
213+ safe,
210214 ) ?,
211215 ) ) ) ,
212216 other => exec_err ! ( "Unsupported data type {other:?} for function {name}" ) ,
213217 } ,
214218 ColumnarValue :: Scalar ( scalar) => match scalar. try_as_str ( ) {
215219 Some ( a) => {
216- let result = a. as_ref ( ) . map ( |x| op ( x) ) . transpose ( ) ?;
217- Ok ( ColumnarValue :: Scalar ( S :: scalar ( result) ) )
220+ let result = a. as_ref ( ) . map ( |x| op ( x) ) . transpose ( ) ;
221+ if let Ok ( v) = result {
222+ Ok ( ColumnarValue :: Scalar ( S :: scalar ( v) ) )
223+ } else if safe {
224+ Ok ( ColumnarValue :: Scalar ( S :: scalar ( None ) ) )
225+ } else {
226+ Err ( result. err ( ) . unwrap ( ) )
227+ }
218228 }
219229 _ => exec_err ! ( "Unsupported data type {scalar:?} for function {name}" ) ,
220230 } ,
@@ -229,6 +239,7 @@ pub(crate) fn handle_multiple<O, F, S, M>(
229239 op : F ,
230240 op2 : M ,
231241 name : & str ,
242+ safe : bool ,
232243) -> Result < ColumnarValue >
233244where
234245 O : ArrowPrimitiveType ,
@@ -260,7 +271,7 @@ where
260271 }
261272
262273 Ok ( ColumnarValue :: Array ( Arc :: new (
263- strings_to_primitive_function :: < O , _ , _ > ( args, op, op2, name) ?,
274+ strings_to_primitive_function :: < O , _ , _ > ( args, op, op2, name, safe ) ?,
264275 ) ) )
265276 }
266277 other => {
@@ -270,12 +281,12 @@ where
270281 // if the first argument is a scalar utf8 all arguments are expected to be scalar utf8
271282 ColumnarValue :: Scalar ( scalar) => match scalar. try_as_str ( ) {
272283 Some ( a) => {
284+ let mut val: Option < Result < ColumnarValue > > = None ;
285+ let mut err: Option < DataFusionError > = None ;
273286 let a = a. as_ref ( ) ;
274287 // ASK: Why do we trust `a` to be non-null at this point?
275288 let a = unwrap_or_internal_err ! ( a) ;
276289
277- let mut ret = None ;
278-
279290 for ( pos, v) in args. iter ( ) . enumerate ( ) . skip ( 1 ) {
280291 let ColumnarValue :: Scalar (
281292 ScalarValue :: Utf8View ( x)
@@ -289,17 +300,26 @@ where
289300 if let Some ( s) = x {
290301 match op ( a, s. as_str ( ) ) {
291302 Ok ( r) => {
292- ret = Some ( Ok ( ColumnarValue :: Scalar ( S :: scalar ( Some (
303+ val = Some ( Ok ( ColumnarValue :: Scalar ( S :: scalar ( Some (
293304 op2 ( r) ,
294305 ) ) ) ) ) ;
295306 break ;
296307 }
297- Err ( e) => ret = Some ( Err ( e ) ) ,
308+ Err ( e) => err = Some ( e ) ,
298309 }
299310 }
300311 }
301312
302- unwrap_or_internal_err ! ( ret)
313+ if let Some ( v) = val {
314+ v
315+ } else if safe {
316+ Ok ( ColumnarValue :: Scalar ( S :: scalar ( None ) ) )
317+ } else {
318+ match err {
319+ Some ( e) => Err ( e) ,
320+ None => Ok ( ColumnarValue :: Scalar ( S :: scalar ( None ) ) ) ,
321+ }
322+ }
303323 }
304324 other => {
305325 exec_err ! ( "Unsupported data type {other:?} for function {name}" )
@@ -317,12 +337,13 @@ where
317337/// # Errors
318338/// This function errors iff:
319339/// * the number of arguments is not > 1 or
320- /// * the function `op` errors for all input
340+ /// * the function `op` errors for all input and safe is false
321341pub ( crate ) fn strings_to_primitive_function < O , F , F2 > (
322342 args : & [ ColumnarValue ] ,
323343 op : F ,
324344 op2 : F2 ,
325345 name : & str ,
346+ safe : bool ,
326347) -> Result < PrimitiveArray < O > >
327348where
328349 O : ArrowPrimitiveType ,
@@ -346,6 +367,7 @@ where
346367 & args[ 1 ..] ,
347368 op,
348369 op2,
370+ safe,
349371 )
350372 }
351373 DataType :: LargeUtf8 => {
@@ -355,6 +377,7 @@ where
355377 & args[ 1 ..] ,
356378 op,
357379 op2,
380+ safe,
358381 )
359382 }
360383 DataType :: Utf8 => {
@@ -364,6 +387,7 @@ where
364387 & args[ 1 ..] ,
365388 op,
366389 op2,
390+ safe,
367391 )
368392 }
369393 other => exec_err ! (
@@ -383,6 +407,7 @@ fn handle_array_op<'a, O, V, F, F2>(
383407 args : & [ ColumnarValue ] ,
384408 op : F ,
385409 op2 : F2 ,
410+ safe : bool ,
386411) -> Result < PrimitiveArray < O > >
387412where
388413 V : StringArrayType < ' a > ,
@@ -422,6 +447,7 @@ where
422447 } ;
423448
424449 val. transpose ( )
450+ . or_else ( |e| if safe { Ok ( None ) } else { Err ( e) } )
425451 } )
426452 . collect ( )
427453}
@@ -432,16 +458,24 @@ where
432458/// # Errors
433459/// This function errors iff:
434460/// * the number of arguments is not 1 or
435- /// * the function `op` errors
461+ /// * the function `op` errors and safe is false
436462fn unary_string_to_primitive_function < ' a , StringArrType , O , F > (
437463 array : StringArrType ,
438464 op : F ,
465+ safe : bool ,
439466) -> Result < PrimitiveArray < O > >
440467where
441468 StringArrType : StringArrayType < ' a > ,
442469 O : ArrowPrimitiveType ,
443470 F : Fn ( & ' a str ) -> Result < O :: Native > ,
444471{
445472 // first map is the iterator, second is for the `Option<_>`
446- array. iter ( ) . map ( |x| x. map ( & op) . transpose ( ) ) . collect ( )
473+ array
474+ . iter ( )
475+ . map ( |x| {
476+ x. map ( & op)
477+ . transpose ( )
478+ . or_else ( |e| if safe { Ok ( None ) } else { Err ( e) } )
479+ } )
480+ . collect ( )
447481}
0 commit comments