Coverage Report

Created: 2026-08-06 22:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/vectorized_agg_fn.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
#include <gen_cpp/Types_types.h>
20
21
#include <cstddef>
22
#include <string>
23
#include <vector>
24
25
#include "common/be_mock_util.h"
26
#include "common/factory_creator.h"
27
#include "common/status.h"
28
#include "core/data_type/data_type.h"
29
#include "exec/sort/sort_description.h"
30
#include "exprs/aggregate/aggregate_function.h"
31
#include "exprs/vexpr_fwd.h"
32
#include "runtime/runtime_profile.h"
33
34
namespace doris {
35
36
class RuntimeState;
37
class SlotDescriptor;
38
class ObjectPool;
39
class RowDescriptor;
40
class TExpr;
41
class TExprNode;
42
class TSortInfo;
43
44
class Arena;
45
class Block;
46
class BufferWritable;
47
class IColumn;
48
class QueryContext;
49
50
class AggFnEvaluator {
51
public:
52
    ENABLE_FACTORY_CREATOR(AggFnEvaluator);
53
181
    MOCK_DEFINE(virtual) ~AggFnEvaluator() = default;
54
55
public:
56
    static Status create(ObjectPool* pool, const TExpr& desc, const TSortInfo& sort_info,
57
                         const bool without_key, const bool is_window_function,
58
                         AggFnEvaluator** result);
59
60
    Status prepare(RuntimeState* state, const RowDescriptor& desc,
61
                   const SlotDescriptor* intermediate_slot_desc,
62
                   const SlotDescriptor* output_slot_desc);
63
64
54
    void set_timer(RuntimeProfile::Counter* merge_timer, RuntimeProfile::Counter* expr_timer) {
65
54
        _merge_timer = merge_timer;
66
54
        _expr_timer = expr_timer;
67
54
    }
68
69
    Status open(RuntimeState* state);
70
71
    // create/destroy AGG Data
72
    void create(AggregateDataPtr place);
73
    void destroy(AggregateDataPtr place);
74
75
    // agg_function
76
    Status execute_single_add(Block* block, AggregateDataPtr place, Arena& arena);
77
78
    Status execute_batch_add(Block* block, size_t offset, AggregateDataPtr* places, Arena& arena,
79
                             bool agg_many = false);
80
81
    Status execute_batch_add_selected(Block* block, size_t offset, AggregateDataPtr* places,
82
                                      Arena& arena);
83
84
    Status streaming_agg_serialize_to_column(Block* block, MutableColumnPtr& dst,
85
                                             const size_t num_rows, Arena& arena);
86
87
    void add_range_single_place(int64_t partition_start, int64_t partition_end, int64_t frame_start,
88
                                int64_t frame_end, AggregateDataPtr place, const IColumn** columns,
89
                                Arena& arena, UInt8* use_null_result,
90
                                UInt8* could_use_previous_result);
91
92
    void execute_function_with_incremental(int64_t partition_start, int64_t partition_end,
93
                                           int64_t frame_start, int64_t frame_end,
94
                                           AggregateDataPtr place, const IColumn** columns,
95
                                           Arena& arena, bool previous_is_nul, bool end_is_nul,
96
                                           bool has_null, UInt8* use_null_result,
97
                                           UInt8* could_use_previous_result);
98
99
    void insert_result_info(AggregateDataPtr place, IColumn* column);
100
101
    void insert_result_info_vec(const std::vector<AggregateDataPtr>& place, size_t offset,
102
                                IColumn* column, const size_t num_rows);
103
104
    void insert_result_info_range(ConstAggregateDataPtr place, IColumn* column, size_t start,
105
                                  size_t end);
106
107
    void reset(AggregateDataPtr place);
108
109
82
    DataTypePtr& data_type() { return _data_type; }
110
111
95
    String get_name() const { return _function->get_name(); }
112
14
    DataTypePtr get_return_type() const { return _function->get_return_type(); }
113
383
    size_t size_of_data() const { return _function->size_of_data(); }
114
55
    size_t align_of_data() const { return _function->align_of_data(); }
115
8
    bool result_column_could_resize() const { return _function->result_column_could_resize(); }
116
8
    bool supported_incremental_mode() const { return _function->supported_incremental_mode(); }
117
45
    bool is_simple_count() const { return _function->is_simple_count(); }
118
0
    void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena& arena) const {
119
0
        _function->merge(place, rhs, arena);
120
0
    }
121
    void serialize_to_column(const std::vector<AggregateDataPtr>& places, size_t offset,
122
203
                             MutableColumnPtr& dst, const size_t num_rows) const {
123
203
        _function->serialize_to_column(places, offset, dst, num_rows);
124
203
    }
125
26
    void serialize_without_key_to_column(ConstAggregateDataPtr place, IColumn& to) const {
126
26
        _function->serialize_without_key_to_column(place, to);
127
26
    }
128
    void deserialize_and_merge_from_column(AggregateDataPtr place, const IColumn& column,
129
25
                                           Arena& arena) const {
130
25
        _function->deserialize_and_merge_from_column(place, column, arena);
131
25
    }
132
    void deserialize_and_merge_from_column_range(AggregateDataPtr place, const IColumn& column,
133
24
                                                 size_t begin, size_t end, Arena& arena) const {
134
24
        _function->deserialize_and_merge_from_column_range(place, column, begin, end, arena);
135
24
    }
136
    void deserialize_and_merge_vec(const AggregateDataPtr* places, size_t offset,
137
                                   AggregateDataPtr rhs, const IColumn* column, Arena& arena,
138
53
                                   const size_t num_rows) const {
139
53
        _function->deserialize_and_merge_vec(places, offset, rhs, column, arena, num_rows);
140
53
    }
141
    void deserialize_and_merge_vec_selected(const AggregateDataPtr* places, size_t offset,
142
                                            AggregateDataPtr rhs, const IColumn* column,
143
25
                                            Arena& arena, const size_t num_rows) const {
144
25
        _function->deserialize_and_merge_vec_selected(places, offset, rhs, column, arena, num_rows);
145
25
    }
146
166
    MutableColumnPtr create_serialize_column() const {
147
166
        return _function->create_serialize_column();
148
166
    }
149
69
    DataTypePtr get_serialized_type() const { return _function->get_serialized_type(); }
150
24
    void set_query_context(QueryContext* context) { _function->set_query_context(context); }
151
152
    static std::string debug_string(const std::vector<AggFnEvaluator*>& exprs);
153
    std::string debug_string() const;
154
62
    bool is_merge() const { return _is_merge; }
155
7
    const VExprContextSPtrs& input_exprs_ctxs() const { return _input_exprs_ctxs; }
156
157
    static Status check_agg_fn_output(uint32_t key_size, const std::vector<AggFnEvaluator*>& agg_fn,
158
                                      const RowDescriptor& output_row_desc);
159
160
20
    void set_version(const int version) { _function->set_version(version); }
161
162
    AggFnEvaluator* clone(RuntimeState* state, ObjectPool* pool);
163
164
    bool is_blockable() const;
165
166
private:
167
    const TFunction _fn;
168
169
    const bool _is_merge;
170
    // We need this flag to distinguish between the two types of aggregation functions:
171
    // 1. executed without group by key (agg function used with window function is also regarded as this type)
172
    // 2. executed with group by key
173
    const bool _without_key;
174
175
    const bool _is_window_function;
176
177
    AggFnEvaluator(const TExprNode& desc, const bool without_key, const bool is_window_function);
178
    AggFnEvaluator(AggFnEvaluator& evaluator, RuntimeState* state);
179
180
#ifdef BE_TEST
181
    AggFnEvaluator(bool is_merge, bool without_key, const bool is_window_function)
182
98
            : _is_merge(is_merge),
183
98
              _without_key(without_key),
184
98
              _is_window_function(is_window_function) {};
185
#endif
186
    Status _calc_argument_columns(Block* block);
187
188
    DataTypes _argument_types_with_sort;
189
    DataTypes _real_argument_types;
190
191
    const SlotDescriptor* _intermediate_slot_desc = nullptr;
192
    const SlotDescriptor* _output_slot_desc = nullptr;
193
194
    RuntimeProfile::Counter* _merge_timer = nullptr;
195
    RuntimeProfile::Counter* _expr_timer = nullptr;
196
197
    // input context
198
    VExprContextSPtrs _input_exprs_ctxs;
199
200
    SortDescription _sort_description;
201
202
    DataTypePtr _data_type;
203
204
    AggregateFunctionPtr _function;
205
206
    std::string _expr_name;
207
208
    std::vector<const IColumn*> _agg_columns;
209
};
210
211
} // namespace doris