From 90bfd87660b906f59f20198c1668b2ed5de9efbf Mon Sep 17 00:00:00 2001 From: xueweizhang Date: Thu, 10 Nov 2022 14:55:41 +0800 Subject: [PATCH] [feature](function) add new function uuid() (#14092) --- be/src/exprs/string_functions.cpp | 7 ++ be/src/exprs/string_functions.h | 3 + be/src/vec/CMakeLists.txt | 1 + .../vec/functions/simple_function_factory.h | 2 + be/src/vec/functions/uuid.cpp | 68 +++++++++++++++++++ .../sql-functions/string-functions/uuid.md | 51 ++++++++++++++ .../sql-functions/string-functions/uuid.md | 51 ++++++++++++++ gensrc/script/doris_builtins_functions.py | 5 +- 8 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 be/src/vec/functions/uuid.cpp create mode 100644 docs/en/docs/sql-manual/sql-functions/string-functions/uuid.md create mode 100644 docs/zh-CN/docs/sql-manual/sql-functions/string-functions/uuid.md diff --git a/be/src/exprs/string_functions.cpp b/be/src/exprs/string_functions.cpp index 96a8be323e..b980ad4a7f 100644 --- a/be/src/exprs/string_functions.cpp +++ b/be/src/exprs/string_functions.cpp @@ -984,4 +984,11 @@ IntVal StringFunctions::bit_length(FunctionContext* context, const StringVal& st } return IntVal(str.len * 8); } + +StringVal StringFunctions::uuid(FunctionContext* ctx) { + boost::uuids::random_generator generator; + std::string uuid = boost::uuids::to_string(generator()); + + return AnyValUtil::from_string_temp(ctx, uuid); +} } // namespace doris diff --git a/be/src/exprs/string_functions.h b/be/src/exprs/string_functions.h index ad8cc356a5..ec3e7c25b0 100644 --- a/be/src/exprs/string_functions.h +++ b/be/src/exprs/string_functions.h @@ -190,6 +190,9 @@ public: static doris_udf::IntVal bit_length(doris_udf::FunctionContext* context, const doris_udf::StringVal& str); + + static doris_udf::StringVal uuid(doris_udf::FunctionContext*); + // The caller owns the returned regex. Returns nullptr if the pattern could not be compiled. static re2::RE2* compile_regex(const StringVal& pattern, std::string* error_str, const StringVal& match_parameter); diff --git a/be/src/vec/CMakeLists.txt b/be/src/vec/CMakeLists.txt index 9be4c6acbe..02bef14dd2 100644 --- a/be/src/vec/CMakeLists.txt +++ b/be/src/vec/CMakeLists.txt @@ -194,6 +194,7 @@ set(VEC_FILES functions/function_ifnull.cpp functions/nullif.cpp functions/random.cpp + functions/uuid.cpp functions/function_coalesce.cpp functions/function_date_or_datetime_computation.cpp functions/function_date_or_datetime_to_string.cpp diff --git a/be/src/vec/functions/simple_function_factory.h b/be/src/vec/functions/simple_function_factory.h index 3da274b097..fbe28338d1 100644 --- a/be/src/vec/functions/simple_function_factory.h +++ b/be/src/vec/functions/simple_function_factory.h @@ -69,6 +69,7 @@ void register_function_ifnull(SimpleFunctionFactory& factory); void register_function_like(SimpleFunctionFactory& factory); void register_function_regexp(SimpleFunctionFactory& factory); void register_function_random(SimpleFunctionFactory& factory); +void register_function_uuid(SimpleFunctionFactory& factory); void register_function_coalesce(SimpleFunctionFactory& factory); void register_function_grouping(SimpleFunctionFactory& factory); void register_function_datetime_floor_ceil(SimpleFunctionFactory& factory); @@ -208,6 +209,7 @@ public: register_function_like(instance); register_function_regexp(instance); register_function_random(instance); + register_function_uuid(instance); register_function_coalesce(instance); register_function_grouping(instance); register_function_datetime_floor_ceil(instance); diff --git a/be/src/vec/functions/uuid.cpp b/be/src/vec/functions/uuid.cpp new file mode 100644 index 0000000000..acd1233cc1 --- /dev/null +++ b/be/src/vec/functions/uuid.cpp @@ -0,0 +1,68 @@ +// 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 +#include +#include + +#include "udf/udf.h" +#include "vec/data_types/data_type_string.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris::vectorized { +class Uuid : public IFunction { +public: + static constexpr auto name = "uuid"; + static constexpr size_t uuid_length = 36; //uuid fixed length + + static FunctionPtr create() { return std::make_shared(); } + + String get_name() const override { return name; } + + bool use_default_implementation_for_constants() const override { return false; } + + size_t get_number_of_arguments() const override { return 0; } + + bool is_variadic() const override { return false; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + return std::make_shared(); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) override { + auto col_res = ColumnString::create(); + col_res->get_offsets().reserve(input_rows_count); + col_res->get_chars().reserve(input_rows_count * uuid_length); + + boost::uuids::random_generator generator; + for (int i = 0; i < input_rows_count; i++) { + std::string uuid = boost::uuids::to_string(generator()); + DCHECK(uuid.length() == uuid_length); + col_res->insert_data_without_reserve(uuid.c_str(), uuid.length()); + } + + block.replace_by_position(result, std::move(col_res)); + return Status::OK(); + } +}; + +void register_function_uuid(SimpleFunctionFactory& factory) { + factory.register_function(); +} + +} // namespace doris::vectorized diff --git a/docs/en/docs/sql-manual/sql-functions/string-functions/uuid.md b/docs/en/docs/sql-manual/sql-functions/string-functions/uuid.md new file mode 100644 index 0000000000..c6de3f7839 --- /dev/null +++ b/docs/en/docs/sql-manual/sql-functions/string-functions/uuid.md @@ -0,0 +1,51 @@ +--- +{ + "title": "uuid", + "language": "en" +} +--- + + + +## uuid +### description +#### Syntax + +`VARCHAR uuid()` + +return a random uuid string + + +### example + +``` +输入字符串 + +mysql> select uuid(); ++--------------------------------------+ +| uuid() | ++--------------------------------------+ +| 29077778-fc5e-4603-8368-6b5f8fd55c24 | ++--------------------------------------+ + +``` + +### keywords + UUID diff --git a/docs/zh-CN/docs/sql-manual/sql-functions/string-functions/uuid.md b/docs/zh-CN/docs/sql-manual/sql-functions/string-functions/uuid.md new file mode 100644 index 0000000000..94c28f6921 --- /dev/null +++ b/docs/zh-CN/docs/sql-manual/sql-functions/string-functions/uuid.md @@ -0,0 +1,51 @@ +--- +{ + "title": "uuid", + "language": "zh-CN" +} +--- + + + +## uuid +### description +#### Syntax + +`VARCHAR uuid()` + +返回一个随机的uuid字符串 + + +### example + +``` +输入字符串 + +mysql> select uuid(); ++--------------------------------------+ +| uuid() | ++--------------------------------------+ +| 29077778-fc5e-4603-8368-6b5f8fd55c24 | ++--------------------------------------+ + +``` + +### keywords + UUID diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py index 193fa37b65..e6aae500c5 100755 --- a/gensrc/script/doris_builtins_functions.py +++ b/gensrc/script/doris_builtins_functions.py @@ -2817,6 +2817,8 @@ visible_functions = [ # search functions [['multi_search_all_positions'], 'ARRAY_INT', ['STRING', 'ARRAY_STRING'], '', '', '', 'vec', 'ALWAYS_NOT_NULLABLE'], [['multi_match_any'], 'TINYINT', ['STRING', 'ARRAY_STRING'], '', '', '', 'vec', 'ALWAYS_NOT_NULLABLE'], + + [['uuid'], 'VARCHAR', [], '_ZN5doris15StringFunctions4uuidEPN9doris_udf15FunctionContextE', '', '', 'vec', 'ALWAYS_NOT_NULLABLE'], ] # Except the following functions, other function will directly return @@ -2852,7 +2854,8 @@ nondeterministic_functions = [ 'current_date', 'curtime', 'current_time', - 'utc_timestamp' + 'utc_timestamp', + 'uuid' ] # This is the subset of ALWAYS_NULLABLE # The function belongs to @null_result_with_one_null_param_functions,