11#include " ../columns/itemview.h"
22
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+
326namespace clickhouse {
427
528void 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+
740 switch (type) {
841 case Type::Code::Void:
9- expected_size = 0 ;
10- break ;
42+ return AssertSize ({0 });
1143
1244 case Type::Code::Int8:
1345 case Type::Code::UInt8:
1446 case Type::Code::Enum8:
15- expected_size = 1 ;
16- break ;
47+ return AssertSize ({1 });
1748
1849 case Type::Code::Int16:
1950 case Type::Code::UInt16:
2051 case Type::Code::Date:
2152 case Type::Code::Enum16:
22- expected_size = 2 ;
23- break ;
53+ return AssertSize ({2 });
2454
2555 case Type::Code::Int32:
2656 case Type::Code::UInt32:
2757 case Type::Code::Float32:
2858 case Type::Code::DateTime:
2959 case Type::Code::IPv4:
3060 case Type::Code::Decimal32:
31- expected_size = 4 ;
32- break ;
61+ return AssertSize ({4 });
3362
3463 case Type::Code::Int64:
3564 case Type::Code::UInt64:
3665 case Type::Code::Float64:
3766 case Type::Code::DateTime64:
38- case Type::Code::IPv6:
3967 case Type::Code::Decimal64:
40- expected_size = 8 ;
41- break ;
68+ return AssertSize ({8 });
4269
4370 case Type::Code::String:
4471 case Type::Code::FixedString:
@@ -49,24 +76,21 @@ void ItemView::ValidateData(Type::Code type, DataType data) {
4976 case Type::Code::Nullable:
5077 case Type::Code::Tuple:
5178 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)));
5380
81+ case Type::Code::IPv6:
5482 case Type::Code::UUID:
5583 case Type::Code::Int128:
56- case Type::Code::Decimal:
5784 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 });
6090
6191 default :
6292 throw UnimplementedError (" Unknon type code:" + std::to_string (static_cast <int >(type)));
6393 }
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- }
7094}
7195
7296}
0 commit comments