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