Skip to content

Commit 9c3c857

Browse files
Merge pull request #424 from pet74alex/Support_UInt128
Support UInt128 type
2 parents b7be415 + d4f8fa4 commit 9c3c857

15 files changed

+57
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ C++ client for [ClickHouse](https://clickhouse.com/).
1919
* LowCardinality(String) or LowCardinality(FixedString(N))
2020
* Tuple
2121
* UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64
22-
* Int128
22+
* UInt128, Int128
2323
* UUID
2424
* Map
2525
* Point, Ring, Polygon, MultiPolygon

clickhouse/base/uuid.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
namespace clickhouse {
77

8-
using UInt128 = std::pair<uint64_t, uint64_t>;
9-
10-
using UUID = UInt128;
8+
using UUID = std::pair<uint64_t, uint64_t>;
119

1210
}

clickhouse/columns/factory.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ static ColumnRef CreateTerminalColumn(const TypeAst& ast) {
6767
return std::make_shared<ColumnInt64>();
6868
case Type::Int128:
6969
return std::make_shared<ColumnInt128>();
70+
case Type::UInt128:
71+
return std::make_shared<ColumnUInt128>();
7072

7173
case Type::Float32:
7274
return std::make_shared<ColumnFloat32>();

clickhouse/columns/itemview.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ void ItemView::ValidateData(Type::Code type, DataType data) {
8282

8383
case Type::Code::IPv6:
8484
case Type::Code::UUID:
85+
case Type::Code::UInt128:
8586
case Type::Code::Int128:
8687
case Type::Code::Decimal128:
8788
return AssertSize({16});

clickhouse/columns/itemview.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct ItemView {
2828
inline auto ConvertToStorageValue(const T& t) {
2929
if constexpr (std::is_same_v<std::string_view, T> || std::is_same_v<std::string, T>) {
3030
return std::string_view{t};
31-
} else if constexpr (std::is_fundamental_v<T> || std::is_same_v<Int128, std::decay_t<T>>) {
31+
} else if constexpr (std::is_fundamental_v<T> || std::is_same_v<Int128, std::decay_t<T>> || std::is_same_v<UInt128, std::decay_t<T>>) {
3232
return std::string_view{reinterpret_cast<const char*>(&t), sizeof(T)};
3333
} else {
3434
static_assert(!std::is_same_v<T, T>, "Unknown type, which can't be stored in ItemView");

clickhouse/columns/numeric.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ template class ColumnVector<uint16_t>;
118118
template class ColumnVector<uint32_t>;
119119
template class ColumnVector<uint64_t>;
120120
template class ColumnVector<Int128>;
121+
template class ColumnVector<UInt128>;
121122

122123
template class ColumnVector<float>;
123124
template class ColumnVector<double>;

clickhouse/columns/numeric.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ class ColumnVector : public Column {
6767
};
6868

6969
using Int128 = absl::int128;
70+
using UInt128 = absl::uint128;
7071
using Int64 = int64_t;
7172

7273
using ColumnUInt8 = ColumnVector<uint8_t>;
7374
using ColumnUInt16 = ColumnVector<uint16_t>;
7475
using ColumnUInt32 = ColumnVector<uint32_t>;
7576
using ColumnUInt64 = ColumnVector<uint64_t>;
77+
using ColumnUInt128 = ColumnVector<UInt128>;
7678

7779
using ColumnInt8 = ColumnVector<int8_t>;
7880
using ColumnInt16 = ColumnVector<int16_t>;

clickhouse/types/type_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static const std::unordered_map<std::string, Type::Code> kTypeCode = {
5454
{ "IPv4", Type::IPv4 },
5555
{ "IPv6", Type::IPv6 },
5656
{ "Int128", Type::Int128 },
57-
// { "UInt128", Type::UInt128 },
57+
{ "UInt128", Type::UInt128 },
5858
{ "Decimal", Type::Decimal },
5959
{ "Decimal32", Type::Decimal32 },
6060
{ "Decimal64", Type::Decimal64 },

clickhouse/types/types.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const char* Type::TypeName(Type::Code code) {
3939
case Type::Code::IPv4: return "IPv4";
4040
case Type::Code::IPv6: return "IPv6";
4141
case Type::Code::Int128: return "Int128";
42+
case Type::Code::UInt128: return "UInt128";
4243
case Type::Code::Decimal: return "Decimal";
4344
case Type::Code::Decimal32: return "Decimal32";
4445
case Type::Code::Decimal64: return "Decimal64";
@@ -68,6 +69,7 @@ std::string Type::GetName() const {
6869
case UInt16:
6970
case UInt32:
7071
case UInt64:
72+
case UInt128:
7173
case UUID:
7274
case Float32:
7375
case Float64:
@@ -126,6 +128,7 @@ uint64_t Type::GetTypeUniqueId() const {
126128
case UInt16:
127129
case UInt32:
128130
case UInt64:
131+
case UInt128:
129132
case UUID:
130133
case Float32:
131134
case Float64:

clickhouse/types/types.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace clickhouse {
1313

1414
using Int128 = absl::int128;
15+
using UInt128 = absl::uint128;
1516
using Int64 = int64_t;
1617

1718
using TypeRef = std::shared_ptr<class Type>;
@@ -43,6 +44,7 @@ class Type {
4344
IPv4,
4445
IPv6,
4546
Int128,
47+
UInt128,
4648
Decimal,
4749
Decimal32,
4850
Decimal64,
@@ -339,6 +341,11 @@ inline TypeRef Type::CreateSimple<Int128>() {
339341
return TypeRef(new Type(Int128));
340342
}
341343

344+
template <>
345+
inline TypeRef Type::CreateSimple<UInt128>() {
346+
return TypeRef(new Type(UInt128));
347+
}
348+
342349
template <>
343350
inline TypeRef Type::CreateSimple<uint8_t>() {
344351
return TypeRef(new Type(UInt8));

ut/Column_ut.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ using TestCases = ::testing::Types<
207207
GenericColumnTestCase<ColumnIPv6, &makeColumn<ColumnIPv6>, in6_addr, &MakeIPv6s>,
208208

209209
GenericColumnTestCase<ColumnInt128, &makeColumn<ColumnInt128>, clickhouse::Int128, &MakeInt128s>,
210+
GenericColumnTestCase<ColumnUInt128, &makeColumn<ColumnUInt128>, clickhouse::UInt128, &MakeUInt128s>,
210211
GenericColumnTestCase<ColumnUUID, &makeColumn<ColumnUUID>, clickhouse::UUID, &MakeUUIDs>,
211212

212213
DecimalColumnTestCase<ColumnDecimal, 18, 0>,
@@ -286,7 +287,7 @@ inline auto convertValueForGetItem(const ColumnType& col, ValueType&& t) {
286287
// Since ColumnDecimal can hold 32, 64, 128-bit wide data and there is no way telling at run-time.
287288
const ItemView item = col.GetItem(0);
288289
return std::string_view(reinterpret_cast<const char*>(&t), item.data.size());
289-
} else if constexpr (std::is_same_v<T, clickhouse::UInt128>
290+
} else if constexpr (std::is_same_v<T, clickhouse::UInt128> || std::is_same_v<T, clickhouse::UUID>
290291
|| std::is_same_v<T, clickhouse::Int128>) {
291292
return std::string_view{reinterpret_cast<const char*>(&t), sizeof(T)};
292293
} else if constexpr (std::is_same_v<T, in_addr>) {

ut/CreateColumnByType_ut.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ INSTANTIATE_TEST_SUITE_P(Basic, CreateColumnByTypeWithName, ::testing::Values(
7676
"Int8", "Int16", "Int32", "Int64",
7777
"UInt8", "UInt16", "UInt32", "UInt64",
7878
"String", "Date", "DateTime",
79-
"UUID", "Int128"
79+
"UUID", "Int128", "UInt128"
8080
));
8181

8282
INSTANTIATE_TEST_SUITE_P(Parametrized, CreateColumnByTypeWithName, ::testing::Values(

ut/columns_ut.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,28 @@ TEST(ColumnsCase, Int128) {
483483
EXPECT_EQ(0, col->At(4));
484484
}
485485

486+
TEST(ColumnsCase, UInt128) {
487+
auto col = std::make_shared<ColumnUInt128>(std::vector<UInt128>{
488+
absl::MakeUint128(0xffffffffffffffffll, 0xffffffffffffffffll), // 2^128 - 1
489+
absl::MakeUint128(0, 0xffffffffffffffffll), // 2^64 - 1
490+
absl::MakeUint128(0xffffffffffffffffll, 0), // 2^128 - 2^64
491+
absl::MakeUint128(0x8000000000000000ll, 0),
492+
UInt128(0)
493+
});
494+
495+
EXPECT_EQ(absl::MakeUint128(0xffffffffffffffffll, 0xffffffffffffffffll), col->At(0));
496+
497+
EXPECT_EQ(absl::MakeUint128(0, 0xffffffffffffffffll), col->At(1));
498+
EXPECT_EQ(0ull, absl::Uint128High64(col->At(1)));
499+
EXPECT_EQ(0xffffffffffffffffull, absl::Uint128Low64(col->At(1)));
500+
501+
EXPECT_EQ(absl::MakeUint128(0xffffffffffffffffll, 0), col->At(2));
502+
EXPECT_EQ(static_cast<uint64_t>(0xffffffffffffffffull), absl::Uint128High64(col->At(2)));
503+
EXPECT_EQ(0ull, absl::Uint128Low64(col->At(2)));
504+
505+
EXPECT_EQ(0, col->At(4));
506+
}
507+
486508
TEST(ColumnsCase, ColumnIPv4)
487509
{
488510
// TODO: split into proper method-level unit-tests

ut/value_generators.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,23 @@ std::vector<clickhouse::Int64> MakeDateTimes() {
104104
std::vector<clickhouse::Int128> MakeInt128s() {
105105
return {
106106
absl::MakeInt128(0xffffffffffffffffll, 0xffffffffffffffffll), // -1
107-
absl::MakeInt128(0, 0xffffffffffffffffll), // 2^64
107+
absl::MakeInt128(0, 0xffffffffffffffffll), // 2^64 - 1
108108
absl::MakeInt128(0xffffffffffffffffll, 0),
109109
absl::MakeInt128(0x8000000000000000ll, 0),
110110
Int128(0)
111111
};
112112
}
113113

114+
std::vector<clickhouse::UInt128> MakeUInt128s() {
115+
return {
116+
absl::MakeUint128(0xffffffffffffffffll, 0xffffffffffffffffll), // 2^128 - 1
117+
absl::MakeUint128(0, 0xffffffffffffffffll), // 2^64 - 1
118+
absl::MakeUint128(0xffffffffffffffffll, 0), // 2^128 - 2^64
119+
absl::MakeUint128(0x8000000000000000ll, 0),
120+
UInt128(0)
121+
};
122+
}
123+
114124
std::vector<clickhouse::Int128> MakeDecimals(size_t /*precision*/, size_t scale) {
115125
const auto scale_multiplier = static_cast<size_t>(std::pow(10, scale));
116126
const long long int rhs_value = 12345678910;

ut/value_generators.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ std::vector<in_addr> MakeIPv4s();
3939
std::vector<in6_addr> MakeIPv6s();
4040
std::vector<clickhouse::UUID> MakeUUIDs();
4141
std::vector<clickhouse::Int128> MakeInt128s();
42+
std::vector<clickhouse::UInt128> MakeUInt128s();
4243
std::vector<clickhouse::Int128> MakeDecimals(size_t precision, size_t scale);
4344

4445
template <typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>

0 commit comments

Comments
 (0)