Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions src/jni/duckdb_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include "duckdb/parser/parsed_data/create_type_info.hpp"
#include "functions.hpp"

#include <cstdint>
#include <limits>

using namespace duckdb;
using namespace std;

Expand Down Expand Up @@ -625,6 +628,9 @@ Value ToValue(JNIEnv *env, jobject param, duckdb::shared_ptr<ClientContext> cont
return (Value::BLOB_RAW(byte_array_to_string(env, (jbyteArray)param)));
} else if (env->IsInstanceOf(param, J_UUID)) {
auto most_significant = (jlong)env->CallObjectMethod(param, J_UUID_getMostSignificantBits);
// Account for the following logic in UUID::FromString:
// Flip the first bit to make `order by uuid` same as `order by uuid::varchar`
most_significant ^= (std::numeric_limits<int64_t>::min)();
auto least_significant = (jlong)env->CallObjectMethod(param, J_UUID_getLeastSignificantBits);
return (Value::UUID(hugeint_t(most_significant, least_significant)));
} else if (env->IsInstanceOf(param, J_DuckMap)) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/duckdb/DuckDBVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,10 @@ UUID getUuid(int idx) throws SQLException {
if (isType(DuckDBColumnType.UUID)) {
ByteBuffer buffer = getbuf(idx, 16);
long leastSignificantBits = buffer.getLong();

// Account for unsigned
long mostSignificantBits = buffer.getLong() - Long.MAX_VALUE - 1;
long mostSignificantBits = buffer.getLong();
// Account for the following logic in UUID::FromString:
// Flip the first bit to make `order by uuid` same as `order by uuid::varchar`
mostSignificantBits ^= Long.MIN_VALUE;
return new UUID(mostSignificantBits, leastSignificantBits);
}
Object o = getObject(idx);
Expand Down
24 changes: 19 additions & 5 deletions src/test/java/org/duckdb/TestDuckDBJDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -4317,11 +4317,25 @@ public static void test_offset_limit() throws Exception {
}

public static void test_UUID_binding() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL);
PreparedStatement statement = conn.prepareStatement("select '0b17ce61-375c-4ad8-97b3-349d96d35ab1'::UUID");
ResultSet resultSet = statement.executeQuery()) {
resultSet.next();
assertEquals(UUID.fromString("0b17ce61-375c-4ad8-97b3-349d96d35ab1"), resultSet.getObject(1));
UUID uuid = UUID.fromString("7f649593-934e-4945-9bd6-9a554f25b573");
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
try (Statement stmt = conn.createStatement()) {
// UUID is parsed from string by UUID::FromString
try (ResultSet rs = stmt.executeQuery("SELECT '" + uuid + "'::UUID")) {
rs.next();
Object obj = rs.getObject(1);
assertEquals(uuid, obj);
}
}
try (PreparedStatement stmt = conn.prepareStatement("SELECT ?")) {
// UUID is passed as 2 longs in JDBC ToValue
stmt.setObject(1, uuid);
try (ResultSet rs = stmt.executeQuery()) {
rs.next();
Object obj = rs.getObject(1);
assertEquals(uuid, obj);
}
}
}
}

Expand Down
Loading