Skip to content

[9.0] LINQ: Take/Skip before SelectMany page the parent documents (backport of #5931) - #5932

Merged
jeremydmiller merged 2 commits into
9.0from
fix/5928-selectmany-paged-parents-9.0
Nov 15, 2026
Merged

jeremydmiller merged 2 commits into
9.0from
fix/5928-selectmany-paged-parents-9.0

Conversation

@jeremydmiller

@jeremydmiller jeremydmiller commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Backport of #5931 to 9.0 (Fixes #5928 on the 9.x line).

Merge order: rebased onto origin/9.0 at e184d6952, 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 -x from #5931:

  1. e7df490c1 (from 75ba3953a): the fix.
  2. e3c8c4209 (from f78d5d12e): a follow-up fixing a regression the first commit introduced. The first commit decided whether an Any() was paged by looking for a LIMIT on the parent statement. But every Any() already had a LIMIT 1 by then, so an unpaged SelectMany(x => x.Children).Any() asked only the first parent. The decision now comes from an explicit flag, and unpaged Any() SQL is byte-identical to before. The page CTE also selects d.* so a paged Any() 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. A Take()/Skip() before SelectMany() used to leave its LIMIT/OFFSET on 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/Single after a paged SelectMany() go through #5923's flattened-rows path and pick from the paged parents' children. Paging after the SelectMany() still pages the flattened rows. SelectMany SQL without paging before it is byte-identical, Any() and FirstOrDefault() included.

SQL for OrderBy(Number).Skip(1).Take(3).SelectMany(x => x.Children):

  • Before: 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;
  • After: 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 SingleValueModeAppliesToFlattenedRows and this PR's anyAppliesToPagedParentsOfSelectMany, both declared just above ProcessSingleValueModeIfAny. I kept both, in the order master has 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)

  • Red: with the product file at origin/9.0 (e184d6952), the original 201-case class fails 115 of 201, the same set as master. Before the rebase, against 7c5e3d753 without the fix, compiled_paging_before_select_many failed 7 of 9.
  • Green with both commits: the two Bug_5928_* classes pass 214/214. Full CompiledQueryTests 31/31.
  • Full LinqTests on net10.0: 2059 total, 2059 passed, 0 skipped, 0 failed.
  • markdownlint and cspell: 0 issues.

CI

tests.yml does not trigger for 9.0 branches, so it was dispatched by hand for e3c8c4209: https://github.com/JasperFx/marten/actions/runs/43737971397. Earlier runs on superseded heads ended cancelled.

🤖 Generated with Claude Code

)

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
jeremydmiller force-pushed the fix/5928-selectmany-paged-parents-9.0 branch from e008647 to e7df490 Compare November 15, 2026 16:45
 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>
@jeremydmiller
jeremydmiller merged commit 4fc7d6c into 9.0 Nov 15, 2026
30 checks passed
@jeremydmiller
jeremydmiller deleted the fix/5928-selectmany-paged-parents-9.0 branch November 15, 2026 17:06
This was referenced Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant