Repository navigation
Lift GroupBy aggregates over navigations next to operator chains - #39648
Open
seragAhmed wants to merge 2 commits into
Open
seragAhmed wants to merge 2 commits into
seragAhmed wants to merge 2 commits into
Conversation
- Accept Where, Select and Distinct chains before a grouping aggregate, so one of them no longer forces every navigation aggregate in the projection into a correlated subquery. - Keep Distinct over a navigation on the correlated translation, which counts the null of a missing principal. - Guard chained aggregates over relaxed filtered principals. Fixes dotnet#39628 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
|
@dotnet-policy-service agree |
1 similar comment
Author
|
@dotnet-policy-service agree |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #39628
The lift of GroupBy aggregates over reference navigations (#38668) is all or nothing:
GroupingAggregateScanneronly recognizes aggregates applied directly to the grouping, and any other use of the grouping makesTryLiftAggregatesOverNavigationsgive up for the whole projection. So oneg.Select(x => x.Value).Distinct().Count()next tog.Max(x => x.Author.Rating)turned every navigation aggregate back into a correlated subquery.The scanner now also accepts a short operator chain before the aggregate:
Whereover the element, at most oneSelect, andDistinctafter theSelect. The lambdas over the element are expanded on the parent like aggregate selectors, and the chain is rebuilt over the lifted grouping. After aSelect, only aggregates without a selector or predicate are accepted (including the non-generic numeric overloads such asSum(IEnumerable<int?>)), since a lambda over the projected values would not have its navigations expanded. Other uses of the grouping keep the existing translation.Two cases are deliberately left alone:
Distinctover a navigation is not lifted. The correlated translation counts the null of a missing principal as a value (COUNT(*)overSELECT DISTINCT), while the lifted one would useCOUNT(DISTINCT ...)and skip it.Group_by_multiple_aggregate_joining_different_tableskeeps its baseline.Where,SelectandDistinctcan observe the null-extended row.Parameter_collection_Contains_with_projection_and_orderingusesg.Select(e => e.Order.OrderDate).Max(), which is now lifted too; its SQL Server baseline is updated (oneINNER JOINinstead of two correlated subqueries).Tests:
NorthwindGroupByQueryTestBase: distinct count, select-then-aggregate and where-then-count next to navigation aggregates (lifted), and a predicate afterSelect(still correlated), with SQL Server baselines.AdHocQueryFiltersQueryRelationalTestBase: the three chain kinds over a required navigation to a filtered principal, with SQL Server and SQLite baselines.Locally, the SQL Server and SQLite
Microsoft.EntityFrameworkCore.Query*functional tests, the InMemory GroupBy tests andEFCore.Testspass.🤖 Generated with Claude Code