Use null propagation to optimize away IS NOT NULL checks - #34127
Conversation
IS NOT NULL checks
3769918 to
afcfba2
Compare
afcfba2 to
d9c0736
Compare
089d329 to
fb551f8
Compare
fb551f8 to
bbe125a
Compare
69ae99a to
65d9a68
Compare
12a532b to
0514a23
Compare
|
rebased to fix conflicts |
249ae47 to
6b86657
Compare
|
@ranma42 Could you rebase on latest main? |
There was a problem hiding this comment.
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
CASEsimplification inSqlNullabilityProcessor.VisitCaseto drop redundant null-checking when the result expression already null-propagates. - Updated multiple SQL Server/SQLite functional tests to reflect the simplified SQL (removing
CASEwrappers 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. |
0514a23 to
7bb1918
Compare
7bb1918 to
46795e6
Compare
Sorry for the delay, I did not have much bandwidth to spare during the week. |
When a CASE expression simply replicates SQL null propagation, simplify it.
46795e6 to
42c0d56
Compare
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!
When a CASE expression simply replicates SQL null propagation, simplify it.
Contributes to #34126.