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 7f7689b0b GH-1477: Fill the offsets of NULL rows in ClobConsumer 
(#1479)
7f7689b0b is described below

commit 7f7689b0b6712b55990f302e9042fdec7cd78d86
Author: JB Onofré <[email protected]>
AuthorDate: Mon Nov 16 06:34:53 2026 +0000

    GH-1477: Fill the offsets of NULL rows in ClobConsumer (#1479)
    
    ## What's Changed
    
    Follow-up to #1231. A NULL row in `ClobConsumer` doesn't write its end
    offset, so the next value read its start offset from that unwritten slot
    (`0`) and was written over the earlier values: rows `"AAAA", NULL, "BB"`
    read back as `"BBAA", NULL, "BB"`, with decreasing offsets.
    
    Both consumers now call `vector.fillEmpties(currentIndex)` before
    writing a value, as `setSafe()` does. It gives the NULL rows since the
    last value empty offsets, and also grows the validity and offset buffers
    when `currentIndex` is past the value capacity, as `BinaryConsumer`
    does. Before, consuming more rows than the value capacity (3970 by
    default) failed with an `IndexOutOfBoundsException`.
    
    `ClobConsumerTest` covers NULL rows before, between and after values for
    both consumers, and twice the default value capacity of rows. The three
    new tests fail without the fix.
    
    Closes #1477.
---
 .../arrow/adapter/jdbc/consumer/ClobConsumer.java  |  4 ++
 .../adapter/jdbc/consumer/ClobConsumerTest.java    | 60 ++++++++++++++++++++++
 2 files changed, 64 insertions(+)

diff --git 
a/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumer.java
 
b/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumer.java
index de2f30510..3eb45dc4c 100644
--- 
a/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumer.java
+++ 
b/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumer.java
@@ -87,6 +87,8 @@ public abstract class ClobConsumer extends 
BaseConsumer<VarCharVector> {
       Clob clob = resultSet.getClob(columnIndexInResultSet);
       if (!resultSet.wasNull()) {
         if (clob != null) {
+          // Fill the offsets of the NULL rows before this one, so it starts 
after the last value.
+          vector.fillEmpties(currentIndex);
           long length = clob.length();
 
           int read = 1;
@@ -131,6 +133,8 @@ public abstract class ClobConsumer extends 
BaseConsumer<VarCharVector> {
     public void consume(ResultSet resultSet) throws SQLException {
       Clob clob = resultSet.getClob(columnIndexInResultSet);
       if (clob != null) {
+        // Fill the offsets of the NULL rows before this one, so it starts 
after the last value.
+        vector.fillEmpties(currentIndex);
         long length = clob.length();
 
         int read = 1;
diff --git 
a/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumerTest.java
 
b/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumerTest.java
index 400881b82..b10a800a6 100644
--- 
a/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumerTest.java
+++ 
b/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumerTest.java
@@ -18,6 +18,7 @@ package org.apache.arrow.adapter.jdbc.consumer;
 
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -28,6 +29,7 @@ import java.sql.SQLException;
 import org.apache.arrow.adapter.jdbc.ResultSetUtility;
 import org.apache.arrow.vector.BaseValueVector;
 import org.apache.arrow.vector.VarCharVector;
+import org.apache.arrow.vector.util.ValueVectorUtility;
 import org.junit.jupiter.api.Test;
 
 public class ClobConsumerTest extends AbstractConsumerTest {
@@ -44,11 +46,52 @@ public class ClobConsumerTest extends AbstractConsumerTest {
     }
   }
 
+  private void assertConsumeRows(boolean nullable, String... values)
+      throws SQLException, IOException {
+    try (final VarCharVector vector = new VarCharVector("clob", allocator)) {
+      ClobConsumer consumer = ClobConsumer.createConsumer(vector, 0, nullable);
+      ClobRowResultSet resultSet = new ClobRowResultSet();
+      for (String value : values) {
+        resultSet.value = value;
+        consumer.consume(resultSet);
+      }
+      vector.setValueCount(values.length);
+      ValueVectorUtility.validateFull(vector);
+      for (int i = 0; i < values.length; i++) {
+        if (values[i] == null) {
+          assertTrue(vector.isNull(i), "row " + i);
+        } else {
+          assertArrayEquals(values[i].getBytes(StandardCharsets.UTF_8), 
vector.get(i), "row " + i);
+        }
+      }
+    }
+  }
+
   @Test
   public void testConsumeSmallClob() throws SQLException, IOException {
     assertConsume(false, repeat('a', DEFAULT_RECORD_BYTE_COUNT));
   }
 
+  @Test
+  public void testConsumeNullableClobsAfterNulls() throws SQLException, 
IOException {
+    // The values after a NULL row must be written after the previous value, 
not over it.
+    assertConsumeRows(true, null, "AAAA", null, null, "BB", null);
+  }
+
+  @Test
+  public void testConsumeNonNullableClobsAfterNullClobs() throws SQLException, 
IOException {
+    assertConsumeRows(false, null, "AAAA", null, null, "BB", null);
+  }
+
+  @Test
+  public void testConsumeMoreClobsThanValueCapacity() throws SQLException, 
IOException {
+    String[] values = new String[INITIAL_VALUE_ALLOCATION * 2];
+    for (int i = 0; i < values.length; i++) {
+      values[i] = i % 3 == 0 ? null : String.valueOf(i);
+    }
+    assertConsumeRows(true, values);
+  }
+
   @Test
   public void testConsumeClobLargerThanDataBuffer() throws SQLException, 
IOException {
     // A CLOB whose bytes exceed the VarCharVector's initial data-buffer 
capacity must grow the
@@ -92,6 +135,23 @@ public class ClobConsumerTest extends AbstractConsumerTest {
     }
   }
 
+  /**
+   * A {@link java.sql.ResultSet} whose CLOB is {@code value}, or NULL when 
{@code value} is null.
+   */
+  private static class ClobRowResultSet extends 
ResultSetUtility.ThrowingResultSet {
+    private String value;
+
+    @Override
+    public Clob getClob(int columnIndex) {
+      return value == null ? null : new StringClob(value);
+    }
+
+    @Override
+    public boolean wasNull() {
+      return value == null;
+    }
+  }
+
   /** Minimal read-only {@link Clob} backed by a {@link String}. */
   private static class StringClob implements Clob {
     private final String value;

Reply via email to