Skip to content

Commit

Permalink
Query: Apply table aliasing recursively
Browse files Browse the repository at this point in the history
We did not visit inner when we updated alias for select expression
  • Loading branch information
smitpatel committed Jun 13, 2019
1 parent 82df078 commit f7b72a9
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ private readonly ISet<TableExpressionBase> _visitedTableExpressionBases

protected override Expression VisitExtension(Expression extensionExpression)
{
switch (extensionExpression)
var visitedExpression = base.VisitExtension(extensionExpression);
if (visitedExpression is TableExpressionBase tableExpressionBase
&& !_visitedTableExpressionBases.Contains(tableExpressionBase)
&& tableExpressionBase.Alias != null)
{
case TableExpressionBase tableExpressionBase
when !_visitedTableExpressionBases.Contains(tableExpressionBase)
&& tableExpressionBase.Alias != null:
tableExpressionBase.Alias = GenerateUniqueAlias(tableExpressionBase.Alias);
_visitedTableExpressionBases.Add(tableExpressionBase);
return tableExpressionBase;

default:
return base.VisitExtension(extensionExpression);
tableExpressionBase.Alias = GenerateUniqueAlias(tableExpressionBase.Alias);
_visitedTableExpressionBases.Add(tableExpressionBase);
}

return visitedExpression;
}

private string GenerateUniqueAlias(string currentAlias)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4401,7 +4401,7 @@ from l2 in grouping.DefaultIfEmpty()
elementAsserter: (e, a) => Assert.Equal(e.Id, a.Id));
}

[ConditionalTheory(Skip = "QueryIssue")]
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Explicit_GroupJoin_in_subquery_with_multiple_result_operator_distinct_count_materializes_main_clause(
bool isAsync)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2690,12 +2690,12 @@ public override async Task Null_reference_protection_complex(bool isAsync)

AssertSql(
@"SELECT [t].[Name]
FROM [LevelThree] AS [l]
FROM [LevelThree] AS [l1]
LEFT JOIN (
SELECT [l0].[Id], [l0].[Date], [l0].[Level1_Optional_Id], [l0].[Level1_Required_Id], [l0].[Name], [l0].[OneToMany_Optional_Inverse2Id], [l0].[OneToMany_Optional_Self_Inverse2Id], [l0].[OneToMany_Required_Inverse2Id], [l0].[OneToMany_Required_Self_Inverse2Id], [l0].[OneToOne_Optional_PK_Inverse2Id], [l0].[OneToOne_Optional_Self2Id]
FROM [LevelOne] AS [l1]
LEFT JOIN [LevelTwo] AS [l0] ON [l1].[Id] = [l0].[Level1_Optional_Id]
) AS [t] ON [l].[Level2_Required_Id] = [t].[Id]");
SELECT [l].[Id], [l].[Date], [l].[Level1_Optional_Id], [l].[Level1_Required_Id], [l].[Name], [l].[OneToMany_Optional_Inverse2Id], [l].[OneToMany_Optional_Self_Inverse2Id], [l].[OneToMany_Required_Inverse2Id], [l].[OneToMany_Required_Self_Inverse2Id], [l].[OneToOne_Optional_PK_Inverse2Id], [l].[OneToOne_Optional_Self2Id]
FROM [LevelOne] AS [l0]
LEFT JOIN [LevelTwo] AS [l] ON [l0].[Id] = [l].[Level1_Optional_Id]
) AS [t] ON [l1].[Level2_Required_Id] = [t].[Id]");
}

