Coverage Report

Created: 2026-03-16 17:53

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 "core/assert_cast.h"
31
#include "core/column/column.h"
32
#include "core/column/column_array.h"
33
#include "core/column/column_nullable.h"
34
#include "core/column/column_vector.h"
35
#include "core/data_type/data_type_array.h"
36
#include "core/data_type/data_type_nullable.h"
37
#include "core/data_type/data_type_number.h"
38
#include "core/types.h"
39
#include "exprs/aggregate/aggregate_function.h"
40
#include "util/var_int.h"
41
42
namespace doris {
43
#include "common/compile_check_begin.h"
44
class Arena;
45
class BufferReadable;
46
class BufferWritable;
47
} // namespace doris
48
49
namespace doris {
50
struct RetentionState {
51
    static constexpr size_t MAX_EVENTS = 32;
52
    uint8_t events[MAX_EVENTS] = {0};
53
54
8
    RetentionState() = default;
55
56
0
    void reset() {
57
0
        for (int64_t i = 0; i < MAX_EVENTS; i++) {
58
0
            events[i] = 0;
59
0
        }
60
0
    }
61
62
8
    void set(int event) { events[event] = 1; }
63
64
3
    void merge(const RetentionState& other) {
65
99
        for (int64_t i = 0; i < MAX_EVENTS; i++) {
66
96
            events[i] |= other.events[i];
67
96
        }
68
3
    }
69
70
2
    void write(BufferWritable& out) const {
71
2
        int64_t serialized_events = 0;
72
66
        for (int64_t i = 0; i < MAX_EVENTS; i++) {
73
64
            serialized_events |= events[i];
74
64
            serialized_events <<= 1;
75
64
        }
76
2
        write_var_int(serialized_events, out);
77
2
    }
78
79
2
    void read(BufferReadable& in) {
80
2
        int64_t serialized_events = 0;
81
2
        uint64_t u_serialized_events = 0;
82
2
        read_var_int(serialized_events, in);
83
2
        u_serialized_events = serialized_events;
84
85
2
        u_serialized_events >>= 1;
86
66
        for (int64_t i = MAX_EVENTS - 1; i >= 0; i--) {
87
64
            events[i] = (uint8_t)(1 & u_serialized_events);
88
64
            u_serialized_events >>= 1;
89
64
        }
90
2
    }
91
92
6
    void insert_result_into(IColumn& to, size_t events_size, const uint8_t* arg_events) const {
93
6
        auto& data_to = assert_cast<ColumnUInt8&>(to).get_data();
94
95
6
        ColumnArray::Offset64 current_offset = data_to.size();
96
6
        data_to.resize(current_offset + events_size);
97
98
6
        bool first_flag = arg_events[0];
99
6
        data_to[current_offset] = first_flag;
100
6
        ++current_offset;
101
102
18
        for (size_t i = 1; i < events_size; ++i) {
103
12
            data_to[current_offset] = (first_flag && arg_events[i]);
104
12
            ++current_offset;
105
12
        }
106
6
    }
107
};
108
109
class AggregateFunctionRetention
110
        : public IAggregateFunctionDataHelper<RetentionState, AggregateFunctionRetention>,
111
          VarargsExpression,
112
          NullableAggregateFunction {
113
public:
114
    AggregateFunctionRetention(const DataTypes& argument_types_)
115
4
            : IAggregateFunctionDataHelper<RetentionState, AggregateFunctionRetention>(
116
4
                      argument_types_) {}
117
118
0
    String get_name() const override { return "retention"; }
119
120
0
    DataTypePtr get_return_type() const override {
121
0
        return std::make_shared<DataTypeArray>(make_nullable(std::make_shared<DataTypeUInt8>()));
122
0
    }
123
124
0
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
125
    void add(AggregateDataPtr __restrict place, const IColumn** columns, const ssize_t row_num,
126
12
             Arena&) const override {
127
48
        for (int i = 0; i < get_argument_types().size(); i++) {
128
36
            auto event = assert_cast<const ColumnUInt8*, TypeCheckOnRelease::DISABLE>(columns[i])
129
36
                                 ->get_data()[row_num];
130
36
            if (event) {
131
8
                this->data(place).set(i);
132
8
            }
133
36
        }
134
12
    }
135
136
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
137
3
               Arena&) const override {
138
3
        this->data(place).merge(this->data(rhs));
139
3
    }
140
141
2
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
142
2
        this->data(place).write(buf);
143
2
    }
144
145
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
146
2
                     Arena&) const override {
147
2
        this->data(place).read(buf);
148
2
    }
149
150
6
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
151
6
        auto& to_arr = assert_cast<ColumnArray&>(to);
152
6
        auto& to_nested_col = to_arr.get_data();
153
6
        if (to_nested_col.is_nullable()) {
154
0
            auto col_null = reinterpret_cast<ColumnNullable*>(&to_nested_col);
155
0
            this->data(place).insert_result_into(col_null->get_nested_column(),
156
0
                                                 get_argument_types().size(),
157
0
                                                 this->data(place).events);
158
0
            col_null->get_null_map_data().resize_fill(col_null->get_nested_column().size(), 0);
159
6
        } else {
160
6
            this->data(place).insert_result_into(to_nested_col, get_argument_types().size(),
161
6
                                                 this->data(place).events);
162
6
        }
163
6
        to_arr.get_offsets().push_back(to_nested_col.size());
164
6
    }
165
};
166
} // namespace doris
167
#include "common/compile_check_end.h"