forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_log_group_by.cpp
More file actions
1308 lines (1254 loc) · 53.9 KB
/
ob_log_group_by.cpp
File metadata and controls
1308 lines (1254 loc) · 53.9 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_OPT
#include "ob_log_group_by.h"
#include "lib/allocator/page_arena.h"
#include "sql/resolver/expr/ob_raw_expr_replacer.h"
#include "ob_log_operator_factory.h"
#include "ob_log_exchange.h"
#include "ob_log_sort.h"
#include "ob_log_topk.h"
#include "ob_log_material.h"
#include "ob_log_table_scan.h"
#include "ob_optimizer_context.h"
#include "ob_optimizer_util.h"
#include "ob_opt_est_cost.h"
#include "ob_raw_expr_pull_up_aggr_expr.h"
#include "ob_raw_expr_push_down_aggr_expr.h"
#include "ob_select_log_plan.h"
#include "common/ob_smart_call.h"
using namespace oceanbase;
using namespace sql;
using namespace oceanbase::common;
int ObLogGroupBy::copy_without_child(ObLogicalOperator*& out)
{
int ret = OB_SUCCESS;
out = NULL;
ObLogicalOperator* op = NULL;
ObLogGroupBy* group_by = NULL;
if (OB_FAIL(clone(op))) {
LOG_WARN("failed to clone ObLogGroupBy", K(ret));
} else if (OB_ISNULL(group_by = static_cast<ObLogGroupBy*>(op))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("failed to cast ObLogicalOperator * to ObLogGroupBy *", K(ret));
} else {
group_by->set_algo_type(algo_);
group_by->aggr_exprs_ = aggr_exprs_;
group_by->group_exprs_ = group_exprs_;
group_by->rollup_exprs_ = rollup_exprs_;
group_by->distinct_card_ = distinct_card_;
group_by->from_pivot_ = from_pivot_;
out = static_cast<ObLogicalOperator*>(group_by);
}
return ret;
}
int32_t ObLogGroupBy::get_explain_name_length() const
{
int32_t length = 0;
if (SCALAR_AGGREGATE == algo_) {
length += (int32_t)strlen("SCALAR ");
} else if (HASH_AGGREGATE == algo_) {
length += (int32_t)strlen("HASH ");
} else {
length += (int32_t)strlen("MERGE ");
}
length += (int32_t)strlen(get_name());
if (from_pivot_) {
length += (int32_t)strlen(" PIVOT");
}
return length;
}
int ObLogGroupBy::get_explain_name_internal(char* buf, const int64_t buf_len, int64_t& pos)
{
int ret = OB_SUCCESS;
if (SCALAR_AGGREGATE == algo_) {
ret = BUF_PRINTF("SCALAR ");
} else if (HASH_AGGREGATE == algo_) {
ret = BUF_PRINTF("HASH ");
} else {
ret = BUF_PRINTF("MERGE ");
}
if (OB_SUCC(ret)) {
ret = BUF_PRINTF("%s", get_name());
}
if (OB_SUCC(ret) && from_pivot_) {
ret = BUF_PRINTF(" PIVOT");
}
if (OB_FAIL(ret)) {
LOG_WARN("BUF_PRINTF fails", K(ret));
}
return ret;
}
int ObLogGroupBy::set_group_by_exprs(const common::ObIArray<ObRawExpr*>& group_by_exprs)
{
return group_exprs_.assign(group_by_exprs);
}
int ObLogGroupBy::set_rollup_exprs(const common::ObIArray<ObRawExpr*>& rollup_exprs)
{
return rollup_exprs_.assign(rollup_exprs);
}
int ObLogGroupBy::set_aggr_exprs(const common::ObIArray<ObAggFunRawExpr*>& aggr_exprs)
{
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < aggr_exprs.count(); i++) {
ObAggFunRawExpr* aggr_expr = aggr_exprs.at(i);
if (OB_ISNULL(aggr_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("aggr_expr is null", K(ret));
} else {
ret = aggr_exprs_.push_back(aggr_expr);
}
}
return ret;
}
int ObLogGroupBy::get_child_groupby_algorithm(const bool is_distinct, const bool can_push_down_distinct,
const bool need_sort, const bool child_need_sort, AggregateAlgo& aggr_algo)
{
int ret = OB_SUCCESS;
ObSelectStmt* stmt = NULL;
if (OB_ISNULL(get_plan()) || OB_ISNULL(stmt = get_plan()->get_stmt())) {
LOG_WARN("plan is null or stmt is null", K(ret), K(get_plan()));
} else if (is_distinct) {
const int64_t group_by_type = stmt->get_stmt_hint().aggregate_;
if (SCALAR_AGGREGATE == get_algo()) {
if (can_push_down_distinct) {
aggr_algo = MERGE_AGGREGATE;
} else {
aggr_algo = ((group_by_type & OB_NO_USE_HASH_AGGREGATE) ||
(!child_need_sort && !(group_by_type & OB_USE_HASH_AGGREGATE)))
? MERGE_AGGREGATE
: HASH_AGGREGATE;
}
} else if (!can_push_down_distinct && MERGE_AGGREGATE == get_algo()) {
aggr_algo =
((group_by_type & OB_NO_USE_HASH_AGGREGATE) || (!need_sort && !(group_by_type & OB_USE_HASH_AGGREGATE)))
? MERGE_AGGREGATE
: HASH_AGGREGATE;
} else {
aggr_algo = get_algo();
}
} else {
if (SCALAR_AGGREGATE == get_algo()) {
aggr_algo = MERGE_AGGREGATE;
} else {
aggr_algo = get_algo();
}
}
return ret;
}
int ObLogGroupBy::get_child_groupby_expected_ordering(
const ObIArray<ObRawExpr*>& distinct_exprs, ObIArray<OrderItem>& expected_ordering)
{
int ret = OB_SUCCESS;
ObLogicalOperator* child = NULL;
ObSEArray<ObRawExpr*, 8> temp_exprs;
if (OB_ISNULL(get_stmt()) || OB_ISNULL(child = get_child(first_child))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(get_stmt()), K(child), K(ret));
} else if (OB_FAIL(expected_ordering.assign(expected_ordering_))) {
LOG_WARN("failed to assign expected ordering", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < expected_ordering.count(); i++) {
if (OB_ISNULL(expected_ordering.at(i).expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(temp_exprs.push_back(expected_ordering.at(i).expr_))) {
LOG_WARN("failed to push back expr", K(ret));
} else { /*do nothing*/
}
}
for (int64_t i = 0; OB_SUCC(ret) && i < distinct_exprs.count(); i++) {
bool is_const = false;
if (OB_FAIL(ObOptimizerUtil::is_const_expr(distinct_exprs.at(i),
child->get_ordering_output_equal_sets(),
child->get_output_const_exprs(),
is_const))) {
LOG_WARN("failed to check const expr", K(ret));
} else if (is_const) {
/*do nothing*/
} else if (ObOptimizerUtil::find_equal_expr(
temp_exprs, distinct_exprs.at(i), child->get_ordering_output_equal_sets())) {
/*do nothing*/
} else if (OB_FAIL(expected_ordering.push_back(OrderItem(distinct_exprs.at(i), default_asc_direction())))) {
LOG_WARN("failed to push back expr", K(ret));
} else { /*do nothing*/
}
}
}
return ret;
}
int ObLogGroupBy::allocate_groupby_below(const ObIArray<ObRawExpr*>& distinct_exprs, const bool should_push_distinct,
ObLogicalOperator*& exchange_point, ObIArray<std::pair<ObRawExpr*, ObRawExpr*> >& group_push_down_replaced_exprs)
{
int ret = OB_SUCCESS;
bool need_sort = false;
bool child_need_sort = false;
ObLogicalOperator* child = NULL;
ObLogGroupBy* child_group_by = NULL;
ObSEArray<ObRawExpr*, 8> child_group_exprs;
AggregateAlgo child_group_algo = AggregateAlgo::AGGREGATE_UNINITIALIZED;
ObSEArray<OrderItem, 8> child_expected_ordering;
exchange_point = NULL;
if ((MERGE_AGGREGATE == get_algo() ||
(!distinct_exprs.empty() && SCALAR_AGGREGATE == get_algo() && !should_push_distinct)) &&
OB_FAIL(get_child_groupby_expected_ordering(distinct_exprs, child_expected_ordering))) {
LOG_WARN("failed to get child groupby expected ordering", K(ret));
} else if (OB_ISNULL(get_child(first_child))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (MERGE_AGGREGATE == get_algo() && log_op_def::LOG_SORT == get_child(first_child)->get_type()) {
need_sort = true;
child_need_sort = true;
exchange_point = get_child(first_child);
} else {
exchange_point = this;
}
if (OB_FAIL(ret)) {
/*do nothing*/
} else if (OB_FAIL(get_group_rollup_exprs(child_group_exprs))) {
LOG_WARN("failed to get groupby/rollup exprs", K(ret));
} else if (!should_push_distinct && OB_FAIL(append_array_no_dup(child_group_exprs, distinct_exprs))) {
LOG_WARN("failed to append distinct exprs", K(ret));
} else if (!need_sort && !child_expected_ordering.empty() && !distinct_exprs.empty() &&
OB_FAIL(check_need_sort_for_grouping_op(first_child, child_expected_ordering, child_need_sort))) {
LOG_WARN("failed to check need sort for grouping op", K(ret));
} else if (OB_FAIL(get_child_groupby_algorithm(
!distinct_exprs.empty(), should_push_distinct, need_sort, child_need_sort, child_group_algo))) {
LOG_WARN("failed to get child group by algo", K(ret));
} else if (child_need_sort && HASH_AGGREGATE != child_group_algo &&
OB_FAIL(exchange_point->allocate_sort_below(first_child, child_expected_ordering))) {
LOG_WARN("failed to allocate sort below", K(ret));
} else if (OB_FAIL(exchange_point->allocate_group_by_below(
first_child, child_group_exprs, child_group_algo, from_pivot_))) {
LOG_WARN("failed to allocate group by below", K(ret));
} else if (OB_ISNULL(child = exchange_point->get_child(first_child))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_UNLIKELY(log_op_def::LOG_GROUP_BY != child->get_type())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected operator type", K(child->get_type()), K(ret));
} else if (FALSE_IT(child_group_by = static_cast<ObLogGroupBy*>(child))) {
/*do nothing*/
} else if (child_need_sort && HASH_AGGREGATE != child_group_algo &&
OB_FAIL(child_group_by->set_expected_ordering(child_expected_ordering))) {
LOG_WARN("failed to set expected ordering", K(ret));
} else {
if (SCALAR_AGGREGATE == get_algo() || get_algo() == child_group_algo) {
child_group_by->set_card(get_card());
child_group_by->set_op_cost(get_op_cost());
child_group_by->set_width(get_width());
} else if (need_sort && OB_FAIL(exchange_point->est_cost())) {
LOG_WARN("failed to estimate cost", K(ret));
} else if (OB_FAIL(est_cost())) {
LOG_WARN("failed to estimate cost", K(ret));
}
if (OB_FAIL(ret)) {
/*do nothing*/
} else if (should_push_distinct ||
(MERGE_AGGREGATE == child_group_algo && !child_need_sort && !child_group_exprs.empty())) {
// pullup gi operator above child-group-by in the following suitaitons
// 1. merge group by without sort operator,
// allocate gi above group-by can make use of the table scan's ordering
// 2. distint expr is pushed down, such as count(distinct c1)
// a partition should not be processed by two px tasks, other the distinct result
// will be mis-estimated
bool find_exchange = false;
if (OB_ISNULL(child = child_group_by->get_child(first_child))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(child), K(ret));
} else if (OB_FAIL(child_group_by->get_sharding_info().copy_with_part_keys(child->get_sharding_info()))) {
LOG_WARN("failed to copy sharding info", K(ret));
} else if (OB_FAIL(child_has_exchange(child, find_exchange))) {
LOG_WARN("fail to find tsc recursive");
} else if (!find_exchange) {
child_group_by->set_is_partition_wise(true);
child_group_by->set_is_block_gi_allowed(!should_push_distinct);
} else { /*do nothing*/
}
}
}
if (OB_SUCC(ret) && (distinct_exprs.empty() || should_push_distinct)) {
ObArray<std::pair<ObRawExpr*, ObRawExpr*> > push_down_agg_expr;
if (OB_FAIL(produce_pushed_down_expressions(child_group_by, group_push_down_replaced_exprs, push_down_agg_expr))) {
LOG_WARN("failed to produced push-down exprs", K(ret), K(child_group_by));
} else if (distinct_exprs.empty() &&
OB_FAIL(allocate_topk_if_needed(exchange_point, child_group_by, push_down_agg_expr))) {
LOG_WARN("failed to alloc_topk_if needed", K(ret));
} else { /*do nothing*/
}
}
return ret;
}
int ObLogGroupBy::get_group_rollup_exprs(common::ObIArray<ObRawExpr*>& group_rollup_exprs) const
{
int ret = OB_SUCCESS;
if (OB_FAIL(append(group_rollup_exprs, group_exprs_))) {
LOG_WARN("failed to append group exprs into group rollup exprs.", K(ret));
} else if (OB_FAIL(append(group_rollup_exprs, rollup_exprs_))) {
LOG_WARN("failed to append rollup exprs into group rollup exprs.", K(ret));
} else { /*do nothing*/
}
return ret;
}
int ObLogGroupBy::allocate_exchange_post(AllocExchContext* ctx)
{
int ret = OB_SUCCESS;
bool is_basic = false;
bool group_compatible = false;
bool group_rollup_compatible = false;
ObLogicalOperator* child = NULL;
ObLogicalOperator* exchange_point = NULL;
bool should_push_groupby = false;
bool should_push_distinct = false;
ObSEArray<ObRawExpr*, 8> group_key_exprs;
ObSEArray<ObRawExpr*, 8> rollup_key_exprs;
ObSEArray<ObRawExpr*, 16> distinct_exprs;
ObSEArray<ObRawExpr*, 8> group_rollup_exprs;
ObSEArray<std::pair<ObRawExpr*, ObRawExpr*>, 4> group_push_down_replaced_exprs;
if (OB_ISNULL(ctx) || OB_ISNULL(get_plan()) || OB_ISNULL(child = get_child(first_child))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ctx), K(get_plan()), K(child), K(ret));
} else if (OB_FAIL(compute_basic_sharding_info(ctx, is_basic))) {
LOG_WARN("failed to compute basic sharding info", K(ret));
} else if (is_basic) {
LOG_TRACE("is basic sharding info", K(sharding_info_));
} else if (OB_FAIL(prune_weak_part_exprs(*ctx, get_group_by_exprs(), group_key_exprs))) {
LOG_WARN("failed to prune weak part exprs", K(ret));
} else if (OB_FAIL(check_sharding_compatible_with_reduce_expr(child->get_sharding_info(),
group_key_exprs,
child->get_sharding_output_equal_sets(),
group_compatible))) {
LOG_WARN("failed to check sharding compatible with groupby expr", K(ret));
} else if (group_compatible) {
if (OB_FAIL(sharding_info_.copy_with_part_keys(child->get_sharding_info()))) {
LOG_WARN("failed to copy sharding info from child", K(ret));
} else {
is_partition_wise_ = (NULL != sharding_info_.get_phy_table_location_info());
}
} else if (OB_FAIL(should_push_down_group_by(*ctx, distinct_exprs, should_push_groupby, should_push_distinct))) {
LOG_WARN("failed to check should push down group by", K(ret));
} else if (!should_push_groupby) {
// if we can not push down groupby operator, we directly allocate exchange operator
if (MERGE_AGGREGATE == get_algo() && log_op_def::LOG_SORT == child->get_type()) {
exchange_point = child;
} else {
exchange_point = this;
}
if (OB_FAIL(exchange_point->allocate_grouping_style_exchange_below(ctx, get_group_by_exprs()))) {
LOG_WARN("failed to allocate exchange below", K(ret));
} else if (OB_FAIL(sharding_info_.copy_with_part_keys(exchange_point->get_sharding_info()))) {
LOG_WARN("failed to copy with partition keys", K(ret));
} else { /*do nothing*/
}
} else if (OB_FAIL(get_group_rollup_exprs(group_rollup_exprs))) {
LOG_WARN("failed to get groupby rollup exprs.", K(ret));
} else if (OB_FAIL(prune_weak_part_exprs(*ctx, group_rollup_exprs, rollup_key_exprs))) {
LOG_WARN("failed to prune weak part exprs", K(ret));
} else if (OB_FAIL(check_sharding_compatible_with_reduce_expr(child->get_sharding_info(),
rollup_key_exprs,
child->get_sharding_output_equal_sets(),
group_rollup_compatible))) {
LOG_WARN("failed to check sharding compatible with groupby rollup expr", K(ret));
} else {
if (!group_rollup_compatible) {
if (OB_FAIL(allocate_groupby_below(
distinct_exprs, should_push_distinct, exchange_point, group_push_down_replaced_exprs))) {
LOG_WARN("failed to allocate group by below op.", K(ret));
} else if (OB_ISNULL(exchange_point)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(exchange_point->allocate_grouping_style_exchange_below(ctx, group_rollup_exprs))) {
LOG_WARN("failed to allocate exchange below op.", K(ret));
} else if (OB_FAIL(sharding_info_.copy_with_part_keys(exchange_point->get_sharding_info()))) {
LOG_WARN("failed to copy with partition keys", K(ret));
} else if (OB_FAIL(append(ctx->group_push_down_replaced_exprs_, group_push_down_replaced_exprs))) {
LOG_WARN("failed to append group push down replace exprs", K(ret));
} else { /*do nothing*/
}
}
if (OB_SUCC(ret) && has_rollup()) {
if (sharding_info_.is_local() || sharding_info_.is_remote() || sharding_info_.is_match_all()) {
/*do nothing*/
} else if (OB_FAIL(allocate_groupby_below(
distinct_exprs, should_push_distinct, exchange_point, group_push_down_replaced_exprs))) {
LOG_WARN("failed to allocate group by below op.", K(ret));
} else if (OB_ISNULL(exchange_point)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(exchange_point->allocate_grouping_style_exchange_below(ctx, group_exprs_))) {
LOG_WARN("failed to allocate exchange below op.", K(ret));
} else if (OB_FAIL(sharding_info_.copy_with_part_keys(exchange_point->get_sharding_info()))) {
LOG_WARN("failed to copy sharding info", K(ret));
} else if (OB_FAIL(append(ctx->group_push_down_replaced_exprs_, group_push_down_replaced_exprs))) {
LOG_WARN("failed to append group push down replace exprs", K(ret));
} else { /*do nothing*/
}
}
}
return ret;
}
int ObLogGroupBy::should_push_down_group_by(
AllocExchContext& ctx, ObIArray<ObRawExpr*>& distinct_exprs, bool& should_push_groupby, bool& should_push_distinct)
{
int ret = OB_SUCCESS;
ObLogicalOperator* child_op = NULL;
ObSQLSessionInfo* session_info = NULL;
should_push_groupby = false;
should_push_distinct = false;
int64_t distinct_aggr_count = 0;
if (OB_ISNULL(get_plan()) || OB_ISNULL(child_op = get_child(first_child)) ||
OB_ISNULL(session_info = get_plan()->get_optimizer_context().get_session_info())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(child_op), K(get_plan()), K(session_info), K(ret));
} else if (OB_FAIL(session_info->if_aggr_pushdown_allowed(should_push_groupby))) {
LOG_WARN("fail to get aggr_pushdown_allowed", K(ret));
} else if (!should_push_groupby) {
/*do nothing*/
} else {
should_push_groupby = true;
// check whether contain agg expr can not be pushed down
for (int64_t i = 0; OB_SUCC(ret) && should_push_groupby && i < aggr_exprs_.count(); ++i) {
ObAggFunRawExpr* aggr_expr = NULL;
if (OB_ISNULL(aggr_exprs_.at(i)) || OB_UNLIKELY(!aggr_exprs_.at(i)->is_aggr_expr())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(ret), K(aggr_exprs_.at(i)));
} else if (FALSE_IT(aggr_expr = static_cast<ObAggFunRawExpr*>(aggr_exprs_.at(i)))) {
/*do nothing*/
} else if (T_FUN_GROUP_CONCAT == aggr_expr->get_expr_type() || T_FUN_GROUP_RANK == aggr_expr->get_expr_type() ||
T_FUN_GROUP_DENSE_RANK == aggr_expr->get_expr_type() ||
T_FUN_GROUP_PERCENT_RANK == aggr_expr->get_expr_type() ||
T_FUN_GROUP_CUME_DIST == aggr_expr->get_expr_type() || T_FUN_MEDIAN == aggr_expr->get_expr_type() ||
T_FUN_GROUP_PERCENTILE_CONT == aggr_expr->get_expr_type() ||
T_FUN_GROUP_PERCENTILE_DISC == aggr_expr->get_expr_type() ||
T_FUN_AGG_UDF == aggr_expr->get_expr_type() || T_FUN_KEEP_MAX == aggr_expr->get_expr_type() ||
T_FUN_KEEP_MIN == aggr_expr->get_expr_type() || T_FUN_KEEP_SUM == aggr_expr->get_expr_type() ||
T_FUN_KEEP_COUNT == aggr_expr->get_expr_type() || T_FUN_KEEP_WM_CONCAT == aggr_expr->get_expr_type() ||
T_FUN_WM_CONCAT == aggr_expr->get_expr_type()) {
should_push_groupby = false;
} else if (!aggr_expr->is_param_distinct() && !distinct_exprs.empty()) {
should_push_groupby = false;
} else if (!aggr_expr->is_param_distinct()) {
/*do nothing*/
} else if (distinct_exprs.empty()) {
if (OB_FAIL(append(distinct_exprs, aggr_expr->get_real_param_exprs()))) {
LOG_WARN("failed to append expr", K(ret));
} else {
++distinct_aggr_count;
}
} else {
// check expr are the same
should_push_groupby =
ObOptimizerUtil::subset_exprs(
distinct_exprs, aggr_expr->get_real_param_exprs(), child_op->get_ordering_output_equal_sets()) &&
ObOptimizerUtil::subset_exprs(
aggr_expr->get_real_param_exprs(), distinct_exprs, child_op->get_ordering_output_equal_sets());
++distinct_aggr_count;
}
}
if (OB_SUCC(ret) && distinct_aggr_count > 0 && distinct_aggr_count < aggr_exprs_.count()) {
should_push_groupby = false;
}
if (OB_SUCC(ret) && should_push_groupby) {
if (SCALAR_AGGREGATE == algo_ || distinct_card_ <= OB_DOUBLE_EPSINON) {
should_push_groupby = true;
} else if (OB_FAIL(check_fulfill_cut_ratio_condition(ctx.parallel_, distinct_card_, should_push_groupby))) {
LOG_WARN("failed to check fulfill cut ratio condition", K(ret));
}
}
if (OB_SUCC(ret) && should_push_groupby && !distinct_exprs.empty() && get_group_by_exprs().empty()) {
if (OB_FAIL(check_sharding_compatible_with_reduce_expr(child_op->get_sharding_info(),
distinct_exprs,
child_op->get_sharding_output_equal_sets(),
should_push_distinct))) {
LOG_WARN("failed to check sharding compatible with reduce expr", K(ret));
} else { /*do nothing*/
}
}
}
return ret;
}
int ObLogGroupBy::allocate_expr_pre(ObAllocExprContext& ctx)
{
int ret = OB_SUCCESS;
uint64_t producer_id = OB_INVALID_ID;
if (OB_FAIL(get_next_producer_id(get_child(first_child), producer_id))) {
LOG_WARN("failed to get non exchange child id", K(ret));
} else if (OB_FAIL(add_exprs_to_ctx(ctx, group_exprs_, producer_id))) {
LOG_WARN("failed to add exprs to ctx", K(ret));
} else if (OB_FAIL(add_exprs_to_ctx(ctx, rollup_exprs_, producer_id))) {
LOG_WARN("failed to add exprs to ctx", K(ret));
} else if (OB_FAIL(add_exprs_to_ctx(ctx, aggr_exprs_))) {
LOG_WARN("failed to add exprs to ctx", K(ret));
} else if (OB_FAIL(add_exprs_to_ctx(ctx, avg_div_exprs_))) {
LOG_WARN("failed to add exprs to ctx", K(ret));
} else if (OB_FAIL(add_exprs_to_ctx(ctx, approx_count_distinct_estimate_ndv_exprs_))) {
LOG_WARN("failed to add exprs to ctx", K(ret));
} else {
ObSEArray<ObRawExpr*, 16> temp_exprs;
for (int64_t i = 0; OB_SUCC(ret) && i < aggr_exprs_.count(); i++) {
ObRawExpr* raw_expr = NULL;
if (OB_ISNULL(raw_expr = aggr_exprs_.at(i)) || OB_ISNULL(get_plan())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(raw_expr), K(get_plan()));
} else {
for (int64_t j = 0; OB_SUCC(ret) && j < raw_expr->get_param_count(); j++) {
bool should_add = false;
ObRawExpr* param_expr = raw_expr->get_param_expr(j);
if (OB_ISNULL(param_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(param_expr));
} else if (OB_FAIL(check_param_expr_should_be_added(param_expr, should_add))) {
LOG_WARN("failed to check whether param expr should be added", K(ret));
} else if (should_add && OB_FAIL(temp_exprs.push_back(param_expr))) {
LOG_WARN("failed to push back expr", K(ret));
} else { /*do nothing*/
}
}
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(add_exprs_to_ctx(ctx, temp_exprs, producer_id))) {
LOG_WARN("failed to add exprs to ctx", K(ret));
} else if (OB_FAIL(ObLogicalOperator::allocate_expr_pre(ctx))) {
LOG_WARN("failed to allocate expr pre", K(ret));
} else { /*do nothing*/
}
}
// set producer id for aggr expr
for (int64_t i = 0; OB_SUCC(ret) && i < ctx.expr_producers_.count(); ++i) {
ExprProducer& producer = ctx.expr_producers_.at(i);
if (OB_ISNULL(producer.expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_INVALID_ID == producer.producer_id_ && producer.expr_->has_flag(IS_AGG) &&
is_my_aggr_expr(producer.expr_)) {
producer.producer_id_ = id_;
} else { /*do nothing*/
}
}
}
return ret;
}
bool ObLogGroupBy::is_my_aggr_expr(const ObRawExpr* expr)
{
return ObOptimizerUtil::find_item(aggr_exprs_, expr);
}
uint64_t ObLogGroupBy::hash(uint64_t seed) const
{
uint64_t hash_value = seed;
hash_value = ObOptimizerUtil::hash_exprs(hash_value, group_exprs_);
hash_value = ObOptimizerUtil::hash_exprs(hash_value, rollup_exprs_);
hash_value = ObOptimizerUtil::hash_exprs(hash_value, aggr_exprs_);
hash_value = ObOptimizerUtil::hash_exprs(hash_value, avg_div_exprs_);
hash_value = ObOptimizerUtil::hash_exprs(hash_value, approx_count_distinct_estimate_ndv_exprs_);
hash_value = do_hash(algo_, hash_value);
hash_value = ObLogicalOperator::hash(hash_value);
return hash_value;
}
// This is invoked for push-down group-by
int ObLogGroupBy::push_down_aggr_exprs_analyze(
ObIArray<ObRawExpr*>& aggr_exprs, ObIArray<std::pair<ObRawExpr*, ObRawExpr*> >& push_down_avg_arr)
{
int ret = OB_SUCCESS;
ObOptimizerContext* opt_ctx = NULL;
if (OB_ISNULL(get_plan()) || OB_ISNULL(opt_ctx = &get_plan()->get_optimizer_context())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Get unexpected null", K(ret), KP(get_plan()), KP(opt_ctx));
} else {
int64_t N = aggr_exprs.count();
for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {
ObRawExprPushDownAggrExpr push_down_visitor(opt_ctx->get_session_info(), opt_ctx->get_expr_factory());
if (OB_ISNULL(aggr_exprs.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("aggr_exprs.at(i) returns null", K(ret));
} else if (OB_FAIL(push_down_visitor.analyze(*aggr_exprs.at(i)))) {
LOG_WARN("analyze aggr push down failed", K(ret));
} else if (0 == push_down_visitor.get_exprs_count()) {
LOG_TRACE("expression can be pushed down directly to sub-group-by", K(aggr_exprs.at(i)), K(i));
ret = aggr_exprs_.push_back(aggr_exprs.at(i));
} else {
// During the push-down analysis, we check if we need to generate new expressions
// ie - in the case of avg, we have to generate sum() and count() to push down
// instead. If we have not, we may use the expressions directly; otherwise, we
// have to remove the expression from the aggregation and output expressions and
// use the new one instead.
LOG_TRACE("expression can not be pushed down directly to sub-group-by", K(aggr_exprs.at(i)), K(i));
const ObIArray<ObRawExpr*>& new_expr = push_down_visitor.get_new_exprs();
if (OB_FAIL(append(aggr_exprs_, new_expr))) {
LOG_WARN("failed to append exprs", K(ret));
} else if (T_FUN_AVG == aggr_exprs.at(i)->get_expr_type()) {
ObOpRawExpr* push_down_avg_expr = push_down_visitor.get_push_down_avg_expr();
if (OB_ISNULL(push_down_avg_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("push down avg expr is NULL", K(i), K(ret));
} else if (OB_FAIL(push_down_avg_arr.push_back(std::make_pair(aggr_exprs.at(i), push_down_avg_expr)))) {
LOG_WARN("failed to push back expr", K(i), K(ret));
} else { /*do nothing*/
}
} else if (T_FUN_APPROX_COUNT_DISTINCT == aggr_exprs.at(i)->get_expr_type() ||
T_FUN_APPROX_COUNT_DISTINCT_SYNOPSIS == aggr_exprs.at(i)->get_expr_type()) {
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("INVALID expr type", K(*aggr_exprs.at(i)), K(ret));
}
}
}
}
return ret;
}
int ObLogGroupBy::pull_up_aggr_exprs_analyze(
ObIArray<ObRawExpr*>& aggr_exprs, common::ObIArray<std::pair<ObRawExpr*, ObRawExpr*> >& ctx_record_arr)
{
int ret = OB_SUCCESS;
ObOptimizerContext* opt_ctx = NULL;
if (OB_ISNULL(get_plan()) || OB_ISNULL(opt_ctx = &get_plan()->get_optimizer_context())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Get unexpected null", K(get_plan()), K(get_stmt()), K(opt_ctx));
} else {
int64_t N = aggr_exprs.count();
ObSEArray<ObRawExpr*, 16> tmp_exprs;
for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {
ObRawExprPullUpAggrExpr pull_up_visitor(opt_ctx->get_expr_factory(), opt_ctx->get_session_info());
if (OB_ISNULL(aggr_exprs.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("aggr_exprs.at(i) returns null", K(ret), K(i));
} else if (OB_FAIL(pull_up_visitor.analyze(*aggr_exprs.at(i)))) {
LOG_WARN("failed to do pull up analyze", K(aggr_exprs.at(i)), K(i));
} else {
/*
* In order to generate the correct output, we need to replace the replaced
* expr in the select list with the new expression, which will be used later
* during output exprs generation.
*/
if (pull_up_visitor.get_sum_sum_expr() && pull_up_visitor.get_sum_count_expr()) {
// This is the case for avg. We put the newly generated sum(sum)
// and sum(count) aggregation functions into aggr_exprs_ and remember
// the division function in avg_div_exprs_, which will be used in expr
// generation but is invisible in the explain and code generation
if (OB_FAIL(tmp_exprs.push_back(pull_up_visitor.get_sum_sum_expr()))) {
LOG_WARN("Push back failed for tmp_exprs", K(ret));
} else if (OB_FAIL(tmp_exprs.push_back(pull_up_visitor.get_sum_count_expr()))) {
LOG_WARN("Push back failed for tmp_exprs", K(ret));
} else {
ret = avg_div_exprs_.push_back(pull_up_visitor.get_new_expr());
}
} else if (pull_up_visitor.get_merge_synopsis_expr()) {
if (OB_FAIL(tmp_exprs.push_back(pull_up_visitor.get_merge_synopsis_expr()))) {
LOG_WARN("Push back failed for tmp_exprs", K(ret));
} else if (OB_FAIL(approx_count_distinct_estimate_ndv_exprs_.push_back(pull_up_visitor.get_new_expr()))) {
LOG_WARN("Push back failed for approx_count_distinct_esitmate_ndv_exprs_", K(ret));
} else { /* Do nothing */
}
} else {
if (OB_FAIL(tmp_exprs.push_back(pull_up_visitor.get_new_expr()))) {
LOG_WARN("Push back failed for tmp_exprs", K(ret));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(ctx_record_arr.push_back(std::make_pair(aggr_exprs.at(i), pull_up_visitor.get_new_expr())))) {
LOG_WARN("failed to push back to ctx record array", K(ret));
} else { /* Do nothing */
}
} else { /* Do nothing */
}
}
}
if (OB_SUCC(ret)) {
ret = ObOptimizerUtil::copy_exprs(opt_ctx->get_expr_factory(), tmp_exprs, aggr_exprs_);
} else { /* Do nothing */
}
}
return ret;
}
int ObLogGroupBy::print_my_plan_annotation(char* buf, int64_t& buf_len, int64_t& pos, ExplainType type)
{
int ret = OB_SUCCESS;
if (OB_FAIL(BUF_PRINTF(", "))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
} else if (OB_FAIL(BUF_PRINTF("\n "))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
} else {
const ObIArray<ObRawExpr*>& group = get_group_by_exprs();
EXPLAIN_PRINT_EXPRS(group, type);
const ObIArray<ObRawExpr*>& rollup = get_rollup_exprs();
if (OB_SUCC(ret) && (rollup.count() > 0)) {
if (OB_FAIL(BUF_PRINTF(", "))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
} else {
EXPLAIN_PRINT_EXPRS(rollup, type);
}
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(BUF_PRINTF(", "))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
} else {
const ObIArray<ObRawExpr*>& agg_func = get_aggr_funcs();
EXPLAIN_PRINT_EXPRS(agg_func, type);
}
}
return ret;
}
int ObLogGroupBy::produce_pushed_down_expressions(ObLogGroupBy* child_group_by,
ObIArray<std::pair<ObRawExpr*, ObRawExpr*> >& ctx_record_arr,
ObIArray<std::pair<ObRawExpr*, ObRawExpr*> >& push_down_avg_arr)
{
int ret = OB_SUCCESS;
/*
* group by
* |
* (sort)
* |
* ex in
* |
* ex out
* |
* group by(child)
* |
* (sort)
*/
if (OB_ISNULL(child_group_by)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Get unexpected null", K(ret));
} else if (OB_FAIL(child_group_by->push_down_aggr_exprs_analyze(aggr_exprs_, push_down_avg_arr))) {
LOG_WARN("failed to push down aggr exprs", K(ret));
} else if (OB_FAIL(pull_up_aggr_exprs_analyze(aggr_exprs_, ctx_record_arr))) {
LOG_WARN("Failed to analyze pull up aggr exprs", K(ret));
} else { /*do nothing*/
}
return ret;
}
int ObLogGroupBy::check_output_dep_specific(ObRawExprCheckDep& checker)
{
int ret = OB_SUCCESS;
// group by exprs
for (int64_t i = 0; OB_SUCC(ret) && i < group_exprs_.count(); ++i) {
if (OB_ISNULL(group_exprs_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("group_exprs_.at(i) is null", K(ret), K(i));
} else if (OB_FAIL(checker.check(*group_exprs_.at(i)))) {
LOG_WARN("failed to check group_exprs_.at(i)", K(ret), K(i));
} else {
}
}
// rollup exprs.
for (int64_t i = 0; OB_SUCC(ret) && i < rollup_exprs_.count(); ++i) {
if (OB_ISNULL(rollup_exprs_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("rollup_exprs.at(i) is null", K(ret), K(i));
} else if (OB_FAIL(checker.check(*rollup_exprs_.at(i)))) {
LOG_WARN("failed to check rollup_exprs.at(i)", K(ret), K(i));
} else {
}
}
// aggregations
for (int64_t i = 0; OB_SUCC(ret) && i < aggr_exprs_.count(); ++i) {
if (OB_ISNULL(aggr_exprs_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("aggr_exprs_.at(i) is null", K(ret), K(i));
} else if (OB_FAIL(checker.check(*aggr_exprs_.at(i)))) {
LOG_WARN("failed to check aggr_exprs_.at(i)", K(ret), K(i));
} else {
}
}
// avg->div expressions
for (int64_t i = 0; OB_SUCC(ret) && i < avg_div_exprs_.count(); ++i) {
if (OB_ISNULL(avg_div_exprs_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("avg_div_exprs_.at(i) is null", K(ret), K(i));
} else if (OB_FAIL(checker.check(*avg_div_exprs_.at(i)))) {
LOG_WARN("failed to check avg_div_exprs_.at(i)", K(ret), K(i));
} else {
}
}
// approx_count_distinct->estimate_ndv expressions
for (int64_t i = 0; i < OB_SUCC(ret) && i < approx_count_distinct_estimate_ndv_exprs_.count(); ++i) {
if (OB_ISNULL(approx_count_distinct_estimate_ndv_exprs_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("approx_count_distinct_estimate_ndv_exprs_.at(i) si null", K(ret), K(i));
} else if (OB_FAIL(checker.check(*approx_count_distinct_estimate_ndv_exprs_.at(i)))) {
LOG_WARN("failed to check approx_count_distinct_estimate_ndv_exprs_.at(i)", K(ret), K(i));
} else {
}
}
return ret;
}
int ObLogGroupBy::transmit_op_ordering()
{
int ret = OB_SUCCESS;
reset_op_ordering();
reset_local_ordering();
if (HASH_AGGREGATE == get_algo() || SCALAR_AGGREGATE == get_algo()) {
// do nothing
} else {
ObLogicalOperator* child = get_child(first_child);
bool need_sort = false;
if (OB_ISNULL(child)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("child is null", K(ret));
} else if (OB_FAIL(check_need_sort_for_grouping_op(0, expected_ordering_, need_sort))) {
LOG_WARN("failed to check need sort", K(ret));
} else if (need_sort && OB_FAIL(allocate_sort_below(0, expected_ordering_))) { // need alloc sort
LOG_WARN("failed to allocate implicit sort", K(ret));
} else if (has_rollup()) {
ObSEArray<OrderItem, 4> ordering;
for (int64_t i = 0; OB_SUCC(ret) && i < group_exprs_.count(); i++) {
if (OB_FAIL(ordering.push_back(expected_ordering_.at(i)))) {
LOG_WARN("failed to push back into ordering.", K(ret));
} else {
}
}
if (OB_SUCC(ret) && OB_FAIL(set_op_ordering(ordering))) {
LOG_WARN("failed to set op ordering.", K(ret));
} else {
}
} else if (FALSE_IT(child = get_child(first_child))) {
} else if (OB_FAIL(set_op_ordering(child->get_op_ordering()))) {
LOG_WARN("failed to set op's ordering", K(ret));
} else { /*do nothing.*/
}
}
return ret;
}
int ObLogGroupBy::est_cost()
{
int ret = OB_SUCCESS;
double distinct_card = 0.0;
double selectivity = 1.0;
double group_cost = 0.0;
common::ObSEArray<ObRawExpr*, 8> group_rollup_exprs;
ObLogicalOperator* child = get_child(ObLogicalOperator::first_child);
if (OB_ISNULL(child) || OB_ISNULL(get_est_sel_info())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(child));
} else if (OB_FAIL(get_group_rollup_exprs(group_rollup_exprs))) {
LOG_WARN("failed to get group rollup exprs.", K(ret));
} else if (group_rollup_exprs.empty() || SCALAR_AGGREGATE == algo_) {
distinct_card = 1.0;
} else if (OB_FAIL(ObOptEstSel::calculate_distinct(
child->get_card(), *get_est_sel_info(), group_rollup_exprs, distinct_card))) {
LOG_WARN("failed to calculate distinct", K(ret));
}
if (OB_SUCC(ret)) {
if (SCALAR_AGGREGATE == algo_) {
group_cost = ObOptEstCost::cost_scalar_group(child->get_card(), get_aggr_funcs().count());
} else if (MERGE_AGGREGATE == algo_) {
group_cost = ObOptEstCost::cost_merge_group(
child->get_card(), distinct_card, group_rollup_exprs, get_aggr_funcs().count());
} else {
group_cost =
ObOptEstCost::cost_hash_group(child->get_card(), distinct_card, group_exprs_, get_aggr_funcs().count());
}
// process having filters
if (OB_FAIL(ObOptEstSel::calculate_selectivity(*get_est_sel_info(),
get_filter_exprs(),
selectivity,
&get_plan()->get_predicate_selectivities(),
UNKNOWN_JOIN,
NULL,
NULL,
child->get_card(),
distinct_card))) {
LOG_WARN("Failed to calculate selectivity", K(ret));
}
}
if (OB_SUCC(ret)) {
distinct_card_ = distinct_card;
set_card(distinct_card * selectivity);
set_cost(child->get_cost() + group_cost);
set_op_cost(group_cost);
set_width(child->get_width());
}
return ret;
}
int ObLogGroupBy::re_est_cost(const ObLogicalOperator* parent, double need_row_count, bool& re_est)
{
int ret = OB_SUCCESS;
ObLogicalOperator* child_op = NULL;
UNUSED(parent);
re_est = false;
common::ObSEArray<ObRawExpr*, 8> group_rollup_exprs;
if (OB_ISNULL(child_op = get_child(0)) || OB_ISNULL(get_est_sel_info()) || OB_UNLIKELY(need_row_count < 0.0)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(child_op), K(get_est_sel_info()), K(need_row_count), K(ret));
} else if (need_row_count >= get_card() || get_card() <= OB_DOUBLE_EPSINON) {
// need row count >= card_, need not re_est cost
} else if (SCALAR_AGGREGATE == algo_) {
// do nothing
} else if (HASH_AGGREGATE == algo_) {
re_est = true;
distinct_card_ *= need_row_count / get_card();
double group_cost =
ObOptEstCost::cost_hash_group(child_op->get_card(), distinct_card_, group_exprs_, aggr_exprs_.count());
card_ = need_row_count;
op_cost_ = group_cost;
cost_ = group_cost + child_op->get_cost();
} else if (OB_FAIL(get_group_rollup_exprs(group_rollup_exprs))) {
LOG_WARN("failed to get group rollup exprs.", K(ret));
} else if (MERGE_AGGREGATE == algo_) {
bool child_re_est = false;
double child_need_card = need_row_count * child_op->get_card() / get_card();
if (OB_FAIL(child_op->re_est_cost(this, child_need_card, child_re_est))) {
LOG_WARN("failed to re_est_cost", K(ret));
} else {
re_est = true;
distinct_card_ *= need_row_count / get_card();
double group_cost =
ObOptEstCost::cost_merge_group(child_op->get_card(), distinct_card_, group_rollup_exprs, aggr_exprs_.count());
card_ = need_row_count;
op_cost_ = group_cost;
cost_ = group_cost + child_op->get_cost();
}
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected aggregation type", K(algo_), K(ret));
}
return ret;
}
int ObLogGroupBy::inner_replace_generated_agg_expr(const ObIArray<std::pair<ObRawExpr*, ObRawExpr*> >& to_replace_exprs)
{
int ret = OB_SUCCESS;
if (OB_FAIL(replace_exprs_action(to_replace_exprs, get_group_by_exprs()))) {
LOG_WARN("failed to extract subplan params in log group by exprs", K(ret));
} else if (OB_FAIL(replace_exprs_action(to_replace_exprs, get_rollup_exprs()))) {
LOG_WARN("failed to extract subplan params in log rollup exprs", K(ret));
} else if (OB_FAIL(replace_exprs_action(to_replace_exprs, get_aggr_funcs()))) {
LOG_WARN("failed to extract subplan params in log agg funcs", K(ret));
} else if (OB_FAIL(replace_exprs_action(to_replace_exprs, avg_div_exprs_))) {
LOG_WARN("failed to extract subplan params in avg_div_exprs", K(ret));
} else if (OB_FAIL(replace_exprs_action(to_replace_exprs, approx_count_distinct_estimate_ndv_exprs_))) {
LOG_WARN("failed to extract subplan params in approx_count_distinct_estimate_ndv_exprs", K(ret));
} else { /* Do nothing */
}
return ret;
}
int ObLogGroupBy::print_outline(planText& plan_text)
{
int ret = OB_SUCCESS;
char* buf = plan_text.buf;
int64_t& buf_len = plan_text.buf_len;
int64_t& pos = plan_text.pos;
bool is_one_line = plan_text.is_oneline_;
OutlineType type = plan_text.outline_type_;
bool is_need = false;
ObString stmt_name;
if (OB_ISNULL(get_stmt())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt is NULL", K(ret), K(get_stmt()));
} else if (USED_HINT == type && OB_FAIL(get_stmt()->get_stmt_name(stmt_name))) {
LOG_WARN("fail to get stmt_name", K(ret));
} else if (OUTLINE_DATA == type && OB_FAIL(get_stmt()->get_stmt_org_name(stmt_name))) {
LOG_WARN("fail to get stmt_name", K(ret));
} else {
const ObStmtHint& stmt_hint = stmt_->get_stmt_hint();
bool is_need = false;
if (OB_FAIL(is_need_print_agg_type(plan_text, stmt_hint, is_need))) {
LOG_WARN("failed to get the aggregate type", K(ret));
} else if (!is_need) { // do nothing.
} else if (MERGE_AGGREGATE == algo_) {
if (!is_one_line && OB_FAIL(BUF_PRINTF("\n"))) {
} else if (OB_FAIL(BUF_PRINTF(ObStmtHint::get_outline_indent(is_one_line)))) {
} else if (OB_FAIL(BUF_PRINTF(