up precision for avg result to max of type

This commit is contained in:
akonyaev 2019-10-23 11:22:51 +03:00
parent 6d0dbf7e95
commit 7426542b8b
3 changed files with 4 additions and 18 deletions

View File

@ -52,14 +52,12 @@ public:
AggregateFunctionAvg(const DataTypes & argument_types_)
: IAggregateFunctionDataHelper<Data, AggregateFunctionAvg<T, Data>>(argument_types_, {})
, scale(0)
, precision(0)
{}
/// ctor for Decimals
AggregateFunctionAvg(const IDataType & data_type, const DataTypes & argument_types_)
: IAggregateFunctionDataHelper<Data, AggregateFunctionAvg<T, Data>>(argument_types_, {})
, scale(getDecimalScale(data_type))
, precision(getDecimalPrecision(data_type))
{}
String getName() const override { return "avg"; }
@ -67,7 +65,7 @@ public:
DataTypePtr getReturnType() const override
{
if constexpr (IsDecimalNumber<T>)
return std::make_shared<ResultDataType>(precision, scale);
return std::make_shared<ResultDataType>(ResultDataType::maxPrecision(), scale);
else
return std::make_shared<ResultDataType>();
}
@ -107,7 +105,6 @@ public:
private:
UInt32 scale;
UInt32 precision;
};

View File

@ -243,17 +243,6 @@ inline UInt32 getDecimalScale(const IDataType & data_type, UInt32 default_value
return default_value;
}
inline UInt32 getDecimalPrecision(const IDataType & data_type, UInt32 default_value = std::numeric_limits<UInt32>::max())
{
if (auto * decimal_type = checkDecimal<Decimal32>(data_type))
return decimal_type->getPrecision();
if (auto * decimal_type = checkDecimal<Decimal64>(data_type))
return decimal_type->getPrecision();
if (auto * decimal_type = checkDecimal<Decimal128>(data_type))
return decimal_type->getPrecision();
return default_value;
}
///
template <typename DataType> constexpr bool IsDataTypeDecimal = false;

View File

@ -5,9 +5,9 @@
0.0000 0.0000000 0.00000000 0.0000 0.0000000 0.00000000
0.0000 0.0000000 0.00000000 0.0000 0.0000000 0.00000000
0.0000 0.0000000 0.00000000 0.0000 0.0000000 0.00000000
0.0000 0.0000000 0.00000000 Decimal(6, 4) Decimal(16, 7) Decimal(20, 8)
0.0000 0.0000000 0.00000000 Decimal(6, 4) Decimal(16, 7) Decimal(20, 8)
0.0000 0.0000000 0.00000000 Decimal(6, 4) Decimal(16, 7) Decimal(20, 8)
0.0000 0.0000000 0.00000000 Decimal(9, 4) Decimal(18, 7) Decimal(38, 8)
0.0000 0.0000000 0.00000000 Decimal(9, 4) Decimal(18, 7) Decimal(38, 8)
0.0000 0.0000000 0.00000000 Decimal(9, 4) Decimal(18, 7) Decimal(38, 8)
(0,0,0) (0,0,0) (0,0,0) (0,0,0) (0,0,0)
0 0 0
0 0 0