Coverage Report

Created: 2026-06-24 08:21

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