Skip to content

Commit 3b18743

Browse files
committed
feat: propagate Java stack traces from JVM upcalls into DataFusionError
1 parent f9cdc15 commit 3b18743

11 files changed

Lines changed: 634 additions & 31 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.datafusion;
21+
22+
/**
23+
* Controls how detailed the representation of a Java throwable is when a JVM upcall (UDF, {@link
24+
* TableProvider}) throws and the failure has to cross back into native code.
25+
*
26+
* <p>Set on {@link SessionContextBuilder#exceptionVerbosity}. The setting is locked at
27+
* session-construction time and applies uniformly to every UDF and table provider registered
28+
* against the session.
29+
*/
30+
public enum ExceptionVerbosity {
31+
/** Class name + {@code getMessage()} + standard Java stack trace. The default. */
32+
FULL,
33+
/**
34+
* Class name + {@code getMessage()} only. Matches the behaviour of `datafusion-java` before this
35+
* setting existed; useful when full traces are too verbose for production logs.
36+
*/
37+
MESSAGE,
38+
/**
39+
* Class name only. For callers who treat exception bodies as untrusted user input that shouldn't
40+
* reach logs.
41+
*/
42+
NONE;
43+
44+
/**
45+
* Byte tag passed through {@code SessionContext.registerScalarUdf} / {@code registerTableNative}
46+
* to the native side. {@code 1=FULL}, {@code 2=MESSAGE}, {@code 3=NONE}; {@code 0} is reserved as
47+
* a "bogus" sentinel and rejected by the Rust decoder.
48+
*/
49+
byte toByte() {
50+
switch (this) {
51+
case FULL:
52+
return 1;
53+
case MESSAGE:
54+
return 2;
55+
case NONE:
56+
return 3;
57+
default:
58+
throw new IllegalStateException("unreachable: unknown ExceptionVerbosity " + this);
59+
}
60+
}
61+
}

‎core/src/main/java/org/apache/datafusion/SessionContext.java‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,27 @@ public final class SessionContext implements AutoCloseable {
4949
}
5050

5151
private long nativeHandle;
52+
// Snapshot of the builder's exceptionVerbosity, passed through to UDF /
53+
// TableProvider registrations. Locked at construction time; not mutable.
54+
// Default FULL keeps stack traces flowing for callers that don't touch the
55+
// setter. Stored as a byte to keep the JNI signature ABI-stable across any
56+
// future ExceptionVerbosity additions.
57+
private final byte exceptionVerbosity;
5258

5359
public SessionContext() {
5460
this.nativeHandle = createSessionContext();
5561
if (this.nativeHandle == 0) {
5662
throw new RuntimeException("Failed to create native SessionContext");
5763
}
64+
this.exceptionVerbosity = ExceptionVerbosity.FULL.toByte();
5865
}
5966

