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 f8ec4be10 GH-1478: Don't split surrogate pairs between ClobConsumer 
chunks (#1480)
f8ec4be10 is described below

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

    GH-1478: Don't split surrogate pairs between ClobConsumer chunks (#1480)
    
    ## What's Changed
    
    Follow-up to #1231. `ClobConsumer` reads a CLOB in chunks of 256
    characters and encodes each chunk to UTF-8 on its own, so a character
    outside the BMP split between two chunks was encoded as `??`.
    
    - A chunk that ends with a high surrogate, while characters remain, now
    leaves it for the next chunk, so the pair is encoded together.
    - The read position advances by the length of the chunk instead of the
    requested size. The shorter chunk needs this, and it also stops skipping
    characters when a driver returns fewer than requested.
    - An empty chunk before the end of the CLOB throws an `SQLException`
    instead of looping forever.
    
    `ClobConsumerTest` covers emojis across chunk boundaries for both
    consumers. The test fails without the fix: 255 `a` followed by 😀 read
    back as 257 bytes instead of 259.
    
    This changes other lines of `ClobConsumer` than #1479, and the two
    branches merge cleanly.
    
    Closes #1478.
---
 .../arrow/adapter/jdbc/consumer/ClobConsumer.java  | 25 ++++++++++++++++++----
 .../adapter/jdbc/consumer/ClobConsumerTest.java    | 11 ++++++++++
 2 files changed, 32 insertions(+), 4 deletions(-)

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 7548f2160..de2f30510 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
@@ -57,6 +57,23 @@ public abstract class ClobConsumer extends 
BaseConsumer<VarCharVector> {
     this.currentIndex = 0;
   }
 
+  /**
+   * Reads at most {@code size} characters of the clob from {@code pos}. A 
chunk never ends with the
+   * high surrogate of a pair: it is left for the next chunk, so that the pair 
is encoded to UTF-8
+   * as one character instead of two '?'.
+   */
+  private static String readChunk(Clob clob, long pos, int size, long length) 
throws SQLException {
+    String chunk = clob.getSubString(pos, size);
+    if (chunk.isEmpty()) {
+      throw new SQLException("No characters read from clob at position " + pos 
+ " of " + length);
+    }
+    int last = chunk.length() - 1;
+    if (last > 0 && pos + last < length && 
Character.isHighSurrogate(chunk.charAt(last))) {
+      return chunk.substring(0, last);
+    }
+    return chunk;
+  }
+
   /** Nullable consumer for clob data. */
   static class NullableClobConsumer extends ClobConsumer {
 
@@ -80,7 +97,7 @@ public abstract class ClobConsumer extends 
BaseConsumer<VarCharVector> {
           ArrowBuf offsetBuffer = vector.getOffsetBuffer();
           int startIndex = offsetBuffer.getInt(currentIndex * 4L);
           while (read <= length) {
-            String str = clob.getSubString(read, readSize);
+            String str = readChunk(clob, read, readSize, length);
             byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
 
             while (dataBuffer.capacity() < startIndex + totalBytes + 
bytes.length) {
@@ -91,7 +108,7 @@ public abstract class ClobConsumer extends 
BaseConsumer<VarCharVector> {
                 bytes, 0, dataBuffer.memoryAddress() + startIndex + 
totalBytes, bytes.length);
 
             totalBytes += bytes.length;
-            read += readSize;
+            read += str.length();
           }
           offsetBuffer.setInt((currentIndex + 1) * 4L, startIndex + 
totalBytes);
           BitVectorHelper.setBit(vector.getValidityBuffer(), currentIndex);
@@ -124,7 +141,7 @@ public abstract class ClobConsumer extends 
BaseConsumer<VarCharVector> {
         ArrowBuf offsetBuffer = vector.getOffsetBuffer();
         int startIndex = offsetBuffer.getInt(currentIndex * 4L);
         while (read <= length) {
-          String str = clob.getSubString(read, readSize);
+          String str = readChunk(clob, read, readSize, length);
           byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
 
           while (dataBuffer.capacity() < startIndex + totalBytes + 
bytes.length) {
@@ -135,7 +152,7 @@ public abstract class ClobConsumer extends 
BaseConsumer<VarCharVector> {
               bytes, 0, dataBuffer.memoryAddress() + startIndex + totalBytes, 
bytes.length);
 
           totalBytes += bytes.length;
-          read += readSize;
+          read += str.length();
         }
         offsetBuffer.setInt((currentIndex + 1) * 4L, startIndex + totalBytes);
         BitVectorHelper.setBit(vector.getValidityBuffer(), currentIndex);
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 eb1081dbc..400881b82 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
@@ -56,6 +56,17 @@ public class ClobConsumerTest extends AbstractConsumerTest {
     assertConsume(false, repeat('a', INITIAL_VALUE_ALLOCATION * 
DEFAULT_RECORD_BYTE_COUNT * 4));
   }
 
+  @Test
+  public void testConsumeSurrogatePairsAcrossChunks() throws SQLException, 
IOException {
+    // The clob is read in chunks of 256 characters, so the first chunk of 
these values ends with
+    // the high surrogate of an emoji and the next one starts with its low 
surrogate.
+    String emoji = Character.toString(0x1F600);
+    for (String value : new String[] {repeat('a', 255) + emoji, "a" + 
emoji.repeat(300)}) {
+      assertConsume(true, value);
+      assertConsume(false, value);
+    }
+  }
+
   private static String repeat(char c, int count) {
     char[] chars = new char[count];
     java.util.Arrays.fill(chars, c);

Reply via email to