Skip to content

fix(postgresql): typed filters keep C#'s answer for null operands (#68) - #69

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/68-typed-filter-null-semantics
Oct 19, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/68-typed-filter-null-semantics

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #68

Problem

A typed filter translated comparisons with SQL's three-valued logic, not the C# semantics of the lambda. On a string? Kind, HasComplexIndex(x => x.Email, x => x.Kind != "federated", isUnique: true) scaffolded WHERE "Kind" <> 'federated'. That is NULL for a null Kind, so those rows were left out of the index: two rows with the same email and a null Kind were both accepted, although the lambda, and EF Core's own query for it ("Kind" <> 'federated' OR "Kind" IS NULL), treat them as covered. A captured variable holding null rendered "Name" = NULL, an index that holds no rows. Both scaffold and apply without an error. Present since typed filters shipped in 5.2.0.

Change

NpgsqlLinqIndexTranslator.TranslatePredicate, used by every typed filter (indexes, expression indexes, HasFilter<T>, AddComplexIndexFilter<T>, exclusion constraints):

  • an operand can be null when it is a Nullable<T> or a reference type not annotated non-nullable (NullabilityInfoContext; oblivious counts as nullable), or is computed from one
  • ==/!= with nullable operands use EF Core's null compensation, character for character
  • a ! over a comparison with a nullable operand is pushed down (De Morgan, complemented comparisons plus OR … IS NULL) instead of wrapping NOT (…) around a value that can be NULL
  • a parameter-free operand that evaluates to null is IS [NOT] NULL
  • operands that cannot be null translate exactly as before, so those filters and their indexes do not churn

Docs: the typed-filter section of docs/postgresql-indexes.md states the rule and its limit (nullability is read from the C# declaration). Changelogs: the 5.5.0 entry opens with ### Changed answers, its row linking #68 and saying what the first migrations add after upgrading does; the existing entry moves under ### Changed.

Upgrading: the first migrations add drops and re-creates each index or exclusion constraint whose typed filter changed. A unique index re-created over rows it used to leave out fails if those rows hold duplicates, which are exactly the duplicates it should have refused.

Tests

  • NpgsqlTypedFilterTests: explicit filters for != on string? and bool?, !(a == 1 || b == 2), !(x > 5), a captured null under == and !=; agreement with ToQueryString() for five predicates; a characterisation test that non-nullable operands render as before.
  • PostgresIntegrationTests: a unique index filtered x.Kind != "federated" on PostgreSQL 18 refuses a second row with the same email and a null Kind, and still admits a federated one.
  • verify-the-guard: with the translator change reverted, the three behaviour tests fail and the characterisation test passes.
  • Full suite 376/376 locally, integration included.

🤖 Generated with Claude Code

A typed filter translated comparisons with SQL's three-valued logic.
x => x.Kind != "federated" on a string? scaffolded
"Kind" <> 'federated', which is NULL for a null Kind, so those rows fell
out of the partial index: a unique index "among rows not federated"
accepted two duplicates with a null Kind, while the lambda, and EF Core's
query for it, treat both as covered. A captured variable holding null
rendered "Name" = NULL, an index of no rows. Both scaffolded and applied
without a word.

Comparisons whose operand can be null (a Nullable<T>, or a reference
type not annotated non-nullable) are now written as EF Core writes the
same lambda: a <> v OR a IS NULL, EF's two-sided forms, and a negation
pushed down to the comparison instead of NOT around a NULL-able value.
A parameter-free operand that evaluates to null is a null check.
Operands that cannot be null translate exactly as before, so those
indexes do not churn.

Verified by reverting the translator: the three new tests (explicit
filters, agreement with ToQueryString() for five predicates, and a live
PostgreSQL 18 unique index refusing a duplicate with a null Kind) fail;
the characterisation test for non-nullable operands passes either way,
by design.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 19, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred severity/high Wrong results, crash or failed writes in a realistic scenario package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL area/indexes Complex-property, composite, unique and filtered indexes area/constraints Temporal (WITHOUT OVERLAPS, PERIOD) and exclusion constraints labels Oct 19, 2026
CaffeinatedCoder added a commit that referenced this pull request Oct 19, 2026
Typed filters had the same null-semantics hole as the guard predicate
4.2 planned; #68 (PR #69) fixes TranslatePredicate for 5.5.0, so guards
need no translator change of their own. Effort table and open questions
updated accordingly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder

Copy link
Copy Markdown
Owner Author

Merge order note: #69, #71, #73, #87 and #89 each open the 5.5.0 changelog entries with a ### Changed answers row (and move the existing bullet under ### Changed). They are independent otherwise; after the first merges, I'll rebase the rest so the rows sit under one heading. #85 adds a plain bullet and #75 edits the same entry's wording.

@CaffeinatedCoder
CaffeinatedCoder merged commit 00e7b82 into main Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/68-typed-filter-null-semantics branch October 19, 2026 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/constraints Temporal (WITHOUT OVERLAPS, PERIOD) and exclusion constraints area/indexes Complex-property, composite, unique and filtered indexes bug Something isn't working package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL severity/high Wrong results, crash or failed writes in a realistic 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.

Typed filters use SQL null semantics: != and ! on a nullable operand drop NULL rows, a captured null renders = NULL

1 participant