Skip to content

Propagate column nullability from failed CASE tests to later clauses - #39660

Merged
AndriySvyryd merged 3 commits into
dotnet:mainfrom
Aykuttonpc:fix-case-null-propagation
Nov 19, 2026
Merged

AndriySvyryd merged 3 commits into
dotnet:mainfrom
Aykuttonpc:fix-case-null-propagation

Conversation

@Aykuttonpc

Copy link
Copy Markdown
Contributor

Fixes #36293

Problem

SqlNullabilityProcessor.VisitCase passes what a WHEN test tells about column nullability to that clause's THEN result, but not to the clauses after it. When a test such as x IS NULL fails, x is known to be non-null in all the later WHEN tests, results and the ELSE result. Without that, comparisons there get the full null-semantics expansion, which SQL Server can't optimize away (see the plans in the issue):

ss.Set<Entity>().Where(e => e.NullableIntA == null ? e.IntB == e.NullableIntB : e.NullableIntC == e.NullableIntA)

Before:

WHEN [e].[NullableIntC] = [e].[NullableIntA] OR ([e].[NullableIntC] IS NULL AND [e].[NullableIntA] IS NULL) THEN CAST(1 AS bit)

After:

WHEN [e].[NullableIntC] = [e].[NullableIntA] THEN CAST(1 AS bit)

Change

  • After each WHEN clause of a searched CASE, the null checks in its test are recorded as facts for the clauses that follow:
    • a failed x IS NULL means x is non-null;
    • a failed x IS NOT NULL means x is null;
    • a failed OR means every one of its operands failed.
  • Other tests contribute nothing. When they fail they can be either false or null, so nothing can be concluded.
  • While there, this fixes the null-value column list being restored with the non-nullable columns count after each clause.

Tests

Added to NullSemanticsQueryTestBase, with SQL Server baselines:

  • Nullable_column_info_propagates_to_else_of_conditional
  • Nullable_column_info_propagates_to_else_of_conditional_in_predicate
  • Nullable_column_info_propagates_to_later_clauses_of_conditional

Existing baselines that get simpler SQL as a result:

  • Is_null_on_column_followed_by_OrElse_optimizes_nullability_conditional (and its _multiple, _nested, _with_setup variants)
  • LeftJoin_with_Any_on_outer_source_and_projecting_collection_from_inner (SQL Server and temporal)

When a WHEN test made of null checks fails, the checked columns' nullability
is known for the following WHEN clauses and the ELSE result, so comparisons
there no longer need the full null-semantics expansion.

Also restore the null-value columns list with its own count after each clause.

Fixes dotnet#36293

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.

🟡 Changes recommended

A moderate nullability bug remains because failed IS NOT NULL facts are not propagated to later WHEN clauses, and the missing regression coverage leaves this behavior unverified.

2 open issues
What changed in this PR

This pull request improves relational SQL nullability propagation across searched CASE clauses, reducing redundant null-semantics compensation in later branches and updating affected query baselines. The failed IS NOT NULL propagation path still does not affect later clause processing and lacks regression coverage.

Changes:

  • Propagates nullability facts from failed WHEN predicates to subsequent clauses.
  • Corrects restoration of null-value column tracking between clauses.
  • Adds shared regression tests and updates SQL Server, SQLite, temporal, and navigation query baselines.
File Description
test/​EFCore.SqlServer.FunctionalTests/​Query/​TemporalComplexNavigationsCollectionsSharedTypeQuerySqlServerTest.cs Updates temporal SQL Server baselines affected by simplified CASE nullability handling.
test/​EFCore.SqlServer.FunctionalTests/​Query/​NullSemanticsQuerySqlServerTest.cs Adds SQL Server baselines for new nullability propagation tests and updates affected null-semantics SQL.
test/​EFCore.SqlServer.FunctionalTests/​Query/​ComplexNavigationsCollectionsSharedTypeQuerySqlServerTest.cs Updates a complex-navigation SQL Server baseline affected by the CASE optimization.
test/​EFCore.Sqlite.FunctionalTests/​Query/​SpatialQuerySqliteTest.cs Updates the SQLite spatial CASE baseline to reflect the revised generated SQL.
test/​EFCore.Relational.Specification.Tests/​Query/​NullSemanticsQueryTestBase.cs Adds shared regression tests for propagating nullability facts through conditional expressions.
src/​EFCore.Relational/​Query/​SqlNullabilityProcessor.cs Propagates nullability facts across failed searched-CASE clauses and fixes restoration of null-value column tracking.

Review effort: Lite

Null value columns are only consumed within the OrElse that added them, so recording
them for a failed CASE test had no effect.

@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 754709c into dotnet:main Nov 19, 2026
19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
@Aykuttonpc

Copy link
Copy Markdown
Contributor Author

Thanks @AndriySvyryd for the review!

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.

Ternary false-expression does not take ternary condition into account, resulting in slow execution on SQL Server

3 participants