forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_compact_row.cpp
More file actions
407 lines (389 loc) · 13 KB
/
Copy pathob_compact_row.cpp
File metadata and controls
407 lines (389 loc) · 13 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright (c) 2025 OceanBase.
*
* Licensed 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.
*/
#define USING_LOG_PREFIX SQL_ENG
#include "ob_compact_row.h"
#include "sql/engine/expr/ob_array_expr_utils.h"
namespace oceanbase
{
namespace sql
{
int RowMeta::init(const ObExprPtrIArray &exprs,
const int32_t extra_size,
const bool enable_reorder_expr /*ture*/,
common::ObIAllocator *allocator /*NULL*/)
{
int ret = OB_SUCCESS;
reset();
col_cnt_ = exprs.count();
extra_size_ = extra_size;
fixed_cnt_ = 0;
fixed_offsets_ = NULL;
projector_ = NULL;
nulls_off_ = 0;
allocator_ = allocator;
var_offsets_off_ = nulls_off_ + ObTinyBitVector::memory_size(col_cnt_);
if (enable_reorder_expr) {
for (int64_t i = 0; i < exprs.count() && OB_SUCC(ret); ++i) {
ObExpr *expr = exprs.at(i);
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr is null", K(ret));
} else if (expr->is_fixed_length_data_) {
fixed_cnt_++;
}
}
}
if (!use_local_allocator() && nullptr == allocator_) {
fixed_cnt_ = 0;
}
const int64_t var_col_cnt = get_var_col_cnt();
extra_off_ = var_offsets_off_;
if (var_col_cnt > 0) {
// If there is no variable-length data, there is no need to store the variable-length offset.
extra_off_ += (var_col_cnt + 1) * sizeof(int32_t);
}
fix_data_off_ = extra_off_ + extra_size_;
if (OB_SUCC(ret) && fixed_cnt_ > 0) {
ObDataBuffer local_alloc(buf_, MAX_LOCAL_BUF_LEN);
ObIAllocator *alloc = use_local_allocator() ? &local_alloc : allocator_;
if (OB_ISNULL(alloc)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("allocator is null", K(ret), K(lbt()));
} else if (OB_ISNULL(projector_ =
static_cast<int32_t *>(alloc->alloc(sizeof(int32_t) * col_cnt_)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc projector failed", K(ret), K(col_cnt_));
} else if (OB_ISNULL(fixed_offsets_ =
static_cast<int32_t *>(alloc->alloc(sizeof(int32_t) * (fixed_cnt_ + 1))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc fixed_offsets_ failed", K(ret), K(col_cnt_));
} else {
int64_t project_idx = fixed_cnt_;
int64_t fixed_idx = 0;
fixed_offsets_[0] = fix_data_off_;
for (int64_t i = 0; i < exprs.count() && OB_SUCC(ret); ++i) {
ObExpr *expr = exprs.at(i);
if (expr->is_fixed_length_data_) {
fixed_offsets_[fixed_idx + 1] = fixed_offsets_[fixed_idx] + expr->get_fixed_length();
projector_[i] = fixed_idx++;
} else {
projector_[i] = project_idx++;
}
}
}
if (OB_FAIL(ret)) {
reset();
}
}
if (OB_SUCC(ret)) {
// The variable-length data is at the end of the fixed-length data. there is no fixed-length
// data, it is after fix_data_off_. If it exists, it is the value of the last fixed offset.
if (fixed_cnt_ > 0) {
var_data_off_ = fixed_offsets_[fixed_cnt_];
} else {
var_data_off_ = fix_data_off_;
}
}
return ret;
}
int RowMeta::assign(const RowMeta &row_meta, common::ObIAllocator *allocator)
{
int ret = OB_SUCCESS;
allocator_ = row_meta.allocator_;
col_cnt_ = row_meta.col_cnt_;
extra_size_ = row_meta.extra_size_;
fixed_cnt_ = row_meta.fixed_cnt_;
nulls_off_ = row_meta.nulls_off_;
var_offsets_off_ = row_meta.var_offsets_off_;
extra_off_ = row_meta.extra_off_;
fix_data_off_ = row_meta.fix_data_off_;
var_data_off_ = row_meta.var_data_off_;
if (fixed_cnt_ > 0) {
ObDataBuffer local_alloc(buf_, MAX_LOCAL_BUF_LEN);
ObIAllocator *alloc = use_local_allocator() ? &local_alloc : allocator_;
if (OB_ISNULL(alloc)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("allocator is null", K(ret), K(lbt()));
} else if (OB_ISNULL(projector_ =
static_cast<int32_t *>(alloc->alloc(sizeof(int32_t) * col_cnt_)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc projector failed", K(ret), K(col_cnt_));
} else if (OB_ISNULL(fixed_offsets_ =
static_cast<int32_t *>(alloc->alloc(sizeof(int32_t) * (fixed_cnt_ + 1))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc fixed_offsets_ failed", K(ret), K(col_cnt_));
} else {
memcpy(projector_, row_meta.projector_, sizeof(int32_t) * col_cnt_);
memcpy(fixed_offsets_, row_meta.fixed_offsets_, sizeof(int32_t) * (fixed_cnt_ + 1));
}
if (OB_FAIL(ret)) {
LOG_ERROR("row meta assgin failed", K(ret));
reset();
}
}
return ret;
}
void RowMeta::reset()
{
if (!use_local_allocator() && NULL != allocator_) {
if (NULL != fixed_offsets_) {
allocator_->free(fixed_offsets_);
}
if (NULL != projector_) {
allocator_->free(projector_);
}
}
col_cnt_ = 0;
fixed_offsets_ = NULL;
projector_ = NULL;
}
int32_t RowMeta::get_row_fixed_size(const int64_t col_cnt,
const int64_t fixed_payload_len,
const int64_t extra_size,
const bool enable_reorder_expr /*false*/)
{
// TODO shengle init fixed info
int32_t row_fixed_size = sizeof(RowHeader) + fixed_payload_len + extra_size;
row_fixed_size += ObBitVector::memory_size(col_cnt);
if (!enable_reorder_expr) {
row_fixed_size += (col_cnt + 1) * sizeof(int32_t);
}
return row_fixed_size;
}
int64_t ObCompactRow::calc_max_row_size(const ObExprPtrIArray &exprs, int32_t extra_size)
{
int64_t res = 0;
ObArenaAllocator alloc;
RowMeta tmp_meta(&alloc);
int ret = OB_SUCCESS;
if (OB_FAIL(tmp_meta.init(exprs, extra_size))) {
res += INT32_MAX;
LOG_WARN("failed to init row meta", K(ret));
} else {
res += tmp_meta.fix_data_off_;
for (int64_t i = 0; i < exprs.count(); ++i) {
if (T_REF_COLUMN == exprs.at(i)->type_ && exprs.at(i)->max_length_ > 0) {
res += exprs.at(i)->max_length_;
} else {
res += INT32_MAX;
}
}
}
return res;
}
void ObCompactRow::init(const RowMeta &meta)
{
MEMSET(payload_, 0, meta.fix_data_off_);
}
int ObCompactRow::calc_row_size(const RowMeta &row_meta, const common::ObIArray<ObExpr *> &exprs,
const ObBatchRows &brs, ObEvalCtx &ctx, int64_t &size)
{
int ret = OB_SUCCESS;
size = row_meta.get_row_fixed_size();
const bool reordered = row_meta.fixed_expr_reordered();
const int64_t row_idx = ctx.get_batch_idx();
for (int64_t col_idx = 0; col_idx < exprs.count() && OB_SUCC(ret); ++col_idx) {
ObExpr *expr = exprs.at(col_idx);
// ensure the vector is evaluated
if (OB_FAIL(expr->eval_vector(ctx, brs))) {
SQL_ENG_LOG(WARN, "fail to evel vector", K(ret), K(expr));
} else if (reordered && row_meta.project_idx(col_idx) < row_meta.fixed_cnt_) {
// continue, the size is computed in `fixed_size`
} else if (expr->is_nested_expr()) {
int64_t len = 0;
if (OB_FAIL(ObArrayExprUtils::calc_nested_expr_data_size(*expr, ctx, row_idx, len))) {
SQL_ENG_LOG(WARN, "fail to calc nested expr data size", K(ret));
} else {
size += len;
}
} else {
ObIVector *vec = expr->get_vector(ctx);
const VectorFormat format = vec->get_format();
if (VEC_DISCRETE == format) {
ObDiscreteBase *disc_vec = static_cast<ObDiscreteBase *>(vec);
if (!disc_vec->is_null(row_idx)) {
ObLength *lens = disc_vec->get_lens();
size += lens[row_idx];
}
} else if (VEC_CONTINUOUS == format) {
ObContinuousBase *cont_vec = static_cast<ObContinuousBase *>(vec);
uint32_t *offsets = cont_vec->get_offsets();
size += (offsets[row_idx + 1] - offsets[row_idx]);
} else if (is_uniform_format(format)) {
ObUniformBase *uni_vec = static_cast<ObUniformBase *>(vec);
ObDatum *datums = uni_vec->get_datums();
size += datums[expr->get_datum_idx(ctx)].len_;
} else if (VEC_FIXED == format) {
ObFixedLengthBase *fixed_vec = static_cast<ObFixedLengthBase *>(vec);
size += fixed_vec->get_length();
}
}
}
return ret;
}
OB_DEF_SERIALIZE(RowMeta)
{
int ret = OB_SUCCESS;
LST_DO_CODE(OB_UNIS_ENCODE,
col_cnt_,
extra_size_,
fixed_cnt_,
nulls_off_,
var_offsets_off_,
extra_off_,
fix_data_off_,
var_data_off_);
if (fixed_expr_reordered()) {
for (int64_t i = 0; OB_SUCC(ret) && i < col_cnt_; ++i) {
OB_UNIS_ENCODE(projector_[i]);
}
for (int64_t i = 0; OB_SUCC(ret) && i <= fixed_cnt_; ++i) {
OB_UNIS_ENCODE(fixed_offsets_[i]);
}
}
return ret;
}
OB_DEF_DESERIALIZE(RowMeta)
{
int ret = OB_SUCCESS;
LST_DO_CODE(OB_UNIS_DECODE,
col_cnt_,
extra_size_,
fixed_cnt_,
nulls_off_,
var_offsets_off_,
extra_off_,
fix_data_off_,
var_data_off_);
projector_ = NULL;
if (fixed_expr_reordered()) {
ObDataBuffer local_alloc(buf_, MAX_LOCAL_BUF_LEN);
ObIAllocator *alloc = use_local_allocator() ? &local_alloc : allocator_;
if (OB_ISNULL(alloc)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("allocator is null", K(ret));
} else if (OB_ISNULL(projector_ =
static_cast<int32_t *>(alloc->alloc(sizeof(int32_t) * col_cnt_)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc projector failed", K(ret), K(col_cnt_));
} else if (OB_ISNULL(fixed_offsets_ =
static_cast<int32_t *>(alloc->alloc(sizeof(int32_t) * (fixed_cnt_ + 1))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc projector failed", K(ret), K(col_cnt_));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < col_cnt_; ++i) {
OB_UNIS_DECODE(projector_[i]);
}
for (int64_t i = 0; OB_SUCC(ret) && i <= fixed_cnt_; ++i) {
OB_UNIS_DECODE(fixed_offsets_[i]);
}
}
if (OB_FAIL(ret)) {
reset();
}
}
return ret;
}
OB_DEF_SERIALIZE_SIZE(RowMeta)
{
int64_t len = 0;
LST_DO_CODE(OB_UNIS_ADD_LEN,
col_cnt_,
extra_size_,
fixed_cnt_,
nulls_off_,
var_offsets_off_,
extra_off_,
fix_data_off_,
var_data_off_);
if (fixed_expr_reordered()) {
for (int64_t i = 0; i < col_cnt_; ++i) {
OB_UNIS_ADD_LEN(projector_[i]);
}
for (int64_t i = 0; i <= fixed_cnt_; ++i) {
OB_UNIS_ADD_LEN(fixed_offsets_[i]);
}
}
return len;
}
int RowMeta::deep_copy(const RowMeta &other, common::ObIAllocator *allocator)
{
int ret = OB_SUCCESS;
reset();
*this = other;
fixed_offsets_ = NULL;
projector_ = NULL;
allocator_ = allocator;
if (fixed_expr_reordered()) {
ObDataBuffer local_alloc(buf_, MAX_LOCAL_BUF_LEN);
ObIAllocator *alloc = use_local_allocator() ? &local_alloc : allocator_;
if (OB_ISNULL(alloc)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("allocator is null", K(ret));
} else if (OB_ISNULL(projector_ =
static_cast<int32_t *>(alloc->alloc(sizeof(int32_t) * col_cnt_)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc projector failed", K(ret), K(col_cnt_));
} else if (OB_ISNULL(fixed_offsets_ =
static_cast<int32_t *>(alloc->alloc(sizeof(int32_t) * (fixed_cnt_ + 1))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc projector failed", K(ret), K(col_cnt_));
} else {
MEMCPY(projector_, other.projector_, col_cnt_ * sizeof(int32_t));
MEMCPY(fixed_offsets_, other.fixed_offsets_, (fixed_cnt_ + 1) * sizeof(int32_t));
}
if (OB_FAIL(ret)) {
reset();
}
}
return ret;
}
int64_t ToStrCompactRow::to_string(char *buf, const int64_t buf_len) const
{
int ret = OB_SUCCESS;
int64_t pos = 0;
J_ARRAY_START();
ObDatum d;
for (int col_id = 0; col_id < meta_.col_cnt_; col_id++) {
d = row_.get_datum(meta_, col_id);
if (NULL == exprs_) {
pos += d.to_string(buf + pos, buf_len - pos);
} else {
ObExpr *expr = exprs_->at(col_id);
if (NULL == expr) {
pos += d.to_string(buf + pos, buf_len - pos);
} else {
pos += DATUM2STR(*expr, d).to_string(buf + pos, buf_len - pos);
}
}
if (col_id != meta_.col_cnt_ - 1) {
J_COMMA();
}
}
if (meta_.extra_size_ > 0) {
char *extra = static_cast<char *>(row_.get_extra_payload(meta_));
// print hex value
BUF_PRINTF(", extra_hex: ");
if (OB_FAIL(hex_print(extra, meta_.extra_size_, buf, buf_len, pos))) {
// do nothing
}
J_COMMA();
}
J_ARRAY_END();
return pos;
}
} // end namespace sql
} // end namespace oceanbase