SQLException
if it is not. The index is out of bounds
+ * if it is less than 1
or greater than the number of
+ * columns in this rowset.
+ *
+ * This method is called internally by the getXXX
and
+ * updateXXX
methods.
+ *
+ * @param idx the number of a column, must be between 1
+ * and the number of rows in this rowset
+ * @throws SQLException if the given index is out of bounds
+ */
+ private void checkColumnIndex(int idx) throws SQLException {
+ if (idx < 1 || idx > getColumnCount()) {
+ throw new SQLException("Column index " + idx + " is out of bound[1, " + getColumnCount() +"]");
}
+ }
- return records.get(cursor);
+ private int getColumnCount() {
+ return batch.getSchema().getFields().size();
}
@Override
public String getString(int columnIndex) throws SQLException {
- return getValue(columnIndex);
+ checkColumnIndex(columnIndex);
+ return batch.getVector(columnIndex - 1).getObject(batchCursor).toString();
}
@Override
@@ -75,12 +110,22 @@ public short getShort(int i) throws SQLException {
@Override
public int getInt(int columnIndex) throws SQLException {
- return Integer.parseInt(getValue(columnIndex));
+ checkColumnIndex(columnIndex);
+ FieldVector vector = batch.getVector(columnIndex - 1);
+ if (vector instanceof IntVector) {
+ return ((IntVector) vector).get(batchCursor);
+ }
+ throw new SQLException("Column [" + vector.getField() + "] is not int");
}
@Override
- public long getLong(int i) throws SQLException {
- throw new SQLException("This method has not been implemented yet.");
+ public long getLong(int columnIndex) throws SQLException {
+ checkColumnIndex(columnIndex);
+ FieldVector vector = batch.getVector(columnIndex);
+ if (vector instanceof BigIntVector) {
+ return ((BigIntVector) vector).get(batchCursor);
+ }
+ throw new SQLException("Column [" + vector.getField() + "] is not long");
}
@Override
@@ -135,7 +180,11 @@ public InputStream getBinaryStream(int i) throws SQLException {
@Override
public void close() throws SQLException {
- // No resources to close
+ try {
+ AutoCloseables.close(batch, arrowStreamReader);
+ } catch (Exception e) {
+ throw new SQLException(e);
+ }
}
@Override
diff --git a/src/main/java/org/chdb/jdbc/ChdbStatement.java b/src/main/java/org/chdb/jdbc/ChdbStatement.java
index 9cff3a0..f0388b5 100644
--- a/src/main/java/org/chdb/jdbc/ChdbStatement.java
+++ b/src/main/java/org/chdb/jdbc/ChdbStatement.java
@@ -12,7 +12,7 @@ public ChdbStatement(ChdbConnection connection) {
@Override
public ResultSet executeQuery(String sql) throws SQLException {
- LocalResultV2 result = ChdbJniUtil.executeQuery(sql);
+ LocalResultV2 result = ChdbJniUtil.executeQuery(sql, "ArrowStream");
System.out.println("sql: " + sql);
try {
return new ChdbResultSet(result);
diff --git a/src/main/java/org/chdb/jdbc/memory/ArrowMemoryManger.java b/src/main/java/org/chdb/jdbc/memory/ArrowMemoryManger.java
new file mode 100644
index 0000000..43af7b3
--- /dev/null
+++ b/src/main/java/org/chdb/jdbc/memory/ArrowMemoryManger.java
@@ -0,0 +1,7 @@
+package org.chdb.jdbc.memory;
+
+import org.apache.arrow.memory.RootAllocator;
+
+public class ArrowMemoryManger {
+ public static RootAllocator ROOT_ALLOCATOR = new RootAllocator();
+}
diff --git a/src/test/java/ChdbJdbcTest.java b/src/test/java/ChdbJdbcTest.java
index 48f3f86..658d985 100644
--- a/src/test/java/ChdbJdbcTest.java
+++ b/src/test/java/ChdbJdbcTest.java
@@ -1,4 +1,4 @@
-import org.junit.jupiter.api.Test;
+import org.chdb.jdbc.memory.ArrowMemoryManger;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -7,8 +7,10 @@
import static org.junit.jupiter.api.Assertions.*;
-public class ChdbJdbcTest {
+public class ChdbJdbcTest {
+ // TODO failed to run by junit, it will crash
+ // Note: adding --add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED
public static void main(String[] args) throws Exception {
// Load the JDBC driver
Class.forName("org.chdb.jdbc.ChdbDriver");
@@ -31,5 +33,6 @@ public static void main(String[] args) throws Exception {
rs.close();
conn.close();
+ assertEquals(0, ArrowMemoryManger.ROOT_ALLOCATOR.getAllocatedMemory());
}
}