Repository navigation
Lift GroupBy aggregates over navigations next to operator chains - #39648
AndriySvyryd merged 5 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>
|
@dotnet-policy-service agree |
1 similar comment
|
@dotnet-policy-service agree |
There was a problem hiding this comment.
🟡 Changes recommended
Critical navigation-aggregate lifting bugs produce incorrect counts for nullable projections and distinct values, and cause translation failures when predicates capture grouping keys.
3 open issues
What changed in this PR
This PR extends EF Core’s GroupBy navigation-aggregate optimization so supported operator chains no longer force sibling aggregates into correlated subqueries, addressing #39628.
Changes:
- Recognizes and rebuilds supported
Where,Select, andDistinctchains before aggregates. - Adds filtered-principal guards and retains fallback for selected unsupported shapes.
- Adds regression coverage and updates SQL baselines for lifted queries.
| File | Description |
|---|---|
src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs |
Extends aggregate scanning, navigation expansion, guards, and chain rebuilding. |
test/EFCore.Specification.Tests/Query/NorthwindGroupByQueryTestBase.cs |
Adds supported-chain and fallback query tests. |
test/EFCore.Relational.Specification.Tests/Query/AdHocQueryFiltersQueryRelationalTestBase.cs |
Tests chain behavior with filtered required principals. |
test/EFCore.SqlServer.FunctionalTests/Query/NorthwindGroupByQuerySqlServerTest.cs |
Adds SQL Server baselines for the new GroupBy tests. |
test/EFCore.SqlServer.FunctionalTests/Query/AdHocQueryFiltersQuerySqlServerTest.cs |
Verifies SQL Server filtered-principal guards. |
test/EFCore.Sqlite.FunctionalTests/Query/AdHocQueryFiltersQuerySqliteTest.cs |
Verifies SQLite filtered-principal guards. |
test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs |
Updates an existing baseline to use a joined aggregate instead of correlated subqueries. |
Review effort: Balanced
- Treat a navigation projected on its own (o.Customer, or EF.Property over it) as a navigation in the Distinct guard, so its distinct count stays on the correlated translation. - Bind g.Key read inside a lifted aggregate's lambdas to the new grouping parameter. - Rebuild Count and LongCount after a Select without the Select, so elements projected to null are still counted.
There was a problem hiding this comment.
🟡 Changes recommended
The query rewrite produces incorrect aggregate results for filtered principals and cast-rooted navigations, and causes translation failures for nested grouping aggregates.
3 open issues
3 resolved since last review
Review effort: Balanced
- Count, LongCount and a parameterless Any drop a trailing Select before its navigations are expanded, as navigation expansion does with a pending selector, so a filtered-out principal no longer removes the element from the count. - An entity cast in a Distinct-counted projection keeps the correlated translation, like the other navigation shapes there. - An aggregate over the grouping nested in another aggregate's lambda keeps the projection on the correlated translation.
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!

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