Coverage Report

Created: 2026-09-18 03:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/aggregate/aggregate_function_percentile_reservoir.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
20
#include <glog/logging.h>
21
22
#include <memory>
23
24
#include "common/exception.h"
25
#include "core/column/column_vector.h"
26
#include "core/data_type/data_type_number.h"
27
#include "core/data_type/primitive_type.h"
28
#include "exprs/aggregate/aggregate_function.h"
29
#include "util/reservoir_sampler.h"
30
31
namespace doris {
32
33
class Arena;
34
class BufferReadable;
35
36
struct QuantileReservoirSampler {
37
382
    void add(const double x, const double input_level) {
38
382
        this->level = input_level;
39
382
        data.insert(x);
40
382
    }
41
42
2
    void add_batch(const double* values, size_t size, const double input_level) {
43
2
        this->level = input_level;
44
2
        data.insert_many(values, size);
45
2
    }
46
47
174
    void merge(const QuantileReservoirSampler& rhs) {
48
174
        if (rhs.data.empty()) {
49
113
            return;
50
113
        }
51
61
        if (data.empty()) {
52
33
            level = rhs.level;
53
33
        } else if (UNLIKELY(level != rhs.level)) {
54
8
            throw Exception(ErrorCode::INVALID_ARGUMENT,
55
8
                            "percentile_reservoir aggregate states have incompatible quantiles");
56
8
        }
57
53
        data.merge(rhs.data);
58
53
    }
59
60
10
    void reset() {
61
10
        level = 0.0;
62
10
        data.clear();
63
10
    }
64
65
77
    void serialize(BufferWritable& buf) const {
66
77
        buf.write_binary(level);
67
77
        data.write(buf);
68
77
    }
69
70
81
    void deserialize(BufferReadable& buf) {
71
81
        buf.read_binary(level);
72
81
        data.read(buf);
73
81
    }
74
75
258
    double get() const {
76
        // The caller is a ConstAggregateDataPtr, but it itself is an AggregateDataPtr.
77
        // To call a non-const method here, a const_cast is required.
78
258
        return const_cast<ReservoirSampler&>(data).quantileInterpolated(this->level);
79
258
    }
80
81
private:
82
    double level = 0.0;
83
    ReservoirSampler data;
84
};
85
86
template <typename Data>
87
class AggregateFunctionPercentileReservoir final
88
        : public IAggregateFunctionDataHelper<Data, AggregateFunctionPercentileReservoir<Data>>,
89
          MultiExpression,
90
          NullableAggregateFunction {
91
public:
92
    AggregateFunctionPercentileReservoir(const DataTypes& argument_types_)
93
11
            : IAggregateFunctionDataHelper<Data, AggregateFunctionPercentileReservoir<Data>>(
94
11
                      argument_types_) {}
95
96
0
    String get_name() const override { return "percentile_reservoir"; }
97
98
256
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeFloat64>(); }
99
100
    void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num,
101
382
             Arena&) const override {
102
382
        auto value = assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[0])
103
382
                             .get_data()[row_num];
104
382
        auto level = assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[1])
105
382
                             .get_data()[0];
106
382
        this->data(place).add(value, level);
107
382
    }
108
109
0
    void check_input_columns_type(const IColumn** columns) const override {
110
0
        this->template check_argument_column_type<ColumnFloat64>(columns[0]);
111
0
        this->template check_argument_column_type<ColumnFloat64>(columns[1]);
112
0
    }
113
114
    void add_batch_single_place(size_t batch_size, AggregateDataPtr place, const IColumn** columns,
115
1
                                Arena&) const override {
116
1
        const auto& sources =
117
1
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[0]);
118
1
        const auto& levels =
119
1
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[1]);
120
1
        this->data(place).add_batch(sources.get_data().data(), batch_size, levels.get_data()[0]);
121
1
    }
122
123
    void add_range_single_place(int64_t partition_start, int64_t partition_end, int64_t frame_start,
124
                                int64_t frame_end, AggregateDataPtr place, const IColumn** columns,
125
                                Arena&, UInt8* use_null_result,
126
2
                                UInt8* could_use_previous_result) const override {
127
2
        frame_start = std::max<int64_t>(frame_start, partition_start);
128
2
        frame_end = std::min<int64_t>(frame_end, partition_end);
129
2
        if (frame_start < frame_end) {
130
1
            const auto& sources =
131
1
                    assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[0]);
132
1
            const auto& levels =
133
1
                    assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[1]);
134
1
            this->data(place).add_batch(sources.get_data().data() + frame_start,
135
1
                                        frame_end - frame_start, levels.get_data()[0]);
136
1
            *use_null_result = false;
137
1
            *could_use_previous_result = true;
138
1
        } else if (!*could_use_previous_result) {
139
1
            *use_null_result = true;
140
1
        }
141
2
    }
142
143
10
    void reset(AggregateDataPtr place) const override { this->data(place).reset(); }
144
145
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
146
174
               Arena&) const override {
147
174
        this->data(place).merge(this->data(rhs));
148
174
    }
149
150
77
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
151
77
        this->data(place).serialize(buf);
152
77
    }
153
154
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
155
81
                     Arena&) const override {
156
81
        this->data(place).deserialize(buf);
157
81
    }
158
159
258
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
160
258
        assert_cast<ColumnFloat64&, TypeCheckOnRelease::DISABLE>(to).get_data().push_back(
161
258
                this->data(place).get());
162
258
    }
163
};
164
165
} // namespace doris