Skip to content

Commit a87eb0f

Browse files
committed
getProfilingInformation api
1 parent eb3c22c commit a87eb0f

File tree

8 files changed

+117
-9
lines changed

8 files changed

+117
-9
lines changed

.github/workflows/Java.yml

+9-9
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
run: |
6464
cp build/release/duckdb_jdbc.jar duckdb_jdbc-linux-amd64.jar
6565
./scripts/upload-assets-to-staging.sh github_release duckdb_jdbc-linux-amd64.jar
66-
- uses: actions/upload-artifact@v3
66+
- uses: actions/upload-artifact@v4
6767
with:
6868
name: java-linux-amd64
6969
path: |
@@ -108,7 +108,7 @@ jobs:
108108
cp build/release/duckdb_jdbc.jar duckdb_jdbc-linux-aarch64.jar
109109
# ./scripts/upload-assets-to-staging.sh github_release duckdb_jdbc-linux-aarch64.jar
110110
111-
- uses: actions/upload-artifact@v3
111+
- uses: actions/upload-artifact@v4
112112
with:
113113
name: java-linux-aarch64
114114
path: |
@@ -150,7 +150,7 @@ jobs:
150150
run: |
151151
cp build/release/duckdb_jdbc.jar duckdb_jdbc-windows-amd64.jar
152152
./scripts/upload-assets-to-staging.sh github_release duckdb_jdbc-windows-amd64.jar
153-
- uses: actions/upload-artifact@v3
153+
- uses: actions/upload-artifact@v4
154154
with:
155155
name: java-windows-amd64
156156
path: |
@@ -192,7 +192,7 @@ jobs:
192192
run: |
193193
cp build/release/duckdb_jdbc.jar duckdb_jdbc-osx-universal.jar
194194
./scripts/upload-assets-to-staging.sh github_release duckdb_jdbc-osx-universal.jar
195-
- uses: actions/upload-artifact@v3
195+
- uses: actions/upload-artifact@v4
196196
with:
197197
name: java-osx-universal
198198
path: |
@@ -218,22 +218,22 @@ jobs:
218218
- shell: bash
219219
run: mkdir jdbc-artifacts
220220

221-
- uses: actions/download-artifact@v3
221+
- uses: actions/download-artifact@v4
222222
with:
223223
name: java-linux-aarch64
224224
path: jdbc-artifacts/java-linux-aarch64
225225

226-
- uses: actions/download-artifact@v3
226+
- uses: actions/download-artifact@v4
227227
with:
228228
name: java-linux-amd64
229229
path: jdbc-artifacts/java-linux-amd64
230230

231-
- uses: actions/download-artifact@v3
231+
- uses: actions/download-artifact@v4
232232
with:
233233
name: java-windows-amd64
234234
path: jdbc-artifacts/java-windows-amd64
235235

236-
- uses: actions/download-artifact@v3
236+
- uses: actions/download-artifact@v4
237237
with:
238238
name: java-osx-universal
239239
path: jdbc-artifacts/java-osx-universal
@@ -267,7 +267,7 @@ jobs:
267267
268268
- name: Upload artifacts
269269
if: always()
270-
uses: actions/upload-artifact@v3
270+
uses: actions/upload-artifact@v4
271271
with:
272272
name: java-jars
273273
path: |

src/jni/duckdb_java.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ static jmethodID J_Object_toString;
105105

106106
static jclass J_DuckDBTime;
107107

