1#include <Functions/IFunctionImpl.h>
2#include <Functions/FunctionFactory.h>
3#include <DataTypes/DataTypeString.h>
4#include <Common/DNSResolver.h>
5#include <Core/Field.h>
6
7
8namespace DB
9{
10
11/// Get the host name. Is is constant on single server, but is not constant in distributed queries.
12class FunctionHostName : public IFunction
13{
14public:
15 static constexpr auto name = "hostName";
16 static FunctionPtr create(const Context &)
17 {
18 return std::make_shared<FunctionHostName>();
19 }
20
21 String getName() const override
22 {
23 return name;
24 }
25
26 bool isDeterministic() const override { return false; }
27
28 bool isDeterministicInScopeOfQuery() const override
29 {
30 return false;
31 }
32
33 size_t getNumberOfArguments() const override
34 {
35 return 0;
36 }
37
38 DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
39 {
40 return std::make_shared<DataTypeString>();
41 }
42
43 /** convertToFullColumn needed because in distributed query processing,
44 * each server returns its own value.
45 */
46 void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override
47 {
48 block.getByPosition(result).column = block.getByPosition(result).type->createColumnConst(
49 input_rows_count, DNSResolver::instance().getHostName())->convertToFullColumnIfConst();
50 }
51};
52
53
54void registerFunctionHostName(FunctionFactory & factory)
55{
56 factory.registerFunction<FunctionHostName>();
57}
58
59}
60