public override async Task Null_reference_protection_complex_materialization(bool isAsync)
Expand All @@ -2718,12 +2718,12 @@ public override async Task Null_reference_protection_complex_client_eval(bool is

AssertSql(
@"SELECT [t].[Name]
FROM [LevelThree] AS [l]
FROM [LevelThree] AS [l1]
LEFT JOIN (
SELECT [l0].[Id], [l0].[Date], [l0].[Level1_Optional_Id], [l0].[Level1_Required_Id], [l0].[Name], [l0].[OneToMany_Optional_Inverse2Id], [l0].[OneToMany_Optional_Self_Inverse2Id], [l0].[OneToMany_Required_Inverse2Id], [l0].[OneToMany_Required_Self_Inverse2Id], [l0].[OneToOne_Optional_PK_Inverse2Id], [l0].[OneToOne_Optional_Self2Id]
FROM [LevelOne] AS [l1]
LEFT JOIN [LevelTwo] AS [l0] ON [l1].[Id] = [l0].[Level1_Optional_Id]
) AS [t] ON [l].[Level2_Required_Id] = [t].[Id]");
SELECT [l].[Id], [l].[Date], [l].[Level1_Optional_Id], [l].[Level1_Required_Id], [l].[Name], [l].[OneToMany_Optional_Inverse2Id], [l].[OneToMany_Optional_Self_Inverse2Id], [l].[OneToMany_Required_Inverse2Id], [l].[OneToMany_Required_Self_Inverse2Id], [l].[OneToOne_Optional_PK_Inverse2Id], [l].[OneToOne_Optional_Self2Id]
FROM [LevelOne] AS [l0]
LEFT JOIN [LevelTwo] AS [l] ON [l0].[Id] = [l].[Level1_Optional_Id]
) AS [t] ON [l1].[Level2_Required_Id] = [t].[Id]");
}

public override async Task GroupJoin_with_complex_subquery_with_joins_does_not_get_flattened(bool isAsync)
Expand All @@ -2732,12 +2732,12 @@ public override async Task GroupJoin_with_complex_subquery_with_joins_does_not_g

AssertSql(
@"SELECT CAST([t].[Id] AS int)
FROM [LevelOne] AS [l]
FROM [LevelOne] AS [l1]
LEFT JOIN (
SELECT [l0].[Id], [l0].[Date], [l0].[Level1_Optional_Id], [l0].[Level1_Required_Id], [l0].[Name], [l0].[OneToMany_Optional_Inverse2Id], [l0].[OneToMany_Optional_Self_Inverse2Id], [l0].[OneToMany_Required_Inverse2Id], [l0].[OneToMany_Required_Self_Inverse2Id], [l0].[OneToOne_Optional_PK_Inverse2Id], [l0].[OneToOne_Optional_Self2Id]
FROM [LevelTwo] AS [l0]
INNER JOIN [LevelOne] AS [l1] ON [l0].[Level1_Required_Id] = [l1].[Id]
) AS [t] ON [l].[Id] = [t].[Level1_Optional_Id]");
SELECT [l].[Id], [l].[Date], [l].[Level1_Optional_Id], [l].[Level1_Required_Id], [l].[Name], [l].[OneToMany_Optional_Inverse2Id], [l].[OneToMany_Optional_Self_Inverse2Id], [l].[OneToMany_Required_Inverse2Id], [l].[OneToMany_Required_Self_Inverse2Id], [l].[OneToOne_Optional_PK_Inverse2Id], [l].[OneToOne_Optional_Self2Id]
FROM [LevelTwo] AS [l]
INNER JOIN [LevelOne] AS [l0] ON [l].[Level1_Required_Id] = [l0].[Id]
) AS [t] ON [l1].[Id] = [t].[Level1_Optional_Id]");
}

public override async Task GroupJoin_with_complex_subquery_with_joins_does_not_get_flattened2(bool isAsync)
Expand All @@ -2746,12 +2746,12 @@ public override async Task GroupJoin_with_complex_subquery_with_joins_does_not_g

