forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_dml_stmt.cpp
More file actions
4749 lines (4505 loc) · 173 KB
/
Copy pathob_dml_stmt.cpp
File metadata and controls
4749 lines (4505 loc) · 173 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_RESV
#include "sql/resolver/dml/ob_dml_stmt.h"
#include "lib/utility/utility.h"
#include "share/inner_table/ob_inner_table_schema.h"
#include "sql/resolver/ob_resolver_utils.h"
#include "sql/resolver/dml/ob_select_stmt.h"
#include "sql/resolver/ob_schema_checker.h"
#include "sql/resolver/expr/ob_raw_expr_util.h"
#include "sql/rewrite/ob_transform_utils.h"
#include "sql/optimizer/ob_logical_operator.h"
#include "sql/parser/parse_malloc.h"
#include "sql/ob_sql_context.h"
#include "sql/rewrite/ob_equal_analysis.h"
#include "sql/resolver/dml/ob_dml_resolver.h"
#include "sql/resolver/dml/ob_stmt_expr_visitor.h"
#include "common/ob_smart_call.h"
#include "share/ob_lob_access_utils.h"
using namespace oceanbase::sql;
using namespace oceanbase::common;
using namespace oceanbase::share::schema;
int TransposeItem::InPair::assign(const TransposeItem::InPair &other)
{
int ret = OB_SUCCESS;
if (this == &other) {
//do nothing
} else if (OB_FAIL(exprs_.assign(other.exprs_))) {
LOG_WARN("assign searray failed", K(other), K(ret));
} else if (OB_FAIL(column_names_.assign(other.column_names_))) {
LOG_WARN("assign searray failed", K(other), K(ret));
} else {
pivot_expr_alias_ = other.pivot_expr_alias_;
}
return ret;
}
int TransposeItem::assign(const TransposeItem &other)
{
int ret = OB_SUCCESS;
if (this == &other) {
//do nothing
} else if (OB_FAIL(for_columns_.assign(other.for_columns_))) {
LOG_WARN("assign searray failed", K(other), K(ret));
} else if (OB_FAIL(in_pairs_.assign(other.in_pairs_))) {
LOG_WARN("assign searray failed", K(other), K(ret));
} else if (OB_FAIL(unpivot_columns_.assign(other.unpivot_columns_))) {
LOG_WARN("assign searray failed", K(other), K(ret));
} else {
aggr_pairs_.reset();
old_column_count_ = other.old_column_count_;
is_unpivot_ = other.is_unpivot_;
is_incude_null_ = other.is_incude_null_;
alias_name_ = other.alias_name_;
}
return ret;
}
int TransposeItem::deep_copy(ObIRawExprCopier &expr_copier,
const TransposeItem &other)
{
int ret = OB_SUCCESS;
if (OB_FAIL(assign(other))) {
LOG_WARN("assign failed", K(other), K(ret));
}
for (int64_t i = 0; i < in_pairs_.count() && OB_SUCC(ret); ++i) {
InPair &in_pair = in_pairs_.at(i);
in_pair.exprs_.reuse();
if (OB_FAIL(expr_copier.copy(other.in_pairs_.at(i).exprs_,
in_pair.exprs_))) {
LOG_WARN("deep copy expr failed", K(ret));
}
}
return ret;
}
OB_SERIALIZE_MEMBER(ObUnpivotInfo,
old_column_count_,
unpivot_column_count_,
for_column_count_,
is_include_null_);
int SemiInfo::deep_copy(ObIRawExprCopier &expr_copier, const SemiInfo &other)
{
int ret = OB_SUCCESS;
if (OB_FAIL(left_table_ids_.assign(other.left_table_ids_))) {
LOG_WARN("failed to assign left table ids", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.semi_conditions_,
semi_conditions_))) {
LOG_WARN("failed to copy semi condition exprs", K(ret));
} else {
join_type_ = other.join_type_;
semi_id_ = other.semi_id_;
right_table_id_ = other.right_table_id_;
}
return ret;
}
int FromItem::deep_copy(const FromItem &other)
{
int ret = OB_SUCCESS;
table_id_ = other.table_id_;
link_table_id_ = other.link_table_id_;
is_joined_ = other.is_joined_;
return ret;
}
bool JoinedTable::same_as(const JoinedTable &other) const
{
bool bret = true;
if (TableItem::JOINED_TABLE != type_
|| TableItem::JOINED_TABLE != other.type_
|| NULL == left_table_
|| NULL == right_table_
|| NULL == other.left_table_
|| NULL == other.right_table_) {
bret = false;
} else {
if (table_id_ != other.table_id_
|| joined_type_ != other.joined_type_
|| join_conditions_.count() != other.join_conditions_.count()) {
bret = false;
} else if (left_table_->type_ != other.left_table_->type_
|| right_table_->type_ != other.right_table_->type_) {
bret = false;
} else if (TableItem::JOINED_TABLE == left_table_->type_) {
const JoinedTable *left_table = static_cast<JoinedTable*>(left_table_);
const JoinedTable *other_left_table = static_cast<JoinedTable*>(other.left_table_);
bret = left_table->same_as(*other_left_table);
} else {
if (left_table_->table_id_ != other.left_table_->table_id_) {
bret = false;
}
}
}
if (true == bret) {
if (TableItem::JOINED_TABLE == right_table_->type_) {
const JoinedTable *right_table = static_cast<JoinedTable*>(right_table_);
const JoinedTable *other_right_table = static_cast<JoinedTable*>(other.right_table_);
bret = right_table->same_as(*other_right_table);
bret = right_table->same_as(*other_right_table);
} else if (right_table_->table_id_ != other.right_table_->table_id_) {
bret = false;
}
}
if (true == bret) {
for (int64_t i = 0; bret && i < join_conditions_.count(); ++i) {
const ObRawExpr *join_condition = join_conditions_.at(i);
const ObRawExpr *other_join_condition = other.join_conditions_.at(i);
if (NULL == join_condition || NULL == other_join_condition) {
bret = false;
} else {
bret = join_condition->same_as(*other_join_condition);
}
}
}
return bret;
}
int ColumnItem::deep_copy(ObIRawExprCopier &expr_copier,
const ColumnItem &other)
{
int ret = OB_SUCCESS;
column_id_ = other.column_id_;
table_id_ = other.table_id_;
column_name_ = other.column_name_;
auto_filled_timestamp_ = other.auto_filled_timestamp_;
base_tid_ = other.base_tid_;
base_cid_ = other.base_cid_;
ObRawExpr *temp_expr = NULL;
is_geo_ = other.is_geo_;
if (OB_FAIL(expr_copier.copy(other.expr_, temp_expr))) {
LOG_WARN("failed to copy column expr", K(ret));
} else if (OB_ISNULL(temp_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("null expr", K(ret));
} else if (OB_UNLIKELY(ObRawExpr::EXPR_COLUMN_REF != temp_expr->get_expr_class())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected expr class", K(temp_expr->get_expr_class()), K(ret));
} else {
expr_ = static_cast<ObColumnRefRawExpr*>(temp_expr);
}
if (OB_FAIL(ret)) {
} else {
col_idx_= other.col_idx_;
if (OB_NOT_NULL(other.default_value_expr_)
&& OB_FAIL(expr_copier.copy(other.default_value_expr_, default_value_expr_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("fail to copy default value expr", K(ret));
} else if (OB_NOT_NULL(other.default_empty_expr_)
&& OB_FAIL(expr_copier.copy(other.default_empty_expr_, default_empty_expr_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("fail to copy default empty expr", K(ret));
}
}
return ret;
}
int TableItem::deep_copy_json_table_def(const ObJsonTableDef& jt_def, ObIRawExprCopier &expr_copier, ObIAllocator* allocator)
{
int ret = OB_SUCCESS;
ObJsonTableDef* tmp_jt_def = nullptr;
if (OB_ISNULL(allocator)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected param, invalid param.", K(ret));
} else if (OB_ISNULL(tmp_jt_def = static_cast<ObJsonTableDef*>(allocator->alloc(sizeof(ObJsonTableDef))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory json table define strunct failed.", K(ret));
} else {
tmp_jt_def = new (tmp_jt_def) ObJsonTableDef();
if (OB_FAIL(tmp_jt_def->deep_copy(jt_def, expr_copier, allocator))) {
LOG_WARN("deep copy json table define failed.", K(ret));
} else {
json_table_def_ = tmp_jt_def;
}
}
return ret;
}
int TableItem::deep_copy(ObIRawExprCopier &expr_copier,
const TableItem &other,
ObIAllocator* allocator)
{
int ret = OB_SUCCESS;
table_id_ = other.table_id_;
table_name_ = other.table_name_;
alias_name_ = other.alias_name_;
synonym_name_ = other.synonym_name_;
synonym_db_name_ = other.synonym_db_name_;
qb_name_ = other.qb_name_;
type_ = other.type_;
ref_id_ = other.ref_id_;
is_system_table_ = other.is_system_table_;
is_index_table_ = other.is_index_table_;
is_view_table_ = other.is_view_table_;
table_type_ = other.table_type_;
is_recursive_union_fake_table_ = other.is_recursive_union_fake_table_;
cte_type_ = other.cte_type_;
database_name_ = other.database_name_;
for_update_ = other.for_update_;
for_update_wait_us_ = other.for_update_wait_us_;
skip_locked_ = other.skip_locked_;
mock_id_ = other.mock_id_;
node_ = other.node_; // should deep copy ? seems to be unnecessary
flashback_query_type_ = other.flashback_query_type_;
// dblink
dblink_id_ = other.dblink_id_;
is_reverse_link_ = other.is_reverse_link_;
dblink_name_ = other.dblink_name_;
link_database_name_ = other.link_database_name_;
// ddl related
ddl_schema_version_ = other.ddl_schema_version_;
ddl_table_id_ = other.ddl_table_id_;
ref_query_ = other.ref_query_;
if (is_json_table()
&& OB_FAIL(deep_copy_json_table_def(*other.json_table_def_, expr_copier, allocator))) {
LOG_WARN("failed to deep copy json table define", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.flashback_query_expr_,
flashback_query_expr_))) {
LOG_WARN("failed to deep copy raw expr", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.function_table_expr_,
function_table_expr_))) {
LOG_WARN("failed to copy function table expr", K(ret));
} else if (OB_FAIL(part_ids_.assign(other.part_ids_))) {
LOG_WARN("failed to assign part ids", K(ret));
} else if (OB_FAIL(part_names_.assign(other.part_names_))) {
LOG_WARN("failed to assign part names", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.table_values_, table_values_))) {
LOG_WARN("failed to deep copy table values", K(ret));
}
return ret;
}
int JoinedTable::deep_copy(ObIAllocator &allocator,
ObIRawExprCopier &expr_copier,
const JoinedTable &other)
{
int ret = OB_SUCCESS;
joined_type_ = other.joined_type_;
if (OB_ISNULL(other.left_table_) || OB_ISNULL(other.right_table_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("null table item", K(other.left_table_), K(other.right_table_), K(ret));
} else if (OB_FAIL(TableItem::deep_copy(expr_copier, other, &allocator))) {
LOG_WARN("deep copy table item failed", K(ret));
} else if (OB_FAIL(single_table_ids_.assign(other.single_table_ids_))) {
LOG_WARN("failed to assign single table ids", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.join_conditions_, join_conditions_))) {
LOG_WARN("failed to copy join condition exprs", K(ret));
} else {
left_table_ = other.left_table_;
right_table_ = other.right_table_;
}
if (OB_SUCC(ret) && left_table_->is_joined_table()) {
JoinedTable *tmp_left = NULL;
void *ptr = NULL;
if (OB_ISNULL(ptr = allocator.alloc(sizeof(JoinedTable)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocate memory for joined table", K(ret));
} else {
tmp_left = new (ptr) JoinedTable();
if (OB_FAIL(tmp_left->deep_copy(allocator,
expr_copier,
static_cast<JoinedTable &>(*left_table_)))) {
LOG_WARN("failed to deep copy left table", K(ret));
} else {
left_table_ = tmp_left;
}
}
}
if (OB_SUCC(ret) && right_table_->is_joined_table()) {
JoinedTable *tmp_right = NULL;
void *ptr = NULL;
if (OB_ISNULL(ptr = allocator.alloc(sizeof(JoinedTable)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocate memory for joined table", K(ret));
} else {
tmp_right = new (ptr) JoinedTable();
if (OB_FAIL(tmp_right->deep_copy(allocator,
expr_copier,
static_cast<JoinedTable &>(*right_table_)))) {
LOG_WARN("failed to deep copy right table", K(ret));
} else {
right_table_ = tmp_right;
}
}
}
return ret;
}
int ObDMLStmt::PartExprItem::deep_copy(ObIRawExprCopier &expr_copier,
const PartExprItem &other)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr_copier.copy(other.part_expr_, part_expr_))) {
LOG_WARN("failed to copy part expr", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.subpart_expr_, subpart_expr_))) {
LOG_WARN("failed to copy subpart expr", K(ret));
} else {
table_id_ = other.table_id_;
index_tid_ = other.index_tid_;
}
return ret;
}
ObDMLStmt::ObDMLStmt(stmt::StmtType type)
: ObStmt(type),
order_items_(),
limit_count_expr_(NULL),
limit_offset_expr_(NULL),
limit_percent_expr_(NULL),
has_fetch_(false),
is_fetch_with_ties_(false),
from_items_(),
part_expr_items_(),
joined_tables_(),
stmt_hint_(),
semi_infos_(),
autoinc_params_(),
is_calc_found_rows_(false),
has_top_limit_(false),
is_contains_assignment_(false),
affected_last_insert_id_(false),
has_part_key_sequence_(false),
table_items_(),
column_items_(),
condition_exprs_(),
pseudo_column_like_exprs_(),
tables_hash_(),
subquery_exprs_(),
transpose_item_(NULL),
user_var_exprs_(),
check_constraint_items_(),
dblink_id_(OB_INVALID_ID),
is_reverse_link_(false)
{
}
ObDMLStmt::~ObDMLStmt()
{
}
// tables come from table_items_
int ObDMLStmt::remove_from_item(ObIArray<TableItem*> &tables)
{
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < tables.count(); i++) {
if (OB_ISNULL(tables.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null", K(ret), K(tables));
} else {
ret = remove_from_item(tables.at(i)->table_id_);
}
}
return ret;
}
int ObDMLStmt::remove_from_item(uint64_t tid, bool *remove_happened/* = NULL*/)
{
int ret = OB_SUCCESS;
if (NULL != remove_happened) {
*remove_happened = false;
}
for (int64_t i = 0; OB_SUCC(ret) && i < from_items_.count(); i++) {
if (tid == from_items_.at(i).table_id_) {
if (OB_FAIL(from_items_.remove(i))) {
LOG_WARN("failed to remove from_items", K(ret));
} else if (NULL != remove_happened) {
*remove_happened = true;
}
break;
}
}
return ret;
}
int ObDMLStmt::remove_semi_info(SemiInfo* info)
{
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < semi_infos_.count(); i++) {
if (info == semi_infos_.at(i)) {
if (OB_FAIL(semi_infos_.remove(i))) {
LOG_WARN("failed to remove from_items", K(ret));
}
break;
}
}
return ret;
}
// ref_query in ObStmt only do pointer copy
int ObDMLStmt::assign(const ObDMLStmt &other)
{
int ret = OB_SUCCESS;
if (OB_FAIL(ObStmt::assign(other))) {
LOG_WARN("failed to copy stmt", K(ret));
} else if (OB_FAIL(table_items_.assign(other.table_items_))) {
LOG_WARN("assign table items failed", K(ret));
} else if (OB_FAIL(assign_tables_hash(other.tables_hash_))) {
LOG_WARN("assign table hash desc failed", K(ret));
} else if (OB_FAIL(column_items_.assign(other.column_items_))) {
LOG_WARN("assign column items failed", K(ret));
} else if (OB_FAIL(condition_exprs_.assign(other.condition_exprs_))) {
LOG_WARN("assign condition exprs failed", K(ret));
} else if (OB_FAIL(order_items_.assign(other.order_items_))) {
LOG_WARN("assign order items failed", K(ret));
} else if (OB_FAIL(from_items_.assign(other.from_items_))) {
LOG_WARN("assign from items failed", K(ret));
} else if (OB_FAIL(part_expr_items_.assign(other.part_expr_items_))) {
LOG_WARN("assign part expr items failed", K(ret));
} else if (OB_FAIL(joined_tables_.assign(other.joined_tables_))) {
LOG_WARN("assign joined tables failed", K(ret));
} else if (OB_FAIL(semi_infos_.assign(other.semi_infos_))) {
LOG_WARN("assign semi infos failed", K(ret));
} else if (OB_FAIL(stmt_hint_.assign(other.stmt_hint_))) {
LOG_WARN("assign stmt hint failed", K(ret));
} else if (OB_FAIL(subquery_exprs_.assign(other.subquery_exprs_))) {
LOG_WARN("assign subquery exprs failed", K(ret));
} else if (OB_FAIL(pseudo_column_like_exprs_.assign(other.pseudo_column_like_exprs_))) {
LOG_WARN("assgin pseudo column exprs fail", K(ret));
} else if (OB_FAIL(autoinc_params_.assign(other.autoinc_params_))) {
LOG_WARN("assign autoinc params fail", K(ret));
} else if (OB_FAIL(nextval_sequence_ids_.assign(other.nextval_sequence_ids_))) {
LOG_WARN("failed to assign sequence ids", K(ret));
} else if (OB_FAIL(currval_sequence_ids_.assign(other.currval_sequence_ids_))) {
LOG_WARN("failed to assign sequence ids", K(ret));
} else if (OB_FAIL(user_var_exprs_.assign(other.user_var_exprs_))) {
LOG_WARN("assign user var exprs fail", K(ret));
} else if (OB_FAIL(check_constraint_items_.assign(other.check_constraint_items_))) {
LOG_WARN("faield to assign check constraint items", K(ret));
} else {
limit_count_expr_ = other.limit_count_expr_;
limit_offset_expr_ = other.limit_offset_expr_;
limit_percent_expr_ = other.limit_percent_expr_;
has_fetch_ = other.has_fetch_;
is_fetch_with_ties_ = other.is_fetch_with_ties_;
is_calc_found_rows_ = other.is_calc_found_rows_;
has_top_limit_ = other.has_top_limit_;
is_contains_assignment_ = other.is_contains_assignment_;
affected_last_insert_id_ = other.affected_last_insert_id_;
has_part_key_sequence_ = other.has_part_key_sequence_;
transpose_item_ = other.transpose_item_;
dblink_id_ = other.dblink_id_;
is_reverse_link_ = other.is_reverse_link_;
}
return ret;
}
int ObDMLStmt::deep_copy(ObStmtFactory &stmt_factory,
ObRawExprFactory &expr_factory,
const ObDMLStmt &other)
{
int ret = OB_SUCCESS;
ObRawExprCopier expr_copier(expr_factory);
if (OB_FAIL(deep_copy(stmt_factory,
expr_copier,
other))) {
LOG_WARN("failed to deep copy stmt", K(ret));
}
return ret;
}
int ObDMLStmt::deep_copy(ObStmtFactory &stmt_factory,
ObRawExprCopier &expr_copier,
const ObDMLStmt &other)
{
int ret = OB_SUCCESS;
ObSEArray<ObSelectStmt *, 4> orgi_child_stmts;
if (OB_UNLIKELY(other.get_stmt_type() != get_stmt_type())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt type does not match", K(ret));
} else if (OB_FAIL(deep_copy_stmt_struct(stmt_factory.get_allocator(),
expr_copier,
other))) {
LOG_WARN("failed to deep copy stmt struct", K(ret));
} else if (OB_FAIL(get_child_stmts(orgi_child_stmts))) {
LOG_WARN("failed to get child stmts", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < orgi_child_stmts.count(); ++i) {
ObSelectStmt *copied_child_stmt = NULL;
if (OB_ISNULL(orgi_child_stmts.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("child stmt is null", K(ret));
} else if (OB_FAIL(stmt_factory.create_stmt(copied_child_stmt))) {
LOG_WARN("failed to create child stmt", K(ret));
} else if (OB_ISNULL(copied_child_stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("child stmt is not created", K(ret));
} else if (OB_FAIL(SMART_CALL(copied_child_stmt->deep_copy(
stmt_factory,
expr_copier,
*orgi_child_stmts.at(i))))) {
LOG_WARN("failed to deep copy stmt struct", K(ret));
} else if (OB_FAIL(set_child_stmt(i, copied_child_stmt))) {
LOG_WARN("failed to set child stmt", K(ret));
}
}
return ret;
}
int deep_copy_stmt_tableItem(ObIAllocator &allocator,
ObIRawExprCopier &expr_copier,
const ObIArray<TableItem *> &objs,
ObIArray<TableItem *> &new_objs)
{
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < objs.count(); ++i) {
const TableItem *obj = objs.at(i);
void *ptr = NULL;
TableItem *new_obj = NULL;
if (OB_LIKELY(NULL != obj)) {
if (OB_ISNULL(ptr = allocator.alloc(sizeof(TableItem)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
SQL_RESV_LOG(WARN, "failed to allocate memory", K(ret), K(lbt()));
} else {
new_obj = new (ptr) TableItem();
if (OB_FAIL(new_obj->deep_copy(expr_copier, *obj, &allocator))) {
SQL_RESV_LOG(WARN, "failed to deep copy obj", K(ret));
}
}
}
if (OB_SUCC(ret) && OB_FAIL(new_objs.push_back(new_obj))) {
SQL_RESV_LOG(WARN, "failed to push back new object", K(ret));
}
}
return ret;
}
int ObDMLStmt::deep_copy_stmt_struct(ObIAllocator &allocator,
ObRawExprCopier &expr_copier,
const ObDMLStmt &other)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(get_stmt_type() != other.get_stmt_type())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt type does not match", K(ret), K(get_stmt_type()), K(other.get_stmt_type()));
} else if (OB_FAIL(ObStmt::deep_copy(other))) {
LOG_WARN("failed to copy stmt", K(ret));
} else if (OB_FAIL(deep_copy_stmt_tableItem(allocator,
expr_copier,
other.table_items_,
table_items_))) {
LOG_WARN("failed to deep copy table items", K(ret));
} else if (OB_FAIL(assign_tables_hash(other.tables_hash_))) {
LOG_WARN("assign table hash desc failed", K(ret));
} else if (OB_FAIL(deep_copy_join_tables(allocator, expr_copier, other))) {
LOG_WARN("failed to copy joined tables", K(ret));
} else if (OB_FAIL(deep_copy_stmt_objects<SemiInfo>(allocator,
expr_copier,
other.semi_infos_,
semi_infos_))) {
LOG_WARN("deep copy semi infos failed", K(ret));
} else if (OB_FAIL(deep_copy_stmt_objects<ColumnItem>(expr_copier,
other.column_items_,
column_items_))) {
LOG_WARN("deep copy column items failed", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.subquery_exprs_,
subquery_exprs_))) {
LOG_WARN("failed to copy subquery exprs", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.pseudo_column_like_exprs_,
pseudo_column_like_exprs_))) {
LOG_WARN("failed to copy pseudo column like exprs", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.condition_exprs_,
condition_exprs_))) {
LOG_WARN("failed to copy condition exprs", K(ret));
} else if (OB_FAIL(deep_copy_stmt_objects<PartExprItem>(expr_copier,
other.part_expr_items_,
part_expr_items_))) {
LOG_WARN("failed to deep copy part expr items", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.limit_count_expr_,
limit_count_expr_))) {
LOG_WARN("deep copy limit count expr failed", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.limit_offset_expr_,
limit_offset_expr_))) {
LOG_WARN("deep copy limit offset expr failed", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.limit_percent_expr_,
limit_percent_expr_))) {
LOG_WARN("deep copy limit percent expr failed", K(ret));
} else if (OB_FAIL(expr_copier.copy(other.user_var_exprs_,
user_var_exprs_))) {
LOG_WARN("deep copy user var exprs failed", K(ret));
} else if (OB_FAIL(deep_copy_stmt_objects<CheckConstraintItem>(expr_copier,
other.check_constraint_items_,
check_constraint_items_))) {
LOG_WARN("failed to deep copy related part expr arrays", K(ret));
} else if (OB_FAIL(from_items_.assign(other.from_items_))) {
LOG_WARN("assign from items failed", K(ret));
} else if (OB_FAIL(stmt_hint_.assign(other.stmt_hint_))) {
LOG_WARN("assign stmt hint failed", K(ret));
} else if (OB_FAIL(autoinc_params_.assign(other.autoinc_params_))) {
LOG_WARN("assign autoinc params failed", K(ret));
} else if (OB_FAIL(nextval_sequence_ids_.assign(other.nextval_sequence_ids_))) {
LOG_WARN("failed to assign sequence ids", K(ret));
} else if (OB_FAIL(currval_sequence_ids_.assign(other.currval_sequence_ids_))) {
LOG_WARN("failed to assign sequence ids", K(ret));
} else {
is_calc_found_rows_ = other.is_calc_found_rows_;
has_top_limit_ = other.has_top_limit_;
is_contains_assignment_ = other.is_contains_assignment_;
affected_last_insert_id_ = other.affected_last_insert_id_;
has_part_key_sequence_ = other.has_part_key_sequence_;
has_fetch_ = other.has_fetch_;
is_fetch_with_ties_ = other.is_fetch_with_ties_;
dblink_id_ = other.dblink_id_;
is_reverse_link_ = other.is_reverse_link_;
}
if (OB_SUCC(ret)) {
TransposeItem *tmp = NULL;
if (OB_FAIL(deep_copy_stmt_object<TransposeItem>(allocator,
expr_copier,
other.transpose_item_,
tmp))) {
LOG_WARN("failed to deep copy transpose item", K(ret));
} else {
transpose_item_ = tmp;
}
}
return ret;
}
int ObDMLStmt::get_child_table_id_recurseive(
common::ObIArray<share::schema::ObObjectStruct> &object_ids,
const int64_t object_limit_count/*OB_MAX_TABLE_NUM_PER_STMT*/) const
{
int ret = OB_SUCCESS;
bool is_stack_overflow = false;
if (OB_FAIL(check_stack_overflow(is_stack_overflow))) {
LOG_WARN("check stack overflow failed", K(ret));
} else if (is_stack_overflow) {
ret = OB_SIZE_OVERFLOW;
LOG_WARN("too deep recursive", K(ret));
}
bool is_finish = false;
for (int64_t i = 0; OB_SUCC(ret) && !is_finish && i < get_table_size(); ++i) {
const sql::TableItem *tmp_table = NULL;
if (OB_ISNULL(tmp_table = get_table_item(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("table item is null", K(ret));
} else if (tmp_table->is_basic_table()) {
ObObjectStruct tmp_struct(share::schema::ObObjectType::TABLE, tmp_table->ref_id_);
if (OB_FAIL(object_ids.push_back(tmp_struct))) {
LOG_WARN("failed to push_back tmp_table->ref_id_", KPC(tmp_table), K(ret));
} else if (OB_UNLIKELY(object_ids.count() >= object_limit_count)) {
is_finish = true;
LOG_DEBUG("arrieve limit count", KPC(this), K(object_limit_count));
} else {
LOG_DEBUG("succ push object_ids", KPC(tmp_table));
}
}
}
//try complex table
if (OB_SUCC(ret) && !is_finish && get_table_size() != object_ids.count()) {
ObArray<ObSelectStmt*> child_stmts;
if (OB_FAIL(get_child_stmts(child_stmts))) {
LOG_WARN("get child stmt failed", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < child_stmts.count(); ++i) {
const ObSelectStmt *sub_stmt = child_stmts.at(i);
if (OB_ISNULL(sub_stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Sub-stmt is NULL", K(ret));
} else if (OB_FAIL(SMART_CALL(sub_stmt->get_child_table_id_recurseive(object_ids,
object_limit_count)))) {
LOG_WARN("failed to get_child_table_id_with_cte_", K(ret));
}
}
}
}
return ret;
}
int ObDMLStmt::get_child_table_id_count_recurseive(
int64_t &object_ids_cnt, const int64_t object_limit_count/*OB_MAX_TABLE_NUM_PER_STMT*/) const
{
int ret = OB_SUCCESS;
bool is_stack_overflow = false;
if (OB_FAIL(check_stack_overflow(is_stack_overflow))) {
LOG_WARN("check stack overflow failed", K(ret));
} else if (is_stack_overflow) {
ret = OB_SIZE_OVERFLOW;
LOG_WARN("too deep recursive", K(ret));
}
bool is_finish = false;
for (int64_t i = 0; OB_SUCC(ret) && !is_finish && i < get_table_size(); ++i) {
const sql::TableItem *tmp_table = NULL;
if (OB_ISNULL(tmp_table = get_table_item(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("table item is null", K(ret));
} else if (tmp_table->is_basic_table()) {
if (OB_UNLIKELY(++object_ids_cnt >= object_limit_count)) {
is_finish = true;
LOG_DEBUG("arrieve limit count", KPC(tmp_table), K(object_limit_count), K(object_ids_cnt));
}
}
}
//try complex table
if (OB_SUCC(ret) && !is_finish && get_table_size() != object_ids_cnt) {
ObArray<ObSelectStmt*> child_stmts;
if (OB_FAIL(get_child_stmts(child_stmts))) {
LOG_WARN("get child stmt failed", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < child_stmts.count(); ++i) {
const ObSelectStmt *sub_stmt = child_stmts.at(i);
if (OB_ISNULL(sub_stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Sub-stmt is NULL", K(ret));
} else if (OB_FAIL(SMART_CALL(sub_stmt->get_child_table_id_count_recurseive(object_ids_cnt,
object_limit_count)))) {
LOG_WARN("failed to get_child_table_id_count_recurseive", K(ret));
}
}
}
}
return ret;
}
int ObDMLStmt::replace_relation_exprs(const common::ObIArray<ObRawExpr *> &other_exprs,
const common::ObIArray<ObRawExpr *> &new_exprs)
{
int ret = OB_SUCCESS;
ObStmtExprReplacer replacer;
replacer.set_relation_scope();
replacer.set_recursive(false);
if (OB_FAIL(replacer.add_replace_exprs(other_exprs, new_exprs))) {
LOG_WARN("failed to add replace exprs", K(ret));
} else if (OB_FAIL(iterate_stmt_expr(replacer))) {
LOG_WARN("failed to iterate stmt expr", K(ret));
}
return ret;
}
int ObDMLStmt::copy_and_replace_stmt_expr(ObRawExprCopier &copier)
{
int ret = OB_SUCCESS;
ObStmtExprCopier visitor(copier);
if (OB_FAIL(copier.add_skipped_expr(get_subquery_exprs(), false))) {
LOG_WARN("failed to copy uncopy exprs", K(ret));
} else if (OB_FAIL(iterate_stmt_expr(visitor))) {
LOG_WARN("failed to iterate stmt expr", K(ret));
}
return ret;
}
int ObDMLStmt::iterate_stmt_expr(ObStmtExprVisitor &visitor)
{
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < part_expr_items_.count(); i++) {
if (OB_FAIL(visitor.visit(part_expr_items_.at(i).part_expr_,
SCOPE_BASIC_TABLE))) {
LOG_WARN("failed to visit part expr", K(ret));
} else if (OB_FAIL(visitor.visit(part_expr_items_.at(i).subpart_expr_,
SCOPE_BASIC_TABLE))) {
LOG_WARN("failed to visit subpart exprs", K(ret));
} else { /*do nothing*/ }
}
for (int64_t i = 0; OB_SUCC(ret) && i < check_constraint_items_.count(); i++) {
if (OB_FAIL(visitor.visit(check_constraint_items_.at(i).check_constraint_exprs_,
SCOPE_BASIC_TABLE))) {
LOG_WARN("failed to visit check constraint exprs", K(ret));
} else { /*+do nothing*/ }
}
for (int64_t i = 0; OB_SUCC(ret) && i < column_items_.count(); i++) {
ObColumnRefRawExpr *&expr = column_items_.at(i).expr_;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("null column expr", K(ret));
} else if (OB_FAIL(visitor.visit(expr, SCOPE_BASIC_TABLE))) {
LOG_WARN("failed to visit column item expr", K(ret));
} else if (NULL != expr->get_dependant_expr()) {
if (OB_FAIL(visitor.visit(expr->get_dependant_expr(), SCOPE_BASIC_TABLE))) {
LOG_WARN("failed to visit temp expr", K(ret));
}
}
if (OB_SUCC(ret) && NULL != column_items_.at(i).default_value_expr_) {
if (OB_FAIL(visitor.visit(column_items_.at(i).default_value_expr_,
SCOPE_BASIC_TABLE))) {
LOG_WARN("failed to visit temp expr", K(ret));
}
} else { /*do nothing*/ }
}
for (int64_t i = 0; OB_SUCC(ret) && i < table_items_.count(); i++) {
if (OB_ISNULL(table_items_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (NULL != table_items_.at(i)->function_table_expr_ &&
OB_FAIL(visitor.visit(table_items_.at(i)->function_table_expr_,
SCOPE_FROM))) {
LOG_WARN("failed to visit function table expr", K(ret));
} else if (NULL != table_items_.at(i)->flashback_query_expr_ &&
OB_FAIL(visitor.visit(table_items_.at(i)->flashback_query_expr_,
SCOPE_FROM))) {
LOG_WARN("failed to visit flashback query expr", K(ret));
} else if (NULL != table_items_.at(i)->json_table_def_ &&
NULL != table_items_.at(i)->json_table_def_->doc_expr_ &&
OB_FAIL(visitor.visit(table_items_.at(i)->json_table_def_->doc_expr_,
SCOPE_FROM))) {
LOG_WARN("failed to add json table doc expr", K(ret));
} else if (OB_FAIL(visitor.visit(table_items_.at(i)->table_values_,
SCOPE_FROM))) {
LOG_WARN("failed to visit table values", K(ret));
} else { /*do nothing*/ }
}
for (int64_t i = 0; OB_SUCC(ret) && i < joined_tables_.count(); i++) {
if (OB_FAIL(iterate_joined_table_expr(joined_tables_.at(i), visitor))) {
LOG_WARN("failed to iterate joined table expr", K(ret));
} else { /*do nothing*/ }
}
for (int64_t i = 0; OB_SUCC(ret) && i < semi_infos_.count(); i++) {
if (OB_ISNULL(semi_infos_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("null semi info", K(ret));
} else if (OB_FAIL(visitor.visit(semi_infos_.at(i)->semi_conditions_,
SCOPE_SEMI_INFO))) {
LOG_WARN("failed to visitor semi conditions", K(ret));
} else { /*do nothing*/ }
}
for (int64_t i = 0; OB_SUCC(ret) && i < order_items_.count(); i++) {
if (OB_FAIL(visitor.visit(order_items_.at(i).expr_,
SCOPE_ORDERBY))) {
LOG_WARN("failed to visit orde by expr", K(ret));
} else { /*do nothing*/ }
}
if (OB_SUCC(ret)) {
if (OB_FAIL(visitor.visit(condition_exprs_, SCOPE_WHERE))) {
LOG_WARN("failed to visit condition exprs", K(ret));
} else if (NULL != limit_count_expr_ &&
OB_FAIL(visitor.visit(limit_count_expr_, SCOPE_LIMIT))) {
LOG_WARN("failed to visit limit count exprs", K(ret));
} else if (NULL != limit_offset_expr_ &&
OB_FAIL(visitor.visit(limit_offset_expr_, SCOPE_LIMIT))) {
LOG_WARN("failed to visit limit offset exprs", K(ret));
} else if (NULL != limit_percent_expr_ &&
OB_FAIL(visitor.visit(limit_percent_expr_, SCOPE_LIMIT))) {
LOG_WARN("failed to visit limit percent exprs", K(ret));
} else {}
}
if (NULL != transpose_item_) {
for (int64_t i = 0; i < transpose_item_->in_pairs_.count() && OB_SUCC(ret); ++i) {
TransposeItem::InPair &in_pair = const_cast<TransposeItem::InPair &>(transpose_item_->in_pairs_.at(i));
if (OB_FAIL(visitor.visit(in_pair.exprs_, SCOPE_PIVOT))) {
LOG_WARN("failed to visit in pair exprs", K(ret));
}
}
}
if (OB_SUCC(ret) && visitor.is_recursive()) {
ObSEArray<ObSelectStmt *, 4> subqueries;
if (OB_FAIL(get_child_stmts(subqueries))) {
LOG_WARN("failed to get subqueries", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < subqueries.count(); i++) {
if (OB_ISNULL(subqueries.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("null stmt", K(ret));
} else if (OB_FAIL(SMART_CALL(subqueries.at(i)->iterate_stmt_expr(visitor)))) {
LOG_WARN("failed to iterate over all exprs", K(ret));
}
}
}
return ret;
}
int ObDMLStmt::iterate_joined_table_expr(JoinedTable *joined_table,
ObStmtExprVisitor &visitor) const
{
int ret = OB_SUCCESS;
if (OB_ISNULL(joined_table)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Joined table is null", K(ret));
} else if (OB_FAIL(visitor.visit(joined_table->join_conditions_,
DmlStmtScope::SCOPE_JOINED_TABLE))) {
LOG_WARN("failed to visit join conditions", K(ret));
}
if (OB_SUCC(ret) &&
NULL != joined_table->left_table_ &&
joined_table->left_table_->is_joined_table()) {
JoinedTable *left = static_cast<JoinedTable *>(joined_table->left_table_);
if (OB_FAIL(iterate_joined_table_expr(left, visitor))) {
LOG_WARN("failed to visit joined table", K(ret));
}
}
if (OB_SUCC(ret) &&
NULL != joined_table->right_table_ &&
joined_table->right_table_->is_joined_table()) {
JoinedTable *right = static_cast<JoinedTable *>(joined_table->right_table_);
if (OB_FAIL(iterate_joined_table_expr(right, visitor))) {
LOG_WARN("failed to visit joined table", K(ret));
}
}
return ret;
}
int ObDMLStmt::deep_copy_join_tables(ObIAllocator &allocator,
ObIRawExprCopier &expr_copier,
const ObDMLStmt &other)
{
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < other.joined_tables_.count(); ++i) {
JoinedTable *table = NULL;
void *ptr = NULL;
if (OB_ISNULL(other.joined_tables_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("joined table is invalid", K(ret));
} else if (OB_ISNULL(ptr = allocator.alloc(sizeof(JoinedTable)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("falied to allocate memory", K(ret), K(ptr));
} else {
table = new (ptr) JoinedTable();
if (OB_FAIL(table->deep_copy(allocator, expr_copier, *other.joined_tables_.at(i)))) {
LOG_WARN("faield to deep copy joined table", K(ret));
} else if (OB_FAIL(construct_join_table(other, *other.joined_tables_.at(i), *table))) {
LOG_WARN("failed to construct joined table", K(ret));
} else if (OB_FAIL(joined_tables_.push_back(table))) {
LOG_WARN("failed to add joined table", K(ret));
}
}
}
return ret;
}
int ObDMLStmt::construct_join_table(const ObDMLStmt &other_stmt,
const JoinedTable &other,
JoinedTable ¤t)
{
int ret = OB_SUCCESS;
int64_t idx_left = -1;
int64_t idx_right = -1;
if (OB_UNLIKELY(table_items_.count() != other_stmt.table_items_.count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("table item count should be the same", K(table_items_.count()),
K(other_stmt.table_items_.count()), K(ret));
} else if (OB_ISNULL(other.left_table_) || OB_ISNULL(other.right_table_) ||
OB_ISNULL(current.left_table_) || OB_ISNULL(current.right_table_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("null table item", K(other.left_table_), K(other.right_table_),
K(current.left_table_), K(current.right_table_), K(ret));
} else if (OB_UNLIKELY(other.left_table_->type_ != current.left_table_->type_) ||