Skip to content

Commit 45b379e

Browse files
committed
feat(dataframe): add limit method
1 parent f61b10f commit 45b379e

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

‎native/src/lib.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,24 @@ pub extern "system" fn Java_org_apache_datafusion_DataFrame_filterRows<'local>(
195195
})
196196
}
197197

198+
#[no_mangle]
199+
pub extern "system" fn Java_org_apache_datafusion_DataFrame_limitRows<'local>(
200+
mut env: JNIEnv<'local>,
201+
_class: JClass<'local>,
202+
handle: jlong,
203+
skip: jint,
204+
fetch: jint,
205+
) -> jlong {
206+
try_unwrap_or_throw(&mut env, 0, |_env| -> JniResult<jlong> {
207+
if handle == 0 {
208+
return Err("DataFrame handle is null".into());
209+
}
210+
let df = unsafe { &*(handle as *const DataFrame) }.clone();
211+
let new_df = df.limit(skip as usize, Some(fetch as usize))?;
212+
Ok(Box::into_raw(Box::new(new_df)) as jlong)
213+
})
214+
}
215+
198216
#[no_mangle]
199217
pub extern "system" fn Java_org_apache_datafusion_DataFrame_closeDataFrame<'local>(
200218
mut env: JNIEnv<'local>,

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,31 @@ public DataFrame filter(String predicate) {
116116
return new DataFrame(filterRows(nativeHandle, predicate));
117117
}
118118

119+
/**
120+
* Take the first {@code fetch} rows. Equivalent to {@link #limit(int, int)} with {@code skip =
121+
* 0}. The receiver remains usable and must still be closed independently.
122+
*/
123+
public DataFrame limit(int fetch) {
124+
return limit(0, fetch);
125+
}
126+
127+
/**
128+
* Skip {@code skip} rows, then take the next {@code fetch} rows. Both arguments must be
129+
* non-negative. The receiver remains usable and must still be closed independently.
130+
*/
131+
public DataFrame limit(int skip, int fetch) {
132+
if (skip < 0) {
133+
throw new IllegalArgumentException("skip must be non-negative, was " + skip);
134+
}
135+
if (fetch < 0) {
136+
throw new IllegalArgumentException("fetch must be non-negative, was " + fetch);
137+
}
138+
if (nativeHandle == 0) {
139+
throw new IllegalStateException("DataFrame is closed or already collected");
140+
}
141+
return new DataFrame(limitRows(nativeHandle, skip, fetch));
142+
}
143+
119144
@Override
120145
public void close() {
121146
if (nativeHandle != 0) {
@@ -137,4 +162,6 @@ public void close() {
137162
private static native long selectColumns(long handle, String[] columnNames);
138163

139164
private static native long filterRows(long handle, String predicate);
165+
166+
private static native long limitRows(long handle, int skip, int fetch);
140167
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,43 @@ void lineitemFilterCountAgainstSqlBaseline() throws Exception {
193193
assertEquals(viaSql, viaDataFrame);
194194
}
195195
}
196+
197+
@Test
198+
void limitTakesFirstNRows() {
199+
try (SessionContext ctx = new SessionContext();
200+
DataFrame source = ctx.sql("SELECT * FROM (VALUES (1), (2), (3), (4), (5)) AS t(x)");
201+
DataFrame limited = source.limit(2)) {
202+
assertEquals(2L, limited.count());
203+
}
204+
}
205+
206+
@Test
207+
void limitWithSkipDropsLeadingRows() {
208+
try (SessionContext ctx = new SessionContext();
209+
DataFrame source = ctx.sql("SELECT * FROM (VALUES (1), (2), (3), (4), (5)) AS t(x)");
210+
DataFrame limited = source.limit(2, 2)) {
211+
assertEquals(2L, limited.count());
212+
}
213+
}
214+
215+
@Test
216+
void limitIsNonDestructive() {
217+
try (SessionContext ctx = new SessionContext();
218+
DataFrame source = ctx.sql("SELECT * FROM (VALUES (1), (2), (3)) AS t(x)")) {
219+
try (DataFrame limited = source.limit(1)) {
220+
assertEquals(1L, limited.count());
221+
}
222+
assertEquals(3L, source.count());
223+
}
224+
}
225+
226+
@Test
227+
void limitRejectsNegativeArgs() {
228+
try (SessionContext ctx = new SessionContext();
229+
DataFrame df = ctx.sql("SELECT 1 AS x")) {
230+
assertThrows(IllegalArgumentException.class, () -> df.limit(-1));
231+
assertThrows(IllegalArgumentException.class, () -> df.limit(-1, 0));
232+
assertThrows(IllegalArgumentException.class, () -> df.limit(0, -1));
233+
}
234+
}
196235
}

0 commit comments

Comments
 (0)