forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_insert_log_plan.cpp
More file actions
1879 lines (1831 loc) · 95 KB
/
Copy pathob_insert_log_plan.cpp
File metadata and controls
1879 lines (1831 loc) · 95 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_OPT
#include "sql/optimizer/ob_log_select_into.h"
#include "sql/optimizer/ob_insert_log_plan.h"
#include "sql/optimizer/ob_log_expr_values.h"
#include "sql/optimizer/ob_log_insert.h"
#include "sql/resolver/dml/ob_del_upd_resolver.h"
#include "sql/rewrite/ob_transform_utils.h"
#include "share/stat/ob_dbms_stats_utils.h"
using namespace oceanbase;
using namespace sql;
using namespace oceanbase::common;
using namespace oceanbase::share::schema;
using namespace oceanbase::sql::log_op_def;
int ObInsertLogPlan::generate_normal_raw_plan()
{
int ret = OB_SUCCESS;
const ObInsertStmt *insert_stmt = get_stmt();
if (OB_ISNULL(insert_stmt) || OB_ISNULL(get_optimizer_context().get_query_ctx())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(insert_stmt), K(ret));
} else {
LOG_TRACE("start to allocate operators for ", "sql", get_optimizer_context().get_query_ctx()->get_sql_stmt());
OPT_TRACE("generate plan for ", get_stmt());
if (!insert_stmt->value_from_select()) {
// insert into values xxxx
ObLogicalOperator *top = NULL;
if (insert_stmt->has_sequence() &&
OB_FAIL(allocate_sequence_as_top(top))) {
LOG_WARN("failed to allocate sequence as top", K(ret));
} else if (OB_FAIL(allocate_insert_values_as_top(top))) {
LOG_WARN("failed to allocate expr values as top", K(ret));
} else if (OB_FAIL(make_candidate_plans(top))) {
LOG_WARN("failed to make candidate plans", K(ret));
} else { /*do nothing*/ }
} else {
// insert into select xxx
if (OB_FAIL(generate_plan_tree())) {
LOG_WARN("failed to generate plan tree", K(ret));
} else if (insert_stmt->has_sequence() &&
OB_FAIL(candi_allocate_sequence())) {
LOG_WARN("failed to allocate sequence", K(ret));
} else { /*do nothing*/}
}
// allocate subplan filter for "INSERT .. ON DUPLICATE KEY UPDATE c1 = (select...)"
if (OB_SUCC(ret) && insert_stmt->is_insert_up()) {
ObSEArray<ObRawExpr*, 4> subquery;
ObSEArray<ObRawExpr*, common::OB_PREALLOCATED_NUM> assign_exprs;
bool contain = false;
if (OB_FAIL(insert_stmt->get_assignments_exprs(assign_exprs))) {
LOG_WARN("failed to get assignment exprs", K(ret));
} else if (OB_FAIL(ObOptimizerUtil::get_subquery_exprs(assign_exprs, subquery))) {
LOG_WARN("failed to get subqueries", K(ret)) ;
} else if (OB_FAIL(check_contain_non_onetime_expr(subquery, contain))) {
LOG_WARN("check contain non onetime expr", K(ret));
} else if (contain) {
ret = OB_NOT_SUPPORTED;
LOG_USER_ERROR(OB_NOT_SUPPORTED, "update values contain non onetime subquery");
LOG_WARN("update values contain non onetime subquery", K(ret));
} else if (OB_FAIL(candi_allocate_subplan_filter_for_assignments(assign_exprs))) {
LOG_WARN("failed to allocate subplan filter for assignments", K(ret));
} else { /*do nothing*/ }
}
bool need_osg = false;
OSGShareInfo *osg_info = NULL;
double online_sample_percent = 100.;
if (OB_SUCC(ret)) {
if (OB_FAIL(check_use_direct_load())) {
LOG_WARN("failed to check use direct load", K(ret));
} else if (OB_FAIL(prepare_dml_infos())) {
LOG_WARN("failed to prepare dml infos", K(ret));
} else if (OB_FAIL(compute_dml_parallel())) {
LOG_WARN("failed to compute dml parallel", K(ret));
}
if (OB_SUCC(ret)) {
bool tmp_need_osg = false;
if (OB_FAIL(check_need_online_stats_gather(tmp_need_osg))) {
LOG_WARN("fail to check wether we need optimizer stats gathering operator", K(ret));
} else if (tmp_need_osg &&
OB_FAIL(get_online_estimate_percent(online_sample_percent))) {
LOG_WARN("failed to get sys online sample percent", K(ret));
} else {
if (get_optimizer_context().get_direct_load_optimizer_ctx().use_direct_load()) {
get_optimizer_context().get_exec_ctx()->get_table_direct_insert_ctx()
.set_is_online_gather_statistics(tmp_need_osg);
get_optimizer_context().get_exec_ctx()->get_table_direct_insert_ctx()
.set_online_sample_percent(online_sample_percent);
} else {
need_osg = tmp_need_osg;
}
}
}
if (OB_FAIL(ret)) {
//pass
} else if (need_osg && OB_FAIL(generate_osg_share_info(osg_info))) {
LOG_WARN("failed to generate osg share info");
} else if (need_osg && OB_ISNULL(osg_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null");
} else if (need_osg) {
osg_info->online_sample_rate_ = online_sample_percent;
}
}
if (OB_SUCC(ret)) {
TableItem *insert_table = NULL;
if (OB_ISNULL(insert_table = insert_stmt->get_table_item_by_id(insert_stmt->get_insert_table_info().table_id_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("insert target table is unexpected null", K(ret));
} else if (schema::EXTERNAL_TABLE == insert_table->table_type_) {
if (OB_FAIL(candi_allocate_select_into_for_insert())) {
LOG_WARN("failed to allocate select into op", K(ret));
} else {
LOG_TRACE("succeed to allocate select into clause", K(candidates_.candidate_plans_.count()));
}
} else if (use_pdml()) {
if (OB_FAIL(candi_allocate_pdml_insert(osg_info))) {
LOG_WARN("failed to allocate pdml insert", K(ret));
} else {
LOG_TRACE("succeed to allocate pdml insert operator",
K(candidates_.candidate_plans_.count()));
}
} else if (OB_FAIL(candi_allocate_insert(osg_info))) {
LOG_WARN("failed to allocate insert operator", K(ret));
} else {
LOG_TRACE("succeed to allocate insert operator", K(candidates_.candidate_plans_.count()));
}
}
if (OB_SUCC(ret) && insert_stmt->get_returning_aggr_item_size() > 0) {
if (OB_FAIL(candi_allocate_scala_group_by(insert_stmt->get_returning_aggr_items()))) {
LOG_WARN("failed to allocate scalar group by", K(ret));
} else {
LOG_TRACE("succeed to allocate group by operator",
K(candidates_.candidate_plans_.count()));
}
}
/* if the plan is pdml or parallel select, should allocate a OSG above insert. This OSG is used to merge
* all information collection by other OSG.
*/
if (OB_SUCC(ret) && need_osg) {
if (OB_FAIL(candi_allocate_optimizer_stats_merge(osg_info))) {
LOG_WARN("fail to allcate osg on top", K(ret));
} else {
LOG_TRACE("succeed to allocate optimizer stat merge",
K(candidates_.candidate_plans_.count()));
}
}
//allocate temp-table transformation if needed.
if (OB_SUCC(ret) && !get_optimizer_context().get_temp_table_infos().empty() && is_final_root_plan()) {
if (OB_FAIL(candi_allocate_temp_table_transformation())) {
LOG_WARN("failed to allocate transformation operator", K(ret));
} else {
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(candi_allocate_root_exchange())) {
LOG_WARN("failed to allocate root exchange", K(ret));
} else {
LOG_TRACE("succeed to allocate root operator",
K(candidates_.candidate_plans_.count()));
}
}
}
return ret;
}
int ObInsertLogPlan::get_index_part_ids(const ObInsertTableInfo& table_info, const ObTableSchema *&data_table_schema, const ObTableSchema *&index_schema, ObIArray<uint64_t> &index_part_ids)
{
int ret = OB_SUCCESS;
common::hash::ObHashSet<int64_t> data_part_id_set;
index_part_ids.reset();
if (OB_UNLIKELY(OB_ISNULL(data_table_schema) || OB_ISNULL(index_schema))) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("the parameters is invalid", K(ret), K(table_info));
} else if (table_info.part_ids_.count() > 0) {
const ObPartitionOption &data_part_option = data_table_schema->get_part_option();
ObPartition **data_partitions = data_table_schema->get_part_array();
ObPartition **index_partitions = index_schema->get_part_array();
const ObPartitionLevel part_level = data_table_schema->get_part_level();
if (OB_ISNULL(data_partitions)) {
ret = OB_PARTITION_NOT_EXIST;
LOG_WARN("data table part array is null", K(ret));
} else if (OB_ISNULL(index_partitions)) {
ret = OB_PARTITION_NOT_EXIST;
LOG_WARN("index table part array is null", K(ret));
} else {
uint64_t part_ids_count = table_info.part_ids_.count();
if (OB_FAIL(data_part_id_set.create(part_ids_count))) {
LOG_WARN("fail to create data part id set", K(ret), K(part_ids_count));
}
for (int64_t i = 0; i < part_ids_count && OB_SUCC(ret); i++) {
if (OB_FAIL(data_part_id_set.set_refactored(table_info.part_ids_.at(i), true/*overwrite*/))) {
LOG_WARN("fail to set refactored", K(ret), K(table_info.part_ids_.at(i)), K(table_info.part_ids_));
}
}
for (int64_t i = 0; i < data_part_option.get_part_num() && OB_SUCC(ret); ++i) {
if (OB_ISNULL(data_partitions[i]) || OB_ISNULL(index_partitions[i])) {
ret = OB_PARTITION_NOT_EXIST;
LOG_WARN("NULL ptr", K(ret), K(i));
} else if (PARTITION_LEVEL_ONE == part_level) {
int64_t data_part_id = data_partitions[i]->get_part_id();
if (OB_UNLIKELY(OB_HASH_EXIST == data_part_id_set.exist_refactored(data_part_id))) {
if (OB_FAIL(index_part_ids.push_back(index_partitions[i]->get_part_id()))) {
LOG_WARN("push back error", K(ret), K(index_partitions[i]->get_part_id()));
}
}
} else if (PARTITION_LEVEL_TWO == part_level) {
ObSubPartition **data_subpart_array = data_partitions[i]->get_subpart_array();
ObSubPartition **index_subpart_array = index_partitions[i]->get_subpart_array();
int64_t subpart_num = index_partitions[i]->get_subpartition_num();
if (OB_ISNULL(data_subpart_array) || OB_ISNULL(index_subpart_array)) {
ret = OB_PARTITION_NOT_EXIST;
LOG_WARN("part array is null", K(ret), K(i));
} else if (OB_UNLIKELY(subpart_num < 1)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("sub part num less than 1", K(ret), K(subpart_num));
} else {
for (int64_t j = 0; j < subpart_num && OB_SUCC(ret); j++) {
int64_t data_part_id = data_subpart_array[j]->get_sub_part_id();
if (OB_UNLIKELY(OB_HASH_EXIST == data_part_id_set.exist_refactored(data_part_id))) {
if (OB_FAIL(index_part_ids.push_back(index_subpart_array[j]->get_sub_part_id()))) {
LOG_WARN("push back error", K(ret), K(index_subpart_array[j]->get_sub_part_id()));
}
}
}
}
}
}
}
}
return ret;
}
int ObInsertLogPlan::generate_osg_share_info(OSGShareInfo *&info)
{
int ret = OB_SUCCESS;
const ObInsertStmt *stmt = NULL;
const ObTableSchema *tab_schema = NULL;
ObSqlSchemaGuard *schema_guard = NULL;
if (OB_ISNULL(schema_guard = get_optimizer_context().get_sql_schema_guard()) ||
OB_UNLIKELY(!get_stmt()->is_insert_stmt())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_ISNULL(info = OB_NEWx(OSGShareInfo, (&get_allocator())))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocate memory");
} else {
stmt = static_cast<const ObInsertStmt *>(get_stmt());
const ObInsertTableInfo& table_info = stmt->get_insert_table_info();
uint64_t table_id = table_info.table_id_;
uint64_t ref_table_id = table_info.ref_table_id_;
if (OB_FAIL(schema_guard->get_table_schema(table_id, ref_table_id, stmt, tab_schema))) {
LOG_WARN("fail to get table schema", K(ref_table_id), K(tab_schema), K(ret));
} else if (OB_ISNULL(tab_schema)) {
ret = OB_TABLE_NOT_EXIST;
LOG_WARN("get unexpected null pointer", K(ret));
} else {
const ObColumnSchemaV2 *col_schema = NULL;
ObSEArray<uint64_t, 4> generated_column_ids;
info->table_id_ = ref_table_id;
if (tab_schema->is_partitioned_table()) {
info->part_level_ = tab_schema->get_part_level();
if (OB_FAIL(gen_calc_part_id_expr(table_info.loc_table_id_,
ref_table_id,
CALC_PARTITION_TABLET_ID,
info->calc_part_id_expr_))) {
LOG_WARN("failed to init calc part id", K(ret));
} else if (OB_FAIL(ObOptimizerUtil::replace_column_with_select_for_partid(stmt,
get_optimizer_context(),
info->calc_part_id_expr_))) {
// using the select column/values expr to calc partid.
LOG_WARN("fail to replace column item with select item", K(ret));
} else {
LOG_TRACE("success to generate calc_part expr", K(ret), K(info->calc_part_id_expr_));
}
}
// column conv;
for (int64_t i = 0; OB_SUCC(ret) && i < table_info.column_exprs_.count(); i++) {
ObColumnRefRawExpr *tmp_col = table_info.column_exprs_.at(i);
ObRawExpr *col_conv_expr = table_info.column_conv_exprs_.at(i);
if (OB_ISNULL(tmp_col)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null pointer", K(ret));
} else if (OB_FAIL(ObTransformUtils::get_base_column(stmt, tmp_col))) {
LOG_WARN("fail to get base column", K(ret));
} else {
// since the column_exprs may be a generated_table's column. e.g., insert into (select c2, c1 from t1) values (1,1);
// for t1, the column_ids of c1 and c2 are 16 and 17. However, in the insert stmt, the column c2 in subquery is 16.
// we need to get the real column id.
col_schema = tab_schema->get_column_schema(tmp_col->get_column_id());
if (OB_ISNULL(col_schema)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("can't get column schema", K(ret));
} else if (!pl::ObDbmsStats::check_column_validity(*tab_schema, *col_schema)) {
// do not gather stats for auto inc column.
// continue, shouldn't add these to osg's output.
} else if (!col_schema->is_generated_column()) {
if (OB_FAIL(info->col_conv_exprs_.push_back(col_conv_expr))) {
LOG_WARN("fail to push back column convert expr", K(ret));
} else if (OB_FAIL(info->column_ids_.push_back(tmp_col->get_column_id()))) {
LOG_WARN("fail to push back column ids", K(ret));
}
} else {
// generated column: no need to replace column expr with select_item in select clause. since this work has already done.
if (OB_FAIL(info->generated_column_exprs_.push_back(col_conv_expr))) {
LOG_WARN("fail to add generated column expr", K(ret));
} else if (OB_FAIL(generated_column_ids.push_back(tmp_col->get_column_id()))) {
LOG_WARN("fail to push back column ids", K(ret));
}
}
}
} // end for
if (OB_SUCC(ret)) {
// column ids of generated column should be add at the tail.
if (OB_FAIL(append(info->column_ids_, generated_column_ids))) {
LOG_WARN("fail to append column ids", K(ret));
}
}
}
}
return ret;
}
int ObInsertLogPlan::check_contain_non_onetime_expr(const ObRawExpr *expr, bool &contain)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr is null", K(ret));
} else if (expr->has_flag(IS_SUB_QUERY)) {
if (!ObOptimizerUtil::find_item(get_onetime_query_refs(), expr)) {
contain = true;
}
} else if (expr->has_flag(CNT_SUB_QUERY)) {
if (ObOptimizerUtil::find_item(get_onetime_query_refs(), expr)) {
//do nothing
} else {
for (int64_t i = 0; OB_SUCC(ret) && !contain && i < expr->get_param_count(); i++) {
if (OB_FAIL(SMART_CALL(check_contain_non_onetime_expr(expr->get_param_expr(i), contain)))) {
LOG_WARN("check contain non one time expr failed", K(ret));
}
}
}
}
return ret;
}
int ObInsertLogPlan::check_contain_non_onetime_expr(const ObIArray<ObRawExpr *> &exprs, bool &contain)
{
int ret = OB_SUCCESS;
contain = false;
for (int64_t i = 0; OB_SUCC(ret) && !contain && i < exprs.count(); i++) {
if (OB_FAIL(check_contain_non_onetime_expr(exprs.at(i), contain))) {
LOG_WARN("check contain non one time expr failed", K(ret));
}
}
return ret;
}
int ObInsertLogPlan::check_need_online_stats_gather(bool &need_osg)
{
int ret = OB_SUCCESS;
need_osg = false;
bool online_sys_var = false;
bool need_gathering = true;
ObObj online_sys_var_obj;
const ObInsertStmt *insert_stmt = NULL;
TableItem *ins_table = NULL;
bool disable_pdml = false;
bool is_pk_auto_inc = false;
if (OB_ISNULL(insert_stmt = get_stmt()) ||
OB_ISNULL(ins_table = insert_stmt->get_table_item_by_id(insert_stmt->get_insert_table_info().table_id_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null pointer", K(ret), K(insert_stmt->get_insert_table_info()));
} else if (OB_UNLIKELY(ins_table->is_system_table_ || ins_table->is_index_table_)
|| insert_stmt->is_insert_up()
|| !insert_stmt->value_from_select()
|| (!get_optimizer_context().get_session_info()->is_user_session())) {
need_gathering = false;
} else if (OB_FAIL(insert_stmt->check_pdml_disabled(get_optimizer_context().is_online_ddl(),
disable_pdml, is_pk_auto_inc))) {
LOG_WARN("fail to check pdml disable for insert stmt", K(ret));
} else if (disable_pdml) {
need_gathering = false;
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(get_optimizer_context().get_session_info()->get_sys_variable(share::SYS_VAR__OPTIMIZER_GATHER_STATS_ON_LOAD,
online_sys_var_obj))) {
LOG_WARN("fail to get sys var", K(ret));
} else {
online_sys_var = online_sys_var_obj.get_bool();
// shouldn't gather stats if the stmt is insert update.
// if the online_opt_stat_gather is enable, should gather opt_stats even there is no hint.
// if the online_opt_stat_gather is disable, only gather opt_stats when there is hint.
if (!need_gathering ||
get_optimizer_context().get_query_ctx()->get_global_hint().has_no_gather_opt_stat_hint()) {
need_osg = false;
} else if (get_optimizer_context().get_query_ctx()->get_global_hint().has_gather_opt_stat_hint()) {
need_osg = true;
} else if (!online_sys_var) {
need_osg = false;
} else if (use_pdml() ||
get_optimizer_context().get_query_ctx()->get_global_hint().should_generate_osg_operator()) {
need_osg = true;
} else {
need_osg = false;
}
LOG_TRACE("online insert stat", K(online_sys_var), K(need_osg), K(need_gathering));
}
return ret;
}
int ObInsertLogPlan::allocate_insert_values_as_top(ObLogicalOperator *&top)
{
int ret = OB_SUCCESS;
ObLogExprValues *values_op = NULL;
const ObInsertStmt *insert_stmt = get_stmt();
ObSQLSessionInfo *session_info = get_optimizer_context().get_session_info();
if (OB_ISNULL(insert_stmt) || OB_ISNULL(session_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(insert_stmt), K(session_info), K(ret));
} else if (OB_ISNULL(values_op = static_cast<ObLogExprValues*>(get_log_op_factory().
allocate(*this, LOG_EXPR_VALUES)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocate values op", K(ret));
} else if (insert_stmt->is_error_logging() && OB_FAIL(values_op->extract_err_log_info())) {
LOG_WARN("failed to extract error log exprs", K(ret));
} else if (OB_FAIL(values_op->compute_property())) {
LOG_WARN("failed to compute property", K(ret));
} else {
if (NULL != top) {
ret = values_op->add_child(top);
}
top = values_op;
bool contain = false;
if (OB_FAIL(ret)) {
} else if (OB_FAIL(check_contain_non_onetime_expr(insert_stmt->get_values_vector(), contain))) {
LOG_WARN("check contain non onetime expr", K(ret));
} else if (contain) {
ret = OB_NOT_SUPPORTED;
LOG_USER_ERROR(OB_NOT_SUPPORTED, "insert values contain non onetime subquery");
LOG_WARN("insert values contain non onetime subquery", K(ret));
} else if (!insert_stmt->get_subquery_exprs().empty() &&
OB_FAIL(allocate_subplan_filter_as_top(top,
insert_stmt->get_values_vector()))) {
LOG_WARN("failed to allocate subplan filter as top", K(ret));
} else if (OB_FAIL(values_op->add_values_expr(insert_stmt->get_values_vector()))) {
LOG_WARN("failed to add values expr", K(ret));
} else if (OB_FAIL(values_op->add_values_desc(insert_stmt->get_values_desc()))) {
LOG_WARN("failed to add values desc", K(ret));
} else { /*do nothing*/ }
}
return ret;
}
int ObInsertLogPlan::candi_allocate_insert(OSGShareInfo *osg_info)
{
int ret = OB_SUCCESS;
ObConstRawExpr *lock_row_flag_expr = NULL;
ObTablePartitionInfo *insert_table_part = NULL;
ObShardingInfo *insert_table_sharding = NULL;
ObSEArray<CandidatePlan, 8> candi_plans;
ObSEArray<CandidatePlan, 8> insert_plans;
const bool force_no_multi_part = get_log_plan_hint().no_use_distributed_dml();
const bool force_multi_part = get_log_plan_hint().use_distributed_dml();
OPT_TRACE("start generate normal insert plan");
OPT_TRACE("force no multi part:", force_no_multi_part);
OPT_TRACE("force multi part:", force_multi_part);
if (OB_FAIL(build_lock_row_flag_expr(lock_row_flag_expr))) {
LOG_WARN("failed to build lock row flag expr", K(ret));
} else if (OB_FAIL(calculate_insert_table_location_and_sharding(insert_table_part,
insert_table_sharding))) {
LOG_WARN("failed to calculate insert table location and sharding", K(ret));
} else if (OB_FAIL(get_minimal_cost_candidates(candidates_.candidate_plans_,
candi_plans))) {
LOG_WARN("failed to get minimal cost candidates", K(ret));
} else if (OB_FAIL(create_insert_plans(candi_plans,
insert_table_part,
insert_table_sharding,
lock_row_flag_expr,
force_no_multi_part,
force_multi_part,
insert_plans,
osg_info))) {
LOG_WARN("failed to create insert plans", K(ret));
} else if (!insert_plans.empty()) {
LOG_TRACE("succeed to create insert plan using hint", K(insert_plans.count()));
} else if (OB_FAIL(create_insert_plans(candi_plans, insert_table_part,
insert_table_sharding,
lock_row_flag_expr,
false, false,
insert_plans,
osg_info))) {
LOG_WARN("failed to create insert plans", K(ret));
} else {
LOG_TRACE("succeed to create insert plan ignore hint", K(insert_plans.count()));
}
if (OB_SUCC(ret)) {
if (OB_FAIL(prune_and_keep_best_plans(insert_plans))) {
LOG_WARN("failed to prune and keep best plans", K(ret));
} else { /*do nothing*/ }
}
return ret;
}
int ObInsertLogPlan::build_lock_row_flag_expr(ObConstRawExpr *&lock_row_flag_expr)
{
int ret = OB_SUCCESS;
lock_row_flag_expr = NULL;
if (OB_ISNULL(get_stmt())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(get_stmt()), K(ret));
} else {
const ObInsertStmt *insert_stmt = get_stmt();
const ObIArray<ObAssignment>& assignments = insert_stmt->get_table_assignments();
if (!assignments.empty()) {
// table_assigns is not empty, indicating that there may be an update subplan
if (OB_FAIL(ObRawExprUtils::build_var_int_expr(optimizer_context_.get_expr_factory(),
lock_row_flag_expr))) {
LOG_WARN("fail to create expr", K(ret));
} else if (OB_FAIL(lock_row_flag_expr->formalize(optimizer_context_.get_session_info()))) {
LOG_WARN("fail to formalize", K(ret));
} else { /*do nothing*/ }
}
}
return ret;
}
int ObInsertLogPlan::get_osg_type(bool is_multi_part_dml,
ObShardingInfo *insert_table_sharding,
int64_t distributed_method,
OSG_TYPE &type)
{
int ret = OB_SUCCESS;
type = OSG_TYPE::NORMAL_OSG;
if (DIST_PARTITION_WISE == distributed_method ||
DIST_PULL_TO_LOCAL == distributed_method) {
//need merge stats
type = OSG_TYPE::GATHER_OSG;
} else if (DIST_BASIC_METHOD == distributed_method) {
if (is_multi_part_dml) {
type = OSG_TYPE::NORMAL_OSG;
} else if (OB_ISNULL(insert_table_sharding)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null sharding", K(ret));
} else if (insert_table_sharding->is_remote()) {
//need merge stats
type = OSG_TYPE::GATHER_OSG;
}
}
return ret;
}
int ObInsertLogPlan::create_insert_plans(ObIArray<CandidatePlan> &candi_plans,
ObTablePartitionInfo *insert_table_part,
ObShardingInfo *insert_table_sharding,
ObConstRawExpr *lock_row_flag_expr,
const bool force_no_multi_part,
const bool force_multi_part,
ObIArray<CandidatePlan> &insert_plans,
OSGShareInfo *osg_info)
{
int ret = OB_SUCCESS;
ObExchangeInfo exch_info;
bool is_multi_part_dml = false;
CandidatePlan candi_plan;
OSG_TYPE osg_type = OSG_TYPE::NORMAL_OSG;
int64_t inherit_sharding_index = OB_INVALID_INDEX;
ObShardingInfo *insert_op_sharding = NULL;
ObSEArray<ObShardingInfo*, 2> input_shardings;
for (int64_t i = 0; OB_SUCC(ret) && i < candi_plans.count(); i++) {
candi_plan = candi_plans.at(i);
is_multi_part_dml = force_multi_part;
input_shardings.reuse();
insert_op_sharding = NULL;
int64_t distributed_methods = DIST_BASIC_METHOD | DIST_PARTITION_WISE | DIST_PULL_TO_LOCAL;
if (OB_ISNULL(candi_plan.plan_tree_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(get_best_insert_dist_method(*candi_plan.plan_tree_,
insert_table_part,
insert_table_sharding,
force_no_multi_part,
force_multi_part,
distributed_methods,
is_multi_part_dml))) {
LOG_WARN("failed to get best insert plan method", K(ret));
} else if (0 == distributed_methods) {
/*do nothing*/
} else if (osg_info != NULL &&
OB_FAIL(get_osg_type(is_multi_part_dml,
insert_table_sharding,
distributed_methods,
osg_type))) {
LOG_WARN("failed to get osg type", K(ret));
} else if (osg_info != NULL &&
OB_FAIL(allocate_optimizer_stats_gathering_as_top(candi_plan.plan_tree_,
*osg_info,
osg_type))) {
LOG_WARN("failed to allocate sequence as top", K(ret));
} else if (DIST_PULL_TO_LOCAL == distributed_methods) {
if (OB_FAIL(allocate_exchange_as_top(candi_plan.plan_tree_, exch_info))) {
LOG_WARN("failed to allocate exchange as top", K(ret));
} else {
insert_op_sharding = get_optimizer_context().get_local_sharding();
}
} else if (DIST_BASIC_METHOD == distributed_methods) {
if (OB_FAIL(input_shardings.push_back(candi_plan.plan_tree_->get_strong_sharding()))) {
LOG_WARN("failed to push back sharding", K(ret));
} else if (OB_FAIL(input_shardings.push_back(is_multi_part_dml ?
get_optimizer_context().get_local_sharding() :
insert_table_sharding))) {
LOG_WARN("failed to push back sharding", K(ret));
} else if (OB_FAIL(ObOptimizerUtil::compute_basic_sharding_info(
get_optimizer_context().get_local_server_addr(),
input_shardings,
get_optimizer_context().get_allocator(),
insert_op_sharding,
inherit_sharding_index))) {
LOG_WARN("failed to compute basic sharding info", K(ret));
}
} else if (DIST_PARTITION_WISE == distributed_methods) {
insert_op_sharding = candi_plan.plan_tree_->get_strong_sharding();
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid method", K(distributed_methods), K(ret));
}
if (OB_FAIL(ret) || OB_ISNULL(insert_op_sharding)) {
} else if (OB_FAIL(allocate_insert_as_top(candi_plan.plan_tree_,
lock_row_flag_expr,
insert_table_part,
insert_op_sharding,
is_multi_part_dml,
DIST_PARTITION_WISE == distributed_methods))) {
LOG_WARN("failed to allocate insert as top", K(ret));
} else if (OB_FAIL(insert_plans.push_back(candi_plan))) {
LOG_WARN("failed to push back", K(ret));
} else { /*do nothing*/ }
}
return ret;
}
int ObInsertLogPlan::allocate_insert_as_top(ObLogicalOperator *&top,
ObRawExpr *lock_row_flag_expr,
ObTablePartitionInfo *table_partition_info,
ObShardingInfo *insert_op_sharding,
bool is_multi_part_dml,
bool is_partition_wise)
{
int ret = OB_SUCCESS;
ObLogInsert *insert_op = NULL;
const ObInsertStmt *insert_stmt = NULL;
if (OB_ISNULL(top) || OB_ISNULL(insert_stmt = get_stmt())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(top), K(insert_stmt), K(ret));
} else if (OB_ISNULL(insert_op = static_cast<ObLogInsert*>(get_log_op_factory().allocate(*this, LOG_INSERT)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate insert operator failed", K(ret));
} else if (OB_FAIL(insert_op->assign_dml_infos(index_dml_infos_))) {
LOG_WARN("failed to assign index dml infos", K(ret));
} else if (insert_stmt->is_replace() &&
OB_FAIL(insert_op->get_replace_index_dml_infos().assign(replace_del_index_del_infos_))) {
LOG_WARN("failed to assign replace index dml infos", K(ret));
} else if (insert_stmt->is_insert_up() &&
OB_FAIL(insert_op->get_insert_up_index_dml_infos().assign(insert_up_index_upd_infos_))) {
LOG_WARN("failed to assign insert_up upd index dml infos", K(ret));
} else {
insert_op->set_child(ObLogicalOperator::first_child, top);
insert_op->set_is_insert_select(insert_stmt->value_from_select());
insert_op->set_is_returning(insert_stmt->is_returning());
insert_op->set_replace(insert_stmt->is_replace());
insert_op->set_ignore(insert_stmt->is_ignore());
insert_op->set_overwrite(insert_stmt->is_overwrite());
insert_op->set_is_multi_part_dml(is_multi_part_dml);
insert_op->set_table_partition_info(table_partition_info);
insert_op->set_lock_row_flag_expr(lock_row_flag_expr);
insert_op->set_has_instead_of_trigger(insert_stmt->has_instead_of_trigger());
if (get_can_use_parallel_das_dml()) {
insert_op->set_das_dop(max_dml_parallel_);
LOG_TRACE("insert das dop", K(max_dml_parallel_));
}
insert_op->set_is_partition_wise(is_partition_wise);
if (OB_NOT_NULL(insert_stmt->get_table_item(0))) {
insert_op->set_append_table_id(insert_stmt->get_table_item(0)->ref_id_);
}
insert_op->set_strong_sharding(insert_op_sharding);
if (insert_stmt->is_insert_up()) {
insert_op->set_insert_up(true);
}
if (insert_stmt->is_insert_up() || insert_stmt->is_replace()) {
insert_op->set_constraint_infos(&uk_constraint_infos_);
}
if (insert_stmt->is_error_logging() && OB_FAIL(insert_op->extract_err_log_info())) {
LOG_WARN("failed to extract error log info", K(ret));
} else if (OB_FAIL(insert_stmt->get_view_check_exprs(insert_op->get_view_check_exprs()))) {
LOG_WARN("failed to get view check exprs", K(ret));
} else if (OB_FAIL(insert_op->compute_property())) {
LOG_WARN("failed to compute equal set", K(ret));
} else {
top = insert_op;
}
}
return ret;
}
int ObInsertLogPlan::candi_allocate_pdml_insert(OSGShareInfo *osg_info)
{
int ret = OB_SUCCESS;
int64_t gidx_cnt = index_dml_infos_.count();
OPT_TRACE("start generate pdml insert plan");
const bool is_pdml_update_split = false;
for (int64_t i = 0; OB_SUCC(ret) && i < gidx_cnt; i++) {
if (OB_FAIL(candi_allocate_one_pdml_insert(i > 0,
i == gidx_cnt - 1,
is_pdml_update_split,
index_dml_infos_.at(i),
i == 0 ? osg_info : NULL))) {
LOG_WARN("failed to allocate one pdml insert", K(ret), K(i), K(index_dml_infos_));
} else {
LOG_TRACE("succeed to allocate one pdml insert");
}
}
return ret;
}
/*
* for insert stmt to check whether need multi-partition dml
*/
int ObInsertLogPlan::get_best_insert_dist_method(ObLogicalOperator &top,
ObTablePartitionInfo *insert_table_partition,
ObShardingInfo *insert_table_sharding,
const bool force_no_multi_part,
const bool force_multi_part,
int64_t &distributed_methods,
bool &is_multi_part_dml)
{
int ret = OB_SUCCESS;
is_multi_part_dml = false;
bool is_basic = false;
bool is_partition_wise = false;
ObShardingInfo *local_sharding = get_optimizer_context().get_local_sharding();
if (OB_ISNULL(local_sharding)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(local_sharding), K(ret));
} else if (OB_FAIL(check_insert_plan_need_multi_partition_dml(insert_table_partition,
insert_table_sharding,
is_multi_part_dml))) {
LOG_WARN("failed to check if insert stmt need multi-partition dml", K(ret));
} else if (is_multi_part_dml && force_no_multi_part) {
distributed_methods = 0;
} else if (OB_FALSE_IT(is_multi_part_dml |= force_multi_part)) {
//hint force use multi part dml
} else if (is_multi_part_dml) {
if (OB_FAIL(check_basic_sharding_for_dml_stmt(*local_sharding,
top,
is_basic))) {
LOG_WARN("failed to check basic sharding for insert stmt", K(ret));
} else if (is_basic) {
distributed_methods = DIST_BASIC_METHOD;
OPT_TRACE("insert plan will use basic method");
} else {
distributed_methods = DIST_PULL_TO_LOCAL;
OPT_TRACE("insert plan will use pull to local method");
}
} else if (OB_ISNULL(insert_table_sharding)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(insert_table_sharding), K(ret));
} else if ((distributed_methods & DIST_PARTITION_WISE) &&
OB_FAIL(check_if_match_partition_wise_insert(*insert_table_sharding,
top,
is_partition_wise))) {
LOG_WARN("failed to check if match partition wise insert", K(ret));
} else if (is_partition_wise) {
distributed_methods = DIST_PARTITION_WISE;
OPT_TRACE("insert plan will use partition wise method");
} else if (OB_FAIL(check_basic_sharding_for_dml_stmt(*insert_table_sharding,
top,
is_basic))) {
LOG_WARN("failed to check basic sharding for insert stmt", K(ret));
} else if (is_basic) {
distributed_methods = DIST_BASIC_METHOD;
OPT_TRACE("insert plan will use basic method");
} else if (!insert_table_sharding->is_local() &&
OB_FALSE_IT(is_multi_part_dml=true)) {
//insert into remote table, force use multi part dml
} else if (is_multi_part_dml &&
OB_FAIL(check_basic_sharding_for_dml_stmt(*local_sharding,
top,
is_basic))) {
LOG_WARN("failed to check basic sharding for insert stmt", K(ret));
} else if (is_basic) {
distributed_methods = DIST_BASIC_METHOD;
OPT_TRACE("insert partition table force use multi part dml");
OPT_TRACE("insert plan will use basic method");
} else {
distributed_methods = DIST_PULL_TO_LOCAL;
OPT_TRACE("insert partition table force use multi part dml");
OPT_TRACE("insert plan will use pull to local method");
}
if (OB_SUCC(ret)) {
LOG_TRACE("succeed to get best insert plan", K(is_multi_part_dml), K(distributed_methods));
}
return ret;
}
int ObInsertLogPlan::check_insert_plan_need_multi_partition_dml(ObTablePartitionInfo *insert_table_part,
ObShardingInfo *insert_table_sharding,
bool &is_multi_part_dml)
{
int ret = OB_SUCCESS;
bool has_rand_part_key = false;
bool has_subquery_part_key = false;
bool has_auto_inc_part_key = false;
bool part_key_update = false;
ObSchemaGetterGuard *schema_guard = NULL;
const ObTableSchema *table_schema = NULL;
const ObInsertStmt *insert_stmt = NULL;
ObSQLSessionInfo *session_info = NULL;
ObSEArray<ObObjectID, 4> part_ids;
is_multi_part_dml = false;
bool is_one_part_table = false;
if (OB_ISNULL(insert_table_part) || OB_ISNULL(insert_table_sharding)) {
//insert into values
is_multi_part_dml = true;
} else if (OB_ISNULL(insert_stmt = get_stmt()) ||
OB_ISNULL(schema_guard = get_optimizer_context().get_schema_guard()) ||
OB_ISNULL(session_info = get_optimizer_context().get_session_info())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(insert_stmt), K(schema_guard), K(session_info), K(ret));
} else if (0 == insert_table_part->get_phy_tbl_location_info().get_partition_cnt()) {
is_multi_part_dml = true;
OPT_TRACE("insert table has no partition, force use multi part dml");
} else if (use_parallel_das_dml_) {
is_multi_part_dml = true;
OPT_TRACE("insert table use parallel das dml, force use multi part dml");
} else if (insert_stmt->has_instead_of_trigger() ||
index_dml_infos_.count() > 1 ||
get_optimizer_context().is_batched_multi_stmt() ||
//ddl sql can produce a PDML plan with PL UDF,
//some PL UDF that cannot be executed in a PDML plan
//will be forbidden during the execution phase
optimizer_context_.contain_user_nested_sql()) {
is_multi_part_dml = true;
OPT_TRACE("trigger/multi insert/batch stmt/nested sql, force use multi part dml");
} else if (!insert_stmt->value_from_select()) {
is_multi_part_dml = true;
OPT_TRACE("insert into values, force use multi part dml");
} else if (OB_FAIL(schema_guard->get_table_schema(session_info->get_effective_tenant_id(),
insert_stmt->get_insert_table_info().ref_table_id_,
table_schema))) {
LOG_WARN("get table schema from schema guard failed", K(ret));
} else if (OB_ISNULL(table_schema)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FALSE_IT(is_one_part_table = ObSQLUtils::is_one_part_table_can_skip_part_calc(*table_schema))) {
} else if (insert_table_sharding->is_single() &&
!is_one_part_table &&
insert_table_part->get_table_location().is_part_or_subpart_all_partition()) {
is_multi_part_dml = true;
OPT_TRACE("insert table has only one partition in partition level, force use multi part dml");
} else if ((insert_stmt->is_ignore() && !is_one_part_table) ||
(lib::is_mysql_mode() && !is_strict_mode(session_info->get_sql_mode()))) {
// insert ignore, and when inserting into a partitioned table, it cannot be optimized
// mysql non strict mode can not optimize as multi part dml
is_multi_part_dml = true;
OPT_TRACE("insert partition table with ignore/mysql non strict mode, force use multi part dml");
} else if (!insert_stmt->get_insert_table_info().part_ids_.empty() &&
insert_stmt->value_from_select()) {
is_multi_part_dml = true;
OPT_TRACE("insert partition table with ignore/mysql non strict mode, force use multi part dml");
} else if (OB_FAIL(insert_stmt->part_key_is_updated(part_key_update))) {
LOG_WARN("failed to check part key is updated", K(ret));
} else if (part_key_update && !is_one_part_table) {
is_multi_part_dml = true;
OPT_TRACE("insert partition table with update part key, force use multi part dml");
} else if (insert_stmt->has_part_key_sequence() &&
share::schema::PARTITION_LEVEL_ZERO != table_schema->get_part_level()) {
// sequence as the value of the partition key is inserted into the partition table or insert...update updated the partition key, both need to use multi table dml
is_multi_part_dml = true;
OPT_TRACE("insert partition table with sequence, force use multi part dml");
} else if (OB_FAIL(insert_stmt->part_key_has_rand_value(has_rand_part_key))) {
LOG_WARN("check part key has rand value failed", K(ret));
} else if (OB_FAIL(insert_stmt->part_key_has_subquery(has_subquery_part_key))) {
LOG_WARN("failed to check part key has subquery", K(ret));
} else if (OB_FAIL(insert_stmt->part_key_has_auto_inc(has_auto_inc_part_key))) {
LOG_WARN("check to check whether part key contains auto inc column", K(ret));
} else if (has_rand_part_key || has_subquery_part_key || has_auto_inc_part_key) {
is_multi_part_dml = true;
OPT_TRACE("part key with rand/subquery/auto_inc expr, force use multi part dml");
} else { /*do nothing*/ }
if (OB_SUCC(ret)) {
LOG_TRACE("succeed to check insert_stmt need multi-partition-dml", K(is_multi_part_dml));
}
return ret;
}
int ObInsertLogPlan::check_if_match_partition_wise_insert(ObShardingInfo &target_sharding,
ObLogicalOperator &top,
bool &is_partition_wise)
{
int ret = OB_SUCCESS;
bool is_match = false;
ObSEArray<ObRawExpr*, 4> target_exprs;
ObSEArray<ObRawExpr*, 4> source_exprs;
const ObInsertStmt *insert_stmt = NULL;
is_partition_wise = false;
if (OB_ISNULL(get_stmt())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(get_stmt()), K(ret));
} else if (!get_stmt()->is_insert_stmt()) {
// do nothing for merge stmt
} else if (FALSE_IT(insert_stmt = get_stmt())) {
/*do nothing*/
} else if (OB_FAIL(append(target_exprs, insert_stmt->get_insert_table_info().column_exprs_))) {
LOG_WARN("failed to append exprs", K(ret));
} else if (OB_FAIL(insert_stmt->get_value_exprs(source_exprs))) {
LOG_WARN("failed to get value exprs", K(ret));
} else if (OB_FAIL(ObShardingInfo::check_if_match_partition_wise(top.get_output_equal_sets(),
target_exprs,
source_exprs,
&target_sharding,
top.get_strong_sharding(),
is_match))) {
LOG_WARN("failed to check if match partition wise join", K(ret));
} else {
is_partition_wise = is_match && !top.is_exchange_allocated();
}
LOG_TRACE("check partition wise", K(is_match), K(top.is_exchange_allocated()));
return ret;
}
int ObInsertLogPlan::prepare_dml_infos()
{
int ret = OB_SUCCESS;
const ObInsertStmt *stmt = NULL;
ObSQLSessionInfo* session_info = optimizer_context_.get_session_info();
if (OB_ISNULL(stmt = get_stmt()) || OB_ISNULL(session_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt is null", K(ret), K(stmt), K(session_info));
} else if (optimizer_context_.is_online_ddl() &&
!ObSQLUtils::is_nested_sql(session_info->get_cur_exec_ctx())) {
//@todo: (cangdi), special operation of DDL should not include nested sql triggered in DDL session
if (OB_FAIL(prepare_table_dml_info_for_ddl(stmt->get_insert_table_info(), index_dml_infos_))) {
LOG_WARN("failed to prepare table dml info for ddl", K(ret));
}
} else {
const ObInsertTableInfo& table_info = stmt->get_insert_table_info();
IndexDMLInfo* table_dml_info = nullptr;
ObSEArray<IndexDMLInfo*, 8> index_dml_infos;
bool has_tg = stmt->has_instead_of_trigger();
if (OB_FAIL(prepare_table_dml_info_basic(table_info, table_dml_info, index_dml_infos, has_tg))) {
LOG_WARN("failed to prepare table dml info basic", K(ret));
} else if (OB_FAIL(prepare_table_dml_info_special(table_info, table_dml_info, index_dml_infos, index_dml_infos_))) {
LOG_WARN("failed to prepare table dml info special", K(ret));
} else if ((stmt->is_insert_up() || stmt->is_replace()) &&
OB_FAIL(prepare_unique_constraint_infos(table_info))) {
LOG_WARN("failed to prepare unique constraint infos", K(ret));
} else if (stmt->is_replace()) {
// prepare_table_dml_info_special will prune virtual column and collect related local
// index ids. Since replace delete index dml info has same pruned column and related
// local index, we can prepare_table_dml_info_special first and copy next.
if (OB_FAIL(ObDelUpdLogPlan::prepare_table_dml_info_special(table_info,
table_dml_info,
index_dml_infos,
index_dml_infos_))) {
LOG_WARN("failed to prepare table dml info special", K(ret));
} else if (OB_FAIL(copy_index_dml_infos_for_replace(index_dml_infos_,
replace_del_index_del_infos_))) {
LOG_WARN("failed to copy log index dml infos", K(ret));
}
} else if (stmt->is_insert_up()) {
// prepare_table_dml_info_special will prune virtual column and collect related local
// index ids. Since insert update index dml info has different pruned column and related
// local index, we need copy dml info fisrt and then prepare_table_dml_info_special.
if (OB_FAIL(copy_index_dml_infos_for_insert_up(table_info, table_dml_info,
index_dml_infos, insert_up_index_upd_infos_))) {
LOG_WARN("failed to copy log index dml infos", K(ret));