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

fix: blob usage after resultset move #103

Merged
merged 2 commits into from
Nov 7, 2024
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
11 changes: 8 additions & 3 deletions src/jni/duckdb_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,15 +994,20 @@ jobject ProcessVector(JNIEnv *env, Connection *conn_ref, Vector &vec, idx_t row_
break;
}
case LogicalTypeId::BLOB:
varlen_data = env->NewObjectArray(row_count, J_ByteBuffer, nullptr);
varlen_data = env->NewObjectArray(row_count, J_ByteArray, nullptr);

for (idx_t row_idx = 0; row_idx < row_count; row_idx++) {
if (FlatVector::IsNull(vec, row_idx)) {
continue;
}
auto &d_str = ((string_t *)FlatVector::GetData(vec))[row_idx];
auto j_obj = env->NewDirectByteBuffer((void *)d_str.GetData(), d_str.GetSize());
env->SetObjectArrayElement(varlen_data, row_idx, j_obj);

auto j_arr = env->NewByteArray(d_str.GetSize());
auto j_arr_el = env->GetByteArrayElements(j_arr, nullptr);
memcpy((void *)j_arr_el, (void *)d_str.GetData(), d_str.GetSize());
env->ReleaseByteArrayElements(j_arr, j_arr_el, 0);

env->SetObjectArrayElement(varlen_data, row_idx, j_arr);
}
break;
case LogicalTypeId::UUID:
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/duckdb/DuckDBVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ Blob getBlob(int idx) throws SQLException {
return null;
}
if (isType(DuckDBColumnType.BLOB)) {
return new DuckDBResultSet.DuckDBBlobResult((ByteBuffer) varlen_data[idx]);
return new DuckDBResultSet.DuckDBBlobResult(ByteBuffer.wrap((byte[]) varlen_data[idx]));
}

throw new SQLFeatureNotSupportedException("getBlob");
Expand All @@ -290,11 +290,7 @@ byte[] getBytes(int idx) throws SQLException {
}

if (isType(DuckDBColumnType.BLOB)) {
ByteBuffer bb = (ByteBuffer) varlen_data[idx];
bb.position(0);
byte[] bytes = new byte[bb.remaining()];
bb.get(bytes);
return bytes;
return (byte[]) varlen_data[idx];
}

throw new SQLFeatureNotSupportedException("getBytes");
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/duckdb/TestDuckDBJDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -4604,6 +4604,20 @@ public static void test_metadata_get_index_info() throws Exception {
}
}

public static void test_blob_after_rs_next() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
try (Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt.executeQuery("SELECT 'AAAA'::BLOB;")) {
Blob blob = null;
while (rs.next()) {
blob = rs.getBlob(1);
}
assertEquals(blob_to_string(blob), "AAAA");
}
}
}
}

public static void main(String[] args) throws Exception {
System.exit(runTests(args, TestDuckDBJDBC.class, TestExtensionTypes.class));
}
Expand Down
Loading