108+
static jobject J_ProfilerPrintFormat_QUERY_TREE;
109+
static jobject J_ProfilerPrintFormat_JSON;
110+
static jobject J_ProfilerPrintFormat_QUERY_TREE_OPTIMIZER;
111+
static jobject J_ProfilerPrintFormat_NO_OUTPUT;
112+
static jobject J_ProfilerPrintFormat_HTML;
113+
static jobject J_ProfilerPrintFormat_GRAPHVIZ;
114+
108115
void ThrowJNI(JNIEnv *env, const char *message) {
109116
D_ASSERT(J_SQLException);
110117
env->ThrowNew(J_SQLException, message);
@@ -123,6 +130,12 @@ static jclass GetClassRef(JNIEnv *env, const string &name) {
123130
return globalRef;
124131
}
125132

133+
static jobject GetObjectStaticField(JNIEnv *env, jclass clazz, const char *name, const char *sig) {
134+
auto field = env->GetStaticFieldID(clazz, name, sig);
135+
auto obj = env->GetStaticObjectField(clazz, field);
136+
return env->NewGlobalRef(obj);
137+
}
138+
126139
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
127140
// Get JNIEnv from vm
128141
JNIEnv *env;
@@ -291,6 +304,21 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
291304
J_Object_toString = env->GetMethodID(tmpLocalRef, "toString", "()Ljava/lang/String;");
292305
env->DeleteLocalRef(tmpLocalRef);
293306

307+
tmpLocalRef = env->FindClass("org/duckdb/ProfilerPrintFormat");
308+
309+
J_ProfilerPrintFormat_QUERY_TREE =
310+
GetObjectStaticField(env, tmpLocalRef, "QUERY_TREE", "Lorg/duckdb/ProfilerPrintFormat;");
311+
J_ProfilerPrintFormat_JSON = GetObjectStaticField(env, tmpLocalRef, "JSON", "Lorg/duckdb/ProfilerPrintFormat;");
312+
J_ProfilerPrintFormat_QUERY_TREE_OPTIMIZER =
313+
GetObjectStaticField(env, tmpLocalRef, "QUERY_TREE_OPTIMIZER", "Lorg/duckdb/ProfilerPrintFormat;");
314+
J_ProfilerPrintFormat_NO_OUTPUT =
315+
GetObjectStaticField(env, tmpLocalRef, "NO_OUTPUT", "Lorg/duckdb/ProfilerPrintFormat;");
316+
J_ProfilerPrintFormat_HTML = GetObjectStaticField(env, tmpLocalRef, "HTML", "Lorg/duckdb/ProfilerPrintFormat;");
317+
J_ProfilerPrintFormat_GRAPHVIZ =
318+
GetObjectStaticField(env, tmpLocalRef, "GRAPHVIZ", "Lorg/duckdb/ProfilerPrintFormat;");
319+
320+
env->DeleteLocalRef(tmpLocalRef);
321+
294322
return JNI_VERSION;
295323
}
296324

@@ -317,6 +345,12 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
317345
env->DeleteGlobalRef(J_DuckResultSetMeta);
318346
env->DeleteGlobalRef(J_DuckVector);
319347
env->DeleteGlobalRef(J_ByteBuffer);
348+
env->DeleteGlobalRef(J_ProfilerPrintFormat_QUERY_TREE);
349+
env->DeleteGlobalRef(J_ProfilerPrintFormat_JSON);
350+
env->DeleteGlobalRef(J_ProfilerPrintFormat_QUERY_TREE_OPTIMIZER);
351+
env->DeleteGlobalRef(J_ProfilerPrintFormat_NO_OUTPUT);
352+
env->DeleteGlobalRef(J_ProfilerPrintFormat_HTML);
353+
env->DeleteGlobalRef(J_ProfilerPrintFormat_GRAPHVIZ);
320354

321355
for (auto &clazz : toFree) {
322356
env->DeleteGlobalRef(clazz);
@@ -1269,3 +1303,34 @@ void _duckdb_jdbc_create_extension_type(JNIEnv *env, jclass, jobject conn_buf) {
12691303
byte_test_type_type.SetAlias("byte_test_type");
12701304
ExtensionUtil::RegisterType(db_instance, "byte_test_type", byte_test_type_type);
12711305
}
1306+
1307+
static ProfilerPrintFormat GetProfilerPrintFormat(JNIEnv *env, jobject format) {
1308+
if (env->IsSameObject(format, J_ProfilerPrintFormat_QUERY_TREE)) {
1309+
return ProfilerPrintFormat::QUERY_TREE;
1310+
}
1311+
if (env->IsSameObject(format, J_ProfilerPrintFormat_JSON)) {
1312+
return ProfilerPrintFormat::JSON;
1313+
}
1314+
if (env->IsSameObject(format, J_ProfilerPrintFormat_QUERY_TREE_OPTIMIZER)) {
1315+
return ProfilerPrintFormat::QUERY_TREE_OPTIMIZER;
1316+
}
1317+
if (env->IsSameObject(format, J_ProfilerPrintFormat_NO_OUTPUT)) {
1318+
return ProfilerPrintFormat::NO_OUTPUT;
1319+
}
1320+
if (env->IsSameObject(format, J_ProfilerPrintFormat_HTML)) {
1321+
return ProfilerPrintFormat::HTML;
1322+
}
1323+
if (env->IsSameObject(format, J_ProfilerPrintFormat_GRAPHVIZ)) {
1324+
return ProfilerPrintFormat::GRAPHVIZ;
1325+
}
1326+
}
1327+
1328+
jstring _duckdb_jdbc_get_profiling_information(JNIEnv *env, jclass, jobject conn_ref_buf, jobject j_format) {
1329+
auto connection = get_connection(env, conn_ref_buf);
1330+
if (!connection) {
1331+
throw InvalidInputException("Invalid connection");
1332+
}
1333+
auto format = GetProfilerPrintFormat(env, j_format);
1334+
auto profiling_info = connection->GetProfilingInformation(format);
1335+
return env->NewStringUTF(profiling_info.c_str());
1336+
}

src/jni/functions.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,13 @@ JNIEXPORT void JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1create_1extens
396396

397397
}
398398
}
399+
400+
JNIEXPORT jstring JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1get_1profiling_1information(JNIEnv * env, jclass param0, jobject param1, jobject param2) {
401+
try {
402+
return _duckdb_jdbc_get_profiling_information(env, param0, param1, param2);
403+
} catch (const std::exception &e) {
404+
duckdb::ErrorData error(e);
405+
ThrowJNI(env, error.Message().c_str());
406+
407+
}
408+
}

