|
| 1 | +#pragma once |
| 2 | + |
| 3 | +/// @file userver/ydb/io/decimal64.hpp |
| 4 | +/// @brief YDB serialization support for `userver::decimal64::Decimal` |
| 5 | +/// |
| 6 | +/// `decimal64::Decimal<Prec, RoundPolicy>` is mapped to the YDB type |
| 7 | +/// `Decimal(22, Prec)`. The precision is fixed at `22`, which matches the |
| 8 | +/// most common "money-like" YDB schema `Decimal(22, 9)` and is sufficient to |
| 9 | +/// hold any value representable by `decimal64::Decimal` (its mantissa fits in |
| 10 | +/// `int64_t`, i.e. up to 19 significant digits). |
| 11 | +/// |
| 12 | +/// Schemas that use a different precision (e.g. `Decimal(35, 18)`) should use |
| 13 | +/// `ydb::Decimal` instead, which carries `precision` and `scale` at runtime. |
| 14 | +/// |
| 15 | +/// On read, `decimal64::Decimal<Prec>::FromStringPermissive` is used, so |
| 16 | +/// values stored with a YDB scale larger than `Prec` are rounded according |
| 17 | +/// to `RoundPolicy` instead of being rejected. |
| 18 | + |
| 19 | +#include <optional> |
| 20 | + |
| 21 | +#include <ydb-cpp-sdk/client/params/params.h> |
| 22 | +#include <ydb-cpp-sdk/client/value/value.h> |
| 23 | + |
| 24 | +#include <userver/compiler/demangle.hpp> |
| 25 | +#include <userver/decimal64/decimal64.hpp> |
| 26 | +#include <userver/logging/log.hpp> |
| 27 | + |
| 28 | +#include <userver/ydb/impl/cast.hpp> |
| 29 | +#include <userver/ydb/io/traits.hpp> |
| 30 | +#include <userver/ydb/types.hpp> |
| 31 | + |
| 32 | +USERVER_NAMESPACE_BEGIN |
| 33 | + |
| 34 | +namespace ydb { |
| 35 | + |
| 36 | +namespace impl { |
| 37 | + |
| 38 | +inline bool IsOptionalDecimal64(const NYdb::TValueParser& parser) { |
| 39 | + return parser.GetKind() == NYdb::TTypeParser::ETypeKind::Optional; |
| 40 | +} |
| 41 | + |
| 42 | +template <int Prec, typename RoundPolicy> |
| 43 | +decimal64::Decimal<Prec, RoundPolicy> ParseDecimal64Value(const NYdb::TValueParser& parser) { |
| 44 | + // YDB stores decimals with a fixed scale, which may differ from `Prec`. |
| 45 | + // `FromStringPermissive` tolerates trailing zeros and rounds extra |
| 46 | + // fractional digits according to `RoundPolicy`, which is what we want. |
| 47 | + return decimal64::Decimal<Prec, RoundPolicy>::FromStringPermissive(parser.GetDecimal().ToString()); |
| 48 | +} |
| 49 | + |
| 50 | +template <int Prec, typename RoundPolicy, typename Builder> |
| 51 | +void WriteDecimal64Value( |
| 52 | + NYdb::TValueBuilderBase<Builder>& builder, |
| 53 | + const decimal64::Decimal<Prec, RoundPolicy>& value |
| 54 | +) { |
| 55 | + builder.Decimal(NYdb::TDecimalValue( |
| 56 | + impl::ToString(decimal64::ToString(value)), |
| 57 | + Decimal::kDefaultPrecision, |
| 58 | + static_cast<std::uint8_t>(Prec) |
| 59 | + )); |
| 60 | +} |
| 61 | + |
| 62 | +inline NYdb::TType MakeDecimal64Type(std::uint8_t scale) { |
| 63 | + NYdb::TTypeBuilder builder; |
| 64 | + builder.Decimal(NYdb::TDecimalType{Decimal::kDefaultPrecision, scale}); |
| 65 | + return builder.Build(); |
| 66 | +} |
| 67 | + |
| 68 | +inline NYdb::TType MakeOptionalDecimal64Type(std::uint8_t scale) { |
| 69 | + NYdb::TTypeBuilder builder; |
| 70 | + builder.BeginOptional(); |
| 71 | + builder.Decimal(NYdb::TDecimalType{Decimal::kDefaultPrecision, scale}); |
| 72 | + builder.EndOptional(); |
| 73 | + return builder.Build(); |
| 74 | +} |
| 75 | + |
| 76 | +} // namespace impl |
| 77 | + |
| 78 | +template <int Prec, typename RoundPolicy> |
| 79 | +struct ValueTraits<decimal64::Decimal<Prec, RoundPolicy>> { |
| 80 | + static_assert( |
| 81 | + Prec >= 0 && Prec <= Decimal::kDefaultPrecision, |
| 82 | + "decimal64::Decimal<Prec> must have 0 <= Prec <= ydb::Decimal::kDefaultPrecision (22) " |
| 83 | + "to map to YDB Decimal(22, Prec); use ydb::Decimal for non-money-like schemas" |
| 84 | + ); |
| 85 | + |
| 86 | + using Type = decimal64::Decimal<Prec, RoundPolicy>; |
| 87 | + |
| 88 | + static Type Parse(NYdb::TValueParser& parser, const ParseContext& /*context*/) { |
| 89 | + const bool is_optional = impl::IsOptionalDecimal64(parser); |
| 90 | + |
| 91 | + if (is_optional) { |
| 92 | + parser.OpenOptional(); |
| 93 | + } |
| 94 | + |
| 95 | + // Will throw exception if value is null. |
| 96 | + auto value = impl::ParseDecimal64Value<Prec, RoundPolicy>(parser); |
| 97 | + |
| 98 | + if (is_optional) { |
| 99 | + parser.CloseOptional(); |
| 100 | + } |
| 101 | + |
| 102 | + return value; |
| 103 | + } |
| 104 | + |
| 105 | + template <typename Builder> |
| 106 | + static void Write(NYdb::TValueBuilderBase<Builder>& builder, const Type& value) { |
| 107 | + impl::WriteDecimal64Value(builder, value); |
| 108 | + } |
| 109 | + |
| 110 | + static NYdb::TType MakeType() { return impl::MakeDecimal64Type(static_cast<std::uint8_t>(Prec)); } |
| 111 | +}; |
| 112 | + |
| 113 | +template <int Prec, typename RoundPolicy> |
| 114 | +struct ValueTraits<std::optional<decimal64::Decimal<Prec, RoundPolicy>>> { |
| 115 | + static_assert( |
| 116 | + Prec >= 0 && Prec <= Decimal::kDefaultPrecision, |
| 117 | + "decimal64::Decimal<Prec> must have 0 <= Prec <= ydb::Decimal::kDefaultPrecision (22) " |
| 118 | + "to map to YDB Decimal(22, Prec); use ydb::Decimal for non-money-like schemas" |
| 119 | + ); |
| 120 | + |
| 121 | + using Type = decimal64::Decimal<Prec, RoundPolicy>; |
| 122 | + |
| 123 | + static std::optional<Type> Parse(NYdb::TValueParser& parser, const ParseContext& context) { |
| 124 | + const bool is_optional = impl::IsOptionalDecimal64(parser); |
| 125 | + if (is_optional) { |
| 126 | + parser.OpenOptional(); |
| 127 | + |
| 128 | + if (parser.IsNull()) { |
| 129 | + parser.CloseOptional(); |
| 130 | + return {}; |
| 131 | + } |
| 132 | + } else { |
| 133 | + LOG_WARNING() |
| 134 | + << "Trying to parse " << context.column_name << " as " |
| 135 | + << compiler::GetTypeName<std::optional<Type>>() << " while actual type is not Optional"; |
| 136 | + } |
| 137 | + |
| 138 | + auto value = impl::ParseDecimal64Value<Prec, RoundPolicy>(parser); |
| 139 | + if (is_optional) { |
| 140 | + parser.CloseOptional(); |
| 141 | + } |
| 142 | + |
| 143 | + return value; |
| 144 | + } |
| 145 | + |
| 146 | + template <typename Builder> |
| 147 | + static void Write(NYdb::TValueBuilderBase<Builder>& builder, const std::optional<Type>& value) { |
| 148 | + if (value) { |
| 149 | + builder.BeginOptional(); |
| 150 | + impl::WriteDecimal64Value(builder, *value); |
| 151 | + builder.EndOptional(); |
| 152 | + } else { |
| 153 | + builder.EmptyOptional(impl::MakeDecimal64Type(static_cast<std::uint8_t>(Prec))); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + static NYdb::TType MakeType() { return impl::MakeOptionalDecimal64Type(static_cast<std::uint8_t>(Prec)); } |
| 158 | +}; |
| 159 | + |
| 160 | +} // namespace ydb |
| 161 | + |
| 162 | +USERVER_NAMESPACE_END |
0 commit comments