Extern templates + minor simplifications

This commit is contained in:
Vasily Nemkov 2019-10-03 10:27:28 +03:00
parent 655ec351ba
commit 180b986972
7 changed files with 43 additions and 31 deletions

View File

@ -21,43 +21,47 @@ struct DecimalComponents
T fractional;
};
template <typename T>
Decimal<T> decimalFromComponents(const T & whole, const T & fractional, UInt32 scale)
template <typename DecimalType>
DecimalType decimalFromComponents(const typename DecimalType::NativeType & whole, const typename DecimalType::NativeType & fractional, UInt32 scale)
{
using T = typename DecimalType::NativeType;
const auto mul = decimalScaleMultiplier<T>(scale);
const T value = whole * mul + fractional / decimalScaleMultiplier<T>(std::numeric_limits<T>::digits10 - scale);
return Decimal<T>(value);
return DecimalType(value);
}
template <typename T>
Decimal<T> decimalFromComponents(const DecimalComponents<T> & components, UInt32 scale)
template <typename DecimalType>
DecimalType decimalFromComponents(const DecimalComponents<typename DecimalType::NativeType> & components, UInt32 scale)
{
return decimalFromComponents(components.whole, components.fractional, scale);
return decimalFromComponents<DecimalType>(components.whole, components.fractional, scale);
}
template <typename T>
DecimalComponents<T> decimalSplit(const Decimal<T> & decimal, UInt32 scale)
template <typename DecimalType>
DecimalComponents<typename DecimalType::NativeType> decimalSplit(const DecimalType & decimal, UInt32 scale)
{
if (scale == 0)
{
return {decimal.value, 0};
}
const auto scaleMultiplier = decimalScaleMultiplier<T>(scale);
const auto scaleMultiplier = decimalScaleMultiplier<typename DecimalType::NativeType>(scale);
return {decimal.value / scaleMultiplier, decimal.value % scaleMultiplier};
}
template <typename T>
T decimalWholePart(const Decimal<T> & decimal, size_t scale)
template <typename DecimalType>
typename DecimalType::NativeType decimalWholePart(const DecimalType & decimal, size_t scale)
{
if (scale == 0)
return decimal.value;
return decimal.value / decimalScaleMultiplier<T>(scale);
return decimal.value / decimalScaleMultiplier<typename DecimalType::NativeType>(scale);
}
template <typename T>
T decimalFractionalPart(const Decimal<T> & decimal, size_t scale)
template <typename DecimalType>
typename DecimalType::NativeType decimalFractionalPart(const DecimalType & decimal, size_t scale)
{
using T = typename DecimalType::NativeType;
if (scale == 0)
return 0;

10
dbms/src/Core/Types.cpp Normal file
View File

@ -0,0 +1,10 @@
#include <Core/Types.h>
namespace DB
{
template struct Decimal<Int32>;
template struct Decimal<Int64>;
template struct Decimal<Int128>;
}

View File

@ -149,6 +149,10 @@ struct Decimal
T value;
};
extern template struct Decimal<Int32>;
extern template struct Decimal<Int64>;
extern template struct Decimal<Int128>;
using Decimal32 = Decimal<Int32>;
using Decimal64 = Decimal<Int64>;
using Decimal128 = Decimal<Int128>;

View File

@ -94,22 +94,10 @@ void DataTypeDecimalBase<T>::deserializeBinaryBulk(IColumn & column, ReadBuffer
x.resize(initial_size + size / sizeof(FieldType));
}
template <>
Decimal32 DataTypeDecimalBase<Decimal32>::getScaleMultiplier(UInt32 scale_)
template <typename T>
T DataTypeDecimalBase<T>::getScaleMultiplier(UInt32 scale_)
{
return decimalScaleMultiplier<Int32>(scale_);
}
template <>
Decimal64 DataTypeDecimalBase<Decimal64>::getScaleMultiplier(UInt32 scale_)
{
return decimalScaleMultiplier<Int64>(scale_);
}
template <>
Decimal128 DataTypeDecimalBase<Decimal128>::getScaleMultiplier(UInt32 scale_)
{
return decimalScaleMultiplier<Int128>(scale_);
return decimalScaleMultiplier<typename T::NativeType>(scale_);
}

View File

@ -347,4 +347,10 @@ DataTypePtr createDecimal(UInt64 precision_value, UInt64 scale_value)
return std::make_shared<DecimalType<Decimal128>>(precision_value, scale_value);
}
extern template class DataTypeDecimalBase<Decimal32>;
extern template class DataTypeDecimalBase<Decimal64>;
extern template class DataTypeDecimalBase<Decimal128>;
extern template class DataTypeDecimalBase<DateTime64>;
}

View File

@ -20,7 +20,7 @@ DateTime64::NativeType nowSubsecond(UInt8 scale) {
timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
return decimalFromComponents<DateTime64::NativeType>(spec.tv_sec, spec.tv_nsec, scale);
return decimalFromComponents<DateTime64>(spec.tv_sec, spec.tv_nsec, scale).value;
}
/// Get the current time. (It is a constant, it is evaluated once for the entire query.)

View File

@ -654,7 +654,7 @@ inline void readDateTimeText(DateTime64 & datetime64, UInt32 scale, ReadBuffer &
auto fractional_length = remainder1 - buf.available();
c.fractional *= common::exp10_i64(scale - fractional_length);
datetime64 = decimalFromComponents(c, scale);
datetime64 = decimalFromComponents<DateTime64>(c, scale);
}
inline bool tryReadDateTimeText(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & date_lut = DateLUT::instance())