#include #include #include #include #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 DECIMAL_OVERFLOW; } template std::string DataTypeDecimal::doGetName() const { std::stringstream ss; ss << "Decimal(" << this->precision << ", " << this->scale << ")"; return ss.str(); } template bool DataTypeDecimal::equals(const IDataType & rhs) const { if (auto * ptype = typeid_cast *>(&rhs)) return this->scale == ptype->getScale(); return false; } template DataTypePtr DataTypeDecimal::promoteNumericType() const { using PromotedType = DataTypeDecimal; return std::make_shared(PromotedType::maxPrecision(), this->scale); } template void DataTypeDecimal::serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const { T value = assert_cast(column).getData()[row_num]; writeText(value, this->scale, ostr); } template bool DataTypeDecimal::tryReadText(T & x, ReadBuffer & istr, UInt32 precision, UInt32 scale) { UInt32 unread_scale = scale; if (!tryReadDecimalText(istr, x, precision, unread_scale)) return false; if (common::mulOverflow(x.value, T::getScaleMultiplier(unread_scale), x.value)) return false; return true; } template void DataTypeDecimal::readText(T & x, ReadBuffer & istr, UInt32 precision, UInt32 scale, bool csv) { UInt32 unread_scale = scale; if (csv) readCSVDecimalText(istr, x, precision, unread_scale); else readDecimalText(istr, x, precision, unread_scale); if (common::mulOverflow(x.value, T::getScaleMultiplier(unread_scale), x.value)) throw Exception("Decimal math overflow", ErrorCodes::DECIMAL_OVERFLOW); } template void DataTypeDecimal::deserializeText(IColumn & column, ReadBuffer & istr, const FormatSettings &) const { T x; readText(x, istr); assert_cast(column).getData().push_back(x); } template void DataTypeDecimal::deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings &) const { T x; readText(x, istr, true); assert_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 = this->scale; readDecimalText(buf, x, this->precision, unread_scale, true); if (common::mulOverflow(x.value, T::getScaleMultiplier(unread_scale), x.value)) throw Exception("Decimal math overflow", ErrorCodes::DECIMAL_OVERFLOW); return x; } template void DataTypeDecimal::serializeProtobuf(const IColumn & column, size_t row_num, ProtobufWriter & protobuf, size_t & value_index) const { if (value_index) return; value_index = static_cast(protobuf.writeDecimal(assert_cast(column).getData()[row_num], this->scale)); } template void DataTypeDecimal::deserializeProtobuf(IColumn & column, ProtobufReader & protobuf, bool allow_add_row, bool & row_added) const { row_added = false; T decimal; if (!protobuf.readDecimal(decimal, this->precision, this->scale)) return; auto & container = assert_cast(column).getData(); if (allow_add_row) { container.emplace_back(decimal); row_added = true; } else container.back() = decimal; } 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 auto * precision = arguments->children[0]->as(); const auto * scale = arguments->children[1]->as(); 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 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 createExact(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 auto * scale_arg = arguments->children[0]->as(); 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 = DecimalUtils::maxPrecision(); UInt64 scale = scale_arg->value.get(); return createDecimal(precision, scale); } void registerDataTypeDecimal(DataTypeFactory & factory) { factory.registerDataType("Decimal32", createExact, DataTypeFactory::CaseInsensitive); factory.registerDataType("Decimal64", createExact, DataTypeFactory::CaseInsensitive); factory.registerDataType("Decimal128", createExact, DataTypeFactory::CaseInsensitive); factory.registerDataType("Decimal", create, DataTypeFactory::CaseInsensitive); factory.registerAlias("DEC", "Decimal", DataTypeFactory::CaseInsensitive); factory.registerAlias("NUMERIC", "Decimal", DataTypeFactory::CaseInsensitive); factory.registerAlias("FIXED", "Decimal", DataTypeFactory::CaseInsensitive); } /// Explicit template instantiations. template class DataTypeDecimal; template class DataTypeDecimal; template class DataTypeDecimal; }