1 | #include <Functions/IFunctionImpl.h> |
---|---|
2 | #include <Functions/FunctionFactory.h> |
3 | #include <Core/Field.h> |
4 | |
5 | |
6 | namespace DB |
7 | { |
8 | |
9 | /// Returns global default value for type of passed argument (example: 0 for numeric types, '' for String). |
10 | class FunctionDefaultValueOfArgumentType : public IFunction |
11 | { |
12 | public: |
13 | static constexpr auto name = "defaultValueOfArgumentType"; |
14 | static FunctionPtr create(const Context &) |
15 | { |
16 | return std::make_shared<FunctionDefaultValueOfArgumentType>(); |
17 | } |
18 | |
19 | String getName() const override |
20 | { |
21 | return name; |
22 | } |
23 | |
24 | bool useDefaultImplementationForNulls() const override { return false; } |
25 | |
26 | size_t getNumberOfArguments() const override |
27 | { |
28 | return 1; |
29 | } |
30 | |
31 | DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override |
32 | { |
33 | return arguments[0]; |
34 | } |
35 | |
36 | void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override |
37 | { |
38 | const IDataType & type = *block.getByPosition(arguments[0]).type; |
39 | block.getByPosition(result).column = type.createColumnConst(input_rows_count, type.getDefault()); |
40 | } |
41 | |
42 | ColumnPtr getResultIfAlwaysReturnsConstantAndHasArguments(const Block & block, const ColumnNumbers & arguments) const override |
43 | { |
44 | const IDataType & type = *block.getByPosition(arguments[0]).type; |
45 | return type.createColumnConst(1, type.getDefault()); |
46 | } |
47 | }; |
48 | |
49 | |
50 | void registerFunctionDefaultValueOfArgumentType(FunctionFactory & factory) |
51 | { |
52 | factory.registerFunction<FunctionDefaultValueOfArgumentType>(); |
53 | } |
54 | |
55 | } |
56 |