forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_aggregate_function.cpp
More file actions
3188 lines (3116 loc) · 125 KB
/
ob_aggregate_function.cpp
File metadata and controls
3188 lines (3116 loc) · 125 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_ENG
#include "sql/engine/aggregate/ob_aggregate_function.h"
#include "share/object/ob_obj_cast.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/engine/expr/ob_expr_add.h"
#include "sql/engine/expr/ob_expr_less_than.h"
#include "sql/engine/expr/ob_expr_div.h"
#include "sql/engine/expr/ob_expr_add.h"
#include "sql/engine/expr/ob_expr_minus.h"
#include "sql/engine/expr/ob_expr_mul.h"
#include "sql/engine/expr/ob_expr_result_type_util.h"
#include "sql/engine/ob_exec_context.h"
#include "sql/engine/expr/ob_expr_estimate_ndv.h"
#include "sql/engine/user_defined_function/ob_udf_util.h"
#include "sql/engine/expr/ob_expr_operator.h"
namespace oceanbase {
using namespace common;
namespace sql {
uint64_t ObAggregateDistinctItem::hash() const
{
uint64_t hash_id = 0;
if (OB_ISNULL(cells_) || OB_ISNULL(cs_type_list_)) {
LOG_ERROR("cells or cs type list is null");
} else {
hash_id = group_id_ + col_idx_;
for (int64_t i = 0; i < cs_type_list_->count(); ++i) {
hash_id = (cells_[i].is_string_type() ? cells_[i].varchar_hash(cs_type_list_->at(i), hash_id)
: cells_[i].hash(hash_id));
}
}
return hash_id;
}
bool ObAggregateDistinctItem::operator==(const ObAggregateDistinctItem& other) const
{
bool bool_ret = true;
if (OB_ISNULL(cells_) || OB_ISNULL(cs_type_list_)) {
LOG_ERROR("cells or cs type list is null");
} else {
bool_ret = (group_id_ == other.group_id_ && col_idx_ == other.col_idx_);
if (bool_ret && cs_type_list_->count() != other.cs_type_list_->count()) {
bool_ret = false;
}
for (int64_t i = 0; bool_ret && i < cs_type_list_->count(); ++i) {
if (cells_[i].compare(other.cells_[i], cs_type_list_->at(i)) != 0) {
bool_ret = false;
}
}
}
return bool_ret;
}
ObAggCellCtx::ObAggCellCtx(ObIAllocator& alloc) : distinct_set_(NULL), alloc_(alloc)
{}
void ObAggCellCtx::reuse()
{
if (NULL != distinct_set_) {
distinct_set_->reuse();
}
}
ObAggCellCtx::~ObAggCellCtx()
{
if (NULL != distinct_set_) {
distinct_set_->~ObUniqueSort();
alloc_.free(distinct_set_);
distinct_set_ = NULL;
}
if (NULL != sort_columns_.get_base_address()) {
for (int64_t i = 0; i < sort_columns_.count(); i++) {
sort_columns_.get_base_address()[i].~ObSortColumn();
}
alloc_.free(sort_columns_.get_base_address());
sort_columns_.reset();
}
}
int ObAggCellCtx::init_distinct_set(const uint64_t tenant_id, const common::ObIArray<common::ObCollationType>& cs_types,
const int64_t sort_col_cnt, const bool need_rewind)
{
int ret = OB_SUCCESS;
if (OB_INVALID_ID == tenant_id || sort_col_cnt <= 0 || sort_col_cnt > cs_types.count()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(tenant_id), K(sort_col_cnt), K(cs_types.count()));
} else {
ObSortColumn* cols = static_cast<ObSortColumn*>(alloc_.alloc(sizeof(ObSortColumn) * sort_col_cnt));
if (NULL == cols) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc memory failed", K(ret));
} else {
for (int64_t i = 0; i < sort_col_cnt; i++) {
new (&cols[i]) ObSortColumn(i, cs_types.at(i), true /* asc order */);
}
sort_columns_.init(sort_col_cnt, cols, sort_col_cnt);
distinct_set_ = static_cast<ObUniqueSort*>(alloc_.alloc(sizeof(*distinct_set_)));
if (NULL == distinct_set_) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(tenant_id), K(sort_col_cnt), K(cs_types.count()));
} else {
new (distinct_set_) ObUniqueSort();
if (OB_FAIL(distinct_set_->init(tenant_id, sort_columns_, need_rewind))) {
LOG_WARN("init distinct set failed", K(ret));
}
}
}
}
if (OB_SUCCESS != ret) {
if (NULL != distinct_set_) {
distinct_set_->~ObUniqueSort();
alloc_.free(distinct_set_);
distinct_set_ = NULL;
}
if (NULL != sort_columns_.get_base_address()) {
for (int64_t i = 0; i < sort_columns_.count(); i++) {
sort_columns_.get_base_address()[i].~ObSortColumn();
}
alloc_.free(sort_columns_.get_base_address());
sort_columns_.reset();
}
}
return ret;
}
ObGroupConcatRowStore::ObGroupConcatRowStore() : need_sort_(false), rows_(0), iter_idx_(0)
{}
ObGroupConcatRowStore::~ObGroupConcatRowStore()
{}
int ObGroupConcatRowStore::init(const uint64_t tenant_id, const ObIArray<ObSortColumn>& sort_columns,
const ObSortImpl::SortExtraInfos* extra_infos, const bool rewind)
{
int ret = OB_SUCCESS;
rows_ = 0;
iter_idx_ = 0;
if (sort_columns.empty()) {
int64_t sort_area_size = 0;
if (OB_FAIL(ObSqlWorkareaUtil::get_workarea_size(SORT_WORK_AREA, tenant_id, sort_area_size))) {
LOG_WARN("failed to get workarea size", K(ret), K(tenant_id));
} else if (OB_FAIL(rs_.init(sort_area_size,
tenant_id,
ObCtxIds::WORK_AREA,
ObModIds::OB_SQL_AGGR_FUN_GROUP_CONCAT,
true /* enable dump */,
ObChunkRowStore::FULL))) {
LOG_WARN("row store failed", K(ret));
}
need_sort_ = false;
} else {
if (OB_FAIL(sort_.init(tenant_id, sort_columns, extra_infos, false /* local order */, rewind))) {
LOG_WARN("sort columns failed", K(ret));
}
need_sort_ = true;
}
return ret;
}
void ObGroupConcatRowStore::reuse()
{
if (need_sort_) {
sort_.reuse();
} else {
rs_it_.reset();
rs_.reuse();
}
rows_ = 0;
iter_idx_ = 0;
}
int ObGroupConcatRowStore::finish_add_row()
{
iter_idx_ = 0;
int ret = OB_SUCCESS;
if (need_sort_) {
ret = sort_.sort();
} else {
rs_it_.reset();
ret = rs_it_.init(&rs_, ObChunkRowStore::BLOCK_SIZE);
}
return ret;
}
int ObGroupConcatRowStore::rewind()
{
int ret = OB_SUCCESS;
if (need_sort_) {
if (OB_FAIL(sort_.rewind())) {
LOG_WARN("rewind failed", K(ret));
}
} else {
rs_it_.reset();
if (OB_FAIL(rs_it_.init(&rs_, ObChunkRowStore::BLOCK_SIZE))) {
LOG_WARN("row store iterator init failed", K(ret));
}
}
iter_idx_ = 0;
return ret;
}
void ObGroupConcatCtx::reuse()
{
if (NULL != gc_rs_) {
gc_rs_->reuse();
}
ObAggCellCtx::reuse();
}
ObGroupConcatCtx::~ObGroupConcatCtx()
{
if (NULL != gc_rs_) {
gc_rs_->~ObGroupConcatRowStore();
alloc_.free(gc_rs_);
gc_rs_ = NULL;
}
}
ObAggregateFunction::ObAggregateFunction()
: has_distinct_(false),
has_sort_(false),
is_sort_based_gby_(false),
in_window_func_(false),
aggr_fun_need_cell_ctx_(false),
agg_cell_ctx_cnt_(0),
output_column_count_(0),
full_column_count_(0),
did_int_div_as_double_(false),
stored_row_buf_(ObModIds::OB_SQL_AGGR_FUNC_ROW, common::OB_MALLOC_MIDDLE_BLOCK_SIZE, OB_SERVER_TENANT_ID,
ObCtxIds::WORK_AREA),
expr_ctx_(),
child_column_count_(0),
aggr_distinct_set_(),
row_array_(),
agg_cell_ctx_alloc_(ObModIds::OB_SQL_AGGR_CELL_CTX, common::OB_MALLOC_NORMAL_BLOCK_SIZE, OB_SERVER_TENANT_ID,
ObCtxIds::WORK_AREA),
group_concat_max_len_(OB_DEFAULT_GROUP_CONCAT_MAX_LEN),
alloc_(ObModIds::OB_SQL_AGGR_FUNC, common::OB_MALLOC_NORMAL_BLOCK_SIZE, OB_SERVER_TENANT_ID, ObCtxIds::WORK_AREA),
input_cells_(NULL),
gconcat_cur_row_num_(0),
first_rollup_cols_(),
agg_udf_buf_(ObModIds::OB_SQL_UDF),
agg_udf_metas_(),
agg_udf_()
{
if (share::is_oracle_mode()) {
group_concat_max_len_ = OB_DEFAULT_GROUP_CONCAT_MAX_LEN_FOR_ORACLE;
}
}
ObAggregateFunction::~ObAggregateFunction()
{
destroy();
}
// select the position where the column appears for the first time as a mark,
// and the first column shall prevail in subsequent processing.
int ObAggregateFunction::init_first_rollup_cols(
common::ObIAllocator* alloc, const ObIArray<ObColumnInfo>& group_idxs, const ObIArray<ObColumnInfo>& rollup_idxs)
{
int64_t ret = OB_SUCCESS;
common::ObSEArray<int64_t, 16> no_dup_group_col_idxs;
common::ObSEArray<int64_t, 16> no_dup_rollup_col_idxs;
first_rollup_cols_.set_allocator(alloc);
if (OB_FAIL(first_rollup_cols_.init(group_idxs.count() + rollup_idxs.count()))) {
LOG_WARN("failed to init the first rollup cols.", K(ret));
} else { /*do nothing.*/
}
for (int64_t i = 0; OB_SUCC(ret) && i < group_idxs.count(); i++) {
if (OB_FAIL(no_dup_group_col_idxs.push_back(group_idxs.at(i).index_))) {
LOG_WARN("failed to push back into group col idxs.", K(ret));
} else if (OB_FAIL(first_rollup_cols_.push_back(false))) {
LOG_WARN("failed to push back cols.", K(ret));
} else { /*do nothing.*/
}
}
for (int64_t j = 0; OB_SUCC(ret) && j < rollup_idxs.count(); j++) {
if (is_contain(no_dup_group_col_idxs, rollup_idxs.at(j).index_)) {
if (OB_FAIL(first_rollup_cols_.push_back(false))) {
LOG_WARN("failed to push back cols.", K(ret));
} else { /*do nothing.*/
}
} else if (is_contain(no_dup_rollup_col_idxs, rollup_idxs.at(j).index_)) {
if (OB_FAIL(first_rollup_cols_.push_back(false))) {
LOG_WARN("failed to push back cols.", K(ret));
} else { /*do nothing.*/
}
} else if (OB_FAIL(no_dup_rollup_col_idxs.push_back(rollup_idxs.at(j).index_))) {
LOG_WARN("failed to push back cols.", K(ret));
} else if (OB_FAIL(first_rollup_cols_.push_back(true))) {
LOG_WARN("failed to push back cols.", K(ret));
} else { /*do nothing.*/
}
}
return ret;
}
int ObAggregateFunction::init(const int64_t input_column_count, const ObAggrExprList* aggr_columns, ObExprCtx& expr_ctx,
int32_t prepare_row_num, int64_t distinct_set_bucket_num)
{
int ret = OB_SUCCESS;
ObItemType aggr_fun;
bool is_distinct = false;
int64_t aggr_count = 0;
ObCollationType tmp_cs_type = CS_TYPE_INVALID;
gconcat_cur_row_num_ = 0;
// copy reference of aggr_column
full_column_count_ = input_column_count;
output_column_count_ = input_column_count;
child_column_count_ = input_column_count;
row_array_.reserve(prepare_row_num);
if (OB_ISNULL(expr_ctx.phy_plan_ctx_) || OB_ISNULL(expr_ctx.calc_buf_) || OB_ISNULL(expr_ctx.my_session_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("expr_ctx is valid", K_(expr_ctx.phy_plan_ctx), K_(expr_ctx.my_session), K_(expr_ctx.calc_buf));
} else {
if (share::is_oracle_mode()) {
group_concat_max_len_ = OB_DEFAULT_GROUP_CONCAT_MAX_LEN_FOR_ORACLE;
} else {
group_concat_max_len_ = OB_DEFAULT_GROUP_CONCAT_MAX_LEN;
}
if (OB_FAIL(expr_ctx.my_session_->get_group_concat_max_len(group_concat_max_len_))) {
LOG_WARN("fail to get group concat max len", K(ret));
} else {
expr_ctx_ = expr_ctx;
}
}
// add aggr columns
agg_cell_ctx_cnt_ = 0;
agg_columns_.reuse();
DLIST_FOREACH(node, *aggr_columns)
{
++output_column_count_;
++full_column_count_;
const ObAggregateExpression* cexpr = static_cast<const ObAggregateExpression*>(node);
if (OB_ISNULL(cexpr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr node is null", K(cexpr));
} else if (OB_FAIL(cexpr->get_aggr_column(aggr_fun, is_distinct, tmp_cs_type))) {
LOG_WARN("failed to get aggr column", K(ret));
} else {
if (is_distinct) {
has_distinct_ = true;
}
if (!cexpr->get_sort_columns().empty()) {
has_sort_ = true;
}
switch (aggr_fun) {
case T_FUN_GROUP_CONCAT:
case T_FUN_GROUP_RANK:
case T_FUN_GROUP_DENSE_RANK:
case T_FUN_GROUP_PERCENT_RANK:
case T_FUN_GROUP_CUME_DIST:
case T_FUN_GROUP_PERCENTILE_CONT:
case T_FUN_GROUP_PERCENTILE_DISC:
case T_FUN_MEDIAN:
case T_FUN_KEEP_MAX:
case T_FUN_KEEP_MIN:
case T_FUN_KEEP_SUM:
case T_FUN_KEEP_COUNT:
case T_FUN_KEEP_WM_CONCAT:
case T_FUN_WM_CONCAT: {
aggr_fun_need_cell_ctx_ = true;
break;
}
default:
break;
}
if (T_FUN_AVG == aggr_fun || T_FUN_APPROX_COUNT_DISTINCT == aggr_fun) {
++full_column_count_;
}
if (aggr_count < cexpr->get_all_param_col_count()) {
aggr_count = cexpr->get_all_param_col_count();
}
ExprCtxIdx pair;
pair.expr_ = cexpr;
// distinct ,group concat ,rank and so on need agg cell context
if (is_distinct || aggr_fun_need_cell_ctx_) {
pair.ctx_idx_ = agg_cell_ctx_cnt_;
agg_cell_ctx_cnt_ += 1;
}
if (OB_FAIL(agg_columns_.push_back(pair))) {
LOG_WARN("array push back failed", K(ret));
}
}
} // end for
if (OB_SUCC(ret)) {
if (aggr_count > 0) {
void* tmp_ptr = NULL;
if (OB_UNLIKELY(NULL == (tmp_ptr = alloc_.alloc(aggr_count * sizeof(ObObj))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("fail to alloc input cells", K(ret), K(aggr_count));
} else {
input_cells_ = new (tmp_ptr) ObObj[aggr_count];
}
}
}
if (OB_SUCC(ret) && has_distinct_) {
if (OB_FAIL(aggr_distinct_set_.create(distinct_set_bucket_num))) {
LOG_WARN("fail to init aggr distinct set", K(ret));
}
}
return ret;
}
void ObAggregateFunction::destroy()
{
if (has_distinct_) {
aggr_distinct_set_.destroy();
}
if (agg_cell_ctx_cnt_ > 0) {
FOREACH_CNT(gr, row_array_)
{
if (NULL != gr->ctx_) {
for (int64_t i = 0; i < agg_cell_ctx_cnt_; i++) {
if (NULL != gr->ctx_[i]) {
gr->ctx_[i]->~ObAggCellCtx();
agg_cell_ctx_alloc_.free(gr->ctx_[i]);
gr->ctx_[i] = NULL;
}
}
}
}
}
// here can not use reuse(),reuse() reuses memory,memory cannot be released,
// which does not satisfy the semantics of destroy
agg_cell_ctx_alloc_.reset();
row_array_.destroy();
stored_row_buf_.reset();
if (NULL != input_cells_) {
alloc_.free(input_cells_);
input_cells_ = NULL;
}
alloc_.reset();
if (agg_udf_.created()) {
common::hash::ObHashMap<int64_t, ObAggUdfExeUnit, common::hash::NoPthreadDefendMode>::iterator iter =
agg_udf_.begin();
for (; iter != agg_udf_.end(); iter++) {
ObAggUdfExeUnit& agg_unit = iter->second;
if (OB_NOT_NULL(agg_unit.agg_func_) && OB_NOT_NULL(agg_unit.udf_ctx_)) {
IGNORE_RETURN agg_unit.agg_func_->process_deinit_func(*agg_unit.udf_ctx_);
}
}
}
first_rollup_cols_.reset();
agg_udf_.destroy();
agg_udf_buf_.reset();
}
void ObAggregateFunction::reuse()
{
if (agg_cell_ctx_cnt_ > 0) {
FOREACH_CNT(gr, row_array_)
{
if (NULL != gr->ctx_) {
for (int64_t i = 0; i < agg_cell_ctx_cnt_; i++) {
if (NULL != gr->ctx_[i]) {
gr->ctx_[i]->~ObAggCellCtx();
// allocate from arena allocator, no need to free
gr->ctx_[i] = NULL;
}
}
}
}
}
agg_cell_ctx_alloc_.reset_remain_one_page();
row_array_.reuse();
stored_row_buf_.reset_remain_one_page();
agg_udf_buf_.reset_remain_one_page();
if (aggr_distinct_set_.size() > 0) {
aggr_distinct_set_.reuse();
}
agg_udf_.reuse();
}
int ObAggregateFunction::clone_cell(const ObObj& src_cell, ObObj& target_cell)
{
int ret = OB_SUCCESS;
if (ObNumberTC == src_cell.get_type_class()) {
ret = clone_number_cell(src_cell, target_cell);
} else if (OB_UNLIKELY(src_cell.need_deep_copy())) {
char* buf = NULL;
// length + magic num + data
int64_t need_size = sizeof(int64_t) * 2 + src_cell.get_deep_copy_size();
void* data_ptr = NULL;
const int64_t data_length = target_cell.get_data_length();
if (target_cell.get_type() == src_cell.get_type() &&
NULL != (data_ptr = const_cast<void*>(target_cell.get_data_ptr())) && 0 != data_length) {
int64_t curr_size = 0;
if (OB_ISNULL((char*)data_ptr - sizeof(int64_t)) ||
OB_ISNULL((char*)data_ptr - sizeof(int64_t) - sizeof(int64_t))) {
ret = OB_ERR_UNEXPECTED;
;
LOG_ERROR("clone_cell use stored_row_buf, need has meta", KP(data_ptr), K(ret));
} else if (OB_UNLIKELY(*((int64_t*)(data_ptr)-1) != STORED_ROW_MAGIC_NUM)) {
ret = OB_ERR_UNEXPECTED;
;
LOG_ERROR("stored_row_buf memory is mismatch, maybe some one make bad things",
"curr_magic",
*((int64_t*)(data_ptr)),
K(ret));
} else if (OB_UNLIKELY((curr_size = *((int64_t*)(data_ptr)-2)) < data_length)) {
ret = OB_ERR_UNEXPECTED;
;
LOG_ERROR("target obj size is overflow", K(curr_size), "target_size", data_length, K(ret));
} else {
if (need_size > curr_size) {
need_size = need_size * 2;
void* buff_ptr = NULL;
if (OB_ISNULL(buff_ptr = static_cast<char*>(stored_row_buf_.alloc(need_size)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
} else {
((int64_t*)buff_ptr)[0] = need_size;
((int64_t*)buff_ptr)[1] = STORED_ROW_MAGIC_NUM;
buf = (char*)((int64_t*)(buff_ptr) + 2);
LOG_DEBUG("succ to alloc buff", K(need_size), K(src_cell), K(target_cell), K(lbt()));
}
} else {
buf = (char*)(data_ptr);
}
}
} else {
void* buff_ptr = NULL;
if (OB_ISNULL(buff_ptr = static_cast<char*>(stored_row_buf_.alloc(need_size)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
} else {
((int64_t*)buff_ptr)[0] = need_size;
((int64_t*)buff_ptr)[1] = STORED_ROW_MAGIC_NUM;
buf = (char*)((int64_t*)(buff_ptr) + 2);
LOG_DEBUG("succ to alloc buff", K(need_size), K(src_cell), K(target_cell), K(lbt()));
}
}
if (OB_SUCC(ret)) {
int64_t pos = 0;
ret = target_cell.deep_copy(src_cell, buf, need_size, pos);
}
} else {
target_cell = src_cell;
}
return ret;
}
int ObAggregateFunction::clone_number_cell(const ObObj& src_cell, ObObj& target_cell)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(ObNumberTC != src_cell.get_type_class()) ||
OB_UNLIKELY(src_cell.get_number_byte_length() > number::ObNumber::MAX_CALC_BYTE_LEN)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("argument is invalid",
K(src_cell.get_type()),
K(target_cell.get_type()),
K(src_cell.get_number_byte_length()),
K(src_cell),
K(ret));
} else {
const int64_t NEED_ALLOC_SIZE = sizeof(int64_t) * 2 + number::ObNumber::MAX_CALC_BYTE_LEN;
char* buf = NULL;
void* data_ptr = NULL;
bool is_finish = false;
if (src_cell.is_zero_number()) {
is_finish = true;
target_cell.set_number(src_cell.get_type(), src_cell.get_number_desc(), NULL);
} else if (ObNumberTC == target_cell.get_type_class() &&
NULL != (data_ptr = (void*)(const_cast<uint32_t*>(target_cell.get_number_digits()))) &&
0 != target_cell.get_number_digit_length()) {
int64_t curr_alloc_size = 0;
if (OB_ISNULL((char*)data_ptr - sizeof(int64_t)) ||
OB_ISNULL((char*)data_ptr - sizeof(int64_t) - sizeof(int64_t))) {
ret = OB_ERR_UNEXPECTED;
;
LOG_ERROR("clone_cell use stored_row_buf, need has meta", KP(data_ptr), K(ret));
} else if (OB_UNLIKELY(*((int64_t*)(data_ptr)-1) != STORED_ROW_MAGIC_NUM) ||
OB_UNLIKELY((curr_alloc_size = *((int64_t*)(data_ptr)-2)) != NEED_ALLOC_SIZE)) {
ret = OB_ERR_UNEXPECTED;
;
LOG_ERROR("stored_row_buf memory is mismatch, maybe some one make bad things",
"curr_magic",
*((int64_t*)(data_ptr)-1),
"curr_size",
*((int64_t*)(data_ptr)-2),
K(target_cell),
K(ret));
} else {
buf = (char*)(data_ptr);
}
} else {
if (OB_ISNULL(data_ptr = static_cast<char*>(stored_row_buf_.alloc(NEED_ALLOC_SIZE)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to alloc memory", K(ret), K(NEED_ALLOC_SIZE));
} else {
((int64_t*)data_ptr)[0] = NEED_ALLOC_SIZE;
((int64_t*)data_ptr)[1] = STORED_ROW_MAGIC_NUM;
buf = (char*)((int64_t*)(data_ptr) + 2);
LOG_DEBUG("succ to alloc buff",
"curr_magic",
*((int64_t*)(buf)-1),
"curr_size",
*((int64_t*)(buf)-2),
K(src_cell),
K(target_cell),
K(lbt()));
}
}
if (OB_SUCC(ret) && !is_finish) {
MEMCPY(buf, src_cell.get_number_digits(), src_cell.get_number_byte_length());
target_cell = src_cell;
target_cell.set_number(src_cell.get_type(), src_cell.get_number_desc(), (uint32_t*)(buf));
}
}
return ret;
}
int ObAggregateFunction::fill_distinct_item_cell_list(
const ObAggregateExpression* cexpr, const ObNewRow& input_row, ObAggregateDistinctItem& distinct_item)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(cexpr) || OB_UNLIKELY(input_row.is_invalid()) ||
OB_UNLIKELY(input_row.get_count() < cexpr->get_real_param_col_count())) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(cexpr), K_(input_row.count));
} else if (OB_ISNULL(distinct_item.cells_ = static_cast<ObObj*>(
stored_row_buf_.alloc(sizeof(ObObj) * cexpr->get_real_param_col_count())))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc memory failed", K(ret));
} else {
distinct_item.cs_type_list_ = cexpr->get_aggr_cs_types();
}
for (int64_t i = 0; OB_SUCC(ret) && i < cexpr->get_real_param_col_count(); ++i) {
new (&distinct_item.cells_[i]) ObObj();
if (OB_FAIL(clone_cell(input_row.get_cell(i), distinct_item.cells_[i]))) {
LOG_WARN("failed to clone cell", K(ret), K(i), K(input_row.cells_[i]));
}
}
return ret;
}
int ObAggregateFunction::prepare(const ObNewRow& row, int64_t group_id, ObRowStore::StoredRow** output_row)
{
int ret = OB_SUCCESS;
ObRowStore::StoredRow* stored_row = NULL;
ObItemType aggr_fun = T_INVALID;
bool is_distinct = false;
ObObj* aggr_cell = NULL;
ObObj* aux_cell = NULL;
int64_t aggr_idx = child_column_count_;
// AVG, APPROX_COUNT_DISTINCT to use auxiliary columns.
int64_t aux_col_idx = 0;
int64_t agg_udf_meta_offset = 0;
// for sort-based group by operator, for performance reason,
// after producing a group, we will invoke reuse_group() function to clear the group
// thus, we do not need to allocate the group space again here, simply reuse the space
if (group_id >= row_array_.count() && OB_FAIL(init_one_group(group_id))) {
LOG_WARN("failed to init one group", K(group_id), K(ret));
}
// get stored row and group_concat_array
if (OB_SUCC(ret)) {
if (OB_FAIL(get_stored_row(group_id, stored_row))) {
LOG_WARN("failed to get stored row and group_concat_array", K(ret), K(group_id));
} else {
stored_row->reserved_cells_count_ = static_cast<int32_t>(full_column_count_);
}
}
// process non aggregate cols
for (int64_t i = 0; OB_SUCC(ret) && i < child_column_count_; i++) {
// If the row generated by the child operator has a projector,
// then the cell should be the projected cell
if (OB_FAIL(clone_cell(row.get_cell(i), stored_row->reserved_cells_[i]))) {
LOG_WARN("failed to clone cell", K(ret));
} else { /*do nothing.*/
}
}
// process aggregate columns
FOREACH_CNT_X(node, agg_columns_, OB_SUCC(ret))
{
ObCollationType cs_type = CS_TYPE_INVALID;
const ObAggregateExpression* cexpr = static_cast<const ObAggregateExpression*>(node->expr_);
int64_t agg_udf_id = GET_AGG_UDF_ID(group_id, child_column_count_, agg_columns_.count(), aggr_idx);
ObAggUdfMeta* agg_udf_meta = nullptr;
if (OB_ISNULL(cexpr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr/node is null", K(node->expr_));
} else if (OB_FAIL(cexpr->get_aggr_column(aggr_fun, is_distinct, cs_type))) {
LOG_WARN("failed to get aggr column", K(ret));
} else {
ObNewRow input_row;
input_row.cells_ = input_cells_;
input_row.count_ = cexpr->get_all_param_col_count();
if (OB_FAIL(cexpr->calc_result_row(expr_ctx_, row, input_row))) {
LOG_WARN("fail to get calc cell list", K(ret), K(row), K(*cexpr));
} else {
aggr_cell = &(stored_row->reserved_cells_[aggr_idx]);
if (T_FUN_AVG != aggr_fun && T_FUN_APPROX_COUNT_DISTINCT != aggr_fun) {
aux_cell = NULL;
} else {
aux_cell = &(stored_row->reserved_cells_[output_column_count_ + aux_col_idx++]);
}
if (T_FUN_AGG_UDF == aggr_fun) {
if (agg_udf_meta_offset >= agg_udf_metas_.count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("the agg udf meta is invalid", K(ret), K(agg_udf_meta_offset), K(agg_udf_metas_.count()));
} else {
agg_udf_meta = &agg_udf_metas_.at(agg_udf_meta_offset);
++agg_udf_meta_offset;
}
}
if (OB_SUCC(ret)) {
ObAggCellCtx* cell_ctx = get_agg_cell_ctx(group_id, node->ctx_idx_);
if (!(is_distinct && is_sort_based_gby_)) {
if (OB_FAIL(init_aggr_cell(
aggr_fun, input_row, *aggr_cell, aux_cell, cell_ctx, cs_type, agg_udf_id, agg_udf_meta))) {
LOG_WARN("failed to init cell", K(ret));
}
}
if (OB_SUCC(ret) && is_distinct) {
if (!is_sort_based_gby_) {
ObAggregateDistinctItem distinct_item;
distinct_item.group_id_ = group_id;
distinct_item.col_idx_ = aggr_idx;
distinct_item.cs_type_ = cs_type;
if (OB_FAIL(fill_distinct_item_cell_list(cexpr, input_row, distinct_item))) {
LOG_WARN("failed to fill distinct item cell list", K(ret), K(*cexpr), K(input_row));
} else if (OB_FAIL(aggr_distinct_set_.set_refactored(distinct_item))) {
// collect distinct cells
LOG_WARN("failed to set distinct item", K(ret));
} else {
ret = OB_SUCCESS;
}
} else {
if (OB_ISNULL(cell_ctx) || OB_ISNULL(cell_ctx->distinct_set_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("distinct set is NULL", K(ret));
} else if (OB_FAIL(cell_ctx->distinct_set_->add_row(input_row))) {
LOG_WARN("add row to distinct set failed", K(ret));
}
}
}
}
}
if (input_row.cells_ != NULL) {
for (int64_t idx = 0; idx < input_row.count_; idx++) {
input_row.cells_[idx].reset();
}
}
}
if (OB_SUCC(ret)) {
++aggr_idx;
}
} // end for
if (OB_SUCC(ret) && output_row != NULL) {
*output_row = stored_row;
}
return ret;
}
int ObAggregateFunction::process(const ObNewRow& row, const ObTimeZoneInfo* tz_info, const int64_t group_id)
{
int ret = OB_SUCCESS;
ObRowStore::StoredRow* stored_row = NULL;
ObObj* aggr_cell = NULL;
ObObj* aux_cell = NULL;
ObItemType aggr_fun = T_INVALID;
bool is_distinct = false;
int64_t aggr_idx = child_column_count_;
int64_t aux_col_idx = 0;
// get stored row and group_concat_array
if (OB_FAIL(get_stored_row(group_id, stored_row))) {
LOG_WARN("failed to get stored row", K(ret));
}
FOREACH_CNT_X(node, agg_columns_, OB_SUCC(ret))
{
ObCollationType cs_type = CS_TYPE_INVALID;
const ObAggregateExpression* cexpr = static_cast<const ObAggregateExpression*>(node->expr_);
int64_t agg_udf_id = GET_AGG_UDF_ID(group_id, child_column_count_, agg_columns_.count(), aggr_idx);
if (OB_ISNULL(cexpr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("cexpr node is null", K(node->expr_), K(ret));
} else if (OB_FAIL(cexpr->get_aggr_column(aggr_fun, is_distinct, cs_type))) {
LOG_WARN("failed to get aggr column", K(ret));
} else if (OB_UNLIKELY(aggr_idx < 0) || OB_UNLIKELY(aggr_idx >= stored_row->reserved_cells_count_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid aggr idx", K(aggr_idx), K(ret));
} else if (T_FUN_COUNT == aggr_fun && cexpr->is_empty()) {
aggr_cell = &stored_row->reserved_cells_[aggr_idx];
int64_t aux = 0;
if (OB_ISNULL(aggr_cell)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("aggr_cell is null", K(ret));
} else if (OB_FAIL(aggr_cell->get_int(aux))) {
LOG_WARN("fail to get aux", K(ret));
} else {
aggr_cell->set_int(++aux);
}
} else {
aggr_cell = &stored_row->reserved_cells_[aggr_idx];
if (T_FUN_AVG != aggr_fun && T_FUN_APPROX_COUNT_DISTINCT != aggr_fun) {
aux_cell = NULL;
} else {
aux_cell = &(stored_row->reserved_cells_[output_column_count_ + aux_col_idx++]);
}
if (OB_SUCC(ret)) {
ObAggCellCtx* cell_ctx = get_agg_cell_ctx(group_id, node->ctx_idx_);
ObNewRow input_row;
input_row.count_ = cexpr->get_all_param_col_count();
input_row.cells_ = input_cells_;
if (OB_FAIL(cexpr->calc_result_row(expr_ctx_, row, input_row))) {
LOG_WARN("fail to get calc cell list", K(ret), K(row), K(*cexpr));
} else if (is_distinct) {
if (!is_sort_based_gby_) {
ObAggregateDistinctItem distinct_item;
distinct_item.group_id_ = group_id;
distinct_item.col_idx_ = aggr_idx;
distinct_item.cs_type_ = cs_type;
if (OB_FAIL(calc_distinct_item(
aggr_fun, input_row, tz_info, cexpr, distinct_item, *aggr_cell, aux_cell, cell_ctx))) {
LOG_WARN("failed to calculate distinct item", K(ret));
}
} else {
if (OB_ISNULL(cell_ctx) || OB_ISNULL(cell_ctx->distinct_set_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("distinct set is NULL", K(ret));
} else if (OB_FAIL(cell_ctx->distinct_set_->add_row(input_row))) {
LOG_WARN("add row to distinct set failed", K(ret));
}
}
} else {
// not distinct aggr column
if (OB_FAIL(calc_aggr_cell(aggr_fun,
input_row,
*aggr_cell,
aux_cell,
tz_info,
cexpr->get_collation_type(),
cell_ctx,
agg_udf_id))) {
LOG_WARN("failed to calculate aggr cell", K(ret));
}
}
if (input_cells_ != NULL) {
for (int64_t idx = 0; idx < input_row.count_; idx++) {
input_cells_[idx].reset();
}
}
}
}
if (OB_SUCC(ret)) {
++aggr_idx;
}
} // end for
if (OB_SUCC(ret)) {
if (static_cast<ObArenaAllocator*>(expr_ctx_.calc_buf_)->used() >= AGGR_CALC_BUF_LIMIT) {
static_cast<ObArenaAllocator*>(expr_ctx_.calc_buf_)->reset_remain_one_page();
}
}
return ret;
}
int ObAggregateFunction::get_stored_row(const int64_t group_id, ObRowStore::StoredRow*& stored_row)
{
int ret = OB_SUCCESS;
GroupRow gr;
// get stored row
if (OB_FAIL(row_array_.at(group_id, gr))) {
LOG_WARN("failed to get stored row", K(ret));
} else if (OB_ISNULL(gr.row_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stored_row is null", K(stored_row));
} else {
stored_row = gr.row_;
// do nothing
}
return ret;
}
int ObAggregateFunction::calc_distinct_item(const ObItemType aggr_fun, const common::ObNewRow& input_row,
const common::ObTimeZoneInfo* tz_info, const ObAggregateExpression* cexpr, ObAggregateDistinctItem& distinct_item,
ObObj& res1, ObObj* res2, ObAggCellCtx* cell_ctx, const bool should_fill_item, const bool should_calc)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(OB_ISNULL(cexpr) || input_row.is_invalid())) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret));
} else {
if (should_fill_item && OB_FAIL(fill_distinct_item_cell_list(cexpr, input_row, distinct_item))) {
LOG_WARN("failed to fill distinct item cell list", K(ret), K(*cexpr), K(input_row));
} else if (OB_HASH_NOT_EXIST == (ret = aggr_distinct_set_.exist_refactored(distinct_item))) {
if (should_calc &&
OB_FAIL(calc_aggr_cell(aggr_fun, input_row, res1, res2, tz_info, cexpr->get_collation_type(), cell_ctx))) {
LOG_WARN("failed to calculate aggr cell", K(ret));
} else if (OB_FAIL(aggr_distinct_set_.set_refactored(distinct_item))) {
LOG_WARN("fail to set distinct item", K(ret));
} else {
ret = OB_SUCCESS;
}
} else if (ret != OB_HASH_EXIST) {
LOG_WARN("check exist from aggr_distinct_set failed", K(ret));
} else {
ret = OB_SUCCESS;
}
}
return ret;
}
int ObAggregateFunction::aggr_distinct_cell(const ObItemType aggr_fun, ObAggCellCtx* cell_ctx, common::ObObj& res1,
common::ObObj* res2, const common::ObTimeZoneInfo* tz_info, common::ObCollationType cs_type, int64_t agg_udf_id,
ObAggUdfMeta* agg_udf_meta)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(cell_ctx) || OB_ISNULL(cell_ctx->distinct_set_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("distinct set is NULL", K(ret));
} else {
const ObNewRow* row = NULL;
bool first = true;
while (OB_SUCCESS == ret) {
if (OB_FAIL(cell_ctx->distinct_set_->get_next_row(row))) {
if (OB_ITER_END == ret) {
ret = OB_SUCCESS;
} else {
LOG_WARN("get row from distinct set failed", K(ret));
}
break;
} else if (OB_ISNULL(row)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("return row is NULL", K(ret));
} else {
if (first) {
if (OB_FAIL(init_aggr_cell(aggr_fun, *row, res1, res2, cell_ctx, cs_type, agg_udf_id, agg_udf_meta))) {
LOG_WARN("init aggregation cell failed", K(ret));
}
} else {
if (OB_FAIL(calc_aggr_cell(aggr_fun, *row, res1, res2, tz_info, cs_type, cell_ctx, agg_udf_id))) {
LOG_WARN("calc aggr cell failed", K(ret));
}
}
first = false;
}
}
}
return ret;
}
int ObAggregateFunction::rollup_init(const int64_t num_group_col)
{
int ret = OB_SUCCESS;
for (int64_t group_id = 0; OB_SUCC(ret) && group_id <= num_group_col; ++group_id) {
if (OB_FAIL(init_one_group(group_id))) {
LOG_WARN("failed to init one group", K(group_id), K(ret));
}
}
return ret;
}
int ObAggregateFunction::init_one_group(const int64_t group_id)
{
int ret = OB_SUCCESS;
GroupRow gr;
const int64_t stored_row_size = sizeof(ObRowStore::StoredRow) + sizeof(ObObj) * full_column_count_;
const int64_t ctx_ptr_size = sizeof(ObAggCellCtx*) * agg_cell_ctx_cnt_;
if (NULL == (gr.row_ = static_cast<ObRowStore::StoredRow*>(stored_row_buf_.alloc(stored_row_size + ctx_ptr_size)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("alloc stored row failed", K(stored_row_size), K(ctx_ptr_size), K(group_id), K(ret));
} else {
MEMSET(gr.row_, 0, stored_row_size + ctx_ptr_size);
gr.ctx_ = reinterpret_cast<ObAggCellCtx**>(reinterpret_cast<char*>(gr.row_) + stored_row_size);
if (OB_FAIL(row_array_.push_back(gr))) {
LOG_WARN("fail to push row to row_array", K(group_id), K(ret));
}
}
// setup aggregate cell context for distinct or (groupconcat,rank,dense rank,percent rank,
// cume_dist,keep_clause(max(...)/min(...)/count(...)/sum(...) keep ...)
if (OB_SUCC(ret) && (has_distinct_ || aggr_fun_need_cell_ctx_)) {
FOREACH_CNT_X(node, agg_columns_, OB_SUCC(ret))
{
ObCollationType cs_type = CS_TYPE_INVALID;
ObItemType aggr_fun = T_INVALID;
bool is_distinct = false;
const ObAggregateExpression* cexpr = static_cast<const ObAggregateExpression*>(node->expr_);
if (OB_ISNULL(cexpr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr/node is null", K(node->expr_), K(ret));
} else if (OB_FAIL(cexpr->get_aggr_column(aggr_fun, is_distinct, cs_type))) {