Skip to content

feat: translate Contains over a captured collection of ranges - #77

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/list-contains-range
Oct 17, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/list-contains-range

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #71

Stacked on #75. This branch is cut from #75's branch (fix/list-contains-value-set) because it extends the query interceptor code #75 adds. Until #75 merges, this PR also shows #75's commit (d076560). Only 69bdcaa is new here.

Problem

wanted.Contains(r.Seats) over a captured collection of ranges failed for every range family. The full matrix is in the issue comment.

r."Seats" = ANY (@wanted)
PostgresException 42809: op ANY/ALL (array) requires array on right side

TimeRange failed with InvalidCastException instead. Npgsql receives the collection as an untyped NpgsqlRange<T>[] and infers it as a multirange. Measured directly: pg_typeof($1) is int4multirange, and int4range[] once DataTypeName is set. timerange has no inferred type at all. A constant list, EF.Constant and an inline new[] { … } already worked, and so did a collection of RangeSet values.

Change

  • The interceptor covers ranges too. feat: translate Contains over a captured collection of value sets #75's collection expander now also sends a captured range collection one parameter per range, IN (@p1, @p2, …). Each parameter is typed by the range's own mapping, so the custom timerange works as well. This is the SQL the constant and inline forms already produce.
  • Why not type the single parameter as int4range[]? That would need two Npgsql-internal types (NpgsqlArrayTypeMapping, INpgsqlTypeMapping), and it still couldn't name timerange.
  • EF.Parameter(wanted).Contains(…) over ranges now throws in translation and says to remove it.
  • Kept to queries that failed. A range collection is only switched when a Contains in the query reads it. The revert proof showed that Any/Count alone over a captured range list ran before, through unnest of the multirange-typed parameter. That path merges overlapping ranges and drops empty ones, so it is silently wrong (measured: Count(Overlaps) 1 where memory says 2, Any(IsEmpty) false where memory says true). Switching it would change an existing answer, so under the release rule it is filed as Any/Count over a captured list of ranges silently merge overlapping ranges and drop empty ones #76 (silent, 10.0.0), and the fix there is to drop this limit. The docs give the EF.Constant workaround, and a live test checks it.
  • Docs: a new section, Match a column against a list of ranges. Claims E37 and E38 are in test/oracles/claims.json. The ### Added lines are in the root and EF Core changelogs. No candidate was keyed to list.Contains(rangeColumn) fails with 42809 'op ANY/ALL (array) requires array on right side' #71.

