Repository navigation
[9.0] LINQ: index into document arrays in OrderBy, Select and jagged Where (backport of #5953) - #5956
Merged
Merged
Conversation
…ixes #5949) An index into an array or list on the document translated only in a Where() comparison with a single index. OrderBy(x => x.Values[1]) was refused ("Invalid OrderBy() expression": MemberFinder treats ArrayIndex as invalid), Select(x => x.Values[1]) threw ArgumentNullException ('array') because SelectorVisitor rebuilt the index node from null children, and a jagged x.Grid[1][0] failed everywhere: SimpleExpression rebuilt the inner index node the same way, and ChildDocumentAtIndex could not index again. - ChildDocumentAtIndex indexes an element that is itself an array: a scalar element through the same SimpleCastMember/StringMember factories value collections use, a complex one as another ChildDocumentAtIndex. - OrderBy/ThenBy, Select (scalar, anonymous type, record) and bool predicates resolve a chain containing an index through the SimpleExpression walk Where() comparisons already used. Chains without an index resolve exactly as before; the SQL for 45 such shapes is byte-identical. - A negative index throws BadLinqExpressionException instead of becoming a negative jsonb index, which PostgreSQL reads from the end of the array. - A bool[] / List<bool> on the document could not be resolved at all (BooleanMember read the member type of an ElementMember); it can now, and Any(f => f) over one registers a filter instead of throwing. - An indexed string element's case-insensitive ordering lowers the real locator, not the placeholder the constructor built. Docs: "Reading an Element by Position" in child-collections.md, and a migration note for the negative-index behavior change (under 9.60.0 on 9.0). 9.0 adaptation: BadLinqExpressionException is Marten.Exceptions on 9.0 (no union members either), and the migration note goes under 9.60.0. (cherry picked from commit 6d2c14e) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 15, 2026
…y-indexing-9.0 # Conflicts: # docs/migration-guide.md
…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>
This was referenced Nov 15, 2026
Merged
This was referenced Nov 16, 2026
This was referenced Nov 16, 2026
This was referenced Nov 20, 2026
Open
This was referenced Nov 27, 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 #5953 to
9.0. Fixes #5949 on 9.x.Cherry-picked with
-xfrom6d2c14e05. The 9.0 adaptations:BadLinqExpressionExceptionisMarten.Exceptions.BadLinqExpressionExceptionon 9.0.usingis dropped inWhereClauseParser.CI (started by hand): https://github.com/JasperFx/marten/actions/runs/43758805993 (head
80b3e0cc6). The stack merges currentorigin/9.0(through #5956), which brings in #5951 (HiLomt_get_next_hi) and #5980 (a docs-only change). With those merged in, the top of the chain (#5971) passes full LinqTests net10.0 (3036/3036) and DocumentDbTests'Writing.Identity.Sequencesnamespace (33/33). #5956 also carries the one-lineisComputedProjectionfix that keeps an array index in a top-levelSelect()on the select visitor.Root cause
An index into an array or list on the document translated in only one place: a
Where()comparison with a single index, throughSimpleExpression. Everywhere else it failed:OrderBy(x => x.Values[1])Invalid OrderBy() expressionOrderingresolves throughMemberFinder, which recordsArrayIndexas an invalid node. AList<T>indexer orx.Children[0].Namesilently lost its index, so the query ordered by the wrong value.Select(x => x.Values[1])ArgumentNullException ('array')SelectorVisitorletExpressionVisitorvisit the index's children, which returnnull, and then rebuild theArrayIndexnode from them.Where(x => x.Grid[1][0] == 4)ArgumentNullException ('array')SimpleExpressionrebuilt the inner index the same way. Past that,ChildDocumentAtIndex(the elementx.Grid[1]) could not be indexed again.Select(x => new { C = x.Grid[1][0] })SelectParserresolved onlyx.Grid[1]and re-applied the outer index, so it readx.Grid[0].Where(x => x.Flags[1])withbool[] Flagsbool[]/List<bool>on a document could be resolved at all.BooleanMemberread the member type of the collection'sElementMember, which has none.The fix
ChildDocumentAtIndexcan now index an element that is itself an array.SimpleCastMember/StringMemberfactories value collections use (nowForIndexedElement), on the element's ownjsonblocator:CAST(d.data -> 'grid' -> 1 ->> 0 as integer).ChildDocumentAtIndex, sox.ChildGrid[1][0].Scoreand three-deep indexes work.MemberType, soSelect(x => x.Children[0])andSelect(x => x.Grid[1])deserialize the element.HasElementIndex()andMemberForIndexedChain()route any member chain that contains an array index, aList<T>indexer orElementAt()through theSimpleExpressionwalk thatWhere()comparisons already use. That walk now visits only the array side of an index rather than rebuilding the node. The callers are:Ordering(OrderBy/ThenBy)SelectorVisitor(scalarSelect, including aList<T>indexer andElementAt)SelectParser(anonymous types and records)WhereClauseParser(bareboolelements, such asWhere(x => x.Flags[1]))Any/Contains/Count/Length,ToLower, case-insensitive ordering, anonymous types, records,Distinct). The SQL is byte-identical on the base and on this branch.BadLinqExpressionException. It used to become a negativejsonbindex, which PostgreSQL reads from the end of the array, sox.Values[-1] == 3silently compared the last element.null, as it always did inWhere(). A comparison against it does not match, it sorts last ascending, and aSelectreturnsnullordefault. This is documented and tested.boolcollections can now be queried, andAny(f => f)over one registers a filter instead of throwingNullReferenceException.child-collections.md, and a 9.60.0 migration note. markdownlint and cspell report 0 issues.Tests
Bug_5949_indexing_document_arrayshas 81 cases, each checked against LINQ to Objects over the same six documents.int[],List<int>,ElementAt,string[],List<string>,bool[],double[],int[][],List<List<int>>,string[][],int[][][], and members of indexed child documents (Children[1].Score,ChildList[0].Name,ChildGrid[1][0].Score).&&/||/!, bareboolelements, andContains/Anyover abool[].ThenByDescendingon an index.Where,OrderByandSelect, and negative-index refusal inWhere,OrderByandSelect.Red, then green on 9.0 (net10.0,
marten_9_y)a2967fe9e, fix reverted): 69 of 81 failed.markdownlint over
docs/reports 0 issues on this branch, as it does on9.0. cspell reports 0.Full suite on this branch
marten_9_y: 2230 / 2230No
CollectionUsage/SelectManycode is touched.🤖 Generated with Claude Code