Repository navigation
Bind complex properties over single-result subqueries - #39763
jeremy-morren wants to merge 5 commits into
Conversation
159c597 to
842f3a1
Compare
842f3a1 to
3d36469
Compare
Accessing a complex property on an entity produced by a single-result subquery (e.g. GroupBy(...).Select(g => g.First(...)).Select(e => e.Complex)) threw "Complex property binding over a subquery". Bind the complex property inside a copy of the subquery and project its type from there, so that its properties are bound as scalar subqueries. Handles table-split and JSON complex types, including nested complex properties.
A complex collection accessed on an entity produced by a single-result subquery threw "Complex collection binding over a subquery". Fail translation instead, so that projecting the collection falls back to client evaluation and other uses report the standard translation error.
3d36469 to
9553587
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Direct column binding bypasses the optional-owned presence guard, causing filters to incorrectly retain groups when an absent owned instance shares a column with its owner.
1 open issue
What changed in this PR
Extends EF Core’s relational query translation to bind complex properties and included reference navigations over single-result subqueries, advancing #36296 while leaving complex-type equality over subqueries unsupported.
Changes:
- Binds nested and optional complex properties through cloned subquery projections.
- Unwraps included navigations and allows client evaluation of unsupported complex-collection projections.
- Adds shared regression tests, SQL Server baselines, and Cosmos translation-failure expectations.
| File | Description |
|---|---|
test/EFCore.SqlServer.FunctionalTests/Query/Associations/OwnedTableSplitting/OwnedTableSplittingMiscellaneousSqlServerTest.cs |
Adds filtering and ordering baselines for table-split owned types. |
test/EFCore.SqlServer.FunctionalTests/Query/Associations/OwnedNavigations/OwnedNavigationsMiscellaneousSqlServerTest.cs |
Adds owned-navigation filtering and ordering baselines. |
test/EFCore.SqlServer.FunctionalTests/Query/Associations/OwnedJson/OwnedJsonMiscellaneousSqlServerTest.cs |
Covers owned JSON with text and native JSON storage. |
test/EFCore.SqlServer.FunctionalTests/Query/Associations/OwnedComplexProperties/OwnedComplexPropertiesMiscellaneousSqlServerTest.cs |
Covers owned types containing complex properties. |
test/EFCore.SqlServer.FunctionalTests/Query/Associations/Navigations/NavigationsMiscellaneousSqlServerTest.cs |
Adds regular-navigation filtering and ordering baselines. |
test/EFCore.SqlServer.FunctionalTests/Query/Associations/ComplexTableSplitting/ComplexTableSplittingProjectionSqlServerTest.cs |
Covers nested and optional complex-property projections. |
test/EFCore.SqlServer.FunctionalTests/Query/Associations/ComplexTableSplitting/ComplexTableSplittingMiscellaneousSqlServerTest.cs |
Adds table-split complex-property filtering and ordering baselines. |
test/EFCore.SqlServer.FunctionalTests/Query/Associations/ComplexJson/ComplexJsonProjectionSqlServerTest.cs |
Covers JSON scalar projections and collection fallback. |
test/EFCore.SqlServer.FunctionalTests/Query/Associations/ComplexJson/ComplexJsonMiscellaneousSqlServerTest.cs |
Adds complex JSON filtering and ordering baselines. |
test/EFCore.Specification.Tests/Query/Associations/ComplexProperties/ComplexPropertiesProjectionTestBase.cs |
Defines shared complex-property projection tests. |
test/EFCore.Specification.Tests/Query/Associations/AssociationsMiscellaneousTestBase.cs |
Defines shared GroupBy element filtering and ordering tests. |
test/EFCore.Relational.Specification.Tests/Query/Associations/ComplexJson/ComplexJsonProjectionRelationalTestBase.cs |
Tests collection and collection-count projections. |
test/EFCore.Cosmos.FunctionalTests/Query/Associations/OwnedNavigations/OwnedNavigationsMiscellaneousCosmosTest.cs |
Records unsupported owned-navigation GroupBy queries. |
test/EFCore.Cosmos.FunctionalTests/Query/Associations/OwnedComplexProperties/OwnedComplexPropertiesMiscellaneousCosmosTest.cs |
Records unsupported owned-complex GroupBy queries. |
test/EFCore.Cosmos.FunctionalTests/Query/Associations/ComplexProperties/ComplexPropertiesProjectionCosmosTest.cs |
Records unsupported complex-property projections. |
test/EFCore.Cosmos.FunctionalTests/Query/Associations/ComplexProperties/ComplexPropertiesMiscellaneousCosmosTest.cs |
Records unsupported complex-property filtering and ordering. |
src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs |
Implements subquery property and included-navigation binding. |
Review effort: Balanced
|
Changes since the last update, all on an entity returned by a single-result subquery such as
The binding over the subquery and the presence check are each shared in a helper, so the subquery and non-subquery paths use the same code. |
…avigations Composing a Where or OrderBy over an entity produced by a single-result subquery (e.g. GroupBy(...).Select(g => g.First(...)).Where(...)) failed to translate when the entity includes navigations, such as owned types or auto-included navigations: the subquery's shaper was wrapped in IncludeExpressions. Unwrap the includes, and bind an included reference navigation over the same subquery, projecting the navigation's target. Moves the GroupBy element tests to the shared associations test base, so that they run for complex types, owned types and navigations.
… subqueries An owned navigation included by a single-result subquery (e.g. g.First(...).Details) can be an optional dependent that shares a column with its owner. Binding such a property read the column directly, so a filter saw the owner's value when the owned instance was absent. Apply the same presence check as for properties bound without a subquery, by sharing it in a helper.
…-result subqueries Comparing a JSON owned navigation of an entity returned by a single-result subquery to null checked the values in the document, so a document whose values are all null was treated as absent. Check the document itself over the subquery instead. Querying a primitive collection of a JSON complex type on such an entity threw while generating the table alias, since the scalar subquery's projection has no alias. Derive the alias from the projected JSON property instead.
e6908ad to
a7e93f6
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Nested owned-JSON predicates still fail after grouped First projections because binding relies on pruned includes, and the new complex-property binding path lacks precompiled-query regression coverage.
2 open issues
1 resolved since last review
Review effort: Balanced
| && includedNavigations.TryGetValue(navigation, out var navigationExpression) | ||
| && Visit(subquery.UpdateShaperExpression(navigationExpression)) is StructuralTypeReferenceExpression navigationReference) |
| subquery = subquery.Update( | ||
| subSelectExpression, | ||
| new RelationalStructuralTypeShaperExpression( | ||
| complexProperty.ComplexType, | ||
| new ProjectionBindingExpression(subSelectExpression, projectionMember, typeof(ValueBuffer)), |



Add translation for complex properties and owned navigations on an entity returned by a single-result subquery, such as
GroupBy(...).Select(g => g.First(...)):Complex property binding over a subquery.Complex collection binding over a subquery, so projecting one falls back to client evaluation and other uses get the standard translation error.WhereandOrderByon such an element failed to translate when the entity includes navigations (e.g. owned types), because the subquery's shaper was wrapped inIncludeExpressions. The includes are unwrapped, and an included reference navigation is bound over the same subquery.Comparing such a complex property to null or to another value still throws
SubqueryOverComplexTypesNotSupported, since it would duplicate the subquery (#32228).The GroupBy element tests are in the shared associations test base, so they run for complex, owned and navigation mappings.
Implements some of #36296 (Subqueries: complex property binding over subquery).