2020-05-07 14:54:33 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-05-29 09:12:08 +00:00
|
|
|
#include <Functions/FunctionsRandom.h>
|
|
|
|
#include <Functions/PerformanceAdaptors.h>
|
2020-05-11 00:23:47 +00:00
|
|
|
#include <pcg_random.hpp>
|
|
|
|
#include <Common/randomSeed.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/unaligned.h>
|
2020-05-07 14:54:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int TOO_LARGE_STRING_SIZE;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-05-07 14:54:33 +00:00
|
|
|
|
2020-05-09 18:52:27 +00:00
|
|
|
/* Generate random string of specified length with fully random bytes (including zero). */
|
2020-05-29 09:12:08 +00:00
|
|
|
template <typename RandImpl>
|
|
|
|
class FunctionRandomStringImpl : public IFunction
|
2020-05-07 14:54:33 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "randomString";
|
2020-05-07 15:36:11 +00:00
|
|
|
|
2020-05-07 14:54:33 +00:00
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
2020-05-07 15:36:11 +00:00
|
|
|
|
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
|
|
|
|
2020-05-07 14:54:33 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.empty())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
|
|
|
"Function {} requires at least one argument: the size of resulting string", getName());
|
2020-05-07 14:54:33 +00:00
|
|
|
|
|
|
|
if (arguments.size() > 2)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
|
|
|
"Function {} requires at most two arguments: the size of resulting string and optional disambiguation tag", getName());
|
2020-05-07 14:54:33 +00:00
|
|
|
|
2020-05-17 13:07:37 +00:00
|
|
|
const IDataType & length_type = *arguments[0];
|
|
|
|
if (!isNumber(length_type))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "First argument of function {} must have numeric type", getName());
|
2020-05-07 14:54:33 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
bool isDeterministicInScopeOfQuery() const override { return false; }
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2020-05-07 14:54:33 +00:00
|
|
|
{
|
2020-05-07 15:36:11 +00:00
|
|
|
auto col_to = ColumnString::create();
|
|
|
|
ColumnString::Chars & data_to = col_to->getChars();
|
|
|
|
ColumnString::Offsets & offsets_to = col_to->getOffsets();
|
|
|
|
|
2020-05-11 00:36:14 +00:00
|
|
|
if (input_rows_count == 0)
|
2020-10-19 15:27:41 +00:00
|
|
|
return col_to;
|
2020-05-11 00:36:14 +00:00
|
|
|
|
|
|
|
/// Fill offsets.
|
|
|
|
offsets_to.resize(input_rows_count);
|
2020-10-19 15:27:41 +00:00
|
|
|
const IColumn & length_column = *arguments[0].column;
|
2020-05-07 15:36:11 +00:00
|
|
|
|
|
|
|
IColumn::Offset offset = 0;
|
|
|
|
for (size_t row_num = 0; row_num < input_rows_count; ++row_num)
|
|
|
|
{
|
|
|
|
size_t length = length_column.getUInt(row_num);
|
|
|
|
if (length > (1 << 30))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::TOO_LARGE_STRING_SIZE, "Too large string size in function {}", getName());
|
2020-05-07 15:36:11 +00:00
|
|
|
|
2020-05-11 00:36:14 +00:00
|
|
|
offset += length + 1;
|
|
|
|
offsets_to[row_num] = offset;
|
|
|
|
}
|
2020-05-07 15:36:11 +00:00
|
|
|
|
2020-05-11 00:36:14 +00:00
|
|
|
/// Fill random bytes.
|
|
|
|
data_to.resize(offsets_to.back());
|
2020-05-29 09:12:08 +00:00
|
|
|
RandImpl::execute(reinterpret_cast<char *>(data_to.data()), data_to.size());
|
2020-05-07 15:36:11 +00:00
|
|
|
|
2020-05-11 00:36:14 +00:00
|
|
|
/// Put zero bytes in between.
|
2020-05-29 09:12:08 +00:00
|
|
|
auto * pos = data_to.data();
|
2020-05-11 00:36:14 +00:00
|
|
|
for (size_t row_num = 0; row_num < input_rows_count; ++row_num)
|
|
|
|
pos[offsets_to[row_num] - 1] = 0;
|
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
return col_to;
|
2020-05-07 14:54:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-29 09:12:08 +00:00
|
|
|
class FunctionRandomString : public FunctionRandomStringImpl<TargetSpecific::Default::RandImpl>
|
|
|
|
{
|
|
|
|
public:
|
2021-06-01 12:20:52 +00:00
|
|
|
explicit FunctionRandomString(ContextPtr context) : selector(context)
|
2020-05-29 09:12:08 +00:00
|
|
|
{
|
|
|
|
selector.registerImplementation<TargetArch::Default,
|
|
|
|
FunctionRandomStringImpl<TargetSpecific::Default::RandImpl>>();
|
2020-05-29 09:18:39 +00:00
|
|
|
|
2020-05-29 09:12:08 +00:00
|
|
|
#if USE_MULTITARGET_CODE
|
|
|
|
selector.registerImplementation<TargetArch::AVX2,
|
|
|
|
FunctionRandomStringImpl<TargetSpecific::AVX2::RandImpl>>();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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-29 09:12:08 +00:00
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
return selector.selectAndExecute(arguments, result_type, input_rows_count);
|
2020-05-29 09:12:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
2020-05-29 09:12:08 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionRandomString>(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ImplementationSelector<IFunction> selector;
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(RandomString)
|
2020-05-07 14:54:33 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionRandomString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|