60-
SessionContext(byte[] optionsBytes) {
67+
SessionContext(byte[] optionsBytes, ExceptionVerbosity exceptionVerbosity) {
6168
this.nativeHandle = createSessionContextWithOptions(optionsBytes);
6269
if (this.nativeHandle == 0) {
6370
throw new RuntimeException("Failed to create native SessionContext");
6471
}
72+
this.exceptionVerbosity = exceptionVerbosity.toByte();
6573
}
6674

6775
/** Start configuring a {@link SessionContext}. */
@@ -550,7 +558,8 @@ public void registerUdf(ScalarUdf udf) {
550558
fields.addAll(udf.argFields());
551559
Schema signatureSchema = new Schema(fields);
552560
byte[] signatureBytes = serializeSchemaIpc(signatureSchema);
553-
registerScalarUdf(nativeHandle, name, signatureBytes, volatility.code(), impl);
561+
registerScalarUdf(
562+
nativeHandle, name, signatureBytes, volatility.code(), impl, exceptionVerbosity);
554563
}
555564

556565
/**
@@ -585,7 +594,7 @@ public void registerTable(String name, TableProvider provider) {
585594
throw new IllegalStateException("TableProvider.schema returned null");
586595
}
587596
byte[] schemaIpc = serializeSchemaIpc(schema);
588-
registerTableNative(nativeHandle, name, schemaIpc, provider);
597+
registerTableNative(nativeHandle, name, schemaIpc, provider, exceptionVerbosity);
589598
}
590599

591600
private static byte[] serializeSchemaIpc(Schema schema) {
@@ -660,8 +669,17 @@ private static native long readJsonWithOptions(
660669
private static native void closeSessionContext(long handle);
661670

662671
private static native void registerScalarUdf(
663-
long handle, String name, byte[] signatureSchemaBytes, byte volatility, ScalarFunction impl);
672+
long handle,
673+
String name,
674+
byte[] signatureSchemaBytes,
675+
byte volatility,
676+
ScalarFunction impl,
677+
byte exceptionVerbosity);
664678

665679
private static native void registerTableNative(
666-
long handle, String name, byte[] schemaIpcBytes, TableProvider provider);
680+
long handle,
681+
String name,
682+
byte[] schemaIpcBytes,
683+
TableProvider provider,
684+
byte exceptionVerbosity);
667685
}

‎core/src/main/java/org/apache/datafusion/SessionContextBuilder.java‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public final class SessionContextBuilder {
4343
private String tempDirectory;
4444
private boolean spillDisabled;
4545
private Long maxTempDirectorySize;
46+
private ExceptionVerbosity exceptionVerbosity;
4647
private CacheManagerOptions cacheManager;
4748
private final LinkedHashMap<String, String> options = new LinkedHashMap<>();
4849
private final List<ObjectStoreOptions> objectStores = new ArrayList<>();
@@ -208,6 +209,24 @@ public SessionContextBuilder setOptions(Map<String, String> entries) {
208209
return this;
209210
}
210211

212+
/**
213+
* Configure how detailed the representation of a Java throwable is when a JVM upcall (UDF, {@link
214+
* TableProvider}) throws and the failure has to cross back into native code.
215+
*
216+
* <p>The setting is locked at session-construction time and applies uniformly to every UDF and
217+
* table provider registered against the session. See {@link ExceptionVerbosity} for the three
218+
* values; default (unset) is {@link ExceptionVerbosity#FULL}.
219+
*
220+
* @throws IllegalArgumentException if {@code verbosity} is {@code null}.
221+
*/
222+
public SessionContextBuilder exceptionVerbosity(ExceptionVerbosity verbosity) {
223+
if (verbosity == null) {
224+
throw new IllegalArgumentException("exceptionVerbosity must be non-null");
225+
}
226+
this.exceptionVerbosity = verbosity;
227+
return this;
228+
}
229+
211230
/**
212231
* Configure DataFusion's built-in {@code CacheManager} for the new context. Build the {@link
213232
* CacheManagerOptions} via {@link CacheManagerOptions#builder()}; each cache slot is independent,
@@ -264,7 +283,7 @@ public SessionContext build() {
264283
throw new IllegalStateException(
265284
"disableSpill() is mutually exclusive with tempDirectory(...)");
266285
}
267-
return new SessionContext(toBytes());
286+
return new SessionContext(toBytes(), exceptionVerbosityOrDefault());
268287
}
269288

270289
byte[] toBytes() {
@@ -306,6 +325,9 @@ byte[] toBytes() {
306325
if (cacheManager != null) {
307326
b.setCacheManager(cacheManager.toProto());
308327
}
328+
// exceptionVerbosity flows via SessionContext's snapshot field, not the
329+
// SessionOptions proto -- it's only consumed at registerUdf /
330+
// registerTable JNI calls, where it rides on its own arg.
309331
for (Map.Entry<String, String> e : options.entrySet()) {
310332
b.addOptions(ConfigOption.newBuilder().setKey(e.getKey()).setValue(e.getValue()).build());
311333
}
@@ -314,4 +336,9 @@ byte[] toBytes() {
314336
}
315337
return b.build().toByteArray();
316338
}
339+
340+
/** Visible for {@link SessionContext} so it can fall back to the FULL byte when unset. */
341+
ExceptionVerbosity exceptionVerbosityOrDefault() {
342+
return exceptionVerbosity != null ? exceptionVerbosity : ExceptionVerbosity.FULL;
343+
}
317344
}

0 commit comments

Comments
 (0)