Repository navigation
[9.0] LINQ: Take/Skip before SelectMany page the parent documents (backport of #5931) - #5932
Merged
Merged
Conversation
) A Take()/Skip() before a SelectMany() left its LIMIT/OFFSET on the statement that explodes the collection. The explosion is a set-returning function in the select list, so the paging landed on the flattened rows: OrderBy(x => x.Number).Take(3).SelectMany(x => x.Children) returned the first three children overall instead of every child of the first three documents (LINQ to Objects / EF Core semantics). CompileSelectMany now turns a paged parent statement into a CTE of the page (keeping its WHERE and ORDER BY) and explodes from that CTE. The same applies to SelectMany(...).Any() over paged parents, which used to page the parents that passed the not-empty filter. Paging after the SelectMany still pages the flattened rows, and unpaged SelectMany SQL is unchanged. First/Single after a paged SelectMany (#5921, backported by #5923) pick from the paged parents' children. Fixes #5928 (cherry picked from commit 75ba395) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jeremydmiller
force-pushed
the
fix/5928-selectmany-paged-parents-9.0
branch
from
November 15, 2026 16:45
e008647 to
e7df490
Compare
follow-up) The first #5928 commit decided in compileNext whether a SelectMany(...).Any() was over paged parents by looking for a LIMIT on the parent statement. But ProcessSingleValueModeIfAny had already given every Any() a LIMIT 1, so an UNPAGED Query<T>().SelectMany(x => x.Children).Any() was wrapped in a page of one parent and answered false whenever the first parent had nothing to flatten. compileNext now acts only when ProcessSingleValueModeIfAny deferred the Any() to it, and unpaged Any() sends exactly the SQL it did before. The CTE of the paged parents now selects d.* instead of d.data, so a paged Any() over a duplicated array field (whose not-empty filter reads the field's own column) still finds that column. Regression tests: unpaged Any() with an empty first parent in every ordering, the unpaged Any() SQL captured on master, and paged/unpaged Any() over a duplicated array field. (cherry picked from commit f78d5d1) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 16, 2026
This was referenced Nov 26, 2026
Closed
This was referenced Dec 3, 2026
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.
Backport of #5931 to
9.0(Fixes #5928 on the 9.x line).Merge order: rebased onto
origin/9.0ate184d6952, which has both #5929 (the 9.0 backport of #5925's count fix) and #5923 (the 9.0 backport of #5922, First/Single after SelectMany). Nothing else has to merge first.Two commits, each cherry-picked with
-xfrom #5931:e7df490c1(from75ba3953a): the fix.e3c8c4209(fromf78d5d12e): a follow-up fixing a regression the first commit introduced. The first commit decided whether anAny()was paged by looking for aLIMITon the parent statement. But everyAny()already had aLIMIT 1by then, so an unpagedSelectMany(x => x.Children).Any()asked only the first parent. The decision now comes from an explicit flag, and unpagedAny()SQL is byte-identical to before. The page CTE also selectsd.*so a pagedAny()over a duplicated array field still sees the field's column.What this changes
The same fix as #5931, in
src/Marten/Linq/CollectionUsage.Compilation.cs. The added and removed lines are identical to master's two commits. ATake()/Skip()beforeSelectMany()used to leave itsLIMIT/OFFSETon the statement that explodes the collection, so it paged the flattened rows. Now the paged parent statement becomes a CTE of the page, and the collection is exploded from that CTE.SelectMany(...).Any()over paged parents asks the page.First/Singleafter a pagedSelectMany()go through #5923's flattened-rows path and pick from the paged parents' children. Paging after theSelectMany()still pages the flattened rows. SelectMany SQL without paging before it is byte-identical,Any()andFirstOrDefault()included.SQL for
OrderBy(Number).Skip(1).Take(3).SelectMany(x => x.Children):WITH cte1 as (select jsonb_array_elements(CAST(d.data ->> 'Children' as jsonb)) as data from mt_doc_pagedparent as d order by CAST(d.data ->> 'Number' as integer) OFFSET $1 LIMIT $2) select d.data as data from cte1 as d;WITH cte1 as (select d.* from mt_doc_pagedparent as d order by CAST(d.data ->> 'Number' as integer) OFFSET $1 LIMIT $2), cte2 as (select jsonb_array_elements(CAST(d.data ->> 'Children' as jsonb)) as data from cte1 as d) select d.data as data from cte2 as d;The migration-guide note sits under "Key Changes in 9.60.0", next to #5929's #5920 note.
Rebase onto #5923
The rebase conflicted in one hunk: #5923's
SingleValueModeAppliesToFlattenedRowsand this PR'sanyAppliesToPagedParentsOfSelectMany, both declared just aboveProcessSingleValueModeIfAny. I kept both, in the ordermasterhas them. The two don't overlap: one covers First/Single, the other covers Any. The test files are identical to master's, with no skips.Red / green on 9.0 (net10.0, database
marten_9_q)origin/9.0(e184d6952), the original 201-case class fails 115 of 201, the same set asmaster. Before the rebase, against7c5e3d753without the fix,compiled_paging_before_select_manyfailed 7 of 9.Bug_5928_*classes pass 214/214. Full CompiledQueryTests 31/31.CI
tests.ymldoes not trigger for9.0branches, so it was dispatched by hand fore3c8c4209: https://github.com/JasperFx/marten/actions/runs/43737971397. Earlier runs on superseded heads ended cancelled.🤖 Generated with Claude Code