forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_hash_join_basic.cpp
More file actions
573 lines (533 loc) · 17.1 KB
/
ob_hash_join_basic.cpp
File metadata and controls
573 lines (533 loc) · 17.1 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL_ENG
#include "sql/engine/join/ob_hash_join_op.h"
#include "sql/engine/px/ob_px_util.h"
#include "observer/omt/ob_tenant_config_mgr.h"
namespace oceanbase {
namespace sql {
//****************************** ObHashJoinBatch ******************************
ObHashJoinBatch::~ObHashJoinBatch()
{
int ret = OB_SUCCESS;
if (OB_FAIL(close())) {
LOG_WARN("close hash join batch failed", K(ret));
}
}
int ObHashJoinBatch::rescan()
{
int ret = OB_SUCCESS;
row_store_iter_.reset();
chunk_iter_.reset();
return ret;
}
int ObHashJoinBatch::load_next_chunk()
{
int ret = OB_SUCCESS;
if (OB_FAIL(chunk_iter_.load_next_chunk(row_store_iter_))) {
if (OB_ITER_END != ret) {
LOG_WARN("faild to load next chunk", K(ret));
}
}
return ret;
}
int ObHashJoinBatch::finish_dump(bool memory_need_dump)
{
int ret = OB_SUCCESS;
if (OB_FAIL(chunk_row_store_.finish_add_row(memory_need_dump))) {
LOG_WARN("failed to finish chunk row store", K(ret));
} else if (memory_need_dump && 0 != chunk_row_store_.get_mem_used()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expect memroy is 0", K(ret), K(chunk_row_store_.get_mem_used()));
}
return ret;
}
int ObHashJoinBatch::dump(bool all_dump)
{
int ret = OB_SUCCESS;
if (0 >= chunk_row_store_.get_mem_used()) {
// ret = OB_ERR_UNEXPECTED;
// LOG_WARN("unexpect chunk row store use memory", K(ret), K(chunk_row_store_.get_mem_used()));
} else if (OB_FAIL(chunk_row_store_.dump(false, all_dump))) {
LOG_WARN("failed to dump data to chunk row store", K(ret));
}
return ret;
}
int ObHashJoinBatch::add_row(const ObIArray<ObExpr*>& exprs, ObEvalCtx* eval_ctx, ObHashJoinStoredJoinRow*& stored_row)
{
int ret = OB_SUCCESS;
ObChunkDatumStore::StoredRow* hash_store_row = nullptr;
if (OB_FAIL(chunk_row_store_.add_row(exprs, eval_ctx, &hash_store_row))) {
LOG_WARN("failed to add row", K(ret));
} else {
stored_row = static_cast<ObHashJoinStoredJoinRow*>(hash_store_row);
++n_add_rows_;
}
return ret;
}
int ObHashJoinBatch::add_row(const ObHashJoinStoredJoinRow* src_stored_row, ObHashJoinStoredJoinRow*& stored_row)
{
int ret = OB_SUCCESS;
ObChunkDatumStore::StoredRow* hash_store_row = nullptr;
if (OB_FAIL(chunk_row_store_.add_row(*src_stored_row, &hash_store_row))) {
LOG_WARN("failed to add row", K(ret));
} else {
stored_row = static_cast<ObHashJoinStoredJoinRow*>(hash_store_row);
++n_add_rows_;
}
return ret;
}
int ObHashJoinBatch::convert_row(
const ObHashJoinStoredJoinRow* stored_row, const ObIArray<ObExpr*>& exprs, ObEvalCtx& eval_ctx)
{
int ret = OB_SUCCESS;
if (OB_FAIL(row_store_iter_.convert_to_row(stored_row, exprs, eval_ctx))) {
LOG_WARN("failed to convert row", K(ret));
}
return ret;
}
int ObHashJoinBatch::get_next_row(
const ObIArray<ObExpr*>& exprs, ObEvalCtx& eval_ctx, const ObHashJoinStoredJoinRow*& stored_row)
{
int ret = OB_SUCCESS;
const ObChunkDatumStore::StoredRow* inner_stored_row = nullptr;
while (OB_SUCC(ret) && nullptr == inner_stored_row) {
if (OB_FAIL(row_store_iter_.get_next_row(inner_stored_row))) {
if (OB_ITER_END != ret) {
LOG_WARN("failed to get next row", K(ret));
} else if (!is_chunk_iter_) {
ret = OB_SUCCESS;
if (OB_FAIL(chunk_iter_.load_next_chunk(row_store_iter_))) {
if (OB_ITER_END != ret) {
LOG_WARN("failed to get next row", K(ret));
} else if (n_get_rows_ != chunk_row_store_.get_row_cnt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Got row count is not match with row count of chunk row store",
K(ret),
K(n_get_rows_),
K(chunk_row_store_.get_row_cnt()));
}
}
}
} else {
stored_row = static_cast<const ObHashJoinStoredJoinRow*>(inner_stored_row);
if (OB_FAIL(convert_row(stored_row, exprs, eval_ctx))) {
LOG_WARN("failed to convert row", K(ret));
}
++n_get_rows_;
}
}
return ret;
}
int ObHashJoinBatch::get_next_row(const ObHashJoinStoredJoinRow*& stored_row)
{
int ret = OB_SUCCESS;
stored_row = nullptr;
const ObChunkDatumStore::StoredRow* inner_stored_row = nullptr;
while (OB_SUCC(ret) && nullptr == stored_row) {
if (OB_FAIL(row_store_iter_.get_next_row(inner_stored_row))) {
if (OB_ITER_END != ret) {
LOG_WARN("failed to get next row", K(ret));
} else if (!is_chunk_iter_) {
ret = OB_SUCCESS;
if (OB_FAIL(chunk_iter_.load_next_chunk(row_store_iter_))) {
if (OB_ITER_END != ret) {
LOG_WARN("failed to get next row", K(ret));
} else if (n_get_rows_ != chunk_row_store_.get_row_cnt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Got row count is not match with row count of chunk row store",
K(ret),
K(n_get_rows_),
K(chunk_row_store_.get_row_cnt()));
}
}
}
} else {
stored_row = static_cast<const ObHashJoinStoredJoinRow*>(inner_stored_row);
++n_get_rows_;
}
}
return ret;
}
int ObHashJoinBatch::get_next_block_row(const ObHashJoinStoredJoinRow*& stored_row)
{
int ret = OB_SUCCESS;
if (!row_store_iter_.is_valid()) {
if (OB_FAIL(load_next_chunk())) {
if (OB_ITER_END != ret) {
LOG_WARN("failed to load next chunk", K(ret));
}
}
}
if (OB_SUCC(ret)) {
stored_row = nullptr;
const ObChunkDatumStore::StoredRow* inner_stored_row = nullptr;
while (OB_SUCC(ret) && nullptr == stored_row) {
if (OB_FAIL(row_store_iter_.get_next_block_row(inner_stored_row))) {
if (OB_ITER_END != ret) {
LOG_WARN("failed to get next row", K(ret));
} else {
chunk_iter_.reset();
row_store_iter_.reset();
}
} else {
stored_row = static_cast<const ObHashJoinStoredJoinRow*>(inner_stored_row);
++n_get_rows_;
}
}
}
return ret;
}
int ObHashJoinBatch::init_progressive_iterator()
{
int ret = OB_SUCCESS;
int64_t chunk_size = 0;
row_store_iter_.reset();
chunk_iter_.reset();
chunk_size = buf_mgr_->get_page_size();
if (chunk_size != ObChunkDatumStore::BLOCK_SIZE) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected status: chunk size", K(ret), K(chunk_size));
} else if (OB_FAIL(chunk_row_store_.begin(chunk_iter_, chunk_size))) {
LOG_WARN("failed to set row store iterator", K(ret), K(chunk_size));
}
n_get_rows_ = 0;
LOG_TRACE("set iterator: ", K(chunk_size));
return ret;
}
int ObHashJoinBatch::set_iterator(bool is_chunk_iter)
{
int ret = OB_SUCCESS;
int64_t chunk_size = 0;
row_store_iter_.reset();
chunk_iter_.reset();
if (OB_ISNULL(buf_mgr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("buf_mgr_ is null", K(ret));
} else if (is_chunk_iter) {
chunk_size = buf_mgr_->get_reserve_memory_size();
if (buf_mgr_->get_reserve_memory_size() > chunk_row_store_.get_mem_used() + chunk_row_store_.get_file_size()) {
chunk_size = chunk_row_store_.get_mem_used() + chunk_row_store_.get_file_size();
}
if (buf_mgr_->get_page_size() > chunk_size) {
chunk_size = buf_mgr_->get_page_size();
}
if (OB_FAIL(chunk_row_store_.begin(chunk_iter_, chunk_size))) {
LOG_WARN("failed to set chunk iter", K(ret), K(buf_mgr_->get_reserve_memory_size()));
}
} else {
chunk_size = buf_mgr_->get_page_size();
if (OB_FAIL(chunk_row_store_.begin(chunk_iter_, chunk_size))) {
LOG_WARN("failed to set row store iterator", K(ret), K(chunk_size));
} else if (OB_FAIL(load_next_chunk())) {
LOG_WARN("failed to load next chunk", K(ret));
}
}
n_get_rows_ = 0;
is_chunk_iter_ = is_chunk_iter;
LOG_TRACE("set iterator: ", K(chunk_size), K(is_chunk_iter));
return ret;
}
int ObHashJoinBatch::init()
{
int ret = OB_SUCCESS;
if (OB_FAIL(chunk_row_store_.init(
0, tenant_id_, common::ObCtxIds::WORK_AREA, common::ObModIds::OB_ARENA_HASH_JOIN, true, 8))) {
LOG_WARN("failed to init chunk row store", K(ret));
} else {
chunk_row_store_.set_callback(buf_mgr_);
}
return ret;
}
int ObHashJoinBatch::open()
{
return OB_SUCCESS;
}
int ObHashJoinBatch::close()
{
int ret = OB_SUCCESS;
row_store_iter_.reset();
chunk_iter_.reset();
chunk_row_store_.reset();
buf_mgr_ = nullptr;
return ret;
}
//****************************** ObHashJoinBatchMgr ******************************
ObHashJoinBatchMgr::~ObHashJoinBatchMgr()
{
reset();
}
void ObHashJoinBatchMgr::reset()
{
FOREACH(p, batch_list_)
{
if (NULL != p->left_) {
free(p->left_);
p->left_ = NULL;
}
if (NULL != p->right_) {
free(p->right_);
p->right_ = NULL;
}
}
batch_list_.clear();
}
int ObHashJoinBatchMgr::next_batch(ObHashJoinBatchPair& batch_pair)
{
int ret = batch_list_.pop_front(batch_pair);
if (OB_ENTRY_NOT_EXIST == ret) {
ret = OB_ITER_END;
} else if (OB_SUCCESS != ret) {
LOG_WARN("fail to pop front", K(ret));
}
return ret;
}
int ObHashJoinBatchMgr::remove_undumped_batch()
{
int ret = OB_SUCCESS;
int64_t size = batch_list_.size();
int64_t erase_cnt = 0;
hj_batch_pair_list_type::iterator iter = batch_list_.begin();
hj_batch_pair_list_type::iterator pre_iter = batch_list_.end();
while (OB_SUCC(ret) && iter != batch_list_.end()) {
ObHashJoinBatch* left = iter->left_;
ObHashJoinBatch* right = iter->right_;
bool erased = false;
if (nullptr != left && nullptr != right) {
if (left->is_dumped()) {
// right maybe empty
// if (!right->is_dumped()) {
// ret = OB_ERR_UNEXPECTED;
// LOG_WARN("unexpect batch is not match", K(ret), K(left), K(right));
// }
} else if (right->is_dumped()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect batch is not match", K(ret), K(left), K(right));
} else {
++erase_cnt;
erased = true;
if (OB_FAIL(batch_list_.erase(iter))) {
LOG_WARN("failed to remove iter", K(left->get_part_level()), K(left->get_batchno()));
} else {
free(left);
free(right);
}
}
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect batch is null", K(ret), K(left), K(right));
}
if (!erased) {
pre_iter = iter;
++iter;
} else if (pre_iter != batch_list_.end()) {
iter = pre_iter;
} else {
iter = batch_list_.begin();
}
}
if (0 < erase_cnt) {
LOG_TRACE("trace remove undumped batch", K(ret), K(erase_cnt));
}
if (OB_SUCC(ret) && erase_cnt + batch_list_.size() != size) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("failed to remove", K(ret));
}
return ret;
}
int ObHashJoinBatchMgr::get_or_create_batch(
int32_t level, int32_t batchno, bool is_left, ObHashJoinBatch*& batch, bool only_get)
{
int ret = OB_SUCCESS;
bool flag = false;
for (hj_batch_pair_list_type::iterator iter = batch_list_.begin(); iter != batch_list_.end(); iter++) {
if (is_left) {
if (iter->left_->get_part_level() == level && iter->left_->get_batchno() == batchno) {
batch = iter->left_;
flag = true;
break;
}
} else {
if (iter->right_->get_part_level() == level && iter->right_->get_batchno() == batchno) {
batch = iter->right_;
flag = true;
break;
}
}
}
if (!flag && !only_get) { // not found, should new one
ObHashJoinBatchPair batch_pair;
void* buf = alloc_.alloc(sizeof(ObHashJoinBatch));
if (NULL == buf) {
ret = OB_ALLOCATE_MEMORY_FAILED;
} else {
batch_count_++;
batch_pair.left_ = new (buf) ObHashJoinBatch(alloc_, buf_mgr_, tenant_id_, level, batchno);
}
if (OB_SUCC(ret)) {
buf = alloc_.alloc(sizeof(ObHashJoinBatch));
if (NULL == buf) {
ret = OB_ALLOCATE_MEMORY_FAILED;
} else {
batch_count_++;
batch_pair.right_ = new (buf) ObHashJoinBatch(alloc_, buf_mgr_, tenant_id_, level, batchno);
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(batch_list_.push_front(batch_pair))) {
LOG_WARN("fail to push batch pair to batch list", K(ret));
} else {
if (is_left) {
batch = batch_pair.left_;
} else {
batch = batch_pair.right_;
}
}
}
}
return ret;
}
//****************************** ObHashJoinPartition ******************************
int ObHashJoinPartition::init(int32_t part_level, int32_t part_id, bool is_left, ObHashJoinBufMgr* buf_mgr,
ObHashJoinBatchMgr* batch_mgr, ObHashJoinBatch* pre_batch, ObOperator* phy_op, ObSqlMemoryCallback* callback,
int64_t dir_id)
{
int ret = OB_SUCCESS;
UNUSED(pre_batch);
UNUSED(phy_op);
part_level_ = part_level;
part_id_ = part_id;
buf_mgr_ = buf_mgr;
batch_mgr_ = batch_mgr;
if (OB_ISNULL(buf_mgr) || OB_ISNULL(batch_mgr)) {
ret = OB_INNER_STAT_ERROR;
LOG_WARN("buf mgr or batch_mgr is null", K(ret), K(part_level_), K(part_id_), K(is_left), K(buf_mgr), K(batch_mgr));
} else if (OB_FAIL(batch_mgr_->get_or_create_batch(part_level_, part_id_, is_left, batch_))) {
LOG_WARN("fail to get batch", K(ret), K(part_level_), K(part_id_), K(is_left));
} else if (OB_ISNULL(batch_)) {
ret = OB_INNER_STAT_ERROR;
LOG_WARN("fail to get batch ", K(ret), K(part_level_), K(part_id_), K(is_left));
} else if (OB_FAIL(check())) {
LOG_WARN("fail to check partition", K(ret), K(part_level_), K(part_id_), K(is_left));
} else if (OB_FAIL(batch_->init())) {
LOG_WARN("fail to init batch", K(ret), K(part_level_), K(part_id_), K(is_left));
} else {
batch_->get_chunk_row_store().set_block_size(buf_mgr_->get_page_size());
batch_->get_chunk_row_store().set_callback(callback);
batch_->get_chunk_row_store().set_dir_id(dir_id);
}
return ret;
}
int ObHashJoinPartition::record_pre_batch_info(int64_t pre_part_count, int64_t pre_bucket_number, int64_t total_size)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(batch_)) {
ret = OB_INNER_STAT_ERROR;
LOG_WARN("fail to get batch ", K(ret), K(pre_part_count), K(pre_bucket_number), K(total_size));
} else {
batch_->set_pre_part_count(pre_part_count);
batch_->set_pre_bucket_number(pre_bucket_number);
batch_->set_pre_total_size(total_size);
}
return ret;
}
int ObHashJoinPartition::init_iterator(bool is_chunk_iter)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(batch_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("batch_ is null", K(ret));
} else if (OB_FAIL(batch_->set_iterator(is_chunk_iter))) {
LOG_WARN("failed to set iterator", K(ret));
}
return ret;
}
int ObHashJoinPartition::init_progressive_iterator()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(batch_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("batch_ is null", K(ret));
} else if (OB_FAIL(batch_->init_progressive_iterator())) {
LOG_WARN("failed to set iterator", K(ret));
}
return ret;
}
int ObHashJoinPartition::check()
{
int ret = common::OB_SUCCESS;
if (part_level_ == -1 || part_id_ == -1) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("part_level_ and part_id_ should not be null");
} else if (buf_mgr_ == NULL || batch_mgr_ == NULL) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("buf_mgr_ and batch_mgr_ should not be null");
}
return ret;
}
void ObHashJoinPartition::reset()
{
buf_mgr_ = nullptr;
batch_mgr_ = nullptr;
batch_ = nullptr;
}
int ObHashJoinPartition::add_row(
const ObIArray<ObExpr*>& exprs, ObEvalCtx* eval_ctx, ObHashJoinStoredJoinRow*& stored_row)
{
int ret = OB_SUCCESS;
if (OB_FAIL(batch_->add_row(exprs, eval_ctx, stored_row))) {
LOG_WARN("failed to add row to chunk row store");
}
return ret;
}
int ObHashJoinPartition::add_row(const ObHashJoinStoredJoinRow* src_stored_row, ObHashJoinStoredJoinRow*& stored_row)
{
int ret = OB_SUCCESS;
if (OB_FAIL(batch_->add_row(src_stored_row, stored_row))) {
LOG_WARN("failed to add row to chunk row store");
}
return ret;
}
int ObHashJoinPartition::finish_dump(bool memory_need_dump)
{
int ret = OB_SUCCESS;
if (OB_FAIL(batch_->finish_dump(memory_need_dump))) {
LOG_WARN("fail to finish batch dump", K(ret));
}
return ret;
}
int ObHashJoinPartition::dump(bool all_dump)
{
int ret = OB_SUCCESS;
if (OB_FAIL(batch_->dump(all_dump))) {
LOG_WARN("failed to dump data to chunk row store", K(ret));
}
return ret;
}
int ObHashJoinPartition::get_next_row(const ObHashJoinStoredJoinRow*& stored_row)
{
int ret = OB_SUCCESS;
if (OB_FAIL(batch_->get_next_row(stored_row)) && OB_ITER_END != ret) {
LOG_WARN("failed to get next row", K(ret));
}
return ret;
}
int ObHashJoinPartition::get_next_block_row(const ObHashJoinStoredJoinRow*& stored_row)
{
int ret = OB_SUCCESS;
if (OB_FAIL(batch_->get_next_block_row(stored_row)) && OB_ITER_END != ret) {
LOG_WARN("failed to get next row", K(ret));
}
return ret;
}
} // end namespace sql
} // end namespace oceanbase