Skip to content

Commit

Permalink
Query: Use aliases when doing pushdown (#22920)
Browse files Browse the repository at this point in the history
What is life if full of care
If we have no time to stand and stare

Sqlite for some reason does not allow aliases - "True"/"False"

Resolves #22915
  • Loading branch information
smitpatel committed Oct 8, 2020
1 parent e3a059b commit 7122369
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ public IDictionary<SqlExpression, ColumnExpression> PushdownIntoSubquery()
else
{
var innerColumn = (SqlExpression)mapping.Value;
var outerColumn = subquery.GenerateOuterColumn(innerColumn);
var outerColumn = subquery.GenerateOuterColumn(innerColumn, mapping.Key.Last?.Name);
projectionMap[innerColumn] = outerColumn;
_projectionMapping[mapping.Key] = outerColumn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5265,7 +5265,7 @@ public virtual Task Select_subquery_projecting_multiple_constants_inside_anonymo
{
s.Name,
Gear = s.Members.Where(g => g.HasSoulPatch).Select(
g => new { True = true, False = false }).FirstOrDefault()
g => new { True1 = true, False1 = false }).FirstOrDefault()
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5935,14 +5935,14 @@ public override async Task Composite_key_join_on_groupby_aggregate_projecting_on
await base.Composite_key_join_on_groupby_aggregate_projecting_only_grouping_key(async);

AssertSql(
@"SELECT [t].[c]
@"SELECT [t].[Key]
FROM [LevelOne] AS [l]
INNER JOIN (
SELECT [l0].[Id] % 3 AS [c], COALESCE(SUM([l0].[Id]), 0) AS [c0]
SELECT [l0].[Id] % 3 AS [Key], COALESCE(SUM([l0].[Id]), 0) AS [Sum]
FROM [LevelTwo] AS [l0]
GROUP BY [l0].[Id] % 3
) AS [t] ON ([l].[Id] = [t].[c]) AND (CAST(1 AS bit) = CASE
WHEN [t].[c0] > 10 THEN CAST(1 AS bit)
) AS [t] ON ([l].[Id] = [t].[Key]) AND (CAST(1 AS bit) = CASE
WHEN [t].[Sum] > 10 THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END)");
}
Expand All @@ -5959,11 +5959,11 @@ END AS [Foo]
FROM [LevelOne] AS [l]
LEFT JOIN [LevelTwo] AS [l0] ON [l].[Id] = [l0].[Level1_Optional_Id]
LEFT JOIN (
SELECT [l1].[Name], COUNT(*) AS [c]
SELECT [l1].[Name] AS [Key], COUNT(*) AS [Count]
FROM [LevelThree] AS [l1]
GROUP BY [l1].[Name]
) AS [t] ON [l].[Name] = [t].[Name]
WHERE [l0].[Name] IS NOT NULL OR ([t].[c] > 0)");
) AS [t] ON [l].[Name] = [t].[Key]
WHERE [l0].[Name] IS NOT NULL OR ([t].[Count] > 0)");
}

