zclllyybb commented on code in PR #34258: URL: https://github.com/apache/doris/pull/34258#discussion_r1689114756
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/AutoPartitionName.java: ########## @@ -0,0 +1,94 @@ +// 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 checkLegalityBeforeTypeCoercion() { Review Comment: use `checkLegalityAfterRewrite` ########## be/src/vec/functions/function_string.h: ########## @@ -386,6 +387,194 @@ 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(); + 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 data_size = 0; + for (int i = 0; i < input_rows_count; i++) { Review Comment: prefer `row`, `col` than `i`, `j` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/AutoPartitionName.java: ########## @@ -0,0 +1,94 @@ +// 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 checkLegalityBeforeTypeCoercion() { + if (arity() < 2) { + throw new AnalysisException("function auto_partition_name must contains at least two arguments"); + } + final String partition_type = ((VarcharLiteral) getArgument(0)).getStringValue().toLowerCase(); + if (!Lists.newArrayList("range", "list").contains(partition_type)) { + throw new AnalysisException("function auto_partition_name must accept range|list for 1nd argument"); + } else if (Lists.newArrayList("range").contains(partition_type)) { + final String range_partition_type = ((VarcharLiteral) getArgument(1)).getStringValue() + .toLowerCase(); + if (!Lists.newArrayList("year", "month", "day") Review Comment: it should also support `hour` and more ########## be/src/vec/functions/function_string.h: ########## @@ -386,6 +387,194 @@ 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(); + 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 data_size = 0; + for (int i = 0; i < input_rows_count; i++) { + std::string res_p = "p"; + for (int j = 1; j < argument_size; j++) { + const auto& current_offsets = *offsets_list[j]; + const auto& current_chars = *chars_list[j]; + + auto idx = index_check_const(i, is_const_args[j]); + int size = current_offsets[idx] - current_offsets[idx - 1]; + + const char* tmp = + reinterpret_cast<const char*>(¤t_chars[current_offsets[idx - 1]]); + const std::string string_to_unicode(tmp, size); + auto unicode_to_string = _string_to_u16string(string_to_unicode); + res_p += _string_to_unicode(unicode_to_string) + + std::to_string(unicode_to_string.size()); + } + int len = res_p.size(); + if (len > 50) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The list partition name cannot exceed 50 characters"); + } + data_size += len; + res_data.resize(data_size); + memcpy(&res_data[res_offset[i - 1]], res_p.c_str(), len); + res_offset[i] = res_offset[i - 1] + len; + } + } else { + // split date with '-' eg. [2022-12-12] + if (argument_size != 3) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The partition type is range, the argument of size must be 3"); + } + const char* range_type = chars_list[1]->raw_data(); + std::string to_split_s(chars_list[2]->raw_data(), chars_list[2]->size()); + std::vector<std::string> str_v; + // split date with different way [2022-12-12 00:00:00] or [2022-12-12] + if (to_split_s.size() == input_rows_count * 19) { + for (int i = 0; i < to_split_s.size(); i += 19) { + auto vec_res = split(to_split_s.substr(i, 10), "-"); + for (const auto& s : vec_res) { + str_v.emplace_back(s); + } + } + } else { + for (int i = 0; i < to_split_s.size(); i += 10) { + auto vec_res = split(to_split_s.substr(i, 10), "-"); + for (const auto& s : vec_res) { + str_v.emplace_back(s); + } + } + } + if (str_v.size() != 3 * input_rows_count) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The range partition only support DATE|DATETIME"); + } + 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*>(¤t_chars[current_offsets[idx - 1]]); + //split the string with '-' + std::string to_split_s(tmp, size); + std::vector<std::string> str_v = split(to_split_s.substr(0, 10), "-"); + + // check if the arg type is DATE|DATETIME + if (str_v.size() != 3) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The range partition only support DATE|DATETIME"); + } + int curr_len = 1; + + res_data[res_offset[i - 1]] = 'p'; + // day => 2022 12 12 000000 + // month => 2022 12 01 000000 + // year => 2022 01 01 000000 + if (range_type[0] == 'd') { + for (int j = 0; j < 3; j++) { + memcpy(&res_data[res_offset[i - 1]] + curr_len, str_v[j].c_str(), + str_v[j].size()); + curr_len += str_v[j].size(); + } + } else if (range_type[0] == 'm') { + for (int j = 0; j < 2; j++) { + memcpy(&res_data[res_offset[i - 1]] + curr_len, str_v[j].c_str(), + str_v[j].size()); + curr_len += str_v[j].size(); + } + memcpy(&res_data[res_offset[i - 1]] + curr_len, "01", 2); + curr_len += 2; + } else if (range_type[0] == 'y') { + memcpy(&res_data[res_offset[i - 1]] + curr_len, str_v[0].c_str(), + str_v[0].size()); + curr_len += str_v[0].size(); + memcpy(&res_data[res_offset[i - 1]] + curr_len, "0101", 4); + curr_len += 4; + } else { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The first argument is range, the second argument muse be " + "day, month or year "); + } + memcpy(&res_data[res_offset[i - 1]] + curr_len, "000000", 6); + curr_len += 6; + res_offset[i] = res_offset[i - 1] + curr_len; + } + } + block.get_by_position(result).column = std::move(res); + return Status::OK(); + } + +private: + std::u16string _string_to_u16string(const std::string& str) const { + std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; + return convert.from_bytes(str); + } + + std::string _string_to_unicode(const std::u16string& s) const { + std::string res_s; Review Comment: reserve it with a proper size may be more efficent ########## be/src/vec/functions/function_string.h: ########## @@ -386,6 +387,194 @@ 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(); + 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 data_size = 0; + for (int i = 0; i < input_rows_count; i++) { + std::string res_p = "p"; + for (int j = 1; j < argument_size; j++) { + const auto& current_offsets = *offsets_list[j]; + const auto& current_chars = *chars_list[j]; + + auto idx = index_check_const(i, is_const_args[j]); + int size = current_offsets[idx] - current_offsets[idx - 1]; + + const char* tmp = + reinterpret_cast<const char*>(¤t_chars[current_offsets[idx - 1]]); + const std::string string_to_unicode(tmp, size); + auto unicode_to_string = _string_to_u16string(string_to_unicode); + res_p += _string_to_unicode(unicode_to_string) + + std::to_string(unicode_to_string.size()); + } + int len = res_p.size(); + if (len > 50) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( Review Comment: just `Status::InvalidArgument` is ok ########## be/src/vec/functions/function_string.h: ########## @@ -386,6 +387,194 @@ 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(); + 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 data_size = 0; + for (int i = 0; i < input_rows_count; i++) { + std::string res_p = "p"; + for (int j = 1; j < argument_size; j++) { + const auto& current_offsets = *offsets_list[j]; + const auto& current_chars = *chars_list[j]; + + auto idx = index_check_const(i, is_const_args[j]); + int size = current_offsets[idx] - current_offsets[idx - 1]; + + const char* tmp = + reinterpret_cast<const char*>(¤t_chars[current_offsets[idx - 1]]); + const std::string string_to_unicode(tmp, size); + auto unicode_to_string = _string_to_u16string(string_to_unicode); + res_p += _string_to_unicode(unicode_to_string) + + std::to_string(unicode_to_string.size()); + } + int len = res_p.size(); + if (len > 50) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The list partition name cannot exceed 50 characters"); + } + data_size += len; + res_data.resize(data_size); + memcpy(&res_data[res_offset[i - 1]], res_p.c_str(), len); + res_offset[i] = res_offset[i - 1] + len; + } + } else { + // split date with '-' eg. [2022-12-12] + if (argument_size != 3) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The partition type is range, the argument of size must be 3"); + } + const char* range_type = chars_list[1]->raw_data(); + std::string to_split_s(chars_list[2]->raw_data(), chars_list[2]->size()); + std::vector<std::string> str_v; + // split date with different way [2022-12-12 00:00:00] or [2022-12-12] + if (to_split_s.size() == input_rows_count * 19) { + for (int i = 0; i < to_split_s.size(); i += 19) { + auto vec_res = split(to_split_s.substr(i, 10), "-"); + for (const auto& s : vec_res) { + str_v.emplace_back(s); + } + } + } else { Review Comment: I think it's better to use `else if` here and add an unlikely else branch to throw Exception ########## be/src/vec/functions/function_string.h: ########## @@ -386,6 +387,194 @@ 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(); + 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 data_size = 0; + for (int i = 0; i < input_rows_count; i++) { + std::string res_p = "p"; + for (int j = 1; j < argument_size; j++) { + const auto& current_offsets = *offsets_list[j]; + const auto& current_chars = *chars_list[j]; + + auto idx = index_check_const(i, is_const_args[j]); + int size = current_offsets[idx] - current_offsets[idx - 1]; + + const char* tmp = + reinterpret_cast<const char*>(¤t_chars[current_offsets[idx - 1]]); + const std::string string_to_unicode(tmp, size); + auto unicode_to_string = _string_to_u16string(string_to_unicode); + res_p += _string_to_unicode(unicode_to_string) + + std::to_string(unicode_to_string.size()); + } + int len = res_p.size(); + if (len > 50) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The list partition name cannot exceed 50 characters"); + } + data_size += len; + res_data.resize(data_size); + memcpy(&res_data[res_offset[i - 1]], res_p.c_str(), len); + res_offset[i] = res_offset[i - 1] + len; + } + } else { + // split date with '-' eg. [2022-12-12] + if (argument_size != 3) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The partition type is range, the argument of size must be 3"); + } + const char* range_type = chars_list[1]->raw_data(); + std::string to_split_s(chars_list[2]->raw_data(), chars_list[2]->size()); + std::vector<std::string> str_v; + // split date with different way [2022-12-12 00:00:00] or [2022-12-12] + if (to_split_s.size() == input_rows_count * 19) { + for (int i = 0; i < to_split_s.size(); i += 19) { + auto vec_res = split(to_split_s.substr(i, 10), "-"); + for (const auto& s : vec_res) { + str_v.emplace_back(s); + } + } + } else { + for (int i = 0; i < to_split_s.size(); i += 10) { + auto vec_res = split(to_split_s.substr(i, 10), "-"); + for (const auto& s : vec_res) { + str_v.emplace_back(s); + } + } + } + if (str_v.size() != 3 * input_rows_count) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The range partition only support DATE|DATETIME"); + } + 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*>(¤t_chars[current_offsets[idx - 1]]); + //split the string with '-' + std::string to_split_s(tmp, size); + std::vector<std::string> str_v = split(to_split_s.substr(0, 10), "-"); + + // check if the arg type is DATE|DATETIME + if (str_v.size() != 3) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The range partition only support DATE|DATETIME"); + } + int curr_len = 1; + + res_data[res_offset[i - 1]] = 'p'; + // day => 2022 12 12 000000 + // month => 2022 12 01 000000 + // year => 2022 01 01 000000 + if (range_type[0] == 'd') { + for (int j = 0; j < 3; j++) { + memcpy(&res_data[res_offset[i - 1]] + curr_len, str_v[j].c_str(), + str_v[j].size()); + curr_len += str_v[j].size(); + } + } else if (range_type[0] == 'm') { + for (int j = 0; j < 2; j++) { + memcpy(&res_data[res_offset[i - 1]] + curr_len, str_v[j].c_str(), + str_v[j].size()); + curr_len += str_v[j].size(); + } + memcpy(&res_data[res_offset[i - 1]] + curr_len, "01", 2); + curr_len += 2; + } else if (range_type[0] == 'y') { + memcpy(&res_data[res_offset[i - 1]] + curr_len, str_v[0].c_str(), + str_v[0].size()); + curr_len += str_v[0].size(); + memcpy(&res_data[res_offset[i - 1]] + curr_len, "0101", 4); + curr_len += 4; + } else { Review Comment: support `h` `m` `s` ########## be/src/vec/functions/function_string.h: ########## @@ -386,6 +387,194 @@ 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(); + 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 data_size = 0; + for (int i = 0; i < input_rows_count; i++) { + std::string res_p = "p"; + for (int j = 1; j < argument_size; j++) { + const auto& current_offsets = *offsets_list[j]; + const auto& current_chars = *chars_list[j]; + + auto idx = index_check_const(i, is_const_args[j]); + int size = current_offsets[idx] - current_offsets[idx - 1]; + + const char* tmp = + reinterpret_cast<const char*>(¤t_chars[current_offsets[idx - 1]]); + const std::string string_to_unicode(tmp, size); + auto unicode_to_string = _string_to_u16string(string_to_unicode); + res_p += _string_to_unicode(unicode_to_string) + + std::to_string(unicode_to_string.size()); + } + int len = res_p.size(); + if (len > 50) { Review Comment: add `[[unlikely]]` on error check branches ########## be/src/vec/functions/function_string.h: ########## @@ -386,6 +387,194 @@ 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(); + 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 data_size = 0; + for (int i = 0; i < input_rows_count; i++) { + std::string res_p = "p"; + for (int j = 1; j < argument_size; j++) { + const auto& current_offsets = *offsets_list[j]; + const auto& current_chars = *chars_list[j]; + + auto idx = index_check_const(i, is_const_args[j]); + int size = current_offsets[idx] - current_offsets[idx - 1]; + + const char* tmp = + reinterpret_cast<const char*>(¤t_chars[current_offsets[idx - 1]]); + const std::string string_to_unicode(tmp, size); + auto unicode_to_string = _string_to_u16string(string_to_unicode); + res_p += _string_to_unicode(unicode_to_string) + + std::to_string(unicode_to_string.size()); + } + int len = res_p.size(); + if (len > 50) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The list partition name cannot exceed 50 characters"); + } + data_size += len; + res_data.resize(data_size); + memcpy(&res_data[res_offset[i - 1]], res_p.c_str(), len); + res_offset[i] = res_offset[i - 1] + len; + } + } else { + // split date with '-' eg. [2022-12-12] + if (argument_size != 3) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The partition type is range, the argument of size must be 3"); + } + const char* range_type = chars_list[1]->raw_data(); + std::string to_split_s(chars_list[2]->raw_data(), chars_list[2]->size()); + std::vector<std::string> str_v; + // split date with different way [2022-12-12 00:00:00] or [2022-12-12] + if (to_split_s.size() == input_rows_count * 19) { + for (int i = 0; i < to_split_s.size(); i += 19) { + auto vec_res = split(to_split_s.substr(i, 10), "-"); + for (const auto& s : vec_res) { + str_v.emplace_back(s); + } + } + } else { + for (int i = 0; i < to_split_s.size(); i += 10) { + auto vec_res = split(to_split_s.substr(i, 10), "-"); + for (const auto& s : vec_res) { + str_v.emplace_back(s); + } + } + } + if (str_v.size() != 3 * input_rows_count) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The range partition only support DATE|DATETIME"); + } + 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*>(¤t_chars[current_offsets[idx - 1]]); Review Comment: could you use `static_cast`? ########## be/src/vec/functions/function_string.h: ########## @@ -386,6 +387,194 @@ 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(); + 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 data_size = 0; + for (int i = 0; i < input_rows_count; i++) { + std::string res_p = "p"; + for (int j = 1; j < argument_size; j++) { + const auto& current_offsets = *offsets_list[j]; + const auto& current_chars = *chars_list[j]; + + auto idx = index_check_const(i, is_const_args[j]); + int size = current_offsets[idx] - current_offsets[idx - 1]; + + const char* tmp = + reinterpret_cast<const char*>(¤t_chars[current_offsets[idx - 1]]); + const std::string string_to_unicode(tmp, size); + auto unicode_to_string = _string_to_u16string(string_to_unicode); + res_p += _string_to_unicode(unicode_to_string) + + std::to_string(unicode_to_string.size()); + } + int len = res_p.size(); + if (len > 50) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The list partition name cannot exceed 50 characters"); + } + data_size += len; + res_data.resize(data_size); + memcpy(&res_data[res_offset[i - 1]], res_p.c_str(), len); + res_offset[i] = res_offset[i - 1] + len; + } + } else { + // split date with '-' eg. [2022-12-12] + if (argument_size != 3) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The partition type is range, the argument of size must be 3"); + } + const char* range_type = chars_list[1]->raw_data(); + std::string to_split_s(chars_list[2]->raw_data(), chars_list[2]->size()); + std::vector<std::string> str_v; + // split date with different way [2022-12-12 00:00:00] or [2022-12-12] + if (to_split_s.size() == input_rows_count * 19) { + for (int i = 0; i < to_split_s.size(); i += 19) { + auto vec_res = split(to_split_s.substr(i, 10), "-"); + for (const auto& s : vec_res) { + str_v.emplace_back(s); + } + } + } else { + for (int i = 0; i < to_split_s.size(); i += 10) { + auto vec_res = split(to_split_s.substr(i, 10), "-"); + for (const auto& s : vec_res) { + str_v.emplace_back(s); + } + } + } + if (str_v.size() != 3 * input_rows_count) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The range partition only support DATE|DATETIME"); + } + 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*>(¤t_chars[current_offsets[idx - 1]]); + //split the string with '-' + std::string to_split_s(tmp, size); + std::vector<std::string> str_v = split(to_split_s.substr(0, 10), "-"); + + // check if the arg type is DATE|DATETIME + if (str_v.size() != 3) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The range partition only support DATE|DATETIME"); + } + int curr_len = 1; + + res_data[res_offset[i - 1]] = 'p'; + // day => 2022 12 12 000000 + // month => 2022 12 01 000000 + // year => 2022 01 01 000000 + if (range_type[0] == 'd') { + for (int j = 0; j < 3; j++) { + memcpy(&res_data[res_offset[i - 1]] + curr_len, str_v[j].c_str(), + str_v[j].size()); + curr_len += str_v[j].size(); + } + } else if (range_type[0] == 'm') { + for (int j = 0; j < 2; j++) { + memcpy(&res_data[res_offset[i - 1]] + curr_len, str_v[j].c_str(), + str_v[j].size()); + curr_len += str_v[j].size(); + } + memcpy(&res_data[res_offset[i - 1]] + curr_len, "01", 2); + curr_len += 2; + } else if (range_type[0] == 'y') { + memcpy(&res_data[res_offset[i - 1]] + curr_len, str_v[0].c_str(), + str_v[0].size()); + curr_len += str_v[0].size(); + memcpy(&res_data[res_offset[i - 1]] + curr_len, "0101", 4); + curr_len += 4; + } else { + return Status::Error<ErrorCode::INVALID_ARGUMENT>( + "The first argument is range, the second argument muse be " + "day, month or year "); + } + memcpy(&res_data[res_offset[i - 1]] + curr_len, "000000", 6); + curr_len += 6; + res_offset[i] = res_offset[i - 1] + curr_len; + } + } + block.get_by_position(result).column = std::move(res); + return Status::OK(); + } + +private: + std::u16string _string_to_u16string(const std::string& str) const { + std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; + return convert.from_bytes(str); + } + + std::string _string_to_unicode(const std::u16string& s) const { + std::string res_s; + if (s.length() > 0 && s[0] == '-') { Review Comment: what's this for? could you add some testcases to show that? I didnot find any testcase result with '_' ########## be/src/vec/functions/function_string.h: ########## @@ -386,6 +387,194 @@ 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(); + 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 data_size = 0; + for (int i = 0; i < input_rows_count; i++) { + std::string res_p = "p"; + for (int j = 1; j < argument_size; j++) { + const auto& current_offsets = *offsets_list[j]; + const auto& current_chars = *chars_list[j]; + + auto idx = index_check_const(i, is_const_args[j]); + int size = current_offsets[idx] - current_offsets[idx - 1]; + + const char* tmp = + reinterpret_cast<const char*>(¤t_chars[current_offsets[idx - 1]]); + const std::string string_to_unicode(tmp, size); + auto unicode_to_string = _string_to_u16string(string_to_unicode); Review Comment: add some comment on this or use a more meaningful var name -- 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