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


##########
be/src/vec/functions/function_string.h:
##########
@@ -386,6 +389,204 @@ class FunctionStrcmp : public IFunction {
     }
 };
 
+class FunctionAutoPartitionName : public IFunction {
+public:
+    static constexpr auto name = "auto_partition_name";
+    static FunctionPtr create() { return 
std::make_shared<FunctionAutoPartitionName>(); }
+    String get_name() const override { return name; }
+    size_t get_number_of_arguments() const override { return 0; }
+    bool is_variadic() const override { return true; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        return std::make_shared<DataTypeString>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) const override 
{
+        int argument_size = arguments.size();
+        if (argument_size < 2) {
+            return Status::InvalidArgument(
+                    "auto_partition_name must contains at least two 
arguments");
+        }
+        std::vector<const ColumnString::Chars*> chars_list(argument_size);
+        std::vector<const ColumnString::Offsets*> offsets_list(argument_size);
+        std::vector<bool> is_const_args(argument_size);
+
+        for (int i = 0; i < argument_size; ++i) {
+            const auto& [col, is_const] =
+                    
unpack_if_const(block.get_by_position(arguments[i]).column);
+
+            const auto* col_str = assert_cast<const ColumnString*>(col.get());
+            chars_list[i] = &col_str->get_chars();
+            offsets_list[i] = &col_str->get_offsets();
+            is_const_args[i] = is_const;
+        }
+        auto res = ColumnString::create();
+        auto& res_data = res->get_chars();
+        auto& res_offset = res->get_offsets();
+        res_offset.resize(input_rows_count);
+
+        const char* partition_type = chars_list[0]->raw_data();
+        // partition type is list|range
+        if (std::strncmp(partition_type, "list", 4) == 0) {
+            int curr_len = 0;
+            for (int row = 0; row < input_rows_count; row++) {
+                std::string res_p = "p";
+                for (int col = 1; col < argument_size; col++) {
+                    const auto& current_offsets = *offsets_list[col];
+                    const auto& current_chars = *chars_list[col];
+
+                    auto idx = index_check_const(row, is_const_args[col]);
+                    int size = current_offsets[idx] - current_offsets[idx - 1];
+                    const char* raw_chars =
+                            reinterpret_cast<const 
char*>(&current_chars[current_offsets[idx - 1]]);
+
+                    // convert string to u16string in order to convert to 
unicode strings
+                    const std::string raw_str(raw_chars, size);
+                    auto u16string = _string_to_u16string(raw_str);
+                    res_p += _string_to_unicode(u16string) + 
std::to_string(u16string.size());
+                }
+
+                // check the name of length
+                int len = res_p.size();
+                if (len > 50) [[unlikely]] {
+                    return Status::InvalidArgument(
+                            "The list partition name cannot exceed 50 
characters");
+                }
+                curr_len += len;
+                res_data.resize(curr_len);
+                memcpy(&res_data[res_offset[row - 1]], res_p.c_str(), len);
+                res_offset[row] = res_offset[row - 1] + len;
+            }
+        } else {
+            const char* range_type = chars_list[1]->raw_data();
+
+            res_data.resize(15 * input_rows_count);
+            for (int i = 0; i < input_rows_count; i++) {
+                const auto& current_offsets = *offsets_list[2];
+                const auto& current_chars = *chars_list[2];
+
+                auto idx = index_check_const(i, is_const_args[2]);
+                int size = current_offsets[idx] - current_offsets[idx - 1];
+                const char* tmp =
+                        reinterpret_cast<const 
char*>(&current_chars[current_offsets[idx - 1]]);
+                std::string to_split_s(tmp, size);
+
+                // check the str if it is date|datetime
+                RE2 date_regex(R"(^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d{2})?$)");
+                if (!RE2::PartialMatch(to_split_s, date_regex)) {
+                    return Status::InvalidArgument(
+                            "The range partition only support DATE|DATETIME");
+                }
+
+                // split date_str from (yyyy-mm-dd hh:mm:ss) to ([yyyy, mm, 
dd, hh, mm, ss])
+                std::vector<std::string> date_str(6);
+                date_str[0] = to_split_s.substr(0, 4);
+                for (int i = 5, j = 1; i <= size; i += 3, j++) {
+                    date_str[j] = to_split_s.substr(i, 2);
+                }
+                int curr_len = 1;
+
+                res_data[res_offset[i - 1]] = 'p';
+                // raw => 2022-12-12 11:30:20
+                // year => 2022 01 01 00 00 00
+                // month => 2022 12 01 00 00 00
+                // day => 2022 12 12 00 00 00
+                // hour => 2022 12 12 11 00 00
+                // minute => 2022 12  11 30 00
+                // second => 2022 12 12 12 30 20
+
+                if (!strncmp(range_type, "year", 4)) {
+                    memcpy(&res_data[res_offset[i - 1]] + curr_len, 
date_str[0].c_str(),
+                           date_str[0].size());
+                    curr_len += date_str[0].size();
+                    memcpy(&res_data[res_offset[i - 1]] + curr_len, "0101", 4);
+                    curr_len += 4;
+                } else if (!strncmp(range_type, "month", 5)) {
+                    for (int j = 0; j < 2; j++) {

Review Comment:
   warp them in function with `j`'s bound as argument



##########
be/src/vec/functions/function_string.h:
##########
@@ -386,6 +389,204 @@ class FunctionStrcmp : public IFunction {
     }
 };
 
+class FunctionAutoPartitionName : public IFunction {
+public:
+    static constexpr auto name = "auto_partition_name";
+    static FunctionPtr create() { return 
std::make_shared<FunctionAutoPartitionName>(); }
+    String get_name() const override { return name; }
+    size_t get_number_of_arguments() const override { return 0; }
+    bool is_variadic() const override { return true; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        return std::make_shared<DataTypeString>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) const override 
{
+        int argument_size = arguments.size();
+        if (argument_size < 2) {
+            return Status::InvalidArgument(
+                    "auto_partition_name must contains at least two 
arguments");
+        }
+        std::vector<const ColumnString::Chars*> chars_list(argument_size);
+        std::vector<const ColumnString::Offsets*> offsets_list(argument_size);
+        std::vector<bool> is_const_args(argument_size);
+
+        for (int i = 0; i < argument_size; ++i) {
+            const auto& [col, is_const] =
+                    
unpack_if_const(block.get_by_position(arguments[i]).column);
+
+            const auto* col_str = assert_cast<const ColumnString*>(col.get());
+            chars_list[i] = &col_str->get_chars();
+            offsets_list[i] = &col_str->get_offsets();
+            is_const_args[i] = is_const;
+        }
+        auto res = ColumnString::create();
+        auto& res_data = res->get_chars();
+        auto& res_offset = res->get_offsets();
+        res_offset.resize(input_rows_count);
+
+        const char* partition_type = chars_list[0]->raw_data();
+        // partition type is list|range
+        if (std::strncmp(partition_type, "list", 4) == 0) {
+            int curr_len = 0;
+            for (int row = 0; row < input_rows_count; row++) {
+                std::string res_p = "p";
+                for (int col = 1; col < argument_size; col++) {
+                    const auto& current_offsets = *offsets_list[col];
+                    const auto& current_chars = *chars_list[col];
+
+                    auto idx = index_check_const(row, is_const_args[col]);
+                    int size = current_offsets[idx] - current_offsets[idx - 1];
+                    const char* raw_chars =
+                            reinterpret_cast<const 
char*>(&current_chars[current_offsets[idx - 1]]);
+
+                    // convert string to u16string in order to convert to 
unicode strings
+                    const std::string raw_str(raw_chars, size);
+                    auto u16string = _string_to_u16string(raw_str);
+                    res_p += _string_to_unicode(u16string) + 
std::to_string(u16string.size());
+                }
+
+                // check the name of length
+                int len = res_p.size();
+                if (len > 50) [[unlikely]] {
+                    return Status::InvalidArgument(
+                            "The list partition name cannot exceed 50 
characters");
+                }
+                curr_len += len;
+                res_data.resize(curr_len);
+                memcpy(&res_data[res_offset[row - 1]], res_p.c_str(), len);
+                res_offset[row] = res_offset[row - 1] + len;
+            }
+        } else {
+            const char* range_type = chars_list[1]->raw_data();
+
+            res_data.resize(15 * input_rows_count);
+            for (int i = 0; i < input_rows_count; i++) {
+                const auto& current_offsets = *offsets_list[2];
+                const auto& current_chars = *chars_list[2];
+
+                auto idx = index_check_const(i, is_const_args[2]);
+                int size = current_offsets[idx] - current_offsets[idx - 1];
+                const char* tmp =
+                        reinterpret_cast<const 
char*>(&current_chars[current_offsets[idx - 1]]);
+                std::string to_split_s(tmp, size);
+
+                // check the str if it is date|datetime
+                RE2 date_regex(R"(^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d{2})?$)");
+                if (!RE2::PartialMatch(to_split_s, date_regex)) {
+                    return Status::InvalidArgument(
+                            "The range partition only support DATE|DATETIME");
+                }
+
+                // split date_str from (yyyy-mm-dd hh:mm:ss) to ([yyyy, mm, 
dd, hh, mm, ss])
+                std::vector<std::string> date_str(6);
+                date_str[0] = to_split_s.substr(0, 4);
+                for (int i = 5, j = 1; i <= size; i += 3, j++) {
+                    date_str[j] = to_split_s.substr(i, 2);
+                }
+                int curr_len = 1;
+
+                res_data[res_offset[i - 1]] = 'p';
+                // raw => 2022-12-12 11:30:20
+                // year => 2022 01 01 00 00 00
+                // month => 2022 12 01 00 00 00
+                // day => 2022 12 12 00 00 00
+                // hour => 2022 12 12 11 00 00
+                // minute => 2022 12  11 30 00
+                // second => 2022 12 12 12 30 20
+
+                if (!strncmp(range_type, "year", 4)) {
+                    memcpy(&res_data[res_offset[i - 1]] + curr_len, 
date_str[0].c_str(),
+                           date_str[0].size());
+                    curr_len += date_str[0].size();
+                    memcpy(&res_data[res_offset[i - 1]] + curr_len, "0101", 4);
+                    curr_len += 4;
+                } else if (!strncmp(range_type, "month", 5)) {
+                    for (int j = 0; j < 2; j++) {
+                        memcpy(&res_data[res_offset[i - 1]] + curr_len, 
date_str[j].c_str(),
+                               date_str[j].size());
+                        curr_len += date_str[j].size();
+                    }
+                    memcpy(&res_data[res_offset[i - 1]] + curr_len, "01", 2);
+                    curr_len += 2;
+                } else if (!strncmp(range_type, "day", 3)) {
+                    for (int j = 0; j < 3; j++) {
+                        memcpy(&res_data[res_offset[i - 1]] + curr_len, 
date_str[j].c_str(),
+                               date_str[j].size());
+                        curr_len += date_str[j].size();
+                    }
+                } else if (!strncmp(range_type, "hour", 4)) {
+                    for (int j = 0; j < 4; j++) {
+                        memcpy(&res_data[res_offset[i - 1]] + curr_len, 
date_str[j].c_str(),
+                               date_str[j].size());
+                        curr_len += date_str[j].size();
+                    }
+                } else if (!strncmp(range_type, "minute", 6)) {
+                    for (int j = 0; j < 5; j++) {
+                        memcpy(&res_data[res_offset[i - 1]] + curr_len, 
date_str[j].c_str(),
+                               date_str[j].size());
+                        curr_len += date_str[j].size();
+                    }
+                } else if (!strncmp(range_type, "second", 6)) {
+                    for (int j = 0; j < 6; j++) {
+                        memcpy(&res_data[res_offset[i - 1]] + curr_len, 
date_str[j].c_str(),
+                               date_str[j].size());
+                        curr_len += date_str[j].size();
+                    }
+                }
+
+                // fill in zero
+                int zero = 15 - curr_len;
+                memcpy(&res_data[res_offset[i - 1]] + curr_len, 
std::string(zero, '0').c_str(),

Review Comment:
   use `std::fill_n`



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/AutoPartitionName.java:
##########
@@ -0,0 +1,102 @@
+// 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.exceptions.AnalysisException;
+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.PropagateNullable;
+import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
+import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
+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 com.google.common.collect.Lists;
+
+import java.util.List;
+
+/**
+ * ScalarFunction 'auto_partition_name'. This class is not generated by
+ * GenerateFunction.
+ */
+public class AutoPartitionName extends ScalarFunction
+        implements UnaryExpression, ExplicitlyCastableSignature, 
PropagateNullable {
+
+    public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
+            
FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT).varArgs(VarcharType.SYSTEM_DEFAULT),
+            
FunctionSignature.ret(StringType.INSTANCE).varArgs(StringType.INSTANCE));
+
+    /**
+     * constructor with 2 or 3 arguments.
+     */
+    public AutoPartitionName(Expression arg, Expression... varArgs) {
+        super("auto_partition_name", ExpressionUtils.mergeArguments(arg, 
varArgs));
+    }
+
+    /**
+     * withChildren.
+     */
+    @Override
+    public AutoPartitionName withChildren(List<Expression> children) {
+        Preconditions.checkArgument(children.size() >= 2);
+        return new AutoPartitionName(children.get(0),
+                children.subList(1, children.size()).toArray(new 
Expression[0]));
+    }
+
+    @Override
+    public void checkLegalityAfterRewrite() {

Review Comment:
   what if range but have more than one datetime inputs?



##########
be/src/vec/functions/function_string.h:
##########
@@ -386,6 +389,204 @@ class FunctionStrcmp : public IFunction {
     }
 };
 
+class FunctionAutoPartitionName : public IFunction {
+public:
+    static constexpr auto name = "auto_partition_name";
+    static FunctionPtr create() { return 
std::make_shared<FunctionAutoPartitionName>(); }
+    String get_name() const override { return name; }
+    size_t get_number_of_arguments() const override { return 0; }
+    bool is_variadic() const override { return true; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        return std::make_shared<DataTypeString>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) const override 
{
+        int argument_size = arguments.size();
+        if (argument_size < 2) {
+            return Status::InvalidArgument(
+                    "auto_partition_name must contains at least two 
arguments");
+        }
+        std::vector<const ColumnString::Chars*> chars_list(argument_size);
+        std::vector<const ColumnString::Offsets*> offsets_list(argument_size);
+        std::vector<bool> is_const_args(argument_size);
+
+        for (int i = 0; i < argument_size; ++i) {
+            const auto& [col, is_const] =
+                    
unpack_if_const(block.get_by_position(arguments[i]).column);
+
+            const auto* col_str = assert_cast<const ColumnString*>(col.get());
+            chars_list[i] = &col_str->get_chars();
+            offsets_list[i] = &col_str->get_offsets();
+            is_const_args[i] = is_const;
+        }
+        auto res = ColumnString::create();
+        auto& res_data = res->get_chars();
+        auto& res_offset = res->get_offsets();
+        res_offset.resize(input_rows_count);
+
+        const char* partition_type = chars_list[0]->raw_data();
+        // partition type is list|range
+        if (std::strncmp(partition_type, "list", 4) == 0) {
+            int curr_len = 0;
+            for (int row = 0; row < input_rows_count; row++) {
+                std::string res_p = "p";
+                for (int col = 1; col < argument_size; col++) {
+                    const auto& current_offsets = *offsets_list[col];
+                    const auto& current_chars = *chars_list[col];
+
+                    auto idx = index_check_const(row, is_const_args[col]);
+                    int size = current_offsets[idx] - current_offsets[idx - 1];
+                    const char* raw_chars =
+                            reinterpret_cast<const 
char*>(&current_chars[current_offsets[idx - 1]]);
+
+                    // convert string to u16string in order to convert to 
unicode strings
+                    const std::string raw_str(raw_chars, size);
+                    auto u16string = _string_to_u16string(raw_str);
+                    res_p += _string_to_unicode(u16string) + 
std::to_string(u16string.size());
+                }
+
+                // check the name of length
+                int len = res_p.size();
+                if (len > 50) [[unlikely]] {
+                    return Status::InvalidArgument(
+                            "The list partition name cannot exceed 50 
characters");
+                }
+                curr_len += len;
+                res_data.resize(curr_len);
+                memcpy(&res_data[res_offset[row - 1]], res_p.c_str(), len);
+                res_offset[row] = res_offset[row - 1] + len;
+            }
+        } else {
+            const char* range_type = chars_list[1]->raw_data();
+
+            res_data.resize(15 * input_rows_count);
+            for (int i = 0; i < input_rows_count; i++) {
+                const auto& current_offsets = *offsets_list[2];
+                const auto& current_chars = *chars_list[2];
+
+                auto idx = index_check_const(i, is_const_args[2]);
+                int size = current_offsets[idx] - current_offsets[idx - 1];
+                const char* tmp =
+                        reinterpret_cast<const 
char*>(&current_chars[current_offsets[idx - 1]]);
+                std::string to_split_s(tmp, size);
+
+                // check the str if it is date|datetime
+                RE2 date_regex(R"(^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d{2})?$)");
+                if (!RE2::PartialMatch(to_split_s, date_regex)) {

Review Comment:
   use FullMatch



##########
be/src/vec/functions/function_string.h:
##########
@@ -386,6 +389,204 @@ class FunctionStrcmp : public IFunction {
     }
 };
 
+class FunctionAutoPartitionName : public IFunction {
+public:
+    static constexpr auto name = "auto_partition_name";
+    static FunctionPtr create() { return 
std::make_shared<FunctionAutoPartitionName>(); }
+    String get_name() const override { return name; }
+    size_t get_number_of_arguments() const override { return 0; }
+    bool is_variadic() const override { return true; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        return std::make_shared<DataTypeString>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) const override 
{
+        int argument_size = arguments.size();
+        if (argument_size < 2) {
+            return Status::InvalidArgument(
+                    "auto_partition_name must contains at least two 
arguments");
+        }
+        std::vector<const ColumnString::Chars*> chars_list(argument_size);
+        std::vector<const ColumnString::Offsets*> offsets_list(argument_size);
+        std::vector<bool> is_const_args(argument_size);
+
+        for (int i = 0; i < argument_size; ++i) {
+            const auto& [col, is_const] =
+                    
unpack_if_const(block.get_by_position(arguments[i]).column);
+
+            const auto* col_str = assert_cast<const ColumnString*>(col.get());
+            chars_list[i] = &col_str->get_chars();
+            offsets_list[i] = &col_str->get_offsets();
+            is_const_args[i] = is_const;
+        }
+        auto res = ColumnString::create();
+        auto& res_data = res->get_chars();
+        auto& res_offset = res->get_offsets();
+        res_offset.resize(input_rows_count);
+
+        const char* partition_type = chars_list[0]->raw_data();
+        // partition type is list|range
+        if (std::strncmp(partition_type, "list", 4) == 0) {

Review Comment:
   wrap in two functions



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

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


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

Reply via email to