-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add field
trait method to WindowUDFImpl
#12374
base: main
Are you sure you want to change the base?
Conversation
I lack permission to apply the |
datafusion/expr/src/expr_schema.rs
Outdated
.iter() | ||
.map(|e| e.get_type(input_schema)) | ||
.collect::<Result<Vec<_>>>()?; | ||
let input_types = data_types_with_window_udf(&data_types, udwf)?; |
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.
I think we should handle this compute in to_field
. data_types_with_window_udf
could reuse to_field
. data_types
and nullable
could reuse data_types_with_window_udf
datafusion/expr/src/expr_schema.rs
Outdated
.map(|e| e.get_type(input_schema)) | ||
.collect::<Result<Vec<_>>>()?; | ||
let input_types = data_types_with_window_udf(&data_types, udwf)?; | ||
let function_name = self.schema_name().to_string(); |
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.
Similar to self.qualified_name()
in to_field
, we might have different name for Column and Alias?
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( | ||
&self, | ||
schema: &dyn ExprSchema, | ||
window_function: &WindowFunction, | ||
) -> Result<(DataType, bool)> { |
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.
Extracted a common method to handle type coercion for all window function types (built-in, udaf and udwf) which is then reused by methods:,
data_type_and_nullable
,get_type
and,nullable
/// Return the type of the function given its input types | ||
/// | ||
/// See [`WindowUDFImpl::return_type`] for more details. | ||
pub fn return_type(&self, args: &[DataType]) -> Result<DataType> { | ||
self.inner.return_type(args) | ||
} | ||
|
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.
Removed return_type
.
/// Returns if column values are nullable for this window function. | ||
/// Returns the field of the final result of evaluating this window function. | ||
/// | ||
/// See [`WindowUDFImpl::nullable`] for more details. | ||
pub fn nullable(&self) -> bool { | ||
self.inner.nullable() |
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.
Removed nullable
) | ||
) | ||
})?; | ||
let (_, function_name) = self.qualified_name(); |
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.
Use Expr::qualified_name
which also handles:
Expr::Column
and,Expr::Alias
WindowFunctionDefinition::WindowUDF(fun) => fun | ||
.field(WindowUDFFieldArgs::new(input_expr_types, display_name)) | ||
.map(|field| field.data_type().clone()), |
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.
Return data type for udwf.
Which issue does this PR close?
Closes #12373.
Rationale for this change
The result field from evaluating the user-defined window function is composed from the
return_type
andnullable
trait methods inWindowUDFImpl
.This change explores folding both methods into a single trait method. The user-defined window functions have to implement only the
field
trait method which makes the intent more explicit.The current implementation for a user-defined window function (without field trait method) looks like this:
The implementation for a user-defined window function after this change:
What changes are included in this PR?
field
trait method:return_type
trait method.datafusion/datafusion/expr/src/udwf.rs
Lines 282 to 284 in a08f923
nullable
trait method which was added in Convert built-inrow_number
to user-defined window function #12030.WindowUDFFieldArgs
:Are these changes tested?
Yes, against existing tests in CI.
Are there any user-facing changes?
Yes, this is a breaking change for user-defined window functions API.