@@ -309,6 +309,142 @@ macro_rules! make_math_unary_udf {
309309 } ;
310310}
311311
312+ /// Macro to create a unary logarithm UDF that rejects negative inputs.
313+ ///
314+ /// A unary logarithm function takes an argument of type Float32 or Float64,
315+ /// applies a unary floating function to the argument, and returns a value of the same type.
316+ ///
317+ /// $UDF: the name of the UDF struct that implements `ScalarUDFImpl`
318+ /// $NAME: the name of the function
319+ /// $UNARY_FUNC: the unary function to apply to the argument
320+ /// $OUTPUT_ORDERING: the output ordering calculation method of the function
321+ /// $GET_DOC: the function to get the documentation of the UDF
322+ macro_rules! make_checked_log_unary_udf {
323+ ( $UDF: ident, $NAME: ident, $UNARY_FUNC: ident, $OUTPUT_ORDERING: expr, $EVALUATE_BOUNDS: expr, $GET_DOC: expr) => {
324+ $crate:: make_udf_function!( $NAME:: $UDF, $NAME) ;
325+
326+ mod $NAME {
327+
328+ use std:: sync:: Arc ;
329+
330+ use arrow:: array:: { Array , ArrayRef , AsArray , Float32Array , Float64Array } ;
331+ use arrow:: datatypes:: { DataType , Float32Type , Float64Type } ;
332+ use arrow:: error:: ArrowError ;
333+ use datafusion_common:: { Result , exec_err} ;
334+ use datafusion_expr:: interval_arithmetic:: Interval ;
335+ use datafusion_expr:: sort_properties:: { ExprProperties , SortProperties } ;
336+ use datafusion_expr:: {
337+ ColumnarValue , Documentation , ScalarFunctionArgs , ScalarUDFImpl ,
338+ Signature , Volatility ,
339+ } ;
340+
341+ #[ derive( Debug , PartialEq , Eq , Hash ) ]
342+ pub struct $UDF {
343+ signature: Signature ,
344+ }
345+
346+ impl $UDF {
347+ pub fn new( ) -> Self {
348+ use DataType :: * ;
349+ Self {
350+ signature: Signature :: uniform(
351+ 1 ,
352+ vec![ Float64 , Float32 ] ,
353+ Volatility :: Immutable ,
354+ ) ,
355+ }
356+ }
357+ }
358+
359+ impl ScalarUDFImpl for $UDF {
360+ fn name( & self ) -> & str {
361+ stringify!( $NAME)
362+ }
363+
364+ fn signature( & self ) -> & Signature {
365+ & self . signature
366+ }
367+
368+ fn return_type( & self , arg_types: & [ DataType ] ) -> Result <DataType > {
369+ let arg_type = & arg_types[ 0 ] ;
370+
371+ match arg_type {
372+ DataType :: Float32 => Ok ( DataType :: Float32 ) ,
373+ // For other types (possible values float64/null/int), use Float64
374+ _ => Ok ( DataType :: Float64 ) ,
375+ }
376+ }
377+
378+ fn output_ordering(
379+ & self ,
380+ input: & [ ExprProperties ] ,
381+ ) -> Result <SortProperties > {
382+ $OUTPUT_ORDERING( input)
383+ }
384+
385+ fn evaluate_bounds( & self , inputs: & [ & Interval ] ) -> Result <Interval > {
386+ $EVALUATE_BOUNDS( inputs)
387+ }
388+
389+ fn invoke_with_args(
390+ & self ,
391+ args: ScalarFunctionArgs ,
392+ ) -> Result <ColumnarValue > {
393+ let args = ColumnarValue :: values_to_arrays( & args. args) ?;
394+ let arr: ArrayRef = match args[ 0 ] . data_type( ) {
395+ DataType :: Float64 => {
396+ let result: Float64Array = args[ 0 ]
397+ . as_primitive:: <Float64Type >( )
398+ . try_unary( checked_log_f64) ?;
399+ Arc :: new( result) as ArrayRef
400+ }
401+ DataType :: Float32 => {
402+ let result: Float32Array = args[ 0 ]
403+ . as_primitive:: <Float32Type >( )
404+ . try_unary( checked_log_f32) ?;
405+ Arc :: new( result) as ArrayRef
406+ }
407+ other => {
408+ return exec_err!(
409+ "Unsupported data type {other:?} for function {}" ,
410+ self . name( )
411+ ) ;
412+ }
413+ } ;
414+
415+ Ok ( ColumnarValue :: Array ( arr) )
416+ }
417+
418+ fn documentation( & self ) -> Option <& Documentation > {
419+ Some ( $GET_DOC( ) )
420+ }
421+ }
422+
423+ fn checked_log_f64( value: f64 ) -> Result <f64 , ArrowError > {
424+ if value < 0.0 {
425+ Err ( negative_log_error( ) )
426+ } else {
427+ Ok ( f64 :: $UNARY_FUNC( value) )
428+ }
429+ }
430+
431+ fn checked_log_f32( value: f32 ) -> Result <f32 , ArrowError > {
432+ if value < 0.0 {
433+ Err ( negative_log_error( ) )
434+ } else {
435+ Ok ( f32 :: $UNARY_FUNC( value) )
436+ }
437+ }
438+
439+ fn negative_log_error( ) -> ArrowError {
440+ ArrowError :: ComputeError (
441+ "Cannot take logarithm of a negative number" . to_string( ) ,
442+ )
443+ }
444+ }
445+ } ;
446+ }
447+
312448/// Macro to create a binary math UDF.
313449///
314450/// A binary math function takes two arguments of types Float32 or Float64,
0 commit comments