Skip to content

[9.0] LINQ: OrderBy() after a projection orders the projected values (backport of #5961) - #5962

Merged
jeremydmiller merged 9 commits into
9.0from
fix/5955-orderby-after-projection-9.0
Nov 15, 2026
Merged

jeremydmiller merged 9 commits into
9.0from
fix/5955-orderby-after-projection-9.0

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Backport of #5961 to 9.0. It fixes #5955 on the 9.x line. Merge after #5954. This branch is stacked on #5954's head, b1bf6a5cc, so it also includes #5939, #5945 and #5954.

3b3887b67 was cherry-picked with -x from cccd16137. Two things are adapted for 9.0:

  • the migration note goes under "Key Changes in 9.60.0"
  • the test file adds using Marten.Exceptions;, because BadLinqExpressionException is not in scope there on 9.0 without it

The product code applied unchanged. #5961 has the root cause, the SQL and the test design.

Red then green on 9.0 (net10.0, marten_9_bb)

Tests On the #5954 head This branch
Bug_5955_order_by_after_projection 20 failed / 14 passed of 34 (the same split as on master) 34 / 34

Full suites on this branch

  • LinqTests net10.0: 2742 / 2742
  • CoreTests net10.0: 911 passed, 9 skipped, 0 failed

markdownlint and cspell are clean.

CI, started by hand: https://github.com/JasperFx/marten/actions/runs/43744909547

🤖 Generated with Claude Code

jeremydmiller and others added 9 commits November 15, 2026 11:12
A Select() after SelectMany() was compiled by the top-level select visitor,
which has no translation for arithmetic or method calls over a flattened
element, so SelectMany(x => x.Values).Select(v => v % 3) returned the raw
values. Where(), OrderBy() and Take() after a projection of child documents
were dropped as well, and a same-type projection shared one collection usage
with the operators before and after it.

The projection is now translated to SQL over a CTE of the flattened rows and
the projected rows are read back like a flattened value collection, so every
operator after the Select() applies to the projected value. Projections that
cannot be translated exactly, and filters/orderings after an object
projection, are refused with BadLinqExpressionException.

A compiled query builds its statements twice; the SelectMany() compiled-once
guard is reset per build so the second build does not stop at the parent
documents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit f7e8969)
DuplicatedArrayField.AttachSelectManyStatement returned the flattened
statement without attaching it after the exploding CTE, so the command read
a CTE it never declared (42P01), and it skipped every Where(), OrderBy(),
paging and Select() after the SelectMany(). Its explosion and count used the
JSONB locator, which is empty for a duplicated field (42883 on
jsonb_array_length()).

The duplicated column is now flattened with unnest(d.<col>) and counted with
coalesce(array_length(d.<col>, 1), 0), and the flattened elements go through
the same configuration as the JSONB value collection the field duplicates.
SQL for collections that are not duplicated is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 6c75ffb)
PostgreSQL orders text by the database collation. CI's PostgreSQL 15 orders
bytes, so "Bravo" sorted before "alpha" there while LINQ to Objects and a
culture-aware collation put it after. The flattened names are all lower case
now, which both orders agree on; verified against a C-collation database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit dd9bf12)
…rtenException

CoreTests' all_exceptions_types convention requires every exception type Marten
defines to derive from MartenException.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 7dc7442)
The scalar select clause behind SelectMany() over a value collection read
each element with GetFieldValue<TEnum>, which Npgsql cannot do from the
integer or text column an enum is stored in, so SelectMany(x => x.Colours)
threw InvalidCastException. It now shares the enum-aware reader the
top-level Select() clause already used: an integer is converted with
Enum.ToObject and a name with Enum.Parse. The SQL is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 743c7c8)
A member of a flattened string -- n.Length, n.ToUpper() -- was looked up as a
JSON field of the string, so Where(n => n.Length == 4) after SelectMany()
rendered CAST(d.data ->> 'Length' as integer) and failed with 42883. The
ordering lookup took every expression over a value collection as the element
itself, so OrderBy(n => n.Length) silently ordered by the string.

SelectManyValueCollection now hands members of a string element to the
element's StringMember, StringMember gains Length as length(), and the
ordering lookup only returns the element for the element itself. Filters
without a string member send the same SQL as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit be61b33)
… matrix

CoreTests' criteria shape matrix pinned string .Length as refused. It is
correct now for a string member in the JSON body, and a string member
duplicated into its own column translates it to length() as well, so the
shape is correct under every configuration the matrix measures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 373d2e2)
A top-level Select() that computes its value -- x.Number % 3, x.Id.ToString(),
(long)x.Number -- went through the select visitor, which only reads members.
It visited the members inside the computation and returned nothing for them,
so rebuilding the expression failed with ArgumentNullException or "Static
method requires null instance", and a cast was read back as the member's own
type (NotSupportedException).

Such a projection is now translated with the scalar translation built for
operators after the Select() apply to the computed value, exactly as they do
after SelectMany(). A projection with no exact translation throws
BadLinqExpressionException naming it. Member, object and ExplicitSql()
projections are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 3abb02e)
Over a flattened value collection the ordering lookup took every key as the
element, so SelectMany(x => x.Values).OrderBy(v => -v) ordered by v. A key
computed from the element is now translated with the scalar translation from

After a top-level Select() only the paging, Distinct() and single-value
operators were carried over, so Select(x => x.Number).OrderBy(n => n) dropped
its ORDER BY; a Select() to the document's own type shared one usage with the
operators after it, so its OrderBy() was applied to the documents. A scalar
projection followed by OrderBy() is now computed into a CTE and ordered there,
the same way a computed projection is (#5942); anything else is refused with
BadLinqExpressionException.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit cccd161)
@jeremydmiller
jeremydmiller merged commit c333f3a into 9.0 Nov 15, 2026
31 checks passed
@jeremydmiller
jeremydmiller deleted the fix/5955-orderby-after-projection-9.0 branch November 15, 2026 19:16
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