forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_plan_set.cpp
More file actions
2415 lines (2298 loc) · 93.5 KB
/
Copy pathob_plan_set.cpp
File metadata and controls
2415 lines (2298 loc) · 93.5 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) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL_PC
#include "sql/plan_cache/ob_plan_set.h"
#include "lib/trace/ob_trace_event.h"
#include "lib/number/ob_number_v2.h"
#include "common/ob_role.h"
#include "observer/ob_server_struct.h"
#include "sql/ob_phy_table_location.h"
#include "sql/ob_sql_utils.h"
#include "sql/ob_sql_context.h"
#include "sql/ob_sql_trans_control.h"
#include "sql/plan_cache/ob_plan_cache.h"
#include "sql/plan_cache/ob_pcv_set.h"
#include "sql/plan_cache/ob_plan_cache_value.h"
#include "sql/plan_cache/ob_cache_object.h"
#include "sql/plan_cache/ob_dist_plans.h"
#include "sql/privilege_check/ob_ora_priv_check.h"
#include "sql/optimizer/ob_log_plan.h"
#include "sql/engine/ob_physical_plan.h"
#include "sql/engine/ob_exec_context.h"
#include "sql/plan_cache/ob_cache_object_factory.h"
#include "pl/ob_pl.h"
#include "ob_plan_set.h"
#include "share/resource_manager/ob_resource_manager.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
{
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;
}
}
}
//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 {
//匹配原始的参数
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));
}
}
// 匹配相关的用户session变量
// 这里应该先进行related_user_var_names_跟session_info里面变量的比较,再进行预计算
// 否则会导致session var类型改变后, 无法匹配上计划. 举例如下
// 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(因为填的是匹配sql2 plan时预计算的结果)
// 结果: sql5无法匹配上sql4的计划
// 原因: sql5匹配sql2的计划时先预计算,得到int obj,再比较related_user_var_names_
// 发现session_info_里面的sess_var为varchar,匹配失败.
// sql5再次匹配sql4的计划,由于已经预计算,不再进行计算,所以ParamStore
// 里面还是int obj,而params_info_中Obj为varchar,匹配失败.
//
// 所以应该改为先比较related_user_var_names是否和sess_var相同,再进行预计算
// 就不会导致匹配时,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_;
for (int64_t i = 0 ; OB_SUCC(ret) && is_same && i < related_user_var_names_.count(); i++) {
if (OB_FAIL(session_info->get_user_variable(related_user_var_names_.at(i), sess_var))) {
LOG_WARN("failed to get user variable", K(ret), K(related_user_var_names_.at(i)), K(i));
} else {
ObPCUserVarMeta tmp_meta(sess_var);
is_same = (related_user_sess_var_metas_.at(i) == tmp_meta);
}
}
}
}
// privilege
if (OB_SUCC(ret) && is_same && all_priv_constraints_.count() > 0) {
if (OB_FAIL(match_priv_cons(pc_ctx, is_same))) {
LOG_WARN("failed to check privilege constraint", K(ret));
}
}
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;
}
}
}
}
//匹配true/false,此时的params中flag_是初始化的值, 不能直接使用。
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;
}
//匹配参数类型信息
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_)
|| (is_sql_planset && lib::is_oracle_mode() &&
(param_info.type_ == ObTinyIntType || param.get_type() == ObTinyIntType))) {
if (lib::is_oracle_mode() &&
param.get_param_meta().get_type() == ObCharType &&
param.get_type() == ObNullType) {
// in oracle mode, empty string represents null
// and therefore meta_'s type (ObCharType) is inconsistent
// with param's type.
} else 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_) {
is_same = false;
} else if (param.get_param_meta().get_type() != param_info.type_) {
is_same = false;
} else if (param.is_ext()) {
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_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_info.is_oracle_empty_string_ && !param.is_null()) { //普通字符串不匹配空串的计划
is_same = false;
} else if (ObSQLUtils::is_oracle_empty_string(param)
&&!param_info.is_oracle_empty_string_) { //空串不匹配普通字符串的计划
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;
}
// 匹配真/假参数
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;
}
int ObPlanSet::match_priv_cons(ObPlanCacheCtx &pc_ctx, bool &is_matched)
{
int ret = OB_SUCCESS;
is_matched = true;
bool has_priv = false;
ObSQLSessionInfo *session_info = pc_ctx.sql_ctx_.session_info_;
ObSchemaGetterGuard *schema_guard = pc_ctx.sql_ctx_.schema_guard_;
if (OB_ISNULL(session_info) || OB_ISNULL(schema_guard)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && is_matched && i < all_priv_constraints_.count(); ++i) {
const ObPCPrivInfo &priv_info = all_priv_constraints_.at(i);
bool has_priv = false;
if (OB_FAIL(ObOraSysChecker::check_ora_user_sys_priv(*schema_guard,
session_info->get_effective_tenant_id(),
session_info->get_priv_user_id(),
session_info->get_database_name(),
priv_info.sys_priv_,
session_info->get_enable_role_array()))) {
if (OB_ERR_NO_PRIVILEGE == ret) {
ret = OB_SUCCESS;
LOG_DEBUG("lack sys privilege", "priv_type", all_priv_constraints_.at(i).sys_priv_);
} else {
LOG_WARN("failed to check ora user sys priv", K(ret));
}
} else {
has_priv = true;
}
if (OB_SUCC(ret)) {
is_matched = priv_info.has_privilege_ == has_priv;
}
}
return ret;
}
//判断多组参数中同一列参数的是否均为true/false, 并返回第一个参数是true/false
int ObPlanSet::check_vector_param_same_bool(const ObObjParam ¶m_obj,
bool &first_val,
bool &is_same)
{
int ret = OB_SUCCESS;
is_same = true;
if (param_obj.is_ext_sql_array()) {
const ObSqlArrayObj *array_params = reinterpret_cast<const ObSqlArrayObj*>(param_obj.get_ext());
if (OB_ISNULL(array_params)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("array params is null", K(ret));
} else if (OB_ISNULL(array_params->data_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("array data is null", K(ret), KPC(array_params));
} else {
int64_t count = array_params->count_;
if (count > 0) {
bool is_value_true = true;
for (int64_t param_idx = 0; OB_SUCC(ret) && is_same && param_idx < count; param_idx++) {
if (OB_FAIL(ObObjEvaluator::is_true(array_params->data_[param_idx], is_value_true))) {
SQL_PC_LOG(WARN, "fail to get param info", K(ret));
} else if (param_idx == 0) {
first_val = is_value_true;
} else if (first_val != is_value_true) {
is_same = false;
}
} // for end
}
}
}
return ret;
}
/*//判断param store中array参数的每个obj是否恒真/假*/
//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()) {
////检查每一组参数的结果是否为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_;
ObSessionVariable sess_var;
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();
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(session_info->get_user_variable(related_user_var_names_.at(i),
sess_var))) {
LOG_WARN("failed to get user variable", K(ret), K(sess_var));
} else {
ObPCUserVarMeta tmp_meta(sess_var);
is_same = (tmp_meta == related_user_sess_var_metas_.at(i));
}
}
}
}
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();
all_priv_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())) {
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.
res_map_rule_id_ = pc_ctx.sql_ctx_.res_map_rule_id_;
res_map_rule_param_idx_ = pc_ctx.sql_ctx_.res_map_rule_param_idx_;
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) );
}
}
for (int64_t i = 0; OB_SUCC(ret) && i < related_user_var_names_.count(); i++) {
OZ( sql_ctx.session_info_->get_user_variable(related_user_var_names_.at(i),
sess_var),
ret,
related_user_var_names_.at(i),
i );
OC( (related_user_sess_var_metas_.push_back)(ObPCUserVarMeta(sess_var)) );
}
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();
all_priv_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_),
OB_NOT_NULL(sql_ctx.all_priv_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_)));
OZ( (set_priv_constraint(*sql_ctx.all_priv_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 (PRE_CALC_ROWID == cur_cons->expect_result_) {
if (OB_ISNULL(cons_buf = alloc_.alloc(sizeof(ObRowidConstraint)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocate memory", K(ret));
} else {
pre_calc_constraint = new(cons_buf)ObRowidConstraint(alloc_);
}
} else {
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;
}
// add priv constraint
int ObPlanSet::set_priv_constraint(common::ObIArray<ObPCPrivInfo> &priv_constraint)
{
int ret = OB_SUCCESS;
all_priv_constraints_.reset();
all_priv_constraints_.set_allocator(&alloc_);
if (priv_constraint.empty()) {
//do nothing
} else if (OB_FAIL(all_priv_constraints_.init(priv_constraint.count()))) {
LOG_WARN("failed to init privilege constraint array", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < priv_constraint.count(); ++i) {
const ObPCPrivInfo &priv_info = priv_constraint.at(i);
if (OB_UNLIKELY(!(priv_info.sys_priv_ > PRIV_ID_NONE && priv_info.sys_priv_ < PRIV_ID_MAX))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid priv type", K(priv_info), K(ret));
} else if (OB_FAIL(all_priv_constraints_.push_back(priv_info))) {
LOG_WARN("failed to push back priv info");
}
}
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_;
ObIArray<ObPCPrivInfo> *priv_cons = pc_ctx.sql_ctx_.all_priv_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() ||
priv_cons->count() != all_priv_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));
}
for (int64_t i=0; is_matched && i < all_priv_constraints_.count(); i++) {
is_matched = (all_priv_constraints_.at(i)==priv_cons->at(i));
}
}
return ret;
}
// 常量约束的检查逻辑:
// 1. all_plan_const_param_constraints_不为空,检查all_plan_const_param_constraints_的约束是否满足,
// 满足则命中plan_set,否则不命中;
// 2. 否则,检查所有可能的常量约束,如果某一个约束被满足,那么需要生成新的计划,也即不命中,否则命中
// 3. 检查要求相等的参数约束是否被满足
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++) {