@@ -19,6 +19,8 @@ namespace Xtensive.Orm.Providers.PostgreSql
19
19
{
20
20
internal class SqlCompiler : Providers . SqlCompiler
21
21
{
22
+ private const int MaxDotnetDecimalPrecision = 28 ;
23
+
22
24
protected override SqlProvider VisitFreeText ( FreeTextProvider provider )
23
25
{
24
26
var rankColumnName = provider . Header . Columns [ provider . Header . Columns . Count - 1 ] . Name ;
@@ -102,7 +104,7 @@ private bool TryAdjustPrecisionScale(
102
104
}
103
105
var typeHintValue = typeHint . Value ;
104
106
105
- if ( typeHintValue . precision == 28 ) {
107
+ if ( typeHintValue . precision == MaxDotnetDecimalPrecision ) {
106
108
// No room for adjust, otherwise we'll lose floor part data
107
109
precision = typeHintValue . precision ;
108
110
scale = typeHintValue . scale ;
@@ -116,10 +118,16 @@ private bool TryAdjustPrecisionScale(
116
118
// sometimes we need bigger floor part, and sometimes bigger fractional part.
117
119
// This algorithm is a trade-off.
118
120
scale = aggregateType switch {
119
- AggregateType . Avg => ( typeHintValue . precision < 28 ) ? typeHintValue . scale + Math . Max ( ( precision - typeHintValue . precision ) / 2 , 1 ) : typeHintValue . scale + 1 ,
120
- AggregateType . Sum => ( typeHintValue . precision < 27 ) ? typeHintValue . scale + 2 : typeHintValue . scale + 1 ,
121
- AggregateType . Min => ( typeHintValue . precision < 27 ) ? typeHintValue . scale + 2 : typeHintValue . scale + 1 ,
122
- AggregateType . Max => ( typeHintValue . precision < 27 ) ? typeHintValue . scale + 2 : typeHintValue . scale + 1 ,
121
+ AggregateType . Avg =>
122
+ ( typeHintValue . precision < MaxDotnetDecimalPrecision )
123
+ ? typeHintValue . scale + Math . Max ( ( precision - typeHintValue . precision ) / 2 , 1 )
124
+ : typeHintValue . scale + 1 ,
125
+ AggregateType . Sum or
126
+ AggregateType . Min or
127
+ AggregateType . Max =>
128
+ ( typeHintValue . precision < MaxDotnetDecimalPrecision - 1 )
129
+ ? typeHintValue . scale + 2
130
+ : typeHintValue . scale + 1 ,
123
131
_ => typeHintValue . scale ,
124
132
} ;
125
133
return true ;
0 commit comments