adonis0147 commented on code in PR #9056: URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r852064119
########## be/src/vec/functions/array/function_array_aggregation.cpp: ########## @@ -0,0 +1,303 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/array/arrayAggregation.cpp +// and modified by Doris + +#include <condition_variable> +#include <type_traits> + +#include "vec/common/arithmetic_overflow.h" +#include "vec/core/types.h" +#include "vec/data_types/data_type_array.h" +#include "vec/data_types/data_type_decimal.h" +#include "vec/data_types/data_type_number.h" +#include "vec/functions/array/function_array_mapped.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris { +namespace vectorized { + +#define RETURN_TRUE_WITH_RUNTIME_ERROR(status, msg) \ + do { \ + status = Status::RuntimeError(msg); \ + return true; \ + } while (0); + +enum class AggregateOperation { min, max, sum, average, product }; + +/** + * During array aggregation we derive result type from operation. + * For array min or array max we use array element as result type. + * For array average we use Float64. + * For array sum for for big integers, we use same type representation, decimal numbers we use Decimal128, + * for floating point numbers Float64, for numeric unsigned Int64, and for numeric signed UInt64. + */ + +template <typename ArrayElement, AggregateOperation operation> +struct ArrayAggregateResultImpl; + +template <typename ArrayElement> +struct ArrayAggregateResultImpl<ArrayElement, AggregateOperation::min> { + using Result = ArrayElement; +}; + +template <typename ArrayElement> +struct ArrayAggregateResultImpl<ArrayElement, AggregateOperation::max> { + using Result = ArrayElement; +}; + +template <typename ArrayElement> +struct ArrayAggregateResultImpl<ArrayElement, AggregateOperation::average> { + using Result = Float64; +}; + +template <typename ArrayElement> +struct ArrayAggregateResultImpl<ArrayElement, AggregateOperation::product> { + using Result = Float64; +}; + +template <typename ArrayElement> +struct ArrayAggregateResultImpl<ArrayElement, AggregateOperation::sum> { + using Result = std::conditional_t< + IsDecimalNumber<ArrayElement>, Decimal128, + std::conditional_t< + IsFloatNumber<ArrayElement>, Float64, + std::conditional_t<std::is_same_v<ArrayElement, Int128>, Int128, Int64>>>; +}; + +template <typename ArrayElement, AggregateOperation operation> +using ArrayAggregateResult = typename ArrayAggregateResultImpl<ArrayElement, operation>::Result; + +template <AggregateOperation aggregate_operation> +struct ArrayAggregateImpl { + using column_type = ColumnArray; + using data_type = DataTypeArray; + + static DataTypePtr getReturnType(const DataTypePtr& expression_return, Review Comment: Refined. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org