@@ -19,6 +19,8 @@ namespace Xtensive.Orm.Providers.PostgreSql
1919{
2020 internal class SqlCompiler : Providers . SqlCompiler
2121 {
22+ private const int MaxDotnetDecimalPrecision = 28 ;
23+
2224 protected override SqlProvider VisitFreeText ( FreeTextProvider provider )
2325 {
2426 var rankColumnName = provider . Header . Columns [ provider . Header . Columns . Count - 1 ] . Name ;
@@ -102,7 +104,7 @@ private bool TryAdjustPrecisionScale(
102104 }
103105 var typeHintValue = typeHint . Value ;
104106
105- if ( typeHintValue . precision == 28 ) {
107+ if ( typeHintValue . precision == MaxDotnetDecimalPrecision ) {
106108 // No room for adjust, otherwise we'll lose floor part data
107109 precision = typeHintValue . precision ;
108110 scale = typeHintValue . scale ;
@@ -116,10 +118,16 @@ private bool TryAdjustPrecisionScale(
116118 // sometimes we need bigger floor part, and sometimes bigger fractional part.
117119 // This algorithm is a trade-off.
118120 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 ,
123131 _ => typeHintValue . scale ,
124132 } ;
125133 return true ;
0 commit comments