Repository navigation
Flatten nested anonymous types in join key selectors - #39514
Conversation
CreateJoinPredicate only unpacked the top-level NewExpression of a composite join key. A nested anonymous type was translated as a single comparison, which returned null, producing a dangling AND in the SQL or a NullReferenceException when the nested type was the only key member. Nested NewExpressions are now flattened recursively into one comparison per leaf. The single-comparison workaround for null semantics now counts comparisons rather than top-level arguments. Fixes dotnet#35028 Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dotnet-policy-service agree |
There was a problem hiding this comment.
🟡 Changes recommended
A moderate correctness issue remains because recursively rewriting every nested constructor can change join equality semantics for non-anonymous keys such as DateTimeOffset, requiring a targeted fix and regression test.
1 open issue
What changed in this PR
This pull request fixes relational SQL translation for joins using nested anonymous types as key selectors, preventing invalid predicates and preserving null-equality behavior.
Changes:
- Recursively flattens nested anonymous key selectors into leaf comparisons.
- Counts flattened comparisons for single-member null compensation.
- Enables regression tests and adds SQL Server baselines for nested and nullable keys.
- The recursive implementation should be restricted to anonymous types so non-anonymous constructor equality semantics are not changed.
| File | Summary |
|---|---|
test/EFCore.SqlServer.FunctionalTests/Query/NorthwindJoinQuerySqlServerTest.cs |
Adds SQL Server assertions and baselines for nested join predicates and nullable-key compensation. |
test/EFCore.Specification.Tests/Query/NorthwindJoinQueryTestBase.cs |
Enables nested-key coverage and adds a nullable single-member regression test. |
src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs |
Recursively builds join predicates from nested key expressions and applies single-comparison null compensation. |
Review effort: Lite
Recursing into every nested NewExpression changed equality semantics for constructed members such as DateTimeOffset, whose equality compares instants rather than constructor arguments. Only anonymous types are flattened now; other members are compared as a whole. A comparison that can't be translated now makes the join untranslatable instead of being dropped from the predicate. GroupJoin_aggregate_nested_anonymous_key_selectors now expects success, like its non-nested counterpart, which InMemory provides; relational providers expect a translation failure. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Picks up dotnet#39512, which fixes the File_based_app_can_be_built test failure. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The CI failure was |
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!

Fixes #35028
A join whose key selectors contain a nested anonymous type generated invalid SQL (
ON a = b ANDwith nothing after theAND), and threw aNullReferenceExceptionwhen the nested type was the only key member.CreateJoinPredicateonly unpacked the top-levelNewExpression. The nested one was passed toTranslateExpressionas a single comparison, which returned null, and the!on that call hid it.Changes
new DateTimeOffset(...), are compared as a whole, since their equality isn't necessarily member-wise.CreateJoinPredicatereturns null, so the join fails translation with the usual error instead of losing that comparison. The callers already handle null.new { Nested = new { x.Region } }. The SQL Server baseline showsOR ([c].[Region] IS NULL AND [c1].[Region] IS NULL).Tests
Join_with_key_selectors_being_nested_anonymous_objects.GroupJoin_aggregate_nested_anonymous_key_selectors. Like its non-nested counterpart, the base test now expects success; InMemory runs it. SQL Server and SQLite override it to expect a translation failure, because the GroupJoin goes through a correlated predicate where nested anonymous type equality isn't supported.Join_with_key_selectors_being_nested_anonymous_objects_single_nullable_member.Join_with_key_selectors_being_nested_anonymous_objects_with_constructed_member: same instant, different offsets. On SQL Server it translates toTODATETIMEOFFSET(...) = TODATETIMEOFFSET(...)and matches. SQLite doesn't translate the constructor, so it expects a translation failure.Local results on macOS arm64:
EFCore.Sqlite.FunctionalTests: no failures except one flakyTPCManyToManyQuerySqliteTestsetup error (no such table), which fails intermittently without code changes.The full SQL Server suite was not run locally.
🤖 Generated with Claude Code