#include #include #include #include #include #include #include #include #include #include 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; } bool decimalCheckComparisonOverflow(const Context & context) { return context.getSettingsRef().decimal_check_overflow; } bool decimalCheckArithmeticOverflow(const Context & context) { return context.getSettingsRef().decimal_check_overflow; } // template std::string DataTypeDecimal::getName() const { std::stringstream ss; ss << "Decimal(" << precision << ", " << scale << ")"; return ss.str(); } template bool DataTypeDecimal::equals(const IDataType & rhs) const { if (auto * ptype = typeid_cast *>(&rhs)) return scale == ptype->getScale(); return false; } template void DataTypeDecimal::serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const { T value = static_cast(column).getData()[row_num]; writeText(value, scale, ostr); } template void DataTypeDecimal::readText(T & x, ReadBuffer & istr, UInt32 precision, UInt32 scale) { UInt32 unread_scale = scale; readDecimalText(istr, x, precision, unread_scale); x *= getScaleMultiplier(unread_scale); } template void DataTypeDecimal::deserializeText(IColumn & column, ReadBuffer & istr, const FormatSettings &) const { T x; readText(x, istr); static_cast(column).getData().push_back(x); } template T DataTypeDecimal::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; } template void DataTypeDecimal::serializeBinary(const Field & field, WriteBuffer & ostr) const { FieldType x = get>(field); writeBinary(x, ostr); } template void DataTypeDecimal::serializeBinary(const IColumn & column, size_t row_num, WriteBuffer & ostr) const { const FieldType & x = static_cast(column).getData()[row_num]; writeBinary(x, ostr); } template void DataTypeDecimal::serializeBinaryBulk(const IColumn & column, WriteBuffer & ostr, size_t offset, size_t limit) const { const typename ColumnType::Container & x = typeid_cast(column).getData(); size_t size = x.size(); if (limit == 0 || offset + limit > size) limit = size - offset; ostr.write(reinterpret_cast(&x[offset]), sizeof(FieldType) * limit); } template void DataTypeDecimal::deserializeBinary(Field & field, ReadBuffer & istr) const { typename FieldType::NativeType x; readBinary(x, istr); field = DecimalField(T(x), scale); } template void DataTypeDecimal::deserializeBinary(IColumn & column, ReadBuffer & istr) const { typename FieldType::NativeType x; readBinary(x, istr); static_cast(column).getData().push_back(FieldType(x)); } template void DataTypeDecimal::deserializeBinaryBulk(IColumn & column, ReadBuffer & istr, size_t limit, double) const { typename ColumnType::Container & x = typeid_cast(column).getData(); size_t initial_size = x.size(); x.resize(initial_size + limit); size_t size = istr.readBig(reinterpret_cast(&x[initial_size]), sizeof(FieldType) * limit); x.resize(initial_size + size / sizeof(FieldType)); } template Field DataTypeDecimal::getDefault() const { return DecimalField(T(0), scale); } template MutableColumnPtr DataTypeDecimal::createColumn() const { return ColumnType::create(0, scale); } // DataTypePtr createDecimal(UInt64 precision_value, UInt64 scale_value) { if (precision_value < minDecimalPrecision() || precision_value > maxDecimalPrecision()) throw Exception("Wrong precision", ErrorCodes::ARGUMENT_OUT_OF_BOUND); if (static_cast(scale_value) > precision_value) throw Exception("Negative scales and scales larger than presicion are not supported", ErrorCodes::ARGUMENT_OUT_OF_BOUND); if (precision_value <= maxDecimalPrecision()) return std::make_shared>(precision_value, scale_value); else if (precision_value <= maxDecimalPrecision()) return std::make_shared>(precision_value, scale_value); return std::make_shared>(precision_value, scale_value); } 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); const ASTLiteral * precision = typeid_cast(arguments->children[0].get()); const ASTLiteral * scale = typeid_cast(arguments->children[1].get()); 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 scale_value = scale->value.get(); return createDecimal(precision_value, scale_value); } template 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); const ASTLiteral * scale_arg = typeid_cast(arguments->children[0].get()); 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(); UInt64 scale = scale_arg->value.get(); return createDecimal(precision, scale); } void registerDataTypeDecimal(DataTypeFactory & factory) { factory.registerDataType("Decimal32", createExect, DataTypeFactory::CaseInsensitive); factory.registerDataType("Decimal64", createExect, DataTypeFactory::CaseInsensitive); factory.registerDataType("Decimal128", createExect, DataTypeFactory::CaseInsensitive); factory.registerDataType("Decimal", create, DataTypeFactory::CaseInsensitive); factory.registerAlias("DEC", "Decimal", DataTypeFactory::CaseInsensitive); } template <> Decimal32 DataTypeDecimal::getScaleMultiplier(UInt32 scale_) { return decimalScaleMultiplier(scale_); } template <> Decimal64 DataTypeDecimal::getScaleMultiplier(UInt32 scale_) { return decimalScaleMultiplier(scale_); } template <> Decimal128 DataTypeDecimal::getScaleMultiplier(UInt32 scale_) { return decimalScaleMultiplier(scale_); } /// Explicit template instantiations. template class DataTypeDecimal; template class DataTypeDecimal; template class DataTypeDecimal; }