coderfender commented on code in PR #102:
URL: https://github.com/apache/datafusion-java/pull/102#discussion_r3366161400


##########
core/src/test/java/org/apache/datafusion/SessionContextTableRegistrationTest.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datafusion;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+class SessionContextTableRegistrationTest {
+
+  @Test

Review Comment:
   Creating a new test class for two new APIs is probably a bit too much IMO. 
Perhaps we could add these tests to existing test suite and/ or create a new 
one? 



##########
core/src/main/java/org/apache/datafusion/SessionContext.java:
##########
@@ -601,6 +601,35 @@ private static byte[] serializeSchemaIpc(Schema schema) {
     return baos.toByteArray();
   }
 
+  /**
+   * Returns {@code true} if a table with the given name is registered in this 
session.
+   *
+   * <p>This is the Java counterpart to DataFusion's Rust {@code 
SessionContext::table_exist}.
+   *
+   * @throws IllegalStateException if this context is closed.
+   */
+  public boolean tableExists(String name) {
+    if (nativeHandle == 0) {
+      throw new IllegalStateException("SessionContext is closed");

Review Comment:
   We could avoid repetitive code here and port the context check to a function 
? 



##########
native/src/lib.rs:
##########
@@ -1042,6 +1042,41 @@ pub extern "system" fn 
Java_org_apache_datafusion_SessionContext_getOptionNative
     )
 }
 
+#[no_mangle]
+pub extern "system" fn 
Java_org_apache_datafusion_SessionContext_tableExists<'local>(
+    mut env: JNIEnv<'local>,
+    _class: JClass<'local>,
+    handle: jlong,
+    name: JString<'local>,
+) -> jboolean {
+    try_unwrap_or_throw(&mut env, 0, |env| -> JniResult<jboolean> {
+        if handle == 0 {
+            return Err("SessionContext handle is null".into());
+        }
+        let ctx = unsafe { &*(handle as *const SessionContext) };
+        let name: String = env.get_string(&name)?.into();
+        Ok(ctx.table_exist(&name)? as jboolean)
+    })
+}
+
+#[no_mangle]
+pub extern "system" fn 
Java_org_apache_datafusion_SessionContext_deregisterTable<'local>(
+    mut env: JNIEnv<'local>,
+    _class: JClass<'local>,
+    handle: jlong,
+    name: JString<'local>,
+) {
+    try_unwrap_or_throw(&mut env, (), |env| -> JniResult<()> {
+        if handle == 0 {
+            return Err("SessionContext handle is null".into());
+        }
+        let ctx = unsafe { &*(handle as *const SessionContext) };

Review Comment:
   Same as above. We might want inline  common in a separate function?



##########
core/src/main/java/org/apache/datafusion/SessionContext.java:
##########
@@ -601,6 +601,35 @@ private static byte[] serializeSchemaIpc(Schema schema) {
     return baos.toByteArray();
   }
 
+  /**
+   * Returns {@code true} if a table with the given name is registered in this 
session.
+   *
+   * <p>This is the Java counterpart to DataFusion's Rust {@code 
SessionContext::table_exist}.
+   *
+   * @throws IllegalStateException if this context is closed.
+   */
+  public boolean tableExists(String name) {
+    if (nativeHandle == 0) {
+      throw new IllegalStateException("SessionContext is closed");
+    }
+    return tableExists(nativeHandle, name);
+  }
+
+  /**
+   * Removes the table with the given name from this session. Does nothing if 
no table with that
+   * name is registered.
+   *
+   * <p>This is the Java counterpart to DataFusion's Rust {@code 
SessionContext::deregister_table}.
+   *
+   * @throws IllegalStateException if this context is closed.
+   */
+  public void deregisterTable(String name) {
+    if (nativeHandle == 0) {
+      throw new IllegalStateException("SessionContext is closed");

Review Comment:
   Same as above



##########
native/src/lib.rs:
##########
@@ -1042,6 +1042,41 @@ pub extern "system" fn 
Java_org_apache_datafusion_SessionContext_getOptionNative
     )
 }
 
+#[no_mangle]
+pub extern "system" fn 
Java_org_apache_datafusion_SessionContext_tableExists<'local>(
+    mut env: JNIEnv<'local>,
+    _class: JClass<'local>,
+    handle: jlong,
+    name: JString<'local>,
+) -> jboolean {
+    try_unwrap_or_throw(&mut env, 0, |env| -> JniResult<jboolean> {
+        if handle == 0 {
+            return Err("SessionContext handle is null".into());
+        }
+        let ctx = unsafe { &*(handle as *const SessionContext) };

Review Comment:
   Could you please add a comment here mentioning why it is okay to use unsafe 
? 



##########
core/src/test/java/org/apache/datafusion/SessionContextTableRegistrationTest.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datafusion;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+class SessionContextTableRegistrationTest {
+
+  @Test
+  void tableExistsReturnsFalseForUnregisteredTable() {
+    try (SessionContext ctx = new SessionContext()) {
+      assertFalse(ctx.tableExists("orders"));
+    }
+  }
+
+  @Test
+  void tableExistsReturnsTrueAfterRegisterCsv(@TempDir Path tempDir) throws 
Exception {
+    Path csv = tempDir.resolve("orders.csv");
+    Files.writeString(csv, "id,amount\n1,100\n2,200\n");
+
+    try (SessionContext ctx = new SessionContext()) {
+      assertFalse(ctx.tableExists("orders"));
+      ctx.registerCsv("orders", csv.toAbsolutePath().toString());
+      assertTrue(ctx.tableExists("orders"));
+    }
+  }
+
+  @Test
+  void deregisterTableRemovesRegisteredTable(@TempDir Path tempDir) throws 
Exception {

Review Comment:
   Thank you for adding all possible options  to test for. Perhaps we could 
also add null input behavior here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to