1#include <common/DateLUT.h>
2
3#include <Core/Field.h>
4
5#include <DataTypes/DataTypeDate.h>
6
7#include <Functions/IFunctionImpl.h>
8#include <Functions/FunctionFactory.h>
9
10
11namespace DB
12{
13
14class ExecutableFunctionToday : public IExecutableFunctionImpl
15{
16public:
17 explicit ExecutableFunctionToday(time_t time_) : day_value(time_) {}
18
19 String getName() const override { return "today"; }
20
21 void execute(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override
22 {
23 block.getByPosition(result).column = DataTypeDate().createColumnConst(input_rows_count, day_value);
24 }
25
26private:
27 DayNum day_value;
28};
29
30class FunctionBaseToday : public IFunctionBaseImpl
31{
32public:
33 explicit FunctionBaseToday(DayNum day_value_) : day_value(day_value_), return_type(std::make_shared<DataTypeDate>()) {}
34
35 String getName() const override { return "today"; }
36
37 const DataTypes & getArgumentTypes() const override
38 {
39 static const DataTypes argument_types;
40 return argument_types;
41 }
42
43 const DataTypePtr & getReturnType() const override
44 {
45 return return_type;
46 }
47
48 ExecutableFunctionImplPtr prepare(const Block &, const ColumnNumbers &, size_t) const override
49 {
50 return std::make_unique<ExecutableFunctionToday>(day_value);
51 }
52
53 bool isDeterministic() const override { return false; }
54 bool isDeterministicInScopeOfQuery() const override { return true; }
55
56private:
57 DayNum day_value;
58 DataTypePtr return_type;
59};
60
61class TodayOverloadResolver : public IFunctionOverloadResolverImpl
62{
63public:
64 static constexpr auto name = "today";
65
66 String getName() const override { return name; }
67
68 bool isDeterministic() const override { return false; }
69
70 size_t getNumberOfArguments() const override { return 0; }
71
72 static FunctionOverloadResolverImplPtr create(const Context &) { return std::make_unique<TodayOverloadResolver>(); }
73
74 DataTypePtr getReturnType(const DataTypes &) const override { return std::make_shared<DataTypeDate>(); }
75
76 FunctionBaseImplPtr build(const ColumnsWithTypeAndName &, const DataTypePtr &) const override
77 {
78 return std::make_unique<FunctionBaseToday>(DateLUT::instance().toDayNum(time(nullptr)));
79 }
80};
81
82void registerFunctionToday(FunctionFactory & factory)
83{
84 factory.registerFunction<TodayOverloadResolver>();
85}
86
87}
88