Skip to content

Commit c768d16

Browse files
committed
feat(session-context): apply SessionConfig and RuntimeEnv options via builder
1 parent 9fbc812 commit c768d16

4 files changed

Lines changed: 85 additions & 2 deletions

File tree

‎native/src/lib.rs‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod session_options {
2323
include!(concat!(env!("OUT_DIR"), "/datafusion_java.rs"));
2424
}
2525

26+
use std::path::PathBuf;
2627
use std::sync::{Arc, OnceLock};
2728

2829
use datafusion::arrow::datatypes::{Schema, SchemaRef};
@@ -31,13 +32,16 @@ use datafusion::arrow::ipc::reader::StreamReader;
3132
use datafusion::arrow::record_batch::RecordBatchIterator;
3233
use datafusion::dataframe::DataFrame;
3334
use datafusion::error::DataFusionError;
34-
use datafusion::prelude::{ParquetReadOptions, SessionContext};
35+
use datafusion::execution::runtime_env::RuntimeEnvBuilder;
36+
use datafusion::prelude::{ParquetReadOptions, SessionConfig, SessionContext};
3537
use jni::objects::{JByteArray, JClass, JObjectArray, JString};
3638
use jni::sys::{jboolean, jint, jlong};
3739
use jni::JNIEnv;
40+
use prost::Message;
3841
use tokio::runtime::Runtime;
3942

4043
use crate::errors::{try_unwrap_or_throw, JniResult};
44+
use crate::session_options::SessionOptions;
4145

4246
pub(crate) fn runtime() -> &'static Runtime {
4347
static RT: OnceLock<Runtime> = OnceLock::new();
@@ -55,6 +59,46 @@ pub extern "system" fn Java_org_apache_datafusion_SessionContext_createSessionCo
5559
})
5660
}
5761

62+
#[no_mangle]
63+
pub extern "system" fn Java_org_apache_datafusion_SessionContext_createSessionContextWithOptions<
64+
'local,
65+
>(
66+
mut env: JNIEnv<'local>,
67+
_class: JClass<'local>,
68+
options_bytes: JByteArray<'local>,
69+
) -> jlong {
70+
try_unwrap_or_throw(&mut env, 0, |env| -> JniResult<jlong> {
71+
let bytes: Vec<u8> = env.convert_byte_array(&options_bytes)?;
72+
let opts = SessionOptions::decode(bytes.as_slice())?;
73+
74+
let mut config = SessionConfig::new();
75+
if let Some(v) = opts.batch_size {
76+
config = config.with_batch_size(v as usize);
77+
}
78+
if let Some(v) = opts.target_partitions {
79+
config = config.with_target_partitions(v as usize);
80+
}
81+
if let Some(v) = opts.collect_statistics {
82+
config = config.with_collect_statistics(v);
83+
}
84+
if let Some(v) = opts.information_schema {
85+
config = config.with_information_schema(v);
86+
}
87+
88+
let mut runtime = RuntimeEnvBuilder::new();
89+
if let Some(mem) = opts.memory_limit {
90+
runtime = runtime.with_memory_limit(mem.max_memory_bytes as usize, mem.memory_fraction);
91+
}
92+
if let Some(dir) = opts.temp_directory {
93+
runtime = runtime.with_temp_file_path(PathBuf::from(dir));
94+
}
95+
96+
let runtime_env = runtime.build()?;
97+
let ctx = SessionContext::new_with_config_rt(config, Arc::new(runtime_env));
98+
Ok(Box::into_raw(Box::new(ctx)) as jlong)
99+
})
100+
}
101+
58102
#[no_mangle]
59103
pub extern "system" fn Java_org_apache_datafusion_SessionContext_createDataFrame<'local>(
60104
mut env: JNIEnv<'local>,

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public SessionContext() {
5454
}
5555
}
5656

57+
SessionContext(byte[] optionsBytes) {
58+
this.nativeHandle = createSessionContextWithOptions(optionsBytes);
59+
if (this.nativeHandle == 0) {
60+
throw new RuntimeException("Failed to create native SessionContext");
61+
}
62+
}
63+
5764
/** Start configuring a {@link SessionContext}. */
5865
public static SessionContextBuilder builder() {
5966
return new SessionContextBuilder();
@@ -256,6 +263,8 @@ public void close() {
256263

257264
private static native long createSessionContext();
258265

266+
private static native long createSessionContextWithOptions(byte[] optionsBytes);
267+
259268
private static native long createDataFrame(long handle, String sql);
260269

261270
private static native long createDataFrameFromProto(long handle, byte[] planBytes);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public SessionContextBuilder informationSchema(boolean informationSchema) {
5757
return this;
5858
}
5959

60-
/** Cap the memory pool at {@code maxMemoryBytes}, reserving {@code fraction} of it for queries. */
60+
/**
61+
* Cap the memory pool at {@code maxMemoryBytes}, reserving {@code fraction} of it for queries.
62+
*/
6163
public SessionContextBuilder memoryLimit(long maxMemoryBytes, double fraction) {
6264
this.memoryLimitBytes = maxMemoryBytes;
6365
this.memoryLimitFraction = fraction;
@@ -70,6 +72,10 @@ public SessionContextBuilder tempDirectory(String path) {
7072
return this;
7173
}
7274

75+
public SessionContext build() {
76+
return new SessionContext(toBytes());
77+
}
78+
7379
byte[] toBytes() {
7480
SessionOptions.Builder b = SessionOptions.newBuilder();
7581
if (batchSize != null) {

‎src/test/java/org/apache/datafusion/SessionContextBuilderTest.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121

2222
import static org.junit.jupiter.api.Assertions.assertEquals;
2323
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
2425
import static org.junit.jupiter.api.Assertions.assertTrue;
2526

27+
import org.apache.arrow.memory.BufferAllocator;
28+
import org.apache.arrow.memory.RootAllocator;
29+
import org.apache.arrow.vector.ipc.ArrowReader;
2630
import org.apache.datafusion.protobuf.SessionOptions;
2731
import org.junit.jupiter.api.Test;
2832

@@ -67,4 +71,24 @@ void unsetFieldsAreAbsentInProto() throws Exception {
6771
assertFalse(parsed.hasMemoryLimit());
6872
assertFalse(parsed.hasTempDirectory());
6973
}
74+
75+
@Test
76+
void informationSchemaEnabledMakesMetaQueryRun() throws Exception {
77+
try (BufferAllocator allocator = new RootAllocator();
78+
SessionContext ctx = SessionContext.builder().informationSchema(true).build();
79+
DataFrame df = ctx.sql("SELECT table_name FROM information_schema.tables");
80+
ArrowReader reader = df.collect(allocator)) {
81+
// Loading a batch is enough to prove the query planned and executed.
82+
reader.loadNextBatch();
83+
}
84+
}
85+
86+
@Test
87+
void informationSchemaDisabledByDefaultThrows() {
88+
try (SessionContext ctx = SessionContext.builder().build()) {
89+
assertThrows(
90+
RuntimeException.class,
91+
() -> ctx.sql("SELECT table_name FROM information_schema.tables"));
92+
}
93+
}
7094
}

0 commit comments

Comments
 (0)