1 | #pragma once |
2 | |
3 | #include <DataStreams/IBlockInputStream.h> |
4 | #include <Core/ColumnWithTypeAndName.h> |
5 | |
6 | namespace DB |
7 | { |
8 | |
9 | /** Adds a materialized const column to the block with a specified value. |
10 | */ |
11 | template <typename T> |
12 | class AddingConstColumnBlockInputStream : public IBlockInputStream |
13 | { |
14 | public: |
15 | AddingConstColumnBlockInputStream( |
16 | BlockInputStreamPtr input_, |
17 | DataTypePtr data_type_, |
18 | T value_, |
19 | String column_name_) |
20 | : data_type(data_type_), value(value_), column_name(column_name_) |
21 | { |
22 | children.push_back(input_); |
23 | } |
24 | |
25 | String getName() const override { return "AddingConstColumn" ; } |
26 | |
27 | Block getHeader() const override |
28 | { |
29 | Block res = children.back()->getHeader(); |
30 | res.insert({data_type->createColumn(), data_type, column_name}); |
31 | return res; |
32 | } |
33 | |
34 | protected: |
35 | Block readImpl() override |
36 | { |
37 | Block res = children.back()->read(); |
38 | if (!res) |
39 | return res; |
40 | |
41 | res.insert({data_type->createColumnConst(res.rows(), value)->convertToFullColumnIfConst(), data_type, column_name}); |
42 | return res; |
43 | } |
44 | |
45 | private: |
46 | DataTypePtr data_type; |
47 | T value; |
48 | String column_name; |
49 | }; |
50 | |
51 | } |
52 | |