Repository navigation
[9.0] LINQ: OrderBy() after a projection orders the projected values (backport of #5961) - #5962
Merged
Merged
Conversation
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)
This was referenced Nov 16, 2026
This was referenced Nov 22, 2026
Open
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 #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.3b3887b67was cherry-picked with-xfromcccd16137. Two things are adapted for 9.0:using Marten.Exceptions;, becauseBadLinqExpressionExceptionis not in scope there on 9.0 without itThe 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)Bug_5955_order_by_after_projectionFull suites on this branch
markdownlint and cspell are clean.
CI, started by hand: https://github.com/JasperFx/marten/actions/runs/43744909547
🤖 Generated with Claude Code