Skip to content

Commit e9eb7dd

Browse files
committed
feat(dataframe): add show() and show(int) methods
1 parent f0990e9 commit e9eb7dd

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎native/src/lib.rs‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use datafusion::dataframe::DataFrame;
2727
use datafusion::error::DataFusionError;
2828
use datafusion::prelude::{ParquetReadOptions, SessionContext};
2929
use jni::objects::{JByteArray, JClass, JString};
30-
use jni::sys::{jboolean, jlong};
30+
use jni::sys::{jboolean, jint, jlong};
3131
use jni::JNIEnv;
3232
use tokio::runtime::Runtime;
3333

@@ -114,6 +114,39 @@ pub extern "system" fn Java_org_apache_datafusion_DataFrame_countRows<'local>(
114114
})
115115
}
116116

117+
#[no_mangle]
118+
pub extern "system" fn Java_org_apache_datafusion_DataFrame_showDataFrame<'local>(
119+
mut env: JNIEnv<'local>,
120+
_class: JClass<'local>,
121+
handle: jlong,
122+
) {
123+
try_unwrap_or_throw(&mut env, (), |_env| -> JniResult<()> {
124+
if handle == 0 {
125+
return Err("DataFrame handle is null".into());
126+
}
127+
let df = unsafe { &*(handle as *const DataFrame) }.clone();
128+
runtime().block_on(async { df.show().await })?;
129+
Ok(())
130+
})
131+
}
132+
133+
#[no_mangle]
134+
pub extern "system" fn Java_org_apache_datafusion_DataFrame_showDataFrameWithLimit<'local>(
135+
mut env: JNIEnv<'local>,
136+
_class: JClass<'local>,
137+
handle: jlong,
138+
limit: jint,
139+
) {
140+
try_unwrap_or_throw(&mut env, (), |_env| -> JniResult<()> {
141+
if handle == 0 {
142+
return Err("DataFrame handle is null".into());
143+
}
144+
let df = unsafe { &*(handle as *const DataFrame) }.clone();
145+
runtime().block_on(async { df.show_limit(limit as usize).await })?;
146+
Ok(())
147+
})
148+
}
149+
117150
#[no_mangle]
118151
pub extern "system" fn Java_org_apache_datafusion_DataFrame_closeDataFrame<'local>(
119152
mut env: JNIEnv<'local>,

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ public long count() {
7878
return countRows(nativeHandle);
7979
}
8080

81+
/** Execute the plan and print formatted batches to native stdout. */
82+
public void show() {
83+
if (nativeHandle == 0) {
84+
throw new IllegalStateException("DataFrame is closed or already collected");
85+
}
86+
showDataFrame(nativeHandle);
87+
}
88+
89+
/** Execute the plan and print the first {@code limit} rows to native stdout. */
90+
public void show(int limit) {
91+
if (nativeHandle == 0) {
92+
throw new IllegalStateException("DataFrame is closed or already collected");
93+
}
94+
showDataFrameWithLimit(nativeHandle, limit);
95+
}
96+
8197
@Override
8298
public void close() {
8399
if (nativeHandle != 0) {
@@ -91,4 +107,8 @@ public void close() {
91107
private static native void closeDataFrame(long handle);
92108

93109
private static native long countRows(long handle);
110+
111+
private static native void showDataFrame(long handle);
112+
113+
private static native void showDataFrameWithLimit(long handle, int limit);
94114
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,22 @@ void countReturnsRowCount() {
3131
assertEquals(3L, df.count());
3232
}
3333
}
34+
35+
@Test
36+
void showDoesNotThrow() {
37+
try (SessionContext ctx = new SessionContext();
38+
DataFrame df = ctx.sql("SELECT * FROM (VALUES (1), (2)) AS t(x)")) {
39+
df.show();
40+
}
41+
}
42+
43+
@Test
44+
void showWithLimitDoesNotThrow() {
45+
try (SessionContext ctx = new SessionContext();
46+
DataFrame df = ctx.sql("SELECT * FROM (VALUES (1), (2), (3)) AS t(x)")) {
47+
df.show(0);
48+
df.show(1);
49+
df.show(1_000_000);
50+
}
51+
}
3452
}

0 commit comments

Comments
 (0)