You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/04/15 14:15:42 UTC

[GitHub] [incubator-doris] adonis0147 opened a new pull request, #9056: [feature-wip](array-type) Add array aggregation functions

adonis0147 opened a new pull request, #9056:
URL: https://github.com/apache/incubator-doris/pull/9056

   # Proposed changes
   
   Issue Number: close #9055 
   
   ## Problem Summary:
   
   Please refer to #9055 
   
   ## Checklist(Required)
   
   1. Does it affect the original behavior: No
   2. Has unit tests been added: Yes
   3. Has document been added or modified: No
   4. Does it need to update dependencies: No
   5. Are there any changes that cannot be rolled back: No
   
   ## Further comments
   
   If this is a relatively large or complex change, kick off the discussion at [dev@doris.apache.org](mailto:dev@doris.apache.org) by explaining why you chose the solution you did and what alternatives you considered, etc...
   


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] HappenLee commented on pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
HappenLee commented on PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#issuecomment-1149935128

   array agg([]) should return `NULL`, support it 


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] HappenLee commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r854030386


##########
be/src/vec/functions/array/function_array_aggregation.cpp:
##########
@@ -0,0 +1,312 @@
+// 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 {
+
+enum class AggregationStatus {
+    MISMATCHED_TYPE = 0,
+    OK,
+    MATH_OVERFLOW,
+    SCALE_OUT_OF_BOUNDS,
+};
+
+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<

Review Comment:
   please check how Aggregate Fucntion `SUM` impl to check the Result



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r852011143


