Fix DefaultIfEmpty lifting logic within SelectMany #36248
Merged
+223
−74
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.
This fixes the logic which lifts DefaultIfEmpty within the SelectMany collection selector; this optimization causes
context.Blogs.SelectMany(b => b.Posts.DefaultIfEmpty()
to be translated as a simple LEFT JOIN, instead of having a subquery with the complex DefaultIfEmpty logic.The previous logic erroneously identified any DefaultIfEmpty anywhere within the SelectMany selector, and lifted it out (translating the SelectMany as LEFT JOIN/OUTER APPLY) in cases which caused incorrect data to be returned:
context.Blogs.SelectMany(b => b.Posts.SelectMany(p => p.Comments.DefaultIfEmpty())
. This is #33343.blogs.SelectMany(b => b.Posts.DefaultIfEmpty().Where(p => p.Id > 3))
).We now have state tracking whether DefaultIfEmpty can be lifted as CorrelationFindingExpressionVisitor visits the SelectMany selector.
Fixes #33343
Fixes #19095