-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flatten nested queries generated by OData $skip and $select combinati…
…ons.
- Loading branch information
1 parent
e7126dc
commit 2aaa2c2
Showing
8 changed files
with
167 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
source/Lucene.Net.Linq.Tests/Integration/SubQueryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System.Linq; | ||
using NUnit.Framework; | ||
|
||
namespace Lucene.Net.Linq.Tests.Integration | ||
{ | ||
[TestFixture] | ||
public class SubQueryTests : IntegrationTestBase | ||
{ | ||
[SetUp] | ||
public void SetUp() | ||
{ | ||
AddDocument(new SampleDocument { Id = "a", Scalar = 5}); | ||
AddDocument(new SampleDocument { Id = "b", Scalar = 1}); | ||
AddDocument(new SampleDocument { Id = "c", Scalar = 3}); | ||
} | ||
|
||
[Test] | ||
public void SubquerySkip() | ||
{ | ||
var docs = provider.AsQueryable<SampleDocument>(); | ||
|
||
var results = (from outer in | ||
(from d in docs | ||
orderby d.Id | ||
select d).Skip(2) | ||
select new { outer.Id }) | ||
.ToList(); | ||
|
||
Assert.That(results.Select(d => d.Id).ToArray(), Is.EqualTo(new[] { "c" })); | ||
} | ||
|
||
[Test] | ||
public void MainSkipSubquerySkip() | ||
{ | ||
var docs = provider.AsQueryable<SampleDocument>(); | ||
|
||
var results = (from outer in | ||
(from d in docs | ||
orderby d.Id descending | ||
select d).Skip(1) | ||
select new { outer.Id }) | ||
.Skip(1) | ||
.ToList(); | ||
|
||
Assert.That(results.Select(d => d.Id).ToArray(), Is.EqualTo(new[] { "a" })); | ||
} | ||
|
||
[Test] | ||
public void MainOrderBy() | ||
{ | ||
var docs = provider.AsQueryable<SampleDocument>(); | ||
|
||
var results = (from outer in | ||
(from d in docs select d) | ||
orderby outer.Id descending | ||
select new { outer.Id }) | ||
.ToList(); | ||
|
||
Assert.That(results.Select(d => d.Id).ToArray(), Is.EqualTo(new[] { "c", "b", "a" })); | ||
} | ||
|
||
[Test] | ||
public void SubQueryOrderWinsOnConflictingOrder() | ||
{ | ||
var docs = provider.AsQueryable<SampleDocument>(); | ||
|
||
var results = (from outer in | ||
(from d in docs | ||
orderby d.Scalar | ||
select d).Skip(1) | ||
orderby outer.Id | ||
select new { outer.Id }) | ||
.Skip(1) | ||
.ToList(); | ||
|
||
Assert.That(results.Select(d => d.Id).ToArray(), Is.EqualTo(new[] { "a" })); | ||
} | ||
|
||
[Test] | ||
public void AllowsConflictingOrderOnNoSkipTake() | ||
{ | ||
var docs = provider.AsQueryable<SampleDocument>(); | ||
|
||
var results = (from outer in | ||
(from d in docs | ||
orderby d.Scalar | ||
select d) | ||
orderby outer.Id | ||
select new { outer.Id }) | ||
.ToList(); | ||
|
||
Assert.That(results.Select(d => d.Id).ToArray(), Is.EqualTo(new[] { "b", "c", "a" })); | ||
} | ||
|
||
[Test] | ||
public void AllowsSameOrderOnSubSkipTake() | ||
{ | ||
var docs = provider.AsQueryable<SampleDocument>(); | ||
|
||
var results = (from outer in | ||
(from d in docs | ||
orderby d.Scalar | ||
orderby d.Id | ||
select d).Skip(1) | ||
orderby outer.Scalar | ||
orderby outer.Id | ||
select new { outer.Id }) | ||
.ToList(); | ||
|
||
Assert.That(results.Select(d => d.Id).ToArray(), Is.EqualTo(new[] { "c", "a" })); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
source/Lucene.Net.Linq/Transformation/TreeVisitors/AggressiveSubQueryFromClauseFlattener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Linq; | ||
using Remotion.Linq; | ||
using Remotion.Linq.Clauses; | ||
using Remotion.Linq.Clauses.Expressions; | ||
using Remotion.Linq.Clauses.ResultOperators; | ||
using Remotion.Linq.Transformations; | ||
|
||
namespace Lucene.Net.Linq.Transformation.TreeVisitors | ||
{ | ||
internal class AggressiveSubQueryFromClauseFlattener : SubQueryFromClauseFlattener | ||
{ | ||
protected override void CheckFlattenable(QueryModel subQueryModel) | ||
{ | ||
var first = subQueryModel.ResultOperators.FirstOrDefault(); | ||
if (first != null) | ||
{ | ||
throw new NotSupportedException(first.GetType() + " is not supported in sub-queries. Sub-queries may only use SequenceTypePreservingResultOperatorBase subclasses."); | ||
} | ||
} | ||
|
||
protected override void FlattenSubQuery(SubQueryExpression subQueryExpression, FromClauseBase fromClause, QueryModel queryModel, | ||
int destinationIndex) | ||
{ | ||
var subQueryModel = subQueryExpression.QueryModel; | ||
MoveResultOperatorsToParent(queryModel, subQueryModel); | ||
base.FlattenSubQuery(subQueryExpression, fromClause, queryModel, destinationIndex); | ||
} | ||
|
||
protected virtual void MoveResultOperatorsToParent(QueryModel queryModel, QueryModel subQueryModel) | ||
{ | ||
foreach (var resultOperator in subQueryModel.ResultOperators.OfType<SequenceTypePreservingResultOperatorBase>().Reverse().ToList()) | ||
{ | ||
queryModel.ResultOperators.Insert(0, resultOperator); | ||
subQueryModel.ResultOperators.Remove(resultOperator); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,4 +57,4 @@ private Expression VisitQueryPredicateExpression(LuceneQueryPredicateExpression | |
return result; | ||
} | ||
} | ||
} | ||
} |