Skip to content

Commit f8ec4be

Browse files
authored
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.
1 parent baee368 commit f8ec4be

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumer.java‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ public void resetValueVector(VarCharVector vector) {
5757
this.currentIndex = 0;
5858
}
5959

60+
/**
61+
* Reads at most {@code size} characters of the clob from {@code pos}. A chunk never ends with the
62+
* high surrogate of a pair: it is left for the next chunk, so that the pair is encoded to UTF-8
63+
* as one character instead of two '?'.
64+
*/
65+
private static String readChunk(Clob clob, long pos, int size, long length) throws SQLException {
66+
String chunk = clob.getSubString(pos, size);
67+
if (chunk.isEmpty()) {
68+
throw new SQLException("No characters read from clob at position " + pos + " of " + length);
69+
}
70+
int last = chunk.length() - 1;
71+
if (last > 0 && pos + last < length && Character.isHighSurrogate(chunk.charAt(last))) {
72+
return chunk.substring(0, last);
73+
}
74+
return chunk;
75+
}
76+
6077
/** Nullable consumer for clob data. */
6178
static class NullableClobConsumer extends ClobConsumer {
6279

@@ -80,7 +97,7 @@ public void consume(ResultSet resultSet) throws SQLException {
8097
ArrowBuf offsetBuffer = vector.getOffsetBuffer();
8198
int startIndex = offsetBuffer.getInt(currentIndex * 4L);
8299
while (read <= length) {
83-
String str = clob.getSubString(read, readSize);
100+
String str = readChunk(clob, read, readSize, length);
84101
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
85102

86103
while (dataBuffer.capacity() < startIndex + totalBytes + bytes.length) {
@@ -91,7 +108,7 @@ public void consume(ResultSet resultSet) throws SQLException {
91108
bytes, 0, dataBuffer.memoryAddress() + startIndex + totalBytes, bytes.length);
92109

93110
totalBytes += bytes.length;
94-
read += readSize;
111+
read += str.length();
95112
}
96113
offsetBuffer.setInt((currentIndex + 1) * 4L, startIndex + totalBytes);
97114
BitVectorHelper.setBit(vector.getValidityBuffer(), currentIndex);
@@ -124,7 +141,7 @@ public void consume(ResultSet resultSet) throws SQLException {
124141
ArrowBuf offsetBuffer = vector.getOffsetBuffer();
125142
int startIndex = offsetBuffer.getInt(currentIndex * 4L);
126143
while (read <= length) {
127-
String str = clob.getSubString(read, readSize);
144+
String str = readChunk(clob, read, readSize, length);
128145
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
129146

130147
while (dataBuffer.capacity() < startIndex + totalBytes + bytes.length) {
@@ -135,7 +152,7 @@ public void consume(ResultSet resultSet) throws SQLException {
135152
bytes, 0, dataBuffer.memoryAddress() + startIndex + totalBytes, bytes.length);
136153

137154
totalBytes += bytes.length;
138-
read += readSize;
155+
read += str.length();
139156
}
140157
offsetBuffer.setInt((currentIndex + 1) * 4L, startIndex + totalBytes);
141158
BitVectorHelper.setBit(vector.getValidityBuffer(), currentIndex);

‎adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumerTest.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ public void testConsumeClobLargerThanDataBuffer() throws SQLException, IOExcepti
5656
assertConsume(false, repeat('a', INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT * 4));
5757
}
5858

59+
@Test
60+
public void testConsumeSurrogatePairsAcrossChunks() throws SQLException, IOException {
61+
// The clob is read in chunks of 256 characters, so the first chunk of these values ends with
62+
// the high surrogate of an emoji and the next one starts with its low surrogate.
63+
String emoji = Character.toString(0x1F600);
64+
for (String value : new String[] {repeat('a', 255) + emoji, "a" + emoji.repeat(300)}) {
65+
assertConsume(true, value);
66+
assertConsume(false, value);
67+
}
68+
}
69+
5970
private static String repeat(char c, int count) {
6071
char[] chars = new char[count];
6172
java.util.Arrays.fill(chars, c);

0 commit comments

Comments
 (0)