1 | #include <Columns/getLeastSuperColumn.h> |
---|---|
2 | #include <Columns/IColumn.h> |
3 | #include <Columns/ColumnConst.h> |
4 | #include <Common/assert_cast.h> |
5 | #include <DataTypes/getLeastSupertype.h> |
6 | |
7 | |
8 | namespace DB |
9 | { |
10 | |
11 | namespace ErrorCodes |
12 | { |
13 | extern const int LOGICAL_ERROR; |
14 | } |
15 | |
16 | static bool sameConstants(const IColumn & a, const IColumn & b) |
17 | { |
18 | return assert_cast<const ColumnConst &>(a).getField() == assert_cast<const ColumnConst &>(b).getField(); |
19 | } |
20 | |
21 | ColumnWithTypeAndName getLeastSuperColumn(const std::vector<const ColumnWithTypeAndName *> & columns) |
22 | { |
23 | if (columns.empty()) |
24 | throw Exception("Logical error: no src columns for supercolumn", ErrorCodes::LOGICAL_ERROR); |
25 | |
26 | ColumnWithTypeAndName result = *columns[0]; |
27 | |
28 | /// Determine common type. |
29 | |
30 | size_t num_const = 0; |
31 | DataTypes types(columns.size()); |
32 | for (size_t i = 0; i < columns.size(); ++i) |
33 | { |
34 | types[i] = columns[i]->type; |
35 | if (isColumnConst(*columns[i]->column)) |
36 | ++num_const; |
37 | } |
38 | |
39 | result.type = getLeastSupertype(types); |
40 | |
41 | /// Create supertype column saving constness if possible. |
42 | |
43 | bool save_constness = false; |
44 | if (columns.size() == num_const) |
45 | { |
46 | save_constness = true; |
47 | for (size_t i = 1; i < columns.size(); ++i) |
48 | { |
49 | const ColumnWithTypeAndName & first = *columns[0]; |
50 | const ColumnWithTypeAndName & other = *columns[i]; |
51 | |
52 | if (!sameConstants(*first.column, *other.column)) |
53 | { |
54 | save_constness = false; |
55 | break; |
56 | } |
57 | } |
58 | } |
59 | |
60 | if (save_constness) |
61 | result.column = result.type->createColumnConst(0, assert_cast<const ColumnConst &>(*columns[0]->column).getField()); |
62 | else |
63 | result.column = result.type->createColumn(); |
64 | |
65 | return result; |
66 | } |
67 | |
68 | } |
69 |