2012-07-15 23:13:08 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-04-04 12:07:05 +00:00
|
|
|
#include <Common/TargetSpecific.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-04-13 09:26:56 +00:00
|
|
|
#include <Functions/PerformanceAdaptors.h>
|
2020-05-29 09:18:39 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
2020-05-18 07:24:22 +00:00
|
|
|
|
2012-07-15 23:13:08 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-06-13 02:06:53 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/** Pseudo-random number generation functions.
|
|
|
|
* The function can be called without arguments or with one argument.
|
|
|
|
* The argument is ignored and only serves to ensure that several calls to one function are considered different and do not stick together.
|
2014-08-17 08:28:03 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* Example:
|
|
|
|
* SELECT rand(), rand() - will output two identical columns.
|
|
|
|
* SELECT rand(1), rand(2) - will output two different columns.
|
2012-07-15 23:13:08 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* Non-cryptographic generators:
|
2014-08-17 08:28:03 +00:00
|
|
|
*
|
2019-01-22 19:56:53 +00:00
|
|
|
* rand - linear congruential generator 0 .. 2^32 - 1.
|
2017-05-27 15:45:25 +00:00
|
|
|
* rand64 - combines several rand values to get values from the range 0 .. 2^64 - 1.
|
2012-07-15 23:13:08 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* randConstant - service function, produces a constant column with a random value.
|
2015-09-07 17:56:56 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* The time is used as the seed.
|
2020-10-14 14:04:50 +00:00
|
|
|
* Note: it is reinitialized for each columns.
|
|
|
|
* This means that the timer must be of sufficient resolution to give different values to each columns.
|
2012-07-15 23:13:08 +00:00
|
|
|
*/
|
|
|
|
|
2020-04-13 09:26:56 +00:00
|
|
|
DECLARE_MULTITARGET_CODE(
|
|
|
|
|
2012-07-15 23:13:08 +00:00
|
|
|
struct RandImpl
|
|
|
|
{
|
2020-05-26 11:15:44 +00:00
|
|
|
/// Fill memory with random data. The memory region must be 15-bytes padded.
|
2018-12-02 01:57:01 +00:00
|
|
|
static void execute(char * output, size_t size);
|
2012-07-15 23:13:08 +00:00
|
|
|
};
|
2020-05-21 07:40:27 +00:00
|
|
|
|
2020-05-20 12:42:21 +00:00
|
|
|
) // DECLARE_MULTITARGET_CODE
|
2020-05-19 10:54:41 +00:00
|
|
|
|
2020-05-15 08:40:27 +00:00
|
|
|
template <typename RandImpl, typename ToType, typename Name>
|
|
|
|
class FunctionRandomImpl : public IFunction
|
2012-07-15 23:13:08 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2019-10-10 14:38:08 +00:00
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
bool isDeterministicInScopeOfQuery() const override { return false; }
|
2019-12-13 13:10:02 +00:00
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2019-10-10 14:38:08 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() > 1)
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
+ toString(arguments.size()) + ", should be 0 or 1.",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2018-12-02 01:57:01 +00:00
|
|
|
return std::make_shared<DataTypeNumber<ToType>>();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnVector<ToType>::create();
|
2017-12-15 21:32:25 +00:00
|
|
|
typename ColumnVector<ToType>::Container & vec_to = col_to->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
size_t size = input_rows_count;
|
2017-04-01 07:20:54 +00:00
|
|
|
vec_to.resize(size);
|
2018-12-02 01:57:01 +00:00
|
|
|
RandImpl::execute(reinterpret_cast<char *>(vec_to.data()), vec_to.size() * sizeof(ToType));
|
2017-12-16 05:21:04 +00:00
|
|
|
|
2020-10-18 19:00:13 +00:00
|
|
|
return col_to;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2012-07-15 23:13:08 +00:00
|
|
|
};
|
|
|
|
|
2020-04-13 09:26:56 +00:00
|
|
|
template <typename ToType, typename Name>
|
2020-05-20 12:42:21 +00:00
|
|
|
class FunctionRandom : public FunctionRandomImpl<TargetSpecific::Default::RandImpl, ToType, Name>
|
2020-04-13 09:26:56 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-06-01 12:20:52 +00:00
|
|
|
explicit FunctionRandom(ContextPtr context) : selector(context)
|
2020-05-16 06:15:39 +00:00
|
|
|
{
|
2020-05-18 20:07:24 +00:00
|
|
|
selector.registerImplementation<TargetArch::Default,
|
2020-05-20 12:42:21 +00:00
|
|
|
FunctionRandomImpl<TargetSpecific::Default::RandImpl, ToType, Name>>();
|
|
|
|
|
2020-05-26 15:56:46 +00:00
|
|
|
#if USE_MULTITARGET_CODE
|
|
|
|
selector.registerImplementation<TargetArch::AVX2,
|
|
|
|
FunctionRandomImpl<TargetSpecific::AVX2::RandImpl, ToType, Name>>();
|
|
|
|
#endif
|
2020-04-13 09:26:56 +00:00
|
|
|
}
|
2020-05-15 08:40:27 +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-18 20:07:24 +00:00
|
|
|
{
|
2020-10-19 18:37:44 +00:00
|
|
|
return selector.selectAndExecute(arguments, result_type, input_rows_count);
|
2020-05-18 20:07:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
2020-05-16 06:15:39 +00:00
|
|
|
{
|
2020-05-17 15:13:01 +00:00
|
|
|
return std::make_shared<FunctionRandom<ToType, Name>>(context);
|
2020-04-13 09:26:56 +00:00
|
|
|
}
|
2020-05-18 20:07:24 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ImplementationSelector<IFunction> selector;
|
2020-04-13 09:26:56 +00:00
|
|
|
};
|
|
|
|
|
2020-05-16 06:59:08 +00:00
|
|
|
}
|