-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Push join argument down into subquery if it contains a Window Function
- Loading branch information
Showing
6 changed files
with
146 additions
and
40 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/Zomp.EFCore.WindowFunctions/Query/Internal/CompareNameAndDeclaringType.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,21 @@ | ||
namespace Zomp.EFCore.WindowFunctions.Query.Internal; | ||
|
||
internal sealed class CompareNameAndDeclaringType : IEqualityComparer<MethodInfo> | ||
{ | ||
public static CompareNameAndDeclaringType Default { get; } = new(); | ||
|
||
public bool Equals(MethodInfo? x, MethodInfo? y) | ||
{ | ||
if (x is null || y is null) | ||
{ | ||
return x is null && y is null; | ||
} | ||
|
||
return x.Name.Equals(y.Name, StringComparison.Ordinal) && x.DeclaringType == y.DeclaringType; | ||
} | ||
|
||
public int GetHashCode(MethodInfo method) | ||
{ | ||
return HashCode.Combine(method.Name.GetHashCode(StringComparison.Ordinal), method.DeclaringType?.GetHashCode()); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Zomp.EFCore.WindowFunctions/Query/Internal/JoinDetector.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,76 @@ | ||
namespace Zomp.EFCore.WindowFunctions.Query.Internal; | ||
|
||
internal sealed class JoinDetector : ExpressionVisitor | ||
{ | ||
private static readonly Dictionary<string, List<MethodInfo>> QueryableMethodGroups = typeof(Enumerable) | ||
.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly) | ||
.GroupBy(mi => mi.Name) | ||
.ToDictionary(e => e.Key, l => l.ToList()); | ||
|
||
/* | ||
private static readonly MethodInfo Where = GetMethod( | ||
nameof(Enumerable.Where), | ||
1, | ||
types => new[] { typeof(IEnumerable<>).MakeGenericType(types[0]), typeof(Func<,>).MakeGenericType(types[0], typeof(bool)) }); | ||
*/ | ||
|
||
public IList<MethodCallExpression> WindowFunctionsCollection { get; } = new List<MethodCallExpression>(); | ||
|
||
/// <inheritdoc/> | ||
protected override Expression VisitMethodCall(MethodCallExpression node) | ||
{ | ||
var method = node.Method; | ||
if (method.DeclaringType == typeof(Queryable)) | ||
{ | ||
if (method.Name == nameof(Queryable.Join)) | ||
{ | ||
var wfd = new WindowFunctionDetectorInternal(); | ||
var joinArg = node.Arguments[1]; | ||
|
||
_ = wfd.Visit(joinArg); | ||
|
||
if (wfd.WindowFunctionsCollection.Count == 0) | ||
{ | ||
return base.VisitMethodCall(node); | ||
} | ||
|
||
var @base = (MethodCallExpression)base.VisitMethodCall(node); | ||
|
||
var type = QueryableMethods.Join.MakeGenericMethod(method.GetGenericArguments()); | ||
|
||
var args = @base.Arguments; | ||
|
||
var argForSubQuery = args[1]; | ||
|
||
var asSubQueryMethod = WindowFunctionsEvaluatableExpressionFilter.AsSubQueryMethod.MakeGenericMethod(argForSubQuery.Type.GetGenericArguments()[0]); | ||
var withSubquery = Expression.Call(null, asSubQueryMethod, argForSubQuery); | ||
|
||
var joinWithSubquery = Expression.Call( | ||
null, | ||
method, | ||
args[0], | ||
withSubquery, | ||
args[2], | ||
args[3], | ||
args[4]); | ||
return joinWithSubquery; | ||
} | ||
} | ||
|
||
if (WindowFunctionsEvaluatableExpressionFilter.WindowFunctionMethods.Contains(node.Method, CompareNameAndDeclaringType.Default)) | ||
{ | ||
WindowFunctionsCollection.Add(node); | ||
} | ||
|
||
var retVal = base.VisitMethodCall(node); | ||
|
||
return retVal; | ||
} | ||
|
||
private static MethodInfo GetMethod(string name, int genericParameterCount, Func<Type[], Type[]> parameterGenerator) | ||
=> QueryableMethodGroups[name].Single( | ||
mi => ((genericParameterCount == 0 && !mi.IsGenericMethod) | ||
|| (mi.IsGenericMethod && mi.GetGenericArguments().Length == genericParameterCount)) | ||
&& mi.GetParameters().Select(e => e.ParameterType).SequenceEqual( | ||
parameterGenerator(mi.IsGenericMethod ? mi.GetGenericArguments() : []))); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Zomp.EFCore.WindowFunctions/Query/Internal/WindowFunctionDetectorInternal.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,16 @@ | ||
namespace Zomp.EFCore.WindowFunctions.Query.Internal; | ||
|
||
internal sealed class WindowFunctionDetectorInternal : ExpressionVisitor | ||
{ | ||
public IList<MethodCallExpression> WindowFunctionsCollection { get; } = []; | ||
|
||
protected override Expression VisitMethodCall(MethodCallExpression node) | ||
{ | ||
if (WindowFunctionsEvaluatableExpressionFilter.WindowFunctionMethods.Contains(node.Method, CompareNameAndDeclaringType.Default)) | ||
{ | ||
WindowFunctionsCollection.Add(node); | ||
} | ||
|
||
return base.VisitMethodCall(node); | ||
} | ||
} |
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