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