##########
be/test/vec/function/function_array_aggregation_test.cpp:
##########
@@ -0,0 +1,122 @@
+// 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.
+
+#include <gtest/gtest.h>
+
+#include <string>
+#include <type_traits>
+
+#include "function_test_util.h"
+#include "vec/data_types/data_type_number.h"
+
+namespace doris {
+namespace vectorized {
+
+using IntDataSet = std::vector<std::pair<std::vector<int>, int>>;
+
+template <typename T, typename ReturnType = T>
+void check_function(const std::string& func_name, const IntDataSet data_set) {
+    InputTypeSet input_types = {TypeIndex::Array, ut_type::get_type_index<T>()};
+    DataSet converted_data_set;
+    for (const auto& row : data_set) {
+        Array array;
+        for (auto value : row.first) {
+            array.push_back(ut_type::convert_to<T>(value));
+        }
+        converted_data_set.emplace_back(std::make_pair<CellSet, Expect>(
+                {array}, ut_type::convert_to<ReturnType>(row.second)));
+    }
+    check_function<ReturnType>(func_name, input_types, converted_data_set);
+}
+
+TEST(FunctionArrayAggregationTest, TestArrayMin) {

Review Comment:
   Added.



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] HappenLee commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r893062130


##########
be/src/vec/functions/array/function_array_aggregation.cpp:
##########
@@ -0,0 +1,300 @@
+// 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 <type_traits>
+
+#include "vec/aggregate_functions/aggregate_function_avg.h"
+#include "vec/aggregate_functions/aggregate_function_min_max.h"
+#include "vec/aggregate_functions/aggregate_function_null.h"
+#include "vec/aggregate_functions/aggregate_function_product.h"
+#include "vec/aggregate_functions/aggregate_function_sum.h"
+#include "vec/aggregate_functions/helpers.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/arena.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/array/function_array_mapped.h"
+#include "vec/functions/simple_function_factory.h"
+
+namespace doris {
+namespace vectorized {
+
+enum class AggregateOperation { MIN, MAX, SUM, AVERAGE, PRODUCT };
+
+template <typename Element, AggregateOperation operation>
+struct ArrayAggregateResultImpl;
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MIN> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MAX> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::AVERAGE> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::PRODUCT> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::SUM> {
+    using Result = std::conditional_t<
+            IsDecimalNumber<Element>, Decimal128,
+            std::conditional_t<IsFloatNumber<Element>, Float64,
+                               std::conditional_t<std::is_same_v<Element, Int128>, Int128, Int64>>>;
+};
+
+template <typename Element, AggregateOperation operation>
+using ArrayAggregateResult = typename ArrayAggregateResultImpl<Element, operation>::Result;
+
+template <AggregateOperation operation>
+struct AggregateFunctionImpl;
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::SUM> {
+    template <typename Element>
+    struct TypeTraits {
+        using ResultType = ArrayAggregateResult<Element, AggregateOperation::SUM>;
+        using AggregateDataType = AggregateFunctionSumData<ResultType>;
+        using Function = AggregateFunctionSum<Element, ResultType, AggregateDataType>;
+    };
+};
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::AVERAGE> {
+    template <typename Element>
+    struct TypeTraits {
+        struct AggregateDataType : public AggregateFunctionAvgData<Element> {
+            using AggregateFunctionAvgData<Element>::count;
+            using AggregateFunctionAvgData<Element>::sum;
+
+            template <typename ResultT>
+            ResultT result() const {
+                return count ? AggregateFunctionAvgData<Element>::template result<ResultT>()
+                             : static_cast<ResultT>(sum);
+            }
+        };
+
+        using ResultType = ArrayAggregateResult<Element, AggregateOperation::AVERAGE>;
+        using Function = AggregateFunctionAvg<Element, AggregateDataType>;
+        static_assert(std::is_same_v<ResultType, typename Function::ResultType>,
+                      "ResultType doesn't match.");
+    };
+};
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::PRODUCT> {
+    template <typename Element>
+    struct TypeTraits {
+        using ResultType = ArrayAggregateResult<Element, AggregateOperation::PRODUCT>;
+        using AggregateDataType = AggregateFunctionProductData<Element>;
+        using Function = AggregateFunctionProduct<Element, ResultType, AggregateDataType>;
+    };
+};
+
+template <typename Derived>
+struct AggregateFunction {
+    template <typename T>
+    using Function = typename Derived::template TypeTraits<T>::Function;
+
+    static auto create(const DataTypePtr& data_type) -> AggregateFunctionPtr {
+        DataTypes data_types = {data_type};
+        AggregateFunctionPtr function;
+
+        if (data_type->is_nullable()) {
+            const auto& nested_data_type =
+                    static_cast<const DataTypeNullable&>(*data_type).get_nested_type();
+            auto nested_function = create(nested_data_type);
+            function.reset(new AggregateFunctionNullUnary<true>(nested_function, data_types, {}));
+        } else {
+            if (is_decimal(data_type)) {
+                function.reset(
+                        create_with_decimal_type<Function>(*data_type, *data_type, data_types));
+            } else {
+                function.reset(create_with_numeric_type<Function>(*data_type, data_types));
+            }
+        }
+        return function;
+    }
+};
+
+template <AggregateOperation operation>
+struct ArrayAggregateImpl {
+    using column_type = ColumnArray;
+    using data_type = DataTypeArray;
+
+    static DataTypePtr get_return_type(const DataTypeArray* data_type_array) {
+        using Function = AggregateFunction<AggregateFunctionImpl<operation>>;
+        auto function = Function::create(data_type_array->get_nested_type());
+        return function->get_return_type();
+    }
+
+    static Status execute(Block& block, size_t result, const DataTypeArray* data_type_array,
+                          const ColumnArray& array) {
+        ColumnPtr res;
+        DataTypePtr type = data_type_array->get_nested_type();
+        const IColumn* data = array.get_data_ptr().get();
+
+        const auto& offsets = array.get_offsets();
+        if (execute_type<Int8>(res, type, data, offsets) ||
+            execute_type<Int16>(res, type, data, offsets) ||
+            execute_type<Int32>(res, type, data, offsets) ||
+            execute_type<Int64>(res, type, data, offsets) ||
+            execute_type<Int128>(res, type, data, offsets) ||
+            execute_type<Float32>(res, type, data, offsets) ||
+            execute_type<Float64>(res, type, data, offsets) ||
+            execute_type<Decimal128>(res, type, data, offsets)) {
+            block.replace_by_position(result, std::move(res));
+            return Status::OK();
+        } else {
+            return Status::RuntimeError("Unexpected column for aggregation: " + data->get_name());
+        }
+    }
+
+    template <typename Element>
+    static bool execute_type(ColumnPtr& res_ptr, const DataTypePtr& type, const IColumn* data,
+                             const ColumnArray::Offsets& offsets) {
+        using ColVecType = ColumnVectorOrDecimal<Element>;
+        using ResultType = ArrayAggregateResult<Element, operation>;
+        using ColVecResultType = ColumnVectorOrDecimal<ResultType>;
+        using Function = AggregateFunction<AggregateFunctionImpl<operation>>;
+
+        const ColVecType* column =
+                data->is_nullable()
+                        ? check_and_get_column<ColVecType>(
+                                  static_cast<const ColumnNullable*>(data)->get_nested_column())
+                        : check_and_get_column<ColVecType>(&*data);
+        if (!column) {
+            return false;
+        }
+
+        ColumnPtr res_column;
+        if constexpr (IsDecimalNumber<Element>) {
+            res_column =
+                    make_nullable(ColVecResultType::create(offsets.size(), column->get_scale()));
+        } else {
+            res_column = make_nullable(ColVecResultType::create(offsets.size()));
+        }
+        static_cast<ColumnNullable&>(res_column->assume_mutable_ref()).clear();

Review Comment:
   `res_column = make_nullable(res_column);`



##########
be/src/vec/functions/array/function_array_mapped.h:
##########
@@ -0,0 +1,76 @@
+// 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/FunctionArrayMapped.h
+// and modified by Doris
+
+#include "vec/columns/column.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/function.h"
+
+namespace doris {
+namespace vectorized {
+
+/** Higher-order functions for arrays.
+  * These functions optionally apply a map (transform) to array (or multiple arrays of identical size) by lambda function,
+  *  and return some result based on that transformation.
+  *
+  * Examples:
+  * arrayMap(x1,...,xn -> expression, array1,...,arrayn) - apply the expression to each element of the array (or set of parallel arrays).
+  * arrayFilter(x -> predicate, array) - leave in the array only the elements for which the expression is true.
+  *
+  * For some functions arrayCount, arrayExists, arrayAll, an overload of the form f(array) is available,
+  *  which works in the same way as f(x -> x, array).
+  *
+  * See the example of Impl template parameter in arrayMap.cpp
+  */
+template <typename Impl, typename Name>
+class FunctionArrayMapped : public IFunction {
+public:
+    static constexpr auto name = Name::name;
+    static FunctionPtr create() { return std::make_shared<FunctionArrayMapped>(); }
+
+    String get_name() const override { return name; }
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        const auto& typed_column = block.get_by_position(arguments[0]);
+        const typename Impl::column_type* column_array;
+        if (typed_column.column->is_nullable()) {
+            column_array = check_and_get_column<const typename Impl::column_type>(
+                    static_cast<const ColumnNullable*>(typed_column.column.get())
+                            ->get_nested_column_ptr()
+                            .get());
+        } else {
+            column_array = check_and_get_column<const typename Impl::column_type>(
+                    typed_column.column.get());
+        }
+        const auto* data_type_array =
+                static_cast<const DataTypeArray*>(remove_nullable(typed_column.type).get());
+        return Impl::execute(block, result, data_type_array, *column_array);
+    }
+    size_t get_number_of_arguments() const override { return 1; }
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
+        const DataTypeArray* data_type_array =
+                static_cast<const DataTypeArray*>(remove_nullable(arguments[0]).get());
+        return Impl::get_return_type(data_type_array);

Review Comment:
   `make_nullable(Impl::get_return_type(data_type_array)`;



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r852010828


##########
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) \

Review Comment:
   Refined.
   Use `enum` variables to indicate the different results. 
   Please refer to [DIFF](https://github.com/apache/incubator-doris/pull/9056/commits/ebe1b54797b0f4e11ce749bff6b30072ac66a61b#diff-d95609164d9cc73b67ee228a5e98e7a3602f4f14994c657e4043d8af87d12c83R35-R40).



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r852011012


##########
be/src/vec/functions/array/function_array_mapped.h:
##########
@@ -0,0 +1,106 @@
+// 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/FunctionArrayMapped.h
+// and modified by Doris
+
+#include "vec/columns/column_nullable.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/function.h"
+#include "vec/functions/function_helpers.h"
+
+namespace doris {
+namespace vectorized {
+
+class ColumnArray;
+class DataTypeArray;
+
+template <typename T>
+ColumnPtr getOffsetsPtr(const T& column) {

Review Comment:
   Useless functions, deleted.



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r895707236


##########
be/src/vec/functions/array/function_array_aggregation.cpp:
##########
@@ -0,0 +1,300 @@
+// 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 <type_traits>
+
+#include "vec/aggregate_functions/aggregate_function_avg.h"
+#include "vec/aggregate_functions/aggregate_function_min_max.h"
+#include "vec/aggregate_functions/aggregate_function_null.h"
+#include "vec/aggregate_functions/aggregate_function_product.h"
+#include "vec/aggregate_functions/aggregate_function_sum.h"
+#include "vec/aggregate_functions/helpers.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/arena.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/array/function_array_mapped.h"
+#include "vec/functions/simple_function_factory.h"
+
+namespace doris {
+namespace vectorized {
+
+enum class AggregateOperation { MIN, MAX, SUM, AVERAGE, PRODUCT };
+
+template <typename Element, AggregateOperation operation>
+struct ArrayAggregateResultImpl;
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MIN> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MAX> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::AVERAGE> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::PRODUCT> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::SUM> {
+    using Result = std::conditional_t<
+            IsDecimalNumber<Element>, Decimal128,
+            std::conditional_t<IsFloatNumber<Element>, Float64,
+                               std::conditional_t<std::is_same_v<Element, Int128>, Int128, Int64>>>;
+};
+
+template <typename Element, AggregateOperation operation>
+using ArrayAggregateResult = typename ArrayAggregateResultImpl<Element, operation>::Result;
+
+template <AggregateOperation operation>
+struct AggregateFunctionImpl;
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::SUM> {
+    template <typename Element>
+    struct TypeTraits {
+        using ResultType = ArrayAggregateResult<Element, AggregateOperation::SUM>;
+        using AggregateDataType = AggregateFunctionSumData<ResultType>;
+        using Function = AggregateFunctionSum<Element, ResultType, AggregateDataType>;
+    };
+};
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::AVERAGE> {
+    template <typename Element>
+    struct TypeTraits {
+        struct AggregateDataType : public AggregateFunctionAvgData<Element> {
+            using AggregateFunctionAvgData<Element>::count;
+            using AggregateFunctionAvgData<Element>::sum;
+
+            template <typename ResultT>
+            ResultT result() const {
+                return count ? AggregateFunctionAvgData<Element>::template result<ResultT>()
+                             : static_cast<ResultT>(sum);
+            }
+        };
+
+        using ResultType = ArrayAggregateResult<Element, AggregateOperation::AVERAGE>;
+        using Function = AggregateFunctionAvg<Element, AggregateDataType>;
+        static_assert(std::is_same_v<ResultType, typename Function::ResultType>,
+                      "ResultType doesn't match.");
+    };
+};
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::PRODUCT> {
+    template <typename Element>
+    struct TypeTraits {
+        using ResultType = ArrayAggregateResult<Element, AggregateOperation::PRODUCT>;
+        using AggregateDataType = AggregateFunctionProductData<Element>;
+        using Function = AggregateFunctionProduct<Element, ResultType, AggregateDataType>;
+    };
+};
+
+template <typename Derived>
+struct AggregateFunction {
+    template <typename T>
+    using Function = typename Derived::template TypeTraits<T>::Function;
+
+    static auto create(const DataTypePtr& data_type) -> AggregateFunctionPtr {
+        DataTypes data_types = {data_type};
+        AggregateFunctionPtr function;
+
+        if (data_type->is_nullable()) {
+            const auto& nested_data_type =
+                    static_cast<const DataTypeNullable&>(*data_type).get_nested_type();
+            auto nested_function = create(nested_data_type);
+            function.reset(new AggregateFunctionNullUnary<true>(nested_function, data_types, {}));
+        } else {
+            if (is_decimal(data_type)) {
+                function.reset(
+                        create_with_decimal_type<Function>(*data_type, *data_type, data_types));
+            } else {
+                function.reset(create_with_numeric_type<Function>(*data_type, data_types));
+            }
+        }
+        return function;
+    }
+};
+
+template <AggregateOperation operation>
+struct ArrayAggregateImpl {
+    using column_type = ColumnArray;
+    using data_type = DataTypeArray;
+
+    static DataTypePtr get_return_type(const DataTypeArray* data_type_array) {
+        using Function = AggregateFunction<AggregateFunctionImpl<operation>>;
+        auto function = Function::create(data_type_array->get_nested_type());
+        return function->get_return_type();
+    }
+
+    static Status execute(Block& block, size_t result, const DataTypeArray* data_type_array,
+                          const ColumnArray& array) {
+        ColumnPtr res;
+        DataTypePtr type = data_type_array->get_nested_type();
+        const IColumn* data = array.get_data_ptr().get();
+
+        const auto& offsets = array.get_offsets();
+        if (execute_type<Int8>(res, type, data, offsets) ||
+            execute_type<Int16>(res, type, data, offsets) ||
+            execute_type<Int32>(res, type, data, offsets) ||
+            execute_type<Int64>(res, type, data, offsets) ||
+            execute_type<Int128>(res, type, data, offsets) ||
+            execute_type<Float32>(res, type, data, offsets) ||
+            execute_type<Float64>(res, type, data, offsets) ||
+            execute_type<Decimal128>(res, type, data, offsets)) {
+            block.replace_by_position(result, std::move(res));
+            return Status::OK();
+        } else {
+            return Status::RuntimeError("Unexpected column for aggregation: " + data->get_name());
+        }
+    }
+
+    template <typename Element>
+    static bool execute_type(ColumnPtr& res_ptr, const DataTypePtr& type, const IColumn* data,
+                             const ColumnArray::Offsets& offsets) {
+        using ColVecType = ColumnVectorOrDecimal<Element>;
+        using ResultType = ArrayAggregateResult<Element, operation>;
+        using ColVecResultType = ColumnVectorOrDecimal<ResultType>;
+        using Function = AggregateFunction<AggregateFunctionImpl<operation>>;
+
+        const ColVecType* column =
+                data->is_nullable()
+                        ? check_and_get_column<ColVecType>(
+                                  static_cast<const ColumnNullable*>(data)->get_nested_column())
+                        : check_and_get_column<ColVecType>(&*data);
+        if (!column) {
+            return false;
+        }
+
+        ColumnPtr res_column;
+        if constexpr (IsDecimalNumber<Element>) {
+            res_column =
+                    make_nullable(ColVecResultType::create(offsets.size(), column->get_scale()));
+        } else {
+            res_column = make_nullable(ColVecResultType::create(offsets.size()));
+        }
+        static_cast<ColumnNullable&>(res_column->assume_mutable_ref()).clear();

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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 closed pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 closed pull request #9056: [feature-wip](array-type) Add array aggregation functions
URL: https://github.com/apache/incubator-doris/pull/9056


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] HappenLee commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r893066739


##########
be/src/vec/functions/array/function_array_mapped.h:
##########
@@ -0,0 +1,76 @@
+// 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/FunctionArrayMapped.h
+// and modified by Doris
+
+#include "vec/columns/column.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/function.h"
+
+namespace doris {
+namespace vectorized {
+
+/** Higher-order functions for arrays.
+  * These functions optionally apply a map (transform) to array (or multiple arrays of identical size) by lambda function,
+  *  and return some result based on that transformation.
+  *
+  * Examples:
+  * arrayMap(x1,...,xn -> expression, array1,...,arrayn) - apply the expression to each element of the array (or set of parallel arrays).
+  * arrayFilter(x -> predicate, array) - leave in the array only the elements for which the expression is true.
+  *
+  * For some functions arrayCount, arrayExists, arrayAll, an overload of the form f(array) is available,
+  *  which works in the same way as f(x -> x, array).
+  *
+  * See the example of Impl template parameter in arrayMap.cpp
+  */
+template <typename Impl, typename Name>
+class FunctionArrayMapped : public IFunction {
+public:
+    static constexpr auto name = Name::name;
+    static FunctionPtr create() { return std::make_shared<FunctionArrayMapped>(); }
+
+    String get_name() const override { return name; }
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        const auto& typed_column = block.get_by_position(arguments[0]);
+        const typename Impl::column_type* column_array;
+        if (typed_column.column->is_nullable()) {

Review Comment:
   should delele the judge of `if (typed_column.column->is_nullable())`, because set the `use_default_implementation_for_nulls()` is `true`



##########
be/src/vec/functions/array/function_array_aggregation.cpp:
##########
@@ -0,0 +1,300 @@
+// 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 <type_traits>
+
+#include "vec/aggregate_functions/aggregate_function_avg.h"
+#include "vec/aggregate_functions/aggregate_function_min_max.h"
+#include "vec/aggregate_functions/aggregate_function_null.h"
+#include "vec/aggregate_functions/aggregate_function_product.h"
+#include "vec/aggregate_functions/aggregate_function_sum.h"
+#include "vec/aggregate_functions/helpers.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/arena.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/array/function_array_mapped.h"
+#include "vec/functions/simple_function_factory.h"
+
+namespace doris {
+namespace vectorized {
+
+enum class AggregateOperation { MIN, MAX, SUM, AVERAGE, PRODUCT };
+
+template <typename Element, AggregateOperation operation>
+struct ArrayAggregateResultImpl;
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MIN> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MAX> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::AVERAGE> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::PRODUCT> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::SUM> {
+    using Result = std::conditional_t<
+            IsDecimalNumber<Element>, Decimal128,
+            std::conditional_t<IsFloatNumber<Element>, Float64,
+                               std::conditional_t<std::is_same_v<Element, Int128>, Int128, Int64>>>;
+};
+
+template <typename Element, AggregateOperation operation>
+using ArrayAggregateResult = typename ArrayAggregateResultImpl<Element, operation>::Result;
+
+template <AggregateOperation operation>
+struct AggregateFunctionImpl;
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::SUM> {
+    template <typename Element>
+    struct TypeTraits {
+        using ResultType = ArrayAggregateResult<Element, AggregateOperation::SUM>;
+        using AggregateDataType = AggregateFunctionSumData<ResultType>;
+        using Function = AggregateFunctionSum<Element, ResultType, AggregateDataType>;
+    };
+};
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::AVERAGE> {
+    template <typename Element>
+    struct TypeTraits {
+        struct AggregateDataType : public AggregateFunctionAvgData<Element> {

Review Comment:
   useless clase to dispose `[]`, delete it



##########
be/src/vec/aggregate_functions/aggregate_function_product.h:
##########
@@ -0,0 +1,137 @@
+// 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.
+
+#pragma once
+
+#include <cstddef>
+#include <type_traits>
+
+#include "vec/aggregate_functions/aggregate_function.h"
+#include "vec/columns/column_decimal.h"
+#include "vec/columns/column_vector.h"
+#include "vec/common/arena.h"
+#include "vec/common/string_buffer.hpp"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type_decimal.h"
+#include "vec/data_types/data_type_number.h"
+#include "vec/io/io_helper.h"
+
+namespace doris {
+namespace vectorized {
+
+template <typename T>
+struct AggregateFunctionProductData {
+    T product {};
+    bool has_data = false;

Review Comment:
   useless `has_data` 



##########
be/src/vec/functions/array/function_array_aggregation.cpp:
##########
@@ -0,0 +1,300 @@
+// 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 <type_traits>
+
+#include "vec/aggregate_functions/aggregate_function_avg.h"
+#include "vec/aggregate_functions/aggregate_function_min_max.h"
+#include "vec/aggregate_functions/aggregate_function_null.h"
+#include "vec/aggregate_functions/aggregate_function_product.h"
+#include "vec/aggregate_functions/aggregate_function_sum.h"
+#include "vec/aggregate_functions/helpers.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/arena.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/array/function_array_mapped.h"
+#include "vec/functions/simple_function_factory.h"
+
+namespace doris {
+namespace vectorized {
+
+enum class AggregateOperation { MIN, MAX, SUM, AVERAGE, PRODUCT };
+
+template <typename Element, AggregateOperation operation>
+struct ArrayAggregateResultImpl;
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MIN> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MAX> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::AVERAGE> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::PRODUCT> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::SUM> {
+    using Result = std::conditional_t<
+            IsDecimalNumber<Element>, Decimal128,
+            std::conditional_t<IsFloatNumber<Element>, Float64,
+                               std::conditional_t<std::is_same_v<Element, Int128>, Int128, Int64>>>;
+};
+
+template <typename Element, AggregateOperation operation>
+using ArrayAggregateResult = typename ArrayAggregateResultImpl<Element, operation>::Result;
+
+template <AggregateOperation operation>
+struct AggregateFunctionImpl;
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::SUM> {

Review Comment:
   Add conment of why MIN/MAX do have AggregateFunctionImpl



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r853841093


##########
be/src/vec/functions/array/function_array_aggregation.cpp:
##########
@@ -0,0 +1,312 @@
+// 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 {
+
+enum class AggregationStatus {
+    MISMATCHED_TYPE = 0,
+    OK,
+    MATH_OVERFLOW,
+    SCALE_OUT_OF_BOUNDS,
+};
+
+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<

Review Comment:
   I think the result of the operation `SUM` should be as big as possible in case of math overflow. This logic is consistent with `ClickHouse`.



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] yangzhg commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
yangzhg commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r851856862


##########
be/test/vec/function/function_array_aggregation_test.cpp:
##########
@@ -0,0 +1,122 @@
+// 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.
+
+#include <gtest/gtest.h>
+
+#include <string>
+#include <type_traits>
+
+#include "function_test_util.h"
+#include "vec/data_types/data_type_number.h"
+
+namespace doris {
+namespace vectorized {
+
+using IntDataSet = std::vector<std::pair<std::vector<int>, int>>;
+
+template <typename T, typename ReturnType = T>
+void check_function(const std::string& func_name, const IntDataSet data_set) {
+    InputTypeSet input_types = {TypeIndex::Array, ut_type::get_type_index<T>()};
+    DataSet converted_data_set;
+    for (const auto& row : data_set) {
+        Array array;
+        for (auto value : row.first) {
+            array.push_back(ut_type::convert_to<T>(value));
+        }
+        converted_data_set.emplace_back(std::make_pair<CellSet, Expect>(
+                {array}, ut_type::convert_to<ReturnType>(row.second)));
+    }
+    check_function<ReturnType>(func_name, input_types, converted_data_set);
+}
+
+TEST(FunctionArrayAggregationTest, TestArrayMin) {

Review Comment:
   better to add  `V` as a prefix of vectorized test



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r852011498


##########
gensrc/script/doris_builtins_functions.py:
##########
@@ -176,6 +176,82 @@
         '_ZN5doris10vectorized18FunctionArrayIndexINS0_19ArrayPositionActionENS0_17NameArrayPositionEE12execute_implEPN9doris_udf15FunctionContextERNS0_5BlockERKSt6vectorImSaImEEmm',
         '', '', 'vec', ''],
 
+    [['array_min'], 'TINYINT', ['ARRAY_TINYINT'],
+        '_ZN5doris10vectorized19FunctionArrayMappedINS0_18ArrayAggregateImplILNS0_18AggregateOperationE0EEENS0_12NameArrayMinEE12execute_implEPN9doris_udf15FunctionContextERNS0_5BlockERKSt6vectorImSaImEEmm',

Review Comment:
   Yes, they are functions for `vec` query engine. Symbols are removed.



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] yangzhg commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
yangzhg commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r851854669


##########
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:
   for `average` why not use `ArrayAggregateResultImpl` 



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#issuecomment-1158453544

   Deleted by accident, please refer to #10108


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r895713249


##########
be/src/vec/functions/array/function_array_mapped.h:
##########
@@ -0,0 +1,76 @@
+// 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/FunctionArrayMapped.h
+// and modified by Doris
+
+#include "vec/columns/column.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/function.h"
+
+namespace doris {
+namespace vectorized {
+
+/** Higher-order functions for arrays.
+  * These functions optionally apply a map (transform) to array (or multiple arrays of identical size) by lambda function,
+  *  and return some result based on that transformation.
+  *
+  * Examples:
+  * arrayMap(x1,...,xn -> expression, array1,...,arrayn) - apply the expression to each element of the array (or set of parallel arrays).
+  * arrayFilter(x -> predicate, array) - leave in the array only the elements for which the expression is true.
+  *
+  * For some functions arrayCount, arrayExists, arrayAll, an overload of the form f(array) is available,
+  *  which works in the same way as f(x -> x, array).
+  *
+  * See the example of Impl template parameter in arrayMap.cpp
+  */
+template <typename Impl, typename Name>
+class FunctionArrayMapped : public IFunction {
+public:
+    static constexpr auto name = Name::name;
+    static FunctionPtr create() { return std::make_shared<FunctionArrayMapped>(); }
+
+    String get_name() const override { return name; }
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        const auto& typed_column = block.get_by_position(arguments[0]);
+        const typename Impl::column_type* column_array;
+        if (typed_column.column->is_nullable()) {
+            column_array = check_and_get_column<const typename Impl::column_type>(
+                    static_cast<const ColumnNullable*>(typed_column.column.get())
+                            ->get_nested_column_ptr()
+                            .get());
+        } else {
+            column_array = check_and_get_column<const typename Impl::column_type>(
+                    typed_column.column.get());
+        }
+        const auto* data_type_array =
+                static_cast<const DataTypeArray*>(remove_nullable(typed_column.type).get());
+        return Impl::execute(block, result, data_type_array, *column_array);
+    }
+    size_t get_number_of_arguments() const override { return 1; }
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
+        const DataTypeArray* data_type_array =
+                static_cast<const DataTypeArray*>(remove_nullable(arguments[0]).get());
+        return Impl::get_return_type(data_type_array);

Review Comment:
   No need to modify due to [function.cpp:304](https://github.com/apache/incubator-doris/blob/master/be/src/vec/functions/function.cpp#L304) will add nullable to the type returned.



##########
be/src/vec/functions/array/function_array_aggregation.cpp:
##########
@@ -0,0 +1,300 @@
+// 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 <type_traits>
+
+#include "vec/aggregate_functions/aggregate_function_avg.h"
+#include "vec/aggregate_functions/aggregate_function_min_max.h"
+#include "vec/aggregate_functions/aggregate_function_null.h"
+#include "vec/aggregate_functions/aggregate_function_product.h"
+#include "vec/aggregate_functions/aggregate_function_sum.h"
+#include "vec/aggregate_functions/helpers.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/arena.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/array/function_array_mapped.h"
+#include "vec/functions/simple_function_factory.h"
+
+namespace doris {
+namespace vectorized {
+
+enum class AggregateOperation { MIN, MAX, SUM, AVERAGE, PRODUCT };
+
+template <typename Element, AggregateOperation operation>
+struct ArrayAggregateResultImpl;
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MIN> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MAX> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::AVERAGE> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::PRODUCT> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::SUM> {
+    using Result = std::conditional_t<
+            IsDecimalNumber<Element>, Decimal128,
+            std::conditional_t<IsFloatNumber<Element>, Float64,
+                               std::conditional_t<std::is_same_v<Element, Int128>, Int128, Int64>>>;
+};
+
+template <typename Element, AggregateOperation operation>
+using ArrayAggregateResult = typename ArrayAggregateResultImpl<Element, operation>::Result;
+
+template <AggregateOperation operation>
+struct AggregateFunctionImpl;
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::SUM> {

Review Comment:
   Added.



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r895713917


##########
be/src/vec/aggregate_functions/aggregate_function_product.h:
##########
@@ -0,0 +1,137 @@
+// 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.
+
+#pragma once
+
+#include <cstddef>
+#include <type_traits>
+
+#include "vec/aggregate_functions/aggregate_function.h"
+#include "vec/columns/column_decimal.h"
+#include "vec/columns/column_vector.h"
+#include "vec/common/arena.h"
+#include "vec/common/string_buffer.hpp"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type_decimal.h"
+#include "vec/data_types/data_type_number.h"
+#include "vec/io/io_helper.h"
+
+namespace doris {
+namespace vectorized {
+
+template <typename T>
+struct AggregateFunctionProductData {
+    T product {};
+    bool has_data = false;

Review Comment:
   Removed.



##########
be/src/vec/functions/array/function_array_aggregation.cpp:
##########
@@ -0,0 +1,300 @@
+// 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 <type_traits>
+
+#include "vec/aggregate_functions/aggregate_function_avg.h"
+#include "vec/aggregate_functions/aggregate_function_min_max.h"
+#include "vec/aggregate_functions/aggregate_function_null.h"
+#include "vec/aggregate_functions/aggregate_function_product.h"
+#include "vec/aggregate_functions/aggregate_function_sum.h"
+#include "vec/aggregate_functions/helpers.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/arena.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/array/function_array_mapped.h"
+#include "vec/functions/simple_function_factory.h"
+
+namespace doris {
+namespace vectorized {
+
+enum class AggregateOperation { MIN, MAX, SUM, AVERAGE, PRODUCT };
+
+template <typename Element, AggregateOperation operation>
+struct ArrayAggregateResultImpl;
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MIN> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::MAX> {
+    using Result = Element;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::AVERAGE> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::PRODUCT> {
+    using Result = std::conditional_t<IsDecimalNumber<Element>, Decimal128, Float64>;
+};
+
+template <typename Element>
+struct ArrayAggregateResultImpl<Element, AggregateOperation::SUM> {
+    using Result = std::conditional_t<
+            IsDecimalNumber<Element>, Decimal128,
+            std::conditional_t<IsFloatNumber<Element>, Float64,
+                               std::conditional_t<std::is_same_v<Element, Int128>, Int128, Int64>>>;
+};
+
+template <typename Element, AggregateOperation operation>
+using ArrayAggregateResult = typename ArrayAggregateResultImpl<Element, operation>::Result;
+
+template <AggregateOperation operation>
+struct AggregateFunctionImpl;
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::SUM> {
+    template <typename Element>
+    struct TypeTraits {
+        using ResultType = ArrayAggregateResult<Element, AggregateOperation::SUM>;
+        using AggregateDataType = AggregateFunctionSumData<ResultType>;
+        using Function = AggregateFunctionSum<Element, ResultType, AggregateDataType>;
+    };
+};
+
+template <>
+struct AggregateFunctionImpl<AggregateOperation::AVERAGE> {
+    template <typename Element>
+    struct TypeTraits {
+        struct AggregateDataType : public AggregateFunctionAvgData<Element> {

Review Comment:
   Removed.



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r852010828


##########
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) \

Review Comment:
   Refined.
   Use `enum` variables to indicate the different results. 



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] cambyzju commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
cambyzju commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r855986107


##########
be/src/vec/functions/array/function_array_aggregation.cpp:
##########
@@ -0,0 +1,312 @@
+// 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 {
+
+enum class AggregationStatus {
+    MISMATCHED_TYPE = 0,
+    OK,
+    MATH_OVERFLOW,
+    SCALE_OUT_OF_BOUNDS,
+};
+
+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 get_return_type(const DataTypePtr& expression_return,
+                                       const DataTypePtr& /*array_element*/) {
+        DataTypePtr result;
+
+        auto call = [&expression_return, &result](const auto& types) {
+            using Types = std::decay_t<decltype(types)>;
+            using DataType = typename Types::LeftType;
+
+            if constexpr (!IsDataTypeDecimalOrNumber<DataType>) {
+                return false;
+            } else if constexpr (aggregate_operation == AggregateOperation::AVERAGE ||
+                                 aggregate_operation == AggregateOperation::PRODUCT ||
+                                 IsDataTypeNumber<DataType>) {
+                using NumberReturnType =
+                        ArrayAggregateResult<typename DataType::FieldType, aggregate_operation>;
+                result = std::make_shared<DataTypeNumber<NumberReturnType>>();
+                return true;
+            } else if constexpr (IsDataTypeDecimal<DataType>) {
+                using DecimalReturnType =
+                        ArrayAggregateResult<typename DataType::FieldType, aggregate_operation>;
+                UInt32 scale = get_decimal_scale(*expression_return);
+                result = std::make_shared<DataTypeDecimal<DecimalReturnType>>(
+                        DataTypeDecimal<DecimalReturnType>::max_precision(), scale);
+                return true;
+            }
+            return false;
+        };
+        if (!call_on_index_and_data_type<void>(expression_return->get_type_id(), call)) {
+            LOG(WARNING) << "array aggregation function cannot be performed on type "
+                         << expression_return->get_name();
+        }
+        return result;
+    }
+
+    static Status execute(Block& block, size_t result, const ColumnArray& array, ColumnPtr mapped) {
+        const IColumn::Offsets& offsets = array.get_offsets();
+        ColumnPtr res;
+        AggregationStatus status;
+        if (static_cast<bool>(status = execute_type<Int8>(res, mapped, offsets)) ||
+            static_cast<bool>(status = execute_type<Int16>(res, mapped, offsets)) ||
+            static_cast<bool>(status = execute_type<Int32>(res, mapped, offsets)) ||
+            static_cast<bool>(status = execute_type<Int64>(res, mapped, offsets)) ||
+            static_cast<bool>(status = execute_type<Int128>(res, mapped, offsets)) ||
+            static_cast<bool>(status = execute_type<Float32>(res, mapped, offsets)) ||
+            static_cast<bool>(status = execute_type<Float64>(res, mapped, offsets)) ||
+            static_cast<bool>(status = execute_type<Decimal32>(res, mapped, offsets)) ||
+            static_cast<bool>(status = execute_type<Decimal64>(res, mapped, offsets)) ||

Review Comment:
   doris only support Decimal128



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r852010828


##########
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) \

Review Comment:
   Refined.
   Use `enum` variables to indicate the different results. 



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] cambyzju commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
cambyzju commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r851852575


##########
be/src/vec/functions/array/function_array_mapped.h:
##########
@@ -0,0 +1,106 @@
+// 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/FunctionArrayMapped.h
+// and modified by Doris
+
+#include "vec/columns/column_nullable.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/function.h"
+#include "vec/functions/function_helpers.h"
+
+namespace doris {
+namespace vectorized {
+
+class ColumnArray;
+class DataTypeArray;
+
+template <typename T>
+ColumnPtr getOffsetsPtr(const T& column) {

Review Comment:
   function name change to get_offsets_ptr



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] HappenLee commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r892413523


##########
be/src/vec/functions/array/function_array_mapped.h:
##########
@@ -0,0 +1,77 @@
+// 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/FunctionArrayMapped.h
+// and modified by Doris
+
+#include "vec/columns/column.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/function.h"
+
+namespace doris {
+namespace vectorized {
+
+/** Higher-order functions for arrays.
+  * These functions optionally apply a map (transform) to array (or multiple arrays of identical size) by lambda function,
+  *  and return some result based on that transformation.
+  *
+  * Examples:
+  * arrayMap(x1,...,xn -> expression, array1,...,arrayn) - apply the expression to each element of the array (or set of parallel arrays).
+  * arrayFilter(x -> predicate, array) - leave in the array only the elements for which the expression is true.
+  *
+  * For some functions arrayCount, arrayExists, arrayAll, an overload of the form f(array) is available,
+  *  which works in the same way as f(x -> x, array).
+  *
+  * See the example of Impl template parameter in arrayMap.cpp
+  */
+template <typename Impl, typename Name>
+class FunctionArrayMapped : public IFunction {
+public:
+    static constexpr auto name = Name::name;
+    static FunctionPtr create() { return std::make_shared<FunctionArrayMapped>(); }
+
+    String get_name() const override { return name; }
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        const auto& typed_column = block.get_by_position(arguments[0]);
+        const typename Impl::column_type* column_array;
+        if (typed_column.column->is_nullable()) {

Review Comment:
   if pass a nullable column ? do nothing with `NULL` value?



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] HappenLee commented on pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
HappenLee commented on PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#issuecomment-1103839421

   I think we should reuse the logic and code of all AggregateionFunction we already impl. 
   Don't repeat yourself !


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] HappenLee commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r853825387


##########
be/src/vec/functions/array/function_array_aggregation.cpp:
##########
@@ -0,0 +1,312 @@
+// 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 {
+
+enum class AggregationStatus {
+    MISMATCHED_TYPE = 0,
+    OK,
+    MATH_OVERFLOW,
+    SCALE_OUT_OF_BOUNDS,
+};
+
+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<

Review Comment:
   Result = std::conditional_t<IsDecimalNumber<T>, Decimal128, NearestFieldType<T>>;



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] yangzhg commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
yangzhg commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r851852467


##########
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) \

Review Comment:
   Is this macro necessary? Just reduce two line to one?



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] HappenLee commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r851857461


##########
gensrc/script/doris_builtins_functions.py:
##########
@@ -176,6 +176,82 @@
         '_ZN5doris10vectorized18FunctionArrayIndexINS0_19ArrayPositionActionENS0_17NameArrayPositionEE12execute_implEPN9doris_udf15FunctionContextERNS0_5BlockERKSt6vectorImSaImEEmm',
         '', '', 'vec', ''],
 
+    [['array_min'], 'TINYINT', ['ARRAY_TINYINT'],
+        '_ZN5doris10vectorized19FunctionArrayMappedINS0_18ArrayAggregateImplILNS0_18AggregateOperationE0EEENS0_12NameArrayMinEE12execute_implEPN9doris_udf15FunctionContextERNS0_5BlockERKSt6vectorImSaImEEmm',

Review Comment:
   the array function only support in `vec` query engine. maybe no need to add symbol?



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] HappenLee commented on pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
HappenLee commented on PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#issuecomment-1149936408

   please add doc for this function


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] adonis0147 commented on a diff in pull request #9056: [feature-wip](array-type) Add array aggregation functions

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #9056:
URL: https://github.com/apache/incubator-doris/pull/9056#discussion_r892647180


##########
be/src/vec/functions/array/function_array_mapped.h:
##########
@@ -0,0 +1,77 @@
+// 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/FunctionArrayMapped.h
+// and modified by Doris
+
+#include "vec/columns/column.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/function.h"
+
+namespace doris {
+namespace vectorized {
+
+/** Higher-order functions for arrays.
+  * These functions optionally apply a map (transform) to array (or multiple arrays of identical size) by lambda function,
+  *  and return some result based on that transformation.
+  *
+  * Examples:
+  * arrayMap(x1,...,xn -> expression, array1,...,arrayn) - apply the expression to each element of the array (or set of parallel arrays).
+  * arrayFilter(x -> predicate, array) - leave in the array only the elements for which the expression is true.
+  *
+  * For some functions arrayCount, arrayExists, arrayAll, an overload of the form f(array) is available,
+  *  which works in the same way as f(x -> x, array).
+  *
+  * See the example of Impl template parameter in arrayMap.cpp
+  */
+template <typename Impl, typename Name>
+class FunctionArrayMapped : public IFunction {
+public:
+    static constexpr auto name = Name::name;
+    static FunctionPtr create() { return std::make_shared<FunctionArrayMapped>(); }
+
+    String get_name() const override { return name; }
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        const auto& typed_column = block.get_by_position(arguments[0]);
+        const typename Impl::column_type* column_array;
+        if (typed_column.column->is_nullable()) {

Review Comment:
   `NULL` values will be skipped.



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org