ClickHouse/src/Functions/randomFixedString.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

115 lines
3.7 KiB
C++
Raw Normal View History

2020-05-12 22:48:55 +00:00
#include <Columns/ColumnFixedString.h>
#include <DataTypes/DataTypeFixedString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
#include <Functions/PerformanceAdaptors.h>
#include <Functions/FunctionsRandom.h>
2020-05-12 22:48:55 +00:00
#include <pcg_random.hpp>
#include <Common/randomSeed.h>
2021-10-02 07:13:14 +00:00
#include <base/arithmeticOverflow.h>
#include <base/unaligned.h>
2020-05-12 22:48:55 +00:00
2021-10-02 07:13:14 +00:00
#include <base/defines.h>
2020-05-12 22:48:55 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int ILLEGAL_COLUMN;
extern const int DECIMAL_OVERFLOW;
}
2020-09-07 18:00:37 +00:00
namespace
{
2020-05-12 22:48:55 +00:00
/* Generate random fixed string with fully random bytes (including zero). */
template <typename RandImpl>
class FunctionRandomFixedStringImpl : public IFunction
2020-05-12 22:48:55 +00:00
{
public:
static constexpr auto name = "randomFixedString";
String getName() const override { return name; }
bool isVariadic() const override { return false; }
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
2020-05-12 22:48:55 +00:00
size_t getNumberOfArguments() const override { return 1; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
if (!isUnsignedInteger(arguments[0].type))
throw Exception("First argument for function " + getName() + " must be unsigned integer", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
2020-05-13 00:20:41 +00:00
if (!arguments[0].column || !isColumnConst(*arguments[0].column))
2020-05-12 22:48:55 +00:00
throw Exception("First argument for function " + getName() + " must be constant", ErrorCodes::ILLEGAL_COLUMN);
2020-05-17 08:00:10 +00:00
const size_t n = assert_cast<const ColumnConst &>(*arguments[0].column).getValue<UInt64>();
2020-05-12 22:48:55 +00:00
return std::make_shared<DataTypeFixedString>(n);
}
bool isDeterministic() const override { return false; }
bool isDeterministicInScopeOfQuery() const override { return false; }
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
2020-05-12 22:48:55 +00:00
{
2020-10-19 15:27:41 +00:00
const size_t n = assert_cast<const ColumnConst &>(*arguments[0].column).getValue<UInt64>();
2020-05-12 22:48:55 +00:00
auto col_to = ColumnFixedString::create(n);
ColumnFixedString::Chars & data_to = col_to->getChars();
if (input_rows_count == 0)
2020-10-19 15:27:41 +00:00
return col_to;
2020-05-12 22:48:55 +00:00
size_t total_size;
if (common::mulOverflow(input_rows_count, n, total_size))
throw Exception("Decimal math overflow", ErrorCodes::DECIMAL_OVERFLOW);
/// Fill random bytes.
data_to.resize(total_size);
RandImpl::execute(reinterpret_cast<char *>(data_to.data()), total_size);
2020-05-12 22:48:55 +00:00
2020-10-19 15:27:41 +00:00
return col_to;
2020-05-12 22:48:55 +00:00
}
};
class FunctionRandomFixedString : public FunctionRandomFixedStringImpl<TargetSpecific::Default::RandImpl>
{
public:
2021-06-01 12:20:52 +00:00
explicit FunctionRandomFixedString(ContextPtr context) : selector(context)
{
selector.registerImplementation<TargetArch::Default,
FunctionRandomFixedStringImpl<TargetSpecific::Default::RandImpl>>();
2020-05-29 09:18:39 +00:00
#if USE_MULTITARGET_CODE
selector.registerImplementation<TargetArch::AVX2,
FunctionRandomFixedStringImpl<TargetSpecific::AVX2::RandImpl>>();
#endif
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
{
2020-10-19 15:27:41 +00:00
return selector.selectAndExecute(arguments, result_type, input_rows_count);
}
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr context)
{
return std::make_shared<FunctionRandomFixedString>(context);
}
private:
ImplementationSelector<IFunction> selector;
};
2020-09-07 18:00:37 +00:00
}
REGISTER_FUNCTION(RandomFixedString)
2020-05-12 22:48:55 +00:00
{
factory.registerFunction<FunctionRandomFixedString>();
}
}