Coverage Report

Created: 2026-07-08 14: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/status.h"
27
#include "core/data_type/data_type.h"
28
#include "exec/sort/sort_description.h"
29
#include "exprs/aggregate/aggregate_function.h"
30
#include "exprs/vexpr_fwd.h"
31
#include "runtime/runtime_profile.h"
32
33
namespace doris {
34
35
class RuntimeState;
36
class SlotDescriptor;
37
class ObjectPool;
38
class RowDescriptor;
39
class TExpr;
40
class TExprNode;
41
class TSortInfo;
42
43
class Arena;
44
class Block;
45
class BufferWritable;
46
class IColumn;
47
class QueryContext;
48
49
class AggFnEvaluator {
50
public:
51
    ENABLE_FACTORY_CREATOR(AggFnEvaluator);
52
292k
    MOCK_DEFINE(virtual) ~AggFnEvaluator() = default;
53
54
public:
55
    static Status create(ObjectPool* pool, const TExpr& desc, const TSortInfo& sort_info,
56
                         const bool without_key, const bool is_window_function,
57
                         AggFnEvaluator** result);
58
59
    Status prepare(RuntimeState* state, const RowDescriptor& desc,
60
                   const SlotDescriptor* intermediate_slot_desc,
61
                   const SlotDescriptor* output_slot_desc);
62
63
192k
    void set_timer(RuntimeProfile::Counter* merge_timer, RuntimeProfile::Counter* expr_timer) {
64
192k
        _merge_timer = merge_timer;
65
192k
        _expr_timer = expr_timer;
66
192k
    }
67
68
    Status open(RuntimeState* state);
69
70
    // create/destroy AGG Data
71
    void create(AggregateDataPtr place);
72
    void destroy(AggregateDataPtr place);
73
74
    // agg_function
75
    Status execute_single_add(Block* block, AggregateDataPtr place, Arena& arena);
76
77
    Status execute_batch_add(Block* block, size_t offset, AggregateDataPtr* places, Arena& arena,
78
                             bool agg_many = false);
79
80
    Status execute_batch_add_selected(Block* block, size_t offset, AggregateDataPtr* places,
81
                                      Arena& arena);
82
83
    Status streaming_agg_serialize_to_column(Block* block, MutableColumnPtr& dst,
84
                                             const size_t num_rows, Arena& arena);
85
86
    void add_range_single_place(int64_t partition_start, int64_t partition_end, int64_t frame_start,
87
                                int64_t frame_end, AggregateDataPtr place, const IColumn** columns,
88
                                Arena& arena, UInt8* use_null_result,
89
                                UInt8* could_use_previous_result);
90
91
    void execute_function_with_incremental(int64_t partition_start, int64_t partition_end,
92
                                           int64_t frame_start, int64_t frame_end,
93
                                           AggregateDataPtr place, const IColumn** columns,
94
                                           Arena& arena, bool previous_is_nul, bool end_is_nul,
95
                                           bool has_null, UInt8* use_null_result,
96
                                           UInt8* could_use_previous_result);
97
98
    void insert_result_info(AggregateDataPtr place, IColumn* column);
99
100
    void insert_result_info_vec(const std::vector<AggregateDataPtr>& place, size_t offset,
101
                                IColumn* column, const size_t num_rows);
102
103
    void insert_result_info_range(ConstAggregateDataPtr place, IColumn* column, size_t start,
104
                                  size_t end);
105
106
    void reset(AggregateDataPtr place);
107
108
14.4k
    DataTypePtr& data_type() { return _data_type; }
109
110
8.65k
    String get_name() const { return _function->get_name(); }
111
151k
    DataTypePtr get_return_type() const { return _function->get_return_type(); }
112
108k
    size_t size_of_data() const { return _function->size_of_data(); }
113
146k
    size_t align_of_data() const { return _function->align_of_data(); }
114
8.59k
    bool result_column_could_resize() const { return _function->result_column_could_resize(); }
115
8.58k
    bool supported_incremental_mode() const { return _function->supported_incremental_mode(); }
116
27.9k
    bool is_simple_count() const { return _function->is_simple_count(); }
117
2.79k
    void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena& arena) const {
118
2.79k
        _function->merge(place, rhs, arena);
119
2.79k
    }
120
    void serialize_to_column(const std::vector<AggregateDataPtr>& places, size_t offset,
121
27.2k
                             MutableColumnPtr& dst, const size_t num_rows) const {
122
27.2k
        _function->serialize_to_column(places, offset, dst, num_rows);
123
27.2k
    }
124
15.1k
    void serialize_without_key_to_column(ConstAggregateDataPtr place, IColumn& to) const {
125
15.1k
        _function->serialize_without_key_to_column(place, to);
126
15.1k
    }
127
    void deserialize_and_merge_from_column(AggregateDataPtr place, const IColumn& column,
128
16.1k
                                           Arena& arena) const {
129
16.1k
        _function->deserialize_and_merge_from_column(place, column, arena);
130
16.1k
    }
131
    void deserialize_and_merge_from_column_range(AggregateDataPtr place, const IColumn& column,
132
                                                 size_t begin, size_t end, Arena& arena) const {
133
        _function->deserialize_and_merge_from_column_range(place, column, begin, end, arena);
134
    }
135
    void deserialize_and_merge_vec(const AggregateDataPtr* places, size_t offset,
136
                                   AggregateDataPtr rhs, const IColumn* column, Arena& arena,
137
17.8k
                                   const size_t num_rows) const {
138
17.8k
        _function->deserialize_and_merge_vec(places, offset, rhs, column, arena, num_rows);
139
17.8k
    }
140
    void deserialize_and_merge_vec_selected(const AggregateDataPtr* places, size_t offset,
141
                                            AggregateDataPtr rhs, const IColumn* column,
142
25
                                            Arena& arena, const size_t num_rows) const {
143
25
        _function->deserialize_and_merge_vec_selected(places, offset, rhs, column, arena, num_rows);
144
25
    }
145
43.4k
    MutableColumnPtr create_serialize_column() const {
146
43.4k
        return _function->create_serialize_column();
147
43.4k
    }
148
43.3k
    DataTypePtr get_serialized_type() const { return _function->get_serialized_type(); }
149
    void set_query_context(QueryContext* context) { _function->set_query_context(context); }
150
151
    static std::string debug_string(const std::vector<AggFnEvaluator*>& exprs);
152
    std::string debug_string() const;
153
121k
    bool is_merge() const { return _is_merge; }
154
34.7k
    const VExprContextSPtrs& input_exprs_ctxs() const { return _input_exprs_ctxs; }
155
156
    static Status check_agg_fn_output(uint32_t key_size, const std::vector<AggFnEvaluator*>& agg_fn,
157
                                      const RowDescriptor& output_row_desc);
158
159
90.4k
    void set_version(const int version) { _function->set_version(version); }
160
161
    AggFnEvaluator* clone(RuntimeState* state, ObjectPool* pool);
162
163
    bool is_blockable() const;
164
165
private:
166
    const TFunction _fn;
167
168
    const bool _is_merge;
169
    // We need this flag to distinguish between the two types of aggregation functions:
170
    // 1. executed without group by key (agg function used with window function is also regarded as this type)
171
    // 2. executed with group by key
172
    const bool _without_key;
173
174
    const bool _is_window_function;
175
176
    AggFnEvaluator(const TExprNode& desc, const bool without_key, const bool is_window_function);
177
    AggFnEvaluator(AggFnEvaluator& evaluator, RuntimeState* state);
178
179
#ifdef BE_TEST
180
    AggFnEvaluator(bool is_merge, bool without_key, const bool is_window_function)
181
            : _is_merge(is_merge),
182
              _without_key(without_key),
183
              _is_window_function(is_window_function) {};
184
#endif
185
    Status _calc_argument_columns(Block* block);
186
187
    DataTypes _argument_types_with_sort;
188
    DataTypes _real_argument_types;
189
190
    const SlotDescriptor* _intermediate_slot_desc = nullptr;
191
    const SlotDescriptor* _output_slot_desc = nullptr;
192
193
    RuntimeProfile::Counter* _merge_timer = nullptr;
194
    RuntimeProfile::Counter* _expr_timer = nullptr;
195
196
    // input context
197
    VExprContextSPtrs _input_exprs_ctxs;
198
199
    SortDescription _sort_description;
200
201
    DataTypePtr _data_type;
202
203
    AggregateFunctionPtr _function;
204
205
    std::string _expr_name;
206
207
    std::vector<const IColumn*> _agg_columns;
208
};
209
210
} // namespace doris