Skip to content

Commit 1bbb09c

Browse files
committed
Small improvements of decimal parameters guessing algorithm
1 parent a534334 commit 1bbb09c

1 file changed

Lines changed: 17 additions & 30 deletions

File tree

Orm/Xtensive.Orm/Orm/Linq/Translator.Queryable.cs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -824,45 +824,32 @@ private Expression VisitAggregate(Expression source, MethodInfo method, LambdaEx
824824
var expression = cColumn.Expression;
825825
var usedColumns = new Rse.Transformation.TupleAccessGatherer().Gather(expression);
826826

827-
var maxFloorDigits = 0;
828-
var maxScaleDigits = 0;
829-
foreach (var cIndex in usedColumns) {
827+
var maxFloorDigits = -1;
828+
var maxScaleDigits = -1;
829+
foreach (var cIndex in usedColumns.Distinct()) {
830830
var usedColumn = headerColumns[cIndex];
831831
if (usedColumn is MappedColumn mmColumn) {
832832
var resolvedColumn = mmColumn.ColumnInfoRef.Resolve(domainModel);
833-
var precision = resolvedColumn.Precision;
834-
var scale = resolvedColumn.Scale;
835-
if (resolvedColumn.ValueType != WellKnownTypes.Decimal) {
836-
if (!precision.HasValue) {
837-
precision = Type.GetTypeCode(resolvedColumn.ValueType) switch {
838-
TypeCode.Byte or TypeCode.SByte => 8,
839-
TypeCode.UInt16 or TypeCode.Int16=> 10,
840-
TypeCode.UInt32 => 18,
841-
TypeCode.UInt64 or TypeCode.Int64 => 28,
842-
TypeCode.Int32 => 19,
843-
_ => null
844-
};
845-
}
846-
847-
if (!scale.HasValue) {
848-
scale = Type.GetTypeCode(resolvedColumn.ValueType) switch {
849-
TypeCode.Byte or TypeCode.SByte or TypeCode.UInt16 or TypeCode.Int16 => 5,
850-
TypeCode.UInt32 or TypeCode.Int32 or TypeCode.UInt64 or TypeCode.Int64 => 8,
851-
_ => null
852-
};
853-
}
854-
}
855833

856-
if (precision.HasValue && scale.HasValue) {
857-
if (maxScaleDigits < scale.Value)
858-
maxScaleDigits = scale.Value;
859-
var floorDigits = precision.Value - scale.Value;
834+
(int? p, int? s) @params = Type.GetTypeCode(resolvedColumn.ValueType) switch {
835+
TypeCode.Decimal => (resolvedColumn.Precision, resolvedColumn.Scale),
836+
TypeCode.Int32 or TypeCode.UInt32 => (19, 8),
837+
TypeCode.Int64 or TypeCode.UInt64 => (28, 8),
838+
TypeCode.Byte or TypeCode.SByte => (8, 5),
839+
TypeCode.Int16 or TypeCode.UInt16 => (10, 5),
840+
_ => (null, null),
841+
};
842+
843+
if (@params.p.HasValue && @params.s.HasValue) {
844+
if (maxScaleDigits < @params.s.Value)
845+
maxScaleDigits = @params.s.Value;
846+
var floorDigits = @params.p.Value - @params.s.Value;
860847
if (maxFloorDigits < floorDigits)
861848
maxFloorDigits = floorDigits;
862849
}
863850
}
864851
}
865-
if (maxFloorDigits == 0 && maxScaleDigits == 0)
852+
if (maxFloorDigits == -1 || maxScaleDigits == -1)
866853
return null;
867854
if (maxFloorDigits + maxScaleDigits <= 28)
868855
return (maxFloorDigits + maxScaleDigits, maxScaleDigits);

0 commit comments

Comments
 (0)