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 | | // TODO: use result type got from FE plan |
31 | | template <PrimitiveType T> |
32 | | struct Avg { |
33 | | static constexpr PrimitiveType ResultPType = T == TYPE_DECIMALV2 ? T : TYPE_DOUBLE; |
34 | | using Function = |
35 | | AggregateFunctionAvg<T, ResultPType, AggregateFunctionAvgData<avg_sum_type(T)>>; |
36 | | }; |
37 | | |
38 | | template <PrimitiveType T> |
39 | | using AggregateFuncAvg = typename Avg<T>::Function; |
40 | | |
41 | | // use result type got from FE plan |
42 | | template <PrimitiveType InputType, PrimitiveType ResultType> |
43 | | struct AvgDecimalV3 { |
44 | | using Function = |
45 | | AggregateFunctionAvg<InputType, ResultType, AggregateFunctionAvgData<ResultType>>; |
46 | | }; |
47 | | |
48 | | template <PrimitiveType InputType, PrimitiveType ResultType> |
49 | | using AggregateFuncAvgDecimalV3 = typename AvgDecimalV3<InputType, ResultType>::Function; |
50 | | |
51 | 6 | void register_aggregate_function_avg(AggregateFunctionSimpleFactory& factory) { |
52 | 6 | AggregateFunctionCreator creator = [&](const std::string& name, const DataTypes& types, |
53 | 6 | const DataTypePtr& result_type, |
54 | 6 | const bool result_is_nullable, |
55 | 2.04k | const AggregateFunctionAttr& attr) { |
56 | 2.04k | if (is_decimalv3(types[0]->get_primitive_type())) { |
57 | 582 | return creator_with_type_list<TYPE_DECIMAL32, TYPE_DECIMAL64, TYPE_DECIMAL128I, |
58 | 582 | TYPE_DECIMAL256>:: |
59 | 582 | creator_with_result_type<AggregateFuncAvgDecimalV3>(name, types, result_type, |
60 | 582 | result_is_nullable, attr); |
61 | 1.46k | } else { |
62 | 1.46k | return creator_with_type_list< |
63 | 1.46k | TYPE_TINYINT, TYPE_SMALLINT, TYPE_INT, TYPE_BIGINT, TYPE_LARGEINT, TYPE_DOUBLE, |
64 | 1.46k | TYPE_DECIMALV2>::creator<AggregateFuncAvg>(name, types, result_type, |
65 | 1.46k | result_is_nullable, attr); |
66 | 1.46k | } |
67 | 2.04k | }; |
68 | 6 | factory.register_function_both("avg", creator); |
69 | 6 | } |
70 | | } // namespace doris |