|
1 | 1 | #include "../columns/itemview.h" |
2 | 2 |
|
| 3 | +#include <algorithm> |
| 4 | +#include <sstream> |
| 5 | + |
| 6 | +namespace { |
| 7 | + |
| 8 | +template <typename Container> |
| 9 | +std::string ContainerToString(Container container, const char * separator = ", ") { |
| 10 | + std::stringstream sstr; |
| 11 | + const auto end = std::end(container); |
| 12 | + for (auto i = std::begin(container); i != end; /*intentionally no ++i*/) { |
| 13 | + const auto & elem = *i; |
| 14 | + sstr << elem; |
| 15 | + |
| 16 | + if (++i != end) { |
| 17 | + sstr << separator; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + return sstr.str(); |
| 22 | +} |
| 23 | + |
| 24 | +} |
| 25 | + |
3 | 26 | namespace clickhouse { |
4 | 27 |
|
5 | 28 | void ItemView::ValidateData(Type::Code type, DataType data) { |
6 | | - int expected_size = 0; |
| 29 | + |
| 30 | + auto AssertSize = [type, &data](std::initializer_list<int> allowed_sizes) -> void { |
| 31 | + const auto end = std::end(allowed_sizes); |
| 32 | + if (std::find(std::begin(allowed_sizes), end, static_cast<int>(data.size())) == end) { |
| 33 | + throw AssertionError(std::string("ItemView value size mismatch for ") |
| 34 | + + Type::TypeName(type) |
| 35 | + + " expected: " + ContainerToString(allowed_sizes, " or ") |
| 36 | + + ", got: " + std::to_string(data.size())); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
7 | 40 | switch (type) { |
8 | 41 | case Type::Code::Void: |
9 | | - expected_size = 0; |
10 | | - break; |
| 42 | + return AssertSize({0}); |
11 | 43 |
|
12 | 44 | case Type::Code::Int8: |
13 | 45 | case Type::Code::UInt8: |
14 | 46 | case Type::Code::Enum8: |
15 | | - expected_size = 1; |
16 | | - break; |
| 47 | + return AssertSize({1}); |
17 | 48 |
|
18 | 49 | case Type::Code::Int16: |
19 | 50 | case Type::Code::UInt16: |
20 | 51 | case Type::Code::Date: |
21 | 52 | case Type::Code::Enum16: |
22 | | - expected_size = 2; |
23 | | - break; |
| 53 | + return AssertSize({2}); |
24 | 54 |
|
25 | 55 | case Type::Code::Int32: |
26 | 56 | case Type::Code::UInt32: |
27 | 57 | case Type::Code::Float32: |
28 | 58 | case Type::Code::DateTime: |
29 | 59 | case Type::Code::IPv4: |
30 | 60 | case Type::Code::Decimal32: |
31 | | - expected_size = 4; |
32 | | - break; |
| 61 | + return AssertSize({4}); |
33 | 62 |
|
34 | 63 | case Type::Code::Int64: |
35 | 64 | case Type::Code::UInt64: |
36 | 65 | case Type::Code::Float64: |
37 | 66 | case Type::Code::DateTime64: |
38 | 67 | case Type::Code::Decimal64: |
39 | | - expected_size = 8; |
40 | | - break; |
| 68 | + return AssertSize({8}); |
41 | 69 |
|
42 | 70 | case Type::Code::String: |
43 | 71 | case Type::Code::FixedString: |
44 | | - case Type::Code::Decimal: |
45 | 72 | // value can be of any size |
46 | 73 | return; |
47 | 74 |
|
48 | 75 | case Type::Code::Array: |
49 | 76 | case Type::Code::Nullable: |
50 | 77 | case Type::Code::Tuple: |
51 | 78 | case Type::Code::LowCardinality: |
52 | | - throw UnimplementedError("Unsupported type in ItemView: " + std::to_string(static_cast<int>(type))); |
| 79 | + throw AssertionError("Unsupported type in ItemView: " + std::string(Type::TypeName(type))); |
53 | 80 |
|
54 | 81 | case Type::Code::IPv6: |
55 | 82 | case Type::Code::UUID: |
56 | 83 | case Type::Code::Int128: |
57 | 84 | case Type::Code::Decimal128: |
58 | | - expected_size = 16; |
59 | | - break; |
| 85 | + return AssertSize({16}); |
| 86 | + |
| 87 | + case Type::Code::Decimal: |
| 88 | + // Could be either Decimal32, Decimal64 or Decimal128 |
| 89 | + return AssertSize({4, 8, 16}); |
60 | 90 |
|
61 | 91 | default: |
62 | 92 | throw UnimplementedError("Unknon type code:" + std::to_string(static_cast<int>(type))); |
63 | 93 | } |
64 | | - |
65 | | - if (expected_size != static_cast<int>(data.size())) { |
66 | | - throw AssertionError("Value size mismatch for type " |
67 | | - + std::to_string(static_cast<int>(type)) + " expected: " |
68 | | - + std::to_string(expected_size) + ", got: " + std::to_string(data.size())); |
69 | | - } |
70 | 94 | } |
71 | 95 |
|
72 | 96 | } |
0 commit comments