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 | 0 | void merge(const QuantileReservoirSampler& rhs) { |
41 | 0 | level = rhs.level; |
42 | 0 | data.merge(rhs.data); |
43 | 0 | } |
44 | | |
45 | 0 | void reset() { |
46 | 0 | level = 0.0; |
47 | 0 | data.clear(); |
48 | 0 | } |
49 | | |
50 | 0 | void serialize(BufferWritable& buf) const { |
51 | 0 | buf.write_binary(level); |
52 | 0 | data.write(buf); |
53 | 0 | } |
54 | | |
55 | 0 | void deserialize(BufferReadable& buf) { |
56 | 0 | buf.read_binary(level); |
57 | 0 | data.read(buf); |
58 | 0 | } |
59 | | |
60 | 0 | double get() const { |
61 | | // The caller is a ConstAggregateDataPtr, but it itself is an AggregateDataPtr. |
62 | | // To call a non-const method here, a const_cast is required. |
63 | 0 | return const_cast<ReservoirSampler&>(data).quantileInterpolated(this->level); |
64 | 0 | } |
65 | | |
66 | | private: |
67 | | double level = 0.0; |
68 | | ReservoirSampler data; |
69 | | }; |
70 | | |
71 | | template <typename Data> |
72 | | class AggregateFunctionPercentileReservoir final |
73 | | : public IAggregateFunctionDataHelper<Data, AggregateFunctionPercentileReservoir<Data>>, |
74 | | MultiExpression, |
75 | | NullableAggregateFunction { |
76 | | public: |
77 | | AggregateFunctionPercentileReservoir(const DataTypes& argument_types_) |
78 | 0 | : IAggregateFunctionDataHelper<Data, AggregateFunctionPercentileReservoir<Data>>( |
79 | 0 | argument_types_) {} |
80 | | |
81 | 0 | String get_name() const override { return "percentile_reservoir"; } |
82 | | |
83 | 0 | DataTypePtr get_return_type() const override { return std::make_shared<DataTypeFloat64>(); } |
84 | | |
85 | | void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num, |
86 | 0 | Arena&) const override { |
87 | 0 | auto value = assert_cast<const ColumnFloat64&>(*columns[0]).get_data()[row_num]; |
88 | 0 | auto level = assert_cast<const ColumnFloat64&>(*columns[1]).get_data()[0]; |
89 | 0 | this->data(place).add(value, level); |
90 | 0 | } |
91 | | |
92 | 0 | void reset(AggregateDataPtr place) const override { this->data(place).reset(); } |
93 | | |
94 | | void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, |
95 | 0 | Arena&) const override { |
96 | 0 | this->data(place).merge(this->data(rhs)); |
97 | 0 | } |
98 | | |
99 | 0 | void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { |
100 | 0 | this->data(place).serialize(buf); |
101 | 0 | } |
102 | | |
103 | | void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, |
104 | 0 | Arena&) const override { |
105 | 0 | this->data(place).deserialize(buf); |
106 | 0 | } |
107 | | |
108 | 0 | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { |
109 | 0 | assert_cast<ColumnFloat64&>(to).get_data().push_back(this->data(place).get()); |
110 | 0 | } |
111 | | }; |
112 | | |
113 | | } // namespace doris |