public override async Task Collection_FirstOrDefault_property_accesses_in_projection(bool async)
Expand Down Expand Up @@ -6034,13 +6034,12 @@ public override async Task Projecting_columns_with_same_name_from_different_enti
{
await base.Projecting_columns_with_same_name_from_different_entities_making_sure_aliasing_works_after_Distinct(async);

// see #22915
AssertSql(
@"@__p_0='10'
SELECT [t].[Id] AS [Foo], [t].[Id0] AS [Bar], [t].[Id1] AS [Baz]
SELECT [t].[Id1] AS [Foo], [t].[Id2] AS [Bar], [t].[Id3] AS [Baz]
FROM (
SELECT DISTINCT TOP(@__p_0) [l].[Id], [l0].[Id] AS [Id0], [l1].[Id] AS [Id1], [l].[Name], [l0].[Name] AS [Name0]
SELECT DISTINCT TOP(@__p_0) [l].[Id] AS [Id1], [l0].[Id] AS [Id2], [l1].[Id] AS [Id3], [l].[Name] AS [Name1], [l0].[Name] AS [Name2]
FROM [LevelOne] AS [l]
INNER JOIN [LevelTwo] AS [l0] ON [l].[Id] = [l0].[Level1_Optional_Id]
INNER JOIN [LevelThree] AS [l1] ON [l0].[Id] = [l1].[Level2_Optional_Id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4782,12 +4782,12 @@ public override async Task Select_subquery_projecting_single_constant_inside_ano
await base.Select_subquery_projecting_single_constant_inside_anonymous(async);

AssertSql(
@"SELECT [s].[Name], [t0].[c]
@"SELECT [s].[Name], [t0].[One]
FROM [Squads] AS [s]
LEFT JOIN (
SELECT [t].[c], [t].[SquadId]
SELECT [t].[One], [t].[SquadId]
FROM (
SELECT 1 AS [c], [g].[SquadId], ROW_NUMBER() OVER(PARTITION BY [g].[SquadId] ORDER BY [g].[Nickname], [g].[SquadId]) AS [row]
SELECT 1 AS [One], [g].[SquadId], ROW_NUMBER() OVER(PARTITION BY [g].[SquadId] ORDER BY [g].[Nickname], [g].[SquadId]) AS [row]
FROM [Gears] AS [g]
WHERE [g].[HasSoulPatch] = CAST(1 AS bit)
) AS [t]
Expand All @@ -4800,12 +4800,12 @@ public override async Task Select_subquery_projecting_multiple_constants_inside_
await base.Select_subquery_projecting_multiple_constants_inside_anonymous(async);

AssertSql(
@"SELECT [s].[Name], [t0].[c], [t0].[c0], [t0].[c1]
@"SELECT [s].[Name], [t0].[True1], [t0].[False1], [t0].[c]
FROM [Squads] AS [s]
LEFT JOIN (
SELECT [t].[c], [t].[c0], [t].[c1], [t].[SquadId]
SELECT [t].[True1], [t].[False1], [t].[c], [t].[SquadId]
FROM (
SELECT CAST(1 AS bit) AS [c], CAST(0 AS bit) AS [c0], 1 AS [c1], [g].[SquadId], ROW_NUMBER() OVER(PARTITION BY [g].[SquadId] ORDER BY [g].[Nickname], [g].[SquadId]) AS [row]
SELECT CAST(1 AS bit) AS [True1], CAST(0 AS bit) AS [False1], 1 AS [c], [g].[SquadId], ROW_NUMBER() OVER(PARTITION BY [g].[SquadId] ORDER BY [g].[Nickname], [g].[SquadId]) AS [row]
FROM [Gears] AS [g]
WHERE [g].[HasSoulPatch] = CAST(1 AS bit)
) AS [t]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1314,13 +1314,13 @@ public override async Task GroupBy_Aggregate_Join(bool async)
AssertSql(
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [o0].[OrderID], [o0].[CustomerID], [o0].[EmployeeID], [o0].[OrderDate]
FROM (
SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [c]
SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [LastOrderID]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
HAVING COUNT(*) > 5
) AS [t]
INNER JOIN [Customers] AS [c] ON [t].[CustomerID] = [c].[CustomerID]
INNER JOIN [Orders] AS [o0] ON [t].[c] = [o0].[OrderID]");
INNER JOIN [Orders] AS [o0] ON [t].[LastOrderID] = [o0].[OrderID]");
}

public override async Task Join_GroupBy_Aggregate_multijoins(bool async)
Expand All @@ -1331,23 +1331,23 @@ public override async Task Join_GroupBy_Aggregate_multijoins(bool async)
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [o0].[OrderID], [o0].[CustomerID], [o0].[EmployeeID], [o0].[OrderDate]
FROM [Customers] AS [c]
INNER JOIN (
SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [c]
SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [LastOrderID]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
HAVING COUNT(*) > 5
) AS [t] ON [c].[CustomerID] = [t].[CustomerID]
INNER JOIN [Orders] AS [o0] ON [t].[c] = [o0].[OrderID]");
INNER JOIN [Orders] AS [o0] ON [t].[LastOrderID] = [o0].[OrderID]");
}

public override async Task Join_GroupBy_Aggregate_single_join(bool async)
{
await base.Join_GroupBy_Aggregate_single_join(async);

AssertSql(
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[c] AS [LastOrderID]
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[LastOrderID]
FROM [Customers] AS [c]
INNER JOIN (
SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [c]
SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [LastOrderID]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
HAVING COUNT(*) > 5
Expand All @@ -1359,10 +1359,10 @@ public override async Task Join_GroupBy_Aggregate_with_another_join(bool async)
await base.Join_GroupBy_Aggregate_with_another_join(async);

AssertSql(
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[c] AS [LastOrderID], [o0].[OrderID]
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[LastOrderID], [o0].[OrderID]
FROM [Customers] AS [c]
INNER JOIN (
SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [c]
SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [LastOrderID]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
HAVING COUNT(*) > 5
Expand All @@ -1375,10 +1375,10 @@ public override async Task Join_GroupBy_Aggregate_with_left_join(bool async)
await base.Join_GroupBy_Aggregate_with_left_join(async);

AssertSql(
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[c] AS [LastOrderID]
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[LastOrderID]
FROM [Customers] AS [c]
LEFT JOIN (
SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [c]
SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [LastOrderID]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
HAVING COUNT(*) > 5
Expand Down Expand Up @@ -1411,14 +1411,14 @@ public override async Task Join_GroupBy_Aggregate_on_key(bool async)
await base.Join_GroupBy_Aggregate_on_key(async);

AssertSql(
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[c] AS [LastOrderID]
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[LastOrderID]
FROM [Customers] AS [c]
INNER JOIN (
SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [c]
SELECT [o].[CustomerID] AS [Key], MAX([o].[OrderID]) AS [LastOrderID]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
HAVING COUNT(*) > 5
) AS [t] ON [c].[CustomerID] = [t].[CustomerID]");
) AS [t] ON [c].[CustomerID] = [t].[Key]");
}

public override async Task GroupBy_with_result_selector(bool async)
Expand Down Expand Up @@ -2091,12 +2091,12 @@ public override async Task GroupBy_based_on_renamed_property_complex(bool async)
await base.GroupBy_based_on_renamed_property_complex(async);

AssertSql(
@"SELECT [t].[City] AS [Key], COUNT(*) AS [Count]
@"SELECT [t].[Renamed] AS [Key], COUNT(*) AS [Count]
FROM (
SELECT DISTINCT [c].[City], [c].[CustomerID]
SELECT DISTINCT [c].[City] AS [Renamed], [c].[CustomerID]
FROM [Customers] AS [c]
) AS [t]
GROUP BY [t].[City]");
GROUP BY [t].[Renamed]");
}

public override async Task Join_groupby_anonymous_orderby_anonymous_projection(bool async)
Expand Down Expand Up @@ -2267,13 +2267,13 @@ public override async Task GroupBy_aggregate_join_with_grouping_key(bool async)
await base.GroupBy_aggregate_join_with_grouping_key(async);

AssertSql(
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[c] AS [Count]
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[Count]
FROM (
SELECT [o].[CustomerID], COUNT(*) AS [c]
SELECT [o].[CustomerID] AS [Key], COUNT(*) AS [Count]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
) AS [t]
INNER JOIN [Customers] AS [c] ON [t].[CustomerID] = [c].[CustomerID]");
INNER JOIN [Customers] AS [c] ON [t].[Key] = [c].[CustomerID]");
}

public override async Task GroupBy_aggregate_join_with_group_result(bool async)
Expand All @@ -2283,11 +2283,11 @@ public override async Task GroupBy_aggregate_join_with_group_result(bool async)
AssertSql(
@"SELECT [o0].[OrderID], [o0].[CustomerID], [o0].[EmployeeID], [o0].[OrderDate]
FROM (
SELECT [o].[CustomerID], MAX([o].[OrderDate]) AS [c]
SELECT [o].[CustomerID] AS [Key], MAX([o].[OrderDate]) AS [LastOrderDate]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
) AS [t]
INNER JOIN [Orders] AS [o0] ON (([t].[CustomerID] = [o0].[CustomerID]) OR ([t].[CustomerID] IS NULL AND [o0].[CustomerID] IS NULL)) AND (([t].[c] = [o0].[OrderDate]) OR ([t].[c] IS NULL AND [o0].[OrderDate] IS NULL))");
INNER JOIN [Orders] AS [o0] ON (([t].[Key] = [o0].[CustomerID]) OR ([t].[Key] IS NULL AND [o0].[CustomerID] IS NULL)) AND (([t].[LastOrderDate] = [o0].[OrderDate]) OR ([t].[LastOrderDate] IS NULL AND [o0].[OrderDate] IS NULL))");
}

public override async Task GroupBy_aggregate_from_right_side_of_join(bool async)
Expand All @@ -2297,14 +2297,14 @@ public override async Task GroupBy_aggregate_from_right_side_of_join(bool async)
AssertSql(
@"@__p_0='10'
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[c] AS [Max]
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[Max]
FROM [Customers] AS [c]
INNER JOIN (
SELECT [o].[CustomerID], MAX([o].[OrderDate]) AS [c]
SELECT [o].[CustomerID] AS [Key], MAX([o].[OrderDate]) AS [Max]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
) AS [t] ON [c].[CustomerID] = [t].[CustomerID]
ORDER BY [t].[c], [c].[CustomerID]
) AS [t] ON [c].[CustomerID] = [t].[Key]
ORDER BY [t].[Max], [c].[CustomerID]
OFFSET @__p_0 ROWS FETCH NEXT @__p_0 ROWS ONLY");
}

Expand All @@ -2313,18 +2313,18 @@ public override async Task GroupBy_aggregate_join_another_GroupBy_aggregate(bool
await base.GroupBy_aggregate_join_another_GroupBy_aggregate(async);

AssertSql(
@"SELECT [t].[CustomerID] AS [Key], [t].[c] AS [Total], [t0].[c] AS [ThatYear]
@"SELECT [t].[Key], [t].[Total], [t0].[ThatYear]
FROM (
SELECT [o].[CustomerID], COUNT(*) AS [c]
SELECT [o].[CustomerID] AS [Key], COUNT(*) AS [Total]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
) AS [t]
INNER JOIN (
SELECT [o0].[CustomerID], COUNT(*) AS [c]
SELECT [o0].[CustomerID] AS [Key], COUNT(*) AS [ThatYear]
FROM [Orders] AS [o0]
WHERE DATEPART(year, [o0].[OrderDate]) = 1997
GROUP BY [o0].[CustomerID]
) AS [t0] ON [t].[CustomerID] = [t0].[CustomerID]");
) AS [t0] ON [t].[Key] = [t0].[Key]");
}

public override async Task GroupBy_with_grouping_key_using_Like(bool async)
Expand Down
Loading

0 comments on commit 7122369

Please sign in to comment.