forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_transform_pre_process.cpp
More file actions
3943 lines (3831 loc) · 169 KB
/
ob_transform_pre_process.cpp
File metadata and controls
3943 lines (3831 loc) · 169 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_pre_process.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 "common/ob_smart_call.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/engine/expr/ob_expr_arg_case.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 "sql/resolver/dml/ob_select_resolver.h"
#include "sql/resolver/dml/ob_merge_stmt.h"
#include "sql/resolver/dml/ob_merge_resolver.h"
#include "sql/rewrite/ob_expand_aggregate_utils.h"
using namespace oceanbase::common;
using namespace oceanbase::share;
using namespace oceanbase::share::schema;
namespace oceanbase {
using namespace common;
namespace sql {
int ObTransformPreProcess::transform_one_stmt(
common::ObIArray<ObParentDMLStmt>& parent_stmts, ObDMLStmt*& stmt, bool& trans_happened)
{
int ret = OB_SUCCESS;
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_FAIL(eliminate_having(stmt, is_happened))) {
LOG_WARN("faield to elinimate having", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to eliminating having statement", K(is_happened));
}
if (OB_SUCC(ret)) {
if (OB_FAIL(transform_for_materialized_view(stmt, is_happened))) {
LOG_WARN("failed to transform for materialized view", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to transform materialized_view", K(ret));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(replace_func_is_serving_tenant(stmt, is_happened))) {
LOG_WARN("failed to replace function is_serving_tenant", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to replace function", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(transform_for_temporary_table(stmt, is_happened))) {
LOG_WARN("failed to transform for temporary table", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to transform for temporary table", K(is_happened), K(ret));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(transform_for_merge_into(stmt, is_happened))) {
LOG_WARN("failed to transform for merge into", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to transform for merge into", K(is_happened), K(ret));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(transform_exprs(stmt, is_happened))) {
LOG_WARN("transform exprs failed", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("success to transform exprs", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(transform_for_nested_aggregate(stmt, is_happened))) {
LOG_WARN("failed to transform for nested aggregate.", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to transform for nested aggregate", K(is_happened), K(ret));
}
if (OB_SUCC(ret)) {
if (OB_FAIL(transformer_aggr_expr(stmt, is_happened))) {
LOG_WARN("failed to transform aggr expr", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to transform aggr expr", K(is_happened), K(ret));
}
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(transform_for_hierarchical_query(stmt, is_happened))) {
LOG_WARN("failed to transform for hierarchical query", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to transform hierarchical query", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(transform_rownum_as_limit_offset(parent_stmts, stmt, is_happened))) {
LOG_WARN("failed to transform rownum as limit", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to transform rownum as limit", K(is_happened));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(transform_for_grouping_sets_and_multi_rollup(stmt, is_happened))) {
LOG_WARN("failed to transform for transform for grouping sets and multi rollup.", K(ret));
} else {
trans_happened |= is_happened;
LOG_TRACE("succeed to transform for grouping sets and multi rollup", K(is_happened), K(ret));
}
}
if (OB_SUCC(ret)) {
LOG_DEBUG("transform pre process succ", K(*stmt));
if (OB_FAIL(stmt->formalize_stmt(ctx_->session_info_))) {
LOG_WARN("failed to formalize stmt", K(ret));
}
}
}
return ret;
}
/*@brief,ObTransformPreProcess::transform_for_grouping_sets_and_multi_rollup, tranform stmt with
* grouping sets or multi rollup to equal set stmt.
* for grouping sets stmt:
* 1.select c1,c2 from t1 group by grouping sets(c1,c2);
* <==>
* select c1, NULL from t1 group by c1
*union all
* select NULL, c2 from t1 group by c1;
*
* 2.select c1,c2,c3,c4 from t1 group by grouping sets(c1,c2), grouping sets(c3,c4);
*<==>
* select c1,NULL,c3,NULL from t1 group by c1,c3
* union all
* select c1,NULL,NULL,c4 from t1 group by c1,c4
* union all
* select NULL,c2,c3,NULL from t1 group by c2,c3
* union all
* select NULL,c2,NULL,c4 from t1 group by c2,c4;
*
* as above, {c1,c2} * {c3,c4} ==> Cartesian product
*
* for multi rollup stmt:
* 1.select c1,c2,c3,sum(c3) from t1 group by rollup((c1,c2),c3);
* <==>
* select c1,c2,c3,sum(c3) from t1 group by c1,c2,c3
* union all
* select c1,c2,NULL,sum(c3) from t1 group by c1,c2
* union all
* select NULL,NULL,NULL,sum(c3) from t1;
*
* 2.select c1,c2,c3,sum(c3) from t1 group by rollup (c1,c2),rollup(c3);
* <==>
* select c1,c2,c3,sum(c3) from t1 group by c1,c2,c3
* union all
* select c1,c2,NULL,sum(c3) from t1 group by c1,c2
* union all
* select c1,NULL,c3,sum(c3) from t1 group by c1,c3
* union all
* select c1,NULL,NULL,sum(c3) from t1 group by c1
* union all
* select NULL,NULL,c3,sum(c3) from t1 group by c3
* union all
* select NULL,NULL,NULL,sum(c3) from t1;
*
* as above, {(c1,c2),(c1),(NULL)} * {(c3),(NULL)} ==> Cartesian product
*/
int ObTransformPreProcess::transform_for_grouping_sets_and_multi_rollup(ObDMLStmt*& stmt, bool& trans_happened)
{
int ret = OB_SUCCESS;
trans_happened = false;
if (OB_ISNULL(stmt) || OB_ISNULL(ctx_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt or ctx is null.", K(ret));
} else if (!stmt->is_select_stmt()) {
/* do nothing.*/
} else {
ObSelectStmt* select_stmt = static_cast<ObSelectStmt*>(stmt);
ObIArray<ObGroupingSetsItem>& grouping_sets_items = select_stmt->get_grouping_sets_items();
ObIArray<ObMultiRollupItem>& multi_rollup_items = select_stmt->get_multi_rollup_items();
if (!select_stmt->has_grouping_sets() && multi_rollup_items.count() == 0) {
/* do nothing.*/
} else if (OB_UNLIKELY(grouping_sets_items.count() == 0 && multi_rollup_items.count() == 0)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error",
K(select_stmt->has_grouping_sets()),
K(multi_rollup_items.count()),
K(grouping_sets_items.count()));
} else if (grouping_sets_items.count() == 1 && grouping_sets_items.at(0).grouping_sets_exprs_.count() == 1 &&
grouping_sets_items.at(0).multi_rollup_items_.count() == 0 && multi_rollup_items.count() == 0) {
// if grouping sets expr has only one,we can remove grouping sets directly.
if (OB_FAIL(append(
select_stmt->get_group_exprs(), grouping_sets_items.at(0).grouping_sets_exprs_.at(0).groupby_exprs_))) {
LOG_WARN("failed to append group exprs", K(ret));
} else {
grouping_sets_items.reset();
bool is_happened = false;
if (!select_stmt->has_group_by() && OB_FAIL(ObTransformUtils::set_limit_expr(select_stmt, ctx_))) {
LOG_WARN("add limit expr failed", K(ret));
} else if (!select_stmt->has_group_by() && OB_FAIL(eliminate_having(select_stmt, is_happened))) {
LOG_WARN("failed to eliminate having", K(ret));
} else {
trans_happened = true;
LOG_TRACE("eliminate having", K(is_happened));
}
}
} else if (select_stmt->get_stmt_hint().use_px_ == ObUsePxHint::DISABLE) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support no use px for grouping sets.", K(ret));
} else {
// as following steps to transform grouping sets stmt to set stmt:
// 1. Creating spj stmt from origin stmt;
// 2. According to the spj stmt separated in the previous step, creating temp table and add it
// to origin stmt
// 3. According to grouping sets items or multi rollup items and stmt created in the previous
// step,creating set stmt
// 4. Merging set stmt created in the previous step and origin stmt into transform stmt.
ObSelectStmt* view_stmt = NULL;
ObSelectStmt* transform_stmt = NULL;
ObSelectStmt* set_view_stmt = NULL;
// step 1, creating spj stmt
if (OB_FAIL(ObTransformUtils::create_simple_view(ctx_, select_stmt, view_stmt))) {
LOG_WARN("failed to create spj view.", K(ret));
// step 2, creating temp table
} else if (OB_FAIL(add_generated_table_as_temp_table(ctx_, select_stmt))) {
LOG_WARN("failed to add generated table as temp table", K(ret));
// setp 3, creating set stmt
} else if (OB_FAIL(create_set_view_stmt(select_stmt, set_view_stmt))) {
LOG_WARN("failed to create grouping sets view.", K(ret));
// step 4, merge stmt
} else if (OB_FAIL(replace_with_set_stmt_view(select_stmt, set_view_stmt, transform_stmt))) {
LOG_WARN("failed to create union view for grouping sets.", K(ret));
} else {
stmt = transform_stmt;
trans_happened = true;
LOG_TRACE("succeed to transform transform for grouping sets and multi rollup", K(*stmt));
}
}
}
return ret;
}
int ObTransformPreProcess::add_generated_table_as_temp_table(ObTransformerCtx* ctx, ObDMLStmt* stmt)
{
int ret = OB_SUCCESS;
TableItem* table_item = NULL;
if (OB_ISNULL(ctx) || OB_ISNULL(ctx->allocator_) || OB_ISNULL(stmt) || OB_ISNULL(stmt->get_query_ctx())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ctx), K(stmt), K(ret));
} else if (OB_UNLIKELY(1 != stmt->get_table_items().count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(stmt->get_table_items().count()), K(ret));
} else if (OB_ISNULL(table_item = stmt->get_table_item(0)) || OB_ISNULL(table_item->ref_query_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(table_item), K(ret));
} else {
table_item->type_ = TableItem::TEMP_TABLE;
table_item->ref_id_ = ObSqlTempTableInfo::generate_temp_table_id();
ObSqlTempTableInfo* temp_table_info = NULL;
void* ptr = NULL;
if (OB_ISNULL(ptr = ctx->allocator_->alloc(sizeof(ObSqlTempTableInfo)))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else {
temp_table_info = new (ptr) ObSqlTempTableInfo();
temp_table_info->ref_table_id_ = table_item->ref_id_;
temp_table_info->table_query_ = table_item->ref_query_;
temp_table_info->table_query_->set_temp_table_info(temp_table_info);
if (OB_FAIL(stmt->generate_view_name(*ctx->allocator_, temp_table_info->table_name_, true))) {
LOG_WARN("failed to generate view name", K(ret));
} else if (OB_FAIL(stmt->get_query_ctx()->add_temp_table(temp_table_info))) {
LOG_WARN("failed to add temp table", K(ret));
} else {
// adjust temp-table name
table_item->table_name_ = temp_table_info->table_name_;
table_item->alias_name_ = table_item->table_name_;
for (int64_t i = 0; OB_SUCC(ret) && i < stmt->get_column_items().count(); i++) {
if (OB_ISNULL(stmt->get_column_item(i)) || OB_ISNULL(stmt->get_column_item(i)->expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else {
stmt->get_column_item(i)->expr_->set_table_name(table_item->table_name_);
}
}
}
}
}
return ret;
}
/*@brief, ObTransformPreProcess::create_select_list_from_grouping_sets, according to oracle action:
* if the expr not in group by expr except in aggr, the expr will replaced with NULL; eg:
* select c1, c2, max(c1), max(c2) from t1 group by grouping sets(c1,c2) having c1 > 1 or c2 > 1 or sum(c1) > 2 or
* sum(c2) > 2;
* <==>
* select c1, NULL, max(c1), max(c1) from t1 group by c1 having c1 > 1 or NULL > 1 or sum(c1) > 2 or sum(c2) > 2
* union all
* select NULL, c2, max(c1), max(c1) from t1 group by c2 having NULL > 1 or c2 > 1 or sum(c1) > 2 or sum(c2) > 2;
*
* select nvl(c1,1),c3 from t1 group by grouping sets(nvl(c1,1),c3);
* <==>
* select nvl(c1,1), NULL from t1 group by nvl(c1,1)
* union all
* select NULL, c3 from t1 group by c3;
*
* select nvl(c1,1) + c3 from t1 group by grouping sets(nvl(c1,1),c3);
*/
int ObTransformPreProcess::create_select_list_from_grouping_sets(ObSelectStmt* stmt,
ObIArray<ObGroupbyExpr>& groupby_exprs_list, int64_t cur_index, ObIArray<ObRawExpr*>& old_exprs,
ObIArray<ObRawExpr*>& new_exprs)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt) || OB_ISNULL(ctx_) || OB_ISNULL(ctx_->expr_factory_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt is null.", K(ret), K(stmt), K(ctx_));
} else {
common::ObSEArray<SelectItem, 4> select_items;
ObRelIds rel_ids;
for (int64_t i = 0; OB_SUCC(ret) && i < stmt->get_select_item_size(); i++) {
if (OB_FAIL(extract_select_expr_and_replace_expr(stmt->get_select_item(i).expr_,
stmt->get_group_exprs(),
stmt->get_rollup_exprs(),
stmt->get_aggr_items(),
groupby_exprs_list,
cur_index,
select_items,
old_exprs,
new_exprs,
rel_ids))) {
LOG_WARN("failed to extract select expr and replace expr", K(ret));
} else { /*do nothing*/
}
}
for (int64_t i = 0; OB_SUCC(ret) && i < stmt->get_column_size(); ++i) {
ObRawExpr* expr = stmt->get_column_items().at(i).expr_;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (ObOptimizerUtil::find_item(stmt->get_group_exprs(), expr) ||
ObOptimizerUtil::find_item(stmt->get_rollup_exprs(), expr) ||
expr->get_relation_ids().is_subset2(rel_ids)) {
/*do nothing*/
} else {
ObRawExpr* null_expr = NULL;
ObSysFunRawExpr* cast_expr = NULL;
if (OB_FAIL(ObRawExprUtils::build_null_expr(*ctx_->expr_factory_, null_expr))) {
LOG_WARN("failed build null exprs.", K(ret));
} else if (OB_FAIL(ObRawExprUtils::create_cast_expr(
*ctx_->expr_factory_, null_expr, expr->get_result_type(), cast_expr, ctx_->session_info_))) {
LOG_WARN("create cast expr failed", K(ret));
} else if (OB_FAIL(cast_expr->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else if (OB_FAIL(old_exprs.push_back(expr))) {
LOG_WARN("failed to push back expr", K(ret));
} else if (OB_FAIL(new_exprs.push_back(cast_expr))) {
LOG_WARN("failed to push back expr", K(ret));
} else { /*do nothing*/
}
}
}
if (OB_SUCC(ret)) {
if (select_items.empty()) {
stmt->get_select_items().reset();
if (OB_FAIL(ObTransformUtils::create_dummy_select_item(*stmt, ctx_))) {
LOG_WARN("failed to create dummy select item", K(ret));
} else { /*do nothing*/
}
} else if (OB_FAIL(stmt->get_select_items().assign(select_items))) {
LOG_WARN("failed to assign to select items.", K(ret));
}
}
}
return ret;
}
int ObTransformPreProcess::extract_select_expr_and_replace_expr(ObRawExpr* expr, ObIArray<ObRawExpr*>& groupby_exprs,
ObIArray<ObRawExpr*>& rollup_exprs, ObIArray<ObAggFunRawExpr*>& aggr_items,
ObIArray<ObGroupbyExpr>& groupby_exprs_list, int64_t cur_index, ObIArray<SelectItem>& select_items,
ObIArray<ObRawExpr*>& old_exprs, ObIArray<ObRawExpr*>& new_exprs, ObRelIds& rel_ids)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr) || OB_ISNULL(ctx_) || OB_ISNULL(ctx_->expr_factory_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(ctx_));
} else if (expr->is_aggr_expr() || ObOptimizerUtil::find_item(groupby_exprs, expr) ||
ObOptimizerUtil::find_item(rollup_exprs, expr)) {
if (!is_expr_in_select_item(select_items, expr)) {
SelectItem select_item;
select_item.expr_ = expr;
select_item.expr_name_ = expr->get_expr_name();
select_item.alias_name_ = expr->get_expr_name();
if (OB_FAIL(select_items.push_back(select_item))) {
LOG_WARN("failed to push back into select items.", K(ret));
} else if (expr->get_expr_type() == T_FUN_GROUPING) {
if (OB_UNLIKELY(expr->get_param_count() != 1)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(*expr), K(ret));
} else if (ObOptimizerUtil::find_item(groupby_exprs, expr->get_param_expr(0)) ||
ObOptimizerUtil::find_item(rollup_exprs, expr->get_param_expr(0))) {
/*do nothing*/
} else {
ObConstRawExpr* one_expr = NULL;
ObSysFunRawExpr* cast_expr = NULL;
if (OB_FAIL(ObRawExprUtils::build_const_int_expr(*ctx_->expr_factory_, ObIntType, 1, one_expr))) {
LOG_WARN("failed to build const int expr", K(ret));
} else if (OB_FAIL(ObRawExprUtils::create_cast_expr(
*ctx_->expr_factory_, one_expr, expr->get_result_type(), cast_expr, ctx_->session_info_))) {
LOG_WARN("create cast expr failed", K(ret));
} else if (OB_FAIL(cast_expr->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else if (OB_FAIL(old_exprs.push_back(expr))) {
LOG_WARN("failed to push back expr", K(ret));
} else if (OB_FAIL(rel_ids.add_members2(expr->get_relation_ids()))) {
LOG_WARN("failed to get relation ids", K(ret));
} else if (OB_FAIL(new_exprs.push_back(cast_expr))) {
LOG_WARN("failed to push back expr", K(ret));
} else if (OB_FAIL(ObOptimizerUtil::remove_item(aggr_items, static_cast<ObAggFunRawExpr*>(expr)))) {
LOG_WARN("failed to remove item", K(ret));
} else { /*do nothing*/
}
}
}
}
} else if (is_select_expr_in_other_groupby_exprs(expr, groupby_exprs_list, cur_index)) {
if (!ObOptimizerUtil::find_item(old_exprs, expr)) {
ObRawExpr* null_expr = NULL;
ObSysFunRawExpr* cast_expr = NULL;
SelectItem select_item;
select_item.expr_ = expr;
select_item.expr_name_ = expr->get_expr_name();
select_item.alias_name_ = expr->get_expr_name();
if (OB_FAIL(ObRawExprUtils::build_null_expr(*ctx_->expr_factory_, null_expr))) {
LOG_WARN("failed build null exprs.", K(ret));
} else if (OB_FAIL(ObRawExprUtils::create_cast_expr(
*ctx_->expr_factory_, null_expr, expr->get_result_type(), cast_expr, ctx_->session_info_))) {
LOG_WARN("create cast expr failed", K(ret));
} else if (OB_FAIL(cast_expr->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else if (OB_FAIL(old_exprs.push_back(expr))) {
LOG_WARN("failed to push back expr", K(ret));
} else if (OB_FAIL(rel_ids.add_members2(expr->get_relation_ids()))) {
LOG_WARN("failed to get relation ids", K(ret));
} else if (OB_FAIL(new_exprs.push_back(cast_expr))) {
LOG_WARN("failed to push back expr", K(ret));
} else if (OB_FAIL(select_items.push_back(select_item))) {
LOG_WARN("failed to push back into select items.", K(ret));
} else { /*do nothing*/
}
}
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < expr->get_param_count(); ++i) {
if (OB_FAIL(SMART_CALL(extract_select_expr_and_replace_expr(expr->get_param_expr(i),
groupby_exprs,
rollup_exprs,
aggr_items,
groupby_exprs_list,
cur_index,
select_items,
old_exprs,
new_exprs,
rel_ids)))) {
LOG_WARN("failed to extract select expr and replace expr", K(ret));
}
}
}
return ret;
}
int ObTransformPreProcess::replace_select_and_having_exprs(
ObSelectStmt* select_stmt, ObIArray<ObRawExpr*>& old_exprs, ObIArray<ObRawExpr*>& new_exprs)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(select_stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(select_stmt));
} else {
// replace select expr
for (int64_t i = 0; OB_SUCC(ret) && i < select_stmt->get_select_item_size(); ++i) {
SelectItem& select_item = select_stmt->get_select_item(i);
if (OB_ISNULL(select_item.expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(select_item.expr_));
} else if (OB_FAIL(replace_stmt_special_exprs(select_stmt, select_item.expr_, old_exprs, new_exprs))) {
LOG_WARN("failed to replace exception aggr exprs", K(ret));
}
}
// replace having expr into null
if (OB_SUCC(ret)) {
ObIArray<ObRawExpr*>& having_exprs = select_stmt->get_having_exprs();
for (int64_t i = 0; OB_SUCC(ret) && i < having_exprs.count(); ++i) {
if (OB_ISNULL(having_exprs.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(having_exprs.at(i)));
} else if (OB_FAIL(replace_stmt_special_exprs(select_stmt, having_exprs.at(i), old_exprs, new_exprs, true))) {
LOG_WARN("failed to replace exception aggr exprs", K(ret));
}
}
}
}
return ret;
}
bool ObTransformPreProcess::is_select_expr_in_other_groupby_exprs(
ObRawExpr* expr, ObIArray<ObGroupbyExpr>& groupby_exprs_list, int64_t cur_index)
{
bool is_true = false;
for (int64_t i = 0; !is_true && i < groupby_exprs_list.count(); ++i) {
if (i == cur_index) {
/*do nothing */
} else {
is_true = ObOptimizerUtil::find_equal_expr(groupby_exprs_list.at(i).groupby_exprs_, expr);
}
}
return is_true;
}
bool ObTransformPreProcess::is_expr_in_select_item(ObIArray<SelectItem>& select_items, ObRawExpr* expr)
{
bool is_true = false;
for (int64_t i = 0; !is_true && i < select_items.count(); ++i) {
is_true = (select_items.at(i).expr_ == expr);
}
return is_true;
}
int ObTransformPreProcess::replace_stmt_special_exprs(ObSelectStmt* select_stmt, ObRawExpr*& expr,
ObIArray<ObRawExpr*>& old_exprs, ObIArray<ObRawExpr*>& new_exprs, bool ignore_const /*default false*/)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(expr));
} else if (ignore_const && expr->has_const_or_const_expr_flag()) {
/*do nothing*/
} else if (ObOptimizerUtil::find_item(select_stmt->get_group_exprs(), expr) ||
ObOptimizerUtil::find_item(select_stmt->get_rollup_exprs(), expr)) {
/*do nothing*/
} else {
int64_t idx = -1;
if (!ObOptimizerUtil::find_item(old_exprs, expr, &idx)) {
if (expr->is_aggr_expr()) {
// do nothing
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < expr->get_param_count(); ++i) {
if (OB_FAIL(SMART_CALL(replace_stmt_special_exprs(
select_stmt, expr->get_param_expr(i), old_exprs, new_exprs, ignore_const)))) {
LOG_WARN("failed to replace exception aggr exprs", K(ret));
} else { /*do nothing */
}
}
}
} else if (OB_UNLIKELY(idx < 0 || idx >= new_exprs.count()) || OB_ISNULL(new_exprs.at(idx))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid index", K(ret), K(idx), K(new_exprs.count()), K(new_exprs));
} else {
expr = new_exprs.at(idx);
}
}
return ret;
}
int ObTransformPreProcess::replace_with_set_stmt_view(
ObSelectStmt* origin_stmt, ObSelectStmt* set_view_stmt, ObSelectStmt*& union_stmt)
{
int ret = OB_SUCCESS;
ObRelIds rel_ids;
TableItem* view_table_item = NULL;
ObSEArray<ObRawExpr*, 4> old_exprs;
ObSEArray<ObRawExpr*, 4> new_exprs;
ObSEArray<ColumnItem, 4> temp_column_items;
if (OB_ISNULL(origin_stmt) || OB_ISNULL(set_view_stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("origin stmt is null", K(ret));
} else if (OB_FAIL(ObTransformUtils::get_from_tables(*origin_stmt, rel_ids))) {
LOG_WARN("failed to get from tables.", K(ret));
} else if (FALSE_IT(origin_stmt->get_table_items().reset())) {
} else if (FALSE_IT(origin_stmt->get_from_items().reset())) {
} else if (OB_FAIL(ObTransformUtils::add_new_table_item(ctx_, origin_stmt, set_view_stmt, view_table_item))) {
LOG_WARN("failed to add new table item.", K(ret));
} else if (OB_FAIL(origin_stmt->add_from_item(view_table_item->table_id_))) {
LOG_WARN("failed to add from item", K(ret));
} else if (OB_FAIL(ObTransformUtils::create_columns_for_view(ctx_, *view_table_item, origin_stmt, new_exprs))) {
LOG_WARN("failed to get select exprs from grouping sets view.", K(ret));
} else { /* do nothing. */
}
for (int64_t i = 0; OB_SUCC(ret) && i < origin_stmt->get_column_size(); i++) {
if (!origin_stmt->get_column_item(i)->expr_->get_relation_ids().overlap(rel_ids)) {
if (OB_FAIL(temp_column_items.push_back(*origin_stmt->get_column_item(i)))) {
LOG_WARN("faield to push back into column items.", K(ret));
} else { /* do nothing. */
}
} else { /* do nothing. */
}
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(origin_stmt->get_column_items().assign(temp_column_items))) {
LOG_WARN("failed to assign column items.", K(ret));
} else if (OB_FAIL(extract_stmt_replace_expr(origin_stmt, old_exprs))) {
LOG_WARN("failed to extract stmt replace expr", K(ret));
} else if (OB_UNLIKELY(old_exprs.count() != 0 && old_exprs.count() != new_exprs.count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(ret), K(old_exprs), K(new_exprs));
} else {
origin_stmt->get_grouping_sets_items().reset();
origin_stmt->get_multi_rollup_items().reset();
origin_stmt->get_aggr_items().reset();
origin_stmt->get_group_exprs().reset();
origin_stmt->get_rollup_exprs().reset();
origin_stmt->reassign_grouping();
origin_stmt->reassign_rollup();
origin_stmt->reassign_grouping_sets();
origin_stmt->get_table_items().reset();
if (old_exprs.count() != 0 && OB_FAIL(origin_stmt->replace_inner_stmt_expr(old_exprs, new_exprs))) {
LOG_WARN("failed to replace inner stmt exprs.", K(ret));
} else if (OB_FAIL(origin_stmt->get_table_items().push_back(view_table_item))) {
LOG_WARN("add table item failed", K(ret));
} else if (OB_FAIL(origin_stmt->rebuild_tables_hash())) {
LOG_WARN("failed to rebuild tables hash.", K(ret));
} else if (OB_FAIL(origin_stmt->update_column_item_rel_id())) {
LOG_WARN("failed to update column items rel id.", K(ret));
} else if (OB_FAIL(origin_stmt->formalize_stmt(ctx_->session_info_))) {
LOG_WARN("failed to formalized stmt.", K(ret));
} else {
origin_stmt->set_need_temp_table_trans(true);
union_stmt = origin_stmt;
}
}
return ret;
}
int ObTransformPreProcess::extract_stmt_replace_expr(ObSelectStmt* select_stmt, ObIArray<ObRawExpr*>& old_exprs)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(select_stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(select_stmt));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < select_stmt->get_select_item_size(); i++) {
if (OB_FAIL(
extract_replace_expr_from_select_expr(select_stmt->get_select_item(i).expr_, select_stmt, old_exprs))) {
LOG_WARN("failed to extract replace expr from select expr", K(ret));
}
}
}
return ret;
}
int ObTransformPreProcess::extract_replace_expr_from_select_expr(
ObRawExpr* expr, ObSelectStmt* select_stmt, ObIArray<ObRawExpr*>& old_exprs)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(expr));
} else if (expr->is_aggr_expr() || ObOptimizerUtil::find_item(select_stmt->get_group_exprs(), expr) ||
ObOptimizerUtil::find_item(select_stmt->get_rollup_exprs(), expr) ||
select_stmt->is_expr_in_groupings_sets_item(expr) || select_stmt->is_expr_in_multi_rollup_items(expr)) {
// here use find_equal_expr function, because same exprs in groupings set item have different ptr
if (!ObOptimizerUtil::find_equal_expr(old_exprs, expr)) {
if (OB_FAIL(old_exprs.push_back(expr))) {
LOG_WARN("failed to push back expr", K(ret));
} else { /*do nothing*/
}
}
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < expr->get_param_count(); ++i) {
if (OB_FAIL(SMART_CALL(extract_replace_expr_from_select_expr(expr->get_param_expr(i), select_stmt, old_exprs)))) {
LOG_WARN("failed to extract replace expr from select expr", K(ret));
} else { /*do nothing*/
}
}
}
return ret;
}
int ObTransformPreProcess::create_set_view_stmt(ObSelectStmt* origin_stmt, ObSelectStmt*& set_view_stmt)
{
int ret = OB_SUCCESS;
ObSelectStmt* groupby_stmt = NULL;
ObSelectStmt* part_union_stmt = NULL;
ObSelectStmt* temp_stmt = NULL;
if (OB_ISNULL(origin_stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("origin stmt is null", K(ret));
} else if (OB_ISNULL(ctx_) || OB_ISNULL(ctx_->stmt_factory_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ctx or ctx stmt factory is null", K(ret));
} else {
/* as following to create set stmt:
* 1. get total group by stmt count(grouping sets + multi rollup)
* 2. deep copy origin stmt;
* 3. generate group by exprs and add stmt;
* 4. deal with stmt other attribute: select list, aggr, column and so on;
* 5. create set stmt.
*/
int64_t count =
get_total_count_of_groupby_stmt(origin_stmt->get_grouping_sets_items(), origin_stmt->get_multi_rollup_items());
if (OB_UNLIKELY(count < 1)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(ret), K(count));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < count; i++) {
ObSEArray<ObGroupbyExpr, 4> groupby_exprs_list;
bool is_happened = false;
if (OB_FAIL(ctx_->stmt_factory_->create_stmt<ObSelectStmt>(groupby_stmt))) {
LOG_WARN("failed to create stmt from ctx.", K(ret));
} else if (OB_ISNULL(groupby_stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("groupby stmt is null", K(ret));
} else if (FALSE_IT(groupby_stmt->set_query_ctx(origin_stmt->get_query_ctx()))) {
} else if (OB_FAIL(groupby_stmt->deep_copy(*ctx_->stmt_factory_, *ctx_->expr_factory_, *origin_stmt))) {
LOG_WARN("failed to deep copy from stmt.", K(ret));
} else if (OB_FAIL(get_groupby_exprs_list(groupby_stmt->get_grouping_sets_items(),
groupby_stmt->get_multi_rollup_items(),
groupby_exprs_list))) {
LOG_WARN("failed to get groupby exprs list", K(ret));
} else if (OB_UNLIKELY(i >= groupby_exprs_list.count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(i), K(groupby_exprs_list.count()), K(ret));
} else if (OB_FAIL(
append_array_no_dup(groupby_stmt->get_group_exprs(), groupby_exprs_list.at(i).groupby_exprs_))) {
LOG_WARN("failed to assign the group exprs.", K(ret));
} else {
// stmt only reserve group by, having and aggr info
groupby_stmt->get_order_items().reset();
groupby_stmt->set_limit_offset(NULL, NULL);
groupby_stmt->set_limit_percent_expr(NULL);
groupby_stmt->set_fetch_with_ties(false);
groupby_stmt->set_has_fetch(false);
groupby_stmt->clear_sequence();
groupby_stmt->set_select_into(NULL);
groupby_stmt->get_grouping_sets_items().reset();
groupby_stmt->get_multi_rollup_items().reset();
groupby_stmt->get_window_func_exprs().reset();
if (groupby_stmt->get_rollup_expr_size() == 0) {
groupby_stmt->reassign_rollup();
}
ObSEArray<ObRawExpr*, 4> old_exprs;
ObSEArray<ObRawExpr*, 4> new_exprs;
if (OB_FAIL(ObTransformUtils::replace_stmt_expr_with_groupby_exprs(groupby_stmt))) {
LOG_WARN("failed to replace stmt expr with groupby columns", K(ret));
} else if (OB_FAIL(create_select_list_from_grouping_sets(
groupby_stmt, groupby_exprs_list, i, old_exprs, new_exprs))) {
LOG_WARN("failed to create select list from grouping sets.", K(ret));
// why not use replace_inner_stmt_expr?, see this example:
// select nvl(c1,1), c1, max(c1) from t1 grouping sets(nvl(c1,1), c1);
} else if (OB_FAIL(replace_select_and_having_exprs(groupby_stmt, old_exprs, new_exprs))) {
LOG_WARN("failed to replace select and having expr", K(ret));
// select c1 from t1 group by grouping sets(c1, ());
//<==>
// select c1 from t1 group by c1 union all select NULL from t1 limit 1;
} else if (!groupby_stmt->has_group_by() && OB_FAIL(ObTransformUtils::set_limit_expr(groupby_stmt, ctx_))) {
LOG_WARN("add limit expr failed", K(ret));
} else if (!groupby_stmt->has_group_by() && OB_FAIL(eliminate_having(groupby_stmt, is_happened))) {
LOG_WARN("failed to eliminate having", K(ret));
} else if (OB_FAIL(groupby_stmt->update_stmt_table_id(*origin_stmt))) {
LOG_WARN("failed to update stmt table id.", K(ret));
} else if (OB_FAIL(groupby_stmt->formalize_stmt(ctx_->session_info_))) {
LOG_WARN("failed to formalized stmt.", K(ret));
} else if (i >= 1) {
if (i == groupby_exprs_list.count() - 1) {
groupby_stmt->set_is_last_access(true);
}
if (OB_FAIL(ObTransformUtils::create_union_stmt(ctx_, false, part_union_stmt, groupby_stmt, temp_stmt))) {
LOG_WARN("failed to create union stmt.", K(ret));
} else if (OB_ISNULL(temp_stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null", K(ret), K(temp_stmt));
} else if (FALSE_IT(temp_stmt->set_query_ctx(origin_stmt->get_query_ctx()))) {
} else if (OB_FAIL(temp_stmt->formalize_stmt(ctx_->session_info_))) {
LOG_WARN("failed to formalize stmt.", K(ret));
} else {
part_union_stmt = temp_stmt;
}
} else if (i == 0) {
part_union_stmt = groupby_stmt;
} else { /*do nothing.*/
}
}
}
set_view_stmt = part_union_stmt;
}
}
if (OB_SUCC(ret)) {
ObSEArray<ObRawExpr*, 4> select_exprs;
if (OB_FAIL(set_view_stmt->get_select_exprs(select_exprs))) {
LOG_WARN("failed to get select exprs.", K(ret));
} else if (FALSE_IT(set_view_stmt->get_select_items().reset())) {
} else if (OB_FAIL(ObTransformUtils::create_select_item(*ctx_->allocator_, select_exprs, set_view_stmt))) {
LOG_WARN("failed to create select items.", K(ret));
} else { /*do nothing.*/
}
}
return ret;
}
int64_t ObTransformPreProcess::get_total_count_of_groupby_stmt(
ObIArray<ObGroupingSetsItem>& grouping_sets_items, ObIArray<ObMultiRollupItem>& multi_rollup_items)
{
int64_t cnt_grouping_sets = 1;
int64_t cnt_multi_rollup = 1;
for (int64_t i = 0; i < grouping_sets_items.count(); ++i) {
int64_t tmp_count = 1;
ObIArray<ObMultiRollupItem>& rollup_items = grouping_sets_items.at(i).multi_rollup_items_;
for (int64_t j = 0; j < rollup_items.count(); ++j) {
tmp_count = tmp_count * (rollup_items.at(j).rollup_list_exprs_.count() + 1);
}
cnt_grouping_sets =
cnt_grouping_sets * (grouping_sets_items.at(i).grouping_sets_exprs_.count() + (tmp_count > 1 ? tmp_count : 0));
}
for (int64_t i = 0; i < multi_rollup_items.count(); ++i) {
cnt_multi_rollup = cnt_multi_rollup * (multi_rollup_items.at(i).rollup_list_exprs_.count() + 1);
}
return cnt_grouping_sets * cnt_multi_rollup;
}
int ObTransformPreProcess::get_groupby_exprs_list(ObIArray<ObGroupingSetsItem>& grouping_sets_items,
ObIArray<ObMultiRollupItem>& multi_rollup_items, ObIArray<ObGroupbyExpr>& groupby_exprs_list)
{
int ret = OB_SUCCESS;
ObSEArray<ObGroupbyExpr, 4> grouping_sets_exprs_list;
ObSEArray<ObGroupbyExpr, 4> multi_rollup_exprs_list;
if (OB_FAIL(expand_grouping_sets_items(grouping_sets_items, grouping_sets_exprs_list))) {
LOG_WARN("failed to expand grouping sets items", K(ret));
} else if (OB_FAIL(expand_multi_rollup_items(multi_rollup_items, multi_rollup_exprs_list))) {
LOG_WARN("failed to expand grouping sets items", K(ret));
} else if (OB_FAIL(
combination_two_rollup_list(grouping_sets_exprs_list, multi_rollup_exprs_list, groupby_exprs_list))) {
LOG_WARN("failed to expand grouping sets items", K(ret));
} else {
LOG_TRACE("succeed to get groupby exprs list",
K(grouping_sets_exprs_list),
K(multi_rollup_exprs_list),
K(groupby_exprs_list));
}
return ret;
}
/*@brief,ObTransformPreProcess::expand_grouping_sets_items,Creating a complete group exprs. such as:
* select c1,c2,c3,c4 from t1 group by grouping sets(c1,c2), grouping sets(c3,c4);
* <==>
* select c1,NULL,c3,NULL from t1 group by c1,c3
* union all
* select c1,NULL,NULL,c4 from t1 group by c1,c4
* union all
* select NULL,c2,c3,NULL from t1 group by c2,c3
* union all
* select NULL,c2,NULL,c4 from t1 group by c2,c4;
* as above example, multi grouping sets is actually grouping sets cartesian product.
*/
int ObTransformPreProcess::expand_grouping_sets_items(
ObIArray<ObGroupingSetsItem>& grouping_sets_items, ObIArray<ObGroupbyExpr>& grouping_sets_exprs)
{
int ret = OB_SUCCESS;
int64_t total_cnt = 1;
ObSEArray<ObGroupingSetsItem, 4> tmp_grouping_sets_items;
for (int64_t i = 0; i < grouping_sets_items.count(); ++i) {
total_cnt = total_cnt * (grouping_sets_items.at(i).grouping_sets_exprs_.count() +
grouping_sets_items.at(i).multi_rollup_items_.count());
}
if (OB_UNLIKELY(total_cnt < 1)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(ret), K(total_cnt), K(grouping_sets_items));
} else if (OB_FAIL(tmp_grouping_sets_items.prepare_allocate(total_cnt))) {
LOG_WARN("failed to prepare allocate", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < total_cnt; ++i) {
int64_t bit_count = i;
int64_t index = 0;
for (int64_t j = 0; OB_SUCC(ret) && j < grouping_sets_items.count(); ++j) {
ObGroupingSetsItem& item = grouping_sets_items.at(j);
int64_t item_count = item.grouping_sets_exprs_.count() + item.multi_rollup_items_.count();
index = bit_count % (item_count);
bit_count = bit_count / (item_count);
if (index < item.grouping_sets_exprs_.count()) {
if (OB_FAIL(
tmp_grouping_sets_items.at(i).grouping_sets_exprs_.push_back(item.grouping_sets_exprs_.at(index)))) {
LOG_WARN("failed to push back item", K(ret));
}
} else if (OB_UNLIKELY(index - item.grouping_sets_exprs_.count() >= item.multi_rollup_items_.count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(ret));
} else if (OB_FAIL(tmp_grouping_sets_items.at(i).multi_rollup_items_.push_back(
item.multi_rollup_items_.at(index - item.grouping_sets_exprs_.count())))) {
LOG_WARN("failed to push back item", K(ret));
} else { /*do nothing*/
}
}
}
if (OB_SUCC(ret)) {
for (int64_t i = 0; OB_SUCC(ret) && i < total_cnt; ++i) {
ObGroupbyExpr groupby_item;
for (int64_t j = 0; OB_SUCC(ret) && j < tmp_grouping_sets_items.at(i).grouping_sets_exprs_.count(); ++j) {
if (OB_FAIL(append(groupby_item.groupby_exprs_,
tmp_grouping_sets_items.at(i).grouping_sets_exprs_.at(j).groupby_exprs_))) {
LOG_WARN("failed to append exprs", K(ret));
} else { /*do nothing*/
}
}
if (tmp_grouping_sets_items.at(i).multi_rollup_items_.count() > 0) {
ObSEArray<ObGroupbyExpr, 4> groupby_exprs_list;
if (OB_FAIL(
expand_multi_rollup_items(tmp_grouping_sets_items.at(i).multi_rollup_items_, groupby_exprs_list))) {
LOG_WARN("failed to expand multi rollup items", K(ret));
} else {
for (int64_t k = 0; OB_SUCC(ret) && k < groupby_exprs_list.count(); ++k) {
if (OB_FAIL(append(groupby_exprs_list.at(k).groupby_exprs_, groupby_item.groupby_exprs_))) {
LOG_WARN("failed to append exprs", K(ret));
} else if (OB_FAIL(grouping_sets_exprs.push_back(groupby_exprs_list.at(k)))) {
LOG_WARN("failed to push back groupby exprs", K(ret));
} else { /*do nothing*/
}
}
}
} else if (OB_FAIL(grouping_sets_exprs.push_back(groupby_item))) {
LOG_WARN("failed to push back item", K(ret));
} else { /*do nothing*/
}
}
}
}
return ret;
}
int ObTransformPreProcess::expand_multi_rollup_items(
ObIArray<ObMultiRollupItem>& multi_rollup_items, ObIArray<ObGroupbyExpr>& rollup_list_exprs)
{
int ret = OB_SUCCESS;
ObSEArray<ObGroupbyExpr, 4> result_rollup_list_exprs;
for (int64_t i = 0; OB_SUCC(ret) && i < multi_rollup_items.count(); ++i) {
ObMultiRollupItem& multi_rollup_item = multi_rollup_items.at(i);
ObSEArray<ObGroupbyExpr, 4> tmp_rollup_list_exprs;
ObGroupbyExpr empty_item;
if (OB_FAIL(tmp_rollup_list_exprs.push_back(empty_item))) {
LOG_WARN("failed to push back item", K(ret));
} else {
for (int64_t j = 0; OB_SUCC(ret) && j < multi_rollup_item.rollup_list_exprs_.count(); ++j) {
ObGroupbyExpr item;
if (OB_FAIL(append(item.groupby_exprs_, tmp_rollup_list_exprs.at(j).groupby_exprs_))) {
LOG_WARN("failed to append exprs", K(ret));
} else if (OB_FAIL(append(item.groupby_exprs_, multi_rollup_item.rollup_list_exprs_.at(j).groupby_exprs_))) {
LOG_WARN("failed to append exprs", K(ret));
} else if (OB_FAIL(tmp_rollup_list_exprs.push_back(item))) {
LOG_WARN("failed to push back item", K(ret));
} else { /*do nothing*/
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(combination_two_rollup_list(result_rollup_list_exprs, tmp_rollup_list_exprs, rollup_list_exprs))) {
LOG_WARN("failed to combination two rollup list", K(ret));
} else if (OB_FAIL(result_rollup_list_exprs.assign(rollup_list_exprs))) {
LOG_WARN("failed to assign exprs", K(ret));
} else { /*do nothing*/
}
}
}
}
return ret;
}
int ObTransformPreProcess::combination_two_rollup_list(ObIArray<ObGroupbyExpr>& rollup_list_exprs1,
ObIArray<ObGroupbyExpr>& rollup_list_exprs2, ObIArray<ObGroupbyExpr>& rollup_list_exprs)
{
int ret = OB_SUCCESS;
rollup_list_exprs.reset();
if (rollup_list_exprs1.count() == 0 && rollup_list_exprs2.count() > 0) {
if (OB_FAIL(append(rollup_list_exprs, rollup_list_exprs2))) {
LOG_WARN("failed to append exprs", K(ret));