Repository navigation
[9.0] LINQ: SelectMany over enum arrays and string members after SelectMany (backport of #5944) - #5945
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)
jeremydmiller
force-pushed
the
fix/5940-5941-selectmany-enum-string-9.0
branch
from
November 15, 2026 17:35
c8ea39a to
383d1cc
Compare
… 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)
This was referenced Nov 16, 2026
This was referenced Nov 22, 2026
Closed
This was referenced Nov 30, 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 #5944 to
9.0. Fixes #5940 and #5941 on the 9.x line. Merge after #5939, which this branch is stacked on.The master commits are cherry-picked with
-xonto the #5939 head (6dc29992f):f40d9f0c7(fromc3da70abb): LINQSelectMany()over an enum collection reads its elements (LINQ: SelectMany over an enum array throws InvalidCastException (enum read from an integer/text column) #5940)383d1cc77(from2a9d8ab80): LINQ string members afterSelectMany()translate (LINQ: string members after SelectMany over strings (e.g. Length) are translated as JSON fields (42883) #5941)846b0eca3(from373d2e2a4): the LINQ: string members after SelectMany over strings (e.g. Length) are translated as JSON fields (42883) #5941 follow-up.Lengthof a duplicated string field, plus the CoreTests shape-matrix expectationThe code applied cleanly. The only adaptation is in
docs/migration-guide.md: both notes go under "Key Changes in 9.60.0" instead of 10.0.0. #5944 has the root causes and the test design.Red then green on 9.0 (net10.0,
marten_9_v)Bug_5940_select_many_over_enum_arrayBug_5941_string_members_after_select_manyFull suites
LinqTests net10.0 passed 2536 of 2536, and CoreTests net10.0 had 911 passed, 9 skipped and 0 failed. markdownlint and cspell are clean.
I started CI by hand: https://github.com/JasperFx/marten/actions/runs/43743518562
🤖 Generated with Claude Code