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