/root/doris/be/src/exprs/function/uuid.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include <glog/logging.h> |
19 | | #include <stddef.h> |
20 | | |
21 | | #include <boost/uuid/random_generator.hpp> |
22 | | #include <boost/uuid/uuid_io.hpp> |
23 | | #include <memory> |
24 | | #include <string> |
25 | | #include <utility> |
26 | | |
27 | | #include "common/status.h" |
28 | | #include "core/block/block.h" |
29 | | #include "core/block/column_numbers.h" |
30 | | #include "core/column/column_string.h" |
31 | | #include "core/data_type/data_type_string.h" |
32 | | #include "core/types.h" |
33 | | #include "exec/common/string_utils/string_utils.h" |
34 | | #include "exprs/aggregate/aggregate_function.h" |
35 | | #include "exprs/function/function.h" |
36 | | #include "exprs/function/function_totype.h" |
37 | | #include "exprs/function/simple_function_factory.h" |
38 | | |
39 | | namespace doris { |
40 | | class FunctionContext; |
41 | | } // namespace doris |
42 | | |
43 | | namespace doris { |
44 | | class Uuid : public IFunction { |
45 | | public: |
46 | | static constexpr auto name = "uuid"; |
47 | | static constexpr size_t uuid_length = 36; //uuid fixed length |
48 | | |
49 | 2 | static FunctionPtr create() { return std::make_shared<Uuid>(); } |
50 | | |
51 | 1 | String get_name() const override { return name; } |
52 | | |
53 | 0 | bool use_default_implementation_for_constants() const override { return false; } |
54 | | |
55 | 0 | size_t get_number_of_arguments() const override { return 0; } |
56 | | |
57 | 1 | bool is_variadic() const override { return false; } |
58 | | |
59 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
60 | 0 | return std::make_shared<DataTypeString>(); |
61 | 0 | } |
62 | | |
63 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
64 | 0 | uint32_t result, size_t input_rows_count) const override { |
65 | 0 | auto col_res = ColumnString::create(); |
66 | 0 | col_res->get_offsets().reserve(input_rows_count); |
67 | 0 | col_res->get_chars().reserve(input_rows_count * uuid_length); |
68 | |
|
69 | 0 | boost::uuids::random_generator generator; |
70 | 0 | for (int i = 0; i < input_rows_count; i++) { |
71 | 0 | std::string uuid = boost::uuids::to_string(generator()); |
72 | 0 | DCHECK(uuid.length() == uuid_length); |
73 | 0 | col_res->insert_data_without_reserve(uuid.c_str(), uuid.length()); |
74 | 0 | } |
75 | |
|
76 | 0 | block.replace_by_position(result, std::move(col_res)); |
77 | 0 | return Status::OK(); |
78 | 0 | } |
79 | | }; |
80 | | |
81 | 1 | void register_function_uuid(SimpleFunctionFactory& factory) { |
82 | 1 | factory.register_function<Uuid>(); |
83 | 1 | } |
84 | | |
85 | | } // namespace doris |