Skip to content

Commit

Permalink
Merge pull request #10771 from Mytherin/uuidfromhugeint
Browse files Browse the repository at this point in the history
Add a method UUID::FromUHugeint to generate a UUID from a uhugeint_t
  • Loading branch information
Mytherin committed Feb 22, 2024
2 parents 431627e + a78e384 commit 894710a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/common/types/uuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ void UUID::ToString(hugeint_t input, char *buf) {
byte_to_hex(input.lower & 0xFF, buf, pos);
}

hugeint_t UUID::FromUHugeint(uhugeint_t input) {
hugeint_t result;
result.lower = input.lower;
if (input.upper > uint64_t(NumericLimits<int64_t>::Maximum())) {
result.upper = int64_t(input.upper - uint64_t(NumericLimits<int64_t>::Maximum()) - 1);
} else {
result.upper = int64_t(input.upper) - NumericLimits<int64_t>::Maximum() - 1;
}
return result;
}

hugeint_t UUID::GenerateRandomUUID(RandomEngine &engine) {
uint8_t bytes[16];
for (int i = 0; i < 16; i += 4) {
Expand Down
3 changes: 3 additions & 0 deletions src/include/duckdb/common/types/uuid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class UUID {
//! Convert a hugeint object to a uuid style string
static void ToString(hugeint_t input, char *buf);

//! Convert a uhugeint_t object to a uuid value
static hugeint_t FromUHugeint(uhugeint_t input);

//! Convert a hugeint object to a uuid style string
static hugeint_t GenerateRandomUUID(RandomEngine &engine);
static hugeint_t GenerateRandomUUID();
Expand Down
4 changes: 4 additions & 0 deletions src/include/duckdb/common/windows_undefs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@
#undef RemoveDirectory
#endif

#ifdef UUID
#undef UUID
#endif

#endif
3 changes: 2 additions & 1 deletion test/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(TEST_API_OBJECTS
test_query_profiler.cpp
test_dbdir.cpp
test_progress_bar.cpp
test_uuid.cpp
test_threads.cpp
test_windows_header_compatibility.cpp
test_windows_unicode_path.cpp
Expand All @@ -37,7 +38,7 @@ if(DUCKDB_EXTENSION_TPCH_SHOULD_LINK)
serialized_plans/test_plan_serialization_bwc.cpp)
endif()

add_library_unity(test_api OBJECT ${TEST_API_OBJECTS})
add_library(test_api OBJECT ${TEST_API_OBJECTS})
set(ALL_OBJECT_FILES
${ALL_OBJECT_FILES} $<TARGET_OBJECTS:test_api>
PARENT_SCOPE)
19 changes: 19 additions & 0 deletions test/api/test_uuid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "test_helpers.hpp"
#include "duckdb/common/types/uuid.hpp"
#include "catch.hpp"

using namespace duckdb;
using namespace std;

TEST_CASE("Test UUID API", "[api]") {
REQUIRE(UUID::ToString(UUID::FromUHugeint(uhugeint_t(0))) == "00000000-0000-0000-0000-000000000000");
REQUIRE(UUID::ToString(UUID::FromUHugeint(uhugeint_t(1))) == "00000000-0000-0000-0000-000000000001");
REQUIRE(UUID::ToString(UUID::FromUHugeint(NumericLimits<uhugeint_t>::Maximum())) ==
"ffffffff-ffff-ffff-ffff-ffffffffffff");
REQUIRE(UUID::ToString(UUID::FromUHugeint(NumericLimits<uhugeint_t>::Maximum() - 1)) ==
"ffffffff-ffff-ffff-ffff-fffffffffffe");
REQUIRE(UUID::ToString(UUID::FromUHugeint(NumericLimits<uhugeint_t>::Maximum() / 2)) ==
"7fffffff-ffff-ffff-ffff-ffffffffffff");
REQUIRE(UUID::ToString(UUID::FromUHugeint((NumericLimits<uhugeint_t>::Maximum() / 2) + 1)) ==
"80000000-0000-0000-0000-000000000000");
}

0 comments on commit 894710a

Please sign in to comment.