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


##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_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 left = 
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        auto* src_arr = 
assert_cast<ColumnArray*>(remove_nullable(left)->assume_mutable().get());
+
+        ColumnPtr k_col =
+                
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
+
+        Int64 k = k_col->get_int(0);
+
+        const auto& offsets =
+                
assert_cast<ColumnArray::ColumnOffsets&>(src_arr->get_offsets_column());
+
+        ColumnPtr res = _execute_combination(src_arr, input_rows_count, 
offsets, k);
+        block.replace_by_position(result, std::move(res));
+        return Status::OK();
+    }
+
+private:
+    size_t _combination_count(size_t array_length, size_t k) const {
+        size_t combinations = 1;
+        for (int i = 1; i <= k; i++) {
+            combinations = combinations * (array_length - k + i) / i;
+        }
+        return combinations;
+    }
+
+    std::vector<size_t> _first_combination(Int64 k, size_t length) const {
+        std::vector<size_t> comb(k + 1);
+        for (size_t i = 0; i < static_cast<size_t>(k); ++i) {
+            comb[i] = i;
+        }
+        comb[k] = length;
+        return comb;
+    }
+
+    bool _next_combination(std::vector<size_t>& comb, Int64 k) const {

Review Comment:
   what's the meaning of I, j, k? dont use those meaningless identifier



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_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 left = 
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();

Review Comment:
   dont directly use `convert_to_full_column_if_const`, but `vector_const`...



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_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 left = 
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        auto* src_arr = 
assert_cast<ColumnArray*>(remove_nullable(left)->assume_mutable().get());

Review Comment:
   why need `assume_mutable` here?



##########
regression-test/suites/query_p0/sql_functions/array_functions/array_combinations.groovy:
##########
@@ -0,0 +1,39 @@
+// 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.
+
+suite("array_combinations") {
+    sql """DROP TABLE IF EXISTS t_array_combinations"""
+    sql """
+            CREATE TABLE IF NOT EXISTS t_array_combinations (
+              `k1` int(11) NULL COMMENT "",
+              `s1` array<string> NULL COMMENT "",
+              `a1` array<tinyint(4)> NULL COMMENT "",
+              `a2` array<largeint(40)> NULL COMMENT "",
+              `aa1` array<array<int(11)>> NOT NULL COMMENT "",
+            ) ENGINE=OLAP
+            DISTRIBUTED BY HASH(`k1`) BUCKETS 1
+            PROPERTIES (
+            "replication_allocation" = "tag.location.default: 1",
+            "storage_format" = "V2"
+            )
+        """
+    sql """ INSERT INTO t_array_combinations VALUES(1, ['foo','bar','baz'], 
[1,2,3], [1,2,2], [[1,1],[4,5],[1,4]]) """
+
+    qt_test """
+    select k1, array_combinations(s1, 2), array_combinations(a1, 2), 
array_combinations(a2, 2), array_combinations(aa1,  2) from 
t_array_combinations order by k1;

Review Comment:
   there's too few testcases. should add more to cover situations



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_nullable(array_type->get_nested_type()));

Review Comment:
   why remove then make? directly make is ok?



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_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 left = 
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        auto* src_arr = 
assert_cast<ColumnArray*>(remove_nullable(left)->assume_mutable().get());
+
+        ColumnPtr k_col =
+                
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
+
+        Int64 k = k_col->get_int(0);
+
+        const auto& offsets =
+                
assert_cast<ColumnArray::ColumnOffsets&>(src_arr->get_offsets_column());
+
+        ColumnPtr res = _execute_combination(src_arr, input_rows_count, 
offsets, k);
+        block.replace_by_position(result, std::move(res));
+        return Status::OK();
+    }
+
+private:
+    size_t _combination_count(size_t array_length, size_t k) const {
+        size_t combinations = 1;
+        for (int i = 1; i <= k; i++) {
+            combinations = combinations * (array_length - k + i) / i;
+        }
+        return combinations;
+    }
+
+    std::vector<size_t> _first_combination(Int64 k, size_t length) const {
+        std::vector<size_t> comb(k + 1);
+        for (size_t i = 0; i < static_cast<size_t>(k); ++i) {
+            comb[i] = i;
+        }
+        comb[k] = length;
+        return comb;
+    }
+
+    bool _next_combination(std::vector<size_t>& comb, Int64 k) const {
+        for (size_t i = 0; i < static_cast<size_t>(k); ++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;
+    }
+
+    ColumnPtr _execute_combination(const ColumnArray* nested, size_t 
input_rows_count,
+                                   const ColumnArray::ColumnOffsets& offsets, 
Int64 k) const {
+        const auto& data_col = nested->get_data();

Review Comment:
   you can reserve for result column



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/ArrayCombinations.java:
##########
@@ -19,51 +19,50 @@
 
 import org.apache.doris.catalog.FunctionSignature;
 import org.apache.doris.nereids.trees.expressions.Expression;
-import 
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
+import org.apache.doris.nereids.trees.expressions.functions.CustomSignature;
 import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
-import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
 import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
-import org.apache.doris.nereids.types.QuantileStateType;
-import org.apache.doris.nereids.types.StringType;
+import org.apache.doris.nereids.types.ArrayType;
+import org.apache.doris.nereids.types.DataType;
 
 import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
 
 import java.util.List;
 
 /**
- * Function 'quantile_state_to_base64'.
+ * ScalarFunction 'combinations'
  */
-public class QuantileStateToBase64 extends ScalarFunction
-        implements UnaryExpression, ExplicitlyCastableSignature, 
PropagateNullable {
-
-    public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
-            
FunctionSignature.ret(StringType.INSTANCE).args(QuantileStateType.INSTANCE)
-    );
+public class ArrayCombinations extends ScalarFunction
+        implements CustomSignature, PropagateNullable {
 
     /**
-     * constructor with 1 argument.
+     * constructor with 2 arguments.
      */
-    public QuantileStateToBase64(Expression arg) {
-        super("quantile_state_to_base64", arg);
+    public ArrayCombinations(Expression arg0, Expression arg1) {
+        super("array_combinations", arg0, arg1);
     }
 
-    /**
-     * withChildren.
-     */
     @Override
-    public QuantileStateToBase64 withChildren(List<Expression> children) {
-        Preconditions.checkArgument(children.size() == 1);
-        return new QuantileStateToBase64(children.get(0));
+    public FunctionSignature customSignature() {
+        DataType arg0Type = getArgument(0).getDataType();
+        Preconditions.checkArgument(arg0Type instanceof ArrayType,
+                "array_combinations first argument must be Array");
+        DataType itemType = ((ArrayType) arg0Type).getItemType();
+        return FunctionSignature.ret(ArrayType.of(ArrayType.of(itemType)))
+            .args(getArgument(0).getDataType(), getArgument(1).getDataType());

Review Comment:
   what if arg1 is not number?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/ArrayCombinations.java:
##########
@@ -19,51 +19,50 @@
 
 import org.apache.doris.catalog.FunctionSignature;
 import org.apache.doris.nereids.trees.expressions.Expression;
-import 
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
+import org.apache.doris.nereids.trees.expressions.functions.CustomSignature;
 import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
-import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
 import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
-import org.apache.doris.nereids.types.QuantileStateType;
-import org.apache.doris.nereids.types.StringType;
+import org.apache.doris.nereids.types.ArrayType;
+import org.apache.doris.nereids.types.DataType;
 
 import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
 
 import java.util.List;
 
 /**
- * Function 'quantile_state_to_base64'.
+ * ScalarFunction 'combinations'
  */
-public class QuantileStateToBase64 extends ScalarFunction
-        implements UnaryExpression, ExplicitlyCastableSignature, 
PropagateNullable {
-
-    public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
-            
FunctionSignature.ret(StringType.INSTANCE).args(QuantileStateType.INSTANCE)
-    );
+public class ArrayCombinations extends ScalarFunction
+        implements CustomSignature, PropagateNullable {
 
     /**
-     * constructor with 1 argument.
+     * constructor with 2 arguments.
      */
-    public QuantileStateToBase64(Expression arg) {
-        super("quantile_state_to_base64", arg);
+    public ArrayCombinations(Expression arg0, Expression arg1) {
+        super("array_combinations", arg0, arg1);
     }
 
-    /**
-     * withChildren.
-     */
     @Override
-    public QuantileStateToBase64 withChildren(List<Expression> children) {
-        Preconditions.checkArgument(children.size() == 1);
-        return new QuantileStateToBase64(children.get(0));
+    public FunctionSignature customSignature() {

Review Comment:
   why not `getSignature`? your way now will skip some passes in 
`ComputeSignature` presets.



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_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 left = 
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        auto* src_arr = 
assert_cast<ColumnArray*>(remove_nullable(left)->assume_mutable().get());
+
+        ColumnPtr k_col =
+                
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
+
+        Int64 k = k_col->get_int(0);

Review Comment:
   should this function restrict the second arg to be const?



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());

Review Comment:
   getting a pointer and not checking result is wrong. maybe `assert_cast`



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_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 left = 
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        auto* src_arr = 
assert_cast<ColumnArray*>(remove_nullable(left)->assume_mutable().get());
+
+        ColumnPtr k_col =
+                
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
+
+        Int64 k = k_col->get_int(0);
+
+        const auto& offsets =
+                
assert_cast<ColumnArray::ColumnOffsets&>(src_arr->get_offsets_column());
+
+        ColumnPtr res = _execute_combination(src_arr, input_rows_count, 
offsets, k);
+        block.replace_by_position(result, std::move(res));
+        return Status::OK();
+    }
+
+private:
+    size_t _combination_count(size_t array_length, size_t k) const {
+        size_t combinations = 1;
+        for (int i = 1; i <= k; i++) {
+            combinations = combinations * (array_length - k + i) / i;
+        }
+        return combinations;
+    }
+
+    std::vector<size_t> _first_combination(Int64 k, size_t length) const {

Review Comment:
   maybe `ALWAYS_INLINE` this function to make sure no copy of vector occurs 
here.



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_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 left = 
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        auto* src_arr = 
assert_cast<ColumnArray*>(remove_nullable(left)->assume_mutable().get());
+
+        ColumnPtr k_col =
+                
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
+
+        Int64 k = k_col->get_int(0);
+
+        const auto& offsets =
+                
assert_cast<ColumnArray::ColumnOffsets&>(src_arr->get_offsets_column());
+
+        ColumnPtr res = _execute_combination(src_arr, input_rows_count, 
offsets, k);
+        block.replace_by_position(result, std::move(res));
+        return Status::OK();
+    }
+
+private:
+    size_t _combination_count(size_t array_length, size_t k) const {
+        size_t combinations = 1;
+        for (int i = 1; i <= k; i++) {
+            combinations = combinations * (array_length - k + i) / i;
+        }
+        return combinations;
+    }
+
+    std::vector<size_t> _first_combination(Int64 k, size_t length) const {
+        std::vector<size_t> comb(k + 1);
+        for (size_t i = 0; i < static_cast<size_t>(k); ++i) {
+            comb[i] = i;
+        }
+        comb[k] = length;
+        return comb;
+    }
+
+    bool _next_combination(std::vector<size_t>& comb, Int64 k) const {
+        for (size_t i = 0; i < static_cast<size_t>(k); ++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;
+    }
+
+    ColumnPtr _execute_combination(const ColumnArray* nested, size_t 
input_rows_count,
+                                   const ColumnArray::ColumnOffsets& offsets, 
Int64 k) const {
+        const auto& data_col = nested->get_data();
+        const auto& in_offs = offsets.get_data();
+
+        auto inner_data = data_col.clone_empty();
+        auto inner_offsets = ColumnArray::ColumnOffsets::create();
+        auto inner_arr = ColumnArray::create(std::move(inner_data), 
std::move(inner_offsets));
+        auto* inner = assert_cast<ColumnArray*>(inner_arr.get());
+
+        auto outer_offsets = ColumnArray::ColumnOffsets::create();
+        auto& outer_offs = outer_offsets->get_data();
+        outer_offs.resize(input_rows_count);
+
+        Field element;
+        size_t prev_off = 0, outer_off = 0;
+
+        for (size_t row = 0; row < input_rows_count; ++row) {
+            size_t curr_off = in_offs[row];
+            size_t row_len = curr_off - prev_off;
+
+            if (k <= 0 || static_cast<size_t>(k) > row_len) {

Review Comment:
   add reg case and make sure this behaviour is same with target system



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_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 left = 
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        auto* src_arr = 
assert_cast<ColumnArray*>(remove_nullable(left)->assume_mutable().get());
+
+        ColumnPtr k_col =
+                
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
+
+        Int64 k = k_col->get_int(0);
+
+        const auto& offsets =
+                
assert_cast<ColumnArray::ColumnOffsets&>(src_arr->get_offsets_column());
+
+        ColumnPtr res = _execute_combination(src_arr, input_rows_count, 
offsets, k);
+        block.replace_by_position(result, std::move(res));
+        return Status::OK();
+    }
+
+private:
+    size_t _combination_count(size_t array_length, size_t k) const {
+        size_t combinations = 1;
+        for (int i = 1; i <= k; i++) {
+            combinations = combinations * (array_length - k + i) / i;
+        }
+        return combinations;
+    }
+
+    std::vector<size_t> _first_combination(Int64 k, size_t length) const {
+        std::vector<size_t> comb(k + 1);
+        for (size_t i = 0; i < static_cast<size_t>(k); ++i) {
+            comb[i] = i;
+        }
+        comb[k] = length;
+        return comb;
+    }
+
+    bool _next_combination(std::vector<size_t>& comb, Int64 k) const {
+        for (size_t i = 0; i < static_cast<size_t>(k); ++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;
+    }
+
+    ColumnPtr _execute_combination(const ColumnArray* nested, size_t 
input_rows_count,
+                                   const ColumnArray::ColumnOffsets& offsets, 
Int64 k) const {
+        const auto& data_col = nested->get_data();
+        const auto& in_offs = offsets.get_data();
+
+        auto inner_data = data_col.clone_empty();
+        auto inner_offsets = ColumnArray::ColumnOffsets::create();
+        auto inner_arr = ColumnArray::create(std::move(inner_data), 
std::move(inner_offsets));
+        auto* inner = assert_cast<ColumnArray*>(inner_arr.get());
+
+        auto outer_offsets = ColumnArray::ColumnOffsets::create();
+        auto& outer_offs = outer_offsets->get_data();
+        outer_offs.resize(input_rows_count);
+
+        Field element;
+        size_t prev_off = 0, outer_off = 0;
+
+        for (size_t row = 0; row < input_rows_count; ++row) {
+            size_t curr_off = in_offs[row];
+            size_t row_len = curr_off - prev_off;
+
+            if (k <= 0 || static_cast<size_t>(k) > row_len) {
+                outer_offs[row] = outer_off;
+                prev_off = curr_off;
+                continue;
+            }
+
+            std::vector comb = _first_combination(k, row_len);
+
+            for (int i = 0; i < static_cast<size_t>(k); ++i) {

Review Comment:
   why put a single same for-loop outside the while-loop? maybe you need a 
do-while?



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_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 left = 
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        auto* src_arr = 
assert_cast<ColumnArray*>(remove_nullable(left)->assume_mutable().get());
+
+        ColumnPtr k_col =
+                
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
+
+        Int64 k = k_col->get_int(0);
+
+        const auto& offsets =
+                
assert_cast<ColumnArray::ColumnOffsets&>(src_arr->get_offsets_column());
+
+        ColumnPtr res = _execute_combination(src_arr, input_rows_count, 
offsets, k);
+        block.replace_by_position(result, std::move(res));
+        return Status::OK();
+    }
+
+private:
+    size_t _combination_count(size_t array_length, size_t k) const {
+        size_t combinations = 1;
+        for (int i = 1; i <= k; i++) {
+            combinations = combinations * (array_length - k + i) / i;

Review Comment:
   add comment to explain this



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_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 left = 
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        auto* src_arr = 
assert_cast<ColumnArray*>(remove_nullable(left)->assume_mutable().get());
+
+        ColumnPtr k_col =
+                
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
+
+        Int64 k = k_col->get_int(0);
+
+        const auto& offsets =
+                
assert_cast<ColumnArray::ColumnOffsets&>(src_arr->get_offsets_column());
+
+        ColumnPtr res = _execute_combination(src_arr, input_rows_count, 
offsets, k);
+        block.replace_by_position(result, std::move(res));
+        return Status::OK();
+    }
+
+private:
+    size_t _combination_count(size_t array_length, size_t k) const {
+        size_t combinations = 1;
+        for (int i = 1; i <= k; i++) {
+            combinations = combinations * (array_length - k + i) / i;
+        }
+        return combinations;
+    }
+
+    std::vector<size_t> _first_combination(Int64 k, size_t length) const {
+        std::vector<size_t> comb(k + 1);
+        for (size_t i = 0; i < static_cast<size_t>(k); ++i) {
+            comb[i] = i;
+        }
+        comb[k] = length;
+        return comb;
+    }
+
+    bool _next_combination(std::vector<size_t>& comb, Int64 k) const {
+        for (size_t i = 0; i < static_cast<size_t>(k); ++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;
+    }
+
+    ColumnPtr _execute_combination(const ColumnArray* nested, size_t 
input_rows_count,
+                                   const ColumnArray::ColumnOffsets& offsets, 
Int64 k) const {
+        const auto& data_col = nested->get_data();

Review Comment:
   re-consider all your var names



##########
be/src/vec/functions/array/function_array_combinations.cpp:
##########
@@ -0,0 +1,163 @@
+// 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/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/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 = 
check_and_get_data_type<DataTypeArray>(arguments[0].get());
+        auto elem_t = 
make_nullable(remove_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 left = 
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        auto* src_arr = 
assert_cast<ColumnArray*>(remove_nullable(left)->assume_mutable().get());
+
+        ColumnPtr k_col =
+                
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
+
+        Int64 k = k_col->get_int(0);
+
+        const auto& offsets =
+                
assert_cast<ColumnArray::ColumnOffsets&>(src_arr->get_offsets_column());
+
+        ColumnPtr res = _execute_combination(src_arr, input_rows_count, 
offsets, k);
+        block.replace_by_position(result, std::move(res));
+        return Status::OK();
+    }
+
+private:
+    size_t _combination_count(size_t array_length, size_t k) const {
+        size_t combinations = 1;
+        for (int i = 1; i <= k; i++) {
+            combinations = combinations * (array_length - k + i) / i;
+        }
+        return combinations;
+    }
+
+    std::vector<size_t> _first_combination(Int64 k, size_t length) const {
+        std::vector<size_t> comb(k + 1);
+        for (size_t i = 0; i < static_cast<size_t>(k); ++i) {
+            comb[i] = i;
+        }
+        comb[k] = length;
+        return comb;
+    }
+
+    bool _next_combination(std::vector<size_t>& comb, Int64 k) const {
+        for (size_t i = 0; i < static_cast<size_t>(k); ++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;
+    }
+
+    ColumnPtr _execute_combination(const ColumnArray* nested, size_t 
input_rows_count,
+                                   const ColumnArray::ColumnOffsets& offsets, 
Int64 k) const {
+        const auto& data_col = nested->get_data();
+        const auto& in_offs = offsets.get_data();
+
+        auto inner_data = data_col.clone_empty();
+        auto inner_offsets = ColumnArray::ColumnOffsets::create();
+        auto inner_arr = ColumnArray::create(std::move(inner_data), 
std::move(inner_offsets));
+        auto* inner = assert_cast<ColumnArray*>(inner_arr.get());
+
+        auto outer_offsets = ColumnArray::ColumnOffsets::create();
+        auto& outer_offs = outer_offsets->get_data();
+        outer_offs.resize(input_rows_count);
+
+        Field element;
+        size_t prev_off = 0, outer_off = 0;
+
+        for (size_t row = 0; row < input_rows_count; ++row) {
+            size_t curr_off = in_offs[row];
+            size_t row_len = curr_off - prev_off;
+
+            if (k <= 0 || static_cast<size_t>(k) > row_len) {
+                outer_offs[row] = outer_off;
+                prev_off = curr_off;
+                continue;
+            }
+
+            std::vector comb = _first_combination(k, row_len);
+
+            for (int i = 0; i < static_cast<size_t>(k); ++i) {
+                size_t idx = prev_off + comb[i];
+                data_col.get(idx, element);
+                inner->get_data().insert(element);

Review Comment:
   directly use `insert_from` could get rid of `Field`?



-- 
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