1#pragma once
2
3#include <unordered_map>
4#include <DataStreams/IBlockInputStream.h>
5
6
7namespace DB
8{
9
10/** Convert one block structure to another:
11 *
12 * Leaves only necessary columns;
13 *
14 * Columns are searched in source first by name;
15 * and if there is no column with same name, then by position.
16 *
17 * Converting types of matching columns (with CAST function).
18 *
19 * Materializing columns which are const in source and non-const in result,
20 * throw if they are const in result and non const in source,
21 * or if they are const and have different values.
22 */
23class ConvertingBlockInputStream : public IBlockInputStream
24{
25public:
26 enum class MatchColumnsMode
27 {
28 /// Require same number of columns in source and result. Match columns by corresponding positions, regardless to names.
29 Position,
30 /// Find columns in source by their names. Allow excessive columns in source.
31 Name
32 };
33
34 ConvertingBlockInputStream(
35 const Context & context,
36 const BlockInputStreamPtr & input,
37 const Block & result_header,
38 MatchColumnsMode mode);
39
40 String getName() const override { return "Converting"; }
41 Block getHeader() const override { return header; }
42
43private:
44 Block readImpl() override;
45
46 const Context & context;
47 Block header;
48
49 /// How to construct result block. Position in source block, where to get each column.
50 using Conversion = std::vector<size_t>;
51 Conversion conversion;
52};
53
54}
55