Coverage Report

Created: 2026-07-25 15:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/aggregate/aggregate_function_retention.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
// This file is copied from
19
// https://github.com/ClickHouse/ClickHouse/blob/master/AggregateFunctionRetention.h
20
// and modified by Doris
21
22
#pragma once
23
24
#include <stddef.h>
25
#include <stdint.h>
26
27
#include <boost/iterator/iterator_facade.hpp>
28
#include <memory>
29
30
#include "common/exception.h"
31
#include "common/status.h"
32
#include "core/assert_cast.h"
33
#include "core/column/column.h"
34
#include "core/column/column_array.h"
35
#include "core/column/column_nullable.h"
36
#include "core/column/column_vector.h"
37
#include "core/data_type/data_type_array.h"
38
#include "core/data_type/data_type_nullable.h"
39
#include "core/data_type/data_type_number.h"
40
#include "core/types.h"
41
#include "exprs/aggregate/aggregate_function.h"
42
#include "util/var_int.h"
43
44
namespace doris {
45
class Arena;
46
class BufferReadable;
47
class BufferWritable;
48
} // namespace doris
49
50
namespace doris {
51
struct RetentionState {
52
    static constexpr size_t MAX_EVENTS = 32;
53
    uint8_t events[MAX_EVENTS] = {0};
54
55
16
    RetentionState() = default;
56
57
0
    void reset() {
58
0
        for (int64_t i = 0; i < MAX_EVENTS; i++) {
59
0
            events[i] = 0;
60
0
        }
61
0
    }
62
63
16
    void set(int event) { events[event] = 1; }
64
65
6
    void merge(const RetentionState& other) {
66
198
        for (int64_t i = 0; i < MAX_EVENTS; i++) {
67
192
            events[i] |= other.events[i];
68
192
        }
69
6
    }
70
71
4
    void write(BufferWritable& out) const {
72
4
        int64_t serialized_events = 0;
73
132
        for (int64_t i = 0; i < MAX_EVENTS; i++) {
74
128
            serialized_events |= events[i];
75
128
            serialized_events <<= 1;
76
128
        }
77
4
        write_var_int(serialized_events, out);
78
4
    }
79
80
4
    void read(BufferReadable& in) {
81
4
        int64_t serialized_events = 0;
82
4
        uint64_t u_serialized_events = 0;
83
4
        read_var_int(serialized_events, in);
84
4
        u_serialized_events = serialized_events;
85
86
4
        u_serialized_events >>= 1;
87
132
        for (int64_t i = MAX_EVENTS - 1; i >= 0; i--) {
88
128
            events[i] = (uint8_t)(1 & u_serialized_events);
89
128
            u_serialized_events >>= 1;
90
128
        }
91
4
    }
92
93
12
    void insert_result_into(IColumn& to, size_t events_size, const uint8_t* arg_events) const {
94
12
        auto& data_to = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(to).get_data();
95
96
12
        ColumnArray::Offset64 current_offset = data_to.size();
97
12
        data_to.resize(current_offset + events_size);
98
99
12
        bool first_flag = arg_events[0];
100
12
        data_to[current_offset] = first_flag;
101
12
        ++current_offset;
102
103
36
        for (size_t i = 1; i < events_size; ++i) {
104
24
            data_to[current_offset] = (first_flag && arg_events[i]);
105
24
            ++current_offset;
106
24
        }
107
12
    }
108
};
109
110
class AggregateFunctionRetention final
111
        : public IAggregateFunctionDataHelper<RetentionState, AggregateFunctionRetention>,
112
          VarargsExpression,
113
          NullableAggregateFunction {
114
public:
115
    AggregateFunctionRetention(const DataTypes& argument_types_)
116
14
            : IAggregateFunctionDataHelper<RetentionState, AggregateFunctionRetention>(
117
14
                      argument_types_) {
118
        // RetentionState only has room for MAX_EVENTS(32) events (fixed-size events[] array,
119
        // plus an int64 serialized bitmap). More params would overflow events[] in add()/
120
        // insert_result_into() and corrupt the heap, so reject it at construction time.
121
14
        if (argument_types_.size() > RetentionState::MAX_EVENTS) {
122
2
            throw Exception(ErrorCode::INVALID_ARGUMENT,
123
2
                            "retention function can accept at most {} params, but got {}",
124
2
                            RetentionState::MAX_EVENTS, argument_types_.size());
125
2
        }
126
14
    }
127
128
0
    String get_name() const override { return "retention"; }
129
130
12
    DataTypePtr get_return_type() const override {
131
12
        return std::make_shared<DataTypeArray>(make_nullable(std::make_shared<DataTypeUInt8>()));
132
12
    }
133
134
0
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
135
    void add(AggregateDataPtr __restrict place, const IColumn** columns, const ssize_t row_num,
136
24
             Arena&) const override {
137
96
        for (int i = 0; i < get_argument_types().size(); i++) {
138
72
            auto event = assert_cast<const ColumnUInt8*, TypeCheckOnRelease::DISABLE>(columns[i])
139
72
                                 ->get_data()[row_num];
140
72
            if (event) {
141
16
                this->data(place).set(i);
142
16
            }
143
72
        }
144
24
    }
145
146
0
    void check_input_columns_type(const IColumn** columns) const override {
147
0
        for (size_t i = 0; i < get_argument_types().size(); ++i) {
148
0
            this->template check_argument_column_type<ColumnUInt8>(columns[i]);
149
0
        }
150
0
    }
151
152
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
153
6
               Arena&) const override {
154
6
        this->data(place).merge(this->data(rhs));
155
6
    }
156
157
4
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
158
4
        this->data(place).write(buf);
159
4
    }
160
161
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
162
4
                     Arena&) const override {
163
4
        this->data(place).read(buf);
164
4
    }
165
166
12
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
167
12
        auto& to_arr = assert_cast<ColumnArray&, TypeCheckOnRelease::DISABLE>(to);
168
12
        auto& to_nested_col = to_arr.get_data();
169
12
        if (is_column_nullable(to_nested_col)) {
170
12
            auto col_null = reinterpret_cast<ColumnNullable*>(&to_nested_col);
171
12
            this->data(place).insert_result_into(col_null->get_nested_column(),
172
12
                                                 get_argument_types().size(),
173
12
                                                 this->data(place).events);
174
12
            col_null->get_null_map_data().resize_fill(col_null->get_nested_column().size(), 0);
175
12
        } else {
176
0
            this->data(place).insert_result_into(to_nested_col, get_argument_types().size(),
177
0
                                                 this->data(place).events);
178
0
        }
179
12
        to_arr.get_offsets().push_back(to_nested_col.size());
180
12
    }
181
};
182
} // namespace doris