[release/6.0] Query: Match joined tables properly when lifting for group by aggregate #27170
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.
Earlier we only matched alias and skipped if alias matched. This could happen if the table name starts with same character.
Even if we do deeper match unwinding joins, we cannot match if the join is to subquery (which can be generated when target of navigation has query filter).
Solution:
When applying group by on SelectExpression, remember the original table count. Once Groupby has been applied we cannot add more joins to SelectExpression other than group by aggregate term lifting.
During lifting:
Resolves #27163
Description
After group by operator, when there are multiple aggregate terms in projection containing navigation expansion, when alias of joined tables match, then we generate invalid SQL if column referenced doesn't exist on other table which we didn't lift or incorrect result if column reference exists.
Customer impact
Customers will have their query throwing invalid SQL error or giving incorrect results.
How found
Customer reported on 6.0.1
Regression
No. The feature was introduced in 6.0 release only.
Testing
Added testing for user scenario and complex scenario in similar pattern.
Risk
Low risk. Logic added for lifting is pretty conservative. Also added quirk to revert to previous behavior.