/root/doris/be/src/vec/functions/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 "vec/aggregate_functions/aggregate_function.h" |
29 | | #include "vec/columns/column_string.h" |
30 | | #include "vec/common/string_utils/string_utils.h" |
31 | | #include "vec/core/block.h" |
32 | | #include "vec/core/column_numbers.h" |
33 | | #include "vec/core/types.h" |
34 | | #include "vec/data_types/data_type_string.h" |
35 | | #include "vec/functions/function.h" |
36 | | #include "vec/functions/function_totype.h" |
37 | | #include "vec/functions/simple_function_factory.h" |
38 | | |
39 | | namespace doris { |
40 | | class FunctionContext; |
41 | | } // namespace doris |
42 | | |
43 | | namespace doris::vectorized { |
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 | | struct NameIsUuid { |
82 | | static constexpr auto name = "is_uuid"; |
83 | | }; |
84 | | |
85 | | struct IsUuidImpl { |
86 | | using ReturnType = DataTypeBool; |
87 | | using ReturnColumnType = ColumnUInt8; |
88 | | static constexpr auto PrimitiveTypeImpl = PrimitiveType::TYPE_STRING; |
89 | | static constexpr size_t uuid_without_dash_length = 32; |
90 | | static constexpr size_t uuid_with_dash_length = 36; |
91 | | static constexpr size_t uuid_with_braces_and_dash_length = 38; |
92 | | static constexpr size_t dash_positions[4] = {8, 13, 18, 23}; |
93 | | |
94 | 12 | static bool is_uuid_with_dash(const char* src, const char* end) { |
95 | 12 | size_t str_size = end - src; |
96 | 300 | for (int i = 0; i < str_size; ++i) { |
97 | 296 | if (!is_hex_ascii(src[i])) { |
98 | 38 | if (i == dash_positions[0] || i == dash_positions[1] || i == dash_positions[2] || |
99 | 38 | i == dash_positions[3]) { |
100 | 30 | if (src[i] != '-') { |
101 | 0 | return false; |
102 | 0 | } |
103 | 30 | } else { |
104 | 8 | return false; |
105 | 8 | } |
106 | 38 | } |
107 | 296 | } |
108 | 4 | return true; |
109 | 12 | } |
110 | | |
111 | | static Status vector(const ColumnString::Chars& data, const ColumnString::Offsets& offsets, |
112 | 15 | PaddedPODArray<UInt8>& res) { |
113 | 15 | size_t rows_count = offsets.size(); |
114 | 15 | res.resize(rows_count); |
115 | 44 | for (size_t i = 0; i < rows_count; ++i) { |
116 | 29 | const char* source = reinterpret_cast<const char*>(&data[offsets[i - 1]]); |
117 | 29 | int str_size = offsets[i] - offsets[i - 1]; |
118 | 29 | if (str_size == uuid_without_dash_length) { |
119 | 4 | bool is_valid = true; |
120 | 130 | for (int j = 0; j < str_size; ++j) { |
121 | 128 | if (!is_hex_ascii(source[j])) { |
122 | 2 | is_valid = false; |
123 | 2 | break; |
124 | 2 | } |
125 | 128 | } |
126 | 4 | res[i] = is_valid; |
127 | 25 | } else if (str_size == uuid_with_dash_length) { |
128 | 8 | res[i] = is_uuid_with_dash(source, source + str_size); |
129 | 17 | } else if (str_size == uuid_with_braces_and_dash_length) { |
130 | 4 | if (source[0] != '{' || source[str_size - 1] != '}') { |
131 | 0 | res[i] = 0; |
132 | 0 | continue; |
133 | 0 | } |
134 | 4 | res[i] = is_uuid_with_dash(source + 1, source + str_size - 1); |
135 | 13 | } else { |
136 | 13 | res[i] = 0; |
137 | 13 | } |
138 | 29 | } |
139 | 15 | return Status::OK(); |
140 | 15 | } |
141 | | }; |
142 | | |
143 | | using FunctionIsUuid = FunctionUnaryToType<IsUuidImpl, NameIsUuid>; |
144 | | |
145 | 1 | void register_function_uuid(SimpleFunctionFactory& factory) { |
146 | 1 | factory.register_function<Uuid>(); |
147 | 1 | factory.register_function<FunctionIsUuid>(); |
148 | 1 | } |
149 | | |
150 | | } // namespace doris::vectorized |