|
| 1 | +using System; |
| 2 | +using System.Data; |
1 | 3 | using System.Diagnostics.CodeAnalysis; |
| 4 | +using EntityFrameworkCore.Ydb.Storage.Internal.Mapping; |
2 | 5 | using Microsoft.EntityFrameworkCore.Query; |
3 | 6 | using Microsoft.EntityFrameworkCore.Query.SqlExpressions; |
4 | 7 | using Microsoft.EntityFrameworkCore.Storage; |
5 | | - |
6 | 8 | namespace EntityFrameworkCore.Ydb.Query.Internal; |
7 | 9 |
|
8 | 10 | public class YdbSqlExpressionFactory(SqlExpressionFactoryDependencies dependencies) : SqlExpressionFactory(dependencies) |
9 | 11 | { |
10 | 12 | [return: NotNullIfNotNull("sqlExpression")] |
11 | 13 | public override SqlExpression? ApplyTypeMapping(SqlExpression? sqlExpression, RelationalTypeMapping? typeMapping) => |
12 | 14 | base.ApplyTypeMapping(sqlExpression, typeMapping); |
| 15 | + |
| 16 | + public override SqlExpression Coalesce(SqlExpression left, SqlExpression right, |
| 17 | + RelationalTypeMapping? typeMapping = null) |
| 18 | + { |
| 19 | + // For .Sum(x => x.Decimal) EF generates coalesce(sum(x.Decimal), 0.0)) because SUM must have value |
| 20 | + var funcExpression = left as SqlFunctionExpression; |
| 21 | + var constExpression = right as SqlConstantExpression; |
| 22 | + |
| 23 | + if (funcExpression != null && constExpression != null && constExpression.TypeMapping != null |
| 24 | + && |
| 25 | + funcExpression.Name.Equals("SUM", StringComparison.OrdinalIgnoreCase) |
| 26 | + && |
| 27 | + constExpression.TypeMapping.DbType == DbType.Decimal |
| 28 | + && |
| 29 | + constExpression.Value != null) |
| 30 | + { |
| 31 | + var correctRight = new SqlConstantExpression(constExpression.Value, |
| 32 | + YdbDecimalTypeMapping.WithMaxPrecision); // in the feature change static max precision/scale to |
| 33 | + // to dynamically created correct precision/scale |
| 34 | + // it depends on db scheme and can not correctly define only in code |
| 35 | + |
| 36 | + return base.Coalesce(left, correctRight, typeMapping); |
| 37 | + } |
| 38 | + |
| 39 | + return base.Coalesce(left, right, typeMapping); |
| 40 | + } |
13 | 41 | } |
0 commit comments