Skip to content

Commit f0990e9

Browse files
committed
feat(dataframe): add count() method
1 parent 233243c commit f0990e9

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎native/src/lib.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ pub extern "system" fn Java_org_apache_datafusion_DataFrame_collectDataFrame<'lo
9898
})
9999
}
100100

101+
#[no_mangle]
102+
pub extern "system" fn Java_org_apache_datafusion_DataFrame_countRows<'local>(
103+
mut env: JNIEnv<'local>,
104+
_class: JClass<'local>,
105+
handle: jlong,
106+
) -> jlong {
107+
try_unwrap_or_throw(&mut env, 0, |_env| -> JniResult<jlong> {
108+
if handle == 0 {
109+
return Err("DataFrame handle is null".into());
110+
}
111+
let df = unsafe { &*(handle as *const DataFrame) }.clone();
112+
let n = runtime().block_on(async { df.count().await })?;
113+
Ok(n as jlong)
114+
})
115+
}
116+
101117
#[no_mangle]
102118
pub extern "system" fn Java_org_apache_datafusion_DataFrame_closeDataFrame<'local>(
103119
mut env: JNIEnv<'local>,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ public ArrowReader collect(BufferAllocator allocator) {
7070
}
7171
}
7272

73+
/** Execute the plan and return the number of rows. */
74+
public long count() {
75+
if (nativeHandle == 0) {
76+
throw new IllegalStateException("DataFrame is closed or already collected");
77+
}
78+
return countRows(nativeHandle);
79+
}
80+
7381
@Override
7482
public void close() {
7583
if (nativeHandle != 0) {
@@ -81,4 +89,6 @@ public void close() {
8189
private static native void collectDataFrame(long handle, long ffiStreamAddr);
8290

8391
private static native void closeDataFrame(long handle);
92+
93+
private static native long countRows(long handle);
8494
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
class DataFrameTransformationsTest {
27+
@Test
28+
void countReturnsRowCount() {
29+
try (SessionContext ctx = new SessionContext();
30+
DataFrame df = ctx.sql("SELECT * FROM (VALUES (1), (2), (3)) AS t(x)")) {
31+
assertEquals(3L, df.count());
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)