src/jni/functions.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,7 @@ JNIEXPORT void JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1appender_1appe
160160
void _duckdb_jdbc_create_extension_type(JNIEnv * env, jclass param0, jobject param1);
161161

162162
JNIEXPORT void JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1create_1extension_1type(JNIEnv * env, jclass param0, jobject param1);
163+
164+
jstring _duckdb_jdbc_get_profiling_information(JNIEnv * env, jclass param0, jobject param1, jobject param2);
165+
166+
JNIEXPORT jstring JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1get_1profiling_1information(JNIEnv * env, jclass param0, jobject param1, jobject param2);

src/main/java/org/duckdb/DuckDBConnection.java

+4
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,8 @@ public void registerArrowStream(String name, Object arrow_array_stream) {
371371
long array_stream_address = getArrowStreamAddress(arrow_array_stream);
372372
DuckDBNative.duckdb_jdbc_arrow_register(conn_ref, array_stream_address, name.getBytes(StandardCharsets.UTF_8));
373373
}
374+
375+
public String getProfilingInformation(ProfilerPrintFormat format) throws SQLException {
376+
return DuckDBNative.duckdb_jdbc_get_profiling_information(conn_ref, format);
377+
}
374378
}

src/main/java/org/duckdb/DuckDBNative.java

+4
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ protected static native void duckdb_jdbc_appender_append_decimal(ByteBuffer appe
167167

168168
protected static native void duckdb_jdbc_create_extension_type(ByteBuffer conn_ref) throws SQLException;
169169

170+
protected static native String duckdb_jdbc_get_profiling_information(ByteBuffer conn_ref,
171+
ProfilerPrintFormat format)
172+
throws SQLException;
173+
170174
public static void duckdb_jdbc_create_extension_type(DuckDBConnection conn) throws SQLException {
171175
duckdb_jdbc_create_extension_type(conn.conn_ref);
172176
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.duckdb;
2+
3+
public enum ProfilerPrintFormat {
4+
5+
QUERY_TREE,
6+
JSON,
7+
QUERY_TREE_OPTIMIZER,
8+
NO_OUTPUT,
9+
HTML,
10+
GRAPHVIZ
11+
}

src/test/java/org/duckdb/TestDuckDBJDBC.java

+10
Original file line numberDiff line numberDiff line change
@@ -4676,6 +4676,16 @@ public static void test_blob_after_rs_next() throws Exception {
46764676
}
46774677
}
46784678

4679+
public static void test_get_profiling_information() throws Exception {
4680+
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement()) {
4681+
stmt.execute("SET enable_profiling = 'no_output';");
4682+
try (ResultSet rs = stmt.executeQuery("SELECT 1+1")) {
4683+
String profile = ((DuckDBConnection) conn).getProfilingInformation(ProfilerPrintFormat.JSON);
4684+
assertTrue(profile.contains("\"query_name\": \"SELECT 1+1\","));
4685+
}
4686+
}
4687+
}
4688+
46794689
public static void main(String[] args) throws Exception {
46804690
System.exit(runTests(args, TestDuckDBJDBC.class, TestExtensionTypes.class));
46814691
}

0 commit comments

Comments
 (0)