Coverage Report

Created: 2026-09-02 21:35

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
183
    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 insert_result_info_repeat_vec(const std::vector<AggregateDataPtr>& places, size_t offset,
108
                                       const std::vector<uint64_t>& repeats, IColumn* column,
109
                                       const size_t num_rows, Arena& arena);
110
111
    void reset(AggregateDataPtr place);
112
113
91
    DataTypePtr& data_type() { return _data_type; }
114
115
96
    String get_name() const { return _function->get_name(); }
116
15
    DataTypePtr get_return_type() const { return _function->get_return_type(); }
117
383
    size_t size_of_data() const { return _function->size_of_data(); }
118
55
    size_t align_of_data() const { return _function->align_of_data(); }
119
9
    bool result_column_could_resize() const { return _function->result_column_could_resize(); }
120
9
    bool supported_incremental_mode() const { return _function->supported_incremental_mode(); }
121
45
    bool is_simple_count() const { return _function->is_simple_count(); }
122
0
    void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena& arena) const {
123
0
        _function->merge(place, rhs, arena);
124
0
    }
125
    void serialize_to_column(const std::vector<AggregateDataPtr>& places, size_t offset,
126
203
                             MutableColumnPtr& dst, const size_t num_rows) const {
127
203
        _function->serialize_to_column(places, offset, dst, num_rows);
128
203
    }
129
26
    void serialize_without_key_to_column(ConstAggregateDataPtr place, IColumn& to) const {
130
26
        _function->serialize_without_key_to_column(place, to);
131
26
    }
132
    void deserialize_and_merge_from_column(AggregateDataPtr place, const IColumn& column,
133
25
                                           Arena& arena) const {
134
25
        _function->deserialize_and_merge_from_column(place, column, arena);
135
25
    }
136
    void deserialize_and_merge_from_column_range(AggregateDataPtr place, const IColumn& column,
137
24
                                                 size_t begin, size_t end, Arena& arena) const {
138
24
        _function->deserialize_and_merge_from_column_range(place, column, begin, end, arena);
139
24
    }
140
    void deserialize_and_merge_vec(const AggregateDataPtr* places, size_t offset,
141
                                   AggregateDataPtr rhs, const IColumn* column, Arena& arena,
142
53
                                   const size_t num_rows) const {
143
53
        _function->deserialize_and_merge_vec(places, offset, rhs, column, arena, num_rows);
144
53
    }
145
    void deserialize_and_merge_vec_selected(const AggregateDataPtr* places, size_t offset,
146
                                            AggregateDataPtr rhs, const IColumn* column,
147
25
                                            Arena& arena, const size_t num_rows) const {
148
25
        _function->deserialize_and_merge_vec_selected(places, offset, rhs, column, arena, num_rows);
149
25
    }
150
166
    MutableColumnPtr create_serialize_column() const {
151
166
        return _function->create_serialize_column();
152
166
    }
153
69
    DataTypePtr get_serialized_type() const { return _function->get_serialized_type(); }
154
24
    void set_query_context(QueryContext* context) { _function->set_query_context(context); }
155
156
    static std::string debug_string(const std::vector<AggFnEvaluator*>& exprs);
157
    std::string debug_string() const;
158
62
    bool is_merge() const { return _is_merge; }
159
7
    const VExprContextSPtrs& input_exprs_ctxs() const { return _input_exprs_ctxs; }
160
161
    static Status check_agg_fn_output(uint32_t key_size, const std::vector<AggFnEvaluator*>& agg_fn,
162
                                      const RowDescriptor& output_row_desc);
163
164
20
    void set_version(const int version) { _function->set_version(version); }
165
166
    AggFnEvaluator* clone(RuntimeState* state, ObjectPool* pool);
167
168
    bool is_blockable() const;
169
170
private:
171
    const TFunction _fn;
172
173
    const bool _is_merge;
174
    // We need this flag to distinguish between the two types of aggregation functions:
175
    // 1. executed without group by key (agg function used with window function is also regarded as this type)
176
    // 2. executed with group by key
177
    const bool _without_key;
178
179
    const bool _is_window_function;
180
181
    AggFnEvaluator(const TExprNode& desc, const bool without_key, const bool is_window_function);
182
    AggFnEvaluator(AggFnEvaluator& evaluator, RuntimeState* state);
183
184
#ifdef BE_TEST
185
    AggFnEvaluator(bool is_merge, bool without_key, const bool is_window_function)
186
99
            : _is_merge(is_merge),
187
99
              _without_key(without_key),
188
99
              _is_window_function(is_window_function) {};
189
#endif
190
    Status _calc_argument_columns(Block* block);
191
192
    DataTypes _argument_types_with_sort;
193
    DataTypes _real_argument_types;
194
195
    const SlotDescriptor* _intermediate_slot_desc = nullptr;
196
    const SlotDescriptor* _output_slot_desc = nullptr;
197
198
    RuntimeProfile::Counter* _merge_timer = nullptr;
199
    RuntimeProfile::Counter* _expr_timer = nullptr;
200
201
    // input context
202
    VExprContextSPtrs _input_exprs_ctxs;
203
204
    SortDescription _sort_description;
205
206
    DataTypePtr _data_type;
207
208
    AggregateFunctionPtr _function;
209
210
    std::string _expr_name;
211
212
    std::vector<const IColumn*> _agg_columns;
213
};
214
215
} // namespace doris