2018-12-02 01:57:01 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionsRandom.h>
|
|
|
|
#include <DataTypes/DataTypeUUID.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-05-25 15:16:19 +00:00
|
|
|
#define DECLARE_SEVERAL_IMPLEMENTATIONS(...) \
|
|
|
|
DECLARE_DEFAULT_CODE (__VA_ARGS__) \
|
|
|
|
DECLARE_AVX2_SPECIFIC_CODE(__VA_ARGS__)
|
|
|
|
|
|
|
|
DECLARE_SEVERAL_IMPLEMENTATIONS(
|
|
|
|
|
2018-12-02 01:57:01 +00:00
|
|
|
class FunctionGenerateUUIDv4 : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "generateUUIDv4";
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2018-12-02 01:57:01 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes &) const override
|
|
|
|
{
|
|
|
|
return std::make_shared<DataTypeUUID>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
|
2018-12-02 01:57:01 +00:00
|
|
|
{
|
2021-05-04 17:47:35 +00:00
|
|
|
auto col_res = ColumnVector<UUID>::create();
|
|
|
|
typename ColumnVector<UUID>::Container & vec_to = col_res->getData();
|
2018-12-02 01:57:01 +00:00
|
|
|
|
|
|
|
size_t size = input_rows_count;
|
|
|
|
vec_to.resize(size);
|
2020-05-25 15:16:19 +00:00
|
|
|
|
|
|
|
/// RandImpl is target-dependent and is not the same in different TargetSpecific namespaces.
|
2021-05-04 17:47:35 +00:00
|
|
|
RandImpl::execute(reinterpret_cast<char *>(vec_to.data()), vec_to.size() * sizeof(UUID));
|
2018-12-02 01:57:01 +00:00
|
|
|
|
2021-05-04 17:47:35 +00:00
|
|
|
for (UUID & uuid : vec_to)
|
2018-12-02 01:57:01 +00:00
|
|
|
{
|
2021-05-04 17:47:35 +00:00
|
|
|
/// https://tools.ietf.org/html/rfc4122#section-4.4
|
|
|
|
|
|
|
|
UInt128 & impl = uuid.toUnderType();
|
|
|
|
impl.items[0] = (impl.items[0] & 0xffffffffffff0fffull) | 0x0000000000004000ull;
|
|
|
|
impl.items[1] = (impl.items[1] & 0x3fffffffffffffffull) | 0x8000000000000000ull;
|
2018-12-02 01:57:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return col_res;
|
2018-12-02 01:57:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-25 15:16:19 +00:00
|
|
|
) // DECLARE_SEVERAL_IMPLEMENTATIONS
|
|
|
|
#undef DECLARE_SEVERAL_IMPLEMENTATIONS
|
|
|
|
|
|
|
|
class FunctionGenerateUUIDv4 : public TargetSpecific::Default::FunctionGenerateUUIDv4
|
|
|
|
{
|
|
|
|
public:
|
2021-06-01 12:20:52 +00:00
|
|
|
explicit FunctionGenerateUUIDv4(ContextPtr context) : selector(context)
|
2020-05-25 15:16:19 +00:00
|
|
|
{
|
|
|
|
selector.registerImplementation<TargetArch::Default,
|
|
|
|
TargetSpecific::Default::FunctionGenerateUUIDv4>();
|
|
|
|
|
2020-05-26 15:56:46 +00:00
|
|
|
#if USE_MULTITARGET_CODE
|
2020-05-25 15:16:19 +00:00
|
|
|
selector.registerImplementation<TargetArch::AVX2,
|
|
|
|
TargetSpecific::AVX2::FunctionGenerateUUIDv4>();
|
2020-05-26 15:56:46 +00:00
|
|
|
#endif
|
2020-05-25 15:16:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2020-05-25 15:16:19 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
return selector.selectAndExecute(arguments, result_type, input_rows_count);
|
2020-05-25 15:16:19 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
2020-05-25 15:16:19 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionGenerateUUIDv4>(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ImplementationSelector<IFunction> selector;
|
|
|
|
};
|
|
|
|
|
2018-12-02 01:57:01 +00:00
|
|
|
void registerFunctionGenerateUUIDv4(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionGenerateUUIDv4>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|