Skip to content
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

[release/6.0] Query: Match joined tables properly when lifting for group by aggregate #27170

Merged
merged 1 commit into from
Jan 20, 2022

Conversation

smitpatel
Copy link
Contributor

@smitpatel smitpatel commented Jan 12, 2022

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:

  • If aliases of original tables in parent and subquery doesn't match then we assume subquery is non-grouping element rooted and we don't lift.
  • If parent have lifted additional joins, one of them being a subquery join, then we abort lifting if the subquery contains a join to lift which is a subquery.
  • If we are allowed to join after first 2 checks then:
    • We copy over owned entity in initial tables
    • We try to match additional joins after initial if they are table joins and joining to same table, in which case we don't need to join them again.
    • We copy over all other joins.

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.

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,
- If aliases of original tables in parent and subquery doesn't match then we assume subquery is non-grouping element rooted and we don't lift.
- If parent have lifted additional joins, one of them being a subquery join, then we abort lifting if the subquery contains a join to lift which is a subquery.
- If we are allowed to join after first 2 checks then,
  - We copy over owned entity in initial tables
  - We try to match additional joins after initial if they are table joins and joining to same table, in which case we don't need to join them again.
  - We copy over all other joins.

Resolves #27163
@smitpatel smitpatel marked this pull request as ready for review January 14, 2022 03:07
@smitpatel smitpatel requested a review from a team January 14, 2022 03:12
@AndriySvyryd AndriySvyryd added this to the 6.0.x milestone Jan 14, 2022
@leecow leecow modified the milestones: 6.0.x, 6.0.3 Jan 18, 2022
@smitpatel smitpatel merged commit 10b4433 into release/6.0 Jan 20, 2022
@smitpatel smitpatel deleted the smit/offo branch January 20, 2022 20:22
@smitpatel
Copy link
Contributor Author

Just realized, branch may not be open.

@AndriySvyryd
Copy link
Member

AFAIK no new builds will be produced for 6.0.2

@dougbu Should we revert this just in case?

@dougbu
Copy link
Member

dougbu commented Jan 20, 2022

@mmitche @wtgodbe @vseanreesermsft if builds have made it from build 20220120.1 to anywhere important, can that be undone❔

@smitpatel and @AndriySvyryd, I defer to the three above on whether reverting the change is needed. It's already correctly marked as going into 6.0.3 😃

@wtgodbe
Copy link
Member

wtgodbe commented Jan 20, 2022

There was an open ingestion PR in aspnetcore that I just closed, we should be good as long as the subscription doesn't get triggered again (which would only happen if there was another build of EFCore).

@smitpatel smitpatel removed this from the 6.0.3 milestone Jan 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants