2019-10-02 05:53:38 +00:00
|
|
|
#include <DataTypes/DataTypeDecimalBase.h>
|
|
|
|
|
|
|
|
#include <Common/assert_cast.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <Core/DecimalFunctions.h>
|
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
|
|
|
#include <Formats/ProtobufReader.h>
|
|
|
|
#include <Formats/ProtobufWriter.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool decimalCheckComparisonOverflow(const Context & context) { return context.getSettingsRef().decimal_check_overflow; }
|
|
|
|
bool decimalCheckArithmeticOverflow(const Context & context) { return context.getSettingsRef().decimal_check_overflow; }
|
|
|
|
|
|
|
|
template <typename T>
|
2019-10-02 10:54:59 +00:00
|
|
|
Field DataTypeDecimalBase<T>::getDefault() const
|
2019-10-02 05:53:38 +00:00
|
|
|
{
|
2019-10-02 10:54:59 +00:00
|
|
|
return DecimalField(T(0), scale);
|
2019-10-02 05:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-10-02 10:54:59 +00:00
|
|
|
MutableColumnPtr DataTypeDecimalBase<T>::createColumn() const
|
2019-10-02 05:53:38 +00:00
|
|
|
{
|
2019-10-02 10:54:59 +00:00
|
|
|
return ColumnType::create(0, scale);
|
2019-10-02 05:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimalBase<T>::serializeBinary(const Field & field, WriteBuffer & ostr) const
|
|
|
|
{
|
|
|
|
FieldType x = get<DecimalField<T>>(field);
|
|
|
|
writeBinary(x, ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimalBase<T>::serializeBinary(const IColumn & column, size_t row_num, WriteBuffer & ostr) const
|
|
|
|
{
|
2019-10-02 10:54:59 +00:00
|
|
|
const FieldType & x = assert_cast<const ColumnType &>(column).getElement(row_num);
|
2019-10-02 05:53:38 +00:00
|
|
|
writeBinary(x, ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimalBase<T>::serializeBinaryBulk(const IColumn & column, WriteBuffer & ostr, size_t offset, size_t limit) const
|
|
|
|
{
|
|
|
|
const typename ColumnType::Container & x = typeid_cast<const ColumnType &>(column).getData();
|
|
|
|
|
|
|
|
size_t size = x.size();
|
|
|
|
|
|
|
|
if (limit == 0 || offset + limit > size)
|
|
|
|
limit = size - offset;
|
|
|
|
|
|
|
|
ostr.write(reinterpret_cast<const char *>(&x[offset]), sizeof(FieldType) * limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimalBase<T>::deserializeBinary(Field & field, ReadBuffer & istr) const
|
|
|
|
{
|
|
|
|
typename FieldType::NativeType x;
|
|
|
|
readBinary(x, istr);
|
2019-10-02 10:54:59 +00:00
|
|
|
field = DecimalField(T(x), this->scale);
|
2019-10-02 05:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimalBase<T>::deserializeBinary(IColumn & column, ReadBuffer & istr) const
|
|
|
|
{
|
|
|
|
typename FieldType::NativeType x;
|
|
|
|
readBinary(x, istr);
|
|
|
|
assert_cast<ColumnType &>(column).getData().push_back(FieldType(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimalBase<T>::deserializeBinaryBulk(IColumn & column, ReadBuffer & istr, size_t limit, double) const
|
|
|
|
{
|
|
|
|
typename ColumnType::Container & x = typeid_cast<ColumnType &>(column).getData();
|
|
|
|
size_t initial_size = x.size();
|
|
|
|
x.resize(initial_size + limit);
|
|
|
|
size_t size = istr.readBig(reinterpret_cast<char*>(&x[initial_size]), sizeof(FieldType) * limit);
|
|
|
|
x.resize(initial_size + size / sizeof(FieldType));
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:27:28 +00:00
|
|
|
template <typename T>
|
|
|
|
T DataTypeDecimalBase<T>::getScaleMultiplier(UInt32 scale_)
|
2019-10-02 05:53:38 +00:00
|
|
|
{
|
2019-12-06 21:05:31 +00:00
|
|
|
return DecimalUtils::scaleMultiplier<typename T::NativeType>(scale_);
|
2019-10-02 05:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Explicit template instantiations.
|
|
|
|
template class DataTypeDecimalBase<Decimal32>;
|
|
|
|
template class DataTypeDecimalBase<Decimal64>;
|
|
|
|
template class DataTypeDecimalBase<Decimal128>;
|
|
|
|
|
|
|
|
}
|