Coverage Report

Created: 2026-04-15 18:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/aggregate/aggregate_function_covar.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 <boost/iterator/iterator_facade.hpp>
23
#include <cstddef>
24
#include <cstdint>
25
#include <memory>
26
27
#include "core/assert_cast.h"
28
#include "core/column/column.h"
29
#include "core/column/column_nullable.h"
30
#include "core/data_type/data_type_decimal.h"
31
#include "core/data_type/data_type_number.h"
32
#include "core/types.h"
33
#include "exprs/aggregate/aggregate_function.h"
34
35
namespace doris {
36
37
class Arena;
38
class BufferReadable;
39
class BufferWritable;
40
template <PrimitiveType T>
41
class ColumnDecimal;
42
template <PrimitiveType T>
43
class ColumnVector;
44
45
template <PrimitiveType T>
46
struct BaseData {
47
826
    BaseData() = default;
48
825
    virtual ~BaseData() = default;
49
174
    static DataTypePtr get_return_type() { return std::make_shared<DataTypeFloat64>(); }
50
51
338
    void write(BufferWritable& buf) const {
52
338
        buf.write_binary(sum_x);
53
338
        buf.write_binary(sum_y);
54
338
        buf.write_binary(sum_xy);
55
338
        buf.write_binary(count);
56
338
    }
57
58
260
    void read(BufferReadable& buf) {
59
260
        buf.read_binary(sum_x);
60
260
        buf.read_binary(sum_y);
61
260
        buf.read_binary(sum_xy);
62
260
        buf.read_binary(count);
63
260
    }
64
65
76
    void reset() {
66
76
        sum_x = 0.0;
67
76
        sum_y = 0.0;
68
76
        sum_xy = 0.0;
69
76
        count = 0;
70
76
    }
71
72
    // Cov(X, Y) = E(XY) - E(X)E(Y)
73
63
    double get_pop_result() const {
74
63
        if (count == 1) {
75
17
            return 0.0;
76
17
        }
77
46
        return sum_xy / (double)count - sum_x * sum_y / ((double)count * (double)count);
78
63
    }
79
80
47
    double get_samp_result() const {
81
47
        return sum_xy / double(count - 1) -
82
47
               sum_x * sum_y / ((double)(count) * ((double)(count - 1)));
83
47
    }
84
85
260
    void merge(const BaseData& rhs) {
86
260
        if (rhs.count == 0) {
87
0
            return;
88
0
        }
89
260
        sum_x += rhs.sum_x;
90
260
        sum_y += rhs.sum_y;
91
260
        sum_xy += rhs.sum_xy;
92
260
        count += rhs.count;
93
260
    }
94
95
404
    void add(const IColumn* column_x, const IColumn* column_y, size_t row_num) {
96
404
        const auto& sources_x = assert_cast<const typename PrimitiveTypeTraits<T>::ColumnType&,
97
404
                                            TypeCheckOnRelease::DISABLE>(*column_x);
98
404
        double source_data_x = double(sources_x.get_data()[row_num]);
99
404
        const auto& sources_y = assert_cast<const typename PrimitiveTypeTraits<T>::ColumnType&,
100
404
                                            TypeCheckOnRelease::DISABLE>(*column_y);
101
404
        double source_data_y = double(sources_y.get_data()[row_num]);
102
103
404
        sum_x += source_data_x;
104
404
        sum_y += source_data_y;
105
404
        sum_xy += source_data_x * source_data_y;
106
404
        count += 1;
107
404
    }
108
109
    double sum_x {};
110
    double sum_y {};
111
    double sum_xy {};
112
    int64_t count {};
113
};
114
115
template <PrimitiveType T>
116
struct PopData : BaseData<T> {
117
53
    static const char* name() { return "covar"; }
118
119
63
    void insert_result_into(IColumn& to) const {
120
63
        auto& col = assert_cast<ColumnFloat64&>(to);
121
63
        col.get_data().push_back(this->get_pop_result());
122
63
    }
123
};
124
125
template <PrimitiveType T>
126
struct SampData : BaseData<T> {
127
37
    static const char* name() { return "covar_samp"; }
128
129
59
    void insert_result_into(IColumn& to) const {
130
59
        auto& col = assert_cast<ColumnFloat64&>(to);
131
59
        if (this->count == 1 || this->count == 0) {
132
12
            col.insert_default();
133
47
        } else {
134
47
            col.get_data().push_back(this->get_samp_result());
135
47
        }
136
59
    }
137
};
138
139
template <typename Data>
140
class AggregateFunctionSampCovariance final
141
        : public IAggregateFunctionDataHelper<Data, AggregateFunctionSampCovariance<Data>>,
142
          MultiExpression,
143
          NullableAggregateFunction {
144
public:
145
    AggregateFunctionSampCovariance(const DataTypes& argument_types_)
146
556
            : IAggregateFunctionDataHelper<Data, AggregateFunctionSampCovariance<Data>>(
147
556
                      argument_types_) {}
_ZN5doris31AggregateFunctionSampCovarianceINS_8SampDataILNS_13PrimitiveTypeE9EEEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS9_EE
Line
Count
Source
146
280
            : IAggregateFunctionDataHelper<Data, AggregateFunctionSampCovariance<Data>>(
147
280
                      argument_types_) {}
_ZN5doris31AggregateFunctionSampCovarianceINS_7PopDataILNS_13PrimitiveTypeE9EEEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS9_EE
Line
Count
Source
146
276
            : IAggregateFunctionDataHelper<Data, AggregateFunctionSampCovariance<Data>>(
147
276
                      argument_types_) {}
148
149
90
    String get_name() const override { return Data::name(); }
_ZNK5doris31AggregateFunctionSampCovarianceINS_8SampDataILNS_13PrimitiveTypeE9EEEE8get_nameB5cxx11Ev
Line
Count
Source
149
37
    String get_name() const override { return Data::name(); }
_ZNK5doris31AggregateFunctionSampCovarianceINS_7PopDataILNS_13PrimitiveTypeE9EEEE8get_nameB5cxx11Ev
Line
Count
Source
149
53
    String get_name() const override { return Data::name(); }
150
151
174
    DataTypePtr get_return_type() const override { return Data::get_return_type(); }
_ZNK5doris31AggregateFunctionSampCovarianceINS_8SampDataILNS_13PrimitiveTypeE9EEEE15get_return_typeEv
Line
Count
Source
151
81
    DataTypePtr get_return_type() const override { return Data::get_return_type(); }
_ZNK5doris31AggregateFunctionSampCovarianceINS_7PopDataILNS_13PrimitiveTypeE9EEEE15get_return_typeEv
Line
Count
Source
151
93
    DataTypePtr get_return_type() const override { return Data::get_return_type(); }
152
153
    void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num,
154
404
             Arena&) const override {
155
404
        this->data(place).add(columns[0], columns[1], row_num);
156
404
    }
_ZNK5doris31AggregateFunctionSampCovarianceINS_8SampDataILNS_13PrimitiveTypeE9EEEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
Line
Count
Source
154
203
             Arena&) const override {
155
203
        this->data(place).add(columns[0], columns[1], row_num);
156
203
    }
