@@ -46,13 +46,14 @@ internal bool HasClientProjections
4646 private readonly List < string ? > _aliasForClientProjections = [ ] ;
4747 private CloningExpressionVisitor ? _cloningExpressionVisitor ;
4848
49- // #30915: when the nullable (inner) side of an outer join projects a non-entity structure (anonymous type / DTO /
50- // GroupBy-with-aggregate), the shaper would otherwise construct that object even on a no-match (all-NULL) row, throwing
51- // "Nullable object must have a value". Entities are protected via their key-column null checks; non-entities have no such
52- // signal. AddJoin injects a marker column (NULL on no-match) into the inner subquery and records here the association
53- // (inner-shaper NewExpression/MemberInitExpression node -> its marker ProjectionBindingExpression). A later phase (the
54- // projection binder) consults this side-table when projecting the *whole* inner object to gate it to null on no-match.
55- // The inner shaper is intentionally left a bare New/MemberInit (NOT wrapped), so existing member-folds keep working.
49+ // #30915: when the nullable side of an outer join (the inner side, or for RIGHT/FULL joins the outer side) projects a
50+ // non-entity structure (anonymous type / DTO / GroupBy-with-aggregate), the shaper would otherwise construct that object
51+ // even on a no-match (all-NULL) row, throwing "Nullable object must have a value". Entities are protected via their
52+ // key-column null checks; non-entities have no such signal. AddJoin injects a marker column (NULL on no-match) into the
53+ // nullable side's subquery and records here the association (nullable-side shaper NewExpression/MemberInitExpression node
54+ // -> its marker ProjectionBindingExpression). A later phase (the projection binder) consults this side-table when
55+ // projecting the *whole* nullable-side object to gate it to null on no-match.
56+ // The shaper is intentionally left a bare New/MemberInit (NOT wrapped), so existing member-folds keep working.
5657 //
5758 // This is transient build-time state, consumed within the same join-building -> projection-binding window on this same
5859 // instance (the binder receives the identical New/MemberInit node and this SelectExpression instance). It is keyed on shaper
@@ -3078,9 +3079,11 @@ private Expression AddJoin(
30783079 var outerNullable = joinType is JoinType . RightJoin or JoinType . FullJoin ;
30793080 var innerNullable = joinType is JoinType . LeftJoin or JoinType . OuterApply or JoinType . FullJoin ;
30803081
3081- // #30915: when the nullable (inner) side of an outer join projects a non-entity structure (anonymous type / DTO /
3082+ // #30915: when the nullable side of an outer join projects a non-entity structure (anonymous type / DTO /
30823083 // GroupBy-with-aggregate), the shaper would construct that object even on a no-match (all-NULL) row, throwing
3083- // "Nullable object must have a value". Inject a marker column into innerSelect now, *before* the AddJoin below pushes
3084+ // "Nullable object must have a value". The text below describes the inner side (LEFT JOIN / OUTER APPLY / FULL JOIN); the
3085+ // outer side of a RIGHT/FULL join is handled symmetrically in the block that follows.
3086+ // Inject a marker column into innerSelect now, *before* the AddJoin below pushes
30843087 // innerSelect into a subquery: the pushdown lifts the marker constant into that subquery ("1 AS marker") and references
30853088 // it as an outer column, so the outer join nulls it on no-match rows. If we instead added the constant after the join, it
30863089 // would be inlined into the combined SELECT and never become NULL. The standalone marker binding returned here is bound
@@ -3097,6 +3100,13 @@ private Expression AddJoin(
30973100 ? InjectNullabilityMarker ( innerSelect )
30983101 : null ;
30993102
3103+ // The same applies to the OUTER side of a RIGHT/FULL join, which becomes the nullable side. The marker is injected into
3104+ // this select before the join below pushes it into a subquery (which happens for the cases that can be fixed, such as a
3105+ // GroupBy or a Limit/Offset/Distinct outer); it is then remapped alongside the outer shaper in each branch.
3106+ Expression ? outerMarkerBinding = outerNullable && ( outerShaper is NewExpression or MemberInitExpression )
3107+ ? InjectNullabilityMarker ( this )
3108+ : null ;
3109+
31003110 AddJoin ( joinType , ref innerSelect , out _ , joinPredicate , isToOneJoin , isPrunableJoin ) ;
31013111
31023112 var transparentIdentifierType = TransparentIdentifierFactory . Create ( outerShaper . Type , innerShaper . Type ) ;
@@ -3152,7 +3162,9 @@ private Expression AddJoin(
31523162 {
31533163 // Since inner projections are populated, we need to populate outer also
31543164 var mapping = ConvertProjectionMappingToClientProjections ( _projectionMapping , outerNullable ) ;
3155- outerShaper = new ProjectionMemberToIndexConvertingExpressionVisitor ( this , mapping ) . Visit ( outerShaper ) ;
3165+ var outerToIndexConverter = new ProjectionMemberToIndexConvertingExpressionVisitor ( this , mapping ) ;
3166+ outerShaper = outerToIndexConverter . Visit ( outerShaper ) ;
3167+ outerMarkerBinding = outerMarkerBinding is null ? null : outerToIndexConverter . Visit ( outerMarkerBinding ) ;
31563168
31573169 var indexMap = new int [ innerSelect . _clientProjections . Count ] ;
31583170 for ( var i = 0 ; i < innerSelect . _clientProjections . Count ; i ++ )
@@ -3189,6 +3201,7 @@ private Expression AddJoin(
31893201 // #30915: the outer remap above may rebuild New/MemberInit nodes recorded as nullability-marker keys;
31903202 // re-key each such marker onto its rebuilt node. See the method for the full rationale and fail-safe.
31913203 RekeyNonEntityNullabilityMarkersAfterOuterShaperRemap ( outerRemapper , mapping ) ;
3204+ outerMarkerBinding = outerMarkerBinding is null ? null : outerRemapper . Visit ( outerMarkerBinding ) ;
31923205
31933206 mapping . Clear ( ) ;
31943207
@@ -3218,6 +3231,7 @@ private Expression AddJoin(
32183231 }
32193232
32203233 TryRecordNonEntityNullabilityMarker ( innerShaper , markerBinding ) ;
3234+ TryRecordNonEntityNullabilityMarker ( outerShaper , outerMarkerBinding ) ;
32213235
32223236 return New (
32233237 transparentIdentifierType . GetTypeInfo ( ) . DeclaredConstructors . Single ( ) ,
@@ -3245,7 +3259,7 @@ private static class NullabilityMarkerHolder
32453259 internal static readonly MemberInfo NullabilityMarkerMemberInfo
32463260 = typeof ( NullabilityMarkerHolder ) . GetField ( nameof ( NullabilityMarkerHolder . Marker ) ) ! ;
32473261
3248- // A synthetic projection member used as the key for the #30915 nullability marker in innerSelect. _projectionMapping.
3262+ // A synthetic projection member used as the key for the #30915 nullability marker in the nullable side's _projectionMapping.
32493263 private static readonly ProjectionMember NullabilityMarkerProjectionMember
32503264 = new ProjectionMember ( ) . Append ( NullabilityMarkerMemberInfo ) ;
32513265
@@ -3261,11 +3275,12 @@ private static readonly ProjectionMember NullabilityMarkerProjectionMember
32613275 private static string ? GetProjectionAlias ( ProjectionMember projectionMember )
32623276 => Equals ( projectionMember . Last , NullabilityMarkerMemberInfo ) ? NullabilityMarkerAlias : projectionMember . Last ? . Name ;
32633277
3264- // Injects a constant "marker" projection into innerSelect (the nullable side of an outer join) and returns a
3265- // ProjectionBindingExpression (typed int?) bound against innerSelect, in whichever projection representation innerSelect is
3266- // currently using (client projections vs. projection mapping). The caller remaps the returned binding alongside the inner
3267- // shaper; after the outer join the marker reads NULL exactly on no-match rows.
3268- private static ProjectionBindingExpression InjectNullabilityMarker ( SelectExpression innerSelect )
3278+ // Injects a constant "marker" projection into nullableSideSelect (the select on the nullable side of an outer join: the inner
3279+ // select, or for RIGHT/FULL joins this select as the outer) and returns a ProjectionBindingExpression (typed int?) bound
3280+ // against it, in whichever projection representation it is currently using (client projections vs. projection mapping). The
3281+ // caller remaps the returned binding alongside that side's shaper; after the outer join the marker reads NULL exactly on
3282+ // no-match rows.
3283+ private static ProjectionBindingExpression InjectNullabilityMarker ( SelectExpression nullableSideSelect )
32693284 {
32703285 // A SqlFragmentExpression is used (rather than a SqlConstantExpression) because it is exempt from the type-mapping
32713286 // postprocessor's "everything must have a type mapping" rule (a standalone projected constant cannot have a mapping
@@ -3274,15 +3289,15 @@ private static ProjectionBindingExpression InjectNullabilityMarker(SelectExpress
32743289 // the postprocessor leaves SqlFragmentExpression's mapping untouched.
32753290 var marker = new SqlFragmentExpression ( "1" , typeof ( int ) , IntTypeMapping . Default ) ;
32763291
3277- if ( innerSelect . _clientProjections . Count > 0 )
3292+ if ( nullableSideSelect . _clientProjections . Count > 0 )
32783293 {
3279- innerSelect . _clientProjections . Add ( marker ) ;
3280- innerSelect . _aliasForClientProjections . Add ( NullabilityMarkerAlias ) ;
3281- return new ProjectionBindingExpression ( innerSelect , innerSelect . _clientProjections . Count - 1 , typeof ( int ? ) ) ;
3294+ nullableSideSelect . _clientProjections . Add ( marker ) ;
3295+ nullableSideSelect . _aliasForClientProjections . Add ( NullabilityMarkerAlias ) ;
3296+ return new ProjectionBindingExpression ( nullableSideSelect , nullableSideSelect . _clientProjections . Count - 1 , typeof ( int ? ) ) ;
32823297 }
32833298
3284- innerSelect . _projectionMapping [ NullabilityMarkerProjectionMember ] = marker ;
3285- return new ProjectionBindingExpression ( innerSelect , NullabilityMarkerProjectionMember , typeof ( int ? ) ) ;
3299+ nullableSideSelect . _projectionMapping [ NullabilityMarkerProjectionMember ] = marker ;
3300+ return new ProjectionBindingExpression ( nullableSideSelect , NullabilityMarkerProjectionMember , typeof ( int ? ) ) ;
32863301 }
32873302
32883303 private void AddJoin (
0 commit comments