Skip to content

Flatten nested anonymous types in join key selectors - #39514

Merged
AndriySvyryd merged 3 commits into
dotnet:mainfrom
Hassan1305:fix/35028-nested-anonymous-join-keys
Nov 13, 2026
Merged

AndriySvyryd merged 3 commits into
dotnet:mainfrom
Hassan1305:fix/35028-nested-anonymous-join-keys

Conversation

@Hassan1305

@Hassan1305 Hassan1305 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #35028

A join whose key selectors contain a nested anonymous type generated invalid SQL (ON a = b AND with nothing after the AND), and threw a NullReferenceException when the nested type was the only key member.

CreateJoinPredicate only unpacked the top-level NewExpression. The nested one was passed to TranslateExpression as a single comparison, which returned null, and the ! on that call hid it.

Changes

  • Nested anonymous types are now flattened recursively, with one comparison per leaf, ANDed together. Only anonymous types are flattened. Other constructed members, such as new DateTimeOffset(...), are compared as a whole, since their equality isn't necessarily member-wise.
  • If a comparison can't be translated, CreateJoinPredicate returns null, so the join fails translation with the usual error instead of losing that comparison. The callers already handle null.
  • The workaround that adds an always-true predicate for single-member anonymous keys now counts comparisons rather than top-level arguments. This keeps null-equals-null semantics for new { Nested = new { x.Region } }. The SQL Server baseline shows OR ([c].[Region] IS NULL AND [c1].[Region] IS NULL).

Tests

  • Unskipped Join_with_key_selectors_being_nested_anonymous_objects.
  • Unskipped 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.
  • Added Join_with_key_selectors_being_nested_anonymous_objects_single_nullable_member.
  • Added Join_with_key_selectors_being_nested_anonymous_objects_with_constructed_member: same instant, different offsets. On SQL Server it translates to TODATETIMEOFFSET(...) = TODATETIMEOFFSET(...) and matches. SQLite doesn't translate the constructor, so it expects a translation failure.
  • SQL Server baselines were generated against SQL Server 2022.

Local results on macOS arm64:

  • Northwind join tests: 149 passed on SQL Server, 148 on SQLite, 148 on InMemory.
  • Full EFCore.Sqlite.FunctionalTests: no failures except one flaky TPCManyToManyQuerySqliteTest setup error (no such table), which fails intermittently without code changes.

The full SQL Server suite was not run locally.

🤖 Generated with Claude Code

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]>
@Hassan1305
Hassan1305 requested a review from a team as a code owner November 4, 2026 06:29
@Hassan1305

Copy link
Copy Markdown
Contributor Author

@Hassan1305 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Hassan1305 and others added 2 commits November 4, 2026 12:02
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]>
@Hassan1305

Copy link
Copy Markdown
Contributor Author

The CI failure was File_based_app_can_be_built in dotnet-ef.Tests, which #39512 fixed on main after this branch was created. I've merged main in. Could you rerun CI?

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit 89c632c into dotnet:main Nov 13, 2026
18 of 19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query: invalid SQL generated when doing join with nested anonymous key selectors

3 participants