-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowFunctionMethodCallTranslator.cs
More file actions
67 lines (56 loc) · 2.96 KB
/
WindowFunctionMethodCallTranslator.cs
File metadata and controls
67 lines (56 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Reflection;
using ExpressiveSharp.EntityFrameworkCore.Relational.WindowFunctions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
namespace ExpressiveSharp.EntityFrameworkCore.Relational.Infrastructure.Internal;
/// <summary>
/// Translates <see cref="WindowFunction"/> static methods into SQL window function expressions.
/// ROW_NUMBER uses the built-in <see cref="RowNumberExpression"/>;
/// RANK, DENSE_RANK, and NTILE use <see cref="WindowFunctionSqlExpression"/>.
/// </summary>
internal sealed class WindowFunctionMethodCallTranslator : IMethodCallTranslator
{
private readonly ISqlExpressionFactory _sqlExpressionFactory;
private readonly IRelationalTypeMappingSource _typeMappingSource;
public WindowFunctionMethodCallTranslator(
ISqlExpressionFactory sqlExpressionFactory,
IRelationalTypeMappingSource typeMappingSource)
{
_sqlExpressionFactory = sqlExpressionFactory;
_typeMappingSource = typeMappingSource;
}
public SqlExpression? Translate(
SqlExpression? instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (method.DeclaringType != typeof(WindowFunction))
return null;
var longTypeMapping = _typeMappingSource.FindMapping(typeof(long))!;
return method.Name switch
{
nameof(WindowFunction.RowNumber) when arguments.Count == 1 && arguments[0] is WindowSpecSqlExpression spec
=> new RowNumberExpression(spec.Partitions, spec.Orderings, longTypeMapping),
nameof(WindowFunction.RowNumber) when arguments.Count == 0
=> new RowNumberExpression(
[],
[new OrderingExpression(
_sqlExpressionFactory.Fragment("(SELECT NULL)"),
ascending: true)],
longTypeMapping),
nameof(WindowFunction.Rank) when arguments.Count >= 1 && arguments[0] is WindowSpecSqlExpression spec
=> new WindowFunctionSqlExpression("RANK", [], spec.Partitions, spec.Orderings, typeof(long), longTypeMapping),
nameof(WindowFunction.DenseRank) when arguments.Count >= 1 && arguments[0] is WindowSpecSqlExpression spec
=> new WindowFunctionSqlExpression("DENSE_RANK", [], spec.Partitions, spec.Orderings, typeof(long), longTypeMapping),
nameof(WindowFunction.Ntile) when arguments.Count >= 2 && arguments[1] is WindowSpecSqlExpression spec
=> new WindowFunctionSqlExpression("NTILE",
[_sqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0])],
spec.Partitions, spec.Orderings, typeof(long), longTypeMapping),
_ => null
};
}
}