2018-09-26 00:31:40 +00:00
|
|
|
#include <common/DateLUT.h>
|
|
|
|
|
2019-10-04 17:46:36 +00:00
|
|
|
#include <Core/Field.h>
|
|
|
|
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
|
2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-12-12 14:16:59 +00:00
|
|
|
class ExecutableFunctionToday : public IExecutableFunctionImpl
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-12-12 14:16:59 +00:00
|
|
|
explicit ExecutableFunctionToday(time_t time_) : day_value(time_) {}
|
2019-10-01 10:03:55 +00:00
|
|
|
|
|
|
|
String getName() const override { return "today"; }
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2019-12-09 19:35:45 +00:00
|
|
|
void execute(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2019-10-01 10:03:55 +00:00
|
|
|
block.getByPosition(result).column = DataTypeDate().createColumnConst(input_rows_count, day_value);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 10:03:55 +00:00
|
|
|
private:
|
|
|
|
DayNum day_value;
|
|
|
|
};
|
|
|
|
|
2019-12-09 19:35:45 +00:00
|
|
|
class FunctionBaseToday : public IFunctionBaseImpl
|
2019-10-01 10:03:55 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit FunctionBaseToday(DayNum day_value_) : day_value(day_value_), return_type(std::make_shared<DataTypeDate>()) {}
|
|
|
|
|
|
|
|
String getName() const override { return "today"; }
|
|
|
|
|
|
|
|
const DataTypes & getArgumentTypes() const override
|
|
|
|
{
|
|
|
|
static const DataTypes argument_types;
|
|
|
|
return argument_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DataTypePtr & getReturnType() const override
|
|
|
|
{
|
|
|
|
return return_type;
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2019-12-09 19:35:45 +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<ExecutableFunctionToday>(day_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-10-01 10:03:55 +00:00
|
|
|
private:
|
|
|
|
DayNum day_value;
|
|
|
|
DataTypePtr return_type;
|
|
|
|
};
|
|
|
|
|
2019-12-12 14:16:59 +00:00
|
|
|
class TodayOverloadResolver : public IFunctionOverloadResolverImpl
|
2019-10-01 10:03:55 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "today";
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
2019-10-10 14:38:08 +00:00
|
|
|
|
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
|
2019-10-01 10:03:55 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
2019-12-12 14:16:59 +00:00
|
|
|
static FunctionOverloadResolverImplPtr create(const Context &) { return std::make_unique<TodayOverloadResolver>(); }
|
2019-10-01 10:03:55 +00:00
|
|
|
|
2019-12-09 19:35:45 +00:00
|
|
|
DataTypePtr getReturnType(const DataTypes &) const override { return std::make_shared<DataTypeDate>(); }
|
2019-10-01 10:03:55 +00:00
|
|
|
|
2019-12-09 19:35:45 +00:00
|
|
|
FunctionBaseImplPtr build(const ColumnsWithTypeAndName &, const DataTypePtr &) const override
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
2019-12-09 19:35:45 +00:00
|
|
|
return std::make_unique<FunctionBaseToday>(DateLUT::instance().toDayNum(time(nullptr)));
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void registerFunctionToday(FunctionFactory & factory)
|
|
|
|
{
|
2019-12-12 14:16:59 +00:00
|
|
|
factory.registerFunction<TodayOverloadResolver>();
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|