Skip to content

Apache DataFusion SQL Query Engine

Refactor duplicate code in `type_coercion/functions.rs` (#19518)

Jeffrey Vo0db668bc9915
diff --git a/datafusion/core/tests/fuzz_cases/window_fuzz.rs b/datafusion/core/tests/fuzz_cases/window_fuzz.rs
index 2ecfcd84ab..1212c081eb 100644
--- a/datafusion/core/tests/fuzz_cases/window_fuzz.rs
+++ b/datafusion/core/tests/fuzz_cases/window_fuzz.rs
@@ -35,7 +35,7 @@ use datafusion::prelude::{SessionConfig, SessionContext};
use datafusion_common::HashMap;
use datafusion_common::{Result, ScalarValue};
use datafusion_common_runtime::SpawnedTask;
-use datafusion_expr::type_coercion::functions::fields_with_aggregate_udf;
+use datafusion_expr::type_coercion::functions::fields_with_udf;
use datafusion_expr::{
WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
};
@@ -451,7 +451,7 @@ fn get_random_function(
// Do type coercion first argument
let a = args[0].clone();
let dt = a.return_field(schema.as_ref()).unwrap();
- let coerced = fields_with_aggregate_udf(&[dt], udf).unwrap();
+ let coerced = fields_with_udf(&[dt], udf.as_ref()).unwrap();
args[0] = cast(a, schema, coerced[0].data_type().clone()).unwrap();
}
diff --git a/datafusion/expr/src/expr_schema.rs b/datafusion/expr/src/expr_schema.rs
index 691a8c508f..dbba0f2914 100644
--- a/datafusion/expr/src/expr_schema.rs
+++ b/datafusion/expr/src/expr_schema.rs
@@ -21,13 +21,11 @@ use crate::expr::{
InSubquery, Placeholder, ScalarFunction, TryCast, Unnest, WindowFunction,
WindowFunctionParams,
};
-use crate::type_coercion::functions::{
- data_types_with_scalar_udf, fields_with_aggregate_udf, fields_with_window_udf,
-};
+use crate::type_coercion::functions::fields_with_udf;
use crate::udf::ReturnFieldArgs;
use crate::{LogicalPlan, Projection, Subquery, WindowFunctionDefinition, utils};
use arrow::compute::can_cast_types;
-use arrow::datatypes::{DataType, Field, FieldRef};
+use arrow::datatypes::{DataType, Field};
use datafusion_common::datatype::FieldExt;
use datafusion_common::metadata::FieldMetadata;
use datafusion_common::{
@@ -169,7 +167,7 @@ impl ExprSchemable for Expr {
.iter()
.map(|e| e.to_field(schema).map(|(_, f)| f))
.collect::<Result<Vec<_>>>()?;
- let new_fields = fields_with_aggregate_udf(&fields, func)
+ let new_fields = fields_with_udf(&fields, func.as_ref())
.map_err(|err| {
let data_types = fields
.iter()
@@ -554,7 +552,7 @@ impl ExprSchemable for Expr {
.map(|e| e.to_field(schema).map(|(_, f)| f))
.collect::<Result<Vec<_>>>()?;
// Verify that function is invoked with correct number and type of arguments as defined in `TypeSignature`
- let new_fields = fields_with_aggregate_udf(&fields, func)
+ let new_fields = fields_with_udf(&fields, func.as_ref())
.map_err(|err| {
let arg_types = fields
.iter()
@@ -588,8 +586,8 @@ impl ExprSchemable for Expr {
.map(|f| (f.data_type().clone(), f))
.unzip();
// Verify that function is invoked with correct number and type of arguments as defined in `TypeSignature`
- let new_data_types = data_types_with_scalar_udf(&arg_types, func)
- .map_err(|err| {
+ let new_fields =
+ fields_with_udf(&fields, func.as_ref()).map_err(|err| {
plan_datafusion_err!(
"{} {}",
match err {
@@ -603,11 +601,6 @@ impl ExprSchemable for Expr {
)
)
})?;
- let new_fields = fields
- .into_iter()
- .zip(new_data_types)
- .map(|(f, d)| f.retyped(d))
- .collect::<Vec<FieldRef>>();
let arguments = args
.iter()
@@ -727,7 +720,7 @@ impl Expr {
.map(|f| f.data_type())
.cloned()
.collect::<Vec<_>>();
- let new_fields = fields_with_aggregate_udf(&fields, udaf)
+ let new_fields = fields_with_udf(&fields, udaf.as_ref())
.map_err(|err| {
plan_datafusion_err!(
"{} {}",
@@ -755,7 +748,7 @@ impl Expr {
.map(|f| f.data_type())
.cloned()
.collect::<Vec<_>>();
- let new_fields = fields_with_window_udf(&fields, udwf)
+ let new_fields = fields_with_udf(&fields, udwf.as_ref())
.map_err(|err| {
plan_datafusion_err!(
"{} {}",
@@ -828,6 +821,7 @@ mod tests {
use super::*;
use crate::{and, col, lit, not, or, out_ref_col_with_metadata, when};
+ use arrow::datatypes::FieldRef;
use datafusion_common::{DFSchema, ScalarValue, assert_or_internal_err};
macro_rules! test_is_expr_nullable {
diff --git a/datafusion/expr/src/type_coercion/functions.rs b/datafusion/expr/src/type_coercion/functions.rs
index 7be9713f53..e1f2a19672 100644
--- a/datafusion/expr/src/type_coercion/functions.rs
+++ b/datafusion/expr/src/type_coercion/functions.rs
@@ -17,7 +17,7 @@
use super::binary::binary_numeric_coercion;
use crate::{AggregateUDF, ScalarUDF, Signature, TypeSignature, WindowUDF};
-use arrow::datatypes::FieldRef;
+use arrow::datatypes::{Field, FieldRef};
use arrow::{
compute::can_cast_types,
datatypes::{DataType, TimeUnit},
@@ -39,6 +39,61 @@ use datafusion_expr_common::{
use itertools::Itertools as _;
use std::sync::Arc;
+/// Extension trait to unify common functionality between [`ScalarUDF`], [`AggregateUDF`]
+/// and [`WindowUDF`] for use by signature coercion functions.
+pub trait UDFCoercionExt {
+ /// Should delegate to [`ScalarUDF::name`], [`AggregateUDF::name`] or [`WindowUDF::name`].
+ fn name(&self) -> &str;
+ /// Should delegate to [`ScalarUDF::signature`], [`AggregateUDF::signature`]
+ /// or [`WindowUDF::signature`].
+ fn signature(&self) -> &Signature;
+ /// Should delegate to [`ScalarUDF::coerce_types`], [`AggregateUDF::coerce_types`]
+ /// or [`WindowUDF::coerce_types`].
+ fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>>;
+}
+
+impl UDFCoercionExt for ScalarUDF {
+ fn name(&self) -> &str {
+ self.name()
+ }
+
+ fn signature(&self) -> &Signature {
+ self.signature()
+ }
+
+ fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+ self.coerce_types(arg_types)
+ }
+}
+
+impl UDFCoercionExt for AggregateUDF {
+ fn name(&self) -> &str {
+ self.name()
+ }
+
+ fn signature(&self) -> &Signature {
+ self.signature()
+ }
+
+ fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+ self.coerce_types(arg_types)
+ }
+}
+
+impl UDFCoercionExt for WindowUDF {
+ fn name(&self) -> &str {
+ self.name()
+ }
+
+ fn signature(&self) -> &Signature {
+ self.signature()
+ }
+
+ fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+ self.coerce_types(arg_types)
+ }
+}
+
/// Performs type coercion for scalar function arguments.
///
/// Returns the data types to which each argument must be coerced to
@@ -46,38 +101,19 @@ use std::sync::Arc;
///
/// 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_with_scalar_udf(
current_types: &[DataType],
func: &ScalarUDF,
) -> Result<Vec<DataType>> {
- let signature = func.signature();
- let type_signature = &signature.type_signature;
-
- if current_types.is_empty() && type_signature != &TypeSignature::UserDefined {
- if type_signature.supports_zero_argument() {
- return Ok(vec![]);
- } else if type_signature.used_to_support_zero_arguments() {
- // Special error to help during upgrade: https://github.com/apache/datafusion/issues/13763
- return plan_err!(
- "'{}' does not support zero arguments. Use TypeSignature::Nullary for zero arguments",
- func.name()
- );
- } else {
- return plan_err!("'{}' does not support zero arguments", func.name());
- }
- }
-
- let valid_types =
- get_valid_types_with_scalar_udf(type_signature, current_types, func)?;
-
- if valid_types
+ let current_fields = current_types
.iter()
- .any(|data_type| data_type == current_types)
- {
- return Ok(current_types.to_vec());
- }
-
- try_coerce_types(func.name(), valid_types, current_types, type_signature)
+ .map(|dt| Arc::new(Field::new("f", dt.clone(), true)))
+ .collect::<Vec<_>>();
+ Ok(fields_with_udf(&current_fields, func)?
+ .iter()
+ .map(|f| f.data_type().clone())
+ .collect())
}
/// Performs type coercion for aggregate function arguments.
@@ -87,52 +123,12 @@ pub fn data_types_with_scalar_udf(
///
/// 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 fields_with_aggregate_udf(
current_fields: &[FieldRef],
func: &AggregateUDF,
) -> Result<Vec<FieldRef>> {
- let signature = func.signature();
- let type_signature = &signature.type_signature;
-
- if current_fields.is_empty() && type_signature != &TypeSignature::UserDefined {
- if type_signature.supports_zero_argument() {
- return Ok(vec![]);
- } else if type_signature.used_to_support_zero_arguments() {
- // Special error to help during upgrade: https://github.com/apache/datafusion/issues/13763
- return plan_err!(
- "'{}' does not support zero arguments. Use TypeSignature::Nullary for zero arguments",
- func.name()
- );
- } else {
- return plan_err!("'{}' does not support zero arguments", func.name());
- }
- }
- let current_types = current_fields
- .iter()
- .map(|f| f.data_type())
- .cloned()
- .collect::<Vec<_>>();
-
- let valid_types =
- get_valid_types_with_aggregate_udf(type_signature, &current_types, func)?;
- if valid_types
- .iter()
- .any(|data_type| data_type == &current_types)
- {
- return Ok(current_fields.to_vec());
- }
-
- let updated_types =
- try_coerce_types(func.name(), valid_types, &current_types, type_signature)?;
-
- Ok(current_fields
- .iter()
- .zip(updated_types)
- .map(|(current_field, new_type)| {
- current_field.as_ref().clone().with_data_type(new_type)
- })
- .map(Arc::new)
- .collect())
+ fields_with_udf(current_fields, func)
}
/// Performs type coercion for window function arguments.
@@ -142,9 +138,24 @@ pub fn fields_with_aggregate_udf(
///
/// 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 fields_with_window_udf(
current_fields: &[FieldRef],
func: &WindowUDF,
+) -> Result<Vec<FieldRef>> {
+ fields_with_udf(current_fields, func)
+}
+
+/// Performs type coercion for UDF arguments.
+///
+/// Returns the data types to which each argument must be coerced to
+/// match `signature`.
+///
+/// For more details on coercion in general, please see the
+/// [`type_coercion`](crate::type_coercion) module.
+pub fn fields_with_udf<F: UDFCoercionExt>(
+ current_fields: &[FieldRef],
+ func: &F,
) -> Result<Vec<FieldRef>> {
let signature = func.signature();
let type_signature = &signature.type_signature;
@@ -162,14 +173,13 @@ pub fn fields_with_window_udf(
return plan_err!("'{}' does not support zero arguments", func.name());
}
}
-
let current_types = current_fields
.iter()
.map(|f| f.data_type())
.cloned()
.collect::<Vec<_>>();
- let valid_types =
- get_valid_types_with_window_udf(type_signature, &current_types, func)?;
+
+ let valid_types = get_valid_types_with_udf(type_signature, &current_types, func)?;
if valid_types
.iter()
.any(|data_type| data_type == &current_types)
@@ -197,6 +207,7 @@ pub fn fields_with_window_udf(
///
/// 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(
function_name: impl AsRef<str>,
current_types: &[DataType],
@@ -239,20 +250,23 @@ pub fn data_types(
}
fn is_well_supported_signature(type_signature: &TypeSignature) -> bool {
- if let TypeSignature::OneOf(signatures) = type_signature {
- return signatures.iter().all(is_well_supported_signature);
- }
-
- matches!(
- type_signature,
+ match type_signature {
+ TypeSignature::OneOf(type_signatures) => {
+ type_signatures.iter().all(is_well_supported_signature)
+ }
TypeSignature::UserDefined
- | TypeSignature::Numeric(_)
- | TypeSignature::String(_)
- | TypeSignature::Coercible(_)
- | TypeSignature::Any(_)
- | TypeSignature::Nullary
- | TypeSignature::Comparable(_)
- )
+ | TypeSignature::Numeric(_)
+ | TypeSignature::String(_)
+ | TypeSignature::Coercible(_)
+ | TypeSignature::Any(_)
+ | TypeSignature::Nullary
+ | TypeSignature::Comparable(_) => true,
+ TypeSignature::Variadic(_)
+ | TypeSignature::VariadicAny
+ | TypeSignature::Uniform(_, _)
+ | TypeSignature::Exact(_)
+ | TypeSignature::ArraySignature(_) => false,
+ }
}
fn try_coerce_types(
@@ -293,25 +307,27 @@ fn try_coerce_types(
)
}
-fn get_valid_types_with_scalar_udf(
+fn get_valid_types_with_udf<F: UDFCoercionExt>(
signature: &TypeSignature,
current_types: &[DataType],
- func: &ScalarUDF,
+ func: &F,
) -> Result<Vec<Vec<DataType>>> {
- match signature {
+ let valid_types = match signature {
TypeSignature::UserDefined => match func.coerce_types(current_types) {
- Ok(coerced_types) => Ok(vec![coerced_types]),
- Err(e) => exec_err!(
- "Function '{}' user-defined coercion failed with {:?}",
- func.name(),
- e.strip_backtrace()
- ),
+ Ok(coerced_types) => vec![coerced_types],
+ Err(e) => {
+ return exec_err!(
+ "Function '{}' user-defined coercion failed with {:?}",
+ func.name(),
+ e.strip_backtrace()
+ );
+ }
},
TypeSignature::OneOf(signatures) => {
let mut res = vec![];
let mut errors = vec![];
for sig in signatures {
- match get_valid_types_with_scalar_udf(sig, current_types, func) {
+ match get_valid_types_with_udf(sig, current_types, func) {
Ok(valid_types) => {
res.extend(valid_types);
}
@@ -323,69 +339,15 @@ fn get_valid_types_with_scalar_udf(
// Every signature failed, return the joined error
if res.is_empty() {
- internal_err!(
+ return internal_err!(
"Function '{}' failed to match any signature, errors: {}",
func.name(),
errors.join(",")
- )
+ );
} else {
- Ok(res)
+ res
}
}
- _ => get_valid_types(func.name(), signature, current_types),
- }
-}
-
-fn get_valid_types_with_aggregate_udf(
- signature: &TypeSignature,
- current_types: &[DataType],
- func: &AggregateUDF,
-) -> Result<Vec<Vec<DataType>>> {
- let valid_types = match signature {
- TypeSignature::UserDefined => match func.coerce_types(current_types) {
- Ok(coerced_types) => vec![coerced_types],
- Err(e) => {
- return exec_err!(
- "Function '{}' user-defined coercion failed with {:?}",
- func.name(),
- e.strip_backtrace()
- );
- }
- },
- TypeSignature::OneOf(signatures) => signatures
- .iter()
- .filter_map(|t| {
- get_valid_types_with_aggregate_udf(t, current_types, func).ok()
- })
- .flatten()
- .collect::<Vec<_>>(),
- _ => get_valid_types(func.name(), signature, current_types)?,
- };
-
- Ok(valid_types)
-}
-
-fn get_valid_types_with_window_udf(
- signature: &TypeSignature,
- current_types: &[DataType],
- func: &WindowUDF,
-) -> Result<Vec<Vec<DataType>>> {
- let valid_types = match signature {
- TypeSignature::UserDefined => match func.coerce_types(current_types) {
- Ok(coerced_types) => vec![coerced_types],
- Err(e) => {
- return exec_err!(
- "Function '{}' user-defined coercion failed with {:?}",
- func.name(),
- e.strip_backtrace()
- );
- }
- },
- TypeSignature::OneOf(signatures) => signatures
- .iter()
- .filter_map(|t| get_valid_types_with_window_udf(t, current_types, func).ok())
- .flatten()
- .collect::<Vec<_>>(),
_ => get_valid_types(func.name(), signature, current_types)?,
};
@@ -1158,10 +1120,27 @@ mod tests {
#[test]
fn test_fixed_list_wildcard_coerce() -> Result<()> {
+ struct MockUdf(Signature);
+
+ impl UDFCoercionExt for MockUdf {
+ fn name(&self) -> &str {
+ "test"
+ }
+ fn signature(&self) -> &Signature {
+ &self.0
+ }
+ fn coerce_types(&self, _arg_types: &[DataType]) -> Result<Vec<DataType>> {
+ unimplemented!()
+ }
+ }
+
let inner = Arc::new(Field::new_list_field(DataType::Int32, false));
- let current_types = vec![
- DataType::FixedSizeList(Arc::clone(&inner), 2), // able to coerce for any size
- ];
+ // able to coerce for any size
+ let current_fields = vec![Arc::new(Field::new(
+ "t",
+ DataType::FixedSizeList(Arc::clone(&inner), 2),
+ true,
+ ))];
let signature = Signature::exact(
vec![DataType::FixedSizeList(
@@ -1171,24 +1150,25 @@ mod tests {
Volatility::Stable,
);
- let coerced_data_types = data_types("test", &current_types, &signature)?;
- assert_eq!(coerced_data_types, current_types);
+ let coerced_fields = fields_with_udf(&current_fields, &MockUdf(signature))?;
+ assert_eq!(coerced_fields, current_fields);
// make sure it can't coerce to a different size
let signature = Signature::exact(
vec![DataType::FixedSizeList(Arc::clone(&inner), 3)],
Volatility::Stable,
);
- let coerced_data_types = data_types("test", &current_types, &signature);
- assert!(coerced_data_types.is_err());
+ let coerced_fields = fields_with_udf(&current_fields, &MockUdf(signature));
+ assert!(coerced_fields.is_err());
// make sure it works with the same type.
let signature = Signature::exact(
vec![DataType::FixedSizeList(Arc::clone(&inner), 2)],
Volatility::Stable,
);
- let coerced_data_types = data_types("test", &current_types, &signature).unwrap();
- assert_eq!(coerced_data_types, current_types);
+ let coerced_fields =
+ fields_with_udf(&current_fields, &MockUdf(signature)).unwrap();
+ assert_eq!(coerced_fields, current_fields);
Ok(())
}
diff --git a/datafusion/ffi/src/udaf/mod.rs b/datafusion/ffi/src/udaf/mod.rs
index c485c9a71b..22cbe8cff0 100644
--- a/datafusion/ffi/src/udaf/mod.rs
+++ b/datafusion/ffi/src/udaf/mod.rs
@@ -28,7 +28,7 @@ use arrow::ffi::FFI_ArrowSchema;
use arrow_schema::FieldRef;
use datafusion_common::{DataFusionError, Result, ffi_datafusion_err};
use datafusion_expr::function::AggregateFunctionSimplification;
-use datafusion_expr::type_coercion::functions::fields_with_aggregate_udf;
+use datafusion_expr::type_coercion::functions::fields_with_udf;
use datafusion_expr::{
Accumulator, AggregateUDF, AggregateUDFImpl, GroupsAccumulator, Signature,
};
@@ -340,7 +340,7 @@ unsafe extern "C" fn coerce_types_fn_wrapper(
.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))
+ let return_types = rresult_return!(fields_with_udf(&arg_fields, udaf.as_ref()))
.into_iter()
.map(|f| f.data_type().to_owned())
.collect::<Vec<_>>();
diff --git a/datafusion/ffi/src/udf/mod.rs b/datafusion/ffi/src/udf/mod.rs
index d7da050b35..94be5f38ea 100644
--- a/datafusion/ffi/src/udf/mod.rs
+++ b/datafusion/ffi/src/udf/mod.rs
@@ -28,7 +28,7 @@ use arrow::ffi::{FFI_ArrowSchema, from_ffi, to_ffi};
use arrow_schema::FieldRef;
use datafusion_common::config::ConfigOptions;
use datafusion_common::{DataFusionError, Result, internal_err};
-use datafusion_expr::type_coercion::functions::data_types_with_scalar_udf;
+use datafusion_expr::type_coercion::functions::fields_with_udf;
use datafusion_expr::{
ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl,
Signature,
@@ -140,8 +140,15 @@ unsafe extern "C" fn coerce_types_fn_wrapper(
) -> FFIResult<RVec<WrappedSchema>> {
let arg_types = rresult_return!(rvec_wrapped_to_vec_datatype(&arg_types));
+ let arg_fields = arg_types
+ .iter()
+ .map(|dt| Arc::new(Field::new("f", dt.clone(), true)))
+ .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<_>>();
rresult!(vec_datatype_to_rvec_wrapped(&return_types))
}
diff --git a/datafusion/ffi/src/udwf/mod.rs b/datafusion/ffi/src/udwf/mod.rs
index 53aa6c34eb..dbac00fd43 100644
--- a/datafusion/ffi/src/udwf/mod.rs
+++ b/datafusion/ffi/src/udwf/mod.rs
@@ -26,7 +26,7 @@ use arrow::datatypes::{DataType, Schema, SchemaRef};
use arrow_schema::{Field, FieldRef};
use datafusion_common::{Result, ffi_err};
use datafusion_expr::function::WindowUDFFieldArgs;
-use datafusion_expr::type_coercion::functions::fields_with_window_udf;
+use datafusion_expr::type_coercion::functions::fields_with_udf;
use datafusion_expr::{
LimitEffect, PartitionEvaluator, Signature, WindowUDF, WindowUDFImpl,
};
@@ -167,7 +167,7 @@ unsafe extern "C" fn coerce_types_fn_wrapper(
.map(Arc::new)
.collect::<Vec<_>>();
- let return_fields = rresult_return!(fields_with_window_udf(&arg_fields, inner));
+ let return_fields = rresult_return!(fields_with_udf(&arg_fields, inner.as_ref()));
let return_types = return_fields
.into_iter()
.map(|f| f.data_type().to_owned())
diff --git a/datafusion/optimizer/src/analyzer/type_coercion.rs b/datafusion/optimizer/src/analyzer/type_coercion.rs
index 0545ffb3b1..02395c76bd 100644
--- a/datafusion/optimizer/src/analyzer/type_coercion.rs
+++ b/datafusion/optimizer/src/analyzer/type_coercion.rs
@@ -42,9 +42,7 @@ use datafusion_expr::expr_rewriter::coerce_plan_expr_for_schema;
use datafusion_expr::expr_schema::cast_subquery;
use datafusion_expr::logical_plan::Subquery;
use datafusion_expr::type_coercion::binary::{comparison_coercion, like_coercion};
-use datafusion_expr::type_coercion::functions::{
- data_types_with_scalar_udf, fields_with_aggregate_udf,
-};
+use datafusion_expr::type_coercion::functions::fields_with_udf;
use datafusion_expr::type_coercion::other::{
get_coerce_type_for_case_expression, get_coerce_type_for_list,
};
@@ -928,17 +926,20 @@ fn coerce_arguments_for_signature_with_scalar_udf(
return Ok(expressions);
}
- let current_types = expressions
+ let current_fields = expressions
.iter()
- .map(|e| e.get_type(schema))
+ .map(|e| e.to_field(schema).map(|(_, f)| f))
.collect::<Result<Vec<_>>>()?;
- let new_types = data_types_with_scalar_udf(&current_types, func)?;
+ let coerced_types = fields_with_udf(&current_fields, func)?
+ .into_iter()
+ .map(|f| f.data_type().clone())
+ .collect::<Vec<_>>();
expressions
.into_iter()
.enumerate()
- .map(|(i, expr)| expr.cast_to(&new_types[i], schema))
+ .map(|(i, expr)| expr.cast_to(&coerced_types[i], schema))
.collect()
}
@@ -960,7 +961,7 @@ fn coerce_arguments_for_signature_with_aggregate_udf(
.map(|e| e.to_field(schema).map(|(_, f)| f))
.collect::<Result<Vec<_>>>()?;
- let new_types = fields_with_aggregate_udf(&current_fields, func)?
+ let coerced_types = fields_with_udf(&current_fields, func)?
.into_iter()
.map(|f| f.data_type().clone())
.collect::<Vec<_>>();
@@ -968,7 +969,7 @@ fn coerce_arguments_for_signature_with_aggregate_udf(
expressions
.into_iter()
.enumerate()
- .map(|(i, expr)| expr.cast_to(&new_types[i], schema))
+ .map(|(i, expr)| expr.cast_to(&coerced_types[i], schema))
.collect()
}
diff --git a/datafusion/physical-expr/src/scalar_function.rs b/datafusion/physical-expr/src/scalar_function.rs
index e6a6db75be..aa090743ad 100644
--- a/datafusion/physical-expr/src/scalar_function.rs
+++ b/datafusion/physical-expr/src/scalar_function.rs
@@ -43,7 +43,7 @@ use datafusion_common::config::{ConfigEntry, ConfigOptions};
use datafusion_common::{Result, ScalarValue, internal_err};
use datafusion_expr::interval_arithmetic::Interval;
use datafusion_expr::sort_properties::ExprProperties;
-use datafusion_expr::type_coercion::functions::data_types_with_scalar_udf;
+use datafusion_expr::type_coercion::functions::fields_with_udf;
use datafusion_expr::{
ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDF, Volatility,
expr_vec_fmt,
@@ -101,11 +101,7 @@ impl ScalarFunctionExpr {
.collect::<Result<Vec<_>>>()?;
// verify that input data types is consistent with function's `TypeSignature`
- let arg_types = arg_fields
- .iter()
- .map(|f| f.data_type().clone())
- .collect::<Vec<_>>();
- data_types_with_scalar_udf(&arg_types, &fun)?;
+ fields_with_udf(&arg_fields, fun.as_ref())?;
let arguments = args
.iter()
diff --git a/datafusion/sqllogictest/test_files/errors.slt b/datafusion/sqllogictest/test_files/errors.slt
index 22430774bb..20c1db5cb1 100644
--- a/datafusion/sqllogictest/test_files/errors.slt
+++ b/datafusion/sqllogictest/test_files/errors.slt
@@ -61,7 +61,7 @@ WITH t AS (WITH u as (SELECT 1) SELECT 1) SELECT * from u
# select_wildcard_without_table
statement error Error during planning: SELECT \* with no tables specified is not valid
-SELECT *
+SELECT *
# invalid_qualified_table_references
statement error Error during planning: table 'datafusion\.nonexistentschema\.aggregate_test_100' not found
@@ -125,7 +125,7 @@ from aggregate_test_100
order by c9
# 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
select
c9,
nth_value(c5, 2, 3) over (order by c9) as nv1