From f1b8d1d9d722d09a279b36f2b91b7887c025e7e2 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Tue, 7 Feb 2023 00:27:12 +0100 Subject: [PATCH] Improve performance of Decimal conversion when scale does not change --- src/DataTypes/DataTypesDecimal.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/DataTypes/DataTypesDecimal.h b/src/DataTypes/DataTypesDecimal.h index 88b3a33a5b8..7a49238b5be 100644 --- a/src/DataTypes/DataTypesDecimal.h +++ b/src/DataTypes/DataTypesDecimal.h @@ -100,7 +100,7 @@ inline UInt32 getDecimalScale(const DataTypeDecimal & data_type) template requires (IsDataTypeDecimal && IsDataTypeDecimal) -inline ReturnType convertDecimalsImpl(const typename FromDataType::FieldType & value, UInt32 scale_from, UInt32 scale_to, typename ToDataType::FieldType& result) +inline ReturnType convertDecimalsImpl(const typename FromDataType::FieldType & value, UInt32 scale_from, UInt32 scale_to, typename ToDataType::FieldType & result) { using FromFieldType = typename FromDataType::FieldType; using ToFieldType = typename ToDataType::FieldType; @@ -121,8 +121,14 @@ inline ReturnType convertDecimalsImpl(const typename FromDataType::FieldType & v return ReturnType(false); } } + else if (scale_to == scale_from) + { + converted_value = value.value; + } else + { converted_value = value.value / DecimalUtils::scaleMultiplier(scale_from - scale_to); + } if constexpr (sizeof(FromFieldType) > sizeof(ToFieldType)) { @@ -155,7 +161,7 @@ inline typename ToDataType::FieldType convertDecimals(const typename FromDataTyp template requires (IsDataTypeDecimal && IsDataTypeDecimal) -inline bool tryConvertDecimals(const typename FromDataType::FieldType & value, UInt32 scale_from, UInt32 scale_to, typename ToDataType::FieldType& result) +inline bool tryConvertDecimals(const typename FromDataType::FieldType & value, UInt32 scale_from, UInt32 scale_to, typename ToDataType::FieldType & result) { return convertDecimalsImpl(value, scale_from, scale_to, result); }