_ZNK5doris31AggregateFunctionSampCovarianceINS_7PopDataILNS_13PrimitiveTypeE9EEEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
Line
Count
Source
154
201
             Arena&) const override {
155
201
        this->data(place).add(columns[0], columns[1], row_num);
156
201
    }
157
158
76
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
_ZNK5doris31AggregateFunctionSampCovarianceINS_8SampDataILNS_13PrimitiveTypeE9EEEE5resetEPc
Line
Count
Source
158
38
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
_ZNK5doris31AggregateFunctionSampCovarianceINS_7PopDataILNS_13PrimitiveTypeE9EEEE5resetEPc
Line
Count
Source
158
38
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
159
160
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
161
260
               Arena&) const override {
162
260
        this->data(place).merge(this->data(rhs));
163
260
    }
_ZNK5doris31AggregateFunctionSampCovarianceINS_8SampDataILNS_13PrimitiveTypeE9EEEE5mergeEPcPKcRNS_5ArenaE
Line
Count
Source
161
129
               Arena&) const override {
162
129
        this->data(place).merge(this->data(rhs));
163
129
    }
_ZNK5doris31AggregateFunctionSampCovarianceINS_7PopDataILNS_13PrimitiveTypeE9EEEE5mergeEPcPKcRNS_5ArenaE
Line
Count
Source
161
131
               Arena&) const override {
162
131
        this->data(place).merge(this->data(rhs));
163
131
    }
164
165
338
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
166
338
        this->data(place).write(buf);
167
338
    }
_ZNK5doris31AggregateFunctionSampCovarianceINS_8SampDataILNS_13PrimitiveTypeE9EEEE9serializeEPKcRNS_14BufferWritableE
Line
Count
Source
165
168
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
166
168
        this->data(place).write(buf);
167
168
    }
_ZNK5doris31AggregateFunctionSampCovarianceINS_7PopDataILNS_13PrimitiveTypeE9EEEE9serializeEPKcRNS_14BufferWritableE
Line
Count
Source
165
170
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
166
170
        this->data(place).write(buf);
167
170
    }
168
169
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
170
260
                     Arena&) const override {
171
260
        this->data(place).read(buf);
172
260
    }
_ZNK5doris31AggregateFunctionSampCovarianceINS_8SampDataILNS_13PrimitiveTypeE9EEEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
Line
Count
Source
170
129
                     Arena&) const override {
171
129
        this->data(place).read(buf);
172
129
    }
_ZNK5doris31AggregateFunctionSampCovarianceINS_7PopDataILNS_13PrimitiveTypeE9EEEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
Line
Count
Source
170
131
                     Arena&) const override {
171
131
        this->data(place).read(buf);
172
131
    }
173
174
122
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
175
122
        this->data(place).insert_result_into(to);
176
122
    }
_ZNK5doris31AggregateFunctionSampCovarianceINS_8SampDataILNS_13PrimitiveTypeE9EEEE18insert_result_intoEPKcRNS_7IColumnE
Line
Count
Source
174
59
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
175
59
        this->data(place).insert_result_into(to);
176
59
    }
_ZNK5doris31AggregateFunctionSampCovarianceINS_7PopDataILNS_13PrimitiveTypeE9EEEE18insert_result_intoEPKcRNS_7IColumnE
Line
Count
Source
174
63
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
175
63
        this->data(place).insert_result_into(to);
176
63
    }
177
};
178
179
} // namespace doris