1 | #include <Functions/IFunctionImpl.h> |
---|---|
2 | #include <Functions/FunctionFactory.h> |
3 | #include <DataTypes/DataTypesNumber.h> |
4 | #include <Interpreters/Context.h> |
5 | |
6 | |
7 | namespace DB |
8 | { |
9 | |
10 | /** Returns server uptime in seconds. |
11 | */ |
12 | class FunctionUptime : public IFunction |
13 | { |
14 | public: |
15 | static constexpr auto name = "uptime"; |
16 | static FunctionPtr create(const Context & context) |
17 | { |
18 | return std::make_shared<FunctionUptime>(context.getUptimeSeconds()); |
19 | } |
20 | |
21 | explicit FunctionUptime(time_t uptime_) : uptime(uptime_) |
22 | { |
23 | } |
24 | |
25 | String getName() const override |
26 | { |
27 | return name; |
28 | } |
29 | |
30 | size_t getNumberOfArguments() const override |
31 | { |
32 | return 0; |
33 | } |
34 | |
35 | DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override |
36 | { |
37 | return std::make_shared<DataTypeUInt32>(); |
38 | } |
39 | |
40 | bool isDeterministic() const override { return false; } |
41 | |
42 | void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override |
43 | { |
44 | block.getByPosition(result).column = DataTypeUInt32().createColumnConst(input_rows_count, static_cast<UInt64>(uptime)); |
45 | } |
46 | |
47 | private: |
48 | time_t uptime; |
49 | }; |
50 | |
51 | |
52 | void registerFunctionUptime(FunctionFactory & factory) |
53 | { |
54 | factory.registerFunction<FunctionUptime>(); |
55 | } |
56 | |
57 | } |
58 |