forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_transform_simplify.cpp
More file actions
3304 lines (3220 loc) · 131 KB
/
ob_transform_simplify.cpp
File metadata and controls
3304 lines (3220 loc) · 131 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_REWRITE
#include "sql/rewrite/ob_transform_simplify.h"
#include "sql/rewrite/ob_transformer_impl.h"
#include "lib/allocator/ob_allocator.h"
#include "lib/oblog/ob_log_module.h"
#include "common/ob_common_utility.h"
#include "share/ob_unit_getter.h"
#include "share/schema/ob_column_schema.h"
#include "sql/ob_sql_context.h"
#include "sql/resolver/expr/ob_raw_expr.h"
#include "sql/resolver/expr/ob_raw_expr_util.h"
#include "sql/optimizer/ob_optimizer_util.h"
#include "sql/code_generator/ob_expr_generator_impl.h"
#include "sql/engine/ob_physical_plan.h"
#include "sql/engine/ob_exec_context.h"
#include "sql/engine/ob_physical_plan.h"
#include "sql/session/ob_sql_session_info.h"
#include "share/config/ob_server_config.h"
#include "sql/rewrite/ob_transform_utils.h"
#include "sql/resolver/dml/ob_select_stmt.h"
#include "common/ob_smart_call.h"
#include "sql/resolver/dml/ob_group_by_checker.h"
#include "sql/rewrite/ob_stmt_comparer.h"
using namespace oceanbase::common;
using namespace oceanbase::share;
using namespace oceanbase::share::schema;
namespace oceanbase {
namespace sql {
int ObTransformSimplify::transform_one_stmt(
common::ObIArray<ObParentDMLStmt>& parent_stmts, ObDMLStmt*& stmt, bool& trans_happened)
{
int ret = OB_SUCCESS;
UNUSED(parent_stmts);
trans_happened = false;
bool is_happened = false;
if (OB_ISNULL(stmt)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("stmt is NULL", K(ret));
} else {
if (OB_SUCC(ret)) {
if (OB_FAIL(remove_stmt_order_by(stmt, is_happened))) {
LOG_WARN("remove stmt order by failed", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to remove stmt order by", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(remove_stmt_win(stmt, is_happened))) {
LOG_WARN("remove stmt group by failed", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to stmt remove group by", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(remove_order_by_duplicates(stmt, is_happened))) {
LOG_WARN("failed to do simplification of order by items", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to remove order by duplicates", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(remove_stmt_group_by(stmt, is_happened))) {
LOG_WARN("remove stmt group by failed", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to stmt remove group by", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(remove_group_by_duplicates(stmt, is_happened))) {
LOG_WARN("failed to remove group by duplicates", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to remove group by duplicates", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(remove_distinct_before_const(stmt, is_happened))) {
LOG_WARN("failed to remove distinct before const", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to remove const distinct", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(remove_stmt_distinct(stmt, is_happened))) {
LOG_WARN("failed to remove stmt distinct", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to remove stmt distinct", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(replace_is_null_condition(stmt, is_happened))) {
LOG_WARN("failed to replace is null condition", K(is_happened));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to replace is null condition", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(push_down_outer_join_condition(stmt, is_happened))) {
LOG_WARN("failed to push down outer join condition", K(is_happened));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to push down outer join condition", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(replace_op_null_condition(stmt, is_happened))) {
LOG_WARN("failed to replace op null condition", K(is_happened));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to replace op null condition", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(convert_preds_vector_to_scalar(stmt, is_happened))) {
LOG_WARN("failed to convert vector predicate to scalar", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to convert vector predicate to scalar", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(transform_subquery_as_expr(stmt, is_happened))) {
LOG_WARN("failed to transform subquery to expr", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to transform subquery to expr", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(remove_dummy_exprs(stmt, is_happened))) {
LOG_WARN("failed to remove dummy exprs", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to remove dummy exprs", K(is_happened), K(ret));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(simplify_win_exprs(stmt, is_happened))) {
LOG_WARN("failed to simplify win expr", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to simplify win expr", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(remove_redundent_select(stmt, is_happened))) {
LOG_WARN("failed to remove simple select", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to remove simple select", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(remove_aggr_distinct(stmt, is_happened))) {
LOG_WARN("failed to remove aggr distinct", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to remove aggr distinct", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(pushdown_limit_offset(stmt, is_happened))) {
LOG_WARN("failed to pushdown limit offset", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to pushdown limit offset", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(remove_redundent_group_by(stmt, is_happened))) {
LOG_WARN("failed to remove redundent group by", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to remove redundent group by", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(stmt->formalize_stmt(ctx_->session_info_))) {
LOG_WARN("failed to formalize stmt", K(ret));
}
}
}
return ret;
}
int ObTransformSimplify::push_down_outer_join_condition(ObDMLStmt* stmt, bool& trans_happened)
{
int ret = OB_SUCCESS;
trans_happened = false;
if (OB_ISNULL(stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt is NULL", K(ret), K(stmt));
} else if (OB_FAIL(ObTransformUtils::right_join_to_left(stmt))) {
LOG_WARN("failed to change right join to left", K(ret));
} else {
ObIArray<JoinedTable*>& join_tables = stmt->get_joined_tables();
for (int64_t i = 0; OB_SUCC(ret) && i < join_tables.count(); ++i) {
bool is_happened = false;
if (OB_FAIL(push_down_outer_join_condition(stmt, join_tables.at(i), is_happened))) {
LOG_WARN("failed to push down outer join condition", K(ret));
} else {
trans_happened |= is_happened;
}
}
}
return ret;
}
int ObTransformSimplify::push_down_outer_join_condition(ObDMLStmt* stmt, TableItem* join_table, bool& trans_happened)
{
int ret = OB_SUCCESS;
trans_happened = false;
bool left_happend = false;
bool right_happend = false;
JoinedTable* cur_joined = NULL;
if (OB_ISNULL(stmt) || OB_ISNULL(join_table)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected NULL", K(ret));
} else if (!join_table->is_joined_table()) {
/*do nothing*/
} else if (FALSE_IT(cur_joined = static_cast<JoinedTable*>(join_table))) {
/*do nothing*/
} else if (OB_FAIL(SMART_CALL(push_down_outer_join_condition(stmt, cur_joined->left_table_, left_happend)))) {
LOG_WARN("failed to push down outer join condition", K(ret));
} else if (OB_FAIL(SMART_CALL(push_down_outer_join_condition(stmt, cur_joined->right_table_, right_happend)))) {
LOG_WARN("failed to push down outer join condition", K(ret));
} else if (OB_FAIL(try_push_down_outer_join_conds(stmt, cur_joined, trans_happened))) {
LOG_WARN("fail to get outer join push down conditions", K(ret));
} else {
trans_happened |= left_happend || right_happend;
}
return ret;
}
int ObTransformSimplify::try_push_down_outer_join_conds(ObDMLStmt* stmt, JoinedTable* join_table, bool& trans_happened)
{
int ret = OB_SUCCESS;
trans_happened = false;
ObSqlBitSet<> right_table_ids;
if (OB_ISNULL(stmt) || OB_ISNULL(join_table) || OB_ISNULL(ctx_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected NULL", K(ret));
} else if (!join_table->is_left_join()) {
/*do nothing*/
} else if (OB_ISNULL(join_table->right_table_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected NULL", K(ret));
} else if (!join_table->right_table_->is_basic_table() && !join_table->right_table_->is_generated_table() &&
!join_table->right_table_->is_joined_table()) {
/*do nothing*/
} else if (OB_FAIL(ObTransformUtils::get_table_rel_ids(*stmt, *join_table->right_table_, right_table_ids))) {
LOG_WARN("failed to get target table rel ids", K(ret));
} else {
ObSEArray<ObRawExpr*, 16> push_down_conds;
ObIArray<ObRawExpr*>& join_conds = join_table->get_join_conditions();
for (int64_t i = 0; OB_SUCC(ret) && i < join_conds.count(); ++i) {
if (OB_ISNULL(join_conds.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected NULL", K(ret));
} else if (join_conds.at(i)->get_relation_ids().is_subset(right_table_ids) &&
join_conds.at(i)->has_flag(CNT_SUB_QUERY)) {
if (OB_FAIL(push_down_conds.push_back(join_conds.at(i)))) {
LOG_WARN("failed to push back expr", K(ret));
}
}
}
TableItem* view_item = NULL;
if (OB_FAIL(ret) || push_down_conds.empty()) {
/*do nothing*/
} else if (OB_FAIL(ObTransformUtils::create_view_with_table(stmt, ctx_, join_table->right_table_, view_item))) {
LOG_WARN("failed to create view with table", K(ret));
} else if (OB_FAIL(push_down_on_condition(stmt, join_table, push_down_conds))) {
LOG_WARN("failed to push down on condition", K(ret));
} else {
trans_happened = true;
}
}
return ret;
}
int ObTransformSimplify::push_down_on_condition(ObDMLStmt* stmt, JoinedTable* join_table, ObIArray<ObRawExpr*>& conds)
{
int ret = OB_SUCCESS;
ObSEArray<ObRawExpr*, 16> old_column_exprs;
ObSEArray<ObRawExpr*, 16> new_column_exprs;
ObSelectStmt* child_stmt = NULL;
if (OB_ISNULL(stmt) || OB_ISNULL(ctx_) || OB_ISNULL(join_table) || OB_ISNULL(join_table->right_table_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected NULL", K(ret));
} else if (!join_table->is_left_join() || !join_table->right_table_->is_generated_table() ||
OB_ISNULL(child_stmt = join_table->right_table_->ref_query_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected join table", K(ret));
} else if (OB_FAIL(ObOptimizerUtil::remove_item(join_table->get_join_conditions(), conds))) {
LOG_WARN("failed to remove item", K(ret));
} else if (OB_FAIL(child_stmt->add_condition_exprs(conds))) {
LOG_WARN("failed to add condotion exprs", K(ret));
} else if (OB_FAIL(ObTransformUtils::extract_query_ref_expr(conds, child_stmt->get_subquery_exprs()))) {
LOG_WARN("failed to adjust subquery list", K(ret));
} else if (OB_FAIL(stmt->adjust_subquery_list())) {
LOG_WARN("failed to adjust subquery list", K(ret));
} else if (OB_FAIL(child_stmt->adjust_subquery_stmt_parent(stmt, child_stmt))) {
LOG_WARN("failed to adjust subquery stmt parent", K(ret));
} else if (OB_FAIL(stmt->get_column_exprs(join_table->right_table_->table_id_, old_column_exprs))) {
LOG_WARN("failed to get column exprs", K(ret));
} else if (OB_FAIL(ObTransformUtils::convert_column_expr_to_select_expr(
old_column_exprs, *child_stmt, new_column_exprs))) {
LOG_WARN("failed to conver column exprs", K(ret));
} else if (OB_FAIL(child_stmt->replace_inner_stmt_expr(old_column_exprs, new_column_exprs))) {
LOG_WARN("failed to replace stmt exprs", K(ret));
} else if (OB_FAIL(child_stmt->formalize_stmt(ctx_->session_info_))) {
LOG_WARN("failed to formalize stmt", K(ret));
}
return ret;
}
// select sum(sc2) from (select sum(c2) sc2 from t ... group by c1);
// --> select sum(c2) from (select c2 from t ...);
int ObTransformSimplify::remove_redundent_group_by(ObDMLStmt* stmt, bool& trans_happened)
{
int ret = OB_SUCCESS;
trans_happened = false;
bool is_valid = false;
ObArray<ObSelectStmt*> valid_child_stmts;
ObSelectStmt* select_stmt = NULL;
if (OB_ISNULL(stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt is null", K(ret));
} else if (!stmt->is_select_stmt() || !stmt->is_single_table_stmt()) {
/*do nothing*/
} else if (FALSE_IT(select_stmt = static_cast<ObSelectStmt*>(stmt))) {
/*do nothing*/
} else if (OB_ISNULL(select_stmt->get_table_item(0))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("table item is null", K(ret));
} else if (!select_stmt->get_table_item(0)->is_generated_table()) {
/*do nothing*/
} else if (OB_FAIL(check_upper_stmt_validity(select_stmt, is_valid))) {
LOG_WARN("failed to check upper stmt validity", K(ret));
} else if (!is_valid) {
/*do nothing*/
} else if (OB_FAIL(
get_valid_child_stmts(select_stmt, select_stmt->get_table_item(0)->ref_query_, valid_child_stmts))) {
LOG_WARN("failed to get valid child stmts", K(ret));
} else if (valid_child_stmts.empty()) {
/*do nothing*/
} else if (OB_FAIL(remove_child_stmts_group_by(valid_child_stmts))) {
LOG_WARN("failed to remove child stmts group by", K(ret));
} else {
trans_happened = true;
}
return ret;
}
int ObTransformSimplify::check_upper_stmt_validity(ObSelectStmt* upper_stmt, bool& is_valid)
{
int ret = OB_SUCCESS;
is_valid = true;
if (!upper_stmt->has_group_by()) {
is_valid = false;
}
for (int64_t i = 0; OB_SUCC(ret) && is_valid && i < upper_stmt->get_condition_size(); ++i) {
ObRawExpr* cond_expr = NULL;
if (OB_ISNULL(cond_expr = upper_stmt->get_condition_expr(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null expr", K(ret));
} else if (cond_expr->has_flag(CNT_ROWNUM)) {
is_valid = false;
}
}
for (int64_t i = 0; OB_SUCC(ret) && is_valid && i < upper_stmt->get_aggr_item_size(); ++i) {
ObAggFunRawExpr* aggr_expr = NULL;
if (OB_ISNULL(aggr_expr = upper_stmt->get_aggr_item(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null aggr", K(ret));
} else if (T_FUN_MIN != aggr_expr->get_expr_type() && T_FUN_MAX != aggr_expr->get_expr_type() &&
T_FUN_SUM != aggr_expr->get_expr_type()) {
is_valid = false;
} else if (T_FUN_SUM == aggr_expr->get_expr_type() && aggr_expr->is_param_distinct()) {
is_valid = false;
}
}
if (OB_SUCC(ret) && is_valid && lib::is_mysql_mode()) {
int ret_tmp = ObGroupByChecker::check_group_by(upper_stmt);
if (OB_ERR_WRONG_FIELD_WITH_GROUP == ret_tmp) {
is_valid = false;
} else if (OB_FAIL(ret_tmp)) {
LOG_WARN("failed to check group by", K(ret));
}
}
return ret;
}
int ObTransformSimplify::get_valid_child_stmts(
ObSelectStmt* upper_stmt, ObSelectStmt* stmt, ObArray<ObSelectStmt*>& valid_child_stmts)
{
int ret = OB_SUCCESS;
bool is_valid = true;
bool has_rownum = false;
ObArray<ObRawExpr*> aggr_column_exprs;
ObArray<ObRawExpr*> no_aggr_column_exprs;
ObArray<ObRawExpr*> child_aggr_exprs;
if (OB_ISNULL(upper_stmt) || OB_ISNULL(stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt is null", K(ret));
} else if (stmt->is_set_stmt()) {
if (ObSelectStmt::UNION != stmt->get_set_op() || stmt->is_set_distinct() || stmt->has_limit()) {
is_valid = false;
} else {
ObIArray<ObSelectStmt*>& child_stmts = stmt->get_set_query();
for (int64_t i = 0; OB_SUCC(ret) && i < child_stmts.count(); ++i) {
ret = SMART_CALL(get_valid_child_stmts(upper_stmt, child_stmts.at(i), valid_child_stmts));
}
}
} else if (stmt->has_rollup() || stmt->has_window_function() || stmt->has_having() || stmt->has_sequence() ||
stmt->has_limit()) {
is_valid = false;
} else if (!((stmt->has_distinct() && !stmt->has_group_by()) || (!stmt->has_distinct() && stmt->has_group_by()))) {
// two scenarios will be allowed:
// distinct without group by & group by without distinct.
// cases with both distinct and group by, expected to be handled
// by remove_stmt_distinct rule before.
is_valid = false;
} else if (OB_FAIL(stmt->has_rownum(has_rownum))) {
LOG_WARN("failed to check has rownum", K(ret));
} else if (has_rownum) {
is_valid = false;
} else if (OB_FAIL(get_upper_column_exprs(
*upper_stmt, *stmt, aggr_column_exprs, no_aggr_column_exprs, child_aggr_exprs, is_valid))) {
LOG_WARN("failed to get upper column exprs", K(ret));
} else if (!is_valid) {
/*do nothing*/
} else if (OB_FAIL(check_upper_group_by(*upper_stmt, no_aggr_column_exprs, is_valid))) {
LOG_WARN("failed to check upper group by", K(ret));
} else if (!is_valid) {
/*do nothing*/
} else if (OB_FAIL(check_aggrs_matched(
upper_stmt->get_aggr_items(), aggr_column_exprs, no_aggr_column_exprs, child_aggr_exprs, is_valid))) {
LOG_WARN("failed to check aggrs matched", K(ret));
} else if (!is_valid) {
/*do nothing*/
} else if (OB_FAIL(check_upper_condition(upper_stmt->get_condition_exprs(), aggr_column_exprs, is_valid))) {
LOG_WARN("failed to check upper condition", K(ret));
} else if (!is_valid) {
/*do nothing*/
} else if (OB_FAIL(valid_child_stmts.push_back(stmt))) {
LOG_WARN("failed to push back child stmt", K(ret));
} else {
/*do nothing*/
}
return ret;
}
int ObTransformSimplify::remove_child_stmts_group_by(ObArray<ObSelectStmt*>& child_stmts)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(ctx_) || OB_ISNULL(ctx_->session_info_) || OB_ISNULL(ctx_->expr_factory_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(ctx_));
}
for (int64_t i = 0; OB_SUCC(ret) && i < child_stmts.count(); ++i) {
ObSelectStmt* stmt = NULL;
if (OB_ISNULL(stmt = child_stmts.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null", K(ret));
} else {
stmt->get_aggr_items().reset();
stmt->get_group_exprs().reset();
stmt->reassign_rollup();
stmt->get_rollup_exprs().reset();
stmt->get_rollup_dirs().reset();
stmt->get_order_items().reset();
stmt->assign_all();
for (int64_t i = 0; OB_SUCC(ret) && i < stmt->get_select_item_size(); ++i) {
ObRawExpr* expr = stmt->get_select_item(i).expr_;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null", K(ret));
} else if (!expr->is_aggr_expr()) {
/*do nothing*/
} else if (T_FUN_MAX == expr->get_expr_type() || T_FUN_MIN == expr->get_expr_type() ||
T_FUN_SUM == expr->get_expr_type()) {
ObRawExpr* cast_expr;
if (OB_FAIL(ObRawExprUtils::try_add_cast_expr_above(ctx_->expr_factory_,
ctx_->session_info_,
*expr->get_param_expr(0),
expr->get_result_type(),
cast_expr))) {
LOG_WARN("try add cast expr above failed", K(ret));
} else {
stmt->get_select_item(i).expr_ = cast_expr;
}
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected aggr", K(ret), K(expr->get_expr_type()));
}
}
}
}
return ret;
}
int ObTransformSimplify::get_upper_column_exprs(ObSelectStmt& upper_stmt, ObSelectStmt& stmt,
ObIArray<ObRawExpr*>& aggr_column_exprs, ObIArray<ObRawExpr*>& no_aggr_column_exprs,
ObIArray<ObRawExpr*>& child_aggr_exprs, bool& is_valid)
{
int ret = OB_SUCCESS;
is_valid = true;
TableItem* table_item;
ObSelectStmt* ref_query;
if (OB_UNLIKELY(!upper_stmt.is_single_table_stmt()) || OB_ISNULL(table_item = upper_stmt.get_table_item(0)) ||
OB_UNLIKELY(!table_item->is_generated_table()) || OB_ISNULL(ref_query = table_item->ref_query_) ||
OB_UNLIKELY(stmt.get_select_item_size() != ref_query->get_select_item_size())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected upper stmt", K(ret), K(upper_stmt));
}
for (int64_t i = 0; OB_SUCC(ret) && is_valid && i < stmt.get_select_item_size(); ++i) {
ObRawExpr* stmt_select_expr = NULL;
ColumnItem* column_item = NULL;
if (OB_ISNULL(stmt_select_expr = stmt.get_select_item(i).expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_ISNULL(
column_item = upper_stmt.get_column_item_by_id(table_item->table_id_, i + OB_APP_MIN_COLUMN_ID))) {
// disable these aggr expr which can not be found in upper stmt,
// otherwise, following elimination will meet unexpected, such as
// inner stmt count(1) cnt with upper stmt max.
if (stmt_select_expr->is_aggr_expr()) {
is_valid = false;
}
} else if (OB_ISNULL(column_item->expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null expr", K(ret));
} else if (stmt_select_expr->is_aggr_expr()) { // is aggr
int64_t idx = OB_INVALID_INDEX;
if (!ObOptimizerUtil::find_item(stmt.get_aggr_items(), static_cast<ObAggFunRawExpr*>(stmt_select_expr), &idx)) {
is_valid = false;
LOG_DEBUG("cannot find aggr in stmt aggr item", K(ret), K(stmt), K(*stmt_select_expr));
} else if (OB_FAIL(aggr_column_exprs.push_back(column_item->expr_)) ||
OB_FAIL(child_aggr_exprs.push_back(stmt.get_aggr_items().at(idx)))) {
LOG_WARN("failed to push back aggr", K(ret));
}
} else if (ObOptimizerUtil::find_item(stmt.get_group_exprs(), stmt_select_expr) ||
stmt_select_expr->has_const_or_const_expr_flag() || (stmt.has_distinct() && !stmt.has_group_by())) {
// in group by or is const or distinct without group by
if (OB_FAIL(no_aggr_column_exprs.push_back(column_item->expr_))) {
LOG_WARN("failed to push back expr", K(ret));
}
} else {
is_valid = false;
}
}
return ret;
}
int ObTransformSimplify::check_upper_group_by(
ObSelectStmt& upper_stmt, ObIArray<ObRawExpr*>& no_aggr_column_exprs, bool& is_valid)
{
int ret = OB_SUCCESS;
is_valid = false;
if (!ObOptimizerUtil::subset_exprs(upper_stmt.get_rollup_exprs(), no_aggr_column_exprs)) {
/*do nothing*/
} else if (!ObOptimizerUtil::subset_exprs(upper_stmt.get_group_exprs(), no_aggr_column_exprs)) {
/*do nothing*/
} else {
is_valid = true;
}
return ret;
}
int ObTransformSimplify::check_aggrs_matched(ObIArray<ObAggFunRawExpr*>& upper_aggrs,
ObIArray<ObRawExpr*>& aggr_column_exprs, ObIArray<ObRawExpr*>& no_aggr_column_exprs,
ObIArray<ObRawExpr*>& child_aggr_exprs, bool& is_valid)
{
int ret = OB_SUCCESS;
is_valid = true;
if (aggr_column_exprs.count() != child_aggr_exprs.count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected array count", K(ret), K(aggr_column_exprs), K(child_aggr_exprs));
}
for (int64_t i = 0; OB_SUCC(ret) && is_valid && i < upper_aggrs.count(); ++i) {
ObAggFunRawExpr* upper_aggr = NULL;
ObRawExpr* aggr_param = NULL;
int64_t idx = OB_INVALID_INDEX;
if (OB_ISNULL(upper_aggr = upper_aggrs.at(i)) || OB_ISNULL(aggr_param = upper_aggr->get_param_expr(0))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null", K(ret));
} else if (ObOptimizerUtil::find_item(aggr_column_exprs, aggr_param, &idx)) {
const ObAggFunRawExpr* child_aggr = static_cast<ObAggFunRawExpr*>(child_aggr_exprs.at(idx));
if (upper_aggr->get_expr_type() != child_aggr->get_expr_type()) {
is_valid = false;
} else if (T_FUN_SUM == child_aggr->get_expr_type() && child_aggr->is_param_distinct()) {
is_valid = false;
} else {
/*do nothing*/
}
} else if (ObOptimizerUtil::find_item(no_aggr_column_exprs, aggr_param) ||
aggr_param->has_const_or_const_expr_flag()) {
is_valid = T_FUN_MAX == upper_aggr->get_expr_type() || T_FUN_MIN == upper_aggr->get_expr_type();
} else {
is_valid = false;
}
}
return ret;
}
// check upper stmt condition does not contain stmt aggr result
int ObTransformSimplify::check_upper_condition(
ObIArray<ObRawExpr*>& cond_exprs, ObIArray<ObRawExpr*>& aggr_column_exprs, bool& is_valid)
{
int ret = OB_SUCCESS;
bool find_expr = false;
int64_t N = cond_exprs.count();
for (int64_t i = 0; OB_SUCC(ret) && !find_expr && i < N; ++i) {
ret = exist_exprs_in_expr(cond_exprs.at(i), aggr_column_exprs, find_expr);
}
is_valid = !find_expr;
return ret;
}
int ObTransformSimplify::exist_exprs_in_expr(const ObRawExpr* src_expr, ObIArray<ObRawExpr*>& dst_exprs, bool& is_exist)
{
int ret = OB_SUCCESS;
is_exist = false;
if (OB_ISNULL(src_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected NULL", K(ret));
} else if (dst_exprs.empty()) {
/*do nothing*/
} else if (ObOptimizerUtil::find_item(dst_exprs, src_expr)) {
is_exist = true;
} else {
int64_t N = src_expr->get_param_count();
for (int64_t i = 0; OB_SUCC(ret) && !is_exist && i < N; ++i) {
const ObRawExpr* tmp_expr = src_expr->get_param_expr(i);
bool is_correlated = false;
if (OB_ISNULL(tmp_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected NULL", K(ret));
} else if (tmp_expr->is_query_ref_expr()) {
if (OB_FAIL(ObTransformUtils::is_correlated_expr(tmp_expr, dst_exprs.at(0)->get_expr_level(), is_correlated))) {
LOG_WARN("failed to check correlated expr", K(ret));
} else if (is_correlated) {
is_exist = true;
}
} else if (OB_FAIL(SMART_CALL(exist_exprs_in_expr(tmp_expr, dst_exprs, is_exist)))) {
LOG_WARN("failed to smart call", K(ret));
}
}
}
return ret;
}
int ObTransformSimplify::replace_is_null_condition(ObDMLStmt* stmt, bool& trans_happened)
{
int ret = OB_SUCCESS;
trans_happened = false;
bool is_happened = false;
if (OB_ISNULL(stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("data member or parameter is NULL", K(stmt), K(ctx_));
} else if (stmt->is_sel_del_upd()) {
for (int64_t i = 0; OB_SUCC(ret) && i < stmt->get_condition_size(); ++i) {
ObRawExpr* cond = NULL;
if (OB_ISNULL(cond = stmt->get_condition_expr(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("condition expr is null", K(ret));
} else if (OB_FAIL(inner_replace_is_null_condition(stmt, stmt->get_condition_exprs().at(i), is_happened))) {
LOG_WARN("failed to replace is null expr", K(ret));
} else {
trans_happened |= is_happened;
}
}
if (OB_SUCC(ret) && stmt->is_select_stmt()) {
ObSelectStmt* sel_stmt = static_cast<ObSelectStmt*>(stmt);
for (int64_t i = 0; OB_SUCC(ret) && i < sel_stmt->get_having_expr_size(); ++i) {
if (OB_FAIL(inner_replace_is_null_condition(sel_stmt, sel_stmt->get_having_exprs().at(i), is_happened))) {
LOG_WARN("failed to replace is null expr", K(ret));
} else {
trans_happened |= is_happened;
}
}
}
}
return ret;
}
int ObTransformSimplify::inner_replace_is_null_condition(ObDMLStmt* stmt, ObRawExpr*& expr, bool& trans_happened)
{
int ret = OB_SUCCESS;
bool is_happened = false;
trans_happened = false;
bool is_stack_overflow = false;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("null expr", K(expr), K(ret));
} else if (OB_FAIL(check_stack_overflow(is_stack_overflow))) {
LOG_WARN("failed to check stack overflow", K(ret));
} else if (is_stack_overflow) {
ret = OB_SIZE_OVERFLOW;
LOG_WARN("too deep recursive", K(ret));
} else if (expr->is_op_expr()) {
// do transformation for child exprs first
ObOpRawExpr* op_expr = static_cast<ObOpRawExpr*>(expr);
for (int64_t i = 0; OB_SUCC(ret) && i < op_expr->get_param_count(); i++) {
ObRawExpr* temp = NULL;
if (OB_ISNULL(temp = op_expr->get_param_expr(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("null expr", K(ret));
} else if (OB_FAIL(SMART_CALL(inner_replace_is_null_condition(stmt, temp, is_happened)))) {
LOG_WARN("failed to replace is null expr", K(ret));
} else {
trans_happened |= is_happened;
op_expr->get_param_expr(i) = temp;
}
}
}
if (OB_SUCC(ret) && (T_OP_IS == expr->get_expr_type() || T_OP_IS_NOT == expr->get_expr_type())) {
// do transforamtion for its own exprs
if (OB_FAIL(do_replace_is_null_condition(stmt, expr, is_happened))) {
LOG_WARN("failed to replace is null condition", K(ret));
} else {
trans_happened |= is_happened;
}
}
if (OB_SUCC(ret) && trans_happened) {
if (OB_ISNULL(ctx_) || OB_ISNULL(ctx_->session_info_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ctx_), K(ret));
} else if (OB_FAIL(expr->formalize(ctx_->session_info_))) {
LOG_WARN("failed to formalize expr", K(ret));
} else { /*do nothing*/
}
}
return ret;
}
int ObTransformSimplify::do_replace_is_null_condition(ObDMLStmt* stmt, ObRawExpr*& expr, bool& trans_happened)
{
int ret = OB_SUCCESS;
trans_happened = false;
if (OB_ISNULL(expr) || OB_ISNULL(stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(stmt), K(expr), K(ret));
} else if (T_OP_IS == expr->get_expr_type() || T_OP_IS_NOT == expr->get_expr_type()) {
const ObOpRawExpr* op_expr = static_cast<ObOpRawExpr*>(expr);
if (OB_UNLIKELY(op_expr->get_param_count() != 3)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected param numm", K(op_expr->get_param_count()), K(ret));
} else {
const ObRawExpr* child_0 = op_expr->get_param_expr(0);
const ObRawExpr* child_1 = op_expr->get_param_expr(1);
const ObRawExpr* child_2 = op_expr->get_param_expr(2);
if (OB_ISNULL(child_0) || OB_ISNULL(child_1) || OB_ISNULL(child_2)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpecte null", K(child_0), K(child_1), K(child_2), K(ret));
} else if (child_1->get_expr_type() == T_NULL && child_2->get_expr_type() == T_BOOL &&
!static_cast<const ObConstRawExpr*>(child_2)->get_value().get_bool()) {
bool is_expected = false;
if (child_0->get_expr_type() == T_FUN_COUNT) {
is_expected = true;
} else if (child_0->is_column_ref_expr()) {
bool sql_auto_is_null = false;
const ObColumnRefRawExpr* col_expr = static_cast<const ObColumnRefRawExpr*>(child_0);
uint64_t table_id = col_expr->get_table_id();
if (OB_FAIL(ctx_->session_info_->get_sql_auto_is_null(sql_auto_is_null))) {
LOG_WARN("fail to get sql_auto_is_null system variable", K(ret));
} else if (col_expr->is_not_null()) {
if (T_OP_IS_NOT == expr->get_expr_type() ||
(T_OP_IS == expr->get_expr_type() && !col_expr->get_result_type().is_datetime() &&
!col_expr->get_result_type().is_date() && !(sql_auto_is_null && col_expr->is_auto_increment()))) {
if (OB_FAIL(is_expected_table_for_replace(stmt, table_id, is_expected))) {
LOG_WARN("fail to judge expected table", K(ret), K(table_id), K(is_expected));
}
}
}
}
if (OB_SUCC(ret) && is_expected) {
bool b_value = (T_OP_IS_NOT == expr->get_expr_type());
if (OB_FAIL(ObRawExprUtils::build_const_bool_expr(ctx_->expr_factory_, expr, b_value))) {
LOG_WARN("create const bool expr failed", K(ret));
} else {
trans_happened = true;
}
}
}
}
}
return ret;
}
// rewrite null-reject conditions
int ObTransformSimplify::replace_op_null_condition(ObDMLStmt* stmt, bool& trans_happened)
{
int ret = OB_SUCCESS;
bool is_happened = false;
trans_happened = false;
ObPhysicalPlanCtx* plan_ctx = NULL;
if (OB_ISNULL(stmt) || OB_ISNULL(ctx_) || OB_ISNULL(ctx_->exec_ctx_) ||
OB_ISNULL(plan_ctx = ctx_->exec_ctx_->get_physical_plan_ctx())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt is NULL", K(ret), K(stmt), K(ctx_), K(plan_ctx));
}
for (int64_t i = 0; OB_SUCC(ret) && i < stmt->get_condition_size(); ++i) {
ObRawExpr* cond = NULL;
if (OB_ISNULL(cond = stmt->get_condition_expr(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("condition expr is null", K(ret));
} else if (OB_FAIL(replace_cmp_null_condition(
stmt->get_condition_exprs().at(i), *stmt, plan_ctx->get_param_store(), is_happened))) {
LOG_WARN("function replace_cmp_null_condition is failure", K(ret));
} else {
trans_happened |= is_happened;
}
}
if (OB_SUCC(ret) && stmt->is_select_stmt()) {
ObSelectStmt* sel_stmt = static_cast<ObSelectStmt*>(stmt);
for (int64_t i = 0; OB_SUCC(ret) && i < sel_stmt->get_having_expr_size(); ++i) {
if (OB_FAIL(replace_cmp_null_condition(
sel_stmt->get_having_exprs().at(i), *stmt, plan_ctx->get_param_store(), is_happened))) {
LOG_WARN("function replace_cmp_null_condition is failure", K(ret));
} else {
trans_happened |= is_happened;
}
}
}
return ret;
}
int ObTransformSimplify::replace_cmp_null_condition(
ObRawExpr*& expr, const ObDMLStmt& stmt, const ParamStore& param_store, bool& trans_happened)
{
int ret = OB_SUCCESS;
trans_happened = false;
bool is_null_reject = false;
bool is_happened = false;
ObSEArray<const ObRawExpr*, 4> null_expr_lists;
bool is_stack_overflow = false;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("null expr", K(ret), K(expr));
} else if (OB_FAIL(check_stack_overflow(is_stack_overflow))) {
LOG_WARN("failed to check stack overflow", K(ret));
} else if (is_stack_overflow) {
ret = OB_SIZE_OVERFLOW;
LOG_WARN("too deep recursive", K(ret), K(is_stack_overflow));
} else if (T_OP_OR == expr->get_expr_type()) {
for (int64_t i = 0; OB_SUCC(ret) && i < expr->get_param_count(); ++i) {
if (OB_FAIL(SMART_CALL(replace_cmp_null_condition(expr->get_param_expr(i), stmt, param_store, is_happened)))) {
LOG_WARN("or type execute failure", K(expr), K(ret));
} else {
trans_happened |= is_happened;
}
}
} else if (OB_FAIL(extract_null_expr(expr, stmt, param_store, null_expr_lists))) {
LOG_WARN("failed to use extract_null_expr funciton", K(ret), K(ctx_));
} else if (0 != null_expr_lists.count()) {
if (OB_FAIL(ObTransformUtils::is_null_reject_condition(expr, null_expr_lists, is_null_reject))) {
LOG_WARN("failed to use is_null_reject_condition function", K(ret));
} else if (is_null_reject) {
bool b_value = false;
if (OB_FAIL(ObRawExprUtils::build_const_bool_expr(ctx_->expr_factory_, expr, b_value))) {
LOG_WARN("create const bool expr failed", K(ret));
} else {
trans_happened = true;
}
}
} else {
/*do nothing*/
}
return ret;
}
int ObTransformSimplify::extract_null_expr(
ObRawExpr* expr, const ObDMLStmt& stmt, const ParamStore& param_store, ObIArray<const ObRawExpr*>& null_expr_lists)
{
int ret = OB_SUCCESS;
bool is_stack_overflow = false;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("null expr", K(expr), K(ret));
} else if (OB_FAIL(check_stack_overflow(is_stack_overflow))) {
LOG_WARN("failed to check stack overflow", K(ret));
} else if (is_stack_overflow) {
ret = OB_SIZE_OVERFLOW;
LOG_WARN("too deep recursive", K(ret), K(is_stack_overflow));
} else if (T_NULL == expr->get_expr_type()) {
if (OB_FAIL(null_expr_lists.push_back(expr))) {
LOG_WARN("null expr", K(expr), K(ret));
} else {
/*do notiing*/
}
} else if (T_QUESTIONMARK == expr->get_expr_type()) {
const ObObj& value = static_cast<ObConstRawExpr*>(expr)->get_value();
bool is_pre_param = false;
if (OB_FAIL(ObTransformUtils::is_question_mark_pre_param(stmt, value.get_unknown(), is_pre_param))) {
LOG_WARN("failed to check is question mark pre param", K(ret));
} else if (!is_pre_param) {
// do nothing
} else if (param_store.at(value.get_unknown()).is_null()) {
if (OB_FAIL(null_expr_lists.push_back(expr))) {
LOG_WARN("failed to push back expr", K(ret));
}
}
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < expr->get_param_count(); ++i) {
ObRawExpr* temp = NULL;
if (OB_ISNULL(temp = expr->get_param_expr(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("null expr", K(ret));
} else if (OB_FAIL(SMART_CALL(extract_null_expr(temp, stmt, param_store, null_expr_lists)))) {
LOG_WARN("failed to use extract_null_expr function", K(ret));
} else {
/*do nothing*/
}
}
}
return ret;
}
int ObTransformSimplify::is_expected_table_for_replace(ObDMLStmt* stmt, uint64_t table_id, bool& is_expected)
{
int ret = OB_SUCCESS;
is_expected = true;
const TableItem* table_item = NULL;
bool is_stack_overflow = false;
if (OB_ISNULL(stmt)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid parameter", K(stmt));
} else if (OB_FAIL(check_stack_overflow(is_stack_overflow))) {
LOG_WARN("failed to check stack overflow", K(ret));
} else if (is_stack_overflow) {
ret = OB_SIZE_OVERFLOW;
LOG_WARN("too deep recursive", K(ret), K(is_stack_overflow));
} else if (NULL == (table_item = stmt->get_table_item_by_id(table_id))) {
// table from upper stmt
ObStmt* upper_stmt = NULL;
if (OB_ISNULL(upper_stmt = stmt->get_parent_namespace_stmt())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("upper stmt shouldn't be NULL", K(ret));
} else if (OB_UNLIKELY(!upper_stmt->is_dml_stmt())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("upper stmt must be dml", K(upper_stmt->get_stmt_type()));
} else if (OB_FAIL(SMART_CALL(
is_expected_table_for_replace(static_cast<ObDMLStmt*>(upper_stmt), table_id, is_expected)))) {
LOG_WARN("fail to judge expected table", K(ret));
} else { /*do nothing*/
}
} else {
// table from current stmt
if (table_item->is_basic_table() || table_item->is_generated_table()) {
bool is_on_null_side = false;
if (OB_FAIL(ObOptimizerUtil::is_table_on_null_side(stmt, table_item->table_id_, is_on_null_side))) {
LOG_WARN("check is table on null side failed", K(ret), K(*table_item));
} else {
is_expected = !is_on_null_side;
}
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected table item type", K_(table_item->type));
}
}
return ret;
}
/*
* select * from t1 where c1 in (select c1 from t2 order by c2);
* ==>
* select * from t1 where c1 in (select c1 from t2);
*
* select distinct c1 from (select c1 from t1 order by c2);
* ==>
* select distinct c1 from (select c1 from t1);
*
* select * from (select * from t1 order by c1) order by c2;
* ==>