@@ -203,10 +203,7 @@ impl ScalarUDF {
203
203
self . inner . simplify ( args, info)
204
204
}
205
205
206
- /// Invoke the function on `args`, returning the appropriate result.
207
- ///
208
- /// See [`ScalarUDFImpl::invoke`] for more details.
209
- #[ deprecated( since = "42.1.0" , note = "Use `invoke_batch` instead" ) ]
206
+ #[ deprecated( since = "42.1.0" , note = "Use `invoke_with_args` instead" ) ]
210
207
pub fn invoke ( & self , args : & [ ColumnarValue ] ) -> Result < ColumnarValue > {
211
208
#[ allow( deprecated) ]
212
209
self . inner . invoke ( args)
@@ -216,20 +213,27 @@ impl ScalarUDF {
216
213
self . inner . is_nullable ( args, schema)
217
214
}
218
215
219
- /// Invoke the function with `args` and number of rows, returning the appropriate result.
220
- ///
221
- /// See [`ScalarUDFImpl::invoke_batch`] for more details.
216
+ #[ deprecated( since = "43.0.0" , note = "Use `invoke_with_args` instead" ) ]
222
217
pub fn invoke_batch (
223
218
& self ,
224
219
args : & [ ColumnarValue ] ,
225
220
number_rows : usize ,
226
221
) -> Result < ColumnarValue > {
222
+ #[ allow( deprecated) ]
227
223
self . inner . invoke_batch ( args, number_rows)
228
224
}
229
225
226
+ /// Invoke the function on `args`, returning the appropriate result.
227
+ ///
228
+ /// See [`ScalarUDFImpl::invoke_with_args`] for details.
229
+ pub fn invoke_with_args ( & self , args : ScalarFunctionArgs ) -> Result < ColumnarValue > {
230
+ self . inner . invoke_with_args ( args)
231
+ }
232
+
230
233
/// Invoke the function without `args` but number of rows, returning the appropriate result.
231
234
///
232
- /// See [`ScalarUDFImpl::invoke_no_args`] for more details.
235
+ /// Note: This method is deprecated and will be removed in future releases.
236
+ /// User defined functions should implement [`Self::invoke_with_args`] instead.
233
237
#[ deprecated( since = "42.1.0" , note = "Use `invoke_batch` instead" ) ]
234
238
pub fn invoke_no_args ( & self , number_rows : usize ) -> Result < ColumnarValue > {
235
239
#[ allow( deprecated) ]
@@ -324,26 +328,37 @@ where
324
328
}
325
329
}
326
330
327
- /// Trait for implementing [`ScalarUDF`].
331
+ pub struct ScalarFunctionArgs < ' a > {
332
+ // The evaluated arguments to the function
333
+ pub args : & ' a [ ColumnarValue ] ,
334
+ // The number of rows in record batch being evaluated
335
+ pub number_rows : usize ,
336
+ // The return type of the scalar function returned (from `return_type` or `return_type_from_exprs`)
337
+ // when creating the physical expression from the logical expression
338
+ pub return_type : & ' a DataType ,
339
+ }
340
+
341
+ /// Trait for implementing user defined scalar functions.
328
342
///
329
343
/// This trait exposes the full API for implementing user defined functions and
330
344
/// can be used to implement any function.
331
345
///
332
346
/// See [`advanced_udf.rs`] for a full example with complete implementation and
333
347
/// [`ScalarUDF`] for other available options.
334
348
///
335
- ///
336
349
/// [`advanced_udf.rs`]: https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/advanced_udf.rs
350
+ ///
337
351
/// # Basic Example
338
352
/// ```
339
353
/// # use std::any::Any;
340
354
/// # use std::sync::OnceLock;
341
355
/// # use arrow::datatypes::DataType;
342
356
/// # use datafusion_common::{DataFusionError, plan_err, Result};
343
- /// # use datafusion_expr::{col, ColumnarValue, Documentation, Signature, Volatility};
357
+ /// # use datafusion_expr::{col, ColumnarValue, Documentation, ScalarFunctionArgs, Signature, Volatility};
344
358
/// # use datafusion_expr::{ScalarUDFImpl, ScalarUDF};
345
359
/// # use datafusion_expr::scalar_doc_sections::DOC_SECTION_MATH;
346
360
///
361
+ /// /// This struct for a simple UDF that adds one to an int32
347
362
/// #[derive(Debug)]
348
363
/// struct AddOne {
349
364
/// signature: Signature,
@@ -356,7 +371,7 @@ where
356
371
/// }
357
372
/// }
358
373
/// }
359
- ///
374
+ ///
360
375
/// static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
361
376
///
362
377
/// fn get_doc() -> &'static Documentation {
@@ -383,7 +398,9 @@ where
383
398
/// Ok(DataType::Int32)
384
399
/// }
385
400
/// // The actual implementation would add one to the argument
386
- /// fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { unimplemented!() }
401
+ /// fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
402
+ /// unimplemented!()
403
+ /// }
387
404
/// fn documentation(&self) -> Option<&Documentation> {
388
405
/// Some(get_doc())
389
406
/// }
@@ -479,24 +496,9 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
479
496
480
497
/// Invoke the function on `args`, returning the appropriate result
481
498
///
482
- /// The function will be invoked passed with the slice of [`ColumnarValue`]
483
- /// (either scalar or array).
484
- ///
485
- /// If the function does not take any arguments, please use [invoke_no_args]
486
- /// instead and return [not_impl_err] for this function.
487
- ///
488
- ///
489
- /// # Performance
490
- ///
491
- /// For the best performance, the implementations of `invoke` should handle
492
- /// the common case when one or more of their arguments are constant values
493
- /// (aka [`ColumnarValue::Scalar`]).
494
- ///
495
- /// [`ColumnarValue::values_to_arrays`] can be used to convert the arguments
496
- /// to arrays, which will likely be simpler code, but be slower.
497
- ///
498
- /// [invoke_no_args]: ScalarUDFImpl::invoke_no_args
499
- #[ deprecated( since = "42.1.0" , note = "Use `invoke_batch` instead" ) ]
499
+ /// Note: This method is deprecated and will be removed in future releases.
500
+ /// User defined functions should implement [`Self::invoke_with_args`] instead.
501
+ #[ deprecated( since = "42.1.0" , note = "Use `invoke_with_args` instead" ) ]
500
502
fn invoke ( & self , _args : & [ ColumnarValue ] ) -> Result < ColumnarValue > {
501
503
not_impl_err ! (
502
504
"Function {} does not implement invoke but called" ,
@@ -507,17 +509,12 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
507
509
/// Invoke the function with `args` and the number of rows,
508
510
/// returning the appropriate result.
509
511
///
510
- /// The function will be invoked with the slice of [`ColumnarValue`]
511
- /// (either scalar or array).
512
- ///
513
- /// # Performance
512
+ /// Note: See notes on [`Self::invoke_with_args`]
514
513
///
515
- /// For the best performance, the implementations should handle the common case
516
- /// when one or more of their arguments are constant values (aka
517
- /// [`ColumnarValue::Scalar`]).
514
+ /// Note: This method is deprecated and will be removed in future releases.
515
+ /// User defined functions should implement [`Self::invoke_with_args`] instead.
518
516
///
519
- /// [`ColumnarValue::values_to_arrays`] can be used to convert the arguments
520
- /// to arrays, which will likely be simpler code, but be slower.
517
+ /// See <https://github.com/apache/datafusion/issues/13515> for more details.
521
518
fn invoke_batch (
522
519
& self ,
523
520
args : & [ ColumnarValue ] ,
@@ -537,9 +534,27 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
537
534
}
538
535
}
539
536
537
+ /// Invoke the function returning the appropriate result.
538
+ ///
539
+ /// # Performance
540
+ ///
541
+ /// For the best performance, the implementations should handle the common case
542
+ /// when one or more of their arguments are constant values (aka
543
+ /// [`ColumnarValue::Scalar`]).
544
+ ///
545
+ /// [`ColumnarValue::values_to_arrays`] can be used to convert the arguments
546
+ /// to arrays, which will likely be simpler code, but be slower.
547
+ fn invoke_with_args ( & self , args : ScalarFunctionArgs ) -> Result < ColumnarValue > {
548
+ #[ allow( deprecated) ]
549
+ self . invoke_batch ( args. args , args. number_rows )
550
+ }
551
+
540
552
/// Invoke the function without `args`, instead the number of rows are provided,
541
553
/// returning the appropriate result.
542
- #[ deprecated( since = "42.1.0" , note = "Use `invoke_batch` instead" ) ]
554
+ ///
555
+ /// Note: This method is deprecated and will be removed in future releases.
556
+ /// User defined functions should implement [`Self::invoke_with_args`] instead.
557
+ #[ deprecated( since = "42.1.0" , note = "Use `invoke_with_args` instead" ) ]
543
558
fn invoke_no_args ( & self , _number_rows : usize ) -> Result < ColumnarValue > {
544
559
not_impl_err ! (
545
560
"Function {} does not implement invoke_no_args but called" ,
@@ -767,6 +782,7 @@ impl ScalarUDFImpl for AliasedScalarUDFImpl {
767
782
args : & [ ColumnarValue ] ,
768
783
number_rows : usize ,
769
784
) -> Result < ColumnarValue > {
785
+ #[ allow( deprecated) ]
770
786
self . inner . invoke_batch ( args, number_rows)
771
787
}
772
788
0 commit comments