2018-07-20 19:05:07 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <DataTypes/DataTypesDecimal.h>
|
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
2019-02-19 20:01:31 +00:00
|
|
|
#include <Formats/ProtobufReader.h>
|
2019-01-23 19:41:18 +00:00
|
|
|
#include <Formats/ProtobufWriter.h>
|
2018-07-20 19:05:07 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/readFloatText.h>
|
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
2018-08-22 13:22:56 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2018-07-20 19:05:07 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int ARGUMENT_OUT_OF_BOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-04 15:04:23 +00:00
|
|
|
bool decimalCheckComparisonOverflow(const Context & context) { return context.getSettingsRef().decimal_check_overflow; }
|
|
|
|
bool decimalCheckArithmeticOverflow(const Context & context) { return context.getSettingsRef().decimal_check_overflow; }
|
2018-08-22 13:22:56 +00:00
|
|
|
|
|
|
|
|
2018-07-20 19:05:07 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
template <typename T>
|
2018-12-13 13:41:47 +00:00
|
|
|
std::string DataTypeDecimal<T>::doGetName() const
|
2018-07-20 19:05:07 +00:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Decimal(" << precision << ", " << scale << ")";
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-07-23 20:19:26 +00:00
|
|
|
bool DataTypeDecimal<T>::equals(const IDataType & rhs) const
|
2018-07-20 19:05:07 +00:00
|
|
|
{
|
2018-07-25 19:38:21 +00:00
|
|
|
if (auto * ptype = typeid_cast<const DataTypeDecimal<T> *>(&rhs))
|
|
|
|
return scale == ptype->getScale();
|
|
|
|
return false;
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 18:25:38 +00:00
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimal<T>::serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const
|
|
|
|
{
|
|
|
|
T value = static_cast<const ColumnType &>(column).getData()[row_num];
|
2018-08-23 19:11:31 +00:00
|
|
|
writeText(value, scale, ostr);
|
2018-08-21 18:25:38 +00:00
|
|
|
}
|
2018-07-20 19:05:07 +00:00
|
|
|
|
2019-05-15 18:50:35 +00:00
|
|
|
template <typename T>
|
|
|
|
bool DataTypeDecimal<T>::tryReadText(T & x, ReadBuffer & istr, UInt32 precision, UInt32 scale)
|
|
|
|
{
|
|
|
|
UInt32 unread_scale = scale;
|
|
|
|
bool done = tryReadDecimalText(istr, x, precision, unread_scale);
|
|
|
|
x *= getScaleMultiplier(unread_scale);
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2018-07-20 19:05:07 +00:00
|
|
|
template <typename T>
|
2019-05-15 13:51:17 +00:00
|
|
|
void DataTypeDecimal<T>::readText(T & x, ReadBuffer & istr, UInt32 precision, UInt32 scale, bool csv)
|
2018-07-20 19:05:07 +00:00
|
|
|
{
|
2018-07-23 20:19:26 +00:00
|
|
|
UInt32 unread_scale = scale;
|
2019-05-15 13:51:17 +00:00
|
|
|
if (csv)
|
|
|
|
readCSVDecimalText(istr, x, precision, unread_scale);
|
|
|
|
else
|
|
|
|
readDecimalText(istr, x, precision, unread_scale);
|
2018-07-23 20:19:26 +00:00
|
|
|
x *= getScaleMultiplier(unread_scale);
|
2018-08-21 18:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimal<T>::deserializeText(IColumn & column, ReadBuffer & istr, const FormatSettings &) const
|
|
|
|
{
|
|
|
|
T x;
|
|
|
|
readText(x, istr);
|
2018-07-25 19:38:21 +00:00
|
|
|
static_cast<ColumnType &>(column).getData().push_back(x);
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 13:51:17 +00:00
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimal<T>::deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings &) const
|
|
|
|
{
|
|
|
|
T x;
|
|
|
|
readText(x, istr, true);
|
|
|
|
static_cast<ColumnType &>(column).getData().push_back(x);
|
|
|
|
}
|
2018-07-20 19:05:07 +00:00
|
|
|
|
2018-07-30 18:10:38 +00:00
|
|
|
template <typename T>
|
|
|
|
T DataTypeDecimal<T>::parseFromString(const String & str) const
|
|
|
|
{
|
|
|
|
ReadBufferFromMemory buf(str.data(), str.size());
|
|
|
|
T x;
|
|
|
|
UInt32 unread_scale = scale;
|
|
|
|
readDecimalText(buf, x, precision, unread_scale, true);
|
|
|
|
x *= getScaleMultiplier(unread_scale);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-20 19:05:07 +00:00
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimal<T>::serializeBinary(const Field & field, WriteBuffer & ostr) const
|
|
|
|
{
|
2018-08-23 14:03:37 +00:00
|
|
|
FieldType x = get<DecimalField<T>>(field);
|
2018-07-20 19:05:07 +00:00
|
|
|
writeBinary(x, ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimal<T>::serializeBinary(const IColumn & column, size_t row_num, WriteBuffer & ostr) const
|
|
|
|
{
|
2018-08-07 13:57:28 +00:00
|
|
|
const FieldType & x = static_cast<const ColumnType &>(column).getData()[row_num];
|
|
|
|
writeBinary(x, ostr);
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-02-19 00:41:24 +00:00
|
|
|
void DataTypeDecimal<T>::serializeBinaryBulk(const IColumn & column, WriteBuffer & ostr, size_t offset, size_t limit) const
|
2018-07-20 19:05:07 +00:00
|
|
|
{
|
2018-07-25 19:38:21 +00:00
|
|
|
const typename ColumnType::Container & x = typeid_cast<const ColumnType &>(column).getData();
|
2018-07-20 19:05:07 +00:00
|
|
|
|
|
|
|
size_t size = x.size();
|
|
|
|
|
|
|
|
if (limit == 0 || offset + limit > size)
|
|
|
|
limit = size - offset;
|
|
|
|
|
2018-08-07 13:57:28 +00:00
|
|
|
ostr.write(reinterpret_cast<const char *>(&x[offset]), sizeof(FieldType) * limit);
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimal<T>::deserializeBinary(Field & field, ReadBuffer & istr) const
|
|
|
|
{
|
2018-08-07 13:57:28 +00:00
|
|
|
typename FieldType::NativeType x;
|
2018-07-20 19:05:07 +00:00
|
|
|
readBinary(x, istr);
|
2018-08-21 04:31:35 +00:00
|
|
|
field = DecimalField(T(x), scale);
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimal<T>::deserializeBinary(IColumn & column, ReadBuffer & istr) const
|
|
|
|
{
|
2018-08-07 13:57:28 +00:00
|
|
|
typename FieldType::NativeType x;
|
2018-07-20 19:05:07 +00:00
|
|
|
readBinary(x, istr);
|
2018-08-07 13:57:28 +00:00
|
|
|
static_cast<ColumnType &>(column).getData().push_back(FieldType(x));
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-02-19 00:41:24 +00:00
|
|
|
void DataTypeDecimal<T>::deserializeBinaryBulk(IColumn & column, ReadBuffer & istr, size_t limit, double) const
|
2018-07-20 19:05:07 +00:00
|
|
|
{
|
2018-07-25 19:38:21 +00:00
|
|
|
typename ColumnType::Container & x = typeid_cast<ColumnType &>(column).getData();
|
2018-07-20 19:05:07 +00:00
|
|
|
size_t initial_size = x.size();
|
|
|
|
x.resize(initial_size + limit);
|
2018-08-07 13:57:28 +00:00
|
|
|
size_t size = istr.readBig(reinterpret_cast<char*>(&x[initial_size]), sizeof(FieldType) * limit);
|
|
|
|
x.resize(initial_size + size / sizeof(FieldType));
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-23 19:41:18 +00:00
|
|
|
template <typename T>
|
2019-02-26 14:06:05 +00:00
|
|
|
void DataTypeDecimal<T>::serializeProtobuf(const IColumn & column, size_t row_num, ProtobufWriter & protobuf, size_t & value_index) const
|
2019-01-23 19:41:18 +00:00
|
|
|
{
|
2019-02-26 14:06:05 +00:00
|
|
|
if (value_index)
|
|
|
|
return;
|
|
|
|
value_index = static_cast<bool>(protobuf.writeDecimal(static_cast<const ColumnType &>(column).getData()[row_num], scale));
|
2019-01-23 19:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-19 20:01:31 +00:00
|
|
|
template <typename T>
|
|
|
|
void DataTypeDecimal<T>::deserializeProtobuf(IColumn & column, ProtobufReader & protobuf, bool allow_add_row, bool & row_added) const
|
|
|
|
{
|
|
|
|
row_added = false;
|
|
|
|
T decimal;
|
|
|
|
if (!protobuf.readDecimal(decimal, precision, scale))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto & container = static_cast<ColumnType &>(column).getData();
|
|
|
|
if (allow_add_row)
|
|
|
|
{
|
|
|
|
container.emplace_back(decimal);
|
|
|
|
row_added = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
container.back() = decimal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-20 19:05:07 +00:00
|
|
|
template <typename T>
|
|
|
|
Field DataTypeDecimal<T>::getDefault() const
|
|
|
|
{
|
2018-08-21 04:31:35 +00:00
|
|
|
return DecimalField(T(0), scale);
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-25 13:06:21 +00:00
|
|
|
template <typename T>
|
2019-01-25 14:16:23 +00:00
|
|
|
DataTypePtr DataTypeDecimal<T>::promoteNumericType() const
|
2019-01-25 13:06:21 +00:00
|
|
|
{
|
2019-01-25 14:16:23 +00:00
|
|
|
using PromotedType = DataTypeDecimal<Decimal128>;
|
|
|
|
return std::make_shared<PromotedType>(PromotedType::maxPrecision(), scale);
|
2019-01-25 13:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-20 19:05:07 +00:00
|
|
|
template <typename T>
|
|
|
|
MutableColumnPtr DataTypeDecimal<T>::createColumn() const
|
|
|
|
{
|
2018-08-27 16:16:16 +00:00
|
|
|
return ColumnType::create(0, scale);
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2018-08-21 18:25:38 +00:00
|
|
|
DataTypePtr createDecimal(UInt64 precision_value, UInt64 scale_value)
|
|
|
|
{
|
2018-08-21 18:55:36 +00:00
|
|
|
if (precision_value < minDecimalPrecision() || precision_value > maxDecimalPrecision<Decimal128>())
|
2018-08-21 18:25:38 +00:00
|
|
|
throw Exception("Wrong precision", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
if (static_cast<UInt64>(scale_value) > precision_value)
|
|
|
|
throw Exception("Negative scales and scales larger than presicion are not supported", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
2018-08-21 18:55:36 +00:00
|
|
|
if (precision_value <= maxDecimalPrecision<Decimal32>())
|
|
|
|
return std::make_shared<DataTypeDecimal<Decimal32>>(precision_value, scale_value);
|
|
|
|
else if (precision_value <= maxDecimalPrecision<Decimal64>())
|
|
|
|
return std::make_shared<DataTypeDecimal<Decimal64>>(precision_value, scale_value);
|
|
|
|
return std::make_shared<DataTypeDecimal<Decimal128>>(precision_value, scale_value);
|
2018-08-21 18:25:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 19:05:07 +00:00
|
|
|
static DataTypePtr create(const ASTPtr & arguments)
|
|
|
|
{
|
|
|
|
if (!arguments || arguments->children.size() != 2)
|
|
|
|
throw Exception("Decimal data type family must have exactly two arguments: precision and scale",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * precision = arguments->children[0]->as<ASTLiteral>();
|
|
|
|
const auto * scale = arguments->children[1]->as<ASTLiteral>();
|
2018-07-20 19:05:07 +00:00
|
|
|
|
|
|
|
if (!precision || precision->value.getType() != Field::Types::UInt64 ||
|
|
|
|
!scale || !(scale->value.getType() == Field::Types::Int64 || scale->value.getType() == Field::Types::UInt64))
|
|
|
|
throw Exception("Decimal data type family must have a two numbers as its arguments", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
UInt64 precision_value = precision->value.get<UInt64>();
|
2018-08-21 18:25:38 +00:00
|
|
|
UInt64 scale_value = scale->value.get<UInt64>();
|
2018-07-20 19:05:07 +00:00
|
|
|
|
2018-08-21 18:25:38 +00:00
|
|
|
return createDecimal(precision_value, scale_value);
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 16:16:39 +00:00
|
|
|
template <typename T>
|
|
|
|
static DataTypePtr createExect(const ASTPtr & arguments)
|
|
|
|
{
|
|
|
|
if (!arguments || arguments->children.size() != 1)
|
|
|
|
throw Exception("Decimal data type family must have exactly two arguments: precision and scale",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * scale_arg = arguments->children[0]->as<ASTLiteral>();
|
2018-08-22 16:16:39 +00:00
|
|
|
|
|
|
|
if (!scale_arg || !(scale_arg->value.getType() == Field::Types::Int64 || scale_arg->value.getType() == Field::Types::UInt64))
|
|
|
|
throw Exception("Decimal data type family must have a two numbers as its arguments", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
UInt64 precision = maxDecimalPrecision<T>();
|
|
|
|
UInt64 scale = scale_arg->value.get<UInt64>();
|
|
|
|
|
|
|
|
return createDecimal(precision, scale);
|
|
|
|
}
|
2018-07-20 19:05:07 +00:00
|
|
|
|
|
|
|
void registerDataTypeDecimal(DataTypeFactory & factory)
|
|
|
|
{
|
2018-08-22 16:16:39 +00:00
|
|
|
factory.registerDataType("Decimal32", createExect<Decimal32>, DataTypeFactory::CaseInsensitive);
|
|
|
|
factory.registerDataType("Decimal64", createExect<Decimal64>, DataTypeFactory::CaseInsensitive);
|
|
|
|
factory.registerDataType("Decimal128", createExect<Decimal128>, DataTypeFactory::CaseInsensitive);
|
|
|
|
|
2018-07-20 19:05:07 +00:00
|
|
|
factory.registerDataType("Decimal", create, DataTypeFactory::CaseInsensitive);
|
2018-07-30 18:10:38 +00:00
|
|
|
factory.registerAlias("DEC", "Decimal", DataTypeFactory::CaseInsensitive);
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 20:19:26 +00:00
|
|
|
|
|
|
|
template <>
|
2018-08-21 04:31:35 +00:00
|
|
|
Decimal32 DataTypeDecimal<Decimal32>::getScaleMultiplier(UInt32 scale_)
|
2018-07-23 20:19:26 +00:00
|
|
|
{
|
2018-08-23 19:11:31 +00:00
|
|
|
return decimalScaleMultiplier<Int32>(scale_);
|
2018-07-23 20:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2018-08-21 04:31:35 +00:00
|
|
|
Decimal64 DataTypeDecimal<Decimal64>::getScaleMultiplier(UInt32 scale_)
|
2018-07-23 20:19:26 +00:00
|
|
|
{
|
2018-08-23 19:11:31 +00:00
|
|
|
return decimalScaleMultiplier<Int64>(scale_);
|
2018-07-23 20:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2018-08-21 04:31:35 +00:00
|
|
|
Decimal128 DataTypeDecimal<Decimal128>::getScaleMultiplier(UInt32 scale_)
|
2018-07-23 20:19:26 +00:00
|
|
|
{
|
2018-08-23 19:11:31 +00:00
|
|
|
return decimalScaleMultiplier<Int128>(scale_);
|
2018-07-23 20:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-20 19:05:07 +00:00
|
|
|
/// Explicit template instantiations.
|
2018-08-21 04:31:35 +00:00
|
|
|
template class DataTypeDecimal<Decimal32>;
|
|
|
|
template class DataTypeDecimal<Decimal64>;
|
|
|
|
template class DataTypeDecimal<Decimal128>;
|
2018-07-20 19:05:07 +00:00
|
|
|
|
|
|
|
}
|