1 | #include <Functions/FunctionFactory.h> |
---|---|
2 | #include <Functions/FunctionsRandom.h> |
3 | #include <DataTypes/DataTypeUUID.h> |
4 | |
5 | namespace DB |
6 | { |
7 | |
8 | class FunctionGenerateUUIDv4 : public IFunction |
9 | { |
10 | public: |
11 | static constexpr auto name = "generateUUIDv4"; |
12 | static FunctionPtr create(const Context &) { return std::make_shared<FunctionGenerateUUIDv4>(); } |
13 | |
14 | String getName() const override |
15 | { |
16 | return name; |
17 | } |
18 | |
19 | size_t getNumberOfArguments() const override { return 0; } |
20 | |
21 | DataTypePtr getReturnTypeImpl(const DataTypes &) const override |
22 | { |
23 | return std::make_shared<DataTypeUUID>(); |
24 | } |
25 | |
26 | bool isDeterministic() const override { return false; } |
27 | |
28 | void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override |
29 | { |
30 | auto col_res = ColumnVector<UInt128>::create(); |
31 | typename ColumnVector<UInt128>::Container & vec_to = col_res->getData(); |
32 | |
33 | size_t size = input_rows_count; |
34 | vec_to.resize(size); |
35 | RandImpl::execute(reinterpret_cast<char *>(vec_to.data()), vec_to.size() * sizeof(UInt128)); |
36 | |
37 | for (UInt128 & uuid: vec_to) |
38 | { |
39 | /** https://tools.ietf.org/html/rfc4122#section-4.4 |
40 | */ |
41 | uuid.low = (uuid.low & 0xffffffffffff0fffull) | 0x0000000000004000ull; |
42 | uuid.high = (uuid.high & 0x3fffffffffffffffull) | 0x8000000000000000ull; |
43 | } |
44 | |
45 | block.getByPosition(result).column = std::move(col_res); |
46 | } |
47 | }; |
48 | |
49 | void registerFunctionGenerateUUIDv4(FunctionFactory & factory) |
50 | { |
51 | factory.registerFunction<FunctionGenerateUUIDv4>(); |
52 | } |
53 | |
54 | } |
55 | |
56 | |
57 | |
58 |