[SPARK-47561][SQL] Fix analyzer rule order issues about Alias #45718
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?
We found two analyzer rule execution order issues in our internal workloads:
CreateStruct.applycreatesNamePlaceholderfor unresolvedNamedExpression. However, with certain rule execution order, theNamedExpressionmay be removed (e.g. remove unnecessaryAlias) beforeNamePlaceholderis resolved, thenNamePlaceholdercan't be resolved anymore.UnresolvedAliasto wrapUnresolvedAttribute. There is a conflict about how to determine the final alias name. IfResolveAliasesruns first, thenUnresolvedAliaswill be removed and eventually the alias will bebfor nested columna.b. IfResolveReferencesruns first, then we resolvea.bfirst and thenUnresolvedAliaswill determine the alias asa.bnotb.This PR fixes the two issues
CreateStruct.applyshould determine the field name immediately if the input isAliasUnresolvedAttributedirectly without theUnresolvedAliaswrapper. It's a bit risky to fix the order issue betweenResolveAliasesandResolveReferencesas it can change the final query schema, we will save it for later.Why are the changes needed?
fix unstable analyzer behavior with different rule execution orders.
Does this PR introduce any user-facing change?
Yes, some failed queries can run now. The issue for UNPIVOT only affects the error message.
How was this patch tested?
verified by our internal workloads. The repro query is quite complicated to trigger a certain rule execution order so we won't add tests for it. The fix is quite obvious.
Was this patch authored or co-authored using generative AI tooling?
no