|
1 | 1 | using System.Collections.Generic; |
| 2 | +using System.Linq; |
2 | 3 | using System.Reflection; |
| 4 | +using EfCore.Ydb.Utilities; |
3 | 5 | using Microsoft.EntityFrameworkCore; |
4 | 6 | using Microsoft.EntityFrameworkCore.Diagnostics; |
5 | 7 | using Microsoft.EntityFrameworkCore.Query; |
6 | 8 | using Microsoft.EntityFrameworkCore.Query.SqlExpressions; |
| 9 | +using Microsoft.EntityFrameworkCore.Storage; |
7 | 10 |
|
8 | 11 | namespace EfCore.Ydb.Query.Internal.Translators; |
9 | 12 |
|
10 | | -public class YdbQueryableAggregateMethodTranslator |
11 | | - : IAggregateMethodCallTranslator |
| 13 | +public class YdbQueryableAggregateMethodTranslator : IAggregateMethodCallTranslator |
12 | 14 | { |
| 15 | + private readonly YdbSqlExpressionFactory _sqlExpressionFactory; |
| 16 | + private readonly IRelationalTypeMappingSource _typeMappingSource; |
| 17 | + |
13 | 18 | public YdbQueryableAggregateMethodTranslator( |
14 | | - YdbSqlExpressionFactory sqlExpressionFactory |
| 19 | + YdbSqlExpressionFactory sqlExpressionFactory, |
| 20 | + IRelationalTypeMappingSource typeMappingSource |
15 | 21 | ) |
16 | 22 | { |
| 23 | + _sqlExpressionFactory = sqlExpressionFactory; |
| 24 | + _typeMappingSource = typeMappingSource; |
17 | 25 | } |
18 | 26 |
|
19 | 27 | public SqlExpression? Translate( |
20 | 28 | MethodInfo method, |
21 | 29 | EnumerableExpression source, |
22 | 30 | IReadOnlyList<SqlExpression> arguments, |
23 | | - IDiagnosticsLogger<DbLoggerCategory.Query> logger) |
| 31 | + IDiagnosticsLogger<DbLoggerCategory.Query> logger |
| 32 | + ) |
24 | 33 | { |
| 34 | + if (method.DeclaringType != typeof(Queryable)) return null; |
| 35 | + |
| 36 | + var methodInfo = method.IsGenericMethod |
| 37 | + ? method.GetGenericMethodDefinition() |
| 38 | + : method; |
| 39 | + switch (methodInfo.Name) |
| 40 | + { |
| 41 | + case nameof(Queryable.Average) |
| 42 | + when (QueryableMethods.IsAverageWithoutSelector(methodInfo) |
| 43 | + || QueryableMethods.IsAverageWithSelector(methodInfo)) |
| 44 | + && source.Selector is SqlExpression averageSqlExpression: |
| 45 | + var averageInputType = averageSqlExpression.Type; |
| 46 | + if (averageInputType == typeof(int) |
| 47 | + || averageInputType == typeof(long)) |
| 48 | + { |
| 49 | + averageSqlExpression = _sqlExpressionFactory.ApplyDefaultTypeMapping( |
| 50 | + _sqlExpressionFactory.Convert(averageSqlExpression, typeof(double))); |
| 51 | + } |
| 52 | + |
| 53 | + return averageInputType == typeof(float) |
| 54 | + ? _sqlExpressionFactory.Convert( |
| 55 | + _sqlExpressionFactory.Function( |
| 56 | + "AVG", |
| 57 | + [averageSqlExpression], |
| 58 | + nullable: true, |
| 59 | + argumentsPropagateNullability: ArrayUtil.FalseArrays[1], |
| 60 | + returnType: typeof(double)), |
| 61 | + averageSqlExpression.Type, |
| 62 | + averageSqlExpression.TypeMapping) |
| 63 | + : _sqlExpressionFactory.Function( |
| 64 | + "AVG", |
| 65 | + [averageSqlExpression], |
| 66 | + nullable: true, |
| 67 | + argumentsPropagateNullability: ArrayUtil.FalseArrays[1], |
| 68 | + averageSqlExpression.Type, |
| 69 | + averageSqlExpression.TypeMapping); |
| 70 | + |
| 71 | + case nameof(Queryable.Count) |
| 72 | + when methodInfo == QueryableMethods.CountWithoutPredicate |
| 73 | + || methodInfo == QueryableMethods.CountWithPredicate: |
| 74 | + var countSqlExpression = (source.Selector as SqlExpression) ?? _sqlExpressionFactory.Fragment("*"); |
| 75 | + return _sqlExpressionFactory.Convert( |
| 76 | + _sqlExpressionFactory.Function( |
| 77 | + "COUNT", |
| 78 | + [countSqlExpression], |
| 79 | + nullable: false, |
| 80 | + argumentsPropagateNullability: ArrayUtil.FalseArrays[1], |
| 81 | + typeof(long)), |
| 82 | + typeof(int), |
| 83 | + _typeMappingSource.FindMapping(typeof(int))); |
| 84 | + |
| 85 | + case nameof(Queryable.LongCount) |
| 86 | + when methodInfo == QueryableMethods.LongCountWithoutPredicate |
| 87 | + || methodInfo == QueryableMethods.LongCountWithPredicate: |
| 88 | + var longCountSqlExpression = (source.Selector as SqlExpression) ?? _sqlExpressionFactory.Fragment("*"); |
| 89 | + return _sqlExpressionFactory.Function( |
| 90 | + "COUNT", |
| 91 | + [longCountSqlExpression], |
| 92 | + nullable: false, |
| 93 | + argumentsPropagateNullability: ArrayUtil.FalseArrays[1], |
| 94 | + typeof(long)); |
| 95 | + |
| 96 | + case nameof(Queryable.Max) |
| 97 | + when (methodInfo == QueryableMethods.MaxWithoutSelector |
| 98 | + || methodInfo == QueryableMethods.MaxWithSelector) |
| 99 | + && source.Selector is SqlExpression maxSqlExpression: |
| 100 | + return _sqlExpressionFactory.Function( |
| 101 | + "MAX", |
| 102 | + [maxSqlExpression], |
| 103 | + nullable: true, |
| 104 | + argumentsPropagateNullability: ArrayUtil.FalseArrays[1], |
| 105 | + maxSqlExpression.Type, |
| 106 | + maxSqlExpression.TypeMapping); |
| 107 | + |
| 108 | + case nameof(Queryable.Min) |
| 109 | + when (methodInfo == QueryableMethods.MinWithoutSelector |
| 110 | + || methodInfo == QueryableMethods.MinWithSelector) |
| 111 | + && source.Selector is SqlExpression minSqlExpression: |
| 112 | + return _sqlExpressionFactory.Function( |
| 113 | + "MIN", |
| 114 | + [minSqlExpression], |
| 115 | + nullable: true, |
| 116 | + argumentsPropagateNullability: ArrayUtil.FalseArrays[1], |
| 117 | + minSqlExpression.Type, |
| 118 | + minSqlExpression.TypeMapping); |
| 119 | + |
| 120 | + case nameof(Queryable.Sum) |
| 121 | + when (QueryableMethods.IsSumWithoutSelector(methodInfo) |
| 122 | + || QueryableMethods.IsSumWithSelector(methodInfo)) |
| 123 | + && source.Selector is SqlExpression sumSqlExpression: |
| 124 | + var sumInputType = sumSqlExpression.Type; |
| 125 | + |
| 126 | + if (sumInputType == typeof(int)) |
| 127 | + { |
| 128 | + return _sqlExpressionFactory.Convert( |
| 129 | + _sqlExpressionFactory.Function( |
| 130 | + "SUM", |
| 131 | + [sumSqlExpression], |
| 132 | + nullable: true, |
| 133 | + argumentsPropagateNullability: ArrayUtil.FalseArrays[1], |
| 134 | + typeof(long)), |
| 135 | + sumInputType, |
| 136 | + sumSqlExpression.TypeMapping); |
| 137 | + } |
| 138 | + |
| 139 | + if (sumInputType == typeof(long)) |
| 140 | + { |
| 141 | + return _sqlExpressionFactory.Convert( |
| 142 | + _sqlExpressionFactory.Function( |
| 143 | + "SUM", |
| 144 | + [sumSqlExpression], |
| 145 | + nullable: true, |
| 146 | + argumentsPropagateNullability: ArrayUtil.FalseArrays[1], |
| 147 | + typeof(decimal)), |
| 148 | + sumInputType, |
| 149 | + sumSqlExpression.TypeMapping); |
| 150 | + } |
| 151 | + |
| 152 | + return _sqlExpressionFactory.Function( |
| 153 | + "SUM", |
| 154 | + [sumSqlExpression], |
| 155 | + nullable: true, |
| 156 | + argumentsPropagateNullability: ArrayUtil.FalseArrays[1], |
| 157 | + sumInputType, |
| 158 | + sumSqlExpression.TypeMapping); |
| 159 | + } |
| 160 | + |
25 | 161 | return null; |
26 | 162 | } |
27 | 163 | } |
0 commit comments