-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Further refactoring of type coercion function code #19603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,11 +21,11 @@ use crate::expr::{ | |
| InSubquery, Placeholder, ScalarFunction, TryCast, Unnest, WindowFunction, | ||
| WindowFunctionParams, | ||
| }; | ||
| use crate::type_coercion::functions::fields_with_udf; | ||
| use crate::type_coercion::functions::{UDFCoercionExt, 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}; | ||
| use arrow::datatypes::{DataType, Field, FieldRef}; | ||
| use datafusion_common::datatype::FieldExt; | ||
| use datafusion_common::metadata::FieldMetadata; | ||
| use datafusion_common::{ | ||
|
|
@@ -152,43 +152,10 @@ impl ExprSchemable for Expr { | |
| } | ||
| } | ||
| } | ||
| Expr::ScalarFunction(_func) => { | ||
| let return_type = self.to_field(schema)?.1.data_type().clone(); | ||
| Ok(return_type) | ||
| } | ||
| Expr::WindowFunction(window_function) => self | ||
| .data_type_and_nullable_with_window_function(schema, window_function) | ||
| .map(|(return_type, _)| return_type), | ||
| Expr::AggregateFunction(AggregateFunction { | ||
| func, | ||
| params: AggregateFunctionParams { args, .. }, | ||
| }) => { | ||
| let fields = args | ||
| .iter() | ||
| .map(|e| e.to_field(schema).map(|(_, f)| f)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| let new_fields = fields_with_udf(&fields, func.as_ref()) | ||
| .map_err(|err| { | ||
| let data_types = fields | ||
| .iter() | ||
| .map(|f| f.data_type().clone()) | ||
| .collect::<Vec<_>>(); | ||
| plan_datafusion_err!( | ||
| "{} {}", | ||
| match err { | ||
| DataFusionError::Plan(msg) => msg, | ||
| err => err.to_string(), | ||
| }, | ||
| utils::generate_signature_error_msg( | ||
| func.name(), | ||
| func.signature().clone(), | ||
| &data_types | ||
| ) | ||
| ) | ||
| })? | ||
| .into_iter() | ||
| .collect::<Vec<_>>(); | ||
| Ok(func.return_field(&new_fields)?.data_type().clone()) | ||
| Expr::ScalarFunction(_) | ||
| | Expr::WindowFunction(_) | ||
| | Expr::AggregateFunction(_) => { | ||
| Ok(self.to_field(schema)?.1.data_type().clone()) | ||
|
Comment on lines
+155
to
+158
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reusing existing logic from |
||
| } | ||
| Expr::Not(_) | ||
| | Expr::IsNull(_) | ||
|
|
@@ -348,21 +315,9 @@ impl ExprSchemable for Expr { | |
| } | ||
| } | ||
| Expr::Cast(Cast { expr, .. }) => expr.nullable(input_schema), | ||
| Expr::ScalarFunction(_func) => { | ||
| let field = self.to_field(input_schema)?.1; | ||
|
|
||
| let nullable = field.is_nullable(); | ||
| Ok(nullable) | ||
| } | ||
| Expr::AggregateFunction(AggregateFunction { func, .. }) => { | ||
| Ok(func.is_nullable()) | ||
| } | ||
| Expr::WindowFunction(window_function) => self | ||
| .data_type_and_nullable_with_window_function( | ||
| input_schema, | ||
| window_function, | ||
| ) | ||
| .map(|(_, nullable)| nullable), | ||
| Expr::ScalarFunction(_) | ||
| | Expr::AggregateFunction(_) | ||
| | Expr::WindowFunction(_) => Ok(self.to_field(input_schema)?.1.is_nullable()), | ||
| Expr::ScalarVariable(field, _) => Ok(field.is_nullable()), | ||
| Expr::TryCast { .. } | Expr::Unnest(_) | Expr::Placeholder(_) => Ok(true), | ||
| Expr::IsNull(_) | ||
|
|
@@ -534,73 +489,49 @@ impl ExprSchemable for Expr { | |
| ))) | ||
| } | ||
| Expr::WindowFunction(window_function) => { | ||
| let (dt, nullable) = self.data_type_and_nullable_with_window_function( | ||
| schema, | ||
| window_function, | ||
| )?; | ||
| Ok(Arc::new(Field::new(&schema_name, dt, nullable))) | ||
| } | ||
| Expr::AggregateFunction(aggregate_function) => { | ||
| let AggregateFunction { | ||
| func, | ||
| params: AggregateFunctionParams { args, .. }, | ||
| let WindowFunction { | ||
| fun, | ||
| params: WindowFunctionParams { args, .. }, | ||
| .. | ||
| } = aggregate_function; | ||
| } = window_function.as_ref(); | ||
|
|
||
| let fields = args | ||
| .iter() | ||
| .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_udf(&fields, func.as_ref()) | ||
| .map_err(|err| { | ||
| let arg_types = fields | ||
| .iter() | ||
| .map(|f| f.data_type()) | ||
| .cloned() | ||
| .collect::<Vec<_>>(); | ||
| plan_datafusion_err!( | ||
| "{} {}", | ||
| match err { | ||
| DataFusionError::Plan(msg) => msg, | ||
| err => err.to_string(), | ||
| }, | ||
| utils::generate_signature_error_msg( | ||
| func.name(), | ||
| func.signature().clone(), | ||
| &arg_types, | ||
| ) | ||
| ) | ||
| })? | ||
| .into_iter() | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| match fun { | ||
| WindowFunctionDefinition::AggregateUDF(udaf) => { | ||
| let new_fields = | ||
| verify_function_arguments(udaf.as_ref(), &fields)?; | ||
| let return_field = udaf.return_field(&new_fields)?; | ||
| Ok(return_field) | ||
| } | ||
| WindowFunctionDefinition::WindowUDF(udwf) => { | ||
| let new_fields = | ||
| verify_function_arguments(udwf.as_ref(), &fields)?; | ||
| let return_field = udwf | ||
| .field(WindowUDFFieldArgs::new(&new_fields, &schema_name))?; | ||
| Ok(return_field) | ||
| } | ||
| } | ||
| } | ||
| Expr::AggregateFunction(AggregateFunction { | ||
| func, | ||
| params: AggregateFunctionParams { args, .. }, | ||
| }) => { | ||
| let fields = args | ||
| .iter() | ||
| .map(|e| e.to_field(schema).map(|(_, f)| f)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| let new_fields = verify_function_arguments(func.as_ref(), &fields)?; | ||
| func.return_field(&new_fields) | ||
| } | ||
| Expr::ScalarFunction(ScalarFunction { func, args }) => { | ||
| let (arg_types, fields): (Vec<DataType>, Vec<Arc<Field>>) = args | ||
| let fields = args | ||
| .iter() | ||
| .map(|e| e.to_field(schema).map(|(_, f)| f)) | ||
| .collect::<Result<Vec<_>>>()? | ||
| .into_iter() | ||
| .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_fields = | ||
| fields_with_udf(&fields, func.as_ref()).map_err(|err| { | ||
| plan_datafusion_err!( | ||
| "{} {}", | ||
| match err { | ||
| DataFusionError::Plan(msg) => msg, | ||
| err => err.to_string(), | ||
| }, | ||
| utils::generate_signature_error_msg( | ||
| func.name(), | ||
| func.signature().clone(), | ||
| &arg_types, | ||
| ) | ||
| ) | ||
| })?; | ||
| .collect::<Result<Vec<_>>>()?; | ||
| let new_fields = verify_function_arguments(func.as_ref(), &fields)?; | ||
|
|
||
| let arguments = args | ||
| .iter() | ||
|
|
@@ -678,6 +609,33 @@ impl ExprSchemable for Expr { | |
| } | ||
| } | ||
|
|
||
| /// Verify that function is invoked with correct number and type of arguments as | ||
| /// defined in `TypeSignature`. | ||
| fn verify_function_arguments<F: UDFCoercionExt>( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the new |
||
| function: &F, | ||
| input_fields: &[FieldRef], | ||
| ) -> Result<Vec<FieldRef>> { | ||
| fields_with_udf(input_fields, function).map_err(|err| { | ||
| let data_types = input_fields | ||
| .iter() | ||
| .map(|f| f.data_type()) | ||
| .cloned() | ||
| .collect::<Vec<_>>(); | ||
| plan_datafusion_err!( | ||
| "{} {}", | ||
| match err { | ||
| DataFusionError::Plan(msg) => msg, | ||
| err => err.to_string(), | ||
| }, | ||
| utils::generate_signature_error_message( | ||
| function.name(), | ||
| function.signature(), | ||
| &data_types | ||
| ) | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| /// Returns the innermost [Expr] that is provably null if `expr` is null. | ||
| fn unwrap_certainly_null_expr(expr: &Expr) -> &Expr { | ||
| match expr { | ||
|
|
@@ -688,93 +646,6 @@ fn unwrap_certainly_null_expr(expr: &Expr) -> &Expr { | |
| } | ||
| } | ||
|
|
||
| impl Expr { | ||
| /// Common method for window functions that applies type coercion | ||
| /// to all arguments of the window function to check if it matches | ||
| /// its signature. | ||
| /// | ||
| /// If successful, this method returns the data type and | ||
| /// nullability of the window function's result. | ||
| /// | ||
| /// Otherwise, returns an error if there's a type mismatch between | ||
| /// the window function's signature and the provided arguments. | ||
| fn data_type_and_nullable_with_window_function( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has been inlined to |
||
| &self, | ||
| schema: &dyn ExprSchema, | ||
| window_function: &WindowFunction, | ||
| ) -> Result<(DataType, bool)> { | ||
| let WindowFunction { | ||
| fun, | ||
| params: WindowFunctionParams { args, .. }, | ||
| .. | ||
| } = window_function; | ||
|
|
||
| let fields = args | ||
| .iter() | ||
| .map(|e| e.to_field(schema).map(|(_, f)| f)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| match fun { | ||
| WindowFunctionDefinition::AggregateUDF(udaf) => { | ||
| let data_types = fields | ||
| .iter() | ||
| .map(|f| f.data_type()) | ||
| .cloned() | ||
| .collect::<Vec<_>>(); | ||
| let new_fields = fields_with_udf(&fields, udaf.as_ref()) | ||
| .map_err(|err| { | ||
| plan_datafusion_err!( | ||
| "{} {}", | ||
| match err { | ||
| DataFusionError::Plan(msg) => msg, | ||
| err => err.to_string(), | ||
| }, | ||
| utils::generate_signature_error_msg( | ||
| fun.name(), | ||
| fun.signature(), | ||
| &data_types | ||
| ) | ||
| ) | ||
| })? | ||
| .into_iter() | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let return_field = udaf.return_field(&new_fields)?; | ||
|
|
||
| Ok((return_field.data_type().clone(), return_field.is_nullable())) | ||
| } | ||
| WindowFunctionDefinition::WindowUDF(udwf) => { | ||
| let data_types = fields | ||
| .iter() | ||
| .map(|f| f.data_type()) | ||
| .cloned() | ||
| .collect::<Vec<_>>(); | ||
| let new_fields = fields_with_udf(&fields, udwf.as_ref()) | ||
| .map_err(|err| { | ||
| plan_datafusion_err!( | ||
| "{} {}", | ||
| match err { | ||
| DataFusionError::Plan(msg) => msg, | ||
| err => err.to_string(), | ||
| }, | ||
| utils::generate_signature_error_msg( | ||
| fun.name(), | ||
| fun.signature(), | ||
| &data_types | ||
| ) | ||
| ) | ||
| })? | ||
| .into_iter() | ||
| .collect::<Vec<_>>(); | ||
| let (_, function_name) = self.qualified_name(); | ||
| let field_args = WindowUDFFieldArgs::new(&new_fields, &function_name); | ||
|
|
||
| udwf.field(field_args) | ||
| .map(|field| (field.data_type().clone(), field.is_nullable())) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Cast subquery in InSubquery/ScalarSubquery to a given type. | ||
| /// | ||
| /// 1. **Projection plan**: If the subquery is a projection (i.e. a SELECT statement with specific | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sample UDF declared it accepted f64's but internally it acted as if input was i64; this was never caught because apparently type coercion wasn't being run on non-aggregate window functions, and the input data was already i64. Nice little fix.