Add function canonicalRand (#43124)

* add function canonicalRand

* add perf test

* revert rand.xml
This commit is contained in:
李扬 2022-11-15 07:27:19 +08:00 committed by GitHub
parent ef45889ecb
commit 1de5bb2392
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 1 deletions

View File

@ -24,6 +24,11 @@ Returns a pseudo-random UInt64 number, evenly distributed among all UInt64-type
Uses a linear congruential generator.
## canonicalRand
The function generates pseudo random results with independent and identically distributed uniformly distributed values in [0, 1).
Non-deterministic. Return type is Float64.
## randConstant
Produces a constant column with a random value.

View File

@ -0,0 +1,59 @@
#include <Common/randomSeed.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionsRandom.h>
#include <pcg-random/pcg_random.hpp>
namespace DB
{
namespace
{
struct CanonicalRandImpl
{
static void execute(char * output, size_t size)
{
pcg64_fast rng1(randomSeed());
pcg64_fast rng2(randomSeed());
std::uniform_real_distribution<Float64> distribution1(min, max);
std::uniform_real_distribution<Float64> distribution2(min, max);
for (const char * end = output + size; output < end; output += 16)
{
unalignedStore<Float64>(output, distribution1(rng1));
unalignedStore<Float64>(output + 8, distribution2(rng2));
}
}
/// It is guaranteed (by PaddedPODArray) that we can overwrite up to 15 bytes after end.
private:
const static constexpr Float64 min = 0;
const static constexpr Float64 max = 1;
};
struct NameCanonicalRand
{
static constexpr auto name = "canonicalRand";
};
class FunctionCanonicalRand : public FunctionRandomImpl<CanonicalRandImpl, Float64, NameCanonicalRand>
{
public:
static FunctionPtr create(ContextPtr /*context*/) { return std::make_shared<FunctionCanonicalRand>(); }
};
}
REGISTER_FUNCTION(CanonicalRand)
{
factory.registerFunction<FunctionCanonicalRand>({
R"(
The function generates pseudo random results with independent and identically distributed uniformly distributed values in [0, 1).
Non-deterministic. Return type is Float64.
)",
Documentation::Examples{{"canonicalRand", "SELECT canonicalRand()"}},
Documentation::Categories{"Mathematical"}});
}
}

View File

@ -10,5 +10,6 @@
<query>SELECT count() FROM (SELECT rand() FROM zeros(1000000000)) </query>
<query>SELECT count() FROM (SELECT rand64() FROM zeros(1000000000)) </query>
<query>SELECT count() FROM (SELECT randUniform(0, 1) FROM zeros(100000000)) </query>
<query>SELECT count() FROM (SELECT generateUUIDv4() FROM zeros( 100000000)) </query>
</test>

View File

@ -1,8 +1,12 @@
UInt32
Float64
UInt32
UInt32
UInt32
Float64
UInt32
0
0
0
0
0
0

View File

@ -1,9 +1,13 @@
select toTypeName(rand(cast(4 as Nullable(UInt8))));
select toTypeName(canonicalRand(CAST(4 as Nullable(UInt8))));
select toTypeName(randConstant(CAST(4 as Nullable(UInt8))));
select toTypeName(rand(Null));
select toTypeName(canonicalRand(Null));
select toTypeName(randConstant(Null));
select rand(cast(4 as Nullable(UInt8))) * 0;
select canonicalRand(cast(4 as Nullable(UInt8))) * 0;
select randConstant(CAST(4 as Nullable(UInt8))) * 0;
select rand(Null) * 0;
select canonicalRand(Null) * 0;
select randConstant(Null) * 0;