ClickHouse/src/Functions/now.cpp

94 lines
2.5 KiB
C++
Raw Normal View History

#include <DataTypes/DataTypeDateTime.h>
#include <Functions/IFunctionImpl.h>
2019-09-26 15:12:40 +00:00
#include <Core/DecimalFunctions.h>
#include <Functions/FunctionFactory.h>
2019-10-04 17:46:36 +00:00
#include <Core/Field.h>
2019-09-26 15:12:40 +00:00
#include <time.h>
namespace DB
{
2020-09-07 18:00:37 +00:00
namespace
{
2020-09-07 18:00:37 +00:00
/// Get the current time. (It is a constant, it is evaluated once for the entire query.)
class ExecutableFunctionNow : public IExecutableFunctionImpl
{
public:
explicit ExecutableFunctionNow(time_t time_) : time_value(time_) {}
String getName() const override { return "now"; }
void execute(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override
{
block.getByPosition(result).column = DataTypeDateTime().createColumnConst(
input_rows_count,
static_cast<UInt64>(time_value));
}
private:
time_t time_value;
};
class FunctionBaseNow : public IFunctionBaseImpl
{
public:
explicit FunctionBaseNow(time_t time_) : time_value(time_), return_type(std::make_shared<DataTypeDateTime>()) {}
String getName() const override { return "now"; }
const DataTypes & getArgumentTypes() const override
{
static const DataTypes argument_types;
return argument_types;
}
const DataTypePtr & getReturnType() const override
{
return return_type;
}
ExecutableFunctionImplPtr prepare(const Block &, const ColumnNumbers &, size_t) const override
{
return std::make_unique<ExecutableFunctionNow>(time_value);
}
bool isDeterministic() const override { return false; }
bool isDeterministicInScopeOfQuery() const override { return true; }
private:
time_t time_value;
DataTypePtr return_type;
};
class NowOverloadResolver : public IFunctionOverloadResolverImpl
{
public:
static constexpr auto name = "now";
String getName() const override { return name; }
bool isDeterministic() const override { return false; }
size_t getNumberOfArguments() const override { return 0; }
static FunctionOverloadResolverImplPtr create(const Context &) { return std::make_unique<NowOverloadResolver>(); }
DataTypePtr getReturnType(const DataTypes &) const override { return std::make_shared<DataTypeDateTime>(); }
2019-12-09 14:41:55 +00:00
FunctionBaseImplPtr build(const ColumnsWithTypeAndName &, const DataTypePtr &) const override
{
2019-12-09 14:41:55 +00:00
return std::make_unique<FunctionBaseNow>(time(nullptr));
}
};
2020-09-07 18:00:37 +00:00
}
void registerFunctionNow(FunctionFactory & factory)
{
factory.registerFunction<NowOverloadResolver>(FunctionFactory::CaseInsensitive);
}
}