Repository navigation
Fix negated nullable comparison in a CASE condition - #39084
Conversation
- 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
There was a problem hiding this comment.
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
Open (1)
What changed in this PR
Fixes incorrect null semantics for negated nullable comparisons in CASE expressions.
Changes:
- Corrects single-clause
CASEflattening. - 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
left a comment
There was a problem hiding this comment.
Thanks for your contribution!
|
Thanks for merging. This is a silent wrong-results bug that has been in place since 9.0 (the rule came with #34175), and |
|
@virzak Unfortunately, the risk compared to impact is currently a bit too high for this to be considered for a servicing fix. |

Fixes #39059
!(e.NullableInt <= 1) ? 0 : 1returned 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 ENDto give it C# semantics (#33757), soNOTof it isCASE WHEN x THEN FALSE ELSE TRUE END. The CASE flattening inSqlExpressionFactory.Case(#34175) then rewroteWHEN (CASE WHEN x THEN FALSE ELSE TRUE END) THEN ytoWHEN 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/NULLsimplifying tox) 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 ofWHEN [o].[OrderID] % 2 <> 0 THEN [o].[OrderID] ELSE -[o].[OrderID].🤖 Generated with Claude Code