AssertSql(
@"SELECT CAST([t].[Id] AS int)
FROM [LevelOne] AS [l]
FROM [LevelOne] AS [l1]
LEFT JOIN (
SELECT [l0].[Id], [l0].[Date], [l0].[Level1_Optional_Id], [l0].[Level1_Required_Id], [l0].[Name], [l0].[OneToMany_Optional_Inverse2Id], [l0].[OneToMany_Optional_Self_Inverse2Id], [l0].[OneToMany_Required_Inverse2Id], [l0].[OneToMany_Required_Self_Inverse2Id], [l0].[OneToOne_Optional_PK_Inverse2Id], [l0].[OneToOne_Optional_Self2Id]
FROM [LevelTwo] AS [l0]
INNER JOIN [LevelOne] AS [l1] ON [l0].[Level1_Required_Id] = [l1].[Id]
) AS [t] ON [l].[Id] = [t].[Level1_Optional_Id]");
SELECT [l].[Id], [l].[Date], [l].[Level1_Optional_Id], [l].[Level1_Required_Id], [l].[Name], [l].[OneToMany_Optional_Inverse2Id], [l].[OneToMany_Optional_Self_Inverse2Id], [l].[OneToMany_Required_Inverse2Id], [l].[OneToMany_Required_Self_Inverse2Id], [l].[OneToOne_Optional_PK_Inverse2Id], [l].[OneToOne_Optional_Self2Id]
FROM [LevelTwo] AS [l]
INNER JOIN [LevelOne] AS [l0] ON [l].[Level1_Required_Id] = [l0].[Id]
) AS [t] ON [l1].[Id] = [t].[Level1_Optional_Id]");
}

public override async Task GroupJoin_with_complex_subquery_with_joins_does_not_get_flattened3(bool isAsync)
Expand All @@ -2760,12 +2760,12 @@ public override async Task GroupJoin_with_complex_subquery_with_joins_does_not_g

AssertSql(
@"SELECT CAST([t].[Id] AS int)
FROM [LevelOne] AS [l]
FROM [LevelOne] AS [l1]
LEFT JOIN (
SELECT [l0].[Id], [l0].[Date], [l0].[Level1_Optional_Id], [l0].[Level1_Required_Id], [l0].[Name], [l0].[OneToMany_Optional_Inverse2Id], [l0].[OneToMany_Optional_Self_Inverse2Id], [l0].[OneToMany_Required_Inverse2Id], [l0].[OneToMany_Required_Self_Inverse2Id], [l0].[OneToOne_Optional_PK_Inverse2Id], [l0].[OneToOne_Optional_Self2Id]
FROM [LevelTwo] AS [l0]
LEFT JOIN [LevelOne] AS [l1] ON [l0].[Level1_Required_Id] = [l1].[Id]
) AS [t] ON [l].[Id] = [t].[Level1_Required_Id]");
SELECT [l].[Id], [l].[Date], [l].[Level1_Optional_Id], [l].[Level1_Required_Id], [l].[Name], [l].[OneToMany_Optional_Inverse2Id], [l].[OneToMany_Optional_Self_Inverse2Id], [l].[OneToMany_Required_Inverse2Id], [l].[OneToMany_Required_Self_Inverse2Id], [l].[OneToOne_Optional_PK_Inverse2Id], [l].[OneToOne_Optional_Self2Id]
FROM [LevelTwo] AS [l]
LEFT JOIN [LevelOne] AS [l0] ON [l].[Level1_Required_Id] = [l0].[Id]
) AS [t] ON [l1].[Id] = [t].[Level1_Required_Id]");
}

public override async Task GroupJoin_with_complex_subquery_with_joins_with_reference_to_grouping1(bool isAsync)
Expand Down Expand Up @@ -2865,14 +2865,14 @@ public override async Task GroupJoin_on_left_side_being_a_subquery(bool isAsync)
AssertSql(
@"@__p_0='2'
SELECT [t].[Id], [l].[Name] AS [Brand]
SELECT [t].[Id], [l1].[Name] AS [Brand]
FROM (
SELECT TOP(@__p_0) [l0].[Id], [l0].[Date], [l0].[Name], [l0].[OneToMany_Optional_Self_Inverse1Id], [l0].[OneToMany_Required_Self_Inverse1Id], [l0].[OneToOne_Optional_Self1Id], [l1].[Name] AS [Name0]
FROM [LevelOne] AS [l0]
LEFT JOIN [LevelTwo] AS [l1] ON [l0].[Id] = [l1].[Level1_Optional_Id]
ORDER BY [l1].[Name], [l0].[Id]
SELECT TOP(@__p_0) [l].[Id], [l].[Date], [l].[Name], [l].[OneToMany_Optional_Self_Inverse1Id], [l].[OneToMany_Required_Self_Inverse1Id], [l].[OneToOne_Optional_Self1Id], [l0].[Name] AS [Name0]
FROM [LevelOne] AS [l]
LEFT JOIN [LevelTwo] AS [l0] ON [l].[Id] = [l0].[Level1_Optional_Id]
ORDER BY [l0].[Name], [l].[Id]
) AS [t]
LEFT JOIN [LevelTwo] AS [l] ON [t].[Id] = [l].[Level1_Optional_Id]
LEFT JOIN [LevelTwo] AS [l1] ON [t].[Id] = [l1].[Level1_Optional_Id]
ORDER BY [t].[Name0], [t].[Id]");
}

