forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_join_filter_op.cpp
More file actions
2226 lines (2105 loc) · 89.8 KB
/
Copy pathob_join_filter_op.cpp
File metadata and controls
2226 lines (2105 loc) · 89.8 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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 "sql/engine/join/ob_join_filter_op.h"
#include "sql/dtl/ob_dtl_channel_group.h"
#include "sql/engine/px/p2p_datahub/ob_p2p_dh_mgr.h"
#include "sql/engine/join/hash_join/ob_hash_join_vec_op.h"
#include "sql/engine/join/ob_partition_store.h"
#include "sql/engine/px/ob_px_sqc_handler.h"
using namespace oceanbase;
using namespace common;
using namespace omt;
using namespace sql;
using namespace oceanbase::sql::dtl;
ERRSIM_POINT_DEF(DHSENDOPTLOCAL);
OB_SERIALIZE_MEMBER(ObJoinFilterShareInfo,
unfinished_count_ptr_,
ch_provider_ptr_,
release_ref_ptr_,
filter_ptr_,
shared_msgs_,
ser_shared_jf_constructor_);
OB_SERIALIZE_MEMBER(ObJoinFilterRuntimeConfig,
bloom_filter_ratio_,
each_group_size_,
bf_piece_size_,
runtime_filter_wait_time_ms_,
runtime_filter_max_in_num_,
runtime_bloom_filter_max_size_,
px_message_compression_,
build_send_opt_);
OB_SERIALIZE_MEMBER(ObRuntimeFilterInfo,
filter_expr_id_,
p2p_datahub_id_,
filter_shared_type_,
dh_msg_type_);
OB_SERIALIZE_MEMBER((ObJoinFilterSpec, ObOpSpec),
mode_,
filter_id_,
filter_len_,
join_keys_,
hash_funcs_,
cmp_funcs_,
filter_shared_type_,
calc_tablet_id_expr_,
rf_infos_,
need_null_cmp_flags_,
is_shuffle_,
each_group_size_,
rf_build_cmp_infos_,
rf_probe_cmp_infos_,
px_query_range_info_,
bloom_filter_ratio_,
send_bloom_filter_size_,
jf_material_control_info_,
join_type_,
full_hash_join_keys_,
hash_join_is_ns_equal_cond_,
rf_max_wait_time_ms_, // FARM COMPAT WHITELIST
use_ndv_runtime_bloom_filter_size_ // FARM COMPAT WHITELIST
);
OB_SERIALIZE_MEMBER(ObJoinFilterOpInput,
share_info_,
bf_idx_at_sqc_proxy_,
px_sequence_id_,
config_);
int SharedJoinFilterConstructor::init()
{
return cond_.init(common::ObWaitEventIds::DEFAULT_COND_WAIT);
}
int SharedJoinFilterConstructor::reset_for_rescan()
{
// Generally speaking, shared join filter is not supported to rescan, this code
// is only for defense.
int ret = OB_ERR_UNEXPECTED;
if (try_release_constructor()) {
is_bloom_filter_constructed_ = false;
}
LOG_ERROR("This may be a unexpected way");
return ret;
}
int SharedJoinFilterConstructor::wait_constructed(ObOperator *join_filter_op,
ObRFBloomFilterMsg *bf_msg)
{
int ret = OB_SUCCESS;
int64_t loop_time = 0;
while (OB_SUCC(ret)) {
{
ObThreadCondGuard guard(cond_);
if (is_bloom_filter_constructed_) {
break;
}
// wait for timeout or until notified.
cond_.wait_us(COND_WAIT_TIME_USEC);
if (is_bloom_filter_constructed_) {
break;
}
}
++loop_time;
if (OB_FAIL(join_filter_op->try_check_status())) {
LOG_WARN("failed to check status", K(loop_time));
}
}
if (OB_FAIL(ret)) {
} else if (!bf_msg->bloom_filter_.is_inited()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("bloom filter initialized failed, please check the leader worker's error report");
}
return ret;
}
int SharedJoinFilterConstructor::notify_constructed()
{
int ret = OB_SUCCESS;
{
ObThreadCondGuard guard(cond_);
is_bloom_filter_constructed_ = true;
}
if (OB_FAIL(cond_.broadcast())) {
LOG_WARN("failed to broadcast");
}
return ret;
}
int ObJoinFilterOpInput::init(ObTaskInfo &task_info)
{
int ret = OB_SUCCESS;
UNUSED(task_info);
//new parallel framework do not use this interface to set parameters
ret = OB_ERR_UNEXPECTED;
LOG_WARN("the interface should not be used", K(ret));
return ret;
}
bool ObJoinFilterOpInput::check_release()
{
bool res = false;
uint64_t *release_ref_ptr = reinterpret_cast<uint64_t *>(share_info_.release_ref_ptr_);
if (OB_ISNULL(release_ref_ptr)) {
res = false;
} else if (0 == ATOMIC_AAF(release_ref_ptr, -1)) {
res = true;
} else {
res = false;
}
return res;
}
ERRSIM_POINT_DEF(JF_BS_OPT);
int ObJoinFilterOpInput::load_runtime_config(const ObJoinFilterSpec &spec, ObExecContext &ctx)
{
int ret = OB_SUCCESS;
if (0 == spec.bloom_filter_ratio_ && 0 == spec.send_bloom_filter_size_) {
// bloom_filter_ratio_ and send_bloom_filter_size_ are default value, which indicates the
// cluster is upgrading. for compatibility, use the value from GCONF
config_.bloom_filter_ratio_ = ((double)GCONF._bloom_filter_ratio / 100.0);
config_.bf_piece_size_ = GCONF._send_bloom_filter_size;
} else {
// bf_piece_size_ means how many int64_t a piece bloom filter contains
// we expect to split bloom filter into k pieces with 1MB = 2^20B
// so a piece bloom filter should contain
// 1024(send_bloom_filter_size_) * 128 = 131,072 int64_t, i.e. 1MB
config_.bloom_filter_ratio_ = ((double)spec.bloom_filter_ratio_ / 100.0);
config_.bf_piece_size_ = spec.send_bloom_filter_size_ * 128;
}
config_.each_group_size_ = spec.each_group_size_;
int64_t sess_wait_time_ms = ctx.get_my_session()->get_runtime_filter_wait_time_ms();
if (sess_wait_time_ms != 0) {
config_.runtime_filter_wait_time_ms_ = sess_wait_time_ms;
} else {
// use adaptive max wait time if session variable is 0
int64_t sqc_count = ctx.get_sqc_handler()->get_sqc_init_arg().sqc_.get_sqc_count();
config_.runtime_filter_wait_time_ms_ = spec.rf_max_wait_time_ms_ / sqc_count;
}
config_.runtime_filter_max_in_num_ = ctx.get_my_session()->
get_runtime_filter_max_in_num();
config_.runtime_bloom_filter_max_size_ = ctx.get_my_session()->
get_runtime_bloom_filter_max_size();
config_.px_message_compression_ = true;
config_.build_send_opt_ = (spec.use_realistic_runtime_bloom_filter_size() && JF_BS_OPT == OB_SUCCESS);
LOG_TRACE("load runtime filter config", K(spec.get_id()), K(config_));
return ret;
}
int ObJoinFilterOpInput::init_share_info(
const ObJoinFilterSpec &spec,
ObExecContext &ctx,
int64_t task_count,
int64_t sqc_count)
{
int ret = OB_SUCCESS;
uint64_t *ptr = NULL;
void *constructor_buf = nullptr;
if (spec.is_shuffle_ && spec.jf_material_control_info_.each_sqc_has_full_data_
&& config_.build_send_opt_) {
// for shuffled join filter, we gather K piece bloom filter create by all K sqcs.
// opt: if is shared hash join, each sqc has full data of build table, we only
// need to gather one piece join filter
sqc_count = 1;
}
common::ObIAllocator &allocator = ctx.get_allocator();
if (OB_ISNULL(ptr = (uint64_t *)allocator.alloc(sizeof(uint64_t) * 2))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("fail to alloc ptr", K(ret));
} else if (OB_ISNULL(constructor_buf = allocator.alloc(sizeof(SharedJoinFilterConstructor)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("fail to alloc bool ptr", K(ret));
} else if (OB_FAIL(init_shared_msgs(spec, ctx, sqc_count))) {
LOG_WARN("fail to init shared msgs", K(ret));
} else {
ptr[0] = task_count;
ptr[1] = task_count;
share_info_.release_ref_ptr_ = reinterpret_cast<uint64_t>(&ptr[0]);
share_info_.unfinished_count_ptr_ = reinterpret_cast<uint64_t>(&ptr[1]);
share_info_.shared_jf_constructor_ = new(constructor_buf) SharedJoinFilterConstructor;
if (OB_FAIL(share_info_.shared_jf_constructor_->init())) {
LOG_WARN("failed to init shared_jf_constructor_");
}
}
return ret;
}
int ObJoinFilterOpInput::init_shared_msgs(
const ObJoinFilterSpec &spec,
ObExecContext &ctx,
int64_t sqc_count)
{
int ret = OB_SUCCESS;
const int64_t tenant_id = ctx.get_my_session()->get_effective_tenant_id();
const int64_t timeout_ts = GET_PHY_PLAN_CTX(ctx)->get_timeout_timestamp();
common::ObIAllocator &allocator = ctx.get_allocator();
ObArray<ObP2PDatahubMsgBase *> *array_ptr = nullptr;
ObPxSQCProxy *sqc_proxy = reinterpret_cast<ObPxSQCProxy *>(share_info_.ch_provider_ptr_);
void *ptr = nullptr;
if (OB_ISNULL(ptr = allocator.alloc(sizeof(ObArray<ObP2PDatahubMsgBase *>)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("fail to allocate memory", K(ret));
} else {
array_ptr = new(ptr) ObArray<ObP2PDatahubMsgBase *>();
array_ptr->set_attr(ObMemAttr(tenant_id, "JFArray"));
ObP2PDatahubMsgBase *msg_ptr = nullptr;
for (int i = 0; i < spec.rf_infos_.count() && OB_SUCC(ret); ++i) {
msg_ptr = nullptr;
bool construct_bloom_filter_later = spec.use_realistic_runtime_bloom_filter_size()
&& spec.rf_infos_.at(i).dh_msg_type_ == ObP2PDatahubMsgBase::BLOOM_FILTER_VEC_MSG;
if (OB_FAIL(PX_P2P_DH.alloc_msg(allocator, spec.rf_infos_.at(i).dh_msg_type_, msg_ptr))) {
LOG_WARN("fail to alloc msg", K(ret));
} else if (OB_FAIL(array_ptr->push_back(msg_ptr))) {
// push_back failed, must destroy the msg immediately
// if init or construct_msg_details failed, destroy msg after the for loop
msg_ptr->destroy();
allocator.free(msg_ptr);
LOG_WARN("fail to push back array ptr", K(ret));
} else if (OB_FAIL(msg_ptr->init(spec.rf_infos_.at(i).p2p_datahub_id_,
px_sequence_id_, 0/*task_id*/, tenant_id, timeout_ts))) {
LOG_WARN("fail to init msg", K(ret));
} else if (!construct_bloom_filter_later
&& OB_FAIL(construct_msg_details(spec, sqc_proxy, config_, *msg_ptr, sqc_count,
spec.filter_len_))) {
LOG_WARN("fail to construct msg details", K(ret), K(tenant_id));
}
}
}
if (OB_SUCC(ret) && OB_NOT_NULL(array_ptr)) {
share_info_.shared_msgs_ = reinterpret_cast<uint64_t>(array_ptr);
}
if (OB_FAIL(ret) && OB_NOT_NULL(array_ptr)) {
// if failed, destroy all msgs
for (int64_t i = 0; i < array_ptr->count(); ++i) {
if (OB_NOT_NULL(array_ptr->at(i))) {
array_ptr->at(i)->destroy();
allocator.free(array_ptr->at(i));
array_ptr->at(i) = nullptr;
}
}
array_ptr->reset();
allocator.free(array_ptr);
array_ptr = nullptr;
}
return ret;
}
int ObJoinFilterOpInput::construct_msg_details(
const ObJoinFilterSpec &spec,
ObPxSQCProxy *sqc_proxy,
ObJoinFilterRuntimeConfig &config,
ObP2PDatahubMsgBase &msg,
int64_t sqc_count,
int64_t estimated_rows)
{
int ret = OB_SUCCESS;
switch(msg.get_msg_type()) {
case ObP2PDatahubMsgBase::BLOOM_FILTER_VEC_MSG: {
ObRFBloomFilterMsg &bf_msg = static_cast<ObRFBloomFilterMsg &>(msg);
bf_msg.set_use_rich_format(true);
if (spec.use_realistic_runtime_bloom_filter_size()) {
bf_msg.set_use_hash_join_seed(true);
}
}
case ObP2PDatahubMsgBase::BLOOM_FILTER_MSG: {
ObSArray<ObAddr> *target_addrs = nullptr;
ObRFBloomFilterMsg &bf_msg = static_cast<ObRFBloomFilterMsg &>(msg);
ObPxSQCProxy::SQCP2PDhMap &dh_map = sqc_proxy->get_p2p_dh_map();
if (OB_FAIL(bf_msg.bloom_filter_.init(estimated_rows,
bf_msg.get_allocator(),
bf_msg.get_tenant_id(),
config.bloom_filter_ratio_,
config.runtime_bloom_filter_max_size_))) {
LOG_WARN("failed to init bloom filter", K(ret));
} else if (!spec.is_shared_join_filter() || !spec.is_shuffle_) {
bf_msg.set_msg_expect_cnt(1);
bf_msg.set_msg_cur_cnt(1);
} else if (OB_FAIL(dh_map.get_refactored(bf_msg.get_p2p_datahub_id(), target_addrs))) {
LOG_WARN("fail to get dh map", K(ret));
} else if (target_addrs->count() == 1 &&
GCTX.self_addr() == target_addrs->at(0) &&
sqc_count == 1) {
bf_msg.set_msg_expect_cnt(1);
bf_msg.set_msg_cur_cnt(1);
} else {
int64_t target_cnt = target_addrs->count();
int64_t each_group_size = (OB_INVALID_ID == config.each_group_size_) ?
sqrt(target_cnt) : config.each_group_size_;
if (OB_FAIL(bf_msg.generate_filter_indexes(each_group_size, target_cnt, config.bf_piece_size_))) {
LOG_WARN("fail to generate filter indexes", K(ret));
} else {
bf_msg.filter_idx_ = 0;
bf_msg.create_finish_ = false;
int64_t piece_cnt = ceil(bf_msg.bloom_filter_.get_bits_array_length() /
(double)config.bf_piece_size_);
bf_msg.set_msg_expect_cnt(sqc_count * piece_cnt);
bf_msg.set_msg_cur_cnt(1);
bf_msg.expect_first_phase_count_ = sqc_count;
bf_msg.piece_size_ = config.bf_piece_size_;
}
}
break;
}
case ObP2PDatahubMsgBase::RANGE_FILTER_MSG: {
ObRFRangeFilterMsg &range_msg = static_cast<ObRFRangeFilterMsg &>(msg);
int64_t col_cnt = spec.join_keys_.count();
if (OB_FAIL(range_msg.lower_bounds_.prepare_allocate(col_cnt))) {
LOG_WARN("fail to prepare allocate col cnt", K(ret));
} else if (OB_FAIL(range_msg.upper_bounds_.prepare_allocate(col_cnt))) {
LOG_WARN("fail to prepare allocate col cnt", K(ret));
} else if (OB_FAIL(range_msg.cells_size_.prepare_allocate(col_cnt))) {
LOG_WARN("fail to prepare allocate col cnt", K(ret));
} else if (OB_FAIL(range_msg.cmp_funcs_.assign(spec.cmp_funcs_))) {
LOG_WARN("fail to init cmp funcs", K(ret));
} else if (OB_FAIL(range_msg.need_null_cmp_flags_.assign(spec.need_null_cmp_flags_))) {
LOG_WARN("fail to init cmp flags", K(ret));
} else if (OB_FAIL(range_msg.build_obj_metas_.init(col_cnt))) {
LOG_WARN("fail to prepare init build obj_metas", K(ret));
} else {
range_msg.set_msg_expect_cnt(sqc_count);
range_msg.set_msg_cur_cnt(1);
if (spec.px_query_range_info_.can_extract()) {
if (OB_FAIL(range_msg.init_query_range_info(spec.px_query_range_info_))) {
LOG_WARN("failed to init_query_range_info");
}
}
}
for (int64_t i = 0; OB_SUCC(ret) && i < col_cnt; ++i) {
if (OB_FAIL(range_msg.build_obj_metas_.push_back(spec.join_keys_.at(i)->obj_meta_))) {
LOG_WARN("fail to push_back build obj_metas");
}
}
break;
}
case ObP2PDatahubMsgBase::RANGE_FILTER_VEC_MSG: {
ObRFRangeFilterVecMsg &range_msg = static_cast<ObRFRangeFilterVecMsg &>(msg);
int64_t col_cnt = spec.join_keys_.count();
if (OB_FAIL(range_msg.lower_bounds_.prepare_allocate(col_cnt))) {
LOG_WARN("fail to prepare allocate col cnt", K(ret));
} else if (OB_FAIL(range_msg.upper_bounds_.prepare_allocate(col_cnt))) {
LOG_WARN("fail to prepare allocate col cnt", K(ret));
} else if (OB_FAIL(range_msg.cells_size_.prepare_allocate(col_cnt))) {
LOG_WARN("fail to prepare allocate col cnt", K(ret));
} else if (OB_FAIL(range_msg.need_null_cmp_flags_.assign(spec.need_null_cmp_flags_))) {
LOG_WARN("fail to init cmp flags", K(ret));
} else if (OB_FAIL(range_msg.build_row_cmp_info_.assign(spec.rf_build_cmp_infos_))) {
LOG_WARN("fail to prepare allocate build_row_cmp_info_", K(ret));
} else if (OB_FAIL(range_msg.probe_row_cmp_info_.assign(spec.rf_probe_cmp_infos_))) {
LOG_WARN("fail to prepare allocate probe_row_cmp_info_", K(ret));
} else {
range_msg.set_msg_expect_cnt(sqc_count);
range_msg.set_msg_cur_cnt(1);
if (spec.px_query_range_info_.can_extract()) {
if (OB_FAIL(range_msg.init_query_range_info(spec.px_query_range_info_))) {
LOG_WARN("failed to init_query_range_info");
}
}
}
break;
}
case ObP2PDatahubMsgBase::IN_FILTER_MSG: {
ObRFInFilterMsg &in_msg = static_cast<ObRFInFilterMsg &>(msg);
int64_t col_cnt = spec.join_keys_.count();
if (OB_FAIL(in_msg.rows_set_.create(config.runtime_filter_max_in_num_ * 2,
"RFInFilter",
"RFInFilter"))) {
LOG_WARN("fail to init in hash set", K(ret));
} else if (OB_FAIL(in_msg.cur_row_.prepare_allocate(col_cnt))) {
LOG_WARN("fail to prepare allocate col cnt", K(ret));
} else if (OB_FAIL(in_msg.cmp_funcs_.assign(spec.cmp_funcs_))) {
LOG_WARN("fail to init cmp funcs", K(ret));
} else if (OB_FAIL(in_msg.hash_funcs_for_insert_.assign(spec.hash_funcs_))) {
LOG_WARN("fail to init cmp funcs", K(ret));
} else if (OB_FAIL(in_msg.need_null_cmp_flags_.assign(spec.need_null_cmp_flags_))) {
LOG_WARN("fail to init cmp flags", K(ret));
} else if (OB_FAIL(in_msg.build_obj_metas_.init(col_cnt))) {
LOG_WARN("fail to prepare init build obj_metas_", K(ret));
} else {
in_msg.set_msg_expect_cnt(sqc_count);
in_msg.set_msg_cur_cnt(1);
in_msg.col_cnt_ = col_cnt;
in_msg.max_in_num_ = config.runtime_filter_max_in_num_;
if (spec.px_query_range_info_.can_extract()) {
if (OB_FAIL(in_msg.init_query_range_info(spec.px_query_range_info_))) {
LOG_WARN("failed to init_query_range_info");
}
}
}
for (int64_t i = 0; OB_SUCC(ret) && i < col_cnt; ++i) {
if (OB_FAIL(in_msg.build_obj_metas_.push_back(spec.join_keys_.at(i)->obj_meta_))) {
LOG_WARN("fail to push_back build obj_metas_");
}
}
break;
}
case ObP2PDatahubMsgBase::IN_FILTER_VEC_MSG: {
ObRFInFilterVecMsg &in_msg = static_cast<ObRFInFilterVecMsg &>(msg);
if (spec.use_realistic_runtime_bloom_filter_size()) {
in_msg.set_use_hash_join_seed(true);
}
int64_t col_cnt = spec.join_keys_.count();
if (OB_FAIL(in_msg.rows_set_.create(config.runtime_filter_max_in_num_ * 2,
"RFInFilter",
"RFInFilter"))) {
LOG_WARN("fail to init in hash set", K(ret));
} else if (OB_FAIL(in_msg.sm_hash_set_.init(config.runtime_filter_max_in_num_,
in_msg.get_tenant_id()))) {
LOG_WARN("failed to init sm_hash_set_", K(config.runtime_filter_max_in_num_));
} else if (OB_FAIL(in_msg.need_null_cmp_flags_.assign(spec.need_null_cmp_flags_))) {
LOG_WARN("fail to init cmp flags", K(ret));
} else if (OB_FAIL(in_msg.build_row_cmp_info_.assign(spec.rf_build_cmp_infos_))) {
LOG_WARN("fail to prepare allocate build_row_cmp_info_", K(ret));
} else if (OB_FAIL(in_msg.probe_row_cmp_info_.assign(spec.rf_probe_cmp_infos_))) {
LOG_WARN("fail to prepare allocate probe_row_cmp_info_", K(ret));
} else if (OB_FAIL(in_msg.build_row_meta_.init(spec.join_keys_, sizeof(uint64_t)))) {
LOG_WARN("failed to init row meta");
} else if (OB_FAIL(in_msg.cur_row_with_hash_.row_.prepare_allocate(col_cnt))) {
LOG_WARN("failed to prepare_allocate cur_row_with_hash_");
} else {
in_msg.build_send_opt_ = config.build_send_opt_;
in_msg.set_msg_expect_cnt(sqc_count);
in_msg.set_msg_cur_cnt(1);
in_msg.max_in_num_ = config.runtime_filter_max_in_num_;
if (spec.px_query_range_info_.can_extract()) {
if (OB_FAIL(in_msg.init_query_range_info(spec.px_query_range_info_))) {
LOG_WARN("failed to init_query_range_info");
} else if (OB_FAIL(in_msg.hash_funcs_for_insert_.assign(spec.hash_funcs_))) {
LOG_WARN("fail to init cmp funcs", K(ret));
}
}
}
break;
}
default: {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected msg type", K(ret));
}
}
return ret;
}
ObJoinFilterSpec::ObJoinFilterSpec(common::ObIAllocator &alloc, const ObPhyOperatorType type)
: ObOpSpec(alloc, type),
mode_(NOT_INIT),
filter_id_(OB_INVALID_ID),
filter_len_(0),
join_keys_(alloc),
hash_funcs_(alloc),
cmp_funcs_(alloc),
filter_shared_type_(JoinFilterSharedType::INVALID_TYPE),
calc_tablet_id_expr_(NULL),
rf_infos_(alloc),
need_null_cmp_flags_(alloc),
is_shuffle_(false),
each_group_size_(OB_INVALID_ID),
rf_build_cmp_infos_(alloc),
rf_probe_cmp_infos_(alloc),
px_query_range_info_(alloc),
bloom_filter_ratio_(0),
send_bloom_filter_size_(0),
full_hash_join_keys_(alloc),
hash_join_is_ns_equal_cond_(alloc)
{
}
int ObJoinFilterSpec::register_to_datahub(ObExecContext &ctx) const
{
int ret = OB_SUCCESS;
// if this is a material controller, and at least one join filter is shared join filter, we need
// to send datahub msg to synchronize row count
if (!need_sync_row_count()) {
} else if (OB_ISNULL(ctx.get_sqc_handler())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("null unexpected", K(ret));
} else {
void *buf = ctx.get_allocator().alloc(sizeof(ObJoinFilterCountRowWholeMsg::WholeMsgProvider));
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
} else {
ObJoinFilterCountRowWholeMsg::WholeMsgProvider *provider =
new (buf) ObJoinFilterCountRowWholeMsg::WholeMsgProvider();
ObSqcCtx &sqc_ctx = ctx.get_sqc_handler()->get_sqc_ctx();
if (OB_FAIL(sqc_ctx.add_whole_msg_provider(get_id(), DH_JOIN_FILTER_COUNT_ROW_WHOLE_MSG,
*provider))) {
LOG_WARN("fail add whole msg provider", K(ret));
}
}
}
return ret;
}
/*
For upgrade compatibility,
if cluster version >= 435, we can directly use flag need_sync_row_count_,
if it is during upgrade and cluster version < 435, the flag need_sync_row_count_ will be false,
we should visit child to check whether need to synchronize row count
*/
int ObJoinFilterSpec::update_sync_row_count_flag()
{
int ret = OB_SUCCESS;
const ObOpSpec *cur_spec = this;
int64_t join_filter_count = under_control_join_filter_count();
// if at least one join filter is shared join filter, we need to send datahub msg to synchronize
// row count
for (int64_t i = 0; i < join_filter_count && OB_NOT_NULL(cur_spec) && OB_SUCC(ret); ++i) {
if (cur_spec->get_type() != PHY_JOIN_FILTER) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("must be join filter bellow");
} else {
const ObJoinFilterSpec &spec = static_cast<const ObJoinFilterSpec &>(*cur_spec);
if (spec.is_shared_join_filter()) {
jf_material_control_info_.need_sync_row_count_ = true;
break;
}
cur_spec = cur_spec->get_child();
}
}
return ret;
}
//------------------------------------------ ObJoinFilterOp --------------------------------
bool ObJoinFilterOp::is_valid()
{
return child_ != NULL && MY_SPEC.mode_ != JoinFilterMode::NOT_INIT &&
MY_SPEC.filter_shared_type_ != JoinFilterSharedType::INVALID_TYPE;
}
ObJoinFilterOp::ObJoinFilterOp(ObExecContext &exec_ctx, const ObOpSpec &spec, ObOpInput *input)
: ObOperator(exec_ctx, spec, input), filter_create_msg_(nullptr), join_filter_hash_values_(NULL),
lucky_devil_champions_(), profile_(ObSqlWorkAreaType::HASH_WORK_AREA),
sql_mem_processor_(profile_, op_monitor_info_)
{
}
ObJoinFilterOp::~ObJoinFilterOp()
{
destroy();
}
int ObJoinFilterOp::inner_open()
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!is_valid())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("builder operator is invalid", K(ret), K(MY_SPEC.filter_shared_type_), K(MY_SPEC.mode_));
} else if (MY_SPEC.is_create_mode() && OB_FAIL(open_join_filter_create())) {
LOG_WARN("fail to open join filter create op", K(ret));
} else if ((MY_SPEC.is_use_mode() && OB_FAIL(open_join_filter_use()))) {
LOG_WARN("fail to open join filter use op", K(ret));
}
return ret;
}
int ObJoinFilterOp::do_create_filter_rescan()
{
int ret = OB_SUCCESS;
ObRFBloomFilterMsg *bf_msg = nullptr;
for (int i = 0; i < local_rf_msgs_.count() && OB_SUCC(ret); ++i) {
if (OB_FAIL(local_rf_msgs_.at(i)->reuse())) {
LOG_WARN("fail to reuse local rf msgs", K(ret));
}
}
if (MY_SPEC.use_realistic_runtime_bloom_filter_size() && OB_NOT_NULL(bf_vec_msg_)) {
// in new fashion, the bloom filter will be reinited
bf_vec_msg_->bloom_filter_.reset_for_rescan();
}
if (MY_SPEC.is_material_controller()) {
partition_splitter_->reuse_for_rescan();
if (MY_SPEC.is_shared_join_filter()) {
static_cast<ObJoinFilterOpInput *>(input_)
->share_info_.shared_jf_constructor_->reset_for_rescan();
}
}
has_sent_runtime_filter_ = false;
return ret;
}
int ObJoinFilterOp::do_use_filter_rescan()
{
int ret = OB_SUCCESS;
for (int i = 0; i < MY_SPEC.rf_infos_.count() && OB_SUCC(ret); ++i) {
if (OB_INVALID_ID != MY_SPEC.rf_infos_.at(i).filter_expr_id_) {
ObExprJoinFilter::ObExprJoinFilterContext *join_filter_ctx = nullptr;
if (OB_NOT_NULL(join_filter_ctx = static_cast<ObExprJoinFilter::ObExprJoinFilterContext *>(
ctx_.get_expr_op_ctx(MY_SPEC.rf_infos_.at(i).filter_expr_id_)))) {
join_filter_ctx->rescan();
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("join filter ctx is unexpected", K(ret));
}
}
}
return ret;
}
int ObJoinFilterOp::inner_rescan()
{
int ret = OB_SUCCESS;
if (MY_SPEC.is_create_mode() && OB_FAIL(do_create_filter_rescan())) {
LOG_WARN("fail to do create filter rescan", K(ret));
} else if (MY_SPEC.is_use_mode() && OB_FAIL(do_use_filter_rescan())) {
LOG_WARN("fail to do use filter rescan", K(ret));
} else if (OB_FAIL(ObOperator::inner_rescan())) {
LOG_WARN("operator rescan failed", K(ret));
}
return ret;
}
// for use mode, update_plan_monitor_info cause get_next_batch may not get iter end
int ObJoinFilterOp::inner_drain_exch()
{
int ret = OB_SUCCESS;
if (row_reach_end_ || batch_reach_end_) {
// update_plan_monitor_info already done in get_next_row/batch iter end
} else if (MY_SPEC.is_use_mode()) {
ret = update_plan_monitor_info();
}
return ret;
}
/*
Join filter create op may enter do_drain_exch without get_next_row reach iter end, thus runtime
filter is not merge & send, so we do these jobs in drain process.
*/
int ObJoinFilterOp::do_drain_exch()
{
int ret = OB_SUCCESS;
if (OB_FAIL(ObOperator::do_drain_exch())) {
LOG_WARN("failed to basic do_drain_exch");
} else if (MY_SPEC.is_material_controller()) {
// if one worker is drained without send_datahub_count_row_msg, other workers who has
// send_datahub_count_row_msg will never break wait, so must send one piece
/* we must do send_datahub_count_row_msg after child drained, or we may trap into endless loop
Good Way Bad Way
HashJoin HashJoin
| |
|---- |----
JoinFilter 3. after drain, send datahub row count msg JoinFilter 1. drain exch, send datahub row count msg, trap into endless loop
| |
Receive 1. drain exch, send channel ready msg Receive 2. wait for parent drain, can't send channel ready msg
| |
Transmit 2. wait channel succ, send rows Transmit 3. wait channel failed
*/
if (OB_LIKELY(has_sent_runtime_filter_)) {
} else if (OB_FAIL(build_and_broadcast_runtime_filter())) {
LOG_WARN("failed to build and broadcast runtime filter");
}
} else if (MY_SPEC.is_create_mode() && !MY_SPEC.use_realistic_runtime_bloom_filter_size()) {
if (OB_LIKELY(has_sent_runtime_filter_)) {
} else if (OB_FAIL(try_merge_join_filter())) {
LOG_WARN("fail to merge join filter");
} else if (OB_FAIL(try_send_join_filter())) {
LOG_WARN("fail to send bloom filter to use filter");
} else {
has_sent_runtime_filter_ = true;
}
}
return ret;
}
int ObJoinFilterOp::inner_get_next_row()
{
int ret = OB_SUCCESS;
ObJoinFilterOpInput *filter_input = static_cast<ObJoinFilterOpInput*>(input_);
while (OB_SUCC(ret)) {
clear_evaluated_flag();
ret = child_->get_next_row();
if (OB_ITER_END == ret) {
if (MY_SPEC.is_create_mode()) {
ret = OB_SUCCESS;
if (OB_FAIL(try_merge_join_filter())) {
LOG_WARN("fail to merge join filter", K(ret));
} else if (OB_FAIL(try_send_join_filter())) {
LOG_WARN("fail to send bloom filter to use filter", K(ret));
}
if (OB_SUCC(ret)) {
ret = OB_ITER_END;
has_sent_runtime_filter_ = true;
}
}
} else if (OB_SUCCESS != ret) {
LOG_WARN("fail to get next row", K(ret));
}
if (OB_SUCC(ret)) {
if (MY_SPEC.is_create_mode()) {
if (OB_FAIL(insert_by_row())) {
LOG_WARN("fail to insert bf", K(ret));
} else {
break;
}
} else if (MY_SPEC.is_use_mode()) {
/*handed to expression calculation, operator does not need to handle related calculation logic*/
break;
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("the mode of px bloom filter is unexpected", K(MY_SPEC.mode_), K(ret));
}
}
}
if (MY_SPEC.is_use_mode() && ret == OB_ITER_END) {
if (OB_FAIL(update_plan_monitor_info())) {
LOG_WARN("fail to update plan monitor info", K(ret));
} else {
ret = OB_ITER_END;
}
}
return ret;
}
int ObJoinFilterOp::inner_get_next_batch(const int64_t max_row_cnt)
{
int ret = OB_SUCCESS;
int64_t batch_cnt = min(max_row_cnt, MY_SPEC.max_batch_size_);
if (MY_SPEC.is_create_mode()) {
if (MY_SPEC.use_realistic_runtime_bloom_filter_size()) {
// in this way, join filter will return brs_.end_ && 0 == brs_.size_ to parent operator
if (MY_SPEC.is_material_controller()) {
// the controller is responsible for do material
ret = join_filter_create_do_material(batch_cnt);
} else {
// the below join filter only bypass all
ret = join_filter_create_bypass_all(batch_cnt);
}
} else {
ret = join_filter_create_get_next_batch(batch_cnt);
}
} else if (MY_SPEC.is_use_mode()) {
ret = join_filter_use_get_next_batch(batch_cnt);
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("the mode of join filter is unexpected", K(MY_SPEC.mode_), K(ret));
}
return ret;
}
// for these function, the hash join operator guarantees it will never call get_next_batch
// if join filter create operator iterators end unless in rescan scene
int ObJoinFilterOp::join_filter_create_do_material(const int64_t max_row_cnt)
{
int ret = OB_SUCCESS;
const ObBatchRows *child_brs = nullptr;
while (OB_SUCC(ret)) {
clear_evaluated_flag();
if (OB_FAIL(child_->get_next_batch(max_row_cnt, child_brs))) {
// get_next_batch will not return OB_ITER_END
LOG_WARN("failed to get next batch for create op", K(ret));
} else if (FALSE_IT(brs_.copy(child_brs))) {
} else if (brs_.size_ > 0) {
if (!got_first_row_) {
op_monitor_info_.first_row_time_ = ObClockGenerator::getClock();
got_first_row_ = true;
}
/*
For each batch:
1. calc partition hash value(for hash join)
2. group fill range filter(for all join filter, range filter can directly filled without
material)
3. group calc join filter hash value(for all join filter, if a join filter reuse hash join's
hash value, skip it)
4. group fill in filter(for all join filter)
5. add batch into store
6. process dump
*/
for (int64_t i = 0; i < build_rows_output_->count() && OB_SUCC(ret); ++i) {
// if join filter's output is not same with child, it is necessary to eval output exprs
if (OB_FAIL(build_rows_output_->at(i)->eval_vector(eval_ctx_, brs_))) {
LOG_WARN("failed to eval vector");
}
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(partition_splitter_->calc_partition_hash_value(
&brs_, eval_ctx_, hash_join_hash_values_,
use_hllc_estimate_ndv(),
group_controller_->hash_join_keys_hllc_, skip_left_null_,
&MY_SPEC.hash_join_is_ns_equal_cond_))) {
LOG_WARN("failed to calc_partition_hash_value");
} else if (OB_FAIL(group_controller_->apply(group_fill_range_filter, brs_))) {
LOG_WARN("failed to group fill range filter");
} else if (OB_FAIL(group_controller_->apply(group_calc_join_filter_hash_values, brs_))) {
LOG_WARN("failed to group calculate join filter hash values");
} else if (OB_FAIL(
group_controller_->apply(group_fill_in_filter, brs_, hash_join_hash_values_))) {
} else if (OB_FAIL(partition_splitter_->add_batch(*group_controller_, hash_join_hash_values_,
brs_,
ObHashJoinVecOp::MAX_PART_LEVEL << 3))) {
} else if (OB_FAIL(process_dump())) {
LOG_WARN("failed to process dump");
}
} else if (brs_.end_ && 0 == brs_.size_) {
break;
}
}
if (OB_SUCC(ret) && brs_.end_ && 0 == brs_.size_) {
if (OB_FAIL(build_and_broadcast_runtime_filter())) {
LOG_WARN("failed to build and broadcast runtime filter");
}
}
// should return B_SUCCESS, brs_.end_ && 0 == brs_.size_ to parent operator
return ret;
}
int ObJoinFilterOp::build_and_broadcast_runtime_filter()
{
int ret = OB_SUCCESS;
// send piece datahub msg, and wait the whole msg, then the total row is get
int64_t worker_row_count = partition_splitter_->get_total_row_count();
int64_t total_row_count = 0;
if (force_dump_ && OB_FAIL(partition_splitter_->force_dump_all_partition())) {
LOG_WARN("failed to force dump all partition");
} else if (OB_FAIL(partition_splitter_->finish_add_row())) {
LOG_WARN("failed to finish add row");
} else if (OB_FAIL(get_exec_row_count_and_ndv(worker_row_count, total_row_count))) {
LOG_WARN("failed to get exec row count");
} else if (OB_FAIL(group_controller_->apply(group_init_bloom_filter, worker_row_count,
total_row_count))) {
LOG_WARN("failed to group_init_bloom_filter");
} else if (OB_FAIL(fill_bloom_filter())) { // group fill bloom filter inner
LOG_WARN("failed to fill bloom filter");
} else if (OB_FAIL(group_controller_->apply(group_merge_and_send_join_filter))) {
LOG_WARN("failed to group control merge_and_send_join_filter");
} else {
has_sent_runtime_filter_ = true;
// for the controller, it need to add row info manually
op_monitor_info_.output_row_count_ += partition_splitter_->get_total_row_count();
}
return ret;
}
int ObJoinFilterOp::join_filter_create_bypass_all(const int64_t max_row_cnt)
{
int ret = OB_SUCCESS;
const ObBatchRows *child_brs = nullptr;
clear_evaluated_flag();
if (OB_FAIL(child_->get_next_batch(max_row_cnt, child_brs))) {
// get_next_batch will not return OB_ITER_END
LOG_WARN("failed to get next batch for use op", K(ret));
} else if (FALSE_IT(brs_.copy(child_brs))) {
}
return ret;
}
int ObJoinFilterOp::join_filter_create_get_next_batch(const int64_t max_row_cnt)
{
int ret = OB_SUCCESS;
const ObBatchRows *child_brs = nullptr;
clear_evaluated_flag();
if (OB_FAIL(child_->get_next_batch(max_row_cnt, child_brs))) {
// get_next_batch will not return OB_ITER_END
LOG_WARN("failed to get next batch for use op", K(ret));
} else if (FALSE_IT(brs_.copy(child_brs))) {
} else if (brs_.size_ > 0) {
if (OB_FAIL(insert_by_row_batch(child_brs))) {
LOG_WARN("fail to insert join filter", K(ret));
}
}
if (OB_SUCC(ret) && brs_.end_ && 0 == brs_.size_) {
if (OB_FAIL(try_merge_join_filter())) {
LOG_WARN("fail to merge join filter", K(ret));
} else if (OB_FAIL(try_send_join_filter())) {
LOG_WARN("fail to send bloom filter to use filter", K(ret));
} else {
has_sent_runtime_filter_ = true;
}
}
return ret;
}
int ObJoinFilterOp::join_filter_use_get_next_batch(const int64_t max_row_cnt)
{
int ret = OB_SUCCESS;
const ObBatchRows *child_brs = nullptr;
clear_evaluated_flag();
if (OB_FAIL(child_->get_next_batch(max_row_cnt, child_brs))) {
// get_next_batch will not return OB_ITER_END
LOG_WARN("failed to get next batch for use op", K(ret));
} else if (FALSE_IT(brs_.copy(child_brs))) {
} else if (brs_.end_ && 0 == brs_.size_) {
if (OB_FAIL(update_plan_monitor_info())) {
LOG_WARN("fail to update plan monitor info", K(ret));
}
}
return ret;
}
int ObJoinFilterOp::insert_by_row()
{
int ret = OB_SUCCESS;
for (int i = 0; i < local_rf_msgs_.count() && OB_SUCC(ret); ++i) {
if (OB_FAIL(local_rf_msgs_.at(i)->insert_by_row(MY_SPEC.join_keys_,
MY_SPEC.hash_funcs_, MY_SPEC.calc_tablet_id_expr_, eval_ctx_))) {
LOG_WARN("fail to insert rf by row", K(ret));
}
}
return ret;
}
int ObJoinFilterOp::insert_by_row_batch(const ObBatchRows *child_brs)
{
int ret = OB_SUCCESS;
if (!MY_SPEC.use_rich_format_) {
for (int i = 0; i < local_rf_msgs_.count() && OB_SUCC(ret); ++i) {
if (OB_FAIL(local_rf_msgs_.at(i)->insert_by_row_batch(child_brs,
MY_SPEC.join_keys_, MY_SPEC.hash_funcs_,
MY_SPEC.calc_tablet_id_expr_, eval_ctx_,
join_filter_hash_values_))) {
LOG_WARN("fail to insert rf by row batch", K(ret));
}
}
} else {
for (int i = 0; i < local_rf_msgs_.count() && OB_SUCC(ret); ++i) {
if (OB_FAIL(local_rf_msgs_.at(i)->insert_by_row_vector(child_brs,
MY_SPEC.join_keys_, MY_SPEC.hash_funcs_,
MY_SPEC.calc_tablet_id_expr_, eval_ctx_,
join_filter_hash_values_))) {
LOG_WARN("fail to insert rf by row vector", K(ret));
}
}
}
return ret;
}
int ObJoinFilterOp::inner_close()
{
int ret = OB_SUCCESS;
ObJoinFilterOpInput *filter_input = static_cast<ObJoinFilterOpInput*>(input_);
if (MY_SPEC.is_create_mode() && OB_FAIL(close_join_filter_create())) {
LOG_WARN("fail to open join filter create op", K(ret));
} else if ((MY_SPEC.is_use_mode() && OB_FAIL(close_join_filter_use()))) {