forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_plan_set.cpp
More file actions
2068 lines (1966 loc) · 82.2 KB
/
Copy pathob_plan_set.cpp
File metadata and controls
2068 lines (1966 loc) · 82.2 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_PC
#include "ob_plan_set.h"
#include "sql/plan_cache/ob_pcv_set.h"
using namespace oceanbase;
using namespace common;
using namespace oceanbase::sql;
using namespace oceanbase::transaction;
using namespace share;
using namespace share::schema;
using namespace pl;
namespace oceanbase
{
namespace sql
{
const ObPCUserVarMeta ObPlanSet::UNKNOWN_VAR_DEFAULT_META = ObPCUserVarMeta(PRECISION_UNKNOWN_YET,
ObVarcharType,
CS_LEVEL_IMPLICIT,
CS_TYPE_BINARY);
ObPlanSet::~ObPlanSet()
{
// Make sure destory planset before destory pre calculable expression.
if (OB_ISNULL(pre_cal_expr_handler_))
{
// have no pre calculable expression, do nothing
} else {
int64_t ref_cnt = pre_cal_expr_handler_->dec_ref_cnt();
if (ref_cnt == 0) {
common::ObIAllocator* alloc = pre_cal_expr_handler_->pc_alloc_;
pre_cal_expr_handler_->~PreCalcExprHandler();
alloc->free(pre_cal_expr_handler_);
pre_cal_expr_handler_ = NULL;
}
}
}
int ObPlanSet::get_variable_meta(const ObSQLSessionInfo *session_info, const ObString &var_name,
ObPCUserVarMeta &meta)
{
int ret = OB_SUCCESS;
ObSessionVariable sess_var;
if (OB_FAIL(session_info->get_user_variable(var_name, sess_var))) {
if (ret == OB_ERR_USER_VARIABLE_UNKNOWN) {
meta = UNKNOWN_VAR_DEFAULT_META;
ret = OB_SUCCESS;
LOG_TRACE("ignore unknow variable error", K(ret), K(var_name));
} else {
LOG_WARN("failed to get user variable", K(ret), K(var_name));
}
} else {
meta.parse_from_variable(sess_var);
}
return ret;
}
//used for get plan
int ObPlanSet::match_params_info(const ParamStore *params,
ObPlanCacheCtx &pc_ctx,
int64_t outline_param_idx,
bool &is_same)
{
int ret = OB_SUCCESS;
is_same = true;
ObExecContext &exec_ctx = pc_ctx.exec_ctx_;
ObSessionVariable sess_var;
bool is_sql = is_sql_planset();
if (false == is_match_outline_param(outline_param_idx)) {
is_same = false;
} else if (OB_ISNULL(params)) {
is_same = true;
} else if (params->count() > params_info_.count()) {
is_same = false;
} else {
// Match the original parameters
int64_t N = params->count();
LOG_TRACE("params info", K(params_info_), K(*params), K(this));
for (int64_t i = 0; OB_SUCC(ret) && is_same && i < N; ++i) {
if (OB_FAIL(match_param_info(params_info_.at(i),
params->at(i),
is_same,
is_sql))) {
LOG_WARN("fail to match param info", K(ret), K(params_info_), K(*params));
}
}
// Match related user session variables
// Here should first compare related_user_var_names_ with variables inside session_info, then perform pre-calculation
// Otherwise it will lead to session var type change, unable to match the plan. Example as follows:
// eg: SQL ParamStore
// 1. set @a := 1;
// 2. select @a; int obj
// 3. set @a := '1';
// 4. select @a; varchar obj
// 5. select @a; int obj(because it fills the pre-calculated result when matching sql2 plan)
// Result: sql5 cannot match sql4's plan
// Reason: sql5 matches sql2's plan by pre-calculating first, obtaining int obj, then comparing related_user_var_names_
// Found sess_var in session_info_ as varchar, matching failed.
// sql5 rematch sql4's plan, since it has already been pre-calculated, no further calculation is performed, so ParamStore
// inside is still int obj, while Obj in params_info_ is varchar, matching failed.
//
// So it should be changed to compare related_user_var_names with sess_var first, then perform pre-calculation
// would not result in matching with precomputed results of the previous plan in ParamStore
if (OB_SUCC(ret) && is_same && related_user_var_names_.count() > 0) {
if (related_user_var_names_.count() != related_user_sess_var_metas_.count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("related_user_var_names and related_user_sess_vars should have the same size",
K(ret), K(related_user_var_names_.count()), K(related_user_sess_var_metas_.count()));
} else if (OB_ISNULL(pc_ctx.sql_ctx_.session_info_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null",
K(ret), K(pc_ctx.sql_ctx_.session_info_));
} else {
ObSQLSessionInfo *session_info = pc_ctx.sql_ctx_.session_info_;
ObPCUserVarMeta tmp_meta;
for (int64_t i = 0 ; OB_SUCC(ret) && is_same && i < related_user_var_names_.count(); i++) {
if (OB_FAIL(get_variable_meta(pc_ctx.sql_ctx_.session_info_,
related_user_var_names_.at(i), tmp_meta))) {
LOG_WARN("failed to get user variable meta", K(ret),
K(related_user_var_names_.at(i)), K(i));
} else {
is_same = (related_user_sess_var_metas_.at(i) == tmp_meta);
}
}
}
}
if (OB_SUCC(ret) && OB_NOT_NULL(pc_ctx.sql_ctx_.session_info_) && is_same) {
is_same = (is_cli_return_rowid_ == pc_ctx.sql_ctx_.session_info_->is_client_return_rowid());
}
//pre calculate
if (OB_SUCC(ret) && is_same) {
ObPhysicalPlanCtx *plan_ctx = exec_ctx.get_physical_plan_ctx();
ObSQLSessionInfo *session = exec_ctx.get_my_session();
if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null session", K(ret));
} else if (OB_ISNULL(plan_ctx)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null plan context", K(ret));
} else if (fetch_cur_time_ && FALSE_IT(plan_ctx->set_cur_time(
ObClockGenerator::getClock(), *session))) {
// never reach
} else if (FALSE_IT(plan_ctx->set_last_trace_id(session->get_last_trace_id()))) {
} else if (params->count() != params_info_.count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("param info count is different", K(params_info_), K(*params), K(ret));
} else {
/* check calculable expr constraints*/
DLIST_FOREACH(pre_calc_con, all_pre_calc_constraints_) {
if (OB_FAIL(ObPlanCacheObject::check_pre_calc_cons(is_ignore_stmt_,
is_same,
*pre_calc_con,
exec_ctx))) {
LOG_WARN("failed to pre calculate expression", K(ret));
} else if (!is_same) {
break;
}
}
}
}
// Match true/false, at this time the flag_ in params is the initial value, and cannot be used directly.
for (int64_t i = 0; OB_SUCC(ret) && is_same && i < params->count(); ++i) {
if (OB_FAIL(match_param_bool_value(params_info_.at(i),
params->at(i),
is_same))) {
LOG_WARN("failed to match param bool value", K(ret), K(params_info_), K(*params));
}
} //for end
// check const constraint
if (OB_SUCC(ret) && is_same) {
OC( (match_constraint)(*params, is_same) );
}
if (OB_SUCC(ret) && is_same) {
if (OB_FAIL(match_multi_stmt_info(*params, multi_stmt_rowkey_pos_, is_same))) {
LOG_WARN("failed to match multi stmt info", K(ret));
} else if (!is_same) {
ret = OB_BATCHED_MULTI_STMT_ROLLBACK;
LOG_TRACE("batched multi stmt needs rollback", K(ret));
}
}
if (OB_FAIL(ret)) {
is_same = false;
LOG_TRACE("after match param result", K(ret), K(is_same), K(params_info_));
}
}
LOG_DEBUG("after match param result", K(ret), K(is_same), K(params_info_));
return ret;
}
int ObPlanSet::copy_param_flag_from_param_info(ParamStore *params)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(params)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("params is null", K(ret));
} else if (params->count() != params_info_.count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("params is null", K(ret), KPC(params), K(params_info_));
}
for (int64_t i = 0; OB_SUCC(ret) && i < params->count(); ++i) {
params->at(i).set_param_flag(params_info_.at(i).flag_);
}
return ret;
}
// Match parameter type information
int ObPlanSet::match_param_info(const ObParamInfo ¶m_info,
const ObObjParam ¶m,
bool &is_same,
bool is_sql_planset) const
{
int ret = OB_SUCCESS;
is_same = true;
// extend type must be checked
// insert into t values (1)
// insert into t values (:0)
// two sql have the same key `insert into t values (?)`
// but they have complete different plans
if (param_info.flag_.need_to_check_type_ || need_match_all_params_) {
if (param.get_param_meta().get_type() != param.get_type()) {
LOG_TRACE("differ in match param info",
K(param.get_param_meta().get_type()),
K(param.get_type()));
}
if (param.get_collation_type() != param_info.col_type_
&& !(param.get_param_meta().is_ext() || param.is_collection_sql_type())) {
is_same = false;
} else if (param.get_param_meta().get_type() != param_info.type_) {
is_same = false;
} else if (ob_is_enumset_inner_tc(param.get_param_meta().get_type())) { // since enunset_inner type param will mock expr use current param, can not resue plan
is_same = false;
} else if (param.is_collection_sql_type()) {
if (param_info.is_oracle_null_value_) {
is_same = false;
} else {
uint64_t udt_id_param = param.get_accuracy().get_accuracy();
uint64_t udt_id_info = static_cast<uint64_t>(param_info.ext_real_type_) << 32
| static_cast<uint32_t>(param_info.col_type_);
is_same = (udt_id_info == udt_id_param) ? true : false;
}
} else if (param.is_ext_sql_array()) {
ObDataType data_type;
if (!param_info.flag_.need_to_check_extend_type_) {
// do nothing
} else if (OB_FAIL(ObSQLUtils::get_ext_obj_data_type(param, data_type))) {
LOG_WARN("fail to get obj data_type", K(ret), K(param));
} else if (data_type.get_obj_type() == ObDecimalIntType) {
is_same = param_info.ext_real_type_ == ObDecimalIntType
&& data_type.get_scale() == param_info.scale_
&& match_decint_precision(param_info, data_type.get_precision());
} else if (data_type.get_scale() == param_info.scale_ &&
data_type.get_obj_type() == param_info.ext_real_type_) {
is_same = true;
} else {
is_same = false;
LOG_TRACE("ext match param info", K(data_type), K(param_info), K(is_same), K(ret));
}
LOG_DEBUG("ext match param info", K(data_type), K(param_info), K(is_same), K(ret));
} else if (param.get_param_meta().is_ext()) {
if (!param_info.flag_.need_to_check_extend_type_) {
// do nothing
} else {
uint64_t udt_id_param = param.get_accuracy().get_accuracy();
uint64_t udt_id_info = static_cast<uint64_t>(param_info.ext_real_type_) << 32
| static_cast<uint32_t>(param_info.col_type_);
is_same = (udt_id_info == udt_id_param) ? true : false;
}
LOG_DEBUG("ext match param info", K(param.get_accuracy()), K(param_info), K(is_same), K(ret));
} else if (param_info.is_oracle_null_value_ && !param.is_null()) {
is_same = false;
} else if (ObSQLUtils::is_oracle_null_with_normal_type(param)
&&!param_info.is_oracle_null_value_) { //Typed nulls can only match plans with the same type of nulls.
is_same = false;
} else if (param_info.flag_.is_boolean_ != param.is_boolean()) { //bool type not match int type
is_same = false;
} else {
// number params in point and st_point can ignore scale check to share plancache
// please refrer to ObSqlParameterization::is_ignore_scale_check
is_same = param_info.flag_.ignore_scale_check_
? true
: (param.get_scale() == param_info.scale_);
is_same = is_same && match_decint_precision(param_info, param.get_precision());
}
}
return ret;
}
// Match true/false parameter
int ObPlanSet::match_param_bool_value(const ObParamInfo ¶m_info,
const ObObjParam ¶m,
bool &is_same) const
{
int ret = OB_SUCCESS;
is_same = true;
bool vec_param_same = true;
bool first_val = true;
if (param_info.flag_.need_to_check_bool_value_) {
bool is_value_true = false;
if (OB_FAIL(ObObjEvaluator::is_true(param, is_value_true))) {
SQL_PC_LOG(WARN, "fail to get param info", K(ret));
} else if (is_value_true != param_info.flag_.expected_bool_value_) {
is_same = false;
}
}
return ret;
}
int ObPlanSet::match_multi_stmt_info(const ParamStore ¶ms,
const ObIArray<int64_t> &multi_stmt_rowkey_pos,
bool &is_match)
{
int ret = OB_SUCCESS;
is_match = false;
if (multi_stmt_rowkey_pos.empty()) {
is_match = true;
} else {
// check all rowkey are different
int64_t stmt_count = 0;
ObSEArray<const ObObj*, 16> binding_data;
for (int64_t i = 0; OB_SUCC(ret) && i < multi_stmt_rowkey_pos.count(); i++) {
int64_t pos = multi_stmt_rowkey_pos.at(i);
if (OB_UNLIKELY(pos < 0 || pos >= params.count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected array pos",K(pos), K(params.count()), K(ret));
} else if (OB_UNLIKELY(!params.at(pos).is_ext_sql_array())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected type", K(params.at(pos)), K(ret));
} else {
const ObSqlArrayObj *array_params = reinterpret_cast<const ObSqlArrayObj*>(
params.at(pos).get_ext());
if (OB_ISNULL(array_params)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", KPC(array_params), K(ret));
} else if (OB_FAIL(binding_data.push_back(array_params->data_))) {
LOG_WARN("failed to push back array", K(ret));
} else if (i == 0) {
stmt_count = array_params->count_;
} else if (OB_UNLIKELY(stmt_count != array_params->count_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected stmt count", K(ret));
} else { /*do nothing*/ }
}
}
if (OB_SUCC(ret)) {
is_match = true;
HashKey hash_key;
UniqueHashSet unique_ctx;
if (OB_FAIL(unique_ctx.create(stmt_count))) {
LOG_WARN("failed to hash set", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && is_match && i < stmt_count; i++) {
hash_key.reuse();
for (int64_t j = 0; OB_SUCC(ret) && j < binding_data.count(); j++) {
ret = hash_key.rowkey_.push_back(binding_data.at(j)[i]);
}
if (OB_SUCC(ret)) {
ret = unique_ctx.exist_refactored(hash_key);
if (OB_HASH_EXIST == ret) {
ret = OB_SUCCESS;
is_match = false;
if (REACH_TIME_INTERVAL(10000000)) {
LOG_INFO("batched multi-stmt does not have the same rowkey", K(i),
K(hash_key));
}
} else if (OB_HASH_NOT_EXIST == ret) {
if (OB_FAIL(unique_ctx.set_refactored(hash_key))) {
LOG_WARN("store rowkey failed", K(ret));
}
} else {
LOG_WARN("check rowkey distinct failed", K(ret));
}
}
}
}
}
}
return ret;
}
// Determine whether all parameters in the same column across multiple groups are true/false, and return the first parameter that is true/false
/*//Determine if each obj in the array parameter of param store is always true/false*/
//int ObPlanSet::check_array_bind_same_bool_param(const Ob2DArray<ObParamInfo,
//OB_MALLOC_BIG_BLOCK_SIZE,
//ObWrapperAllocator, false> ¶m_infos,
//const ParamStore ¶m_store,
//bool &same_bool_param)
//{
//int ret = OB_SUCCESS;
//bool first_val = false;
//same_bool_param = true;
//for (int64_t i = 0; OB_SUCC(ret) && same_bool_param && i < param_store.count(); ++i) {
//if (param_infos.at(i).flag_.need_to_check_bool_value_
//&& param_store.at(i).is_ext()) {
////Check the result of each group of parameters is true/false
//if (OB_FAIL(check_vector_param_same_bool(param_store.at(i),
//first_val,
//same_bool_param))) {
//LOG_WARN("fail to check vector param same bool", K(ret));
//}
//}
//} //for end
//return ret;
/*}*/
//used for add plan
int ObPlanSet::match_params_info(const Ob2DArray<ObParamInfo,
OB_MALLOC_BIG_BLOCK_SIZE,
ObWrapperAllocator, false> &infos,
int64_t outline_param_idx,
const ObPlanCacheCtx &pc_ctx,
bool &is_same)
{
int ret = OB_SUCCESS;
is_same = true;
ObSQLSessionInfo *session_info = pc_ctx.sql_ctx_.session_info_;
if (OB_ISNULL(session_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null session_info", K(ret));
} else if (false == is_match_outline_param(outline_param_idx)) {
is_same = false;
} else if (infos.count() != params_info_.count()) {
is_same = false;
} else {
int64_t N = infos.count();
for (int64_t i = 0; is_same && i < N; ++i) {
if (true == is_same
&& (params_info_.at(i).flag_.need_to_check_type_ || need_match_all_params_)) {
if (infos.at(i).type_ != params_info_.at(i).type_
|| infos.at(i).scale_ != params_info_.at(i).scale_
|| infos.at(i).col_type_ != params_info_.at(i).col_type_
|| (params_info_.at(i).flag_.need_to_check_extend_type_
&& infos.at(i).ext_real_type_ != params_info_.at(i).ext_real_type_)
|| (params_info_.at(i).flag_.is_boolean_ != infos.at(i).flag_.is_boolean_)
|| !match_decint_precision(params_info_.at(i), infos.at(i).precision_)) {
is_same = false;
}
}
if (true == is_same && params_info_.at(i).flag_.need_to_check_bool_value_) {
if (infos.at(i).flag_.expected_bool_value_
!= params_info_.at(i).flag_.expected_bool_value_) {
is_same = false;
}
}
}
if (is_same && related_user_var_names_.count() > 0) {
if (related_user_var_names_.count() != pc_ctx.sql_ctx_.related_user_var_names_.count()) {
is_same = false;
} else {
int64_t CNT = related_user_var_names_.count();
ObPCUserVarMeta tmp_meta;
for (int64_t i = 0; OB_SUCC(ret) && is_same && i < CNT; i++) {
if (related_user_var_names_.at(i) != pc_ctx.sql_ctx_.related_user_var_names_.at(i)) {
is_same = false;
} else if (OB_FAIL(get_variable_meta(pc_ctx.sql_ctx_.session_info_,
related_user_var_names_.at(i), tmp_meta))) {
LOG_WARN("failed to get user variable meta", K(ret),
K(related_user_var_names_.at(i)), K(i));
} else {
is_same = (related_user_sess_var_metas_.at(i) == tmp_meta);
}
}
}
}
if (OB_SUCC(ret) && OB_NOT_NULL(pc_ctx.sql_ctx_.session_info_) && is_same) {
is_same = (is_cli_return_rowid_ == pc_ctx.sql_ctx_.session_info_->is_client_return_rowid());
}
if (OB_SUCC(ret) && is_same) {
if (OB_FAIL(ObPlanCacheObject::match_pre_calc_cons(all_pre_calc_constraints_, pc_ctx,
is_ignore_stmt_, is_same))) {
LOG_WARN("failed to match pre calc cons", K(ret));
} else if (!is_same) {
LOG_TRACE("pre calc constraints for plan set and cur plan not match");
}
}
if (is_sql_planset() && OB_SUCC(ret) && is_same) {
CK( OB_NOT_NULL(pc_ctx.exec_ctx_.get_physical_plan_ctx()) );
if (OB_SUCC(ret)) {
const ParamStore ¶ms = pc_ctx.exec_ctx_.get_physical_plan_ctx()->get_param_store();
OC( (match_constraint)(params, is_same));
OC( (match_cons)(pc_ctx, is_same));
}
}
}
if (OB_FAIL(ret)) {
is_same = false;
}
return ret;
}
bool ObPlanSet::can_skip_params_match()
{
bool can_skip = true;
for (int64_t i = 0; can_skip && i < params_info_.count(); i++) {
if (params_info_.at(i).flag_.need_to_check_type_) {
can_skip = false;
}
}
if (can_skip) {
if (!all_plan_const_param_constraints_.empty() ||
!all_possible_const_param_constraints_.empty() ||
!all_equal_param_constraints_.empty() ||
all_pre_calc_constraints_.get_size() != 0) {
can_skip = false;
LOG_DEBUG("print can't skip", K(can_skip), K(all_plan_const_param_constraints_.empty()),
K(all_possible_const_param_constraints_.empty()),
K(all_equal_param_constraints_.empty()),
K(all_pre_calc_constraints_.get_size()));
}
}
return can_skip;
}
bool ObPlanSet::can_delay_init_datum_store()
{
bool can_delay = true;
if (all_pre_calc_constraints_.get_size() != 0) {
can_delay = false;
}
return can_delay;
}
void ObPlanSet::reset()
{
ObDLinkBase<ObPlanSet>::reset();
plan_cache_value_ = NULL;
params_info_.reset();
stmt_type_ = stmt::T_NONE;
fetch_cur_time_ = false;
is_ignore_stmt_ = false;
//is_wise_join_ = false;
outline_param_idx_ = OB_INVALID_INDEX;
related_user_var_names_.reset();
related_user_sess_var_metas_.reset();
is_cli_return_rowid_ = false;
all_possible_const_param_constraints_.reset();
all_plan_const_param_constraints_.reset();
all_equal_param_constraints_.reset();
all_pre_calc_constraints_.reset();
can_skip_params_match_ = false;
can_delay_init_datum_store_ = false;
alloc_.reset();
}
ObPlanCache *ObPlanSet::get_plan_cache() const
{
ObPlanCache *pc = NULL;
if (NULL == plan_cache_value_
|| NULL == plan_cache_value_->get_pcv_set()
|| NULL == plan_cache_value_->get_pcv_set()->get_plan_cache()) {
pc = NULL;
} else {
pc = plan_cache_value_->get_pcv_set()->get_plan_cache();
}
return pc;
}
int ObPlanSet::remove_cache_obj_entry(const ObCacheObjID obj_id)
{
int ret = OB_SUCCESS;
ObPlanCache *pc = NULL;
ObPCVSet *pcv_set = NULL;
if (OB_ISNULL(get_plan_cache_value())
|| OB_ISNULL(pcv_set = get_plan_cache_value()->get_pcv_set())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid argument", K(pcv_set));
} else if (NULL == (pc = get_plan_cache())) {
LOG_WARN("invalid argument", K(pc));
} else if (OB_FAIL(pcv_set->remove_cache_obj_entry(obj_id))) {
LOG_WARN("failed to remove cache obj entry", K(ret), K(obj_id));
} else if (OB_FAIL(pc->remove_cache_obj_stat_entry(obj_id))) {
LOG_WARN("failed to remove plan stat", K(obj_id), K(ret));
}
return ret;
}
int ObPlanSet::init_new_set(const ObPlanCacheCtx &pc_ctx,
const ObPlanCacheObject &plan,
int64_t outline_param_idx,
common::ObIAllocator* pc_alloc_)
{
int ret = OB_SUCCESS;
ObPlanCache *pc = nullptr;
const ObSqlCtx &sql_ctx = pc_ctx.sql_ctx_;
const ObSQLSessionInfo *session_info = sql_ctx.session_info_;
if (OB_ISNULL(pc = get_plan_cache()) || OB_ISNULL(session_info)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid null plan cache or session info", K(ret), K(pc), K(session_info));
} else {
alloc_.set_tenant_id(pc->get_tenant_id());
alloc_.set_ctx_id(ObCtxIds::PLAN_CACHE_CTX_ID);
}
if (OB_SUCC(ret)) {
// set outline_param_idx
outline_param_idx_ = outline_param_idx;
char *buf = NULL;
ObString var_name;
ObSessionVariable sess_var;
fetch_cur_time_ = plan.get_fetch_cur_time();
stmt_type_ = plan.get_stmt_type();
is_ignore_stmt_ = plan.is_ignore();
is_cli_return_rowid_ = session_info->is_client_return_rowid();
//add param info
params_info_.reset();
// set variables for resource map rule
// if rule changed, plan cache will be flush.
resource_map_rule_.deep_copy(pc_ctx.sql_ctx_.resource_map_rule_, alloc_);
if (OB_FAIL(init_pre_calc_exprs(plan, pc_alloc_))) {
LOG_WARN("failed to init pre calc exprs", K(ret));
} else if (OB_FAIL(params_info_.reserve(plan.get_params_info().count()))) {
LOG_WARN("failed to reserve 2d array", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < plan.get_params_info().count(); ++i) {
if (OB_FAIL(params_info_.push_back(plan.get_params_info().at(i)))) {
SQL_PC_LOG(WARN, "fail to push back param info", K(ret));
}
}
need_match_all_params_ = sql_ctx.need_match_all_params_;
// add user session vars if necessary
CK( OB_NOT_NULL(sql_ctx.session_info_) );
if (OB_SUCC(ret) && sql_ctx.related_user_var_names_.count() > 0) {
related_user_var_names_.reset();
related_user_var_names_.set_allocator(&alloc_);
related_user_sess_var_metas_.reset();
related_user_sess_var_metas_.set_allocator(&alloc_);
int64_t N = sql_ctx.related_user_var_names_.count();
OZ( related_user_var_names_.init(N), N );
OZ( related_user_sess_var_metas_.init(N), N );
for (int64_t i = 0; OB_SUCC(ret) && i < sql_ctx.related_user_var_names_.count(); i++) {
buf = (char *)alloc_.alloc(sql_ctx.related_user_var_names_.at(i).length());
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocate memory",
K(ret), K(sql_ctx.related_user_var_names_.at(i).length()));
} else {
MEMCPY(buf, sql_ctx.related_user_var_names_.at(i).ptr(), sql_ctx.related_user_var_names_.at(i).length());
var_name.assign_ptr(buf, sql_ctx.related_user_var_names_.at(i).length());
OC( (related_user_var_names_.push_back)(var_name) );
}
}
ObPCUserVarMeta tmp_meta;
for (int64_t i = 0 ; OB_SUCC(ret) && i < related_user_var_names_.count(); i++) {
if (OB_FAIL(get_variable_meta(pc_ctx.sql_ctx_.session_info_,
related_user_var_names_.at(i), tmp_meta))) {
LOG_WARN("failed to get user variable meta", K(ret),
K(related_user_var_names_.at(i)), K(i));
}
OC( (related_user_sess_var_metas_.push_back)(tmp_meta) );
}
if (OB_FAIL(ret)) {
related_user_var_names_.reset();
related_user_sess_var_metas_.reset();
}
}
// init const param constraints
ObPlanSetType ps_t = get_plan_set_type_by_cache_obj_type(plan.get_ns());
if (PST_PRCD == ps_t) {
// pl does not have any const param constraint
all_possible_const_param_constraints_.reset();
all_plan_const_param_constraints_.reset();
all_equal_param_constraints_.reset();
all_pre_calc_constraints_.reset();
} else if (PST_SQL_CRSR == ps_t) {
// otherwise it should not be empty
CK( OB_NOT_NULL(sql_ctx.all_plan_const_param_constraints_),
OB_NOT_NULL(sql_ctx.all_possible_const_param_constraints_),
OB_NOT_NULL(sql_ctx.all_equal_param_constraints_),
OB_NOT_NULL(sql_ctx.all_pre_calc_constraints_));
OZ( (set_const_param_constraint)(*sql_ctx.all_plan_const_param_constraints_, false) );
OZ( (set_const_param_constraint)(*sql_ctx.all_possible_const_param_constraints_, true) );
OZ( (set_equal_param_constraint)(*sql_ctx.all_equal_param_constraints_) );
OZ( (set_pre_calc_constraint(*sql_ctx.all_pre_calc_constraints_)));
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get an unexpected plan set type", K(ps_t), K(plan.get_ns()));
}
// initialize multi_stmt rowkey pos
if (OB_SUCC(ret) && sql_ctx.multi_stmt_rowkey_pos_.count() > 0) {
if (OB_FAIL(multi_stmt_rowkey_pos_.init(sql_ctx.multi_stmt_rowkey_pos_.count()))) {
LOG_WARN("failed to init array count", K(ret));
} else if (OB_FAIL(append(multi_stmt_rowkey_pos_, sql_ctx.multi_stmt_rowkey_pos_))) {
LOG_WARN("failed to append multi stmt rowkey pos", K(ret));
} else { /*do nothing*/ }
}
if (OB_SUCC(ret) && sql_ctx.is_do_insert_batch_opt()) {
can_skip_params_match_ = can_skip_params_match();
can_delay_init_datum_store_ = can_delay_init_datum_store();
}
}
return ret;
}
int ObPlanSet::set_const_param_constraint(ObIArray<ObPCConstParamInfo> &const_param_constraint,
const bool is_all_constraint)
{
int ret = OB_SUCCESS;
ConstParamConstraint &cons_array = (is_all_constraint ?
all_possible_const_param_constraints_ : all_plan_const_param_constraints_);
cons_array.reset();
cons_array.set_allocator(&alloc_);
if (const_param_constraint.count() > 0) {
if (OB_FAIL(cons_array.prepare_allocate(const_param_constraint.count()))) {
LOG_WARN("failed to init const param constraint array", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < const_param_constraint.count(); i++) {
ObPCConstParamInfo &tmp_info = cons_array.at(i);
tmp_info = const_param_constraint.at(i);
if (tmp_info.const_idx_.count() <= 0 ||
tmp_info.const_params_.count() <= 0 ||
tmp_info.const_idx_.count() != tmp_info.const_params_.count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected const param info", K(tmp_info.const_idx_), K(tmp_info.const_params_));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < tmp_info.const_params_.count(); i++) {
if (tmp_info.const_params_.at(i).need_deep_copy()) {
const ObObj &src_obj = tmp_info.const_params_.at(i);
int64_t deep_cp_size = tmp_info.const_params_.at(i).get_deep_copy_size();
int64_t pos = 0;
char *tmp_buf = NULL;
if (OB_ISNULL(tmp_buf = (char *)alloc_.alloc(deep_cp_size))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocate mem", K(ret));
} else if (OB_FAIL(tmp_info.const_params_.at(i).deep_copy(src_obj, tmp_buf, deep_cp_size, pos))) {
LOG_WARN("failed to deep copy obj", K(ret));
} else if (pos != deep_cp_size) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("deep copy went wrong", K(ret));
} else {
// do nothing
}
}
} // for end
}
} // for end
}
}
if (OB_FAIL(ret)) {
cons_array.reset();
}
return ret;
}
int ObPlanSet::set_equal_param_constraint(common::ObIArray<ObPCParamEqualInfo> &equal_param_constraint)
{
int ret = OB_SUCCESS;
all_equal_param_constraints_.reset();
all_equal_param_constraints_.set_allocator(&alloc_);
if (equal_param_constraint.empty()) {
//do nothing
} else if (OB_FAIL(all_equal_param_constraints_.init(equal_param_constraint.count()))) {
LOG_WARN("failed to init equal param constraint array", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < equal_param_constraint.count(); ++i) {
ObPCParamEqualInfo &equal_info = equal_param_constraint.at(i);
if (equal_info.first_param_idx_ < 0 || equal_info.second_param_idx_ < 0 ||
equal_info.first_param_idx_ > params_info_.count() ||
equal_info.second_param_idx_ > params_info_.count() ||
equal_info.first_param_idx_ == equal_info.second_param_idx_) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get invalid equal param constraint", K(ret), K(equal_info));
} else if (OB_FAIL(all_equal_param_constraints_.push_back(equal_info))) {
LOG_WARN("failed to push back equal param info", K(ret));
}
}
return ret;
}
// adds pre calc constraint
int ObPlanSet::set_pre_calc_constraint(common::ObDList<ObPreCalcExprConstraint> &pre_calc_cons)
{
int ret = OB_SUCCESS;
ObPreCalcExprConstraint *pre_calc_constraint = NULL;
void *cons_buf = NULL;
DLIST_FOREACH(cur_cons, pre_calc_cons) {
if (OB_ISNULL(cons_buf = alloc_.alloc(sizeof(ObPreCalcExprConstraint)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocate memory", K(ret));
} else {
pre_calc_constraint = new(cons_buf)ObPreCalcExprConstraint(alloc_);
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(pre_calc_constraint->assign(*cur_cons, alloc_))) {
LOG_WARN("failed to deep copy pre calculable expression constriants", K(*cur_cons), K(ret));
} else if (OB_UNLIKELY(!all_pre_calc_constraints_.add_last(pre_calc_constraint))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("failed to add element to dlist", K(ret));
}
}
return ret;
}
// match actually constraint
int ObPlanSet::match_cons(const ObPlanCacheCtx &pc_ctx, bool &is_matched)
{
int ret = OB_SUCCESS;
ObIArray<ObPCConstParamInfo> *param_cons = pc_ctx.sql_ctx_.all_plan_const_param_constraints_;
ObIArray<ObPCConstParamInfo> *possible_param_cons =
pc_ctx.sql_ctx_.all_possible_const_param_constraints_;
ObIArray<ObPCParamEqualInfo> *equal_cons = pc_ctx.sql_ctx_.all_equal_param_constraints_;
is_matched = true;
if (OB_ISNULL(param_cons) ||
OB_ISNULL(possible_param_cons) ||
OB_ISNULL(equal_cons)) {
is_matched = false;
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(param_cons), K(possible_param_cons), K(equal_cons));
} else if (param_cons->count() != all_plan_const_param_constraints_.count() ||
possible_param_cons->count() != all_possible_const_param_constraints_.count() ||
equal_cons->count() != all_equal_param_constraints_.count()) {
is_matched = false;
} else {
for (int64_t i=0; is_matched && i < all_plan_const_param_constraints_.count(); i++) {
is_matched = (all_plan_const_param_constraints_.at(i)==param_cons->at(i));
}
for (int64_t i=0; is_matched && i < all_possible_const_param_constraints_.count(); i++) {
is_matched = (all_possible_const_param_constraints_.at(i)==possible_param_cons->at(i));
}
for (int64_t i=0; is_matched && i < all_equal_param_constraints_.count(); i++) {
is_matched = (all_equal_param_constraints_.at(i)==equal_cons->at(i));
}
}
return ret;
}
// Constant constraint check logic:
// 1. all_plan_const_param_constraints_ is not empty, check if the constraints of all_plan_const_param_constraints_ are satisfied,
// Satisfy then hit plan_set, otherwise not hit;
// 2. Otherwise, check all possible constant constraints, if one of the constraints is satisfied, then a new plan needs to be generated, that is, it does not hit, otherwise it hits
// 3. Check if the parameter constraints that require equality are satisfied
int ObPlanSet::match_constraint(const ParamStore ¶ms, bool &is_matched)
{
int ret = OB_SUCCESS;
is_matched = true;
if (all_plan_const_param_constraints_.count() > 0) { // check all_plan_const_param_constraints_ first
for (int64_t i = 0; is_matched && OB_SUCC(ret) && i < all_plan_const_param_constraints_.count(); i++) {
const ObPCConstParamInfo &const_param_info = all_plan_const_param_constraints_.at(i);
CK( const_param_info.const_idx_.count() > 0,
const_param_info.const_params_.count() > 0,
const_param_info.const_idx_.count() == const_param_info.const_params_.count() );
for (int64_t j = 0; is_matched && OB_SUCC(ret) && j < const_param_info.const_idx_.count(); j++) {
const int64_t param_idx = const_param_info.const_idx_.at(j);
const ObObj &const_param = const_param_info.const_params_.at(j);
if (param_idx >= params.count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get an unexpected param index", K(ret), K(param_idx), K(params.count()));
} else if (const_param.is_invalid_type() ||
params.at(param_idx).is_invalid_type()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected invalid type",
K(ret), K(const_param.get_type()), K(params.at(param_idx).get_type()));
} else if (!const_param.can_compare(params.at(param_idx)) ||
0 != const_param.compare(params.at(param_idx))) {
LOG_TRACE("not matched const param", K(const_param), K(params.at(param_idx)));
is_matched = false;
} else {
// do nothing
}
}
}
} else if (all_possible_const_param_constraints_.count() > 0) {
// check if possible generated column exists
for (int64_t i = 0; is_matched && OB_SUCC(ret) && i < all_possible_const_param_constraints_.count(); i++) {
bool match_const = true;
const ObPCConstParamInfo &const_param_info = all_possible_const_param_constraints_.at(i);
CK( const_param_info.const_idx_.count() > 0,
const_param_info.const_params_.count() > 0,
const_param_info.const_idx_.count() == const_param_info.const_params_.count() );
for (int64_t j = 0; match_const && OB_SUCC(ret) && j < const_param_info.const_idx_.count(); j++) {
const int64_t param_idx = const_param_info.const_idx_.at(j);
const ObObj &const_param = const_param_info.const_params_.at(j);
if (param_idx >= params.count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get an unexpected param index", K(ret), K(param_idx), K(params.count()));
} else if (const_param.is_invalid_type() ||
params.at(param_idx).is_invalid_type()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected invalid type",
K(ret), K(const_param.get_type()), K(params.at(param_idx).get_type()));
} else if (!const_param.can_compare(params.at(param_idx)) ||
0 != const_param.compare(params.at(param_idx))) {
match_const = false;
} else {
// do nothing
}
}
if (match_const) {
LOG_TRACE("matched const param constraint", K(params), K(all_possible_const_param_constraints_.at(i)));
is_matched = false; // matching one of the constraint, need to generated new plan
}
}
} else {
// do nothing
}
for (int64_t i = 0; is_matched && OB_SUCC(ret) && i < all_equal_param_constraints_.count(); ++i) {
int64_t first_idx = all_equal_param_constraints_.at(i).first_param_idx_;
int64_t second_idx = all_equal_param_constraints_.at(i).second_param_idx_;
common::ObObjParam param1 = params.at(first_idx);
common::ObObjParam param2 = params.at(second_idx);
param1.set_collation_type(CS_TYPE_BINARY);
param2.set_collation_type(CS_TYPE_BINARY);
if (OB_UNLIKELY(first_idx < 0 || first_idx >= params.count() ||
second_idx < 0 || second_idx >= params.count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("param index is invalid", K(ret), K(params.count()), K(first_idx), K(second_idx));
} else if (!all_equal_param_constraints_.at(i).use_abs_cmp_ &&
param1.can_compare(param2) &&
param1.get_collation_type() == param2.get_collation_type()) {
is_matched = (0 == param1.compare(param2));
} else if (all_equal_param_constraints_.at(i).use_abs_cmp_) {
/*
* for plan like: select ? from t1 group by grouping sets (c1, ?);
* if plan has absequal constraint,
* then both "select -1 from t1 group by grouping sets (c1, 1);"
* and "select 1 from t1 group by grouping sets (c1, 1);"
* will hit the plan.
* but "select -2 from t1 group by grouping sets (c1, 1);" won't hit the plan.
*/
if (param1.is_number() && param2.is_number()) {
is_matched = (0 == param1.get_number().abs_compare(param2.get_number()));
} else if (param1.is_double() && param2.is_double()) {
is_matched = (0 == param1.get_double() + param2.get_double()) ||
(param1.get_double() == param2.get_double());
} else if (param1.is_float() && param2.is_float()) {
is_matched = (0 == param1.get_float() + param2.get_float()) ||
(param1.get_float() == param2.get_float());
} else if (param1.is_decimal_int() && param2.is_decimal_int()) {
is_matched = wide::abs_equal(param1, param2);
} else if (param1.can_compare(param2) &&
param1.get_collation_type() == param2.get_collation_type()) {
is_matched = (0 == param1.compare(param2));
}
}
if (OB_SUCC(ret) && !is_matched) {
is_matched = false;
LOG_TRACE("not match equal param constraint", K(params), K(first_idx), K(second_idx));
}
}
if (OB_FAIL(ret)) {
is_matched = false;
}
return ret;
}
int ObPlanSet::init_pre_calc_exprs(const ObPlanCacheObject &phy_plan,
common::ObIAllocator* pc_alloc_)
{
int ret = OB_SUCCESS;
void *buf = NULL;
if (phy_plan.get_pre_calc_frames().get_size() == 0) {
// have no pre calculable expression, not initialize pre calculable expression handle.
pre_cal_expr_handler_ = NULL;
} else if (OB_ISNULL(pc_alloc_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("plan cache allocator has not been initialized.");
} else if(OB_ISNULL( buf = pc_alloc_->alloc(sizeof(PreCalcExprHandler)))) {