[feature](function) add new function uuid() (#14092)

This commit is contained in:
xueweizhang
2022-11-10 14:55:41 +08:00
committed by GitHub
parent 0dfdbe4508
commit 90bfd87660
8 changed files with 187 additions and 1 deletions

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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 <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#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<Uuid>(); }
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<DataTypeString>();
}
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<Uuid>();
}
} // namespace doris::vectorized

View File

@ -0,0 +1,51 @@
---
{
"title": "uuid",
"language": "en"
}
---
<!--
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.
-->
## uuid
### description
#### Syntax
`VARCHAR uuid()`
return a random uuid string
### example
```
输入字符串
mysql> select uuid();
+--------------------------------------+
| uuid() |
+--------------------------------------+
| 29077778-fc5e-4603-8368-6b5f8fd55c24 |
+--------------------------------------+
```
### keywords
UUID

View File

@ -0,0 +1,51 @@
---
{
"title": "uuid",
"language": "zh-CN"
}
---
<!--
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.
-->
## uuid
### description
#### Syntax
`VARCHAR uuid()`
返回一个随机的uuid字符串
### example
```
输入字符串
mysql> select uuid();
+--------------------------------------+
| uuid() |
+--------------------------------------+
| 29077778-fc5e-4603-8368-6b5f8fd55c24 |
+--------------------------------------+
```
### keywords
UUID

View File

@ -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,