Skip to content

Fix ExecuteUpdate setter over left-joined GroupBy projection - #39721

Open
EnesKartal wants to merge 3 commits into
dotnet:mainfrom
EnesKartal:fix/39714-execute-update-left-join-groupby
Open

EnesKartal wants to merge 3 commits into
dotnet:mainfrom
EnesKartal:fix/39714-execute-update-left-join-groupby

Conversation

@EnesKartal

Copy link
Copy Markdown
Contributor

Fixes #39714

Root Cause

Since #38333, when translating a LEFT JOIN onto a subquery whose projection is a non-entity NewExpression or MemberInitExpression (such as a GroupBy projection into an anonymous type or DTO), the inner shaper is wrapped in a ConditionalExpression gated on a nullability marker column:
\\csharp
marker == null ? null : new { PartId = ..., NormalHours = ... }
\
When an ExecuteUpdate setter accesses a member on that outer-joined projection (e.g. x.Agg.NormalHours.GetValueOrDefault()), RelationalSqlTranslatingExpressionVisitor.VisitMember folds the member access into both branches of the ConditionalExpression:
\\csharp
Expression.Condition(
conditional.Test,
Expression.MakeMemberAccess(conditional.IfTrue, member),
Expression.MakeMemberAccess(conditional.IfFalse, member))
\
Because conditional.IfTrue is ConstantExpression(null), Expression.MakeMemberAccess(null, member) produces ((AnonType)null).NormalHours, which fails to translate in VisitMember and causes TranslateSetters to throw InvalidOperationException: The following lambda argument to 'SetProperty' does not represent a valid value.

Additionally, if the GroupBy subquery projects into a DTO using MemberInitExpression (
ew AggDto { ... }), VisitMember previously only reduced NewExpression and not MemberInitExpression.

Description of Change

  • In RelationalSqlTranslatingExpressionVisitor.VisitMember:
    • When folding a member access into a ConditionalExpression, if either branch is a
      ull constant or reference-type default, propagate a typed
      ull constant (memberExpression.Type.MakeNullable()) instead of constructing a member access on
      ull, and align the branch types to nullable when folding over a non-nullable value-type member.
    • Reduce member access over MemberInitExpression when the accessed member has a matching MemberAssignment binding, matching existing NewExpression reduction.
  • Added relational specification tests in NonSharedModelBulkUpdatesRelationalTestBase (along with SQL baselines in SQLite and SQL Server) covering both anonymous type (NewExpression) and DTO (MemberInitExpression) left-joined GroupBy projections in ExecuteUpdate.

@EnesKartal
EnesKartal requested a review from a team as a code owner November 19, 2026 15:09
Copilot AI balanced review requested due to automatic review settings November 19, 2026 15:09

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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

The null-propagation change incorrectly translates Nullable.HasValue to NULL instead of false on SQLite, causing ExecuteUpdate setters to write incorrect values or violate non-nullable column constraints.

1 open issue

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 20, 2026 07:58

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 20, 2026 08:40

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

🔵 Needs a closer look

A moderate correctness issue remains because value-type MemberInit projections are incorrectly treated as null, changing a left-joined struct DTO’s expected default value of 0 to SQL NULL without regression coverage.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Value-type DefaultExpression is incorrectly treated as null

src/​EFCore.Relational/​Query/​RelationalSqlTranslatingExpressionVisitor.cs:675

GateNonEntityOnNullabilityMarker also produces Expression.Default(objectType) for value-type MemberInit projections, which is a zeroed struct rather than null. This condition classifies every DefaultExpression as a null target, so a left-joined mutable struct/record-struct DTO such as default(AggStruct).Count is rewritten to a nullable SQL NULL instead of the LINQ DefaultIfEmpty value 0; the new reference-type DTO test does not cover this path. Restrict the DefaultExpression case to reference types (as the existing marker checks do) and add a value-type projection regression test.

1 resolved since last review

Review effort: Lite

=> !target.Type.IsValueType && target is ConstantExpression { Value: null } or DefaultExpression
? Expression.Constant(null, memberType.MakeNullable())
: Expression.MakeMemberAccess(target, member);
}

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.

GateNonEntityOnNullabilityMarker also produces Expression.Default(objectType) for value-type MemberInit projections, which is a zeroed struct rather than null. This condition classifies every DefaultExpression as a null target, so a left-joined mutable struct/record-struct DTO such as default(AggStruct).Count is rewritten to a nullable SQL NULL instead of the LINQ DefaultIfEmpty value 0; the new reference-type DTO test does not cover this path. Restrict the DefaultExpression case to reference types (as the existing marker checks do) and add a value-type projection regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants