Skip to content

Commit

Permalink
Query: Pushdown SelectExpression when adding skip over skip
Browse files Browse the repository at this point in the history
Resolves #11166
  • Loading branch information
smitpatel committed Mar 9, 2018
1 parent 9172c66 commit 1697f0c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/EFCore.Relational/Query/Expressions/SelectExpression.cs
Expand Up @@ -204,7 +204,8 @@ public virtual Expression Offset
[param: CanBeNull]
set
{
if (_limit != null
if ((_limit != null
|| _offset != null)
&& value != null)
{
PushDownSubquery();
Expand Down
13 changes: 13 additions & 0 deletions src/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs
Expand Up @@ -3228,6 +3228,19 @@ public virtual void OrderBy_skip_take()
entryCount: 8);
}

[ConditionalFact]
public virtual void OrderBy_skip_skip_take()
{
AssertQuery<Customer>(
cs => cs.OrderBy(c => c.ContactTitle)
.ThenBy(c => c.ContactName)
.Skip(5)
.Skip(8)
.Take(3),
assertOrder: true,
entryCount: 3);
}

[ConditionalFact]
public virtual void OrderBy_skip_take_take()
{
Expand Down
24 changes: 24 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/Query/RowNumberPagingTest.cs
Expand Up @@ -253,6 +253,30 @@ public override void OrderBy_skip_take()
WHERE ([t].[__RowNumber__] > @__p_0) AND ([t].[__RowNumber__] <= (@__p_0 + @__p_1))");
}

public override void OrderBy_skip_skip_take()
{
base.OrderBy_skip_skip_take();

AssertSql(
@"@__p_0='5'
@__p_1='8'
@__p_2='3'
SELECT [t1].*
FROM (
SELECT [t].*, ROW_NUMBER() OVER(ORDER BY [t].[ContactTitle], [t].[ContactName]) AS [__RowNumber__1]
FROM (
SELECT [t0].[CustomerID], [t0].[Address], [t0].[City], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Country], [t0].[Fax], [t0].[Phone], [t0].[PostalCode], [t0].[Region]
FROM (
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], ROW_NUMBER() OVER(ORDER BY [c].[ContactTitle], [c].[ContactName]) AS [__RowNumber__]
FROM [Customers] AS [c]
) AS [t0]
WHERE [t0].[__RowNumber__] > @__p_0
) AS [t]
) AS [t1]
WHERE ([t1].[__RowNumber__1] > @__p_1) AND ([t1].[__RowNumber__1] <= (@__p_1 + @__p_2))");
}

public override void OrderBy_skip_take_take()
{
base.OrderBy_skip_take_take();
Expand Down
Expand Up @@ -3181,6 +3181,27 @@ public override void OrderBy_skip_take()
OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY");
}

[SqlServerCondition(SqlServerCondition.SupportsOffset)]
public override void OrderBy_skip_skip_take()
{
base.OrderBy_skip_skip_take();

AssertSql(
@"@__p_0='5'
@__p_1='8'
@__p_2='3'
SELECT [t].*
FROM (
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
ORDER BY [c].[ContactTitle], [c].[ContactName]
OFFSET @__p_0 ROWS
) AS [t]
ORDER BY [t].[ContactTitle], [t].[ContactName]
OFFSET @__p_1 ROWS FETCH NEXT @__p_2 ROWS ONLY");
}

[SqlServerCondition(SqlServerCondition.SupportsOffset)]
public override void OrderBy_skip_take_take()
{
Expand Down

0 comments on commit 1697f0c

Please sign in to comment.