be/src/exprs/aggregate/aggregate_function_avg_weighted.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 <stddef.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <boost/iterator/iterator_facade.hpp> |
24 | | #include <cmath> |
25 | | #include <memory> |
26 | | #include <type_traits> |
27 | | |
28 | | #include "common/status.h" |
29 | | #include "core/assert_cast.h" |
30 | | #include "core/binary_cast.hpp" |
31 | | #include "core/column/column_vector.h" |
32 | | #include "core/data_type/data_type_number.h" |
33 | | #include "core/types.h" |
34 | | #include "core/value/decimalv2_value.h" |
35 | | #include "exprs/aggregate/aggregate_function.h" |
36 | | #include "util/io_helper.h" |
37 | | |
38 | | namespace doris { |
39 | | #include "common/compile_check_begin.h" |
40 | | class Arena; |
41 | | class BufferReadable; |
42 | | class BufferWritable; |
43 | | class IColumn; |
44 | | template <PrimitiveType T> |
45 | | class ColumnDecimal; |
46 | | |
47 | | template <PrimitiveType T> |
48 | | struct AggregateFunctionAvgWeightedData { |
49 | | using DataType = typename PrimitiveTypeTraits<T>::CppType; |
50 | 0 | void add(const DataType& data_val, double weight_val) { |
51 | 0 | #ifdef __clang__ |
52 | 0 | #pragma clang fp reassociate(on) |
53 | 0 | #endif |
54 | 0 | data_sum = data_sum + (double(data_val) * weight_val); |
55 | 0 | weight_sum = weight_sum + weight_val; |
56 | 0 | } |
57 | | |
58 | 0 | void write(BufferWritable& buf) const { |
59 | 0 | buf.write_binary(data_sum); |
60 | 0 | buf.write_binary(weight_sum); |
61 | 0 | } |
62 | | |
63 | 0 | void read(BufferReadable& buf) { |
64 | 0 | buf.read_binary(data_sum); |
65 | 0 | buf.read_binary(weight_sum); |
66 | 0 | } |
67 | | |
68 | 0 | void merge(const AggregateFunctionAvgWeightedData& rhs) { |
69 | 0 | #ifdef __clang__ |
70 | 0 | #pragma clang fp reassociate(on) |
71 | 0 | #endif |
72 | 0 | data_sum = data_sum + rhs.data_sum; |
73 | 0 | weight_sum = weight_sum + rhs.weight_sum; |
74 | 0 | } |
75 | | |
76 | 0 | void reset() { |
77 | 0 | data_sum = 0.0; |
78 | 0 | weight_sum = 0.0; |
79 | 0 | } |
80 | | |
81 | 0 | double get() const { return data_sum / weight_sum; } |
82 | | |
83 | | double data_sum = 0.0; |
84 | | double weight_sum = 0.0; |
85 | | }; |
86 | | |
87 | | template <PrimitiveType type> |
88 | | class AggregateFunctionAvgWeight final |
89 | | : public IAggregateFunctionDataHelper<AggregateFunctionAvgWeightedData<type>, |
90 | | AggregateFunctionAvgWeight<type>>, |
91 | | MultiExpression, |
92 | | NullableAggregateFunction { |
93 | | public: |
94 | | using T = typename PrimitiveTypeTraits<type>::CppType; |
95 | | using ColVecType = typename PrimitiveTypeTraits<type>::ColumnType; |
96 | | |
97 | | AggregateFunctionAvgWeight(const DataTypes& argument_types_) |
98 | 0 | : IAggregateFunctionDataHelper<AggregateFunctionAvgWeightedData<type>, |
99 | 0 | AggregateFunctionAvgWeight<type>>(argument_types_) {} |
100 | | |
101 | 0 | String get_name() const override { return "avg_weighted"; } |
102 | | |
103 | 0 | DataTypePtr get_return_type() const override { return std::make_shared<DataTypeFloat64>(); } |
104 | | |
105 | | void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num, |
106 | 0 | Arena&) const override { |
107 | 0 | const auto& column = |
108 | 0 | assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(*columns[0]); |
109 | 0 | const auto& weight = |
110 | 0 | assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[1]); |
111 | 0 | this->data(place).add(column.get_data()[row_num], weight.get_element(row_num)); |
112 | 0 | } |
113 | | |
114 | 0 | void reset(AggregateDataPtr place) const override { this->data(place).reset(); } |
115 | | |
116 | | void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, |
117 | 0 | Arena&) const override { |
118 | 0 | this->data(place).merge(this->data(rhs)); |
119 | 0 | } |
120 | | |
121 | 0 | void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { |
122 | 0 | this->data(place).write(buf); |
123 | 0 | } |
124 | | |
125 | | void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, |
126 | 0 | Arena&) const override { |
127 | 0 | this->data(place).read(buf); |
128 | 0 | } |
129 | | |
130 | 0 | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { |
131 | 0 | auto& column = assert_cast<ColumnFloat64&>(to); |
132 | 0 | column.get_data().push_back(this->data(place).get()); |
133 | 0 | } |
134 | | }; |
135 | | |
136 | | } // namespace doris |
137 | | |
138 | | #include "common/compile_check_end.h" |