Skip to content

Commit 0db668b

Browse files
Jefffreycomphead
andauthored
Refactor duplicate code in type_coercion/functions.rs (#19518)
## 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. --> - 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. --> 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? <!-- 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. --> Yes, deprecating functions. <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Co-authored-by: Oleks V <[email protected]>
1 parent 818706a commit 0db668b

File tree

9 files changed

+180
-202
lines changed

9 files changed

+180
-202
lines changed

datafusion/core/tests/fuzz_cases/window_fuzz.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use datafusion::prelude::{SessionConfig, SessionContext};
3535
use datafusion_common::HashMap;
3636
use datafusion_common::{Result, ScalarValue};
3737
use datafusion_common_runtime::SpawnedTask;
38-
use datafusion_expr::type_coercion::functions::fields_with_aggregate_udf;
38+
use datafusion_expr::type_coercion::functions::fields_with_udf;
3939
use datafusion_expr::{
4040
WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
4141
};
@@ -451,7 +451,7 @@ fn get_random_function(
451451
// Do type coercion first argument
452452
let a = args[0].clone();
453453
let dt = a.return_field(schema.as_ref()).unwrap();
454-
let coerced = fields_with_aggregate_udf(&[dt], udf).unwrap();
454+
let coerced = fields_with_udf(&[dt], udf.as_ref()).unwrap();
455455
args[0] = cast(a, schema, coerced[0].data_type().clone()).unwrap();
456456
}
457457

datafusion/expr/src/expr_schema.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ use crate::expr::{
2121
InSubquery, Placeholder, ScalarFunction, TryCast, Unnest, WindowFunction,
2222
WindowFunctionParams,
2323
};
24-
use crate::type_coercion::functions::{
25-
data_types_with_scalar_udf, fields_with_aggregate_udf, fields_with_window_udf,
26-
};
24+
use crate::type_coercion::functions::fields_with_udf;
2725
use crate::udf::ReturnFieldArgs;
2826
use crate::{LogicalPlan, Projection, Subquery, WindowFunctionDefinition, utils};
2927
use arrow::compute::can_cast_types;
30-
use arrow::datatypes::{DataType, Field, FieldRef};
28+
use arrow::datatypes::{DataType, Field};
3129
use datafusion_common::datatype::FieldExt;
3230
use datafusion_common::metadata::FieldMetadata;
3331
use datafusion_common::{
@@ -169,7 +167,7 @@ impl ExprSchemable for Expr {
169167
.iter()
170168
.map(|e| e.to_field(schema).map(|(_, f)| f))
171169
.collect::<Result<Vec<_>>>()?;
172-
let new_fields = fields_with_aggregate_udf(&fields, func)
170+
let new_fields = fields_with_udf(&fields, func.as_ref())
173171
.map_err(|err| {
174172
let data_types = fields
175173
.iter()
@@ -554,7 +552,7 @@ impl ExprSchemable for Expr {
554552
.map(|e| e.to_field(schema).map(|(_, f)| f))
555553
.collect::<Result<Vec<_>>>()?;
556554
// Verify that function is invoked with correct number and type of arguments as defined in `TypeSignature`
557-
let new_fields = fields_with_aggregate_udf(&fields, func)
555+
let new_fields = fields_with_udf(&fields, func.as_ref())
558556
.map_err(|err| {
559557
let arg_types = fields
560558
.iter()
@@ -588,8 +586,8 @@ impl ExprSchemable for Expr {
588586
.map(|f| (f.data_type().clone(), f))
589587
.unzip();
590588
// Verify that function is invoked with correct number and type of arguments as defined in `TypeSignature`
591-
let new_data_types = data_types_with_scalar_udf(&arg_types, func)
592-
.map_err(|err| {
589+
let new_fields =
590+
fields_with_udf(&fields, func.as_ref()).map_err(|err| {
593591
plan_datafusion_err!(
594592
"{} {}",
595593
match err {
@@ -603,11 +601,6 @@ impl ExprSchemable for Expr {
603601
)
604602
)
605603
})?;
606-
let new_fields = fields
607-
.into_iter()
608-
.zip(new_data_types)
609-
.map(|(f, d)| f.retyped(d))
610-
.collect::<Vec<FieldRef>>();
611604

612605
let arguments = args
613606
.iter()
@@ -727,7 +720,7 @@ impl Expr {
727720
.map(|f| f.data_type())
728721
.cloned()
729722
.collect::<Vec<_>>();
730-
let new_fields = fields_with_aggregate_udf(&fields, udaf)
723+
let new_fields = fields_with_udf(&fields, udaf.as_ref())
731724
.map_err(|err| {
732725
plan_datafusion_err!(
733726
"{} {}",
@@ -755,7 +748,7 @@ impl Expr {
755748
.map(|f| f.data_type())
756749
.cloned()
757750
.collect::<Vec<_>>();
758-
let new_fields = fields_with_window_udf(&fields, udwf)
751+
let new_fields = fields_with_udf(&fields, udwf.as_ref())
759752
.map_err(|err| {
760753
plan_datafusion_err!(
761754
"{} {}",
@@ -828,6 +821,7 @@ mod tests {
828821
use super::*;
829822
use crate::{and, col, lit, not, or, out_ref_col_with_metadata, when};
830823

824+
use arrow::datatypes::FieldRef;
831825
use datafusion_common::{DFSchema, ScalarValue, assert_or_internal_err};
832826

833827
macro_rules! test_is_expr_nullable {

0 commit comments

Comments
 (0)