Coverage Report

Created: 2026-04-10 04:05

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