Repository navigation
Support && and || null checks in conditional projection member access - #39636
Open
AhmedSadek10 wants to merge 1 commit into
Open
AhmedSadek10 wants to merge 1 commit into
AhmedSadek10 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
A critical active finding shows the rewrite can change null-branch member access from a CLR NullReferenceException into null for all providers, so its eligibility condition is not semantics-preserving.
1 open issue
What changed in this PR
This PR fixes issue #38565 by supporting member access over conditional projections guarded by compound null checks (&& and ||). The generalized optimizer rewrite requires a null-safety restriction to avoid changing behavior for arbitrary conditionals with one null branch.
Changes:
- Broadens conditional member-access optimization for compound null-check predicates.
- Adds shared synchronous and asynchronous regression coverage for conjunctive and disjunctive null checks.
- Adds SQL Server and SQLite query baselines.
| File | Description |
|---|---|
| test/EFCore.SqlServer.FunctionalTests/Query/AdHocNavigationsQuerySqlServerTest.cs | Adds SQL Server query baselines covering the conditional projection regression cases. |
| test/EFCore.Sqlite.FunctionalTests/Query/AdHocNavigationsQuerySqliteTest.cs | Adds SQLite query baselines covering the conditional projection regression cases. |
| test/EFCore.Specification.Tests/Query/AdHocNavigationsQueryTestBase.cs | Adds shared synchronous and asynchronous regression tests, seed data, and assertions for compound null checks. |
| src/EFCore/Query/Internal/QueryOptimizingExpressionVisitor.cs | Generalizes conditional projection member-access optimization to handle null branches regardless of the conditional test expression. |
Review effort: Lite
Comment on lines
+368
to
+372
| if (conditional is { } conditionalExpression | ||
| && !(conditionalExpression.Type.IsNullableValueType() | ||
| && visitedMemberExpression.Member.Name is nameof(Nullable<>.HasValue) or nameof(Nullable<>.Value))) | ||
| { | ||
| var isLeftNullConstant = binaryTest.Left is ConstantExpression { Value: null }; | ||
| var isRightNullConstant = binaryTest.Right is ConstantExpression { Value: null }; | ||
|
|
||
| if (isLeftNullConstant != isRightNullConstant | ||
| && ((binaryTest.NodeType == ExpressionType.Equal | ||
| && conditionalExpression.IfTrue is ConstantExpression { Value: null }) | ||
| || (binaryTest.NodeType == ExpressionType.NotEqual | ||
| && conditionalExpression.IfFalse is ConstantExpression { Value: null }))) | ||
| var isTrueNullConstant = conditionalExpression.IfTrue is ConstantExpression { Value: null }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #38565