mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-17 21:24:28 +00:00
Extern templates + minor simplifications
This commit is contained in:
parent
655ec351ba
commit
180b986972
@ -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
10
dbms/src/Core/Types.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include <Core/Types.h>
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
template struct Decimal<Int32>;
|
||||
template struct Decimal<Int64>;
|
||||
template struct Decimal<Int128>;
|
||||
|
||||
}
|
@ -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>;
|
||||
|
@ -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_);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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>;
|
||||
|
||||
|
||||
}
|
||||
|
@ -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.)
|
||||
|
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user