Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: Pushdown into subquery when applying over Skip after Distinct … #22249

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,8 @@ public void ApplyOffset([NotNull] SqlExpression sqlExpression)
Check.NotNull(sqlExpression, nameof(sqlExpression));

if (Limit != null
|| Offset != null)
|| Offset != null
|| (IsDistinct && Orderings.Count == 0))
{
PushdownIntoSubquery();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5619,5 +5619,31 @@ public virtual Task Member_over_null_check_ternary_and_nested_anonymous_type(boo
}
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Distinct_skip_without_orderby(bool async)
{
return AssertQuery(
async,
ss => from l1 in ss.Set<Level1>()
where l1.Id < 3
select (from l3 in ss.Set<Level3>()
orderby l3.Id
select l3).Distinct().Skip(1).OrderBy(e => e.Id).FirstOrDefault().Name);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Distinct_take_without_orderby(bool async)
{
return AssertQuery(
async,
ss => from l1 in ss.Set<Level1>()
where l1.Id < 3
select (from l3 in ss.Set<Level3>()
orderby l3.Id
select l3).Distinct().Take(1).OrderBy(e => e.Id).FirstOrDefault().Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5860,6 +5860,43 @@ FROM [LevelOne] AS [l]
ORDER BY [t].[Id], [l0].[Id]");
}

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

AssertSql(
@"SELECT (
SELECT TOP(1) [t0].[Name]
FROM (
SELECT [t].[Id], [t].[Level2_Optional_Id], [t].[Level2_Required_Id], [t].[Name], [t].[OneToMany_Optional_Inverse3Id], [t].[OneToMany_Optional_Self_Inverse3Id], [t].[OneToMany_Required_Inverse3Id], [t].[OneToMany_Required_Self_Inverse3Id], [t].[OneToOne_Optional_PK_Inverse3Id], [t].[OneToOne_Optional_Self3Id]
FROM (
SELECT DISTINCT [l].[Id], [l].[Level2_Optional_Id], [l].[Level2_Required_Id], [l].[Name], [l].[OneToMany_Optional_Inverse3Id], [l].[OneToMany_Optional_Self_Inverse3Id], [l].[OneToMany_Required_Inverse3Id], [l].[OneToMany_Required_Self_Inverse3Id], [l].[OneToOne_Optional_PK_Inverse3Id], [l].[OneToOne_Optional_Self3Id]
FROM [LevelThree] AS [l]
) AS [t]
ORDER BY (SELECT 1)
OFFSET 1 ROWS
) AS [t0]
ORDER BY [t0].[Id])
FROM [LevelOne] AS [l0]
WHERE [l0].[Id] < 3");
}

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

AssertSql(
@"SELECT (
SELECT TOP(1) [t].[Name]
FROM (
SELECT DISTINCT TOP(1) [l].[Id], [l].[Level2_Optional_Id], [l].[Level2_Required_Id], [l].[Name], [l].[OneToMany_Optional_Inverse3Id], [l].[OneToMany_Optional_Self_Inverse3Id], [l].[OneToMany_Required_Inverse3Id], [l].[OneToMany_Required_Self_Inverse3Id], [l].[OneToOne_Optional_PK_Inverse3Id], [l].[OneToOne_Optional_Self3Id]
FROM [LevelThree] AS [l]
) AS [t]
ORDER BY [t].[Id])
FROM [LevelOne] AS [l0]
WHERE [l0].[Id] < 3");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}
Expand Down