forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_type_string.cpp
More file actions
190 lines (170 loc) · 7.52 KB
/
Copy pathdata_type_string.cpp
File metadata and controls
190 lines (170 loc) · 7.52 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
// 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/DataTypeString.cpp
// and modified by Doris
#include "core/data_type/data_type_string.h"
#include <lz4/lz4.h>
#include <streamvbyte.h>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include "agent/be_exec_version_manager.h"
#include "common/cast_set.h"
#include "common/exception.h"
#include "common/status.h"
#include "core/assert_cast.h"
#include "core/column/column.h"
#include "core/column/column_const.h"
#include "core/column/column_string.h"
#include "core/data_type/primitive_type.h"
#include "core/field.h"
#include "core/string_buffer.hpp"
#include "core/string_ref.h"
#include "core/types.h"
namespace doris {
MutableColumnPtr DataTypeString::create_column() const {
return ColumnString::create();
}
Status DataTypeString::check_column(const IColumn& column) const {
if (column.is_column_string64()) {
return Status::OK();
}
return check_column_non_nested_type<ColumnString>(column);
}
bool DataTypeString::equals(const IDataType& rhs) const {
return typeid(rhs) == typeid(*this);
}
// binary: const flag | row num | read saved num | offset | chars
// offset: {offset1 | offset2 ...} or {encode_size | offset1 |offset2 ...}
// chars : {value_length | <value1> | <value2 ...} or {value_length | encode_size | <value1> | <value2 ...}
int64_t DataTypeString::get_uncompressed_serialized_bytes(const IColumn& column,
int be_exec_version) const {
int64_t size = sizeof(bool) + sizeof(size_t) + sizeof(size_t);
bool is_const_column = is_column_const(column);
const IColumn* string_column = &column;
if (is_const_column) {
const auto& const_column = assert_cast<const ColumnConst&>(column);
string_column = &(const_column.get_data_column());
}
const auto& data_column = assert_cast<const ColumnString&>(*string_column);
auto real_need_copy_num = is_const_column ? 1 : data_column.size();
auto offsets_size = real_need_copy_num * sizeof(IColumn::Offset);
if (offsets_size <= SERIALIZED_MEM_SIZE_LIMIT) {
size += offsets_size;
} else {
// Throw exception if offsets_size is large than UINT32_MAX
size += sizeof(size_t) +
std::max(offsets_size, streamvbyte_max_compressedbytes(
cast_set<UInt32>(upper_int32(offsets_size))));
}
size += sizeof(size_t);
if (size_t bytes = data_column.get_chars().size(); bytes <= SERIALIZED_MEM_SIZE_LIMIT) {
size += bytes;
} else {
if (bytes > LZ4_MAX_INPUT_SIZE) {
throw Exception(ErrorCode::BUFFER_OVERFLOW,
"LZ4_compressBound meet invalid input size, input_size={}, "
"LZ4_MAX_INPUT_SIZE={}",
bytes, LZ4_MAX_INPUT_SIZE);
}
size += sizeof(size_t) +
std::max(bytes, (size_t)LZ4_compressBound(cast_set<UInt32>(bytes)));
}
return size;
}
char* DataTypeString::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);
// mem_size = real_row_num * sizeof(IColumn::Offset)
size_t mem_size = real_need_copy_num * sizeof(IColumn::Offset);
const auto& string_column = assert_cast<const ColumnString&>(*data_column);
// offsets
if (mem_size <= SERIALIZED_MEM_SIZE_LIMIT) {
memcpy(buf, string_column.get_offsets().data(), mem_size);
buf += mem_size;
} else {
// Throw exception if mem_size is large than UINT32_MAX
auto encode_size = streamvbyte_encode(
reinterpret_cast<const uint32_t*>(string_column.get_offsets().data()),
cast_set<uint32_t>(upper_int32(mem_size)), (uint8_t*)(buf + sizeof(size_t)));
unaligned_store<size_t>(buf, encode_size);
buf += (sizeof(size_t) + encode_size);
}
// values
auto value_len = string_column.get_chars().size();
unaligned_store<size_t>(buf, value_len);
buf += sizeof(size_t);
if (value_len <= SERIALIZED_MEM_SIZE_LIMIT) {
memcpy(buf, string_column.get_chars().data(), value_len);
buf += value_len;
} else {
auto encode_size = LZ4_compress_fast(string_column.get_chars().raw_data(),
(buf + sizeof(size_t)), cast_set<Int32>(value_len),
LZ4_compressBound(cast_set<Int32>(value_len)), 1);
unaligned_store<size_t>(buf, encode_size);
buf += (sizeof(size_t) + encode_size);
}
return buf;
}
const char* DataTypeString::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);
auto mem_size = real_have_saved_num * sizeof(IColumn::Offset);
auto* column_string = assert_cast<ColumnString*>(origin_column);
ColumnString::Chars& data = column_string->get_chars();
ColumnString::Offsets& offsets = column_string->get_offsets();
offsets.resize(real_have_saved_num);
// offsets
if (mem_size <= SERIALIZED_MEM_SIZE_LIMIT) {
memcpy(offsets.data(), buf, mem_size);
buf += mem_size;
} else {
size_t encode_size = unaligned_load<size_t>(buf);
buf += sizeof(size_t);
streamvbyte_decode((const uint8_t*)buf, (uint32_t*)(offsets.data()),
cast_set<UInt32>(upper_int32(mem_size)));
buf += encode_size;
}
// total length
size_t value_len = unaligned_load<size_t>(buf);
buf += sizeof(size_t);
data.resize(value_len);
// values
if (value_len <= SERIALIZED_MEM_SIZE_LIMIT) {
memcpy(data.data(), buf, value_len);
buf += value_len;
} else {
size_t encode_size = unaligned_load<size_t>(buf);
buf += sizeof(size_t);
LZ4_decompress_safe(buf, reinterpret_cast<char*>(data.data()), cast_set<Int32>(encode_size),
cast_set<Int32>(value_len));
buf += encode_size;
}
return buf;
}
FieldWithDataType DataTypeString::get_field_with_data_type(const IColumn& column,
size_t row_num) const {
const auto& column_data = assert_cast<const ColumnString&, TypeCheckOnRelease::DISABLE>(column);
return FieldWithDataType {
.field = Field::create_field<TYPE_STRING>(column_data.get_data_at(row_num).to_string()),
.base_scalar_type_id = get_primitive_type()};
}
} // namespace doris