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


##########
be/src/vec/functions/function_string.h:
##########
@@ -4957,6 +4957,103 @@ class FunctionXPathString : public IFunction {
     }
 };
 
+class FunctionMakeSet : public IFunction {
+public:
+    static constexpr auto name = "make_set";
+    static FunctionPtr create() { return std::make_shared<FunctionMakeSet>(); }
+    String get_name() const override { return name; }
+    size_t get_number_of_arguments() const override { return 0; }
+    bool is_variadic() const override { return true; }
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        if (arguments[0].get()->is_nullable()) {
+            return make_nullable(std::make_shared<DataTypeString>());
+        }
+        return std::make_shared<DataTypeString>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        auto res_col = ColumnString::create();
+        auto null_map = ColumnUInt8::create();
+
+        const auto& [bit_col, bit_const] =
+                unpack_if_const(block.get_by_position(arguments[0]).column);
+
+        if (bit_const) {
+            if (bit_col->is_null_at(0)) {
+                res_col->insert_many_defaults(input_rows_count);
+                null_map->insert_many_vals(1, input_rows_count);
+            } else {
+                const int64_t bit_data =
+                        assert_cast<const 
ColumnInt64*>(bit_col.get())->get_element(0);
+                vector_execute<true>(block, arguments, input_rows_count, 
*res_col, bit_data,
+                                     null_map->get_data());
+            }
+        } else if (const auto* bit_data = 
check_and_get_column<ColumnNullable>(bit_col.get())) {
+            null_map->insert_range_from(bit_data->get_null_map_column(), 0, 
input_rows_count);
+            vector_execute<false>(block, arguments, input_rows_count, *res_col,
+                                  assert_cast<const 
ColumnInt64&>(bit_data->get_nested_column()),
+                                  null_map->get_data());
+
+        } else {
+            null_map->get_data().resize_fill(input_rows_count, 0);
+            vector_execute<false>(block, arguments, input_rows_count, *res_col,
+                                  assert_cast<const 
ColumnInt64&>(*bit_col.get()),
+                                  null_map->get_data());
+        }
+
+        if (block.get_by_position(arguments[0]).type.get()->is_nullable()) {
+            block.replace_by_position(
+                    result, ColumnNullable::create(std::move(res_col), 
std::move(null_map)));
+        } else {
+            block.replace_by_position(result, std::move(res_col));
+        }
+        return Status::OK();
+    }
+
+private:
+    template <bool bit_const>
+    void vector_execute(const Block& block, const ColumnNumbers& arguments, 
size_t input_rows_count,
+                        ColumnString& res_col, const ColumnInt64& bit_col,
+                        PaddedPODArray<uint8_t>& null_map) const {
+        if constexpr (bit_const) {
+            int64_t bit = bit_col.get_element(0);
+            for (size_t i = 0; i < input_rows_count; ++i) {
+                execute_one_row(block, arguments, res_col, bit, i);
+            }
+        } else {
+            for (size_t i = 0; i < input_rows_count; ++i) {
+                if (null_map[i]) {
+                    res_col.insert_default();
+                    continue;
+                }
+                execute_one_row(block, arguments, res_col, 
bit_col.get_element(i), i);
+            }
+        }
+    }
+
+    void execute_one_row(const Block& block, const ColumnNumbers& arguments, 
ColumnString& res_col,
+                         int64_t bit, int row_num) const {
+        static constexpr char SEPARATOR = ',';
+        int64_t pos = __builtin_ffs(bit);
+        ColumnString::Chars data;
+        while (pos != 0 && pos < arguments.size() && bit != 0) {
+            auto col = block.get_by_position(arguments[pos]).column;
+            if (!col->is_null_at(row_num)) {
+                auto s_ref = col->get_data_at(row_num);
+                if (data.size()) {
+                    data.push_back(SEPARATOR);
+                }
+                data.insert(s_ref.data, s_ref.data + s_ref.size);
+            }
+            bit &= ~(1 << (pos - 1));

Review Comment:
   ```suggestion
               bit &= ~(1ULL << (pos - 1));
   ```



##########
be/src/vec/functions/function_string.h:
##########
@@ -4957,6 +4957,103 @@ class FunctionXPathString : public IFunction {
     }
 };
 
+class FunctionMakeSet : public IFunction {
+public:
+    static constexpr auto name = "make_set";
+    static FunctionPtr create() { return std::make_shared<FunctionMakeSet>(); }
+    String get_name() const override { return name; }
+    size_t get_number_of_arguments() const override { return 0; }
+    bool is_variadic() const override { return true; }
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        if (arguments[0].get()->is_nullable()) {
+            return make_nullable(std::make_shared<DataTypeString>());
+        }
+        return std::make_shared<DataTypeString>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        auto res_col = ColumnString::create();
+        auto null_map = ColumnUInt8::create();
+
+        const auto& [bit_col, bit_const] =
+                unpack_if_const(block.get_by_position(arguments[0]).column);
+
+        if (bit_const) {
+            if (bit_col->is_null_at(0)) {
+                res_col->insert_many_defaults(input_rows_count);
+                null_map->insert_many_vals(1, input_rows_count);
+            } else {
+                const int64_t bit_data =
+                        assert_cast<const 
ColumnInt64*>(bit_col.get())->get_element(0);
+                vector_execute<true>(block, arguments, input_rows_count, 
*res_col, bit_data,
+                                     null_map->get_data());
+            }
+        } else if (const auto* bit_data = 
check_and_get_column<ColumnNullable>(bit_col.get())) {
+            null_map->insert_range_from(bit_data->get_null_map_column(), 0, 
input_rows_count);
+            vector_execute<false>(block, arguments, input_rows_count, *res_col,
+                                  assert_cast<const 
ColumnInt64&>(bit_data->get_nested_column()),
+                                  null_map->get_data());
+
+        } else {
+            null_map->get_data().resize_fill(input_rows_count, 0);
+            vector_execute<false>(block, arguments, input_rows_count, *res_col,
+                                  assert_cast<const 
ColumnInt64&>(*bit_col.get()),
+                                  null_map->get_data());
+        }
+
+        if (block.get_by_position(arguments[0]).type.get()->is_nullable()) {
+            block.replace_by_position(
+                    result, ColumnNullable::create(std::move(res_col), 
std::move(null_map)));
+        } else {
+            block.replace_by_position(result, std::move(res_col));
+        }
+        return Status::OK();
+    }
+
+private:
+    template <bool bit_const>
+    void vector_execute(const Block& block, const ColumnNumbers& arguments, 
size_t input_rows_count,
+                        ColumnString& res_col, const ColumnInt64& bit_col,
+                        PaddedPODArray<uint8_t>& null_map) const {
+        if constexpr (bit_const) {
+            int64_t bit = bit_col.get_element(0);
+            for (size_t i = 0; i < input_rows_count; ++i) {
+                execute_one_row(block, arguments, res_col, bit, i);
+            }
+        } else {
+            for (size_t i = 0; i < input_rows_count; ++i) {
+                if (null_map[i]) {
+                    res_col.insert_default();
+                    continue;
+                }
+                execute_one_row(block, arguments, res_col, 
bit_col.get_element(i), i);
+            }
+        }
+    }
+
+    void execute_one_row(const Block& block, const ColumnNumbers& arguments, 
ColumnString& res_col,
+                         int64_t bit, int row_num) const {
+        static constexpr char SEPARATOR = ',';
+        int64_t pos = __builtin_ffs(bit);
+        ColumnString::Chars data;
+        while (pos != 0 && pos < arguments.size() && bit != 0) {
+            auto col = block.get_by_position(arguments[pos]).column;
+            if (!col->is_null_at(row_num)) {
+                auto s_ref = col->get_data_at(row_num);
+                if (data.size()) {

Review Comment:
   如果第一个元素是个空字符串呢?



##########
regression-test/suites/query_p0/sql_functions/string_functions/test_string_function.groovy:
##########


Review Comment:
   我们在P1里面加一个bit参数超过int32,在int64范围内的合理case吧



##########
be/src/vec/functions/function_string.h:
##########
@@ -4957,6 +4957,103 @@ class FunctionXPathString : public IFunction {
     }
 };
 
+class FunctionMakeSet : public IFunction {
+public:
+    static constexpr auto name = "make_set";
+    static FunctionPtr create() { return std::make_shared<FunctionMakeSet>(); }
+    String get_name() const override { return name; }
+    size_t get_number_of_arguments() const override { return 0; }
+    bool is_variadic() const override { return true; }
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        if (arguments[0].get()->is_nullable()) {
+            return make_nullable(std::make_shared<DataTypeString>());
+        }
+        return std::make_shared<DataTypeString>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        auto res_col = ColumnString::create();
+        auto null_map = ColumnUInt8::create();
+
+        const auto& [bit_col, bit_const] =
+                unpack_if_const(block.get_by_position(arguments[0]).column);
+
+        if (bit_const) {
+            if (bit_col->is_null_at(0)) {
+                res_col->insert_many_defaults(input_rows_count);
+                null_map->insert_many_vals(1, input_rows_count);
+            } else {
+                const int64_t bit_data =
+                        assert_cast<const 
ColumnInt64*>(bit_col.get())->get_element(0);
+                vector_execute<true>(block, arguments, input_rows_count, 
*res_col, bit_data,
+                                     null_map->get_data());
+            }
+        } else if (const auto* bit_data = 
check_and_get_column<ColumnNullable>(bit_col.get())) {
+            null_map->insert_range_from(bit_data->get_null_map_column(), 0, 
input_rows_count);
+            vector_execute<false>(block, arguments, input_rows_count, *res_col,
+                                  assert_cast<const 
ColumnInt64&>(bit_data->get_nested_column()),
+                                  null_map->get_data());
+
+        } else {
+            null_map->get_data().resize_fill(input_rows_count, 0);
+            vector_execute<false>(block, arguments, input_rows_count, *res_col,
+                                  assert_cast<const 
ColumnInt64&>(*bit_col.get()),
+                                  null_map->get_data());
+        }
+
+        if (block.get_by_position(arguments[0]).type.get()->is_nullable()) {
+            block.replace_by_position(
+                    result, ColumnNullable::create(std::move(res_col), 
std::move(null_map)));
+        } else {
+            block.replace_by_position(result, std::move(res_col));
+        }
+        return Status::OK();
+    }
+
+private:
+    template <bool bit_const>
+    void vector_execute(const Block& block, const ColumnNumbers& arguments, 
size_t input_rows_count,
+                        ColumnString& res_col, const ColumnInt64& bit_col,
+                        PaddedPODArray<uint8_t>& null_map) const {
+        if constexpr (bit_const) {
+            int64_t bit = bit_col.get_element(0);
+            for (size_t i = 0; i < input_rows_count; ++i) {
+                execute_one_row(block, arguments, res_col, bit, i);
+            }
+        } else {
+            for (size_t i = 0; i < input_rows_count; ++i) {
+                if (null_map[i]) {
+                    res_col.insert_default();
+                    continue;
+                }
+                execute_one_row(block, arguments, res_col, 
bit_col.get_element(i), i);
+            }
+        }
+    }
+
+    void execute_one_row(const Block& block, const ColumnNumbers& arguments, 
ColumnString& res_col,
+                         int64_t bit, int row_num) const {
+        static constexpr char SEPARATOR = ',';
+        int64_t pos = __builtin_ffs(bit);

Review Comment:
   builtin_ffs是32位的吧,得用64位版本?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/MakeSet.java:
##########
@@ -0,0 +1,78 @@
+// 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.
+
+package org.apache.doris.nereids.trees.expressions.functions.scalar;
+
+import org.apache.doris.catalog.FunctionSignature;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import 
org.apache.doris.nereids.trees.expressions.functions.ImplicitlyCastableSignature;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
+import org.apache.doris.nereids.types.BigIntType;
+import org.apache.doris.nereids.types.StringType;
+import org.apache.doris.nereids.types.VarcharType;
+import org.apache.doris.nereids.util.ExpressionUtils;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * ScalarFunction 'make_set'.
+ */
+public class MakeSet extends ScalarFunction implements 
ImplicitlyCastableSignature {

Review Comment:
   这个为何是ImplicitlyCastableSignature?而不是Explicitly的呢?



##########
be/src/vec/functions/function_string.h:
##########
@@ -4957,6 +4957,103 @@ class FunctionXPathString : public IFunction {
     }
 };
 
+class FunctionMakeSet : public IFunction {
+public:
+    static constexpr auto name = "make_set";
+    static FunctionPtr create() { return std::make_shared<FunctionMakeSet>(); }
+    String get_name() const override { return name; }
+    size_t get_number_of_arguments() const override { return 0; }
+    bool is_variadic() const override { return true; }
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        if (arguments[0].get()->is_nullable()) {
+            return make_nullable(std::make_shared<DataTypeString>());
+        }
+        return std::make_shared<DataTypeString>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        auto res_col = ColumnString::create();
+        auto null_map = ColumnUInt8::create();
+
+        const auto& [bit_col, bit_const] =
+                unpack_if_const(block.get_by_position(arguments[0]).column);
+
+        if (bit_const) {
+            if (bit_col->is_null_at(0)) {
+                res_col->insert_many_defaults(input_rows_count);
+                null_map->insert_many_vals(1, input_rows_count);
+            } else {
+                const int64_t bit_data =
+                        assert_cast<const 
ColumnInt64*>(bit_col.get())->get_element(0);
+                vector_execute<true>(block, arguments, input_rows_count, 
*res_col, bit_data,
+                                     null_map->get_data());
+            }
+        } else if (const auto* bit_data = 
check_and_get_column<ColumnNullable>(bit_col.get())) {
+            null_map->insert_range_from(bit_data->get_null_map_column(), 0, 
input_rows_count);
+            vector_execute<false>(block, arguments, input_rows_count, *res_col,
+                                  assert_cast<const 
ColumnInt64&>(bit_data->get_nested_column()),
+                                  null_map->get_data());
+
+        } else {
+            null_map->get_data().resize_fill(input_rows_count, 0);
+            vector_execute<false>(block, arguments, input_rows_count, *res_col,
+                                  assert_cast<const 
ColumnInt64&>(*bit_col.get()),
+                                  null_map->get_data());
+        }
+
+        if (block.get_by_position(arguments[0]).type.get()->is_nullable()) {
+            block.replace_by_position(
+                    result, ColumnNullable::create(std::move(res_col), 
std::move(null_map)));
+        } else {
+            block.replace_by_position(result, std::move(res_col));
+        }
+        return Status::OK();
+    }
+
+private:
+    template <bool bit_const>
+    void vector_execute(const Block& block, const ColumnNumbers& arguments, 
size_t input_rows_count,
+                        ColumnString& res_col, const ColumnInt64& bit_col,
+                        PaddedPODArray<uint8_t>& null_map) const {
+        if constexpr (bit_const) {
+            int64_t bit = bit_col.get_element(0);
+            for (size_t i = 0; i < input_rows_count; ++i) {
+                execute_one_row(block, arguments, res_col, bit, i);
+            }
+        } else {
+            for (size_t i = 0; i < input_rows_count; ++i) {
+                if (null_map[i]) {
+                    res_col.insert_default();
+                    continue;
+                }
+                execute_one_row(block, arguments, res_col, 
bit_col.get_element(i), i);
+            }
+        }
+    }
+
+    void execute_one_row(const Block& block, const ColumnNumbers& arguments, 
ColumnString& res_col,
+                         int64_t bit, int row_num) const {
+        static constexpr char SEPARATOR = ',';
+        int64_t pos = __builtin_ffs(bit);

Review Comment:
   pos是不是也用uint64比较合适?



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