Skip to content

[9.0] LINQ: computed top-level Select() projections translate or are refused (backport of #5952) - #5954

Merged
jeremydmiller merged 8 commits into
9.0from
fix/5942-top-level-computed-select-9.0
Nov 15, 2026
Merged

jeremydmiller merged 8 commits into
9.0from
fix/5942-top-level-computed-select-9.0

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Backport of #5952 to 9.0. Fixes #5942 on the 9.x line. Merge after #5939 and #5945: this branch is stacked on #5945's head (846b0eca3).

b1bf6a5cc is cherry-picked with -x from 3abb02e7e. The code applied cleanly. The only adaptation is the migration note, which goes under "Key Changes in 9.60.0". #5952 has the root cause, the SQL and the test design.

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

Tests Before (#5945 head) After
Bug_5942_computed_top_level_select 166 failed / 6 passed of 172. The 5 unchanged-SQL checks pass, as on master. 172 / 172

Full suites on this branch

Suite Result
LinqTests net10.0 2708 / 2708
CompiledQueryTests net10.0 41 / 41, including the new compiled computed-select shape
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/43743750400

🤖 Generated with Claude Code

jeremydmiller and others added 8 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)
@jeremydmiller
jeremydmiller merged commit 5e061a7 into 9.0 Nov 15, 2026
31 checks passed
@jeremydmiller
jeremydmiller deleted the fix/5942-top-level-computed-select-9.0 branch November 15, 2026 19:15
jeremydmiller added a commit that referenced this pull request Nov 15, 2026
…5949 x #5942)

#5942 (merged on 9.0 as #5954) sends a top-level projection it classifies as
computed through the scalar translator and refuses it when that fails. Every
BinaryExpression counted as computed, which took in #5949's array indexes:
Select(x => x.Values[1]), Select(x => x.Grid[1][0]) and Select(x => x.Grid[1])
were refused instead of reaching the select visitor that translates them.
An ArrayIndex is now not a computed projection; arithmetic over one still is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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