Coverage Report

Created: 2026-07-23 23:41

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 <limits>
26
#include <memory>
27
#include <type_traits>
28
29
#include "common/compiler_util.h"
30
#include "common/status.h"
31
#include "core/assert_cast.h"
32
#include "core/binary_cast.hpp"
33
#include "core/column/column_vector.h"
34
#include "core/data_type/data_type_number.h"
35
#include "core/types.h"
36
#include "core/value/decimalv2_value.h"
37
#include "exprs/aggregate/aggregate_function.h"
38
39
namespace doris {
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
        ALLOW_FP_REASSOCIATION
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
        ALLOW_FP_REASSOCIATION
68
0
        data_sum = data_sum + rhs.data_sum;
69
0
        weight_sum = weight_sum + rhs.weight_sum;
70
0
    }
71
72
0
    void reset() {
73
0
        data_sum = 0.0;
74
0
        weight_sum = 0.0;
75
0
    }
76
77
0
    double get() const {
78
0
        if (weight_sum == 0.0) {
79
0
            return std::numeric_limits<double>::quiet_NaN();
80
0
        }
81
0
        return data_sum / weight_sum;
82
0
    }
83
84
    double data_sum = 0.0;
85
    double weight_sum = 0.0;
86
};
87
88
template <PrimitiveType type>
89
class AggregateFunctionAvgWeight final
90
        : public IAggregateFunctionDataHelper<AggregateFunctionAvgWeightedData<type>,
91
                                              AggregateFunctionAvgWeight<type>>,
92
          MultiExpression,
93
          NullableAggregateFunction {
94
public:
95
    using T = typename PrimitiveTypeTraits<type>::CppType;
96
    using ColVecType = typename PrimitiveTypeTraits<type>::ColumnType;
97
98
    AggregateFunctionAvgWeight(const DataTypes& argument_types_)
99
0
            : IAggregateFunctionDataHelper<AggregateFunctionAvgWeightedData<type>,
100
0
                                           AggregateFunctionAvgWeight<type>>(argument_types_) {}
101
102
0
    String get_name() const override { return "avg_weighted"; }
103
104
0
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeFloat64>(); }
105
106
    void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num,
107
0
             Arena&) const override {
108
0
        const auto& column =
109
0
                assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(*columns[0]);
110
0
        const auto& weight =
111
0
                assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[1]);
112
0
        this->data(place).add(column.get_data()[row_num], weight.get_element(row_num));
113
0
    }
114
115
0
    void check_input_columns_type(const IColumn** columns) const override {
116
0
        this->template check_argument_column_type<ColVecType>(columns[0]);
117
0
        this->template check_argument_column_type<ColumnFloat64>(columns[1]);
118
0
    }
119
120
0
    void reset(AggregateDataPtr place) const override { this->data(place).reset(); }
121
122
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
123
0
               Arena&) const override {
124
0
        this->data(place).merge(this->data(rhs));
125
0
    }
126
127
0
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
128
0
        this->data(place).write(buf);
129
0
    }
130
131
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
132
0
                     Arena&) const override {
133
0
        this->data(place).read(buf);
134
0
    }
135
136
0
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
137
0
        auto& column = assert_cast<ColumnFloat64&, TypeCheckOnRelease::DISABLE>(to);
138
0
        column.get_data().push_back(this->data(place).get());
139
0
    }
140
};
141
142
} // namespace doris