Skip to content

Use null propagation to optimize away IS NOT NULL checks - #34127

Merged
AndriySvyryd merged 3 commits into
dotnet:mainfrom
ranma42:drop-not-null-checks
Jun 15, 2026
Merged

Use null propagation to optimize away IS NOT NULL checks#34127
AndriySvyryd merged 3 commits into
dotnet:mainfrom
ranma42:drop-not-null-checks

Conversation

@ranma42

@ranma42 ranma42 commented Jun 30, 2024

Copy link
Copy Markdown
Contributor

When a CASE expression simply replicates SQL null propagation, simplify it.

Contributes to #34126.

@ranma42 ranma42 changed the title Optimize null propagation Use null propagation to optimize away IS NOT NULL checks Jun 30, 2024
@ranma42
ranma42 force-pushed the drop-not-null-checks branch 2 times, most recently from 3769918 to afcfba2 Compare June 30, 2024 19:46
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
@ranma42
ranma42 marked this pull request as draft July 2, 2024 23:39
@ranma42
ranma42 force-pushed the drop-not-null-checks branch from afcfba2 to d9c0736 Compare July 3, 2024 15:54
@ranma42
ranma42 marked this pull request as ready for review July 3, 2024 15:54
@ranma42
ranma42 marked this pull request as draft July 3, 2024 15:56
@ranma42
ranma42 force-pushed the drop-not-null-checks branch 2 times, most recently from 089d329 to fb551f8 Compare July 3, 2024 18:42
@ranma42
ranma42 marked this pull request as ready for review July 4, 2024 07:11
@ranma42
ranma42 force-pushed the drop-not-null-checks branch from fb551f8 to bbe125a Compare July 9, 2024 22:52
@roji roji self-assigned this Jul 12, 2024
roji
roji previously requested changes Jul 12, 2024

@roji roji 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.

Great work as always @ranma42, it's really nice to see our SQL baselines getting progressively tighter and better.

See mainly nits below.

Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
@ranma42
ranma42 force-pushed the drop-not-null-checks branch 2 times, most recently from 69ae99a to 65d9a68 Compare July 12, 2024 22:49
@ranma42
ranma42 requested a review from roji July 21, 2024 15:44
@ranma42
ranma42 force-pushed the drop-not-null-checks branch from 12a532b to 0514a23 Compare February 9, 2025 20:44
@ranma42
ranma42 requested a review from a team as a code owner February 9, 2025 20:44
@ranma42

ranma42 commented Feb 9, 2025

Copy link
Copy Markdown
Contributor Author

rebased to fix conflicts

@AndriySvyryd

Copy link
Copy Markdown
Member

@ranma42 Could you rebase on latest main?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves relational query SQL generation by simplifying CASE expressions that merely replicate SQL’s built-in null propagation behavior, removing redundant IS NULL/IS NOT NULL checks and producing simpler SQL. This aligns with the null-semantics propagation optimization work tracked in #34126.

Changes:

  • Added a CASE simplification in SqlNullabilityProcessor.VisitCase to drop redundant null-checking when the result expression already null-propagates.
  • Updated multiple SQL Server/SQLite functional tests to reflect the simplified SQL (removing CASE wrappers and redundant null predicates).
  • Added new null-semantics tests to validate the optimization scenarios (unary op, binary op, partial/nested/mixed checks).

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Implements the CASE-to-null-propagating-expression simplification in the nullability processor.
test/EFCore.Relational.Specification.Tests/Query/NullSemanticsQueryTestBase.cs Adds new relational-spec tests covering IS NOT NULL optimization scenarios.
test/EFCore.SqlServer.FunctionalTests/Query/NullSemanticsQuerySqlServerTest.cs Adds SQL Server AssertSql baselines for the new null-semantics optimization tests.
test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerTest.cs Updates expected SQL to remove redundant CASE null-propagation patterns.
test/EFCore.SqlServer.FunctionalTests/Query/TPTGearsOfWarQuerySqlServerTest.cs Updates expected SQL to remove redundant CASE null-propagation patterns.
test/EFCore.SqlServer.FunctionalTests/Query/TPCGearsOfWarQuerySqlServerTest.cs Updates expected SQL to remove redundant CASE null-propagation patterns.
test/EFCore.SqlServer.FunctionalTests/Query/TemporalGearsOfWarQuerySqlServerTest.cs Updates expected SQL to remove redundant CASE null-propagation patterns (temporal queries).
test/EFCore.SqlServer.FunctionalTests/Query/TemporalComplexNavigationsCollectionsSharedTypeQuerySqlServerTest.cs Updates expected SQL by removing redundant IS NOT NULL checks inside CASE projections.
test/EFCore.SqlServer.FunctionalTests/Query/NorthwindGroupByQuerySqlServerTest.cs Simplifies aggregate SQL baseline (MAX([OrderDate]) instead of MAX(CASE ...)).
test/EFCore.SqlServer.FunctionalTests/Query/SpatialQuerySqlServerGeometryTest.cs Removes redundant null-check CASE around SQL Server spatial instance method calls.
test/EFCore.SqlServer.FunctionalTests/Query/SpatialQuerySqlServerGeographyTest.cs Removes redundant null-check CASE around SQL Server spatial instance method calls.
test/EFCore.SqlServer.FunctionalTests/Query/EntitySplittingQuerySqlServerTest.cs Removes redundant IS NOT NULL checks in a projection CASE baseline.
test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteTest.cs Updates SQLite baselines to remove redundant CASE null-propagation patterns.
test/EFCore.Sqlite.FunctionalTests/Query/SpatialQuerySqliteTest.cs Removes redundant null-check CASE around spatial function call baseline.

Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs
@ranma42
ranma42 force-pushed the drop-not-null-checks branch from 0514a23 to 7bb1918 Compare June 13, 2026 11:52
Copilot AI review requested due to automatic review settings June 13, 2026 19:51
@ranma42
ranma42 force-pushed the drop-not-null-checks branch from 7bb1918 to 46795e6 Compare June 13, 2026 19:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlNullabilityProcessor.cs Outdated
@ranma42

ranma42 commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

@ranma42 Could you rebase on latest main?

Sorry for the delay, I did not have much bandwidth to spare during the week.
It should now be rebased (with baselines updated and Copilot suggestions applied).

ranma42 added 2 commits June 14, 2026 09:17
When a CASE expression simply replicates SQL null propagation, simplify it.
@ranma42
ranma42 force-pushed the drop-not-null-checks branch from 46795e6 to 42c0d56 Compare June 14, 2026 07:18

@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 3cdbbc1 into dotnet:main Jun 15, 2026
13 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 23, 2026
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.

4 participants