Skip to content

fix: a predicate over a null range, range set or value set is false - #371

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/355-null-predicates-false
Oct 24, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/355-null-predicates-false

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #355
Refs #350

Rule (c), decided on #350: a Boolean predicate with a null range, range set or value set on either side is false, and its negation true, in memory and in SQL. This PR is the predicate half for main. #350 stays open for its 11.0.0-only value-producing half (ArgumentNullException from Intersect, Gap, Merge, Union, Except). The 10.x port is #357.

Problem

In SQL (#355): a predicate over a NULL column was NULL, so a filter dropped the row whether the predicate was negated or not. EF Core's own translations treat a predicate over NULL as false and keep the row under a negation (!b.Name.StartsWith("a") → NOT LIKE … OR "Name" IS NULL; string[] alike). The package even disagreed with itself: a value set's !IsEmpty kept the NULL row, its !Contains dropped it. A projected predicate over a NULL row threw InvalidOperationException when read into bool.

In memory (#350): a null range answered most predicates false, but not all. Contains(Empty), IsContainedBy((,)) and (,).Contains(null) returned true. Contains(T) threw, and so did a null RangeSet operand. RangeSet's own predicates threw or returned true for a null operand. In total, 1105 calls across the 13 range types didn't answer false.

Change

  • Memory, RangeExtensions and RangeSet: a null guard on every predicate that didn't already answer false. Block bodies get a first statement, expression bodies x is not null && …, and the members that delegate to RangeSet inherit its guard. Value set predicates keep their deliberate contract: they throw for a null set (ThrowIfNull(other); a null receiver fails on Values).
  • SQL, both method-call translators: Translate wraps every bool translation with x IS NOT NULL AND <predicate>, for each range, multirange or value set operand (FalseOverNull). EF Core's nullability processing drops the check for a required column and a non-null parameter, and writes x IS NULL OR NOT (…) under a negation. That shape was measured identical on EF Core 11 RC, 10.0.0 and 10.0.13 with Npgsql 10.0.3, in A predicate over a NULL range, RangeSet or value set column is NULL in SQL, so its negation drops the row EF Core's own translations keep #355. CompareTo never reaches the translator: EF Core rewrites it into a comparison and A NULL range column now matches CompareTo(null) > 0 in a query; Comparer treats null vs null as equal #261's rule applies. Equality is EF Core's own.
  • A required column's SQL is unchanged. Every existing pin for one passes untouched. The eight captured-list pins gain w."Value" IS NOT NULL AND, because EF Core treats a VALUES or WHERE FALSE element column as nullable; a null in the list now answers false in SQL as in memory.
  • docs/ranges.md (under "Ask questions about a range") and docs/efcore.md (under "Query ranges with LINQ"): the rule, quoted by claims R49 and E71.
  • docs/migration.md: a bullet and ### Check predicates over a null range or a NULL column under ## Migration from v10.x, with before/after tables for memory and SQL.
  • Changelogs: a Ranges: line (root, core) and an EF Core: line (root, EF Core, EF Core NodaTime) under ### Breaking in [11.0.0-preview.1].
  • test/oracles/candidates.json: A null range answers some extensions in memory and throws on others, and the answers it gives disagree with PostgreSQL on a NULL column #350's seven predicate candidates reported candidate fixed and are removed. Its four value-producing ones stay for the 11.0.0 half.

Tests

Local, SDK 11.0.100-rc.2.26504.105, Docker (OrbStack).

  • RangeNullPredicateContractTests (conventions, new, registered as an oracle): it discovers every public bool member of RangeExtensions for every range type of both libraries, and every Boolean RangeSet instance member taking a range or set. It calls each with null on either side and both, against every shape and set probe. Before the change, 1105 calls failed; now none do. Seed: range-predicate-answers-on-a-null.patch.
  • ExecutedQueryTests.EveryPredicate_OverANullableColumn_IsFalseForTheNullRow (new): it discovers every bool predicate of RangeExtensions, RangeSet and ValueSetExtensions, about 140 that translate. Each runs over a nullable DateRange, RangeSet<DateRange, DateOnly> and StringSet column, with the column on either side, as Where(f), Where(!f) and Select(f), against memory over the loaded rows. Where memory throws on the NULL row, the expected answer is false. Seeds range-predicate-over-a-null-column-is-null.patch and value-set-predicate-over-a-null-column-is-null.patch, each the wrapper removed from one translator; with them the sweep fails on 175 and 21 results.
  • NullOperandTranslationTests (new) pins the SQL: a required column unchanged (NOT (r."Period" && @window)), and a nullable range, multirange and value set column with IS NULL OR NOT (…), IS NOT NULL AND … and the projection.
  • PredicatesOverANullableColumn_SelectTheRowsMemorySelects (from test: register #349 and #350 as ExecutedQueryTests candidates #352) reported exactly A null range answers some extensions in memory and throws on others, and the answers it gives disagree with PostgreSQL on a NULL column #350's seven predicate candidates fixed, and nothing else.
  • Seeds: the stranded range-against-set-reads-first-element-for-upper-bound.patch is refreshed, its context taking the new guard, and both its oracles still catch it. Every seed that patches a changed file applies and is caught (dotnet run tools/oracles.cs seeds, listed in the run below).
  • Full suite on postgres:16-alpine: all eight projects pass (core 1399, NodaTime 239, EF 297, EF NodaTime 83, ASP.NET Core 92, conventions 409, integration 288, infinity-disabled 4).

🤖 Generated with Claude Code

…355, #350)

Rule (c) from #350, the predicate half: a Boolean predicate with a null range,
range set or value set on either side is false, and its negation true, in memory
and in SQL, as EF Core answers for its own types.

Memory: null guards on every RangeExtensions and RangeSet predicate that did not
already answer false. Contains(Empty), IsContainedBy((,)) and (,).Contains(null)
returned true; Contains(T), a null RangeSet operand and RangeSet's own predicates
with a null operand threw. Value set predicates keep throwing for a null set.

SQL: both method-call translators wrap every bool translation as
x IS NOT NULL AND <predicate> for each range, multirange or value set operand.
EF Core drops the check for a required column and a non-null parameter and
writes x IS NULL OR NOT (...) under a negation, so a negated predicate keeps the
NULL row and a projected one reads false. CompareTo never reaches the
translator, and equality is EF Core's own.

RangeNullPredicateContractTests (new oracle) and
ExecutedQueryTests.EveryPredicate_OverANullableColumn_IsFalseForTheNullRow
discover every predicate; three seeds keep the reverts. #350's seven predicate
candidates are removed as fixed. NullOperandTranslationTests pins the SQL; the
captured-list pins gain the list element's null check. Claims R49 and E71.

Found-By: US
Defect-Class: SQL

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Oct 24, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred breaking Changes an answer or a signature — needs a major area/ranges Range unions, variants and the shape engines area/range-set RangeSet and multirange algebra area/value-sets Value set families and wrapper arities package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/core Affects CodoMetis.ValueRanges package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime severity/medium Inconsistency or failure in a narrow scenario labels Oct 24, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 1169d2d into main Oct 24, 2026
11 of 15 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/355-null-predicates-false branch October 24, 2026 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/range-set RangeSet and multirange algebra area/ranges Range unions, variants and the shape engines area/value-sets Value set families and wrapper arities breaking Changes an answer or a signature — needs a major bug Something isn't working package/core Affects CodoMetis.ValueRanges package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime severity/medium Inconsistency or failure in a narrow scenario silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A predicate over a NULL range, RangeSet or value set column is NULL in SQL, so its negation drops the row EF Core's own translations keep

1 participant