ClickHouse/src/Functions/randConstant.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
3.8 KiB
C++
Raw Normal View History

#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/FunctionsRandom.h>
namespace DB
{
2020-02-25 18:10:48 +00:00
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
2020-09-07 18:00:37 +00:00
namespace
{
template <typename ToType, typename Name>
2021-05-15 17:33:15 +00:00
class ExecutableFunctionRandomConstant : public IExecutableFunction
{
public:
explicit ExecutableFunctionRandomConstant(ToType value_) : value(value_) {}
String getName() const override { return Name::name; }
2019-12-13 13:47:28 +00:00
bool useDefaultImplementationForNulls() const override { return false; }
2021-05-15 17:33:15 +00:00
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
{
2020-10-19 15:27:41 +00:00
return DataTypeNumber<ToType>().createColumnConst(input_rows_count, value);
}
private:
ToType value;
};
template <typename ToType, typename Name>
2021-05-15 17:33:15 +00:00
class FunctionBaseRandomConstant : public IFunctionBase
{
public:
explicit FunctionBaseRandomConstant(ToType value_, DataTypes argument_types_, DataTypePtr return_type_)
: value(value_)
, argument_types(std::move(argument_types_))
, return_type(std::move(return_type_)) {}
String getName() const override { return Name::name; }
const DataTypes & getArgumentTypes() const override
{
return argument_types;
}
2020-10-19 15:27:41 +00:00
const DataTypePtr & getResultType() const override
{
return return_type;
}
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
2021-05-15 17:33:15 +00:00
ExecutableFunctionPtr prepare(const ColumnsWithTypeAndName &) const override
{
return std::make_unique<ExecutableFunctionRandomConstant<ToType, Name>>(value);
}
bool isDeterministic() const override
{
return false;
}
private:
ToType value;
DataTypes argument_types;
DataTypePtr return_type;
};
template <typename ToType, typename Name>
2021-05-15 17:33:15 +00:00
class RandomConstantOverloadResolver : public IFunctionOverloadResolver
{
public:
static constexpr auto name = Name::name;
String getName() const override { return name; }
bool isDeterministic() const override { return false; }
bool useDefaultImplementationForNulls() const override { return false; }
bool isVariadic() const override { return true; }
size_t getNumberOfArguments() const override { return 0; }
2021-06-01 12:20:52 +00:00
static FunctionOverloadResolverPtr create(ContextPtr)
{
return std::make_unique<RandomConstantOverloadResolver<ToType, Name>>();
}
2021-05-15 17:33:15 +00:00
DataTypePtr getReturnTypeImpl(const DataTypes & data_types) const override
{
size_t number_of_arguments = data_types.size();
if (number_of_arguments > 1)
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
+ toString(number_of_arguments) + ", should be 0 or 1.",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
return std::make_shared<DataTypeNumber<ToType>>();
}
2021-05-15 17:33:15 +00:00
FunctionBasePtr buildImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & return_type) const override
{
DataTypes argument_types;
if (!arguments.empty())
argument_types.emplace_back(arguments.back().type);
typename ColumnVector<ToType>::Container vec_to(1);
2020-05-20 12:42:21 +00:00
TargetSpecific::Default::RandImpl::execute(reinterpret_cast<char *>(vec_to.data()), sizeof(ToType));
ToType value = vec_to[0];
return std::make_unique<FunctionBaseRandomConstant<ToType, Name>>(value, argument_types, return_type);
}
};
struct NameRandConstant { static constexpr auto name = "randConstant"; };
using FunctionBuilderRandConstant = RandomConstantOverloadResolver<UInt32, NameRandConstant>;
2020-09-07 18:00:37 +00:00
}
REGISTER_FUNCTION(RandConstant)
{
factory.registerFunction<FunctionBuilderRandConstant>();
}
}