zclllyybb commented on code in PR #60192:
URL: https://github.com/apache/doris/pull/60192#discussion_r2786453144


##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,184 @@
+// 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 "common/compiler_util.h"
+#include "common/logging.h"
+#include "common/status.h"
+#include "runtime/define_primitive_type.h"
+#include "runtime/primitive_type.h"
+#include "vec/columns/column.h"
+#include "vec/columns/column_array.h"
+#include "vec/columns/column_const.h"
+#include "vec/common/assert_cast.h"
+#include "vec/core/field.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_decimal.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/functions/function.h"
+#include "vec/functions/function_helpers.h"
+#include "vec/functions/simple_function_factory.h"
+
+namespace doris::vectorized {
+// array_combinations([1, 2, 3],2) -> [[1,2], [1,3], [2,3]]
+// array_combinations([1, NULL, 3, NULL, 5],4) -> [[1,NULL,3,NULL], 
[1,NULL,3,5], [NULL,3,NULL,5]]
+
+class FunctionArrayCombinations : public IFunction {
+public:
+    static constexpr auto name = "array_combinations";
+    static FunctionPtr create() { return 
std::make_shared<FunctionArrayCombinations>(); }
+    bool is_variadic() const override { return false; }
+    String get_name() const override { return name; }
+
+    size_t get_number_of_arguments() const override { return 2; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        const auto* array_type = assert_cast<const 
DataTypeArray*>(arguments[0].get());
+        auto elem_t = make_nullable(array_type->get_nested_type());
+        auto res = std::make_shared<DataTypeArray>(
+                make_nullable(std::make_shared<DataTypeArray>(elem_t)));
+        return res;
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        auto array = block.get_by_position(arguments[0]).column;
+        ColumnPtr num = block.get_by_position(arguments[1]).column;
+
+        Int64 combination_length = num->get_int(0);
+
+        if (combination_length > MAX_COMBINATION_LENGTH || combination_length 
< 0) {
+            return Status::RuntimeError(
+                    fmt::format("execute failed, function {}'s second argument 
must be bigger than "
+                                "0 and not bigger than 5",
+                                get_name()));
+        }
+
+        ColumnPtr res;
+        const auto* src_arr = assert_cast<const 
ColumnArray*>(remove_nullable(array).get());
+        const auto& offsets =
+                assert_cast<const 
ColumnArray::ColumnOffsets&>(src_arr->get_offsets_column());
+        Status status = vector_const(src_arr, input_rows_count, res, offsets, 
combination_length);
+        if (!status.ok()) {
+            return status;
+        }
+        block.replace_by_position(result, std::move(res));
+        return status;
+    }
+
+    ColumnNumbers get_arguments_that_are_always_constant() const override { 
return {1}; }
+
+private:
+    static const size_t MAX_COMBINATION_LENGTH = 5;
+    static const size_t MAX_COMBINATION_COUNT = 100000;
+
+    // Then combinationCount(n, k) = combinationCount(n-1, k-1) * n/k 
(https://en.wikipedia.org/wiki/Combination#Number_of_k-combinations)
+    // The formula is recursive. Here, instead of starting with 
k=combinationCount, n=arrayLength and recursing,
+    // we start with k=0 n=(arrayLength-combinationLength) and proceed "bottom 
up".
+    size_t _combination_count(size_t array_length, size_t combination_length) 
const {
+        size_t combinations = 1;
+
+        for (int i = 1; i <= combination_length; i++) {
+            combinations = combinations * (array_length - combination_length + 
i) / i;
+        }
+
+        return combinations;
+    }
+
+    ALWAYS_INLINE std::vector<size_t> _first_combination(Int64 
combination_length,
+                                                         size_t length) const {
+        std::vector<size_t> comb(combination_length + 1);
+        for (size_t i = 0; i < static_cast<size_t>(combination_length); ++i) {
+            comb[i] = i;
+        }
+        comb[combination_length] = length;
+        return comb;
+    }
+
+    // generate next combination
+    bool _next_combination(std::vector<size_t>& comb, Int64 
combination_length) const {
+        for (size_t i = 0; i < static_cast<size_t>(combination_length); ++i) {
+            if (comb[i] + 1 < comb[i + 1]) {
+                ++comb[i];
+                for (size_t j = 0; j < i; ++j) {
+                    comb[j] = j;
+                }
+                return true;
+            }
+        }
+        return false;
+    }
+
+    Status vector_const(const ColumnArray* nested_src_column_ptr, size_t 
input_rows_count,
+                        ColumnPtr& res, const ColumnArray::ColumnOffsets& 
offsets,
+                        Int64 combination_length) const {

Review Comment:
   to avoid many subsequent `static_cast`. make logic more concentrate



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to