Skip to content

Commit 5b4689e

Browse files
committed
feat(dataframe): add filter(String) method
1 parent 68417cb commit 5b4689e

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎native/src/lib.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,25 @@ pub extern "system" fn Java_org_apache_datafusion_DataFrame_selectColumns<'local
174174
})
175175
}
176176

177+
#[no_mangle]
178+
pub extern "system" fn Java_org_apache_datafusion_DataFrame_filterRows<'local>(
179+
mut env: JNIEnv<'local>,
180+
_class: JClass<'local>,
181+
handle: jlong,
182+
predicate: JString<'local>,
183+
) -> jlong {
184+
try_unwrap_or_throw(&mut env, 0, |env| -> JniResult<jlong> {
185+
if handle == 0 {
186+
return Err("DataFrame handle is null".into());
187+
}
188+
let df = unsafe { &*(handle as *const DataFrame) }.clone();
189+
let predicate: String = env.get_string(&predicate)?.into();
190+
let expr = df.parse_sql_expr(&predicate)?;
191+
let new_df = df.filter(expr)?;
192+
Ok(Box::into_raw(Box::new(new_df)) as jlong)
193+
})
194+
}
195+
177196
#[no_mangle]
178197
pub extern "system" fn Java_org_apache_datafusion_DataFrame_closeDataFrame<'local>(
179198
mut env: JNIEnv<'local>,

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ public DataFrame select(String... columnNames) {
105105
return new DataFrame(selectColumns(nativeHandle, columnNames));
106106
}
107107

108+
/**
109+
* Apply a SQL predicate to produce a filtered DataFrame. The predicate is parsed against this
110+
* DataFrame's own schema. The receiver remains usable and must still be closed independently.
111+
*/
112+
public DataFrame filter(String predicate) {
113+
if (nativeHandle == 0) {
114+
throw new IllegalStateException("DataFrame is closed or already collected");
115+
}
116+
return new DataFrame(filterRows(nativeHandle, predicate));
117+
}
118+
108119
@Override
109120
public void close() {
110121
if (nativeHandle != 0) {
@@ -124,4 +135,6 @@ public void close() {
124135
private static native void showDataFrameWithLimit(long handle, int limit);
125136

126137
private static native long selectColumns(long handle, String[] columnNames);
138+
139+
private static native long filterRows(long handle, String predicate);
127140
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,34 @@ void selectIsNonDestructive() {
8585
assertEquals(1L, source.count());
8686
}
8787
}
88+
89+
@Test
90+
void filterRemovesRows() {
91+
try (SessionContext ctx = new SessionContext();
92+
DataFrame source = ctx.sql("SELECT * FROM (VALUES (1), (2), (3), (4)) AS t(x)");
93+
DataFrame filtered = source.filter("x > 2")) {
94+
assertEquals(2L, filtered.count());
95+
}
96+
}
97+
98+
@Test
99+
void filterIsNonDestructive() {
100+
try (SessionContext ctx = new SessionContext();
101+
DataFrame source = ctx.sql("SELECT * FROM (VALUES (1), (2), (3), (4)) AS t(x)")) {
102+
try (DataFrame filtered = source.filter("x > 2")) {
103+
assertEquals(2L, filtered.count());
104+
}
105+
assertEquals(4L, source.count());
106+
}
107+
}
108+
109+
@Test
110+
void chainFilterSelectCount() {
111+
try (SessionContext ctx = new SessionContext();
112+
DataFrame source =
113+
ctx.sql("SELECT 1 AS a, 2 AS b UNION ALL SELECT 10 AS a, 20 AS b");
114+
DataFrame chained = source.filter("a > 5").select("b")) {
115+
assertEquals(1L, chained.count());
116+
}
117+
}
88118
}

0 commit comments

Comments
 (0)