forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_exec_context.cpp
More file actions
1351 lines (1262 loc) · 43.8 KB
/
Copy pathob_exec_context.cpp
File metadata and controls
1351 lines (1262 loc) · 43.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 "ob_exec_context.h"
#include "sql/engine/px/ob_px_util.h"
#include "sql/engine/expr/ob_expr_lob_utils.h"
#include "observer/ob_server.h"
#include "storage/lob/ob_lob_persistent_reader.h"
#include "sql/executor/ob_memory_tracker.h"
namespace oceanbase
{
using namespace oceanbase::common;
namespace sql
{
int ObOpKitStore::init(ObIAllocator &alloc, const int64_t size)
{
int ret = OB_SUCCESS;
if (size < 0) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(size));
} else if (NULL == (kits_ = static_cast<ObOperatorKit *>(
alloc.alloc(size * sizeof(kits_[0]))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
} else {
memset(kits_, 0, size * sizeof(kits_[0]));
size_ = size;
}
LOG_DEBUG("trace init kit store", K(ret), K(size));
return ret;
}
void ObOpKitStore::destroy()
{
if (NULL != kits_) {
for (int64_t i = 0; i < size_; i++) {
ObOperatorKit &kit = kits_[i];
if (NULL != kit.op_) {
kit.op_->destroy();
}
if (NULL != kit.input_) {
kit.input_->~ObOpInput();
}
}
}
}
int ObDiagnosisManager::add_warning_info(int err_ret, int line_idx) {
int ret = OB_SUCCESS;
if (OB_FAIL(rets_.push_back(err_ret))) {
LOG_WARN("failed to push back error code into array", K(ret), K(err_ret));
} else if (OB_FAIL(idxs_.push_back(line_idx))) {
LOG_WARN("failed to push back line number into array", K(ret), K(line_idx));
}
return ret;
}
int ObDiagnosisManager::do_diagnosis(ObBitVector &skip, int64_t limit_num) {
int ret = OB_SUCCESS;
if (idxs_.count() != rets_.count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("idxs_ and rets_ count mismatch", K(ret), K(idxs_.count()), K(rets_.count()));
} else if (idxs_.count() > 0) {
if (cur_file_url_.empty()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("missing cur_file_url", K(ret));
} else {
ObWarningBuffer *buffer = ob_get_tsi_warning_buffer();
bool has_col_info = idxs_.count() == col_names_.count();
for (int i = 0; OB_SUCC(ret) && i < idxs_.count(); i++) {
int64_t idx = idxs_.at(i);
int64_t err_ret = rets_.at(i);
ObSqlString err_msg;
if (skip.at(idx)) {
continue;
}
if (has_col_info) {
ObString cur_col_name = col_names_.at(i);
if (OB_FAIL(err_msg.append_fmt("fail to scan file %.*s at line %ld for column %.*s, error: %s",
cur_file_url_.length(), cur_file_url_.ptr(),
idx + cur_line_number_,
cur_col_name.length(), cur_col_name.ptr(),
common::ob_strerror(err_ret)))) {
LOG_WARN("failed to append error message", K(err_ret));
}
} else {
if (OB_FAIL(err_msg.append_fmt("fail to scan file %.*s at line %ld, error: %s",
cur_file_url_.length(), cur_file_url_.ptr(),
idx + cur_line_number_,
common::ob_strerror(err_ret)))) {
LOG_WARN("failed to append error message", K(err_ret));
}
}
if (OB_SUCC(ret)) {
skip.set(idx);
buffer->append_warning(err_msg.ptr(), err_ret);
if (limit_num >= 0 && buffer->get_total_warning_count() > limit_num) {
ret = OB_REACH_DIAGNOSIS_ERROR_LIMIT;
}
}
}
idxs_.reuse();
rets_.reuse();
col_names_.reuse();
allocator_.reuse();
}
} else {
// do nothing
}
return ret;
}
ObExecContext::ObExecContext(ObIAllocator &allocator)
: allocator_(allocator),
phy_op_size_(0),
phy_op_ctx_store_(NULL),
phy_op_input_store_(NULL),
phy_plan_ctx_(NULL),
expr_op_size_(0),
expr_op_ctx_store_(NULL),
task_executor_ctx_(*this),
my_session_(NULL),
sql_proxy_(NULL),
stmt_factory_(NULL),
expr_factory_(NULL),
outline_params_wrapper_(NULL),
execution_id_(OB_INVALID_ID),
has_non_trivial_expr_op_ctx_(false),
sql_ctx_(NULL),
pl_stack_ctx_(nullptr),
need_disconnect_(true),
pl_ctx_(NULL),
package_guard_(NULL),
row_id_list_(nullptr),
row_id_list_array_(),
total_row_count_(0),
reusable_interm_result_(false),
is_async_end_trans_(false),
gi_task_map_(nullptr),
udf_ctx_mgr_(nullptr),
output_row_(NULL),
field_columns_(NULL),
is_direct_local_plan_(false),
sqc_handler_(nullptr),
px_task_id_(-1),
px_sqc_id_(-1),
bloom_filter_ctx_array_(),
frames_(NULL),
frame_cnt_(0),
ori_frames_(nullptr),
ori_frame_cnt_(0),
ori_expr_op_size_(0),
op_kit_store_(),
convert_allocator_(nullptr),
mem_context_(nullptr),
pwj_map_(nullptr),
group_pwj_map_(nullptr),
check_status_times_(0),
vt_ift_(nullptr),
px_batch_id_(0),
admission_version_(UINT64_MAX),
admission_addr_map_(),
use_temp_expr_ctx_cache_(false),
temp_expr_ctx_map_(),
dml_event_(ObDmlEventType::DE_INVALID),
update_columns_(nullptr),
expect_range_count_(0),
das_ctx_(allocator),
parent_ctx_(nullptr),
nested_level_(0),
is_ps_prepare_stage_(false),
register_op_id_(OB_INVALID_ID),
tmp_alloc_used_(false),
table_direct_insert_ctx_(),
errcode_(OB_SUCCESS),
dblink_snapshot_map_(),
user_logging_ctx_(),
is_online_stats_gathering_(false),
is_ddl_idempotent_auto_inc_(false),
slice_count_(0),
slice_idx_(0),
slice_row_idx_(0),
autoinc_range_interval_(0),
lob_access_ctx_(nullptr),
auto_dop_map_(),
force_local_plan_(false),
diagnosis_manager_(),
deterministic_udf_cache_allocator_("UDFCACHE", OB_MALLOC_NORMAL_BLOCK_SIZE, MTL_ID()),
current_granule_type_(OB_GRANULE_UNINITIALIZED)
{
}
ObExecContext::~ObExecContext()
{
row_id_list_array_.reset();
destroy_eval_allocator();
reset_op_ctx();
if (NULL != phy_plan_ctx_) {
if (!THIS_WORKER.has_req_flag()) {
// For background threads, need to call destructor
phy_plan_ctx_->~ObPhysicalPlanCtx();
} else {
// free subschema map memory
phy_plan_ctx_->destroy();
}
}
phy_plan_ctx_ = NULL;
// destory gi task info map
if (OB_NOT_NULL(gi_task_map_)) {
gi_task_map_->destroy();
gi_task_map_ = NULL;
}
if (OB_NOT_NULL(udf_ctx_mgr_)) {
udf_ctx_mgr_->~ObUdfCtxMgr();
udf_ctx_mgr_ = NULL;
}
if (OB_NOT_NULL(pl_ctx_)) {
pl_ctx_->~ObPLCtx();
pl_ctx_ = NULL;
}
if (OB_NOT_NULL(package_guard_)) {
package_guard_->~ObPLPackageGuard();
package_guard_ = NULL;
}
if (OB_NOT_NULL(group_pwj_map_)) {
group_pwj_map_->destroy();
group_pwj_map_ = nullptr;
}
if (OB_NOT_NULL(vt_ift_)) {
vt_ift_->~ObIVirtualTableIteratorFactory();
vt_ift_ = nullptr;
}
clean_resolve_ctx();
sqc_handler_ = nullptr;
if (OB_LIKELY(NULL != convert_allocator_)) {
DESTROY_CONTEXT(convert_allocator_);
convert_allocator_ = NULL;
}
if (OB_LIKELY(NULL != mem_context_)) {
DESTROY_CONTEXT(mem_context_);
mem_context_ = NULL;
}
admission_addr_map_.destroy();
if (!temp_expr_ctx_map_.created()) {
// do nothing
} else {
for (hash::ObHashMap<int64_t, int64_t>::iterator it = temp_expr_ctx_map_.begin();
it != temp_expr_ctx_map_.end();
++it) {
(reinterpret_cast<ObTempExprCtx *>(it->second))->~ObTempExprCtx();
}
temp_expr_ctx_map_.destroy();
}
update_columns_ = nullptr;
errcode_ = OB_SUCCESS;
if (OB_NOT_NULL(lob_access_ctx_)) {
lob_access_ctx_->~ObLobAccessCtx();
lob_access_ctx_ = nullptr;
}
auto_dop_map_.destroy();
}
void ObExecContext::clean_resolve_ctx()
{
if (OB_NOT_NULL(expr_factory_)) {
expr_factory_->~ObRawExprFactory();
expr_factory_ = nullptr;
}
if (OB_NOT_NULL(stmt_factory_)) {
stmt_factory_->~ObStmtFactory();
stmt_factory_ = nullptr;
}
sql_ctx_ = nullptr;
pl_stack_ctx_ = nullptr;
}
uint64_t ObExecContext::get_ser_version() const
{
return SER_VERSION_1;
}
void ObExecContext::reset_op_ctx()
{
reset_expr_op();
op_kit_store_.destroy();
}
void ObExecContext::reset_op_env()
{
reset_op_ctx();
op_kit_store_.reset();
phy_op_size_ = 0;
expr_op_size_ = 0;
output_row_ = NULL;
field_columns_ = NULL;
if (OB_NOT_NULL(gi_task_map_)) {
if (gi_task_map_->created()) {
gi_task_map_->clear();
}
}
if (OB_NOT_NULL(udf_ctx_mgr_)) {
udf_ctx_mgr_->reset();
}
}
int ObExecContext::init_phy_op(const uint64_t phy_op_size)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(phy_op_size <= 0)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("fail to initialize", K(phy_op_size));
} else if (OB_UNLIKELY(phy_op_size_ > 0)) {
ret = OB_INIT_TWICE;
LOG_WARN("init exec ctx twice", K_(phy_op_size));
} else if (NULL == my_session_) {
ret = OB_NOT_INIT;
LOG_WARN("session info not set", K(ret));
} else {
phy_op_size_ = phy_op_size;
if (OB_FAIL(op_kit_store_.init(allocator_, phy_op_size))) {
LOG_WARN("init operator kit store failed", K(ret));
}
}
return ret;
}
int ObExecContext::init_expr_op(uint64_t expr_op_size, ObIAllocator *allocator)
{
int ret = OB_SUCCESS;
ObIAllocator &real_alloc = allocator != NULL ? *allocator : allocator_;
if (OB_UNLIKELY(expr_op_size_ > 0)) {
ret = OB_INIT_TWICE;
LOG_WARN("init exec ctx twice", K(ret), K_(expr_op_size));
} else if (expr_op_size > 0) {
int64_t ctx_store_size = static_cast<int64_t>(expr_op_size * sizeof(ObExprOperatorCtx *));
if (OB_ISNULL(expr_op_ctx_store_ = static_cast<ObExprOperatorCtx **>(real_alloc.alloc(ctx_store_size)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("fail to alloc expr_op_ctx_store_ memory", K(ret), K(ctx_store_size));
} else {
expr_op_size_ = expr_op_size;
MEMSET(expr_op_ctx_store_, 0, ctx_store_size);
}
}
return ret;
}
void ObExecContext::reset_expr_op()
{
if (expr_op_ctx_store_ != NULL) {
ObExprOperatorCtx **it = expr_op_ctx_store_;
ObExprOperatorCtx **it_end = &expr_op_ctx_store_[expr_op_size_];
for (; it != it_end; ++it) {
if (NULL != (*it)) {
(*it)->~ObExprOperatorCtx();
}
}
has_non_trivial_expr_op_ctx_ = false;
expr_op_ctx_store_ = NULL;
expr_op_size_ = 0;
}
}
void ObExecContext::destroy_eval_allocator()
{
eval_res_allocator_.reset();
eval_tmp_allocator_.reset();
tmp_alloc_used_ = false;
}
int ObExecContext::get_temp_expr_eval_ctx(const ObTempExpr &temp_expr,
ObTempExprCtx *&temp_expr_ctx)
{
int ret = OB_SUCCESS;
if (use_temp_expr_ctx_cache_) {
if (!temp_expr_ctx_map_.created()) {
OZ(temp_expr_ctx_map_.create(8, ObMemAttr(OB_SERVER_TENANT_ID, "TempExprCtx")));
}
if (OB_SUCC(ret)) {
int64_t ctx_ptr = 0;
if (OB_FAIL(temp_expr_ctx_map_.get_refactored(reinterpret_cast<int64_t>(&temp_expr),
ctx_ptr))) {
if (OB_HASH_NOT_EXIST == ret) {
ret = OB_SUCCESS;
OZ(build_temp_expr_ctx(temp_expr, temp_expr_ctx));
CK(OB_NOT_NULL(temp_expr_ctx));
OZ(temp_expr_ctx_map_.set_refactored(reinterpret_cast<int64_t>(&temp_expr),
reinterpret_cast<int64_t>(temp_expr_ctx)));
} else {
LOG_WARN("fail to get temp expr ctx", K(temp_expr), K(ret));
}
} else {
temp_expr_ctx = reinterpret_cast<ObTempExprCtx *>(ctx_ptr);
}
}
} else {
OZ(build_temp_expr_ctx(temp_expr, temp_expr_ctx));
}
return ret;
}
int ObExecContext::build_temp_expr_ctx(const ObTempExpr &temp_expr, ObTempExprCtx *&temp_expr_ctx)
{
int ret = OB_SUCCESS;
uint64_t frame_cnt = 0;
char **frames = NULL;
char *mem = static_cast<char*>(get_allocator().alloc(sizeof(ObTempExprCtx)));
ObArray<char *> tmp_param_frame_ptrs;
if (OB_ISNULL(mem)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("no more memory to create temp expr ctx", K(ret));
}
OX(temp_expr_ctx = new(mem)ObTempExprCtx(*this));
OZ(temp_expr.alloc_frame(get_allocator(), tmp_param_frame_ptrs, frame_cnt, frames));
OX(temp_expr_ctx->frames_ = frames);
OX(temp_expr_ctx->frame_cnt_ = frame_cnt);
// init expr_op_size_ and expr_op_ctx_store_
if (OB_SUCC(ret)) {
if (temp_expr.need_ctx_cnt_ > 0) {
int64_t ctx_store_size = static_cast<int64_t>(
temp_expr.need_ctx_cnt_ * sizeof(ObExprOperatorCtx *));
if (OB_ISNULL(temp_expr_ctx->expr_op_ctx_store_
= static_cast<ObExprOperatorCtx **>(allocator_.alloc(ctx_store_size)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("fail to alloc expr_op_ctx_store_ memory", K(ret), K(ctx_store_size));
} else {
temp_expr_ctx->expr_op_size_ = temp_expr.need_ctx_cnt_;
MEMSET(temp_expr_ctx->expr_op_ctx_store_, 0, ctx_store_size);
}
}
}
return ret;
}
ObIAllocator &ObExecContext::get_sche_allocator()
{
return sche_allocator_;
}
ObIAllocator &ObExecContext::get_allocator()
{
return allocator_;
}
int ObExecContext::create_expr_op_ctx(uint64_t op_id, int64_t op_ctx_size, void *&op_ctx)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(op_id >= expr_op_size_ || op_ctx_size <= 0 || OB_ISNULL(expr_op_ctx_store_))) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(op_id), K(op_ctx_size), K(expr_op_ctx_store_));
} else if (OB_UNLIKELY(NULL != get_expr_op_ctx(op_id))) {
ret = OB_INIT_TWICE;
LOG_WARN("expr operator context has been created", K(op_id));
} else if (OB_ISNULL(op_ctx = allocator_.alloc(op_ctx_size))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("allocate memory failed", K(ret), K(op_id), K(op_ctx_size));
} else {
expr_op_ctx_store_[op_id] = static_cast<ObExprOperatorCtx *>(op_ctx);
has_non_trivial_expr_op_ctx_ = true;
}
return ret;
}
void *ObExecContext::get_expr_op_ctx(uint64_t op_id)
{
return (OB_LIKELY(op_id < expr_op_size_) && !OB_ISNULL(expr_op_ctx_store_)) ? expr_op_ctx_store_[op_id] : NULL;
}
int ObExecContext::create_physical_plan_ctx()
{
int ret = OB_SUCCESS;
ObPhysicalPlanCtx *local_plan_ctx = NULL;
if (OB_UNLIKELY(phy_plan_ctx_ != NULL)) {
ret = OB_INIT_TWICE;
LOG_WARN("phy_plan_ctx_ is not null");
} else if (OB_UNLIKELY(NULL == (local_plan_ctx = static_cast<ObPhysicalPlanCtx *>(
allocator_.alloc(sizeof(ObPhysicalPlanCtx)))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("no more memory to create physical_plan_ctx");
} else {
phy_plan_ctx_ = new (local_plan_ctx) ObPhysicalPlanCtx(allocator_);
phy_plan_ctx_->set_exec_ctx(this);
}
return ret;
}
ObStmtFactory *ObExecContext::get_stmt_factory()
{
if (OB_ISNULL(stmt_factory_)) {
if (OB_ISNULL(stmt_factory_ = OB_NEWx(ObStmtFactory, (&allocator_), allocator_))) {
LOG_WARN_RET(OB_ALLOCATE_MEMORY_FAILED, "fail to create log plan factory", K(stmt_factory_));
}
} else {
// do nothing
}
return stmt_factory_;
}
ObRawExprFactory *ObExecContext::get_expr_factory()
{
if (OB_ISNULL(expr_factory_)) {
if (OB_ISNULL(expr_factory_ = OB_NEWx(ObRawExprFactory, (&allocator_), allocator_))) {
LOG_WARN_RET(OB_ALLOCATE_MEMORY_FAILED, "fail to create log plan factory", K(expr_factory_));
}
} else {
// do nothing
}
return expr_factory_;
}
int ObExecContext::check_status()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(phy_plan_ctx_)) {
ret = OB_NOT_INIT;
LOG_WARN("physical plan ctx is null");
} else if (phy_plan_ctx_->is_exec_timeout()) {
ret = OB_TIMEOUT;
LOG_WARN("query is timeout", K(ret));
} else if (OB_ISNULL(my_session_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session info is null");
} else if (my_session_->is_terminate(ret)){
LOG_WARN("execution was terminated", K(ret));
} else if (IS_INTERRUPTED()) {
ObInterruptCode &ic = GET_INTERRUPT_CODE();
ret = ic.code_;
LOG_WARN("px execution was interrupted", K(ic), K(ret));
} else if (lib::Worker::WS_OUT_OF_THROTTLE == THIS_WORKER.check_wait()) {
ret = OB_KILLED_BY_THROTTLING;
} else if (OB_UNLIKELY((OB_SUCCESS != (ret = CHECK_MEM_STATUS())))) {
LOG_WARN("Exceeded memory usage limit", K(ret));
}
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = check_extra_status())) {
LOG_WARN("check extra status failed", K(tmp_ret));
if (OB_SUCC(ret)) {
ret = tmp_ret;
}
}
return ret;
}
int ObExecContext::fast_check_status(const int64_t n)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY((check_status_times_++ & n) == n)) {
ret = check_status();
}
return ret;
}
int ObExecContext::check_status_ignore_interrupt()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(phy_plan_ctx_)) {
ret = OB_NOT_INIT;
LOG_WARN("physical plan ctx is null", K(ret));
} else if (phy_plan_ctx_->is_timeout()) {
ret = OB_TIMEOUT;
LOG_WARN("query is timeout", K(ret));
} else if (OB_ISNULL(my_session_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session info is null", K(ret));
} else if (my_session_->is_terminate(ret)){
LOG_WARN("execution was terminated", K(ret));
} else if (lib::Worker::WS_OUT_OF_THROTTLE == THIS_WORKER.check_wait()) {
ret = OB_KILLED_BY_THROTTLING;
}
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = check_extra_status())) {
LOG_WARN("check extra status failed", K(tmp_ret));
} else if (OB_SUCC(ret)) {
ret = tmp_ret;
}
return ret;
}
int ObExecContext::fast_check_status_ignore_interrupt(const int64_t n)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY((check_status_times_++ & n) == n)) {
ret = check_status_ignore_interrupt();
}
return ret;
}
int ObExecContext::init_pl_ctx()
{
int ret = OB_SUCCESS;
pl::ObPLCtx *pl_ctx = NULL;
if (OB_ISNULL(pl_ctx =
static_cast<pl::ObPLCtx*>(get_allocator().alloc(sizeof(pl::ObPLCtx))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocator memory", K(ret), K(sizeof(pl::ObPLCtx)));
} else {
new(pl_ctx)pl::ObPLCtx();
set_pl_ctx(pl_ctx);
}
return ret;
}
uint64_t ObExecContext::get_min_cluster_version() const
{
return task_executor_ctx_.get_min_cluster_version();
}
const common::ObAddr& ObExecContext::get_addr() const
{
return MYADDR;
}
int ObExecContext::get_gi_task_map(GIPrepareTaskMap *&gi_task_map)
{
int ret = OB_SUCCESS;
gi_task_map = nullptr;
if (nullptr == gi_task_map_) {
void *buf = allocator_.alloc(sizeof(GIPrepareTaskMap));
if (nullptr == buf) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("Failed to allocate memories", K(ret));
} else if (FALSE_IT(gi_task_map_ = new(buf) GIPrepareTaskMap())) {
} else if (OB_FAIL(gi_task_map_->create(PARTITION_WISE_JOIN_TSC_HASH_BUCKET_NUM, /* assume no more than 8 table scan in a plan */
ObModIds::OB_SQL_PX))) {
LOG_WARN("Failed to create gi task map", K(ret));
} else {
gi_task_map = gi_task_map_;
}
} else {
gi_task_map = gi_task_map_;
}
return ret;
}
int ObExecContext::get_convert_charset_allocator(ObArenaAllocator *&allocator)
{
int ret = OB_SUCCESS;
allocator = NULL;
if (OB_ISNULL(convert_allocator_)) {
if (OB_ISNULL(my_session_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("session is null", K(ret));
} else {
lib::ContextParam param;
param.set_properties(lib::USE_TL_PAGE_OPTIONAL)
.set_mem_attr(my_session_->get_effective_tenant_id(),
common::ObModIds::OB_SQL_EXPR_CALC,
common::ObCtxIds::DEFAULT_CTX_ID);
if (OB_FAIL(CURRENT_CONTEXT->CREATE_CONTEXT(convert_allocator_, param))) {
SQL_ENG_LOG(WARN, "create entity failed", K(ret));
}
}
}
if (OB_SUCC(ret)) {
allocator = &convert_allocator_->get_arena_allocator();
}
return ret;
}
int ObExecContext::get_malloc_allocator(ObIAllocator *&allocator)
{
int ret = OB_SUCCESS;
allocator = NULL;
if (OB_ISNULL(mem_context_)) {
if (OB_ISNULL(my_session_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("session is null", K(ret));
} else {
lib::ContextParam param;
param.set_properties(lib::USE_TL_PAGE_OPTIONAL)
.set_mem_attr(my_session_->get_effective_tenant_id(),
common::ObModIds::OB_SQL_EXPR_CALC,
common::ObCtxIds::DEFAULT_CTX_ID);
if (OB_FAIL(CURRENT_CONTEXT->CREATE_CONTEXT(mem_context_, param))) {
SQL_ENG_LOG(WARN, "create entity failed", K(ret));
}
}
}
if (OB_SUCC(ret)) {
allocator = &mem_context_->get_malloc_allocator();
}
return ret;
}
void ObExecContext::try_reset_convert_charset_allocator()
{
if (OB_NOT_NULL(convert_allocator_)) {
convert_allocator_->reset_remain_one_page();
}
}
int ObExecContext::add_temp_table_interm_result_ids(uint64_t temp_table_id,
const common::ObAddr &sqc_addr,
const ObIArray<uint64_t> &ids)
{
int ret = OB_SUCCESS;
bool is_existed = false;
ObIArray<ObSqlTempTableCtx>& temp_ctx = get_temp_table_ctx();
for (int64_t i = 0; OB_SUCC(ret) && !is_existed && i < temp_ctx.count(); i++) {
ObSqlTempTableCtx &ctx = temp_ctx.at(i);
if (temp_table_id == ctx.temp_table_id_) {
ObTempTableResultInfo info;
info.addr_ = sqc_addr;
if (OB_FAIL(info.interm_result_ids_.assign(ids))) {
LOG_WARN("failed to assign to interm result ids.", K(ret));
} else if (OB_FAIL(ctx.interm_result_infos_.push_back(info))) {
LOG_WARN("failed to push back result info", K(ret));
} else {
is_existed = true;
}
}
}
if (OB_SUCC(ret) && !is_existed) {
ObSqlTempTableCtx ctx;
ctx.is_local_interm_result_ = false;
ctx.temp_table_id_ = temp_table_id;
ObTempTableResultInfo info;
info.addr_ = sqc_addr;
if (OB_FAIL(info.interm_result_ids_.assign(ids))) {
LOG_WARN("failed to assign to interm result ids.", K(ret));
} else if (OB_FAIL(ctx.interm_result_infos_.push_back(info))) {
LOG_WARN("failed to push back result info", K(ret));
} else if (OB_FAIL(temp_ctx.push_back(ctx))) {
LOG_WARN("failed to push back temp table context", K(ret));
}
}
return ret;
}
ObVirtualTableCtx ObExecContext::get_virtual_table_ctx()
{
int ret = OB_SUCCESS;
ObVirtualTableCtx vt_ctx;
if (OB_ISNULL(vt_ift_)) {
int64_t len = sizeof(observer::ObVirtualTableIteratorFactory);
void *buf = allocator_.alloc(len);
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate ObVirtualTableIteratorFactory failed", K(ret), K(len));
} else {
vt_ift_ = new(buf) observer::ObVirtualTableIteratorFactory(*GCTX.vt_iter_creator_);
}
}
vt_ctx.vt_iter_factory_ = vt_ift_;
vt_ctx.session_ = my_session_;
vt_ctx.schema_guard_ = sql_ctx_->schema_guard_;
return vt_ctx;
}
int ObExecContext::init_physical_plan_ctx(const ObPhysicalPlan &plan)
{
int ret = OB_SUCCESS;
int64_t foreign_key_checks = 0;
uint64_t tenant_data_version = 0;
bool supprt_check_pdml_affected_row = false;
if (OB_ISNULL(phy_plan_ctx_) || OB_ISNULL(my_session_) || OB_ISNULL(sql_ctx_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K_(phy_plan_ctx), K_(my_session), K(ret));
} else if (OB_FAIL(my_session_->get_foreign_key_checks(foreign_key_checks))) {
LOG_WARN("failed to get foreign_key_checks", K(ret));
} else {
int64_t start_time = my_session_->get_query_start_time();
int64_t plan_timeout = 0;
const ObPhyPlanHint &phy_plan_hint = plan.get_phy_plan_hint();
ObConsistencyLevel consistency = INVALID_CONSISTENCY;
my_session_->set_cur_phy_plan(const_cast<ObPhysicalPlan*>(&plan));
part_ranges_.set_tenant_id(my_session_->get_effective_tenant_id());
part_ranges_.set_label("PxTabletRangArr");
if (OB_UNLIKELY(phy_plan_hint.query_timeout_ > 0)) {
plan_timeout = phy_plan_hint.query_timeout_;
} else {
if (OB_FAIL(my_session_->get_query_timeout(plan_timeout))) {
LOG_WARN("fail to get query timeout", K(ret));
}
}
if (OB_SUCC(ret)) {
if (!plan.is_remote_plan()) {
if (OB_FAIL(phy_plan_ctx_->reserve_param_space(plan.get_param_count()))) {
LOG_WARN("reserve param space failed", K(ret), K(plan.get_param_count()));
}
}
}
if (OB_SUCC(ret)) {
if (stmt::T_SELECT == plan.get_stmt_type()) { // select has weak
if (sql_ctx_->is_protocol_weak_read_) {
consistency = WEAK;
} else if (OB_UNLIKELY(phy_plan_hint.read_consistency_ != INVALID_CONSISTENCY)) {
consistency = phy_plan_hint.read_consistency_;
} else {
consistency = my_session_->get_consistency_level();
}
} else {
consistency = STRONG;
}
phy_plan_ctx_->set_is_direct_insert_plan(plan.get_enable_append());
phy_plan_ctx_->set_consistency_level(consistency);
phy_plan_ctx_->set_timeout_timestamp(start_time + plan_timeout);
phy_plan_ctx_->set_rich_format(my_session_->use_rich_format());
reference_my_plan(&plan);
phy_plan_ctx_->set_ignore_stmt(plan.is_ignore());
phy_plan_ctx_->set_foreign_key_checks(0 != foreign_key_checks);
phy_plan_ctx_->set_table_row_count_list_capacity(plan.get_access_table_num());
phy_plan_ctx_->set_check_pdml_affected_rows(supprt_check_pdml_affected_row);
THIS_WORKER.set_timeout_ts(phy_plan_ctx_->get_timeout_timestamp());
}
}
if (OB_SUCC(ret)) {
const auto ¶m_store = phy_plan_ctx_->get_param_store();
int64_t first_array_index = plan.get_first_array_index();
const ObSqlArrayObj *array_param = NULL;
if (OB_LIKELY(OB_INVALID_INDEX == first_array_index)) {
//this query has no array binding, do nothing
} else if (OB_UNLIKELY(first_array_index >= param_store.count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("first array index is invalid", K(ret), K(first_array_index), K(param_store.count()));
} else if (OB_UNLIKELY(!param_store.at(first_array_index).is_ext_sql_array())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("first array param is invalid", K(ret), K(param_store.at(first_array_index)));
} else if (OB_ISNULL(array_param = reinterpret_cast<const ObSqlArrayObj*>(
param_store.at(first_array_index).get_ext()))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("array param is null", K(ret), K(param_store.at(first_array_index)));
} else {
phy_plan_ctx_->set_bind_array_count(array_param->count_);
}
}
return ret;
}
int ObExecContext::set_partition_ranges(const Ob2DArray<ObPxTabletRange> &part_ranges,
char *buf, int64_t size)
{
int ret = OB_SUCCESS;
part_ranges_.reset();
if (OB_UNLIKELY(part_ranges.count() <= 0)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("part ranges is empty", K(ret), K(part_ranges.count()));
} else {
int64_t pos = 0;
ObPxTabletRange tmp_range;
for (int64_t i = 0; OB_SUCC(ret) && i < part_ranges.count(); ++i) {
const ObPxTabletRange &cur_range = part_ranges.at(i);
if (0 == size && OB_FAIL(tmp_range.deep_copy_from<true>(cur_range, get_allocator(), buf, size, pos))) {
LOG_WARN("deep copy partition range failed", K(ret), K(cur_range));
} else if (0 != size && OB_FAIL(tmp_range.deep_copy_from<false>(cur_range, get_allocator(), buf, size, pos))) {
LOG_WARN("deep copy partition range failed", K(ret), K(cur_range));
} else if (OB_FAIL(part_ranges_.push_back(tmp_range))) {
LOG_WARN("push back partition range failed", K(ret), K(tmp_range));
}
}
}
return ret;
}
int ObExecContext::reset_one_row_id_list(const common::ObIArray<int64_t> *row_id_list)
{
int ret = OB_SUCCESS;
CK(OB_NOT_NULL(row_id_list));
if (OB_SUCC(ret)) {
row_id_list_array_.reset();
total_row_count_ = 0;
OZ(row_id_list_array_.push_back(row_id_list));
total_row_count_ += row_id_list->count();
}
return ret;
}
int ObExecContext::get_group_pwj_map(GroupPWJTabletIdMap *&group_pwj_map)
{
int ret = OB_SUCCESS;
group_pwj_map = nullptr;
if (nullptr == group_pwj_map_) {
void *buf = allocator_.alloc(sizeof(GroupPWJTabletIdMap));
if (nullptr == buf) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("Failed to allocate memories", K(ret));
} else {
group_pwj_map_ = new (buf) GroupPWJTabletIdMap();
/* assume no more than 8table scan in a plan */
if (OB_FAIL(group_pwj_map_->create(PARTITION_WISE_JOIN_TSC_HASH_BUCKET_NUM, ObModIds::OB_SQL_PX))) {
LOG_WARN("Failed to create group_pwj_map_", K(ret));
} else {
group_pwj_map = group_pwj_map_;
}
}
} else {
group_pwj_map = group_pwj_map_;
}
return ret;
}
int ObExecContext::deep_copy_group_pwj_map(const GroupPWJTabletIdMap *src)
{
int ret = OB_SUCCESS;
GroupPWJTabletIdMap *des = nullptr;
if (OB_ISNULL(src)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null");
} else if (OB_FAIL(get_group_pwj_map(des))) {
LOG_WARN("failed to get_group_pwj_map");
} else if (des->size() > 0) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("size should be 0", K(des->size()), K(src->size()));
} else {
FOREACH_X(iter, *src, OB_SUCC(ret)) {
const uint64_t table_id = iter->first;
const GroupPWJTabletIdInfo &group_pwj_tablet_id_info = iter->second;
if (OB_FAIL(des->set_refactored(table_id, group_pwj_tablet_id_info))) {
LOG_WARN("failed to set refactored", K(table_id));
}
}
}
return ret;
}
int ObExecContext::fill_px_batch_info(ObBatchRescanParams ¶ms,
int64_t batch_id, const sql::ObExpr::ObExprIArray &array)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(phy_plan_ctx_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("phy plan ctx is null", K(ret));
} else if (batch_id >= params.get_count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("batch param is unexpected", K(ret));
} else {
common::ObIArray<common::ObObjParam> &one_params =
params.get_one_batch_params(batch_id);
ObEvalCtx eval_ctx(*this);
for (int i = 0; OB_SUCC(ret) && i < one_params.count(); ++i) {
if (i > params.param_idxs_.count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("batch param is unexpected", K(ret));
} else {
phy_plan_ctx_->get_param_store_for_update().at(params.get_param_idx(i)) = one_params.at(i);
if (params.param_expr_idxs_.count() == one_params.count()) {
const sql::ObExpr *expr = NULL;
int64_t idx = params.param_expr_idxs_.at(i);
if (OB_FAIL(ret)) {
} else if (OB_UNLIKELY(idx > array.count())) {
// do nothing.
LOG_TRACE("param idx out of array count", K(idx), K(array.count()));
} else if (FALSE_IT(expr = &array.at(idx - 1))) {
} else if (T_INVALID == expr->type_) {
// do nothing.
LOG_TRACE("empty expr", KPC(expr));
} else {
expr->get_eval_info(eval_ctx).clear_evaluated_flag();
ObDynamicParamSetter::clear_parent_evaluated_flag(eval_ctx, *expr);
ObDatum ¶m_datum = expr->locate_datum_for_write(eval_ctx);
if (OB_FAIL(param_datum.from_obj(one_params.at(i), expr->obj_datum_map_))) {
LOG_WARN("fail to cast datum", K(ret));
} else if (is_lob_storage(one_params.at(i).get_type()) &&
OB_FAIL(ob_adjust_lob_datum(one_params.at(i), expr->obj_meta_,
expr->obj_datum_map_, get_allocator(), param_datum))) {
LOG_WARN("adjust lob datum failed", K(ret), K(i),
K(one_params.at(i).get_meta()), K(expr->obj_meta_));
} else {
expr->get_eval_info(eval_ctx).evaluated_ = true;
}
}
}
}
}