jcsherin commented on code in PR #12374:
URL: https://github.com/apache/datafusion/pull/12374#discussion_r1763245501
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -499,6 +455,76 @@ impl ExprSchemable for 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(
+ &self,
+ schema: &dyn ExprSchema,
+ window_function: &WindowFunction,
+ ) -> Result<(DataType, bool)> {
+ let WindowFunction { fun, args, .. } = window_function;
+
+ let data_types = args
+ .iter()
+ .map(|e| e.get_type(schema))
+ .collect::<Result<Vec<_>>>()?;
+ match fun {
+ WindowFunctionDefinition::BuiltInWindowFunction(window_fun) => {
+ let return_type = window_fun.return_type(&data_types)?;
+ let nullable =
+ !["RANK", "NTILE",
"CUME_DIST"].contains(&window_fun.name());
+ Ok((return_type, nullable))
+ }
+ WindowFunctionDefinition::AggregateUDF(udaf) => {
+ let new_types = data_types_with_aggregate_udf(&data_types,
udaf)
+ .map_err(|err| {
+ plan_datafusion_err!(
+ "{} {}",
+ err,
+ utils::generate_signature_error_msg(
+ fun.name(),
+ fun.signature(),
+ &data_types
+ )
+ )
+ })?;
+
+ let return_type = udaf.return_type(&new_types)?;
+ let nullable = udaf.is_nullable();
+
+ Ok((return_type, nullable))
+ }
+ WindowFunctionDefinition::WindowUDF(udwf) => {
+ let new_types =
+ data_types_with_window_udf(&data_types,
udwf).map_err(|err| {
+ plan_datafusion_err!(
+ "{} {}",
+ err,
+ utils::generate_signature_error_msg(
+ fun.name(),
+ fun.signature(),
+ &data_types
+ )
+ )
+ })?;
+ let (_, function_name) = self.qualified_name();
Review Comment:
Use `Expr::qualified_name` which also handles:
- `Expr::Column` and,
- `Expr::Alias`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]