Skip to content

Fix negated nullable comparison in a CASE condition - #39084

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
virzak:fix/case-negated-null-semantics
Sep 26, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
virzak:fix/case-negated-null-semantics

Conversation

@virzak

@virzak virzak commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #39059

!(e.NullableInt <= 1) ? 0 : 1 returned 1 for rows where the column is NULL, where C# gives 0.

The nullability processor wraps a nullable ordered comparison as CASE WHEN x THEN TRUE ELSE FALSE END to give it C# semantics (#33757), so NOT of it is CASE WHEN x THEN FALSE ELSE TRUE END. The CASE flattening in SqlExpressionFactory.Case (#34175) then rewrote WHEN (CASE WHEN x THEN FALSE ELSE TRUE END) THEN y to WHEN NOT x THEN y, which is NULL rather than TRUE when x is NULL, undoing the compensation.

With a single WHEN clause the CASE is inverted instead: CASE WHEN x THEN z ELSE y END, which keeps a NULL x in the same branch. With more clauses, swapping would change which clause the remaining rows reach, so the nested CASE is left as is.

The positive form of the rule (THEN TRUE ELSE FALSE/NULL simplifying to x) is unaffected: a NULL x does not match either way.

Testing: new spec test with SQLite and SQL Server baselines. All SQLite query tests pass (17617), and all SQL Server query tests except those needing features my local SQL Server lacks (json and vector types, Full-Text Search). One existing baseline changed, Select_conditional_flatten_nested_tests, where the operands are non-nullable: WHEN [o].[OrderID] % 2 = 0 THEN -[o].[OrderID] ELSE [o].[OrderID] instead of WHEN [o].[OrderID] % 2 <> 0 THEN [o].[OrderID] ELSE -[o].[OrderID].

🤖 Generated with Claude Code

- The CASE flattening from dotnet#34175 simplified
  WHEN (CASE WHEN x THEN FALSE ELSE TRUE END) THEN y to WHEN NOT x THEN y,
  which is wrong when x is NULL: the nested CASE is TRUE, NOT x is NULL.
  The nullability processor produces exactly that nested CASE to give a
  nullable comparison C# semantics under a negation, so for example
  !(e.NullableIntA <= 1) ? 0 : 1 returned 1 instead of 0 for NULL.
- With a single WHEN clause the CASE is now inverted instead:
  CASE WHEN x THEN z ELSE y END. With more clauses the nested CASE is
  kept.

Fixes dotnet#39059

Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Add provider-independent coverage for the multi-clause CASE path.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
What changed in this PR

Fixes incorrect null semantics for negated nullable comparisons in CASE expressions.

Changes:

  • Corrects single-clause CASE flattening.
  • Adds relational, SQLite, and SQL Server regression coverage.
  • Updates affected SQL baselines.
File Description
test/​EFCore.SqlServer.FunctionalTests/​Query/​NullSemanticsQuerySqlServerTest.cs Adds SQL Server baseline coverage.
test/​EFCore.SqlServer.FunctionalTests/​Query/​NorthwindSelectQuerySqlServerTest.cs Updates the affected SQL baseline.
test/​EFCore.Sqlite.FunctionalTests/​Query/​NullSemanticsQuerySqliteTest.cs Adds SQLite baseline coverage.
test/​EFCore.Relational.Specification.Tests/​Query/​NullSemanticsQueryTestBase.cs Adds regression test coverage.
src/​EFCore.Relational/​Query/​SqlExpressionFactory.cs Corrects nullable CASE inversion logic.

- The negated nullable comparison as a later WHEN clause: without the
  guard the nested CASE becomes NOT x, which sends a NULL row to the
  ELSE instead of that clause, returning 3 where C# gives 2.

Generated with Claude Code
@AndriySvyryd AndriySvyryd self-assigned this Sep 26, 2026

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit 7ae0f8f into dotnet:main Sep 26, 2026
19 checks passed
@virzak

virzak commented Sep 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for merging. This is a silent wrong-results bug that has been in place since 9.0 (the rule came with #34175), and release/11.0 still carries it. Given 11.0 hasn't GA'd yet, is this something that could be considered for 11.0, and possibly a 10.0.x patch? The change is one branch in SqlExpressionFactory.Case with spec tests, though it does alter the generated SQL for the single-clause case, so I understand if the SQL churn is the concern.

@AndriySvyryd

Copy link
Copy Markdown
Member

@virzak Unfortunately, the risk compared to impact is currently a bit too high for this to be considered for a servicing fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Negated nullable comparison in a conditional returns wrong results

3 participants