Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

Commit 23551e3

Browse files
authored
Add index hashing for uint128 (#6052)
1 parent fadb62e commit 23551e3

File tree

10 files changed

+160
-9
lines changed

10 files changed

+160
-9
lines changed

src/binder/bind/bind_ddl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ static void validatePrimaryKey(const std::string& pkColName,
112112
case PhysicalTypeID::INT32:
113113
case PhysicalTypeID::INT64:
114114
case PhysicalTypeID::INT128:
115+
case PhysicalTypeID::UINT128:
115116
case PhysicalTypeID::STRING:
116117
case PhysicalTypeID::FLOAT:
117118
case PhysicalTypeID::DOUBLE:

src/include/common/types/types.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,14 @@ concept ComparableTypes = NumericTypes<T> || std::is_same_v<T, ku_string_t> ||
159159
std::is_same_v<T, interval_t> || std::is_same_v<T, bool>;
160160

161161
template<typename T>
162-
concept HashablePrimitive = ((std::integral<T> && !std::is_same_v<T, bool>) ||
163-
std::floating_point<T> || std::is_same_v<T, int128_t>);
162+
concept HashablePrimitive =
163+
((std::integral<T> && !std::is_same_v<T, bool>) || std::floating_point<T> ||
164+
std::is_same_v<T, int128_t> || std::is_same_v<T, uint128_t>);
164165
template<typename T>
165166
concept IndexHashable = ((std::integral<T> && !std::is_same_v<T, bool>) || std::floating_point<T> ||
166-
std::is_same_v<T, int128_t> || std::is_same_v<T, ku_string_t> ||
167-
std::is_same_v<T, std::string_view> || std::same_as<T, std::string>);
167+
std::is_same_v<T, int128_t> || std::is_same_v<T, uint128_t> ||
168+
std::is_same_v<T, ku_string_t> || std::is_same_v<T, std::string_view> ||
169+
std::same_as<T, std::string>);
168170

169171
template<typename T>
170172
concept HashableNonNestedTypes =

src/include/processor/operator/persistent/index_builder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "common/static_vector.h"
99
#include "common/types/int128_t.h"
1010
#include "common/types/types.h"
11+
#include "common/types/uint128_t.h"
1112
#include "processor/operator/persistent/node_batch_insert_error_handler.h"
1213
#include "storage/index/hash_index.h"
1314
#include "storage/index/hash_index_utils.h"
@@ -76,7 +77,7 @@ class IndexBuilderGlobalQueues {
7677
// Queues for distributing primary keys.
7778
std::variant<Queue<std::string>, Queue<int64_t>, Queue<int32_t>, Queue<int16_t>, Queue<int8_t>,
7879
Queue<uint64_t>, Queue<uint32_t>, Queue<uint16_t>, Queue<uint8_t>, Queue<common::int128_t>,
79-
Queue<float>, Queue<double>>
80+
Queue<common::uint128_t>, Queue<float>, Queue<double>>
8081
queues;
8182
transaction::Transaction* transaction;
8283
};
@@ -128,7 +129,8 @@ class IndexBuilderLocalBuffers {
128129
std::variant<UniqueBuffers<std::string>, UniqueBuffers<int64_t>, UniqueBuffers<int32_t>,
129130
UniqueBuffers<int16_t>, UniqueBuffers<int8_t>, UniqueBuffers<uint64_t>,
130131
UniqueBuffers<uint32_t>, UniqueBuffers<uint16_t>, UniqueBuffers<uint8_t>,
131-
UniqueBuffers<common::int128_t>, UniqueBuffers<float>, UniqueBuffers<double>>
132+
UniqueBuffers<common::int128_t>, UniqueBuffers<common::uint128_t>, UniqueBuffers<float>,
133+
UniqueBuffers<double>>
132134
buffers;
133135
};
134136

src/processor/result/base_hash_table.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ static compare_function_t getCompareEntryFunc(const LogicalType& type) {
227227
default: {
228228
TypeUtils::visit(
229229
type.getPhysicalType(), [&]<HashableTypes T>(T) { func = compareEntry<T>; },
230-
[&](uint128_t) {
231-
func = compareEntry<uint128_t>;
232-
}, // TODO: remove when uint128_t hashing is implemented
233230
[](auto) { KU_UNREACHABLE; });
234231
}
235232
}

src/storage/index/hash_index.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "common/types/int128_t.h"
99
#include "common/types/ku_string.h"
1010
#include "common/types/types.h"
11+
#include "common/types/uint128_t.h"
1112
#include "main/client_context.h"
1213
#include "storage/disk_array.h"
1314
#include "storage/disk_array_collection.h"
@@ -452,6 +453,7 @@ template class HashIndex<uint8_t>;
452453
template class HashIndex<double>;
453454
template class HashIndex<float>;
454455
template class HashIndex<int128_t>;
456+
template class HashIndex<uint128_t>;
455457
template class HashIndex<ku_string_t>;
456458

457459
std::unique_ptr<IndexStorageInfo> PrimaryKeyIndexStorageInfo::deserialize(

src/storage/index/in_mem_hash_index.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ template class InMemHashIndex<uint8_t>;
282282
template class InMemHashIndex<double>;
283283
template class InMemHashIndex<float>;
284284
template class InMemHashIndex<int128_t>;
285+
template class InMemHashIndex<uint128_t>;
285286
template class InMemHashIndex<ku_string_t>;
286287

287288
} // namespace storage

test/test_files/copy/copy_pk_basic.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ Bar|10020
117117
---- ok
118118
-INSERT_STATEMENT_BLOCK CREATE_REL_AND_COPY_NODES_AND_VALIDATE
119119

120+
-CASE CopyUInt128PK
121+
-STATEMENT create node table person (ID UINT128, fName STRING, age INT64, PRIMARY KEY (ID));
122+
---- ok
123+
-STATEMENT create node table org (ID UINT128, PRIMARY KEY (ID));
124+
---- ok
125+
-INSERT_STATEMENT_BLOCK CREATE_REL_AND_COPY_NODES_AND_VALIDATE
126+
120127
-CASE CopyDoublePK
121128
-STATEMENT create node table person (ID double, fName STRING, age INT64, PRIMARY KEY (ID));
122129
---- ok

test/test_files/dml_node/create/create_empty.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ foobar-ewe-j323-8nd*-ewew
235235
---- 1
236236
170141183460469231731687303715884105727
237237

238+
-CASE CreateUInt128PK
239+
-STATEMENT CREATE NODE TABLE test(id UINT128, PRIMARY KEY(id));
240+
---- ok
241+
-STATEMENT CREATE (t:test {id:1});
242+
---- ok
243+
-STATEMENT CREATE (t:test {id:340282366920938463463374607431768211455});
244+
---- ok
245+
-STATEMENT MATCH (t:test) WHERE t.id = 340282366920938463463374607431768211455 RETURN t.id;
246+
---- 1
247+
340282366920938463463374607431768211455
248+
238249
-CASE CreateTimestampPK
239250
-STATEMENT CREATE NODE TABLE test(id TIMESTAMP, PRIMARY KEY(id));
240251
---- ok

test/test_files/transaction/create_node/create_empty_checkpoint.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,21 @@ foobar-ewe-j323-8nd*-ewew
296296
---- 1
297297
170141183460469231731687303715884105727
298298

299+
-CASE CreateUInt128PK
300+
-STATEMENT CALL auto_checkpoint=true;
301+
---- ok
302+
-STATEMENT CALL checkpoint_threshold=0;
303+
---- ok
304+
-STATEMENT CREATE NODE TABLE test(id UINT128, PRIMARY KEY(id));
305+
---- ok
306+
-STATEMENT CREATE (t:test {id:1});
307+
---- ok
308+
-STATEMENT CREATE (t:test {id:340282366920938463463374607431768211455});
309+
---- ok
310+
-STATEMENT MATCH (t:test) WHERE t.id = 340282366920938463463374607431768211455 RETURN t.id;
311+
---- 1
312+
340282366920938463463374607431768211455
313+
299314
-CASE CreateTimestampPK
300315
-STATEMENT CALL auto_checkpoint=true;
301316
---- ok

test/test_files/uint128/uint128.test

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,119 @@
3030
2|340282366920938463463374607431768211455
3131
3|340282366920938463463374607431768211455
3232

33+
-CASE UInt128PK
34+
-STATEMENT CREATE NODE TABLE tab(uint128key UINT128 PRIMARY KEY, value UINT128);
35+
---- ok
36+
-STATEMENT CREATE (n1:tab {uint128key: 0, value: 170141183460469231731687303715884105728}) RETURN n1.*;
37+
---- 1
38+
0|170141183460469231731687303715884105728
39+
-STATEMENT CREATE (n2:tab {uint128key: 170141183460469231731687303715884105737, value: 1869}) RETURN n2.*;
40+
---- 1
41+
170141183460469231731687303715884105737|1869
42+
-STATEMENT CREATE (n3:tab {uint128key: -1, value: 12}) RETURN n3.*;
43+
---- error
44+
Overflow exception: Cannot cast negative value to UINT128.
45+
-STATEMENT CREATE (n4:tab {uint128key: 340282366920938463463374607431768211456, value: 0}) RETURN n4.*;
46+
---- error
47+
Conversion exception: Cast failed. Could not convert "340282366920938463463374607431768211456" to UINT128.
48+
-STATEMENT CREATE (n5:tab {uint128key: 0, value: 1}) RETURN n5.*;
49+
---- error
50+
Runtime exception: Found duplicated primary key value 0, which violates the uniqueness constraint of the primary key column.
51+
-STATEMENT CREATE (n6:tab {uint128key: 170141183460469231731687303715884105737, value: 1869}) RETURN n6.*;
52+
---- error
53+
Runtime exception: Found duplicated primary key value 170141183460469231731687303715884105737, which violates the uniqueness constraint of the primary key column.
54+
-STATEMENT CREATE (n7:tab {uint128key: 2025, value: NULL}) RETURN n7.*;
55+
---- 1
56+
2025|
57+
-STATEMENT CREATE (n8:tab {uint128key: NULL, value: 1}) RETURN n8.*;
58+
---- error
59+
Runtime exception: Found NULL, which violates the non-null constraint of the primary key column.
60+
61+
-CASE UInt128PKMatch
62+
-STATEMENT CREATE NODE TABLE tab(uint128key UINT128 PRIMARY KEY, value UINT128);
63+
---- ok
64+
-STATEMENT CREATE (n1:tab {uint128key: 0, value: 0});
65+
---- ok
66+
-STATEMENT CREATE (n2:tab {uint128key: 11111, value: 1});
67+
---- ok
68+
-STATEMENT CREATE (n3:tab {uint128key: 22222, value: 2});
69+
---- ok
70+
-STATEMENT MATCH (z:tab {uint128key: 11111}) RETURN z.*;
71+
---- 1
72+
11111|1
73+
-STATEMENT MATCH (z:tab) WHERE z.uint128key = 22222 RETURN z.value;
74+
---- 1
75+
2
76+
-STATEMENT CREATE (n4:tab {uint128key: 15000, value: 3});
77+
---- ok
78+
-STATEMENT MATCH (z:tab) WHERE z.uint128key > 10000 AND z.uint128key < 20000 RETURN z.*;
79+
---- 2
80+
11111|1
81+
15000|3
82+
-STATEMENT MATCH (z:tab) WHERE z.uint128key = 99999 RETURN z.*;
83+
---- 0
84+
85+
-CASE UInt128PKMatchAndDelete
86+
-STATEMENT CREATE NODE TABLE tab(uint128key UINT128 PRIMARY KEY, value UINT128);
87+
---- ok
88+
-STATEMENT CREATE (n1:tab {uint128key: 0, value: 0});
89+
---- ok
90+
-STATEMENT CREATE (n2:tab {uint128key: 11111, value: 1});
91+
---- ok
92+
-STATEMENT CREATE (n3:tab {uint128key: 22222, value: 2});
93+
---- ok
94+
-STATEMENT CREATE (n4:tab {uint128key: 15000, value: 3});
95+
---- ok
96+
-STATEMENT MATCH (z:tab) RETURN z.*;
97+
---- 4
98+
0|0
99+
11111|1
100+
22222|2
101+
15000|3
102+
-STATEMENT MATCH (z:tab {uint128key: 15000}) DELETE z;
103+
---- ok
104+
-STATEMENT MATCH (z:tab) RETURN z.*;
105+
---- 3
106+
0|0
107+
11111|1
108+
22222|2
109+
-STATEMENT CREATE (n4:tab {uint128key: 15000, value: 9876543210});
110+
---- ok
111+
-STATEMENT MATCH (z:tab) RETURN z.*;
112+
---- 4
113+
0|0
114+
11111|1
115+
22222|2
116+
15000|9876543210
117+
-STATEMENT MATCH (z:tab {uint128key: 12}) DELETE z;
118+
---- ok
119+
-STATEMENT MATCH (z:tab) RETURN z.*;
120+
---- 4
121+
0|0
122+
11111|1
123+
22222|2
124+
15000|9876543210
125+
-STATEMENT MATCH (z:tab) WHERE z.uint128key >= 0 DELETE z;
126+
---- 0
127+
-STATEMENT MATCH (z:tab) RETURN z.*;
128+
---- 0
129+
130+
-CASE Set
131+
-STATEMENT CREATE NODE TABLE tab(uint128key UINT128 PRIMARY KEY, value1 UINT128, value2 UINT128);
132+
---- ok
133+
-STATEMENT CREATE (n1:tab {uint128key: 1024, value1: 2048, value2: 4096}) RETURN n1.*;
134+
---- 1
135+
1024|2048|4096
136+
-STATEMENT MATCH (z:tab {uint128key: 1024}) SET z.value1 = 9999999 RETURN z.*;
137+
---- 1
138+
1024|9999999|4096
139+
-STATEMENT MATCH (z:tab {uint128key: 1024}) SET z.value2 = z.value1 + to_uint128(1) RETURN z.*;
140+
---- 1
141+
1024|9999999|10000000
142+
-STATEMENT MATCH (z:tab {uint128key: 1024}) SET z.uint128key = 1234567;
143+
---- error
144+
Binder exception: Cannot set property uint128key in table tab because it is used as primary key. Try delete and then insert.
145+
33146
-CASE Overflow
34147
-STATEMENT CREATE NODE TABLE tab(intkey INT64 PRIMARY KEY, value UINT128);
35148
---- ok

0 commit comments

Comments
 (0)