HappenLee commented on code in PR #33005: URL: https://github.com/apache/doris/pull/33005#discussion_r1544204394
########## be/src/vec/functions/function_uuid.cpp: ########## @@ -0,0 +1,214 @@ +// 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 <cctype> +#include <cstddef> +#include <cstring> +#include <memory> +#include <string_view> +#include <utility> + +#include "common/status.h" +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/columns/column.h" +#include "vec/columns/column_nullable.h" +#include "vec/columns/column_string.h" +#include "vec/columns/column_vector.h" +#include "vec/columns/columns_number.h" +#include "vec/common/assert_cast.h" +#include "vec/core/block.h" +#include "vec/core/column_numbers.h" +#include "vec/core/column_with_type_and_name.h" +#include "vec/core/types.h" +#include "vec/data_types/data_type.h" +#include "vec/data_types/data_type_nullable.h" +#include "vec/data_types/data_type_number.h" +#include "vec/data_types/data_type_string.h" +#include "vec/functions/function.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris { +class FunctionContext; +} // namespace doris + +namespace doris::vectorized { +constexpr static int split_pos[4] = {8, 13, 18, 23}; // 8-4-4-4-12 +constexpr static char spliter = '-'; + +class FunctionUuidtoInt : public IFunction { +public: + static constexpr auto name = "uuid_to_int"; + + static FunctionPtr create() { return std::make_shared<FunctionUuidtoInt>(); } + + String get_name() const override { return name; } + + size_t get_number_of_arguments() const override { return 1; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + return make_nullable(std::make_shared<DataTypeInt128>()); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) const override { + const auto& arg_column = + assert_cast<const ColumnString&>(*block.get_by_position(arguments[0]).column); + + auto result_column = ColumnInt128::create(input_rows_count); + auto& result_data = result_column->get_data(); + auto null_column = ColumnUInt8::create(input_rows_count); + auto& null_map = null_column->get_data(); + + for (int row = 0; row < input_rows_count; row++) { + auto str = arg_column.get_data_at(row); + const auto* data = str.data; + Int128* result_cell = &result_data[row]; + *result_cell = 0; + null_map[row] = false; + + if (str.size == 36) { + if (data[split_pos[0]] != spliter || data[split_pos[1]] != spliter || + data[split_pos[2]] != spliter || data[split_pos[3]] != spliter) { + null_map[row] = true; + continue; + } + char new_data[32]; + memset(new_data, 0, sizeof(new_data)); + // ignore '-' + memcpy(new_data, data, 8); + memcpy(new_data + 8, data + split_pos[0] + 1, 4); + memcpy(new_data + 12, data + split_pos[1] + 1, 4); + memcpy(new_data + 16, data + split_pos[2] + 1, 4); + memcpy(new_data + 20, data + split_pos[3] + 1, 12); + + if (!serialize(new_data, (char*)result_cell, 32).ok()) { + null_map[row] = true; + continue; + } + } else if (str.size == 32) { + if (!serialize(data, (char*)result_cell, 32).ok()) { + null_map[row] = true; + continue; + } + } else { + null_map[row] = true; + continue; + } + } + + block.replace_by_position( + result, ColumnNullable::create(std::move(result_column), std::move(null_column))); + return Status::OK(); + } + + // use char* to write dst is the only legal way by 'restrict aliasing rule' + static Status serialize(const char* __restrict src, char* __restrict dst, size_t length) { + char target; // 8bit, contains 2 char input + auto translate = [&target](const char ch) { + if (isdigit(ch)) { + target += ch - '0'; + } else if (ch >= 'a' && ch <= 'f') { + target += ch - 'a' + 10; + } else if (ch >= 'A' && ch <= 'F') { + target += ch - 'A' + 10; + } else { + return Status::InvalidArgument(""); + } + return Status::OK(); + }; + for (size_t i = 0; i < length; i += 2, src++, dst++) { + target = 0; + RETURN_IF_ERROR(translate(*src)); + + src++; + target <<= 4; + RETURN_IF_ERROR(translate(*src)); + *dst = target; + } + + return Status::OK(); + } +}; + +class FunctionInttoUuid : public IFunction { +public: + static constexpr auto name = "int_to_uuid"; + + static FunctionPtr create() { return std::make_shared<FunctionInttoUuid>(); } + + String get_name() const override { return name; } + + size_t get_number_of_arguments() const override { return 1; } + + 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 { + const auto& arg_column = + assert_cast<const ColumnInt128&>(*block.get_by_position(arguments[0]).column); + auto result_column = ColumnString::create(); + + for (int row = 0; row < input_rows_count; row++) { + const Int128* arg = &arg_column.get_data()[row]; + unsigned char result[32]; + deserialize((char*)arg, result, 16); // 16 chars, 32 data + + // insert delimiter + char format_result[36]; + memcpy(format_result, result, 8); + memcpy(format_result + split_pos[0] + 1, result + 8, 4); + memcpy(format_result + split_pos[1] + 1, result + 12, 4); + memcpy(format_result + split_pos[2] + 1, result + 16, 4); + memcpy(format_result + split_pos[3] + 1, result + 20, 12); + for (auto pos : split_pos) { + format_result[pos] = spliter; + } + + result_column->insert_data(format_result, 36); + } + block.replace_by_position(result, std::move(result_column)); + return Status::OK(); + } + + // use char* to read src is the only legal way by 'restrict aliasing rule' + static void deserialize(const char* __restrict src, unsigned char* __restrict dst, + size_t length) { + auto transform = [](char ch) -> unsigned char { + LOG(WARNING) << (unsigned int)ch; + if (ch < 10) { + return ch + '0'; + } else { + return ch - 10 + 'a'; + } + }; + for (size_t i = 0; i < length; i++, src++) { Review Comment: check the code simd? -- 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