Repository navigation
fix(postgresql): typed filters keep C#'s answer for null operands (#68) - #69
Merged
Merged
Conversation
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]>
This was referenced 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]>
Owner
Author
|
Merge order note: #69, #71, #73, #87 and #89 each open the 5.5.0 changelog entries with a |
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.
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)scaffoldedWHERE "Kind" <> 'federated'. That is NULL for a nullKind, so those rows were left out of the index: two rows with the same email and a nullKindwere 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):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!over a comparison with a nullable operand is pushed down (De Morgan, complemented comparisons plusOR … IS NULL) instead of wrappingNOT (…)around a value that can be NULLIS [NOT] NULLDocs: the typed-filter section of
docs/postgresql-indexes.mdstates 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 firstmigrations addafter upgrading does; the existing entry moves under### Changed.Upgrading: the first
migrations adddrops 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!=onstring?andbool?,!(a == 1 || b == 2),!(x > 5), a captured null under==and!=; agreement withToQueryString()for five predicates; a characterisation test that non-nullable operands render as before.PostgresIntegrationTests: a unique index filteredx.Kind != "federated"on PostgreSQL 18 refuses a second row with the same email and a nullKind, and still admits afederatedone.🤖 Generated with Claude Code