ClickHouse/src/Functions/FunctionsRandom.h

119 lines
4.0 KiB
C++
Raw Normal View History

2012-07-15 23:13:08 +00:00
#pragma once
#include <DataTypes/DataTypesNumber.h>
#include <Columns/ColumnVector.h>
#include <Functions/IFunctionImpl.h>
#include <IO/WriteHelpers.h>
2012-07-15 23:13:08 +00:00
2020-04-13 09:26:56 +00:00
#include <Functions/TargetSpecific.h>
#include <Functions/PerformanceAdaptors.h>
// #include "TargetSpecific.h"
// #include "PerformanceAdaptors.h"
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.
*
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:
*
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.
*
2017-05-27 15:45:25 +00:00
* The time is used as the seed.
* Note: it is reinitialized for each block.
* This means that the timer must be of sufficient resolution to give different values to each block.
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
{
static void execute(char * output, size_t size);
static String getImplementationTag() { return ToString(BuildArch); }
2012-07-15 23:13:08 +00:00
};
2020-05-15 08:40:27 +00:00
) // DECLARE_MULTITARGET_CODE
template <typename RandImpl, typename ToType, typename Name>
class FunctionRandomImpl : public IFunction
2012-07-15 23:13:08 +00:00
{
public:
static constexpr auto name = Name::name;
String getName() const override
{
return name;
}
static String getImplementationTag()
{
return RandImpl::getImplementationTag();
}
bool isDeterministic() const override { return false; }
bool isDeterministicInScopeOfQuery() const override { return false; }
bool useDefaultImplementationForNulls() const override { return false; }
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);
return std::make_shared<DataTypeNumber<ToType>>();
}
2018-04-24 07:16:39 +00:00
void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override
{
auto col_to = ColumnVector<ToType>::create();
typename ColumnVector<ToType>::Container & vec_to = col_to->getData();
2018-04-24 07:16:39 +00:00
size_t size = input_rows_count;
vec_to.resize(size);
RandImpl::execute(reinterpret_cast<char *>(vec_to.data()), vec_to.size() * sizeof(ToType));
block.getByPosition(result).column = std::move(col_to);
}
2012-07-15 23:13:08 +00:00
};
2020-04-13 09:26:56 +00:00
template <typename ToType, typename Name>
2020-05-15 08:40:27 +00:00
class FunctionRandom : public FunctionPerformanceAdaptor<FunctionRandomImpl<TargetSpecific::Default::RandImpl, ToType, Name>>
2020-04-13 09:26:56 +00:00
{
public:
FunctionRandom(const Context & context_)
: FunctionPerformanceAdaptor<FunctionRandomImpl<TargetSpecific::Default::RandImpl, ToType, Name>>(context_)
{
if constexpr (UseMultitargetCode)
{
registerImplementation<FunctionRandomImpl<TargetSpecific::SSE42::RandImpl, ToType, Name>>(TargetArch::SSE42);
registerImplementation<FunctionRandomImpl<TargetSpecific::AVX::RandImpl, ToType, Name>>(TargetArch::AVX);
registerImplementation<FunctionRandomImpl<TargetSpecific::AVX2::RandImpl, ToType, Name>>(TargetArch::AVX2);
registerImplementation<FunctionRandomImpl<TargetSpecific::AVX512F::RandImpl, ToType, Name>>(TargetArch::AVX512F);
}
2020-04-13 09:26:56 +00:00
}
2020-05-15 08:40:27 +00:00
static FunctionPtr create(const Context & context)
{
return std::make_shared<FunctionRandom<ToType, Name>>(context);
2020-04-13 09:26:56 +00:00
}
};
2020-05-16 06:59:08 +00:00
}