2018-12-02 01:57:01 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Functions/FunctionsRandom.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename ToType, typename Name>
|
2019-10-01 10:03:55 +00:00
|
|
|
class PreparedFunctionRandomConstant : public PreparedFunctionImpl
|
2018-12-02 01:57:01 +00:00
|
|
|
{
|
2019-10-01 10:03:55 +00:00
|
|
|
public:
|
|
|
|
explicit PreparedFunctionRandomConstant(ToType value_) : value(value_) {}
|
|
|
|
|
|
|
|
String getName() const override { return Name::name; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override
|
|
|
|
{
|
|
|
|
block.getByPosition(result).column = DataTypeNumber<ToType>().createColumnConst(input_rows_count, value);
|
|
|
|
}
|
|
|
|
|
2018-12-02 01:57:01 +00:00
|
|
|
private:
|
|
|
|
ToType value;
|
2019-10-01 10:03:55 +00:00
|
|
|
};
|
2018-12-02 01:57:01 +00:00
|
|
|
|
2019-10-01 10:03:55 +00:00
|
|
|
template <typename ToType, typename Name>
|
|
|
|
class FunctionBaseRandomConstant : public IFunctionBase
|
|
|
|
{
|
2018-12-02 01:57:01 +00:00
|
|
|
public:
|
2019-10-01 10:03:55 +00:00
|
|
|
explicit FunctionBaseRandomConstant(ToType value_, DataTypes argument_types_)
|
|
|
|
: value(value_)
|
|
|
|
, argument_types(std::move(argument_types_))
|
|
|
|
, return_type(std::make_shared<DataTypeNumber<ToType>>()) {}
|
|
|
|
|
|
|
|
String getName() const override { return Name::name; }
|
|
|
|
|
|
|
|
const DataTypes & getArgumentTypes() const override
|
|
|
|
{
|
|
|
|
return argument_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DataTypePtr & getReturnType() const override
|
|
|
|
{
|
|
|
|
return return_type;
|
|
|
|
}
|
2018-12-02 01:57:01 +00:00
|
|
|
|
2019-10-01 10:03:55 +00:00
|
|
|
PreparedFunctionPtr prepare(const Block &, const ColumnNumbers &, size_t) const override
|
2018-12-02 01:57:01 +00:00
|
|
|
{
|
2019-10-01 10:10:07 +00:00
|
|
|
return std::make_shared<PreparedFunctionRandomConstant<ToType, Name>>(value);
|
2018-12-02 01:57:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 10:03:55 +00:00
|
|
|
bool isDeterministic() const override { return false; }
|
2019-10-10 14:38:08 +00:00
|
|
|
bool isDeterministicInScopeOfQuery() const override { return true; }
|
2019-10-01 10:03:55 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ToType value;
|
|
|
|
DataTypes argument_types;
|
|
|
|
DataTypePtr return_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ToType, typename Name>
|
|
|
|
class FunctionBuilderRandomConstant : public FunctionBuilderImpl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Name::name;
|
2019-10-01 10:11:16 +00:00
|
|
|
String getName() const override { return name; }
|
2019-10-01 10:03:55 +00:00
|
|
|
|
2019-10-10 14:38:08 +00:00
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
|
2018-12-02 01:57:01 +00:00
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
2019-10-01 10:03:55 +00:00
|
|
|
void checkNumberOfArguments(size_t number_of_arguments) const override
|
2018-12-02 01:57:01 +00:00
|
|
|
{
|
2019-10-01 10:03:55 +00:00
|
|
|
if (number_of_arguments > 1)
|
2018-12-02 01:57:01 +00:00
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2019-10-01 10:03:55 +00:00
|
|
|
+ toString(number_of_arguments) + ", should be 0 or 1.",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
}
|
2018-12-02 01:57:01 +00:00
|
|
|
|
2019-10-01 10:03:55 +00:00
|
|
|
static FunctionBuilderPtr create(const Context &)
|
|
|
|
{
|
2019-10-01 10:10:07 +00:00
|
|
|
return std::make_shared<FunctionBuilderRandomConstant<ToType, Name>>();
|
2018-12-02 01:57:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 10:03:55 +00:00
|
|
|
protected:
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes &) const override { return std::make_shared<DataTypeNumber<ToType>>(); }
|
|
|
|
|
|
|
|
FunctionBasePtr buildImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &) const override
|
2018-12-02 01:57:01 +00:00
|
|
|
{
|
2019-10-01 10:03:55 +00:00
|
|
|
DataTypes argument_types;
|
|
|
|
|
|
|
|
if (!arguments.empty())
|
|
|
|
argument_types.emplace_back(arguments.back().type);
|
|
|
|
|
|
|
|
typename ColumnVector<ToType>::Container vec_to(1);
|
|
|
|
RandImpl::execute(reinterpret_cast<char *>(vec_to.data()), sizeof(ToType));
|
|
|
|
ToType value = vec_to[0];
|
|
|
|
|
2019-10-01 10:10:07 +00:00
|
|
|
return std::make_shared<FunctionBaseRandomConstant<ToType, Name>>(value, argument_types);
|
2018-12-02 01:57:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-01 10:03:55 +00:00
|
|
|
|
2018-12-02 01:57:01 +00:00
|
|
|
struct NameRandConstant { static constexpr auto name = "randConstant"; };
|
2019-10-01 10:03:55 +00:00
|
|
|
using FunctionBuilderRandConstant = FunctionBuilderRandomConstant<UInt32, NameRandConstant>;
|
2018-12-02 01:57:01 +00:00
|
|
|
|
|
|
|
void registerFunctionRandConstant(FunctionFactory & factory)
|
|
|
|
{
|
2019-10-01 10:03:55 +00:00
|
|
|
factory.registerFunction<FunctionBuilderRandConstant>();
|
2018-12-02 01:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|