File tree Expand file tree Collapse file tree
main/java/org/apache/datafusion
test/java/org/apache/datafusion Expand file tree Collapse file tree Original file line number Diff line number Diff 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]
102118pub extern "system" fn Java_org_apache_datafusion_DataFrame_closeDataFrame < ' local > (
103119 mut env : JNIEnv < ' local > ,
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments