#include #include #include #include #include #include #include #include #include #include #include #include namespace DB { namespace ErrorCodes { extern const int ILLEGAL_TYPE_OF_ARGUMENT; extern const int ILLEGAL_COLUMN; extern const int DECIMAL_OVERFLOW; } namespace { /* Generate random fixed string with fully random bytes (including zero). */ template class FunctionRandomFixedStringImpl : public IFunction { public: static constexpr auto name = "randomFixedString"; String getName() const override { return name; } bool isVariadic() const override { return false; } bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; } size_t getNumberOfArguments() const override { return 1; } DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override { if (!isUnsignedInteger(arguments[0].type)) throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "First argument for function {} must be unsigned integer", getName()); if (!arguments[0].column || !isColumnConst(*arguments[0].column)) throw Exception(ErrorCodes::ILLEGAL_COLUMN, "First argument for function {} must be constant", getName()); const size_t n = assert_cast(*arguments[0].column).getValue(); return std::make_shared(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 { const size_t n = assert_cast(*arguments[0].column).getValue(); auto col_to = ColumnFixedString::create(n); ColumnFixedString::Chars & data_to = col_to->getChars(); if (input_rows_count == 0) return col_to; size_t total_size; if (common::mulOverflow(input_rows_count, n, total_size)) throw Exception(ErrorCodes::DECIMAL_OVERFLOW, "Decimal math overflow"); /// Fill random bytes. data_to.resize(total_size); RandImpl::execute(reinterpret_cast(data_to.data()), total_size); return col_to; } }; class FunctionRandomFixedString : public FunctionRandomFixedStringImpl { public: explicit FunctionRandomFixedString(ContextPtr context) : selector(context) { selector.registerImplementation>(); #if USE_MULTITARGET_CODE selector.registerImplementation>(); #endif } ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override { return selector.selectAndExecute(arguments, result_type, input_rows_count); } static FunctionPtr create(ContextPtr context) { return std::make_shared(context); } private: ImplementationSelector selector; }; } REGISTER_FUNCTION(RandomFixedString) { factory.registerFunction(); } }