-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Align scalar UDF literal args with coerced return fields #23232
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
Open
yinli-systems
wants to merge
6
commits into
apache:main
Choose a base branch
from
yinli-systems:kevin/return-field-scalar-arg-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a008e6
Align scalar UDF return-field literal args
053b1c0
Limit scalar literal coercion to implicit casts
d121bfc
Merge remote-tracking branch 'upstream/main' into kevin/return-field-…
yinli-systems 955c4e0
fix: align scalar UDF literals with coerced fields
yinli-systems 6e56abb
test: address scalar argument review feedback
yinli-systems 1f00d84
Merge remote-tracking branch 'upstream/main' into kevin/return-field-…
yinli-systems File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -745,8 +745,11 @@ impl TreeNodeRewriter for TypeCoercionRewriter<'_> { | |
| Ok(Transformed::yes(Expr::Case(case))) | ||
| } | ||
| Expr::ScalarFunction(ScalarFunction { func, args }) => { | ||
| let new_expr = | ||
| coerce_arguments_for_signature(args, self.schema, func.as_ref())?; | ||
| let new_expr = coerce_scalar_function_arguments_for_signature( | ||
| args, | ||
| self.schema, | ||
| func.as_ref(), | ||
| )?; | ||
| Ok(Transformed::yes(Expr::ScalarFunction( | ||
| ScalarFunction::new_udf(func, new_expr), | ||
| ))) | ||
|
|
@@ -1065,23 +1068,80 @@ fn coerce_arguments_for_signature<F: UDFCoercionExt>( | |
| schema: &DFSchema, | ||
| func: &F, | ||
| ) -> Result<Vec<Expr>> { | ||
| let current_fields = expressions | ||
| .iter() | ||
| .map(|e| e.to_field(schema).map(|(_, f)| f)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| let coerced_types = coerced_argument_types(&expressions, schema, func)?; | ||
|
|
||
| let coerced_types = fields_with_udf(¤t_fields, func)? | ||
| expressions | ||
| .into_iter() | ||
| .map(|f| f.data_type().clone()) | ||
| .collect::<Vec<_>>(); | ||
| .zip(coerced_types) | ||
| .map(|(expr, data_type)| expr.cast_to(&data_type, schema)) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Coerces scalar function arguments while materializing successful implicit | ||
| /// casts of literals. This preserves the literal for subsequent calls to | ||
| /// `return_field_from_args` without treating user-written `Cast` or `TryCast` | ||
| /// expressions as scalar arguments. | ||
| fn coerce_scalar_function_arguments_for_signature<F: UDFCoercionExt>( | ||
| expressions: Vec<Expr>, | ||
| schema: &DFSchema, | ||
| func: &F, | ||
| ) -> Result<Vec<Expr>> { | ||
| let coerced_types = coerced_argument_types(&expressions, schema, func)?; | ||
|
|
||
| expressions | ||
| .into_iter() | ||
| .enumerate() | ||
| .map(|(i, expr)| expr.cast_to(&coerced_types[i], schema)) | ||
| .zip(coerced_types) | ||
| .map(|(expr, data_type)| { | ||
| coerce_scalar_function_argument(expr, &data_type, schema) | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| fn coerced_argument_types<F: UDFCoercionExt>( | ||
| expressions: &[Expr], | ||
| schema: &DFSchema, | ||
| func: &F, | ||
| ) -> Result<Vec<DataType>> { | ||
| let current_fields = expressions | ||
| .iter() | ||
| .map(|e| e.to_field(schema).map(|(_, f)| f)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
|
|
||
| fields_with_udf(¤t_fields, func).map(|fields| { | ||
| fields | ||
| .into_iter() | ||
| .map(|field| field.data_type().clone()) | ||
| .collect() | ||
| }) | ||
| } | ||
|
|
||
| fn coerce_scalar_function_argument( | ||
| expr: Expr, | ||
| data_type: &DataType, | ||
| schema: &DFSchema, | ||
| ) -> Result<Expr> { | ||
| if matches!(&expr, Expr::Cast(_) | Expr::TryCast(_)) | ||
| && expr.get_type(schema)? == *data_type | ||
| { | ||
| return Ok(expr); | ||
| } | ||
|
|
||
| let Expr::Literal(value, metadata) = expr else { | ||
| return expr.cast_to(data_type, schema); | ||
| }; | ||
|
|
||
| if value.data_type() != *data_type | ||
| && let Ok(value) = value.cast_to(data_type) | ||
| { | ||
| return Ok(Expr::Literal(value, metadata)); | ||
| } | ||
|
|
||
| // A failed value cast remains an expression cast so execution produces the | ||
| // same error as before. Since it is no longer a literal at the coerced type, | ||
| // it is reported as `None` in `ReturnFieldArgs::scalar_arguments`. | ||
| Expr::Literal(value, metadata).cast_to(data_type, schema) | ||
|
Comment on lines
+1139
to
+1142
Contributor
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. is this to maintain backwards compatibility? |
||
| } | ||
|
|
||
| fn coerce_case_expression(case: Case, schema: &DFSchema) -> Result<Case> { | ||
| // Given expressions like: | ||
| // | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 suppose this is an easier way to ensure proper type without a wider refactor 🤔