Coverage Report

Created: 2026-04-10 12:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/aggregate/aggregate_function_avg.cpp
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
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/AggregateFunctions/AggregateFunctionAvg.cpp
19
// and modified by Doris
20
21
#include "exprs/aggregate/aggregate_function_avg.h"
22
23
#include "core/data_type/define_primitive_type.h"
24
#include "core/field.h"
25
#include "exprs/aggregate/aggregate_function_simple_factory.h"
26
#include "exprs/aggregate/helpers.h"
27
28
namespace doris {
29
30
constexpr PrimitiveType result_type(PrimitiveType T) {
31
    if (T == TYPE_LARGEINT || T == TYPE_BIGINT) {
32
        return TYPE_LARGEINT;
33
    } else if (is_int_or_bool(T)) {
34
        return TYPE_BIGINT;
35
    } else if (is_float_or_double(T) || is_time_type(T)) {
36
        return TYPE_DOUBLE;
37
    } else if (is_decimalv3(T) && T != TYPE_DECIMAL256) {
38
        return TYPE_DECIMAL128I;
39
    } else {
40
        return T;
41
    }
42
}
43
44
// TODO: use result type got from FE plan
45
template <PrimitiveType T>
46
struct Avg {
47
    static constexpr PrimitiveType ResultPType = T == TYPE_DECIMALV2 ? T : TYPE_DOUBLE;
48
    using Function = AggregateFunctionAvg<T, ResultPType, AggregateFunctionAvgData<result_type(T)>>;
49
};
50
51
template <PrimitiveType T>
52
using AggregateFuncAvg = typename Avg<T>::Function;
53
54
// use result type got from FE plan
55
template <PrimitiveType InputType, PrimitiveType ResultType>
56
struct AvgDecimalV3 {
57
    using Function =
58
            AggregateFunctionAvg<InputType, ResultType, AggregateFunctionAvgData<ResultType>>;
59
};
60
61
template <PrimitiveType InputType, PrimitiveType ResultType>
62
using AggregateFuncAvgDecimalV3 = typename AvgDecimalV3<InputType, ResultType>::Function;
63
64
4
void register_aggregate_function_avg(AggregateFunctionSimpleFactory& factory) {
65
4
    AggregateFunctionCreator creator = [&](const std::string& name, const DataTypes& types,
66
4
                                           const DataTypePtr& result_type,
67
4
                                           const bool result_is_nullable,
68
2.82k
                                           const AggregateFunctionAttr& attr) {
69
2.82k
        if (is_decimalv3(types[0]->get_primitive_type())) {
70
897
            return creator_with_type_list<TYPE_DECIMAL32, TYPE_DECIMAL64, TYPE_DECIMAL128I,
71
897
                                          TYPE_DECIMAL256>::
72
897
                    creator_with_result_type<AggregateFuncAvgDecimalV3>(name, types, result_type,
73
897
                                                                        result_is_nullable, attr);
74
1.92k
        } else {
75
1.92k
            return creator_with_type_list<
76
1.92k
                    TYPE_TINYINT, TYPE_SMALLINT, TYPE_INT, TYPE_BIGINT, TYPE_LARGEINT, TYPE_DOUBLE,
77
1.92k
                    TYPE_DECIMALV2>::creator<AggregateFuncAvg>(name, types, result_type,
78
1.92k
                                                               result_is_nullable, attr);
79
1.92k
        }
80
2.82k
    };
81
4
    factory.register_function_both("avg", creator);
82
4
}
83
} // namespace doris