This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-java.git


The following commit(s) were added to refs/heads/main by this push:
     new 1c5716066 GH-1458: [JDBC] Close parameter VectorSchemaRoot if 
setRowCount or setParameters fails (#1459)
1c5716066 is described below

commit 1c571606649af3065d3352a8499d7ab811ac9670
Author: JB Onofré <[email protected]>
AuthorDate: Thu Nov 12 05:29:58 2026 +0000

    GH-1458: [JDBC] Close parameter VectorSchemaRoot if setRowCount or 
setParameters fails (#1459)
    
    ## What's Changed
    
    Follow-up to #1441 (#1439).
    
    Expand the `try` block in `AvaticaParameterBinder.bind(List<TypedValue>,
    int)` to cover the initial parameter-count check as well as
    `parameters.setRowCount(index + 1)` and
    `preparedStatement.setParameters(parameters)` (up to
    `parametersHandedOff = true`), so that any `RuntimeException` thrown
    before ownership is handed off to the prepared statement closes
    `parameters`:
    
    - `parameters.setRowCount(index + 1)` calls `setValueCount` on each
    vector, which can still allocate buffers (for example, child vector
    buffers of a `FixedSizeListVector` when `null` was bound) and throw
    `OutOfMemoryException`.
    - `preparedStatement.setParameters(parameters)` can also throw before
    `parametersHandedOff` is set to `true`.
    
    Added unit tests in `AvaticaParameterBinderTest` covering both
    `setRowCount` (`OutOfMemoryException`) and `setParameters`
    (`IllegalStateException`) failures.
    
    Closes #1458.
---
 .../driver/jdbc/utils/AvaticaParameterBinder.java  | 26 +++++-----
 .../jdbc/utils/AvaticaParameterBinderTest.java     | 57 +++++++++++++++++++++-
 2 files changed, 68 insertions(+), 15 deletions(-)

diff --git 
a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinder.java
 
b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinder.java
index e2a05c3c4..02e5c4014 100644
--- 
a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinder.java
+++ 
b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinder.java
@@ -92,17 +92,23 @@ public class AvaticaParameterBinder {
    * @param index index for parameter.
    */
   public void bind(List<TypedValue> typedValues, int index) {
-    if (preparedStatement.getParameterSchema().getFields().size() != 
typedValues.size()) {
-      throw new IllegalStateException(
-          String.format(
-              "Prepared statement has %s parameters, but only received %s",
-              preparedStatement.getParameterSchema().getFields().size(), 
typedValues.size()));
-    }
-
     try {
+      if (preparedStatement.getParameterSchema().getFields().size() != 
typedValues.size()) {
+        throw new IllegalStateException(
+            String.format(
+                "Prepared statement has %s parameters, but only received %s",
+                preparedStatement.getParameterSchema().getFields().size(), 
typedValues.size()));
+      }
+
       for (int i = 0; i < typedValues.size(); i++) {
         bind(parameters.getVector(i), typedValues.get(i), index);
       }
+
+      if (!typedValues.isEmpty()) {
+        parameters.setRowCount(index + 1);
+        preparedStatement.setParameters(parameters);
+        parametersHandedOff = true;
+      }
     } catch (RuntimeException e) {
       // Until the root is handed to the prepared statement, nothing else owns 
it, so release the
       // memory held by any values bound before the failure.
@@ -111,12 +117,6 @@ public class AvaticaParameterBinder {
       }
       throw e;
     }
-
-    if (!typedValues.isEmpty()) {
-      parameters.setRowCount(index + 1);
-      preparedStatement.setParameters(parameters);
-      parametersHandedOff = true;
-    }
   }
 
   /**
diff --git 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinderTest.java
 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinderTest.java
index 9375d460a..f9f1b6fd9 100644
--- 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinderTest.java
+++ 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinderTest.java
@@ -25,6 +25,7 @@ import java.util.Collections;
 import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler;
 import org.apache.arrow.flight.FlightInfo;
 import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.OutOfMemoryException;
 import org.apache.arrow.memory.RootAllocator;
 import org.apache.arrow.vector.VectorSchemaRoot;
 import org.apache.arrow.vector.types.pojo.ArrowType;
@@ -114,11 +115,63 @@ public class AvaticaParameterBinderTest {
     assertEquals(0, allocator.getAllocatedMemory());
   }
 
+  @Test
+  public void testFailedSetRowCountReleasesBoundValues() {
+    Schema nullableFixedSizeListSchema =
+        new Schema(
+            Collections.singletonList(
+                new Field(
+                    "list",
+                    FieldType.nullable(new ArrowType.FixedSizeList(4)),
+                    Collections.singletonList(
+                        new Field(
+                            "item", FieldType.nullable(new ArrowType.Int(64, 
true)), null)))));
+    // Binding null to a FixedSizeListVector only allocates its validity 
buffer (512 bytes) during
+    // bind(), while setRowCount(1) allocates the child vector's buffers (32 
KiB).
+    try (BufferAllocator cappedAllocator = 
allocator.newChildAllocator("capped", 0, 1024);
+        StubPreparedStatement statement = new 
StubPreparedStatement(nullableFixedSizeListSchema)) {
+      AvaticaParameterBinder binder = new AvaticaParameterBinder(statement, 
cappedAllocator);
+      assertThrows(OutOfMemoryException.class, () -> 
binder.bind(Collections.singletonList(null)));
+      assertEquals(0, cappedAllocator.getAllocatedMemory());
+    }
+    assertEquals(0, allocator.getAllocatedMemory());
+  }
+
+  @Test
+  public void testFailedSetParametersReleasesBoundValues() {
+    try (StubPreparedStatement statement =
+        new StubPreparedStatement(PARAMETER_SCHEMA) {
+          @Override
+          public void setParameters(VectorSchemaRoot parameters) {
+            throw new IllegalStateException("Statement closed");
+          }
+        }) {
+      AvaticaParameterBinder binder = new AvaticaParameterBinder(statement, 
allocator);
+      assertThrows(
+          IllegalStateException.class,
+          () ->
+              binder.bind(
+                  Arrays.asList(
+                      TypedValue.ofLocal(ColumnMetaData.Rep.STRING, "text"),
+                      TypedValue.ofLocal(ColumnMetaData.Rep.LONG, 42L))));
+    }
+    assertEquals(0, allocator.getAllocatedMemory());
+  }
+
   /** A prepared statement that owns the parameters it is given, like the 
Flight SQL client. */
-  private static final class StubPreparedStatement
+  private static class StubPreparedStatement
       implements ArrowFlightSqlClientHandler.PreparedStatement {
+    private final Schema parameterSchema;
     private VectorSchemaRoot parameters;
 
+    StubPreparedStatement() {
+      this(PARAMETER_SCHEMA);
+    }
+
+    StubPreparedStatement(Schema parameterSchema) {
+      this.parameterSchema = parameterSchema;
+    }
+
     @Override
     public FlightInfo executeQuery() {
       throw new UnsupportedOperationException();
@@ -141,7 +194,7 @@ public class AvaticaParameterBinderTest {
 
     @Override
     public Schema getParameterSchema() {
-      return PARAMETER_SCHEMA;
+      return parameterSchema;
     }
 
     @Override

Reply via email to