1 | #include <Functions/IFunctionImpl.h> |
---|---|
2 | #include <Functions/FunctionFactory.h> |
3 | #include <DataTypes/DataTypeTuple.h> |
4 | #include <Columns/ColumnTuple.h> |
5 | #include <memory> |
6 | |
7 | |
8 | namespace DB |
9 | { |
10 | |
11 | namespace ErrorCodes |
12 | { |
13 | extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; |
14 | } |
15 | |
16 | |
17 | /** tuple(x, y, ...) is a function that allows you to group several columns |
18 | * tupleElement(tuple, n) is a function that allows you to retrieve a column from tuple. |
19 | */ |
20 | |
21 | class FunctionTuple : public IFunction |
22 | { |
23 | public: |
24 | static constexpr auto name = "tuple"; |
25 | |
26 | static FunctionPtr create(const Context &) |
27 | { |
28 | return std::make_shared<FunctionTuple>(); |
29 | } |
30 | |
31 | String getName() const override |
32 | { |
33 | return name; |
34 | } |
35 | |
36 | bool isVariadic() const override |
37 | { |
38 | return true; |
39 | } |
40 | |
41 | size_t getNumberOfArguments() const override |
42 | { |
43 | return 0; |
44 | } |
45 | |
46 | bool isInjective(const Block &) override |
47 | { |
48 | return true; |
49 | } |
50 | |
51 | bool useDefaultImplementationForNulls() const override { return false; } |
52 | bool useDefaultImplementationForConstants() const override { return true; } |
53 | |
54 | DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override |
55 | { |
56 | if (arguments.size() < 1) |
57 | throw Exception("Function "+ getName() + " requires at least one argument.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); |
58 | |
59 | return std::make_shared<DataTypeTuple>(arguments); |
60 | } |
61 | |
62 | void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override |
63 | { |
64 | size_t tuple_size = arguments.size(); |
65 | Columns tuple_columns(tuple_size); |
66 | for (size_t i = 0; i < tuple_size; ++i) |
67 | { |
68 | /** If tuple is mixed of constant and not constant columns, |
69 | * convert all to non-constant columns, |
70 | * because many places in code expect all non-constant columns in non-constant tuple. |
71 | */ |
72 | tuple_columns[i] = block.getByPosition(arguments[i]).column->convertToFullColumnIfConst(); |
73 | } |
74 | block.getByPosition(result).column = ColumnTuple::create(tuple_columns); |
75 | } |
76 | }; |
77 | |
78 | void registerFunctionTuple(FunctionFactory & factory) |
79 | { |
80 | factory.registerFunction<FunctionTuple>(); |
81 | } |
82 | |
83 | } |
84 |