Tests

  • RangeCollectionTranslationTests (no database) checks the whole WHERE clause for:
    • a list, an array and the custom timerange;
    • NOT IN;
    • Any beside a Contains (VALUES) and Any alone (unchanged unnest);
    • the EF.Parameter refusal, and EF.Parameter with Any alone (unchanged);
    • EF.Constant, and a RangeSet collection (unchanged = ANY).
  • RangeCollectionContainsTests (live PostgreSQL) covers all 11 range families: six BCL ranges, TimeRange and four NodaTime ranges. Each runs as an array, a List and an IEnumerable, with its negation, against the in-memory answer. Equal ranges are built differently: [1,6) against [1,5], another decimal scale, another offset. It also runs the issue's query, an empty list, Contains + Count over overlapping ranges, the EF.Constant workaround, the EF.Parameter refusal and the unchanged RangeSet collection.
  • Revert proof (feat: translate Contains over a captured collection of value sets #75's interceptor restored, so only the range fix is gone):
    • 7 of 8 live tests fail, six with 42809 and the EF.Parameter test catching PostgresException.
    • 6 of 10 translation tests fail.
    • The tests that pass either way pin unchanged behaviour.
  • Seeded the other way: dropping the Contains-only limit fails AnyAlone_OverACapturedList_IsUnchanged and AnyAlone_OverEFParameter_IsUnchanged, so the 9.1.0 scope is guarded.
  • dotnet test on the whole solution, integration included: green.

🤖 Generated with Claude Code

CaffeinatedCoder and others added 2 commits October 17, 2026 20:13
wanted.Contains(r.Tags) over a captured list, array or IEnumerable of
value sets went out as one array parameter (r."Tags" = ANY (@wanted)).
A value set is already an array, so that parameter is an array of
arrays, which PostgreSQL does not have, and Npgsql threw
InvalidCastException writing T[][] for every family.

The query interceptor now switches such a parameter to EF Core's
MultipleParameters mode: one parameter per set, so Contains runs as
IN (@p1, @p2, ...) and Any/Count read from VALUES. Equality of canonical
arrays is set equality, so the rows match the in-memory answer. Only a
collection whose effective mode is Parameter is changed; Constant and
EF.Constant keep their literals. EF.Parameter(wanted), which asks for
the impossible single parameter, throws in translation and says to
remove it. A server-side Union stays refused under IN by
ValueSetSqlGuard.

Closes #46

Found-By: RV
Defect-Class: SQL
Co-Authored-By: Claude Opus 5.5 <[email protected]>
wanted.Contains(r.Seats) over a captured list, array or IEnumerable of
ranges went out as one parameter, r."Seats" = ANY (@wanted). Npgsql
received an untyped NpgsqlRange<T>[] and inferred it as a multirange,
so PostgreSQL failed with 42809 (op ANY/ALL (array) requires array on
right side), and the custom timerange threw InvalidCastException.

The interceptor's collection expander from #46 now covers ranges too:
a captured range collection that a Contains reads is sent one
parameter per range, IN (@p1, @p2, ...), each typed by the range's own
mapping. EF.Parameter(wanted) as the source of Contains throws in
translation and says to remove it. RangeSet collections already bound
as one multirange array and are left alone.

Any/Count over a range collection with no Contains ran before, through
unnest of the multirange-typed parameter, which merges overlapping
ranges and drops empty ones. Switching those would change an answer,
so they are left as they are and filed as #76 for 10.0.0.

Closes #71

Found-By: CA
Defect-Class: SQL
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 9.1.0 milestone Oct 17, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working area/ranges Range unions, variants and the shape engines package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL severity/medium Inconsistency or failure in a narrow scenario stacked Base is another open PR; merge in order, CI does not run until it targets main labels Oct 17, 2026
@CaffeinatedCoder CaffeinatedCoder removed the stacked Base is another open PR; merge in order, CI does not run until it targets main label Oct 17, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit f2689a1 into main Oct 17, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/list-contains-range branch October 17, 2026 18:26
CaffeinatedCoder added a commit that referenced this pull request Oct 17, 2026
CapturedCollectionProbeTests (integration) adds the captured collection to
the EF Core operand-position axis. It reads every discovered range and value
set type through:

- a captured array, List, HashSet and IEnumerable
- six lists that tell a list from the multirange it would collapse into
  (empty, overlapping, adjacent, with an empty value, duplicate, every shape)
- Contains, Any and Count, alone and beside a Contains that reads the same
  collection

The oracle is the same projection compiled and run over the rows as loaded.
The lists hold loaded values, so the store's microsecond truncation does not
count as a disagreement.

Two families, registered as candidates:

- #76, measured wider: a range collection that no Contains reads also fails
  loudly. Any(==) is rewritten to = ANY and fails with 42809, and timerange
  cannot be written at all.
- #137 (new): Any/Count with a predicate over an empty collection throws
  "Empty collections are not supported as inline query roots". It affects
  every value set type, and ranges beside Contains.

The sweep is registered in oracles.json and named in the heldBy of E36 and
E37. It is seeded with set-collection-sent-as-one-parameter (the #46 defect),
which tools/oracles.cs reports CAUGHT. Reverting #77 fails it too (42809 on
every Contains). axes.md gains the value. It runs in about 5 seconds.

Closes #136

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/ranges Range unions, variants and the shape engines bug Something isn't working package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL severity/medium Inconsistency or failure in a narrow scenario

Projects

None yet

Development

Successfully merging this pull request may close these issues.

list.Contains(rangeColumn) fails with 42809 'op ANY/ALL (array) requires array on right side'

1 participant