Repository navigation
Fix split query paging order when limit/offset is pushed into a subquery - #39687
Conversation
- When a reference navigation is joined after Skip/Take, the paging is pushed into a subquery before the split query identifier orderings are added, so those orderings only reached the outer query. - Also add the identifier orderings to the paged subquery, so the main and split queries select the same rows. Fixes dotnet#39206
@dotnet-policy-service agree |
There was a problem hiding this comment.
🔵 Needs a closer look
Human validation is needed because the shared SelectExpression ordering change affects recursive nested split-query pagination and provider-specific SQL shapes beyond the covered SQL Server/SQLite scenarios.
No open issues 🎉
What changed in this PR
This pull request fixes inconsistent parent and child row selection in relational split queries when paging is pushed into subqueries, by propagating split-query identifier ordering into the paged query paths.
Changes:
- Updates relational SQL shaping for identifier ordering in paged subqueries.
- Adds projection and reference-include regression coverage with tied ordering values.
- Refreshes SQL Server baselines for split-query paging and filtered include scenarios.
| File | Description |
|---|---|
| test/EFCore.SqlServer.FunctionalTests/Query/NorthwindSplitIncludeQuerySqlServerTest.cs | Updates tracking split-include SQL baselines to include identifier ordering in per-parent paging queries. |
| test/EFCore.SqlServer.FunctionalTests/Query/NorthwindSplitIncludeNoTrackingQuerySqlServerTest.cs | Updates no-tracking split-include SQL baselines to include identifier ordering in per-parent paging queries. |
| test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsCollectionsSplitQuerySqlServerTest.cs | Updates complex navigation split-query paging baselines with the propagated identifier ordering. |
| test/EFCore.SqlServer.FunctionalTests/Query/AdHocQuerySplittingQuerySqlServerTest.cs | Adds SQL Server baselines for the new split-query projection and reference-include regression tests. |
| test/EFCore.Relational.Specification.Tests/Query/AdHocQuerySplittingQueryTestBase.cs | Adds shared regression tests and test models for split-query paging with non-unique ordering. |
| src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs | Adds identifier-order propagation into paged subqueries during relational SQL shaping. |
Review effort: Lite
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!
* Order the row limits of split queries by the key Each statement of a split query selects the parents again. When a row limit is ordered by something that ties, the database can break the ties differently in each statement, and so select different parents. EF 11 throws a DbQueryConcurrencyException for that, although nothing was modified and retrying does not help. EF 10 returned the collections empty. For a query that is split, the key is now added to the end of the ordering of every row limit. A query that is not split is left as it is. PagedOrdering covers the limits on the query itself: skip and take, connections, and those a resolver composes. It works around a bug in EF, and can be removed when the minimum EF version has the fix. SubqueryOrdering covers limits in predicates, unions, joins, filtered includes, and groups. The EF fix does not cover most of these, so it stays. dotnet/efcore#39206 dotnet/efcore#39687 * Fix and extend the ordering of split query limits - Order and OrderDescending are explicit orderings, which the key replaced. - The limits of a collection mapped to JSON are by position, and ordering them selected the wrong element. - MinBy and MaxBy select a row the way First does, so are ordered too. - Reverse is ordered beneath, rather than after. - The default ordering of EfOrderBy is not cached by context type, since contexts of the same type can differ in whether they use it. - ApplyProjection leaves a query that is not split as it is where that is known: one AsSingleQuery was called on, and one of another provider. - A projection, a group, and the result of a Distinct or a Union are ordered by what identifies their rows. - A query is not split when a limit in it can not be ordered, such as one over a keyless entity, or one that is ordered at random. * . * Update Directory.Build.props
Fixes #39206
With
AsSplitQuery(), a non-uniqueOrderByandSkip/Take, joining a reference navigation (in a projection or throughInclude) makes EF push the paging into a subquery beforeApplyProjectionadds the split query identifier orderings (#26808, added in #34097). Those orderings then only reached the outer query, so the subquery that selects the page was still ordered by the non-unique column alone, and the main query and the split queries could select different rows. Child collections could then come back empty, with no error.This change also appends the identifier orderings to the paged subquery (following the identifier column into nested paged subqueries), for both the main query and the split query. Subqueries without a limit or offset are not changed.
Before (excerpt of the main query for the projection from the issue):
After, both the main query and the split query page with
ORDER BY [p].[Name], [p].[Id].Baseline changes in existing tests
Complex_multi_include_with_order_by_and_paging*: the same bug (paging ordered by[l].[Name]only).Lift_projection_mapping_when_pushing_down_subquery:Take(25)withoutOrderBynow orders the pushed-downTOPby the key, so the main and split queries take the same 25 rows.Include_collection_with_cross_apply_with_filter,Include_collection_with_outer_apply_with_filterandInclude_collection_with_outer_apply_with_filter_non_equality(tracking and no-tracking split include tests): theTOP(5)inside theAPPLYwas ordered only by[c].[CustomerID], which is the same for every row of one customer; it is now also ordered by[o].[OrderID].Tests
AdHocQuerySplittingQueryTestBase(projection andIncludeshapes) with tiedNamevalues, with SQL Server baselines. Without the fix they fail on the SQL baseline; the data check alone does not reliably fail, because SQL Server tends to return tied rows in the same order.EFCore.Sqlite.FunctionalTests: 38,996 passed, 0 failed, 282 skipped.EFCore.Relational.Tests: 1,662 passed, 0 failed, 1 skipped.EFCore.SqlServer.FunctionalTestson LocalDB: 50,230 passed, 498 skipped, 956 failed. All 956 failures are in test classes whose fixtures need features this LocalDB does not have:JsonQueryJsonTypeSqlServerTest,PrimitiveCollectionsQuerySqlServerJsonTypeTest,JsonUpdateJsonTypeSqlServerTest(nojsontype),VectorTranslationsSqlServerTest(novectortype),FullTextSearchTranslationsSqlServerTest,SqlAzureDatabaseCreationTestandMemoryOptimizedTablesTest.Note
I used an AI assistant (Claude) to help investigate this, write the change and tests, and write this description. The test results above come from local runs against this branch.