Skip to content

Commit 1712064

Browse files
committed
inner functions
1 parent 366ac26 commit 1712064

1 file changed

Lines changed: 40 additions & 46 deletions

File tree

‎datafusion/expr/src/udf.rs‎

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,16 @@ pub trait ScalarUDFImpl: Debug + DynEq + DynHash + Send + Sync + Any {
543543
///
544544
/// See [`Expr::schema_name`] for details
545545
fn schema_name(&self, args: &[Expr]) -> Result<String> {
546-
udf_default_schema_name(self.name(), args)
546+
/// Use an inner function to avoid code duplication over all
547+
/// generic callsites as the body is the same.
548+
fn inner(name: &str, args: &[Expr]) -> Result<String> {
549+
Ok(format!(
550+
"{}({})",
551+
name,
552+
schema_name_from_exprs_comma_separated_without_space(args)?
553+
))
554+
}
555+
inner(self.name(), args)
547556
}
548557

549558
/// Returns a [`Signature`] describing the argument types for which this
@@ -656,6 +665,11 @@ pub trait ScalarUDFImpl: Debug + DynEq + DynHash + Send + Sync + Any {
656665
/// logical input even if the input is simplified (e.g. it must return the same
657666
/// value for `('foo' | 'bar')` as it does for ('foobar').
658667
fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
668+
/// Use an inner function to avoid code duplication over all
669+
/// generic callsites as the body is the same.
670+
fn arg_fields_to_data_types(arg_fields: &[FieldRef]) -> Vec<DataType> {
671+
arg_fields.iter().map(|f| f.data_type().clone()).collect()
672+
}
659673
let data_types = arg_fields_to_data_types(args.arg_fields);
660674
let return_type = self.return_type(&data_types)?;
661675
Ok(Arc::new(Field::new(self.name(), return_type, true)))
@@ -932,7 +946,25 @@ pub trait ScalarUDFImpl: Debug + DynEq + DynHash + Send + Sync + Any {
932946
if !self.preserves_lex_ordering(inputs)? {
933947
return Ok(SortProperties::Unordered);
934948
}
935-
Ok(common_sort_properties(inputs))
949+
950+
/// Use an inner function to avoid code duplication over all
951+
/// generic callsites as the body is the same.
952+
fn inner(inputs: &[ExprProperties]) -> SortProperties {
953+
let Some(first_order) = inputs.first().map(|p| &p.sort_properties) else {
954+
return SortProperties::Singleton;
955+
};
956+
957+
if inputs
958+
.iter()
959+
.skip(1)
960+
.all(|input| &input.sort_properties == first_order)
961+
{
962+
*first_order
963+
} else {
964+
SortProperties::Unordered
965+
}
966+
}
967+
Ok(inner(inputs))
936968
}
937969

938970
/// Returns true if the function preserves lexicographical ordering based on
@@ -973,7 +1005,12 @@ pub trait ScalarUDFImpl: Debug + DynEq + DynHash + Send + Sync + Any {
9731005
/// A Vec the same length as `arg_types`. DataFusion will `CAST` the function call
9741006
/// arguments to these specific types.
9751007
fn coerce_types(&self, _arg_types: &[DataType]) -> Result<Vec<DataType>> {
976-
coerce_types_not_implemented(self.name())
1008+
/// Use an inner function to avoid code duplication over all
1009+
/// generic callsites as the body is the same.
1010+
fn inner(name: &str) -> Result<Vec<DataType>> {
1011+
not_impl_err!("Function {name} does not implement coerce_types")
1012+
}
1013+
inner(self.name())
9771014
}
9781015

9791016
/// For struct-producing functions, return how output fields map to input
@@ -1018,49 +1055,6 @@ pub trait ScalarUDFImpl: Debug + DynEq + DynHash + Send + Sync + Any {
10181055
}
10191056
}
10201057

1021-
/// Default implementation of [`ScalarUDFImpl::coerce_types`].
1022-
/// Extracted to a free-standing function to reduce binary size by avoiding instantiating code per ScalarUDFImpl impl.
1023-
fn coerce_types_not_implemented(name: &str) -> Result<Vec<DataType>> {
1024-
not_impl_err!("Function {name} does not implement coerce_types")
1025-
}
1026-
1027-
/// Default implementation of [`ScalarUDFImpl::schema_name`]:
1028-
/// `name(arg1,arg2,..)`
1029-
/// Extracted to a free-standing function to reduce binary size by avoiding instantiating code per ScalarUDFImpl impl.
1030-
fn udf_default_schema_name(name: &str, args: &[Expr]) -> Result<String> {
1031-
Ok(format!(
1032-
"{}({})",
1033-
name,
1034-
schema_name_from_exprs_comma_separated_without_space(args)?
1035-
))
1036-
}
1037-
1038-
/// Collects the [`DataType`] of each field
1039-
/// Extracted to a free-standing function to reduce binary size by avoiding instantiating code per ScalarUDFImpl impl.
1040-
fn arg_fields_to_data_types(arg_fields: &[FieldRef]) -> Vec<DataType> {
1041-
arg_fields.iter().map(|f| f.data_type().clone()).collect()
1042-
}
1043-
1044-
/// Returns the [`SortProperties`] shared by all `inputs`, or
1045-
/// [`SortProperties::Unordered`] if they differ. Used by the default
1046-
/// implementation of [`ScalarUDFImpl::output_ordering`].
1047-
/// Extracted to a free-standing function to reduce binary size by avoiding instantiating code per ScalarUDFImpl impl.
1048-
fn common_sort_properties(inputs: &[ExprProperties]) -> SortProperties {
1049-
let Some(first_order) = inputs.first().map(|p| &p.sort_properties) else {
1050-
return SortProperties::Singleton;
1051-
};
1052-
1053-
if inputs
1054-
.iter()
1055-
.skip(1)
1056-
.all(|input| &input.sort_properties == first_order)
1057-
{
1058-
*first_order
1059-
} else {
1060-
SortProperties::Unordered
1061-
}
1062-
}
1063-
10641058
impl dyn ScalarUDFImpl {
10651059
/// Returns `true` if the implementation is of type `T`.
10661060
///

0 commit comments

Comments
 (0)