2012-07-15 23:13:08 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
|
#include <DB/Functions/IFunction.h>
|
2014-08-17 08:28:03 +00:00
|
|
|
|
#include <DB/Common/HashTable/Hash.h>
|
2012-07-15 23:13:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-12 02:21:15 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int CANNOT_CLOCK_GETTIME;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-15 23:13:08 +00:00
|
|
|
|
/** Функции генерации псевдослучайных чисел.
|
|
|
|
|
* Функция может быть вызвана без аргументов или с одним аргументом.
|
|
|
|
|
* Аргумент игнорируется и служит лишь для того, чтобы несколько вызовов одной функции считались разными и не склеивались.
|
2014-08-17 08:28:03 +00:00
|
|
|
|
*
|
2012-07-15 23:13:08 +00:00
|
|
|
|
* Пример:
|
|
|
|
|
* SELECT rand(), rand() - выдаст два одинаковых столбца.
|
|
|
|
|
* SELECT rand(1), rand(2) - выдаст два разных столбца.
|
|
|
|
|
*
|
|
|
|
|
* Некриптографические генераторы:
|
2014-08-17 08:28:03 +00:00
|
|
|
|
*
|
|
|
|
|
* rand - linear congruental generator 0 .. 2^32 - 1.
|
2012-07-15 23:13:08 +00:00
|
|
|
|
* rand64 - комбинирует несколько значений rand, чтобы получить значения из диапазона 0 .. 2^64 - 1.
|
|
|
|
|
*
|
2015-09-07 17:56:56 +00:00
|
|
|
|
* randConstant - служебная функция, выдаёт константный столбец со случайным значением.
|
|
|
|
|
*
|
2012-07-15 23:13:08 +00:00
|
|
|
|
* В качестве затравки используют время.
|
|
|
|
|
* Замечание: переинициализируется на каждый блок.
|
|
|
|
|
* Это значит, что таймер должен быть достаточного разрешения, чтобы выдавать разные значения на каждый блок.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
|
{
|
2014-08-17 08:28:03 +00:00
|
|
|
|
struct LinearCongruentialGenerator
|
|
|
|
|
{
|
|
|
|
|
/// Константы из man lrand48_r.
|
|
|
|
|
static constexpr UInt64 a = 0x5DEECE66D;
|
|
|
|
|
static constexpr UInt64 c = 0xB;
|
|
|
|
|
|
|
|
|
|
/// А эта - из head -c8 /dev/urandom | xxd -p
|
|
|
|
|
UInt64 current = 0x09826f4a081cee35ULL;
|
|
|
|
|
|
|
|
|
|
LinearCongruentialGenerator() {}
|
|
|
|
|
LinearCongruentialGenerator(UInt64 value) : current(value) {}
|
|
|
|
|
|
|
|
|
|
void seed(UInt64 value)
|
|
|
|
|
{
|
|
|
|
|
current = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UInt32 next()
|
|
|
|
|
{
|
|
|
|
|
current = current * a + c;
|
|
|
|
|
return current >> 16;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void seed(LinearCongruentialGenerator & generator, intptr_t additional_seed)
|
2012-07-15 23:13:08 +00:00
|
|
|
|
{
|
|
|
|
|
struct timespec times;
|
2012-07-15 23:16:35 +00:00
|
|
|
|
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, ×))
|
2012-07-15 23:13:08 +00:00
|
|
|
|
throwFromErrno("Cannot clock_gettime.", ErrorCodes::CANNOT_CLOCK_GETTIME);
|
|
|
|
|
|
2014-08-17 08:28:03 +00:00
|
|
|
|
generator.seed(intHash64(times.tv_nsec ^ intHash64(additional_seed)));
|
2012-07-15 23:13:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct RandImpl
|
|
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using ReturnType = UInt32;
|
2014-08-17 08:28:03 +00:00
|
|
|
|
|
2016-04-15 00:33:21 +00:00
|
|
|
|
static void execute(PaddedPODArray<ReturnType> & res)
|
2012-07-15 23:13:08 +00:00
|
|
|
|
{
|
2014-08-17 08:28:03 +00:00
|
|
|
|
detail::LinearCongruentialGenerator generator0;
|
|
|
|
|
detail::LinearCongruentialGenerator generator1;
|
|
|
|
|
detail::LinearCongruentialGenerator generator2;
|
|
|
|
|
detail::LinearCongruentialGenerator generator3;
|
|
|
|
|
|
|
|
|
|
detail::seed(generator0, 0xfb4121280b2ab902ULL + reinterpret_cast<intptr_t>(&res[0]));
|
|
|
|
|
detail::seed(generator1, 0x0121cf76df39c673ULL + reinterpret_cast<intptr_t>(&res[0]));
|
|
|
|
|
detail::seed(generator2, 0x17ae86e3a19a602fULL + reinterpret_cast<intptr_t>(&res[0]));
|
|
|
|
|
detail::seed(generator3, 0x8b6e16da7e06d622ULL + reinterpret_cast<intptr_t>(&res[0]));
|
|
|
|
|
|
2012-07-15 23:13:08 +00:00
|
|
|
|
size_t size = res.size();
|
2014-08-17 08:28:03 +00:00
|
|
|
|
ReturnType * pos = &res[0];
|
|
|
|
|
ReturnType * end = pos + size;
|
|
|
|
|
ReturnType * end4 = pos + size / 4 * 4;
|
|
|
|
|
|
|
|
|
|
while (pos < end4)
|
|
|
|
|
{
|
|
|
|
|
pos[0] = generator0.next();
|
|
|
|
|
pos[1] = generator1.next();
|
|
|
|
|
pos[2] = generator2.next();
|
|
|
|
|
pos[3] = generator3.next();
|
|
|
|
|
pos += 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (pos < end)
|
2012-07-15 23:13:08 +00:00
|
|
|
|
{
|
2014-08-17 08:28:03 +00:00
|
|
|
|
pos[0] = generator0.next();
|
|
|
|
|
++pos;
|
2012-07-15 23:13:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Rand64Impl
|
|
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using ReturnType = UInt64;
|
2012-07-15 23:13:08 +00:00
|
|
|
|
|
2016-04-15 00:33:21 +00:00
|
|
|
|
static void execute(PaddedPODArray<ReturnType> & res)
|
2012-07-15 23:13:08 +00:00
|
|
|
|
{
|
2014-08-17 08:28:03 +00:00
|
|
|
|
detail::LinearCongruentialGenerator generator0;
|
|
|
|
|
detail::LinearCongruentialGenerator generator1;
|
|
|
|
|
detail::LinearCongruentialGenerator generator2;
|
|
|
|
|
detail::LinearCongruentialGenerator generator3;
|
|
|
|
|
|
|
|
|
|
detail::seed(generator0, 0xfb4121280b2ab902ULL + reinterpret_cast<intptr_t>(&res[0]));
|
|
|
|
|
detail::seed(generator1, 0x0121cf76df39c673ULL + reinterpret_cast<intptr_t>(&res[0]));
|
|
|
|
|
detail::seed(generator2, 0x17ae86e3a19a602fULL + reinterpret_cast<intptr_t>(&res[0]));
|
|
|
|
|
detail::seed(generator3, 0x8b6e16da7e06d622ULL + reinterpret_cast<intptr_t>(&res[0]));
|
2012-07-15 23:13:08 +00:00
|
|
|
|
|
|
|
|
|
size_t size = res.size();
|
2014-08-17 08:28:03 +00:00
|
|
|
|
ReturnType * pos = &res[0];
|
|
|
|
|
ReturnType * end = pos + size;
|
|
|
|
|
ReturnType * end2 = pos + size / 2 * 2;
|
|
|
|
|
|
|
|
|
|
while (pos < end2)
|
|
|
|
|
{
|
|
|
|
|
pos[0] = (static_cast<UInt64>(generator0.next()) << 32) | generator1.next();
|
|
|
|
|
pos[1] = (static_cast<UInt64>(generator2.next()) << 32) | generator3.next();
|
|
|
|
|
pos += 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (pos < end)
|
2012-07-15 23:13:08 +00:00
|
|
|
|
{
|
2014-08-17 08:28:03 +00:00
|
|
|
|
pos[0] = (static_cast<UInt64>(generator0.next()) << 32) | generator1.next();
|
|
|
|
|
++pos;
|
2012-07-15 23:13:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Impl, typename Name>
|
|
|
|
|
class FunctionRandom : public IFunction
|
|
|
|
|
{
|
|
|
|
|
private:
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using ToType = typename Impl::ReturnType;
|
2014-08-17 08:28:03 +00:00
|
|
|
|
|
2012-07-15 23:13:08 +00:00
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = Name::name;
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionRandom>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2012-07-15 23:13:08 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2012-07-15 23:13:08 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2012-07-15 23:13:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2016-07-06 09:47:55 +00:00
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2012-07-15 23:13:08 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() > 1)
|
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 0 or 1.",
|
2012-07-15 23:13:08 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<typename DataTypeFromFieldType<typename Impl::ReturnType>::Type>();
|
2012-07-15 23:13:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2016-07-06 09:47:55 +00:00
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2012-07-15 23:13:08 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
auto col_to = std::make_shared<ColumnVector<ToType>>();
|
2012-07-15 23:13:08 +00:00
|
|
|
|
block.getByPosition(result).column = col_to;
|
|
|
|
|
|
|
|
|
|
typename ColumnVector<ToType>::Container_t & vec_to = col_to->getData();
|
|
|
|
|
|
2013-06-08 20:19:29 +00:00
|
|
|
|
size_t size = block.rowsInFirstColumn();
|
2012-07-15 23:13:08 +00:00
|
|
|
|
vec_to.resize(size);
|
|
|
|
|
Impl::execute(vec_to);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2015-09-07 17:56:56 +00:00
|
|
|
|
template <typename Impl, typename Name>
|
|
|
|
|
class FunctionRandomConstant : public IFunction
|
|
|
|
|
{
|
|
|
|
|
private:
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using ToType = typename Impl::ReturnType;
|
2015-09-07 17:56:56 +00:00
|
|
|
|
|
|
|
|
|
/// Значение одно для разных блоков.
|
|
|
|
|
bool is_initialized = false;
|
|
|
|
|
ToType value;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = Name::name;
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionRandomConstant>(); }
|
2015-09-07 17:56:56 +00:00
|
|
|
|
|
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2015-09-07 17:56:56 +00:00
|
|
|
|
{
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2016-07-06 09:47:55 +00:00
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-09-07 17:56:56 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<typename DataTypeFromFieldType<typename Impl::ReturnType>::Type>();
|
2015-09-07 17:56:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2016-07-06 09:47:55 +00:00
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2015-09-07 17:56:56 +00:00
|
|
|
|
{
|
|
|
|
|
if (!is_initialized)
|
|
|
|
|
{
|
|
|
|
|
is_initialized = true;
|
|
|
|
|
typename ColumnVector<ToType>::Container_t vec_to(1);
|
|
|
|
|
Impl::execute(vec_to);
|
|
|
|
|
value = vec_to[0];
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnConst<ToType>>(block.rowsInFirstColumn(), value);
|
2015-09-07 17:56:56 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct NameRand { static constexpr auto name = "rand"; };
|
|
|
|
|
struct NameRand64 { static constexpr auto name = "rand64"; };
|
|
|
|
|
struct NameRandConstant { static constexpr auto name = "randConstant"; };
|
2012-07-15 23:13:08 +00:00
|
|
|
|
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using FunctionRand = FunctionRandom<RandImpl, NameRand> ;
|
|
|
|
|
using FunctionRand64 = FunctionRandom<Rand64Impl, NameRand64>;
|
|
|
|
|
using FunctionRandConstant = FunctionRandomConstant<RandImpl, NameRandConstant>;
|
2012-07-15 23:13:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|