Skip to content

[9.0] GroupJoin + SelectMany: Any() asks whether the join has a row (backport from #5911) - #5917

Merged
jeremydmiller merged 1 commit into
9.0from
fix/groupjoin-any-9.0
Nov 15, 2026
Merged

jeremydmiller merged 1 commit into
9.0from
fix/groupjoin-any-9.0

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Backport to the 9.x line of the GroupJoin Any() fix from #5911 (Marten 10; merged to master as ac3536902).

The bug

Any() / AnyAsync() over a GroupJoin + SelectMany query selected from the outer CTE alone. So it ignored the join and any filter on the inner side, and returned true whenever outer rows existed:

// true on 9.x today, though no order is over 1000
await session.Query<Customer>()
    .GroupJoin(session.Query<Order>(), c => c.Id, o => o.CustomerId, (c, orders) => new { c, orders })
    .SelectMany(x => x.orders, (x, o) => new { x.c.Name, o.Amount })
    .Where(x => x.Amount > 1000m)
    .AnyAsync();

Count() already wrapped the join in a CTE before counting. Any() now does the same: a 19-line block in CollectionUsage.Compilation.cs, identical to master's.

Deliberately not backported

#5911's second GroupJoin fix makes Marten refuse a Where() on the inner side after a left join (DefaultIfEmpty()), which today returns wrong rows. It turns a wrong result into an exception, so it stays a 10.0 behaviour change rather than going into a 9.x minor.

Verification (net10.0, PostgreSQL 17)

Note that tests.yml runs only for master, so this PR gets no CI checks unless the workflow is started by hand:
gh workflow run tests.yml --repo JasperFx/marten --ref fix/groupjoin-any-9.0

🤖 Generated with Claude Code

…rt from #5911)

Any() over a GroupJoin/SelectMany selected from the outer CTE alone, so it
ignored the join and every filter on its inner side and answered true for any
outer row. It now wraps the join in a CTE first, the same wrap Count() already
had. Backport of the first of the two GroupJoin fixes in #5911 (10.0). The
second, refusing an inner-side Where() after a left join, turns a wrong result
into an exception and stays a 10.0 behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremydmiller
jeremydmiller merged commit d824a8d into 9.0 Nov 15, 2026
31 checks passed
@jeremydmiller
jeremydmiller deleted the fix/groupjoin-any-9.0 branch November 15, 2026 14:42
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