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: Encapsulate more logic about pushdown into SelectExpression #16027

Merged
merged 1 commit into from
Jun 11, 2019
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 @@ -59,17 +59,11 @@ public override ShapedQueryExpression TranslateSubquery(Expression expression)

protected override ShapedQueryExpression TranslateAll(ShapedQueryExpression source, LambdaExpression predicate)
{
var selectExpression = (SelectExpression)source.QueryExpression;
if (selectExpression.Limit != null
|| selectExpression.Offset != null)
{
selectExpression.PushdownIntoSubQuery();
}

var translation = TranslateLambdaExpression(source, predicate);

if (translation != null)
{
var selectExpression = (SelectExpression)source.QueryExpression;
selectExpression.ApplyPredicate(_sqlExpressionFactory.Not(translation));
selectExpression.ReplaceProjectionMapping(new Dictionary<ProjectionMember, Expression>());
if (selectExpression.Limit == null
Expand Down Expand Up @@ -113,11 +107,7 @@ protected override ShapedQueryExpression TranslateAny(ShapedQueryExpression sour
protected override ShapedQueryExpression TranslateAverage(ShapedQueryExpression source, LambdaExpression selector, Type resultType)
{
var selectExpression = (SelectExpression)source.QueryExpression;
if (selectExpression.Limit != null
|| selectExpression.Offset != null)
{
selectExpression.PushdownIntoSubQuery();
}
selectExpression.PrepareForAggregate();

if (selector != null)
{
Expand Down Expand Up @@ -192,13 +182,7 @@ protected override ShapedQueryExpression TranslateContains(ShapedQueryExpression
protected override ShapedQueryExpression TranslateCount(ShapedQueryExpression source, LambdaExpression predicate)
{
var selectExpression = (SelectExpression)source.QueryExpression;

if (selectExpression.IsDistinct
|| selectExpression.Limit != null
|| selectExpression.Offset != null)
{
selectExpression.PushdownIntoSubQuery();
}
selectExpression.PrepareForAggregate();

if (predicate != null)
{
Expand Down Expand Up @@ -307,19 +291,6 @@ protected override ShapedQueryExpression TranslateJoin(
LambdaExpression innerKeySelector,
LambdaExpression resultSelector)
{
// TODO: write a test which has distinct on outer so that we can verify pushdown
var innerSelectExpression = (SelectExpression)inner.QueryExpression;
if (innerSelectExpression.Orderings.Any()
|| innerSelectExpression.Limit != null
|| innerSelectExpression.Offset != null
|| innerSelectExpression.IsDistinct
// TODO: Predicate can be lifted in inner join
|| innerSelectExpression.Predicate != null
|| innerSelectExpression.Tables.Count > 1)
{
innerSelectExpression.PushdownIntoSubQuery();
}

var joinPredicate = CreateJoinPredicate(outer, outerKeySelector, inner, innerKeySelector);
if (joinPredicate != null)
{
Expand All @@ -328,7 +299,7 @@ protected override ShapedQueryExpression TranslateJoin(
resultSelector.Parameters[1].Type);

((SelectExpression)outer.QueryExpression).AddInnerJoin(
innerSelectExpression, joinPredicate, transparentIdentifierType);
(SelectExpression)inner.QueryExpression, joinPredicate, transparentIdentifierType);

return TranslateResultSelectorForJoin(
outer,
Expand All @@ -343,34 +314,15 @@ protected override ShapedQueryExpression TranslateJoin(

protected override ShapedQueryExpression TranslateLeftJoin(ShapedQueryExpression outer, ShapedQueryExpression inner, LambdaExpression outerKeySelector, LambdaExpression innerKeySelector, LambdaExpression resultSelector)
{
var outerSelectExpression = (SelectExpression)outer.QueryExpression;
if (outerSelectExpression.Limit != null
|| outerSelectExpression.Offset != null
|| outerSelectExpression.IsDistinct)
{
outerSelectExpression.PushdownIntoSubQuery();
}

var innerSelectExpression = (SelectExpression)inner.QueryExpression;
if (innerSelectExpression.Orderings.Any()
|| innerSelectExpression.Limit != null
|| innerSelectExpression.Offset != null
|| innerSelectExpression.IsDistinct
|| innerSelectExpression.Predicate != null
|| innerSelectExpression.Tables.Count > 1)
{
innerSelectExpression.PushdownIntoSubQuery();
}

var joinPredicate = CreateJoinPredicate(outer, outerKeySelector, inner, innerKeySelector);
if (joinPredicate != null)
{
var transparentIdentifierType = CreateTransparentIdentifierType(
resultSelector.Parameters[0].Type,
resultSelector.Parameters[1].Type);

outerSelectExpression.AddLeftJoin(
innerSelectExpression, joinPredicate, transparentIdentifierType);
((SelectExpression)outer.QueryExpression).AddLeftJoin(
(SelectExpression)inner.QueryExpression, joinPredicate, transparentIdentifierType);

return TranslateResultSelectorForJoin(
outer,
Expand Down Expand Up @@ -447,13 +399,6 @@ protected override ShapedQueryExpression TranslateLastOrDefault(
}

var selectExpression = (SelectExpression)source.QueryExpression;

if (selectExpression.Limit != null
|| selectExpression.Offset != null)
{
selectExpression.PushdownIntoSubQuery();
}

selectExpression.ReverseOrderings();
selectExpression.ApplyLimit(TranslateExpression(Expression.Constant(1)));

Expand All @@ -468,13 +413,7 @@ protected override ShapedQueryExpression TranslateLastOrDefault(
protected override ShapedQueryExpression TranslateLongCount(ShapedQueryExpression source, LambdaExpression predicate)
{
var selectExpression = (SelectExpression)source.QueryExpression;

if (selectExpression.IsDistinct
|| selectExpression.Limit != null
|| selectExpression.Offset != null)
{
selectExpression.PushdownIntoSubQuery();
}
selectExpression.PrepareForAggregate();

if (predicate != null)
{
Expand All @@ -498,11 +437,7 @@ protected override ShapedQueryExpression TranslateLongCount(ShapedQueryExpressio
protected override ShapedQueryExpression TranslateMax(ShapedQueryExpression source, LambdaExpression selector, Type resultType)
{
var selectExpression = (SelectExpression)source.QueryExpression;
if (selectExpression.Limit != null
|| selectExpression.Offset != null)
{
selectExpression.PushdownIntoSubQuery();
}
selectExpression.PrepareForAggregate();

if (selector != null)
{
Expand All @@ -519,11 +454,7 @@ protected override ShapedQueryExpression TranslateMax(ShapedQueryExpression sour
protected override ShapedQueryExpression TranslateMin(ShapedQueryExpression source, LambdaExpression selector, Type resultType)
{
var selectExpression = (SelectExpression)source.QueryExpression;
if (selectExpression.Limit != null
|| selectExpression.Offset != null)
{
selectExpression.PushdownIntoSubQuery();
}
selectExpression.PrepareForAggregate();
smitpatel marked this conversation as resolved.
Show resolved Hide resolved

if (selector != null)
{
Expand Down Expand Up @@ -600,19 +531,10 @@ protected override ShapedQueryExpression TranslateOfType(ShapedQueryExpression s

protected override ShapedQueryExpression TranslateOrderBy(ShapedQueryExpression source, LambdaExpression keySelector, bool ascending)
{
var selectExpression = (SelectExpression)source.QueryExpression;
if (selectExpression.IsDistinct
|| selectExpression.Limit != null
|| selectExpression.Offset != null)
{
selectExpression.PushdownIntoSubQuery();
}

var translation = TranslateLambdaExpression(source, keySelector);

if (translation != null)
{
selectExpression.ApplyOrdering(new OrderingExpression(translation, ascending));
((SelectExpression)source.QueryExpression).ApplyOrdering(new OrderingExpression(translation, ascending));

return source;
}
Expand All @@ -632,7 +554,7 @@ protected override ShapedQueryExpression TranslateSelect(ShapedQueryExpression s
var selectExpression = (SelectExpression)source.QueryExpression;
if (selectExpression.IsDistinct)
{
selectExpression.PushdownIntoSubQuery();
selectExpression.PushdownIntoSubquery();
}

var newSelectorBody = ReplacingExpressionVisitor.Replace(selector.Parameters.Single(), source.ShaperExpression, selector.Body);
Expand Down Expand Up @@ -669,31 +591,11 @@ protected override ShapedQueryExpression TranslateSelectMany(ShapedQueryExpressi
{
if (Visit(collectionSelectorBody) is ShapedQueryExpression inner)
{
var outerSelectExpression = (SelectExpression)source.QueryExpression;
if (outerSelectExpression.Limit != null
|| outerSelectExpression.Offset != null
|| outerSelectExpression.IsDistinct
|| outerSelectExpression.Predicate != null)
{
outerSelectExpression.PushdownIntoSubQuery();
}

var innerSelectExpression = (SelectExpression)inner.QueryExpression;
if (innerSelectExpression.Orderings.Any()
|| innerSelectExpression.Limit != null
|| innerSelectExpression.Offset != null
|| innerSelectExpression.IsDistinct
|| innerSelectExpression.Predicate != null)
{
innerSelectExpression.PushdownIntoSubQuery();
}

var transparentIdentifierType = CreateTransparentIdentifierType(
resultSelector.Parameters[0].Type,
resultSelector.Parameters[1].Type);

outerSelectExpression.AddCrossJoin(
innerSelectExpression, transparentIdentifierType);
((SelectExpression)source.QueryExpression).AddCrossJoin(
(SelectExpression)inner.QueryExpression, transparentIdentifierType);

return TranslateResultSelectorForJoin(
source,
Expand Down Expand Up @@ -775,11 +677,7 @@ protected override ShapedQueryExpression TranslateSkip(ShapedQueryExpression sou
protected override ShapedQueryExpression TranslateSum(ShapedQueryExpression source, LambdaExpression selector, Type resultType)
{
var selectExpression = (SelectExpression)source.QueryExpression;
if (selectExpression.Limit != null
|| selectExpression.Offset != null)
{
selectExpression.PushdownIntoSubQuery();
}
selectExpression.PrepareForAggregate();

if (selector != null)
{
Expand Down Expand Up @@ -825,7 +723,6 @@ protected override ShapedQueryExpression TranslateTake(ShapedQueryExpression sou
protected override ShapedQueryExpression TranslateThenBy(ShapedQueryExpression source, LambdaExpression keySelector, bool ascending)
{
var translation = TranslateLambdaExpression(source, keySelector);

if (translation != null)
{
((SelectExpression)source.QueryExpression).AppendOrdering(new OrderingExpression(translation, ascending));
Expand All @@ -840,17 +737,10 @@ protected override ShapedQueryExpression TranslateThenBy(ShapedQueryExpression s

protected override ShapedQueryExpression TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate)
{
var selectExpression = (SelectExpression)source.QueryExpression;
if (selectExpression.Limit != null
|| selectExpression.Offset != null)
{
selectExpression.PushdownIntoSubQuery();
}

var translation = TranslateLambdaExpression(source, predicate);
if (translation != null)
{
selectExpression.ApplyPredicate(translation);
((SelectExpression)source.QueryExpression).ApplyPredicate(translation);

return source;
}
Expand Down
Loading