1 | #include <Functions/IFunctionImpl.h> |
2 | #include <Functions/FunctionFactory.h> |
3 | #include <Core/Field.h> |
4 | #include <DataTypes/DataTypeString.h> |
5 | |
6 | |
7 | namespace DB |
8 | { |
9 | |
10 | /** toTypeName(x) - get the type name |
11 | * Returns name of IDataType instance (name of data type). |
12 | */ |
13 | class ExecutableFunctionToTypeName : public IExecutableFunctionImpl |
14 | { |
15 | public: |
16 | static constexpr auto name = "toTypeName" ; |
17 | String getName() const override { return name; } |
18 | |
19 | bool useDefaultImplementationForNulls() const override { return false; } |
20 | bool useDefaultImplementationForLowCardinalityColumns() const override { return false; } |
21 | |
22 | /// Execute the function on the block. |
23 | void execute(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override |
24 | { |
25 | block.getByPosition(result).column |
26 | = DataTypeString().createColumnConst(input_rows_count, block.getByPosition(arguments[0]).type->getName()); |
27 | } |
28 | }; |
29 | |
30 | |
31 | class BaseFunctionToTypeName : public IFunctionBaseImpl |
32 | { |
33 | public: |
34 | BaseFunctionToTypeName(DataTypes argument_types_, DataTypePtr return_type_) |
35 | : argument_types(std::move(argument_types_)), return_type(std::move(return_type_)) {} |
36 | |
37 | static constexpr auto name = "toTypeName" ; |
38 | String getName() const override { return name; } |
39 | |
40 | bool isDeterministic() const override { return true; } |
41 | bool isDeterministicInScopeOfQuery() const override { return true; } |
42 | |
43 | const DataTypes & getArgumentTypes() const override { return argument_types; } |
44 | const DataTypePtr & getReturnType() const override { return return_type; } |
45 | |
46 | ExecutableFunctionImplPtr prepare(const Block &, const ColumnNumbers &, size_t) const override |
47 | { |
48 | return std::make_unique<ExecutableFunctionToTypeName>(); |
49 | } |
50 | |
51 | ColumnPtr getResultIfAlwaysReturnsConstantAndHasArguments(const Block &, const ColumnNumbers &) const override |
52 | { |
53 | return DataTypeString().createColumnConst(1, argument_types.at(0)->getName()); |
54 | } |
55 | |
56 | private: |
57 | DataTypes argument_types; |
58 | DataTypePtr return_type; |
59 | }; |
60 | |
61 | |
62 | class FunctionToTypeNameBuilder : public IFunctionOverloadResolverImpl |
63 | { |
64 | public: |
65 | static constexpr auto name = "toTypeName" ; |
66 | String getName() const override { return name; } |
67 | static FunctionOverloadResolverImplPtr create(const Context &) { return std::make_unique<FunctionToTypeNameBuilder>(); } |
68 | |
69 | size_t getNumberOfArguments() const override { return 1; } |
70 | |
71 | DataTypePtr getReturnType(const DataTypes &) const override { return std::make_shared<DataTypeString>(); } |
72 | |
73 | FunctionBaseImplPtr build(const ColumnsWithTypeAndName & arguments, const DataTypePtr & return_type) const override |
74 | { |
75 | DataTypes types; |
76 | types.reserve(arguments.size()); |
77 | for (auto & elem : arguments) |
78 | types.emplace_back(elem.type); |
79 | |
80 | return std::make_unique<BaseFunctionToTypeName>(types, return_type); |
81 | } |
82 | |
83 | bool useDefaultImplementationForNulls() const override { return false; } |
84 | bool useDefaultImplementationForLowCardinalityColumns() const override { return false; } |
85 | ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; } |
86 | }; |
87 | |
88 | |
89 | void registerFunctionToTypeName(FunctionFactory & factory) |
90 | { |
91 | factory.registerFunction<FunctionToTypeNameBuilder>(); |
92 | } |
93 | |
94 | } |
95 | |