Skip to content

Commit 615721a

Browse files
committed
ItemView fixes for setting Int128 value
Tests for ItemView contructor
1 parent 4b0e45c commit 615721a

File tree

5 files changed

+215
-4
lines changed

5 files changed

+215
-4
lines changed

clickhouse/columns/decimal.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "decimal.h"
22

3-
#include <iostream>
4-
53
namespace
64
{
75
using namespace clickhouse;

clickhouse/columns/itemview.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ void ItemView::ValidateData(Type::Code type, DataType data) {
2020
ANY, /*String*/
2121
ANY, /*FixedString*/
2222
4, /*DateTime*/
23-
8, /*DateTime64*/
2423
2, /*Date*/
2524
ERR, /*Array*/
2625
ERR, /*Nullable*/
@@ -36,6 +35,7 @@ void ItemView::ValidateData(Type::Code type, DataType data) {
3635
8, /*Decimal64*/
3736
16, /*Decimal128*/
3837
ERR, /*LowCardinality*/
38+
8, /*DateTime64*/
3939
};
4040

4141
if (type >= sizeof(value_size_by_type)/sizeof(value_size_by_type[0]) || type < 0) {

clickhouse/columns/itemview.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct ItemView {
5858
T get() const {
5959
if constexpr (std::is_same_v<std::string_view, T> || std::is_same_v<std::string, T>) {
6060
return data;
61-
} else if constexpr (std::is_fundamental_v<T>) {
61+
} else if constexpr (std::is_fundamental_v<T> || std::is_same_v<Int128, T>) {
6262
if (sizeof(T) == data.size()) {
6363
return *reinterpret_cast<const T*>(data.data());
6464
} else {

ut/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ ADD_EXECUTABLE (clickhouse-cpp-ut
33

44
client_ut.cpp
55
columns_ut.cpp
6+
itemview_ut.cpp
67
socket_ut.cpp
78
stream_ut.cpp
89
type_parser_ut.cpp

ut/itemview_ut.cpp

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#include <clickhouse/columns/itemview.h>
2+
#include <clickhouse/columns/numeric.h>
3+
4+
#include <contrib/gtest/gtest.h>
5+
#include "utils.h"
6+
7+
#include <string_view>
8+
#include <limits>
9+
10+
namespace
11+
{
12+
using namespace clickhouse;
13+
}
14+
15+
TEST(ItemView, StorableTypes) {
16+
/// Validate that is it possible to store proper value of a proper type into a ItemView.
17+
18+
#define TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, NativeValue) \
19+
EXPECT_EQ(static_cast<NativeType>(NativeValue), ItemView(TypeCode, static_cast<NativeType>(NativeValue)).get<NativeType>()) \
20+
<< " TypeCode:" << #TypeCode << " NativeType: " << #NativeType;
21+
22+
#define TEST_ITEMVIEW_TYPE_VALUES(TypeCode, NativeType) \
23+
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, std::numeric_limits<NativeType>::min()); \
24+
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, std::numeric_limits<NativeType>::min() + 1); \
25+
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, -1); \
26+
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, 0); \
27+
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, 1); \
28+
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, std::numeric_limits<NativeType>::max() - 1); \
29+
TEST_ITEMVIEW_TYPE_VALUE(TypeCode, NativeType, std::numeric_limits<NativeType>::max());
30+
31+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Int8, int8_t);
32+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Int16, int16_t);
33+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Int32, int32_t);
34+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Int64, int64_t);
35+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Int128, Int128);
36+
37+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::UInt8, uint8_t);
38+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::UInt16, uint16_t);
39+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::UInt32, uint32_t);
40+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::UInt64, uint64_t);
41+
42+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Float32, float);
43+
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::Float32, float, 0.5);
44+
45+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Float64, double);
46+
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::Float64, double, 0.5);
47+
48+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Date, uint16_t);
49+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::DateTime, uint32_t);
50+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::DateTime64, int64_t);
51+
52+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Decimal, Int128);
53+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Decimal32, int32_t);
54+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Decimal64, int64_t);
55+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Decimal128, Int128);
56+
57+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Enum8, uint8_t);
58+
TEST_ITEMVIEW_TYPE_VALUES(Type::Code::Enum16, uint16_t);
59+
60+
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::String, std::string_view, "");
61+
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::String, std::string_view, "here is a string");
62+
63+
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::FixedString, std::string_view, "");
64+
TEST_ITEMVIEW_TYPE_VALUE(Type::Code::FixedString, std::string_view, "here is a string");
65+
}
66+
67+
#define TEST_ITEMVIEW_ERROR(TypeCode, NativeType) \
68+
EXPECT_ANY_THROW(ItemView(TypeCode, static_cast<NativeType>(0))) \
69+
<< " TypeCode:" << #TypeCode << " NativeType: " << #NativeType;
70+
71+
TEST(ItemView, ErrorTypes) {
72+
// Types that is impossible to store certain Type::Code into an ItemView.
73+
TEST_ITEMVIEW_ERROR(Type::Code::Array, int);
74+
TEST_ITEMVIEW_ERROR(Type::Code::Nullable, int);
75+
TEST_ITEMVIEW_ERROR(Type::Code::Tuple, int);
76+
TEST_ITEMVIEW_ERROR(Type::Code::LowCardinality, int);
77+
}
78+
79+
TEST(ItemView, TypeSizeMismatch) {
80+
// Validate that it is impossible to initialize ItemView with mismatching Type::Code and native value.
81+
82+
TEST_ITEMVIEW_ERROR(Type::Code::Int8, int16_t);
83+
TEST_ITEMVIEW_ERROR(Type::Code::Int8, int32_t);
84+
TEST_ITEMVIEW_ERROR(Type::Code::Int8, int64_t);
85+
TEST_ITEMVIEW_ERROR(Type::Code::Int8, Int128);
86+
87+
TEST_ITEMVIEW_ERROR(Type::Code::Int16, int8_t);
88+
TEST_ITEMVIEW_ERROR(Type::Code::Int16, int32_t);
89+
TEST_ITEMVIEW_ERROR(Type::Code::Int16, int64_t);
90+
TEST_ITEMVIEW_ERROR(Type::Code::Int16, Int128);
91+
92+
TEST_ITEMVIEW_ERROR(Type::Code::Int32, int8_t);
93+
TEST_ITEMVIEW_ERROR(Type::Code::Int32, int16_t);
94+
TEST_ITEMVIEW_ERROR(Type::Code::Int32, int64_t);
95+
TEST_ITEMVIEW_ERROR(Type::Code::Int32, Int128);
96+
97+
TEST_ITEMVIEW_ERROR(Type::Code::Int64, int8_t);
98+
TEST_ITEMVIEW_ERROR(Type::Code::Int64, int16_t);
99+
TEST_ITEMVIEW_ERROR(Type::Code::Int64, int32_t);
100+
TEST_ITEMVIEW_ERROR(Type::Code::Int64, Int128);
101+
102+
TEST_ITEMVIEW_ERROR(Type::Code::Int128, int8_t);
103+
TEST_ITEMVIEW_ERROR(Type::Code::Int128, int16_t);
104+
TEST_ITEMVIEW_ERROR(Type::Code::Int128, int32_t);
105+
TEST_ITEMVIEW_ERROR(Type::Code::Int128, int64_t);
106+
107+
TEST_ITEMVIEW_ERROR(Type::Code::UInt8, int16_t);
108+
TEST_ITEMVIEW_ERROR(Type::Code::UInt8, int32_t);
109+
TEST_ITEMVIEW_ERROR(Type::Code::UInt8, int64_t);
110+
TEST_ITEMVIEW_ERROR(Type::Code::UInt8, Int128);
111+
112+
TEST_ITEMVIEW_ERROR(Type::Code::UInt16, int8_t);
113+
TEST_ITEMVIEW_ERROR(Type::Code::UInt16, int32_t);
114+
TEST_ITEMVIEW_ERROR(Type::Code::UInt16, int64_t);
115+
TEST_ITEMVIEW_ERROR(Type::Code::UInt16, Int128);
116+
117+
TEST_ITEMVIEW_ERROR(Type::Code::UInt32, int8_t);
118+
TEST_ITEMVIEW_ERROR(Type::Code::UInt32, int16_t);
119+
TEST_ITEMVIEW_ERROR(Type::Code::UInt32, int64_t);
120+
TEST_ITEMVIEW_ERROR(Type::Code::UInt32, Int128);
121+
122+
TEST_ITEMVIEW_ERROR(Type::Code::UInt64, int8_t);
123+
TEST_ITEMVIEW_ERROR(Type::Code::UInt64, int16_t);
124+
TEST_ITEMVIEW_ERROR(Type::Code::UInt64, int32_t);
125+
TEST_ITEMVIEW_ERROR(Type::Code::UInt64, Int128);
126+
127+
TEST_ITEMVIEW_ERROR(Type::Code::Float32, int8_t);
128+
TEST_ITEMVIEW_ERROR(Type::Code::Float32, int16_t);
129+
TEST_ITEMVIEW_ERROR(Type::Code::Float32, int64_t);
130+
TEST_ITEMVIEW_ERROR(Type::Code::Float32, Int128);
131+
TEST_ITEMVIEW_ERROR(Type::Code::Float32, double);
132+
133+
TEST_ITEMVIEW_ERROR(Type::Code::Float64, int8_t);
134+
TEST_ITEMVIEW_ERROR(Type::Code::Float64, int16_t);
135+
TEST_ITEMVIEW_ERROR(Type::Code::Float64, int32_t);
136+
TEST_ITEMVIEW_ERROR(Type::Code::Float64, Int128);
137+
TEST_ITEMVIEW_ERROR(Type::Code::Float64, float);
138+
139+
TEST_ITEMVIEW_ERROR(Type::Code::Date, int8_t);
140+
TEST_ITEMVIEW_ERROR(Type::Code::Date, int32_t);
141+
TEST_ITEMVIEW_ERROR(Type::Code::Date, int64_t);
142+
TEST_ITEMVIEW_ERROR(Type::Code::Date, Int128);
143+
144+
TEST_ITEMVIEW_ERROR(Type::Code::DateTime, int8_t);
145+
TEST_ITEMVIEW_ERROR(Type::Code::DateTime, int16_t);
146+
TEST_ITEMVIEW_ERROR(Type::Code::DateTime, int64_t);
147+
TEST_ITEMVIEW_ERROR(Type::Code::DateTime, Int128);
148+
149+
TEST_ITEMVIEW_ERROR(Type::Code::DateTime64, int8_t);
150+
TEST_ITEMVIEW_ERROR(Type::Code::DateTime64, int16_t);
151+
TEST_ITEMVIEW_ERROR(Type::Code::DateTime64, int32_t);
152+
TEST_ITEMVIEW_ERROR(Type::Code::DateTime64, Int128);
153+
154+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal, int8_t);
155+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal, int16_t);
156+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal, int32_t);
157+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal, int64_t);
158+
159+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal32, int8_t);
160+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal32, int16_t);
161+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal32, int64_t);
162+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal32, Int128);
163+
164+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal64, int8_t);
165+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal64, int16_t);
166+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal64, int32_t);
167+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal64, Int128);
168+
169+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal128, int8_t);
170+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal128, int16_t);
171+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal128, int32_t);
172+
TEST_ITEMVIEW_ERROR(Type::Code::Decimal128, int64_t);
173+
174+
TEST_ITEMVIEW_ERROR(Type::Code::Enum8, int16_t);
175+
TEST_ITEMVIEW_ERROR(Type::Code::Enum8, int32_t);
176+
TEST_ITEMVIEW_ERROR(Type::Code::Enum8, int64_t);
177+
TEST_ITEMVIEW_ERROR(Type::Code::Enum8, Int128);
178+
179+
TEST_ITEMVIEW_ERROR(Type::Code::Enum16, int8_t);
180+
TEST_ITEMVIEW_ERROR(Type::Code::Enum16, int32_t);
181+
TEST_ITEMVIEW_ERROR(Type::Code::Enum16, int64_t);
182+
TEST_ITEMVIEW_ERROR(Type::Code::Enum16, Int128);
183+
}
184+
185+
TEST(ItemView, Int128_values) {
186+
const auto vals = {
187+
std::numeric_limits<Int128>::min() + 2,
188+
std::numeric_limits<Int128>::min() + 1,
189+
std::numeric_limits<Int128>::min(),
190+
absl::MakeInt128(0xffffffffffffffffll - 2, 0),
191+
absl::MakeInt128(0xffffffffffffffffll - 1, 0),
192+
absl::MakeInt128(0xffffffffffffffffll, 0),
193+
absl::MakeInt128(0xffffffffffffffffll, 0xffffffffffffffffll),
194+
absl::MakeInt128(0, 0xffffffffffffffffll - 2),
195+
absl::MakeInt128(0, 0xffffffffffffffffll - 1),
196+
absl::MakeInt128(0, 0xffffffffffffffffll),
197+
Int128(-1),
198+
Int128(0),
199+
Int128(1),
200+
std::numeric_limits<Int128>::max() - 2,
201+
std::numeric_limits<Int128>::max() - 1,
202+
std::numeric_limits<Int128>::max(),
203+
};
204+
205+
for (size_t i = 0; i < vals.size(); ++i)
206+
{
207+
const auto value = vals.begin()[i];
208+
const ItemView item_view(Type::Code::Decimal128, value);
209+
210+
EXPECT_EQ(value, item_view.get<Int128>()) << "# index: " << i << " Int128 value: " << value;
211+
}
212+
}

0 commit comments

Comments
 (0)