-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query: Dedupe & simplify materializer
- Loading branch information
Showing
9 changed files
with
226 additions
and
104 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
18 changes: 18 additions & 0 deletions
18
src/EFCore.InMemory/Query/PipeLine/InMemoryShapedQueryOptimizer.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,18 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Linq.Expressions; | ||
using Microsoft.EntityFrameworkCore.Query.Pipeline; | ||
|
||
namespace Microsoft.EntityFrameworkCore.InMemory.Query.Pipeline | ||
{ | ||
public class InMemoryShapedQueryOptimizer : ShapedQueryOptimizer | ||
{ | ||
public override Expression Visit(Expression query) | ||
{ | ||
query = base.Visit(query); | ||
|
||
return query; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/EFCore.InMemory/Query/PipeLine/InMemoryShapedQueryOptimizerFactory.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,15 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.EntityFrameworkCore.Query.Pipeline; | ||
|
||
namespace Microsoft.EntityFrameworkCore.InMemory.Query.Pipeline | ||
{ | ||
public class InMemoryShapedQueryOptimizerFactory : ShapedQueryOptimizerFactory | ||
{ | ||
public override ShapedQueryOptimizer Create(QueryCompilationContext2 queryCompilationContext) | ||
{ | ||
return new InMemoryShapedQueryOptimizer(); | ||
} | ||
} | ||
} |
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
86 changes: 86 additions & 0 deletions
86
src/EFCore.Relational/Query/PipeLine/ShaperExpressionDedupingExpressionVisitor.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,86 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using Microsoft.EntityFrameworkCore.Query.Pipeline; | ||
using Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline | ||
{ | ||
public class ShaperExpressionDedupingExpressionVisitor : ExpressionVisitor | ||
{ | ||
private SelectExpression _selectExpression; | ||
private IDictionary<Expression, IList<Expression>> _duplicateShapers; | ||
|
||
public Expression Process(Expression expression) | ||
{ | ||
if (expression is ShapedQueryExpression shapedQueryExpression) | ||
{ | ||
_selectExpression = (SelectExpression)shapedQueryExpression.QueryExpression; | ||
_duplicateShapers = new Dictionary<Expression, IList<Expression>>(); | ||
Visit(shapedQueryExpression.ShaperExpression); | ||
|
||
var variables = new List<ParameterExpression>(); | ||
var expressions = new List<Expression>(); | ||
var replacements = new Dictionary<Expression, Expression>(); | ||
var index = 0; | ||
|
||
foreach (var kvp in _duplicateShapers) | ||
{ | ||
if (kvp.Value.Count > 1) | ||
{ | ||
var firstShaper = kvp.Value[0]; | ||
var entityParameter = Expression.Parameter(firstShaper.Type, $"entity{index++}"); | ||
variables.Add(entityParameter); | ||
expressions.Add(Expression.Assign( | ||
entityParameter, | ||
firstShaper)); | ||
|
||
foreach (var shaper in kvp.Value) | ||
{ | ||
replacements[shaper] = entityParameter; | ||
} | ||
} | ||
} | ||
|
||
if (variables.Count == 0) | ||
{ | ||
return shapedQueryExpression; | ||
} | ||
|
||
expressions.Add(new ReplacingExpressionVisitor(replacements) | ||
.Visit(shapedQueryExpression.ShaperExpression)); | ||
|
||
shapedQueryExpression.ShaperExpression = Expression.Block(variables, expressions); | ||
|
||
return shapedQueryExpression; | ||
} | ||
|
||
return expression; | ||
} | ||
|
||
protected override Expression VisitExtension(Expression extensionExpression) | ||
{ | ||
if (extensionExpression is EntityShaperExpression entityShaperExpression) | ||
{ | ||
var serverProjection = _selectExpression.GetProjectionExpression( | ||
entityShaperExpression.ValueBufferExpression.ProjectionMember); | ||
|
||
if (_duplicateShapers.ContainsKey(serverProjection)) | ||
{ | ||
_duplicateShapers[serverProjection].Add(entityShaperExpression); | ||
} | ||
else | ||
{ | ||
_duplicateShapers[serverProjection] = new List<Expression> { entityShaperExpression }; | ||
} | ||
|
||
return entityShaperExpression; | ||
} | ||
|
||
return base.VisitExtension(extensionExpression); | ||
} | ||
} | ||
} |
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