HappenLee commented on code in PR #34258: URL: https://github.com/apache/doris/pull/34258#discussion_r1701390762
########## 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*>(¤t_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*>(¤t_chars[current_offsets[idx - 1]]); + std::string to_split_s(tmp, size); + + // check the str if it is date|datetime + std::regex dateRegex(R"(^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d{2})?$)"); + if (!std::regex_match(to_split_s, dateRegex)) { + 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; Review Comment: do check zero <= 0 is ub -- 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