Skip to content

Fall back to a subquery for GroupBy element orderings and predicates over owned types in their own table - #39835

Open
jeremy-morren wants to merge 1 commit into
dotnet:mainfrom
jeremy-morren:fix-owned-own-table-groupby
Open

jeremy-morren wants to merge 1 commit into
dotnet:mainfrom
jeremy-morren:fix-owned-own-table-groupby

Conversation

@jeremy-morren

Copy link
Copy Markdown
Contributor

Ordering or filtering a group's elements by a member of an owned type mapped to its own table (g.OrderByDescending(e => e.Created.At).First(), g.MaxBy(...), g.Count(e => e.Created.At > x)) threw "Translation of 'EF.Property<...>' failed" instead of falling back to a subquery.

  • Over a grouping element, an owned type that needs a join is left unexpanded (Query: expand owned navigations in GroupBy aggregate selectors #38918), so its property doesn't bind. Aggregate selectors (g.Max(e => e.Created.At)) already handled this by translating with the EF.Property error turned off, and fell back to a subquery; orderings, predicates and Any/All didn't, so they threw.
  • TranslateAggregateArgument, which all of these share, now turns the error off itself and restores the previous setting afterwards. All of them now fall back to the subquery, which joins the owned table in.
  • Added GroupBy tests over an associate's property (OrderByDescending(...).First(), MaxBy, Max, Count with a predicate) to the shared associations tests.

Fixes #39833

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

Copilot AI balanced review requested due to automatic review settings November 27, 2026 01:30
@jeremy-morren
jeremy-morren requested a review from a team as a code owner November 27, 2026 01:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The review has no active issues requiring resolution.

No open issues 🎉

What changed in this PR

Fixes #39833 by extending EF Core’s relational GroupBy translation fallback to orderings and predicates over owned types mapped to separate tables. This builds on #38918 without adding joins directly to the grouped query.

Changes:

  • Centralizes suppression of unbound EF.Property errors in TranslateAggregateArgument, restoring the previous setting afterward.
  • Adds shared regression tests for OrderByDescending(...).First(), MaxBy, Max, and predicate-based Count.
  • Adds SQL Server baselines across association mappings and Cosmos assertions for unsupported GroupBy translation.
File Description
test/​EFCore.SqlServer.FunctionalTests/​Query/​Associations/​OwnedTableSplitting/​OwnedTableSplittingMiscellaneousSqlServerTest.cs Adds baselines for table-split owned types.
test/​EFCore.SqlServer.FunctionalTests/​Query/​Associations/​OwnedNavigations/​OwnedNavigationsMiscellaneousSqlServerTest.cs Verifies separate-table owned types use subquery fallback.
test/​EFCore.SqlServer.FunctionalTests/​Query/​Associations/​OwnedJson/​OwnedJsonMiscellaneousSqlServerTest.cs Adds baselines for JSON-mapped owned types.
test/​EFCore.SqlServer.FunctionalTests/​Query/​Associations/​OwnedComplexProperties/​OwnedComplexPropertiesMiscellaneousSqlServerTest.cs Adds baselines for mixed owned and complex mappings.
test/​EFCore.SqlServer.FunctionalTests/​Query/​Associations/​Navigations/​NavigationsMiscellaneousSqlServerTest.cs Adds baselines for regular navigation properties.
test/​EFCore.SqlServer.FunctionalTests/​Query/​Associations/​ComplexTableSplitting/​ComplexTableSplittingMiscellaneousSqlServerTest.cs Adds baselines for table-split complex types.
test/​EFCore.SqlServer.FunctionalTests/​Query/​Associations/​ComplexJson/​ComplexJsonMiscellaneousSqlServerTest.cs Adds baselines for JSON-mapped complex types.
test/​EFCore.Specification.Tests/​Query/​Associations/​AssociationsMiscellaneousTestBase.cs Defines four shared GroupBy regression tests.
test/​EFCore.Cosmos.FunctionalTests/​Query/​Associations/​OwnedNavigations/​OwnedNavigationsMiscellaneousCosmosTest.cs Asserts existing GroupBy limitations for owned navigations.
test/​EFCore.Cosmos.FunctionalTests/​Query/​Associations/​OwnedComplexProperties/​OwnedComplexPropertiesMiscellaneousCosmosTest.cs Asserts existing GroupBy limitations for mixed mappings.
test/​EFCore.Cosmos.FunctionalTests/​Query/​Associations/​ComplexProperties/​ComplexPropertiesMiscellaneousCosmosTest.cs Asserts existing GroupBy limitations for complex properties.
src/​EFCore.Relational/​Query/​RelationalSqlTranslatingExpressionVisitor.cs Enables shared subquery fallback while preserving error-handling state.

Review effort: Balanced

…over owned types in their own table

Over a grouping element, an owned type that needs a join is left unexpanded,
so its property doesn't bind. Aggregate selectors already translated with
the EF.Property error turned off and fell back to a subquery, but orderings,
predicates and Any/All threw instead. TranslateAggregateArgument, which all
of them share, now turns the error off itself and restores it afterwards.

Fixes dotnet#39833

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremy-morren
jeremy-morren force-pushed the fix-owned-own-table-groupby branch from 49608f8 to 87ec5f5 Compare November 27, 2026 06:33
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.

GroupBy element OrderBy(...).First()/MaxBy over an owned type mapped to its own table fails to translate

2 participants