-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathdecimal.cpp
More file actions
250 lines (207 loc) · 6.74 KB
/
decimal.cpp
File metadata and controls
250 lines (207 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "decimal.h"
#include "../types/int128.h"
namespace
{
using namespace clickhouse;
#ifdef ABSL_HAVE_INTRINSIC_INT128
template <typename T>
inline bool addOverflow(const Int128 & l, const T & r, Int128 * result)
{
__int128 res;
const auto ret_value = __builtin_add_overflow(static_cast<__int128>(l), static_cast<__int128>(r), &res);
*result = res;
return ret_value;
}
template <typename T>
inline bool mulOverflow(const Int128 & l, const T & r, Int128 * result)
{
__int128 res;
const auto ret_value = __builtin_mul_overflow(static_cast<__int128>(l), static_cast<__int128>(r), &res);
*result = res;
return ret_value;
}
#else
template <typename T>
inline bool getSignBit(const T & v)
{
return std::signbit(v);
}
inline bool getSignBit(const Int128 & v)
{
// static constexpr Int128 zero {};
// return v < zero;
// Sign of the whole absl::int128 value is determined by sign of higher 64 bits.
return absl::Int128High64(v) < 0;
}
inline bool addOverflow(const Int128 & l, const Int128 & r, Int128 * result)
{
// *result = l + r;
// const auto result_sign = getSignBit(*result);
// return l_sign == r_sign && l_sign != result_sign;
// Based on code from:
// https://wiki.sei.cmu.edu/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow#INT32C.Ensurethatoperationsonsignedintegersdonotresultinoverflow-CompliantSolution
const auto r_positive = !getSignBit(r);
if ((r_positive && (l > (std::numeric_limits<Int128>::max() - r))) ||
(!r_positive && (l < (std::numeric_limits<Int128>::min() - r)))) {
return true;
}
*result = l + r;
return false;
}
template <typename T>
inline bool mulOverflow(const Int128 & l, const T & r, Int128 * result)
{
// Based on code from:
// https://wiki.sei.cmu.edu/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow#INT32C.Ensurethatoperationsonsignedintegersdonotresultinoverflow-CompliantSolution.3
const auto l_positive = !getSignBit(l);
const auto r_positive = !getSignBit(r);
if (l_positive) {
if (r_positive) {
if (r != 0 && l > (std::numeric_limits<Int128>::max() / r)) {
return true;
}
} else {
if (l != 0 && r < (std::numeric_limits<Int128>::min() / l)) {
return true;
}
}
} else {
if (r_positive) {
if (r != 0 && l < (std::numeric_limits<Int128>::min() / r)) {
return true;
}
} else {
if (l != 0 && (r < (std::numeric_limits<Int128>::max() / l))) {
return true;
}
}
}
*result = l * r;
return false;
}
#endif
}
namespace clickhouse {
ColumnDecimal::ColumnDecimal(size_t precision, size_t scale)
: Column(Type::CreateDecimal(precision, scale))
{
if (precision <= 9) {
data_ = std::make_shared<ColumnInt32>();
} else if (precision <= 18) {
data_ = std::make_shared<ColumnInt64>();
} else {
data_ = std::make_shared<ColumnInt128>();
}
}
ColumnDecimal::ColumnDecimal(TypeRef type, ColumnRef data)
: Column(type),
data_(data)
{
}
void ColumnDecimal::Append(Int64 value) {
Append(static_cast<Int128>(value));
}
void ColumnDecimal::Append(const Int128& value) {
if (data_->Type()->GetCode() == Type::Int32) {
data_->As<ColumnInt32>()->Append(static_cast<ColumnInt32::DataType>(value));
} else if (data_->Type()->GetCode() == Type::Int64) {
data_->As<ColumnInt64>()->Append(static_cast<ColumnInt64::DataType>(value));
} else {
data_->As<ColumnInt128>()->Append(static_cast<ColumnInt128::DataType>(value));
}
}
void ColumnDecimal::Append(const std::string& value) {
Int128 int_value = 0;
auto c = value.begin();
auto end = value.end();
bool sign = true;
bool has_dot = false;
int zeros = 0;
while (c != end) {
if (*c == '-') {
sign = false;
if (c != value.begin()) {
break;
}
} else if (*c == '.' && !has_dot) {
size_t distance = std::distance(c, end) - 1;
auto scale = type_->As<DecimalType>()->GetScale();
if (distance <= scale) {
zeros = scale - distance;
} else {
std::advance(end, scale - distance);
}
has_dot = true;
} else if (*c >= '0' && *c <= '9') {
if (mulOverflow(int_value, 10, &int_value) ||
addOverflow(int_value, *c - '0', &int_value)) {
throw std::runtime_error("value is too big for 128-bit integer");
}
} else {
throw std::runtime_error(std::string("unexpected symbol '") + (*c) + "' in decimal value");
}
++c;
}
if (c != end) {
throw std::runtime_error("unexpected symbol '-' in decimal value");
}
while (zeros) {
if (mulOverflow(int_value, 10, &int_value)) {
throw std::runtime_error("value is too big for 128-bit integer");
}
--zeros;
}
Append(sign ? int_value : -int_value);
}
Int128 ColumnDecimal::At(size_t i) const {
switch (data_->Type()->GetCode()) {
case Type::Int32:
return static_cast<Int128>(data_->As<ColumnInt32>()->At(i));
case Type::Int64:
return static_cast<Int128>(data_->As<ColumnInt64>()->At(i));
case Type::Int128:
return data_->As<ColumnInt128>()->At(i);
default:
throw std::runtime_error("Invalid data_ column type in ColumnDecimal");
}
}
Int64 ColumnDecimal::AtAsInt64(size_t i) const {
return static_cast<Int64>(At(i));
}
void ColumnDecimal::Append(ColumnRef column) {
if (auto col = column->As<ColumnDecimal>()) {
data_->Append(col->data_);
}
}
bool ColumnDecimal::Load(CodedInputStream* input, size_t rows) {
return data_->Load(input, rows);
}
void ColumnDecimal::Save(CodedOutputStream* output) {
data_->Save(output);
}
void ColumnDecimal::Clear() {
data_->Clear();
}
size_t ColumnDecimal::Size() const {
return data_->Size();
}
ColumnRef ColumnDecimal::Slice(size_t begin, size_t len) const {
// coundn't use std::make_shared since this c-tor is private
return ColumnRef{new ColumnDecimal(type_, data_->Slice(begin, len))};
}
void ColumnDecimal::Swap(Column& other) {
auto & col = dynamic_cast<ColumnDecimal &>(other);
data_.swap(col.data_);
}
ItemView ColumnDecimal::GetItem(size_t index) const {
return data_->GetItem(index);
}
size_t ColumnDecimal::GetScale() const
{
return type_->As<DecimalType>()->GetScale();
}
size_t ColumnDecimal::GetPrecision() const
{
return type_->As<DecimalType>()->GetPrecision();
}
}