Skip to content

Commit

Permalink
Fix to #18808 - Query: incorrect results for query with Join where ke…
Browse files Browse the repository at this point in the history
…y selector is anonymous type with a single property

Problem was that on c# join predicate behaves differently when it's a regular property (null and null yield false), and where its anonymous type with just a single property (new { A = null } and new { A = null } yields true).
However, we lose that distinction during initial Join predicate translation.

Fix is to add a sentinel true == true condition, if the anonymous type only has one element. This condition can be later optimized out.

Resolves #18808
  • Loading branch information
maumar committed Nov 7, 2019
1 parent a41d6a8 commit 89e3239
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,13 @@ private SqlExpression CreateJoinPredicate(
: _sqlExpressionFactory.AndAlso(result, joinPredicate);
}

if (outerNew.Arguments.Count == 1)
{
result = _sqlExpressionFactory.AndAlso(
result,
CreateJoinPredicate(Expression.Constant(true), Expression.Constant(true)));
}

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3921,7 +3921,7 @@ public virtual Task Where_on_multilevel_reference_in_subquery_with_outer_project
.Select(l3 => l3.Name));
}

[ConditionalTheory(Skip = " Issue#16093")]
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Join_condition_optimizations_applied_correctly_when_anonymous_type_with_single_property(bool isAsync)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2876,9 +2876,9 @@ public override async Task Join_condition_optimizations_applied_correctly_when_a
await base.Join_condition_optimizations_applied_correctly_when_anonymous_type_with_single_property(isAsync);

AssertSql(
@"SELECT [l1].[Id], [l1].[Date], [l1].[Name], [l1].[OneToMany_Optional_Self_Inverse1Id], [l1].[OneToMany_Required_Self_Inverse1Id], [l1].[OneToOne_Optional_Self1Id]
FROM [LevelOne] AS [l1]
INNER JOIN [LevelTwo] AS [l2] ON ([l1].[OneToMany_Optional_Self_Inverse1Id] = [l2].[Level1_Optional_Id]) OR ([l1].[OneToMany_Optional_Self_Inverse1Id] IS NULL AND [l2].[Level1_Optional_Id] IS NULL)");
@"SELECT [l].[Id], [l].[Date], [l].[Name], [l].[OneToMany_Optional_Self_Inverse1Id], [l].[OneToMany_Required_Self_Inverse1Id], [l].[OneToOne_Optional_Self1Id]
FROM [LevelOne] AS [l]
INNER JOIN [LevelTwo] AS [l0] ON (([l].[OneToMany_Optional_Self_Inverse1Id] = [l0].[Level1_Optional_Id]) OR ([l].[OneToMany_Optional_Self_Inverse1Id] IS NULL AND [l0].[Level1_Optional_Id] IS NULL)) AND (CAST(1 AS bit) = CAST(1 AS bit))");
}

public override async Task Join_condition_optimizations_applied_correctly_when_anonymous_type_with_multiple_properties(bool isAsync)
Expand Down

0 comments on commit 89e3239

Please sign in to comment.