[release/6.0] Fix to #27356 - Collection_navigation_equal_to_null_for_subquery fails after merging from release/6.0 #27429
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.
Improvement on the previous patch fix (#26744). We were not taking into the account scenario where collection projected from FirstOrDefault was compared to null. Collection can only be null if the parent entity is null, so we can safely apply the same optimization we do for anonymous types and required entities.
Fixes #27356
Description
Problem was that we were scaled down the optimization for comparing complex expressions to null, but we scaled it down too much. Scenario involving collection projection could also be optimized (and was being optimized in the too eager version before the previous fix), but now the optimization wouldn't apply causing query compilation error.
Customer impact
Customer executing the query in the scenario would see query compilation error now, whereas before query worked correctly. The scenario is quite niche/contrived (projecting collection navigation, then composing First/Single/etc and then projecting another collection and comparing that to null)
How found
We found it after merging patch to main - we have test coverage for this scenario but it was (incorrectly) disabled with another issue on release 6.0 branch.
Regression
Yes. Optimization before #26744 was too aggressive, but this scenario would have been optimized correctly. Comments in #26744 predicts these breaks could happen. Most of the time it would just lead to worse sql, but here its a scenario enabling optimization, since we are unable to translate the query in it's original form.
Testing
Re-enabled the test case covering this, that we already had.
Risk
Low. Extended the optimization to another (straightforward) scenario. Code path affected is very isolated. Quirk already present.