Skip to content
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 @@ -13,9 +13,7 @@ protected override QueryableSortOperation HandleOperation(
QueryableFieldSelector fieldSelector,
ISortField field,
SortEnumValue? sortEnumValue)
{
return AscendingSortOperation.From(fieldSelector);
}
=> AscendingSortOperation.From(fieldSelector);

public static QueryableAscendingSortOperationHandler Create(SortProviderContext context) => new();

Expand All @@ -28,6 +26,30 @@ private AscendingSortOperation(QueryableFieldSelector fieldSelector)

public override Expression CompileOrderBy(Expression expression)
{
// We try to push the sort through any .Select() projection so the database can sort
// before projecting. If that works, we apply the sort on the source and re-attach the projection.
if (QueryableSortExpressionOptimizer.TryRewriteSelectorToSource(
expression,
ParameterExpression,
Selector,
out var rewrittenSource,
out var rewrittenSelector,
out var projection))
{
var sortedSource = Expression.Call(
rewrittenSource.GetEnumerableKind(),
nameof(Queryable.OrderBy),
[rewrittenSelector.Parameters[0].Type, rewrittenSelector.ReturnType],
rewrittenSource,
rewrittenSelector);

return QueryableSortExpressionOptimizer.ReapplyProjection(
sortedSource,
projection);
}

// If the optimization is not possible, we fall back to a plain OrderBy on
// the expression as-is.
return Expression.Call(
expression.GetEnumerableKind(),
nameof(Queryable.OrderBy),
Expand All @@ -38,6 +60,30 @@ public override Expression CompileOrderBy(Expression expression)

public override Expression CompileThenBy(Expression expression)
{
// We try to push the sort through any .Select() projection so the database can sort
// before projecting. If that works, we apply the sort on the source and re-attach the projection.
if (QueryableSortExpressionOptimizer.TryRewriteSelectorToSource(
expression,
ParameterExpression,
Selector,
out var rewrittenSource,
out var rewrittenSelector,
out var projection))
{
var sortedSource = Expression.Call(
rewrittenSource.GetEnumerableKind(),
nameof(Queryable.ThenBy),
[rewrittenSelector.Parameters[0].Type, rewrittenSelector.ReturnType],
rewrittenSource,
rewrittenSelector);

return QueryableSortExpressionOptimizer.ReapplyProjection(
sortedSource,
projection);
}

// If the optimization is not possible, we fall back to a plain ThenBy on
// the expression as-is.
return Expression.Call(
expression.GetEnumerableKind(),
nameof(Queryable.ThenBy),
Expand All @@ -46,7 +92,7 @@ public override Expression CompileThenBy(Expression expression)
Expression.Lambda(Selector, ParameterExpression));
}

public static AscendingSortOperation From(QueryableFieldSelector selector) =>
new AscendingSortOperation(selector);
public static AscendingSortOperation From(QueryableFieldSelector selector)
=> new AscendingSortOperation(selector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ protected override QueryableSortOperation HandleOperation(
QueryableFieldSelector fieldSelector,
ISortField field,
SortEnumValue? sortEnumValue)
{
return DescendingSortOperation.From(fieldSelector);
}
=> DescendingSortOperation.From(fieldSelector);

public static QueryableDescendingSortOperationHandler Create(SortProviderContext context) => new();

Expand All @@ -28,6 +26,30 @@ private DescendingSortOperation(QueryableFieldSelector fieldSelector)

public override Expression CompileOrderBy(Expression expression)
{
// We try to push the sort through any .Select() projection so the database can sort
// before projecting. If that works, we apply the sort on the source and re-attach the projection.
if (QueryableSortExpressionOptimizer.TryRewriteSelectorToSource(
expression,
ParameterExpression,
Selector,
out var rewrittenSource,
out var rewrittenSelector,
out var projection))
{
var sortedSource = Expression.Call(
rewrittenSource.GetEnumerableKind(),
nameof(Queryable.OrderByDescending),
[rewrittenSelector.Parameters[0].Type, rewrittenSelector.ReturnType],
rewrittenSource,
rewrittenSelector);

return QueryableSortExpressionOptimizer.ReapplyProjection(
sortedSource,
projection);
}

// If the optimization is not possible, we fall back to a plain OrderByDescending on
// the expression as-is.
return Expression.Call(
expression.GetEnumerableKind(),
nameof(Queryable.OrderByDescending),
Expand All @@ -38,6 +60,30 @@ public override Expression CompileOrderBy(Expression expression)

public override Expression CompileThenBy(Expression expression)
{
// We try to push the sort through any .Select() projection so the database can sort
// before projecting. If that works, we apply the sort on the source and re-attach the projection.
if (QueryableSortExpressionOptimizer.TryRewriteSelectorToSource(
expression,
ParameterExpression,
Selector,
out var rewrittenSource,
out var rewrittenSelector,
out var projection))
{
var sortedSource = Expression.Call(
rewrittenSource.GetEnumerableKind(),
nameof(Queryable.ThenByDescending),
[rewrittenSelector.Parameters[0].Type, rewrittenSelector.ReturnType],
rewrittenSource,
rewrittenSelector);

return QueryableSortExpressionOptimizer.ReapplyProjection(
sortedSource,
projection);
}

// If the optimization is not possible, we fall back to a plain ThenByDescending on
// the expression as-is.
return Expression.Call(
expression.GetEnumerableKind(),
nameof(Queryable.ThenByDescending),
Expand All @@ -46,7 +92,7 @@ public override Expression CompileThenBy(Expression expression)
Expression.Lambda(Selector, ParameterExpression));
}

public static DescendingSortOperation From(QueryableFieldSelector selector) =>
new DescendingSortOperation(selector);
public static DescendingSortOperation From(QueryableFieldSelector selector)
=> new DescendingSortOperation(selector);
}
}
Loading
Loading