Skip to content

fix(postgresql): coalesce a nullable operand of a typed concatenation - #123

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/94-concat-coalesce
Oct 20, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/94-concat-coalesce

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #94

Stacked on #122 (#80), which is stacked on #121 (the parity sweep); merge those first.

Problem

C# concatenates null as an empty string; PostgreSQL's || returns NULL. A typed expression index or filter over x => x.Code + x.Suffix with string? Suffix scaffolded ("Code" || "Suffix"), where EF Core's query for the same lambda writes "Code" || COALESCE("Suffix", ''). The index held NULL for every row without a suffix, so it served no EF Core query over the lambda, and a unique one checked none of those rows: two rows with code A and no suffix both went in. It scaffolded and applied cleanly.

Change

  • NpgsqlLinqIndexTranslator: each operand of a string concatenation that can be null is wrapped in coalesce(…, '') (a non-string one cast to text first, as COALESCE needs one type), a captured null becomes '', and a concatenation is never null, so a filter that compares one adds no null check. Operands that cannot be null render as before.
  • Rendering version: none needed, so 5.5.0 stays at 3. The template is stored at the declaration, so a snapshot written before holds the old one and the next migrations add drops and re-creates the index once (Nullable_concatenation_recreates_once).
  • waivers.txt: the Typed expression indexes: string concatenation with a nullable operand differs from EF Core (COALESCE) #94 block is gone. Its last 20 rows concatenate a non-string column (x.NStr + x.Int), which now matches EF Core except for EF Core's ::text, so they join the existing block for that.
  • Changelog: a ### Changed answers row (root and PostgreSQL) and the intro now counts six; docs/upgrading.md: "Concatenation with a nullable operand" under Changed answers in 5.5.0; docs/postgresql-indexes.md names the rule.

Tests

🤖 Generated with Claude Code

@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 20, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred stacked Base is another open PR; merge in order, CI does not run until it targets main severity/medium Inconsistency or failure in a narrow scenario package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL area/expression-indexes Expression, typed LINQ and JSON member indexes labels Oct 20, 2026
…#94)

C# concatenates null as an empty string; PostgreSQL's || returns NULL.
x => x.Code + x.Suffix on a string? Suffix rendered ("Code" || "Suffix"),
NULL for every row without a suffix: the index served no EF Core query
over the lambda (EF Core writes COALESCE("Suffix", '')), and a unique
index checked none of those rows. Silent: it scaffolded and applied.

An operand that can be null is wrapped in coalesce(…, ''), a non-string
one cast to text first, and a captured null is ''. A concatenation is
therefore never null, so a filter comparing one needs no null check.

The template is stored at the declaration, so a snapshot written before
holds the old template and the next migrations add re-creates the index
once. No rendering version is involved; 5.5.0 stays at 3.

Verified with the translator change reverted: the parity sweep fails 71
rows (this change removes them from waivers.txt), and the new tests fail,
including a live unique index that admitted two rows with code A and no
suffix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder
CaffeinatedCoder force-pushed the fix/80-char-hasvalue-scalar-members branch from 472139d to cfa3ed0 Compare October 20, 2026 06:35
@CaffeinatedCoder
CaffeinatedCoder changed the base branch from fix/80-char-hasvalue-scalar-members to main October 20, 2026 07:01
@CaffeinatedCoder
CaffeinatedCoder merged commit e7b375e into main Oct 20, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/94-concat-coalesce branch October 20, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/expression-indexes Expression, typed LINQ and JSON member indexes bug Something isn't working package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL severity/medium Inconsistency or failure in a narrow scenario silent Answers wrongly without throwing — fixes go in the next major, never deferred stacked Base is another open PR; merge in order, CI does not run until it targets main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typed expression indexes: string concatenation with a nullable operand differs from EF Core (COALESCE)

1 participant