2021-09-13 12:19:37 +00:00
|
|
|
#pragma once
|
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-09-15 21:17:20 +00:00
|
|
|
/// Base class for constant functions
|
|
|
|
template<typename Derived, typename T, typename ColumnT>
|
|
|
|
class FunctionConstantBase : public IFunction
|
2021-09-13 12:19:37 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2021-09-15 21:17:20 +00:00
|
|
|
/// For server-level constants (uptime(), version(), etc)
|
|
|
|
explicit FunctionConstantBase(ContextPtr context, T && constant_value_)
|
2021-09-13 12:19:37 +00:00
|
|
|
: is_distributed(context->isDistributed())
|
2021-09-15 21:17:20 +00:00
|
|
|
, constant_value(std::forward<T>(constant_value_))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For real constants (pi(), e(), etc)
|
|
|
|
explicit FunctionConstantBase(const T & constant_value_)
|
|
|
|
: is_distributed(false)
|
|
|
|
, constant_value(constant_value_)
|
2021-09-13 12:19:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
2021-09-15 21:17:20 +00:00
|
|
|
return Derived::name;
|
2021-09-13 12:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
|
|
|
|
{
|
|
|
|
return std::make_shared<ColumnT>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
bool isDeterministicInScopeOfQuery() const override { return true; }
|
|
|
|
|
2021-09-15 21:17:20 +00:00
|
|
|
/// Some functions may return different values on different shards/replicas, so it's not constant for distributed query
|
2021-09-13 12:19:37 +00:00
|
|
|
bool isSuitableForConstantFolding() const override { return !is_distributed; }
|
|
|
|
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
|
|
|
|
{
|
2021-09-15 21:17:20 +00:00
|
|
|
return ColumnT().createColumnConst(input_rows_count, constant_value);
|
2021-09-13 12:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_distributed;
|
2021-09-15 21:17:20 +00:00
|
|
|
const T constant_value;
|
2021-09-13 12:19:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|