Skip to content

fix: cast non-string JSON members in exclusion constraint filters (#78) - #108

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/78-exclusion-json-filter
Oct 20, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/78-exclusion-json-filter

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #78

Problem

A typed exclusion constraint filter on a non-string ToJson() member scaffolded SQL that PostgreSQL rejects when the migration is applied:

b.HasExclusionConstraint(x => x.Count, x => x.Period, x => x.Doc.Rank > 5);   // Rank: int
// WHERE (("doc" ->> 'Rank') > 5)  →  42883 operator does not exist: text > integer

b.HasExclusionConstraint(ex => ex.WithEquality(x => x.Count).WithOverlaps(x => x.Period)
                                 .HasFilter(x => x.Doc.Active));               // Active: bool
// WHERE (("doc" ->> 'Active'))    →  42804 argument of WHERE must be type boolean, not type text

BuildExclusionDescriptors resolves exclusion filters with renderingVersion: 1, which reads every JSON member as text, so that existing constraints never rebuild. The index path renders the same predicate as CAST("doc" ->> 'Rank' AS integer) > 5, which applies.

Change

  • NpgsqlComplexIndexMigrationsModelDiffer: exclusion filters resolve through ResolveExclusionFilter, which flags the resolution. In ResolveUnmappedPart, a leaf that the current rendering casts (RendersAsCast: a number, boolean, uuid or enum, not a string, primitive collection, json/jsonb, bytea or date/time text fallback) takes the current rendering under that flag.
  • The rule is narrower than "every non-string leaf" (the issue's wording) on purpose. A primitive collection renders -> (jsonb) under the current rules where rendering 1 had ->> (text), and the two treat a JSON null differently, so {Doc.Tags} IS NOT NULL would change meaning. A cast keeps NULL as NULL. For a cast member, a filter that did apply under rendering 1 (IS NOT NULL, or a comparison with a string literal) therefore holds for the same rows. Both sides of the diff resolve alike, so no existing constraint gets a migration.
  • The core's ResolveFilter stays untouched (stream C's file). The flag is a field set for the duration of the call.
  • CLAUDE.md's rendering-version note names the exception. One ### Fixed line goes into the root and PostgreSQL changelogs.

Tests

  • NpgsqlJsonMemberRenderingTests.Exclusion_filters_cast_non_string_members: Rank > 5, nested Address.Zip > 0 and HasFilter(x => x.Profile.Active) render CAST(… AS integer) / #>> '{Address,Zip}' / CAST(… AS boolean). {Profile.Tags} IS NOT NULL keeps ->>.
  • Rollout_does_not_touch_exclusion_constraints now covers the new constraints too: a pre-5.4.0 snapshot against the current model produces no exclusion operations.
  • PostgresIntegrationTests.Exclusion_filter_on_non_string_json_members_applies: both constraints apply on PostgreSQL 18 without runtime wiring, and each rejects an overlapping row in its own partition.
  • Seeded with the switch disabled: the differ test fails, and the integration test fails with 42883: operator does not exist: text > integer. With the switch widened to every member, the string-member (City) and primitive-collection (Tags) assertions fail.
  • Full suite, integration included: 400 passed.

🤖 Generated with Claude Code

Exclusion constraint filters resolve JSON placeholders under rendering 1,
so that the 5.4.0 rendering change never rebuilt a constraint. Rendering 1
reads every member as text, so a typed filter on an int or bool ToJson()
member scaffolded SQL PostgreSQL rejects: ("doc" ->> 'Rank') > 5 fails with
42883, and a bare ("doc" ->> 'Active') with 42804.

A member the current rendering casts (number, boolean, uuid, enum) now
takes that rendering in exclusion filters too, on both sides of the diff.
As text it never applied in a comparison; where it did apply (IS NOT NULL,
a string literal), the cast keeps NULL as NULL and the same rows, and no
migration is produced since both sides resolve alike. Strings, primitive
collections, json, bytea and the date/time text fallbacks keep rendering 1,
since -> and ->> differ on a JSON null.

Proven by seeding: without the switch the differ test fails and the
integration test fails with 42883 on PostgreSQL 18; widening it to every
member fails the string and primitive-collection assertions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 20, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working severity/medium Inconsistency or failure in a narrow scenario package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL area/constraints Temporal (WITHOUT OVERLAPS, PERIOD) and exclusion constraints labels Oct 20, 2026
# Conflicts:
#	CHANGELOG.md
#	src/EFCore.ComplexIndexes.PostgreSQL/CHANGELOG.md
@CaffeinatedCoder
CaffeinatedCoder merged commit 4f2b89d into main Oct 20, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/78-exclusion-json-filter branch October 20, 2026 06:14
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 bug Something isn't working package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL severity/medium Inconsistency or failure in a narrow scenario

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typed exclusion-constraint filters on non-string JSON members fail at apply (text > integer)

1 participant