Coverage Report

Created: 2026-04-16 21:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
37
namespace doris {
38
class Arena;
39
class BufferReadable;
40
class BufferWritable;
41
class IColumn;
42
template <PrimitiveType T>
43
class ColumnDecimal;
44
45
template <PrimitiveType T>
46
struct AggregateFunctionAvgWeightedData {
47
    using DataType = typename PrimitiveTypeTraits<T>::CppType;
48
0
    void add(const DataType& data_val, double weight_val) {
49
0
#ifdef __clang__
50
0
#pragma clang fp reassociate(on)
51
0
#endif
52
0
        data_sum = data_sum + (double(data_val) * weight_val);
53
0
        weight_sum = weight_sum + weight_val;
54
0
    }
55
56
0
    void write(BufferWritable& buf) const {
57
0
        buf.write_binary(data_sum);
58
0
        buf.write_binary(weight_sum);
59
0
    }
60
61
0
    void read(BufferReadable& buf) {
62
0
        buf.read_binary(data_sum);
63
0
        buf.read_binary(weight_sum);
64
0
    }
65
66
0
    void merge(const AggregateFunctionAvgWeightedData& rhs) {
67
0
#ifdef __clang__
68
0
#pragma clang fp reassociate(on)
69
0
#endif
70
0
        data_sum = data_sum + rhs.data_sum;
71
0
        weight_sum = weight_sum + rhs.weight_sum;
72
0
    }
73
74
0
    void reset() {
75
0
        data_sum = 0.0;
76
0
        weight_sum = 0.0;
77
0
    }
78
79
0
    double get() const { return data_sum / weight_sum; }
80
81
    double data_sum = 0.0;
82
    double weight_sum = 0.0;
83
};
84
85
template <PrimitiveType type>
86
class AggregateFunctionAvgWeight final
87
        : public IAggregateFunctionDataHelper<AggregateFunctionAvgWeightedData<type>,
88
                                              AggregateFunctionAvgWeight<type>>,
89
          MultiExpression,
90
          NullableAggregateFunction {
91
public:
92
    using T = typename PrimitiveTypeTraits<type>::CppType;
93
    using ColVecType = typename PrimitiveTypeTraits<type>::ColumnType;
94
95
    AggregateFunctionAvgWeight(const DataTypes& argument_types_)
96
0
            : IAggregateFunctionDataHelper<AggregateFunctionAvgWeightedData<type>,
97
0
                                           AggregateFunctionAvgWeight<type>>(argument_types_) {}
98
99
0
    String get_name() const override { return "avg_weighted"; }
100
101
0
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeFloat64>(); }
102
103
    void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num,
104
0
             Arena&) const override {
105
0
        const auto& column =
106
0
                assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(*columns[0]);
107
0
        const auto& weight =
108
0
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[1]);
109
0
        this->data(place).add(column.get_data()[row_num], weight.get_element(row_num));
110
0
    }
111
112
0
    void reset(AggregateDataPtr place) const override { this->data(place).reset(); }
113
114
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
115
0
               Arena&) const override {
116
0
        this->data(place).merge(this->data(rhs));
117
0
    }
118
119
0
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
120
0
        this->data(place).write(buf);
121
0
    }
122
123
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
124
0
                     Arena&) const override {
125
0
        this->data(place).read(buf);
126
0
    }
127
128
0
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
129
0
        auto& column = assert_cast<ColumnFloat64&>(to);
130
0
        column.get_data().push_back(this->data(place).get());
131
0
    }
132
};
133
134
} // namespace doris