Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method UUID::FromUHugeint to generate a UUID from a uhugeint_t #10771

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add a method UUID::FromUHugeint to generate a UUID from a uhugeint_t
  • Loading branch information
Mytherin committed Feb 20, 2024
commit 158db1bfc8042c94da9b740485c2281a20d2f4b8
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
1 change: 1 addition & 0 deletions 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 Down
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 "catch.hpp"
#include "test_helpers.hpp"
#include "duckdb/common/types/uuid.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");
}
Loading