Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public class YdbQueryableAggregateMethodTranslator(
[sumSqlExpression],
nullable: true,
argumentsPropagateNullability: ArrayUtil.FalseArrays[1],
typeof(decimal)),
typeof(long)),
sumInputType,
sumSqlExpression.TypeMapping);
}
Expand Down
30 changes: 29 additions & 1 deletion src/EFCore.Ydb/src/Query/Internal/YdbSqlExpressionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
using System;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using EntityFrameworkCore.Ydb.Storage.Internal.Mapping;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;

namespace EntityFrameworkCore.Ydb.Query.Internal;

public class YdbSqlExpressionFactory(SqlExpressionFactoryDependencies dependencies) : SqlExpressionFactory(dependencies)
{
[return: NotNullIfNotNull("sqlExpression")]
public override SqlExpression? ApplyTypeMapping(SqlExpression? sqlExpression, RelationalTypeMapping? typeMapping) =>
base.ApplyTypeMapping(sqlExpression, typeMapping);

public override SqlExpression Coalesce(SqlExpression left, SqlExpression right,
RelationalTypeMapping? typeMapping = null)
{
// For .Sum(x => x.Decimal) EF generates coalesce(sum(x.Decimal), 0.0)) because SUM must have value
var funcExpression = left as SqlFunctionExpression;
var constExpression = right as SqlConstantExpression;

if (funcExpression != null && constExpression != null && constExpression.TypeMapping != null
&&
funcExpression.Name.Equals("SUM", StringComparison.OrdinalIgnoreCase)
&&
constExpression.TypeMapping.DbType == DbType.Decimal
&&
constExpression.Value != null)
{
var correctRight = new SqlConstantExpression(constExpression.Value,
YdbDecimalTypeMapping.WithMaxPrecision); // in the feature change static max precision/scale to
// to dynamically created correct precision/scale
// it depends on db scheme and can not correctly define only in code

return base.Coalesce(left, correctRight, typeMapping);
}

return base.Coalesce(left, right, typeMapping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,32 @@ public class YdbDecimalTypeMapping : DecimalTypeMapping
{
private const byte DefaultPrecision = 22;
private const byte DefaultScale = 9;

private const byte MaxPrecision = 35;

public new static YdbDecimalTypeMapping Default => new();

static YdbDecimalTypeMapping()
{
WithMaxPrecision = GetWithMaxPrecision();
}

public static YdbDecimalTypeMapping WithMaxPrecision { get; }

private static YdbDecimalTypeMapping GetWithMaxPrecision()
{
var result = new YdbDecimalTypeMapping(new RelationalTypeMappingParameters(
new CoreTypeMappingParameters(
typeof(decimal)),
storeType: "Decimal",
dbType: System.Data.DbType.Decimal,
precision: MaxPrecision,
scale: DefaultScale)
);

return result;
}

public YdbDecimalTypeMapping() : this(
new RelationalTypeMappingParameters(
new CoreTypeMappingParameters(typeof(decimal)),
Expand Down Expand Up @@ -41,4 +64,9 @@ protected override void ConfigureParameter(DbParameter parameter)
if (Scale is { } s)
parameter.Scale = (byte)s;
}

protected override string GenerateNonNullSqlLiteral(object value)
{
return $"Decimal('{base.GenerateNonNullSqlLiteral(value)}', {this.Precision ?? MaxPrecision}, {this.Scale ?? DefaultScale})";
}
}
Loading