Repository navigation
Keep matching null keys when a correlated Where becomes a join - #39365
Merged
AndriySvyryd merged 2 commits intoOct 22, 2026
Merged
Conversation
- A correlated subquery's Where has C# null semantics: q.Category == p.Category matches rows where both are null. When the subquery was turned into a LEFT JOIN, the comparison became a lone equality in the join predicate, which SqlNullabilityProcessor processes as a LINQ equijoin - null matching nothing - so ToList() over the subquery lost the rows a Count() over the same Where kept. GroupBy projecting its elements alongside an aggregate correlates the groupings with their elements this way, so the group with a null key came back with no elements. - Correlations which do want null keys to match nothing (navigations, GroupJoin) state it with a null check on the outer key, which extraction removes as redundant. A lone extracted equality without one is now given an always-true partner, as CreateJoinPredicate does for an anonymous type with a single property, so that it's processed with C# null semantics. - Add tests for correlated collections over nullable keys, with C# and relational null semantics, and for a GroupBy over a nullable key projecting its elements and an aggregate; update the baselines of grouping element projections over nullable keys. Fixes dotnet#32026
5 of 6 tasks
- JsonQueryJsonTypeSqlServerTest only runs against SQL Server 2025, so its Group_by_Skip_Take_on_json_scalar baseline was missed when the JsonQuerySqlServerTest one gained the null compensation.
AndriySvyryd
approved these changes
Oct 22, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
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 #32026
Summary
A correlated subquery's
Wherehas C# null semantics, soq.Category == p.Categorymatches rows where both are null. That's how it's translated while it stays a subquery. But whenAddJointurns the subquery into aLEFT JOIN,TryExtractJoinKeymoves the comparison into the join predicate. There,SqlNullabilityProcessor.ProcessJoinPredicatetreats a lone equality as a LINQ equijoin, where null matches nothing. So the sameWheregives different answers:GroupBy hits this when it projects elements together with an aggregate. Those projections aren't lifted by #38963, so each grouping is still correlated with its elements through
groupingTerm == elementTerm. The null group then came back asCount = 2with no elements, which is the bug reported in #32026.Change
The correlations EF generates itself already say when null keys mustn't match:
outer != null && outer == innerfor a collection navigation;GroupJoinconversion builds the same guard.RemoveRedundantNullChecksthen removes that guard while extracting the join key, relying on the equijoin semantics of a lone join equality (see #35706).So
TryExtractJoinKeynow reports whether the extracted predicate is a lone equality from which no such null check was removed. In that case,AddJoingives the equality the always-true partner thatCreateJoinPredicatealready uses for a single-property anonymous type.ProcessJoinPredicatethen processes it with C# null semantics:[a] = [b] OR ([a] IS NULL AND [b] IS NULL)for nullable columns, and nothing extra for non-nullable ones. UnderUseRelationalNulls(), the predicate stays a plain=.Composite correlations already took this path, because an
AndAlsogets the regular null-semantics treatment.Behavior change
A correlated collection over nullable keys now includes the rows where both keys are null. That matches LINQ, and matches the same correlation in an aggregate. The SQL of such joins gains an
OR ... IS NULLterm, which can affect the plan the database picks. Both the result change and the plan change may be worth a breaking-change note.Tests
NullSemanticsQueryTestBase:Correlated_collection_uses_csharp_semantics, which failed before;Correlated_collection_uses_csharp_semantics_with_composite_correlation;Correlated_collection_using_relational_null_semantics, where null keys still don't match.NorthwindGroupByQueryTestBase:GroupBy_nullable_key_selecting_grouping_element_list_with_aggregate, the No items when projecting out groupings with a null key #32026 case overCustomers.Region, which failed before.Distinctand user-written correlations over nullable keys. Every changed baseline line is the sameONwith theOR ... IS NULLcompensation added. No navigation orGroupJoinjoin changed.I ran the full
Microsoft.EntityFrameworkCore.Querynamespace locally against LocalDB: 15,364 tests on SQL Server and 12,728 on SQLite. With the updated baselines, everything passes. Since the spec tests compare against LINQ-to-Objects, no result changed except where it now matches LINQ.