[SPARK-55500][SQL] Fix analyzer cycle between ApplyDefaultCollation, ExtractWindowExpressions and CollationTypeCasts
#54284
+94
−2
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.
What changes were proposed in this pull request?
Call
CollationTypeCastsimmediately to avoid cycle betweenApplyDefaultCollation,ExtractWindowExpressionsandCollationTypeCasts.Example:
Analyzer runs rules in batches sequentially, and the rule order is:
ApplyDefaultCollation->ExtractWindowExpressions->CollationTypeCasts.Iteration 1:
ApplyDefaultCollationappliesUTF8_LCASEcollation to the literal'HELLO'. ExpressionEqualTo(c1 = 'HELLO') is not resolved after this because c1 and the literal have different types.ExtractWindowExpressionstries to extract the window expressions, but it can't because it expects the whole projectList to be resolved (bothEqualToandROW_NUMBER()).EqualTois not resolved, so it can't apply the rule.CollationTypeCastsapplies coercion rules, changing the type of the literal to the defaultStringTypeagain (because that's the type of the column). NowEqualTois resolved.Iteration 2:
ApplyDefaultCollationagain appliesUTF8_LCASEcollation to the literal, because its type is defaultStringTypeagain.ExtractWindowExpressionsagain can't extract the window expressions for the same reason as before.CollationTypeCastsapplies the same coercion rules again, changing the type of the literal back to the defaultStringType.This cycle continues, and the plan never gets resolved. By calling
CollationTypeCastsright after this rule, we ensure that other rules likeExtractWindowExpressionsthat expect resolved expressions can be applied.Why are the changes needed?
Bug fix.
Does this PR introduce any user-facing change?
No.
How was this patch tested?
New tests.
Was this patch authored or co-authored using generative AI tooling?
No.