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 | |
11 | namespace DB |
12 | { |
13 | |
14 | class ExecutableFunctionYesterday : public IExecutableFunctionImpl |
15 | { |
16 | public: |
17 | explicit ExecutableFunctionYesterday(time_t time_) : day_value(time_) {} |
18 | |
19 | String getName() const override { return "yesterday"; } |
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 | |
26 | private: |
27 | DayNum day_value; |
28 | }; |
29 | |
30 | class FunctionBaseYesterday : public IFunctionBaseImpl |
31 | { |
32 | public: |
33 | explicit FunctionBaseYesterday(DayNum day_value_) : day_value(day_value_), return_type(std::make_shared<DataTypeDate>()) {} |
34 | |
35 | String getName() const override { return "yesterday"; } |
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<ExecutableFunctionYesterday>(day_value); |
51 | } |
52 | |
53 | bool isDeterministic() const override { return false; } |
54 | bool isDeterministicInScopeOfQuery() const override { return true; } |
55 | |
56 | private: |
57 | DayNum day_value; |
58 | DataTypePtr return_type; |
59 | }; |
60 | |
61 | class YesterdayOverloadResolver : public IFunctionOverloadResolverImpl |
62 | { |
63 | public: |
64 | static constexpr auto name = "yesterday"; |
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<YesterdayOverloadResolver>(); } |
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 | auto day_num = DateLUT::instance().toDayNum(time(nullptr)) - 1; |
79 | return std::make_unique<FunctionBaseYesterday>(static_cast<DayNum>(day_num)); |
80 | } |
81 | }; |
82 | |
83 | void registerFunctionYesterday(FunctionFactory & factory) |
84 | { |
85 | factory.registerFunction<YesterdayOverloadResolver>(); |
86 | } |
87 | |
88 | } |
89 |