Skip to content

Refactor duplicate code in type_coercion/functions.rs - #19518

Merged
Jefffrey merged 7 commits into
apache:mainfrom
Jefffrey:refactor-type-coercion
Jan 1, 2026
Merged

Refactor duplicate code in type_coercion/functions.rs#19518
Jefffrey merged 7 commits into
apache:mainfrom
Jefffrey:refactor-type-coercion

Conversation

@Jefffrey

@Jefffrey Jefffrey commented Dec 28, 2025

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Found lots of code duplicated here, so unifying them.

What changes are included in this PR?

Introduce new trait UDFCoercionExt to unify functions across scalar/aggregate/window UDFs for use in a single generic implementation. New unified functions fields_with_udf and get_valid_types_with_udf which are generic across this new trait.

Are these changes tested?

Existing tests.

Are there any user-facing changes?

Yes, deprecating functions.

@github-actions github-actions Bot added logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) ffi Changes to the ffi crate labels Dec 28, 2025
Comment on lines +143 to +152
let arg_fields = arg_types
.iter()
.map(|dt| Field::new("f", dt.clone(), true))
.map(Arc::new)
.collect::<Vec<_>>();
let return_types =
rresult_return!(data_types_with_scalar_udf(&arg_types, udf.inner()));
rresult_return!(fields_with_udf(&arg_fields, udf.inner().as_ref()))
.into_iter()
.map(|f| f.data_type().to_owned())
.collect::<Vec<_>>();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as how UDAF handles this:

unsafe extern "C" fn coerce_types_fn_wrapper(
udaf: &FFI_AggregateUDF,
arg_types: RVec<WrappedSchema>,
) -> FFIResult<RVec<WrappedSchema>> {
unsafe {
let udaf = udaf.inner();
let arg_types = rresult_return!(rvec_wrapped_to_vec_datatype(&arg_types));
let arg_fields = arg_types
.iter()
.map(|dt| Field::new("f", dt.clone(), true))
.map(Arc::new)
.collect::<Vec<_>>();
let return_types = rresult_return!(fields_with_aggregate_udf(&arg_fields, udaf))
.into_iter()
.map(|f| f.data_type().to_owned())
.collect::<Vec<_>>();
rresult!(vec_datatype_to_rvec_wrapped(&return_types))
}
}

///
/// For more details on coercion in general, please see the
/// [`type_coercion`](crate::type_coercion) module.
#[deprecated(since = "52.0.0", note = "use fields_with_udf")]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'm of the mind to just remove these functions completely

/// For more details on coercion in general, please see the
/// [`type_coercion`](crate::type_coercion) module.
#[deprecated(since = "52.0.0", note = "use fields_with_udf")]
pub fn data_types(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was only ever used in unit tests below 🤔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if its pub it still might be used elsewhere beyond DF engine


matches!(
type_signature,
match type_signature {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using match here makes it more obvious which variants are not well supported

);
}
},
TypeSignature::OneOf(signatures) => signatures

@Jefffrey Jefffrey Dec 28, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scalar fn version actually has extra functionality in that it collects the errors together for oneof; unifying this code with aggregate/window udfs brings in this functionality for them, hence SLT fix was needed

@github-actions github-actions Bot added the core Core DataFusion crate label Dec 28, 2025
@Jefffrey
Jefffrey marked this pull request as ready for review December 28, 2025 10:50
Comment thread datafusion/ffi/src/udf/mod.rs Outdated
}

let current_types = expressions
let current_fields = expressions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread datafusion/optimizer/src/analyzer/type_coercion.rs Outdated
}

impl UDFCoercionExt for ScalarUDF {
fn name(&self) -> &str {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its bad the specialization feature is in nightly releases, so we could get rid of those parts

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Jefffrey


# WindowFunction wrong signature
statement error DataFusion error: Error during planning: Failed to coerce arguments to satisfy a call to 'nth_value' function: coercion from Int32, Int64, Int64 to the signature OneOf\(\[Any\(0\), Any\(1\), Any\(2\)\]\) failed
statement error DataFusion error: Error during planning: Internal error: Function 'nth_value' failed to match any signature

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really an Internal error ?
It is an error caused by the user's input.

@Jefffrey Jefffrey Dec 31, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I aim to tackle in #19004

// Every signature failed, return the joined error
if res.is_empty() {
internal_err!(
return internal_err!(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a plan_err!()

@Jefffrey Jefffrey Dec 31, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I aim to tackle in #19004

@Jefffrey
Jefffrey added this pull request to the merge queue Jan 1, 2026
Merged via the queue into apache:main with commit 0db668b Jan 1, 2026
32 checks passed
@Jefffrey
Jefffrey deleted the refactor-type-coercion branch January 1, 2026 15:31
@Jefffrey

Jefffrey commented Jan 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @comphead & @martin-g

github-merge-queue Bot pushed a commit that referenced this pull request Jan 28, 2026
## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes #123` indicates that this PR will close issue #123.
-->

- Follow up to #19518
- Initial work before tackling #19004

## Rationale for this change

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

Found lots of code duplicated here, so unifying them.

## What changes are included in this PR?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

- Reduce code duplication around handling scalar/aggregate/window UDFs
in type coercion related code, via usage of new `UDFCoercionExt` trait
which unifies some of their behaviours (introduced by #19518)
- Deprecate functions `can_coerce_from()` and
`generate_signature_error_msg()` to work towards minimizing our public
API surface
- Fix some UDF signatures which nested `UserDefined` within `OneOf`
- Fix bug where type coercion rewrites weren't being applied to
arguments of non-aggregate window UDFs

## Are these changes tested?

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

Existing tests.

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

Deprecated some functions.

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->

---------

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
gabotechs added a commit to DataDog/datafusion that referenced this pull request Feb 2, 2026
de-bgunter pushed a commit to de-bgunter/datafusion that referenced this pull request Mar 24, 2026
## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Follow up to apache#19518
- Initial work before tackling apache#19004

## Rationale for this change

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

Found lots of code duplicated here, so unifying them.

## What changes are included in this PR?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

- Reduce code duplication around handling scalar/aggregate/window UDFs
in type coercion related code, via usage of new `UDFCoercionExt` trait
which unifies some of their behaviours (introduced by apache#19518)
- Deprecate functions `can_coerce_from()` and
`generate_signature_error_msg()` to work towards minimizing our public
API surface
- Fix some UDF signatures which nested `UserDefined` within `OneOf`
- Fix bug where type coercion rewrites weren't being applied to
arguments of non-aggregate window UDFs

## Are these changes tested?

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

Existing tests.

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

Deprecated some functions.

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->

---------

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate ffi Changes to the ffi crate logical-expr Logical plan and expressions optimizer Optimizer rules physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants