forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_type_decimal.cpp
More file actions
283 lines (255 loc) · 12.2 KB
/
Copy pathdata_type_decimal.cpp
File metadata and controls
283 lines (255 loc) · 12.2 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// This file is copied from
// https://github.com/ClickHouse/ClickHouse/blob/master/src/DataTypes/DataTypesDecimal.cpp
// and modified by Doris
#include "core/data_type/data_type_decimal.h"
#include <fmt/format.h>
#include <gen_cpp/data.pb.h>
#include <streamvbyte.h>
#include <sys/types.h>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include "agent/be_exec_version_manager.h"
#include "common/cast_set.h"
#include "core/assert_cast.h"
#include "core/column/column.h"
#include "core/column/column_const.h"
#include "core/data_type/data_type.h"
#include "core/data_type/data_type_array.h"
#include "core/data_type/data_type_factory.hpp"
#include "core/data_type/data_type_map.h"
#include "core/data_type/data_type_nullable.h"
#include "core/data_type/data_type_struct.h"
#include "core/string_buffer.hpp"
#include "core/typeid_cast.h"
#include "core/types.h"
#include "core/value/decimalv2_value.h"
#include "exec/common/int_exp.h"
#include "exprs/function/cast/cast_to_string.h"
#include "util/string_parser.hpp"
namespace doris {
DataTypePtr get_data_type_with_default_argument(DataTypePtr type) {
auto transform = [&](DataTypePtr t) -> DataTypePtr {
if (t->get_primitive_type() == PrimitiveType::TYPE_DECIMALV2) {
auto not_nullable_t = remove_nullable(t);
const auto* real_type_t = assert_cast<const DataTypeDecimalV2*>(not_nullable_t.get());
// should keep the original precision and scale
DataTypePtr res = std::make_shared<DataTypeDecimalV2>(
BeConsts::MAX_DECIMALV2_PRECISION, BeConsts::MAX_DECIMALV2_SCALE,
real_type_t->get_original_precision(), real_type_t->get_original_scale());
// keep nullable property
if (t->is_nullable()) {
res = make_nullable(res);
}
DCHECK_EQ(res->get_scale(), BeConsts::MAX_DECIMALV2_SCALE);
return res;
} else if (t->get_primitive_type() == PrimitiveType::TYPE_BINARY) {
return DataTypeFactory::instance().create_data_type(TYPE_STRING, t->is_nullable());
} else {
return t;
}
};
// FIXME(gabriel): DECIMALV2 should be processed in a more general way.
if (type->get_primitive_type() == PrimitiveType::TYPE_ARRAY) {
auto nested = std::make_shared<DataTypeArray>(get_data_type_with_default_argument(
assert_cast<const DataTypeArray*>(remove_nullable(type).get())->get_nested_type()));
return type->is_nullable() ? make_nullable(nested) : nested;
} else if (type->get_primitive_type() == PrimitiveType::TYPE_MAP) {
auto nested = std::make_shared<DataTypeMap>(
get_data_type_with_default_argument(
assert_cast<const DataTypeMap*>(remove_nullable(type).get())
->get_key_type()),
get_data_type_with_default_argument(
assert_cast<const DataTypeMap*>(remove_nullable(type).get())
->get_value_type()));
return type->is_nullable() ? make_nullable(nested) : nested;
} else if (type->get_primitive_type() == PrimitiveType::TYPE_STRUCT) {
std::vector<DataTypePtr> data_types;
std::vector<std::string> names;
const auto* type_struct = assert_cast<const DataTypeStruct*>(remove_nullable(type).get());
data_types.reserve(type_struct->get_elements().size());
names.reserve(type_struct->get_elements().size());
for (size_t i = 0; i < type_struct->get_elements().size(); i++) {
data_types.push_back(get_data_type_with_default_argument(type_struct->get_element(i)));
names.push_back(type_struct->get_element_name(i));
}
auto nested = std::make_shared<DataTypeStruct>(data_types, names);
return type->is_nullable() ? make_nullable(nested) : nested;
} else {
return transform(type);
}
}
template <PrimitiveType T>
std::string DataTypeDecimal<T>::do_get_name() const {
std::stringstream ss;
ss << "Decimal(" << precision << ", " << scale << ")";
return ss.str();
}
template <PrimitiveType T>
bool DataTypeDecimal<T>::equals(const IDataType& rhs) const {
if (auto* ptype = typeid_cast<const DataTypeDecimal<T>*>(&rhs)) {
return precision == ptype->get_precision() && scale == ptype->get_scale();
}
return false;
}
template <PrimitiveType T>
std::string DataTypeDecimal<T>::to_string(const FieldType& value) const {
if constexpr (T != TYPE_DECIMALV2) {
return value.to_string(scale);
} else {
auto decemalv2_value = (DecimalV2Value)value;
return decemalv2_value.to_string(get_format_scale());
}
}
// binary: const flag | row num | real_saved_num | data
// data : {val1 | val2| ...} or {encode_size | val1 | val2| ...}
template <PrimitiveType T>
int64_t DataTypeDecimal<T>::get_uncompressed_serialized_bytes(const IColumn& column,
int be_exec_version) const {
auto size = sizeof(bool) + sizeof(size_t) + sizeof(size_t);
auto real_need_copy_num = is_column_const(column) ? 1 : column.size();
auto mem_size = cast_set<UInt32>(sizeof(FieldType) * real_need_copy_num);
if (mem_size <= SERIALIZED_MEM_SIZE_LIMIT) {
return size + mem_size;
} else {
return size + sizeof(size_t) +
std::max(cast_set<size_t>(mem_size),
streamvbyte_max_compressedbytes(upper_int32(mem_size)));
}
}
template <PrimitiveType T>
char* DataTypeDecimal<T>::serialize(const IColumn& column, char* buf, int be_exec_version) const {
const auto* data_column = &column;
size_t real_need_copy_num = 0;
buf = serialize_const_flag_and_row_num(&data_column, buf, &real_need_copy_num);
UInt32 mem_size = cast_set<UInt32>(real_need_copy_num * sizeof(FieldType));
const auto* origin_data = assert_cast<const ColumnDecimal<T>&>(*data_column).get_data().data();
// column data
if (mem_size <= SERIALIZED_MEM_SIZE_LIMIT) {
memcpy(buf, origin_data, mem_size);
return buf + mem_size;
} else {
auto encode_size =
streamvbyte_encode(reinterpret_cast<const uint32_t*>(origin_data),
upper_int32(mem_size), (uint8_t*)(buf + sizeof(size_t)));
unaligned_store<size_t>(buf, encode_size);
buf += sizeof(size_t);
return buf + encode_size;
}
}
template <PrimitiveType T>
const char* DataTypeDecimal<T>::deserialize(const char* buf, MutableColumnPtr* column,
int be_exec_version) const {
auto* origin_column = column->get();
size_t real_have_saved_num = 0;
buf = deserialize_const_flag_and_row_num(buf, column, &real_have_saved_num);
// column data
UInt32 mem_size = cast_set<UInt32>(real_have_saved_num * sizeof(FieldType));
auto& container = assert_cast<ColumnDecimal<T>*>(origin_column)->get_data();
container.resize(real_have_saved_num);
if (mem_size <= SERIALIZED_MEM_SIZE_LIMIT) {
memcpy(container.data(), buf, mem_size);
buf = buf + mem_size;
} else {
auto encode_size = unaligned_load<size_t>(buf);
buf += sizeof(size_t);
streamvbyte_decode((const uint8_t*)buf, (uint32_t*)(container.data()),
upper_int32(mem_size));
buf = buf + encode_size;
}
return buf;
}
template <PrimitiveType T>
void DataTypeDecimal<T>::to_pb_column_meta(PColumnMeta* col_meta) const {
IDataType::to_pb_column_meta(col_meta);
if constexpr (T == TYPE_DECIMALV2) {
const auto* real_type_t = this;
col_meta->mutable_decimal_param()->set_precision(real_type_t->get_original_precision());
col_meta->mutable_decimal_param()->set_scale(real_type_t->get_original_scale());
} else {
col_meta->mutable_decimal_param()->set_precision(precision);
col_meta->mutable_decimal_param()->set_scale(scale);
}
}
template <PrimitiveType T>
MutableColumnPtr DataTypeDecimal<T>::create_column() const {
return ColumnType::create(0, scale);
}
template <PrimitiveType T>
Status DataTypeDecimal<T>::check_column(const IColumn& column) const {
return check_column_non_nested_type<ColumnType>(column);
}
template <PrimitiveType T>
bool DataTypeDecimal<T>::parse_from_string(const std::string& str, FieldType* res) const {
StringParser::ParseResult result = StringParser::PARSE_SUCCESS;
if constexpr (T == TYPE_DECIMALV2) {
*res = DecimalV2Value(
StringParser::string_to_decimal<DataTypeDecimalSerDe<T>::get_primitive_type()>(
str.c_str(), cast_set<Int32>(str.size()), precision, scale, &result));
} else {
res->value = StringParser::string_to_decimal<DataTypeDecimalSerDe<T>::get_primitive_type()>(
str.c_str(), cast_set<Int32>(str.size()), precision, scale, &result);
}
return result == StringParser::PARSE_SUCCESS || result == StringParser::PARSE_UNDERFLOW;
}
DataTypePtr create_decimal(UInt64 precision_value, UInt64 scale_value, bool use_v2) {
auto max_precision = use_v2 ? max_decimal_precision<TYPE_DECIMALV2>()
: max_decimal_precision<TYPE_DECIMAL256>();
if (precision_value < min_decimal_precision() || precision_value > max_precision) {
throw doris::Exception(doris::ErrorCode::NOT_IMPLEMENTED_ERROR,
"Wrong precision {}, min: {}, max: {}", precision_value,
min_decimal_precision(), max_precision);
}
if (scale_value > precision_value) {
throw doris::Exception(doris::ErrorCode::NOT_IMPLEMENTED_ERROR,
"Negative scales and scales larger than precision are not "
"supported, scale_value: {}, precision_value: {}",
scale_value, precision_value);
}
if (use_v2) {
return std::make_shared<DataTypeDecimal<TYPE_DECIMALV2>>(precision_value, scale_value);
}
if (precision_value <= max_decimal_precision<TYPE_DECIMAL32>()) {
return std::make_shared<DataTypeDecimal<TYPE_DECIMAL32>>(precision_value, scale_value);
} else if (precision_value <= max_decimal_precision<TYPE_DECIMAL64>()) {
return std::make_shared<DataTypeDecimal<TYPE_DECIMAL64>>(precision_value, scale_value);
} else if (precision_value <= max_decimal_precision<TYPE_DECIMAL128I>()) {
return std::make_shared<DataTypeDecimal<TYPE_DECIMAL128I>>(precision_value, scale_value);
}
return std::make_shared<DataTypeDecimal<TYPE_DECIMAL256>>(precision_value, scale_value);
}
template <PrimitiveType T>
FieldWithDataType DataTypeDecimal<T>::get_field_with_data_type(const IColumn& column,
size_t row_num) const {
const auto& decimal_column =
assert_cast<const ColumnDecimal<T>&, TypeCheckOnRelease::DISABLE>(column);
Field field;
decimal_column.get(row_num, field);
return FieldWithDataType {.field = std::move(field),
.base_scalar_type_id = get_primitive_type(),
.precision = static_cast<int>(precision),
.scale = static_cast<int>(scale)};
}
/// Explicit template instantiations.
template class DataTypeDecimal<TYPE_DECIMAL32>;
template class DataTypeDecimal<TYPE_DECIMAL64>;
template class DataTypeDecimal<TYPE_DECIMALV2>;
template class DataTypeDecimal<TYPE_DECIMAL128I>;
template class DataTypeDecimal<TYPE_DECIMAL256>;
} // namespace doris