2020-05-07 14:54:33 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Functions/IFunctionImpl.h>
|
2020-05-11 00:23:47 +00:00
|
|
|
#include <pcg_random.hpp>
|
2020-05-11 00:16:50 +00:00
|
|
|
#include <common/unaligned.h>
|
2020-05-11 00:23:47 +00:00
|
|
|
#include <Common/randomSeed.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-05-09 18:52:27 +00:00
|
|
|
/* Generate random string of specified length with fully random bytes (including zero). */
|
2020-05-07 15:36:11 +00:00
|
|
|
class FunctionRandomString : 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
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionRandomString>(); }
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
2020-05-07 15:36:11 +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())
|
|
|
|
throw Exception(
|
|
|
|
"Function " + getName() + " requires at least one argument: the size of resulting string",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
if (arguments.size() > 2)
|
|
|
|
throw Exception(
|
|
|
|
"Function " + getName() + " requires at most two arguments: the size of resulting string and optional disambiguation tag",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
const IDataType & length_type = *arguments[0];
|
|
|
|
if (!isNumber(length_type))
|
|
|
|
throw Exception("First argument of function " + getName() + " must have numeric type", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
bool isDeterministicInScopeOfQuery() const override { return false; }
|
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
|
|
|
|
{
|
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();
|
|
|
|
offsets_to.resize(input_rows_count);
|
|
|
|
|
|
|
|
const IColumn & length_column = *block.getByPosition(arguments[0]).column;
|
|
|
|
|
|
|
|
IColumn::Offset offset = 0;
|
|
|
|
|
2020-05-11 00:23:47 +00:00
|
|
|
pcg64_fast rng(randomSeed());
|
|
|
|
|
2020-05-07 15:36:11 +00:00
|
|
|
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))
|
|
|
|
throw Exception("Too large string size in function " + getName(), ErrorCodes::TOO_LARGE_STRING_SIZE);
|
|
|
|
|
|
|
|
IColumn::Offset next_offset = offset + length + 1;
|
|
|
|
data_to.resize(next_offset);
|
|
|
|
offsets_to[row_num] = next_offset;
|
|
|
|
|
2020-05-10 19:31:47 +00:00
|
|
|
auto * data_to_ptr = data_to.data(); // avoid assert on array indexing after end
|
2020-05-07 15:36:11 +00:00
|
|
|
for (size_t pos = offset, end = offset + length; pos < end;
|
2020-05-10 19:07:02 +00:00
|
|
|
pos += sizeof(UInt64)) // We have padding in column buffers that we can overwrite.
|
2020-05-07 15:36:11 +00:00
|
|
|
{
|
2020-05-11 00:23:47 +00:00
|
|
|
unalignedStore<UInt64>(data_to_ptr + pos, rng());
|
2020-05-07 15:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data_to[offset + length] = 0;
|
|
|
|
|
|
|
|
offset = next_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_to);
|
2020-05-07 14:54:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void registerFunctionRandomString(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionRandomString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|