Expand Down Expand Up @@ -3123,16 +3123,15 @@ public override async Task Explicit_GroupJoin_in_subquery_with_multiple_result_o
await base.Explicit_GroupJoin_in_subquery_with_multiple_result_operator_distinct_count_materializes_main_clause(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]
@"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]
WHERE (
SELECT COUNT(*)
FROM (
SELECT DISTINCT [l1_inner].*
FROM [LevelOne] AS [l1_inner]
LEFT JOIN [LevelTwo] AS [l2] ON [l1_inner].[Id] = [l2].[Level1_Optional_Id]
) AS [t]
) > 4");
SELECT DISTINCT [l0].[Id], [l0].[Date], [l0].[Name], [l0].[OneToMany_Optional_Self_Inverse1Id], [l0].[OneToMany_Required_Self_Inverse1Id], [l0].[OneToOne_Optional_Self1Id]
FROM [LevelOne] AS [l0]
LEFT JOIN [LevelTwo] AS [l1] ON [l0].[Id] = [l1].[Level1_Optional_Id]
) AS [t]) > 4");
}

public override async Task Where_on_multilevel_reference_in_subquery_with_outer_projection(bool isAsync)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2093,17 +2093,17 @@ public override async Task Join_navigation_translated_to_subquery_composite_key(
await base.Join_navigation_translated_to_subquery_composite_key(isAsync);

AssertSql(
@"SELECT [g].[FullName], [t].[Note]
@"SELECT [g].[FullName], [t1].[Note]
FROM [Gears] AS [g]
INNER JOIN (
SELECT [t0].[Id], [t0].[GearNickName], [t0].[GearSquadId], [t0].[Note], [t1].[Nickname], [t1].[SquadId], [t1].[AssignedCityName], [t1].[CityOrBirthName], [t1].[Discriminator], [t1].[FullName], [t1].[HasSoulPatch], [t1].[LeaderNickname], [t1].[LeaderSquadId], [t1].[Rank]
FROM [Tags] AS [t0]
SELECT [t].[Id], [t].[GearNickName], [t].[GearSquadId], [t].[Note], [t0].[Nickname], [t0].[SquadId], [t0].[AssignedCityName], [t0].[CityOrBirthName], [t0].[Discriminator], [t0].[FullName], [t0].[HasSoulPatch], [t0].[LeaderNickname], [t0].[LeaderSquadId], [t0].[Rank]
FROM [Tags] AS [t]
LEFT JOIN (
SELECT [g0].[Nickname], [g0].[SquadId], [g0].[AssignedCityName], [g0].[CityOrBirthName], [g0].[Discriminator], [g0].[FullName], [g0].[HasSoulPatch], [g0].[LeaderNickname], [g0].[LeaderSquadId], [g0].[Rank]
FROM [Gears] AS [g0]
WHERE [g0].[Discriminator] IN (N'Gear', N'Officer')
) AS [t1] ON (([t0].[GearNickName] = [t1].[Nickname]) AND [t0].[GearNickName] IS NOT NULL) AND (([t0].[GearSquadId] = [t1].[SquadId]) AND [t0].[GearSquadId] IS NOT NULL)
) AS [t] ON [g].[FullName] = [t].[FullName]
) AS [t0] ON (([t].[GearNickName] = [t0].[Nickname]) AND [t].[GearNickName] IS NOT NULL) AND (([t].[GearSquadId] = [t0].[SquadId]) AND [t].[GearSquadId] IS NOT NULL)
) AS [t1] ON [g].[FullName] = [t1].[FullName]
WHERE [g].[Discriminator] IN (N'Gear', N'Officer')");
}

Expand Down
Loading

0 comments on commit f7b72a9

Please sign in to comment.