This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new b4812eff28 [common] Grow from one byte in MemorySliceOutput when the
segment is empty (#9523)
b4812eff28 is described below
commit b4812eff289563835276fe0654bc98be774f3543
Author: YangJie <[email protected]>
AuthorDate: Wed Sep 2 02:40:33 2026 -0400
[common] Grow from one byte in MemorySliceOutput when the segment is empty
(#9523)
---
.../apache/paimon/memory/MemorySliceOutput.java | 3 +-
.../paimon/memory/MemorySliceOutputTest.java | 59 ++++++++++++++++++++++
2 files changed, 61 insertions(+), 1 deletion(-)
diff --git
a/paimon-common/src/main/java/org/apache/paimon/memory/MemorySliceOutput.java
b/paimon-common/src/main/java/org/apache/paimon/memory/MemorySliceOutput.java
index 7254f590b5..63b1f19ec2 100644
---
a/paimon-common/src/main/java/org/apache/paimon/memory/MemorySliceOutput.java
+++
b/paimon-common/src/main/java/org/apache/paimon/memory/MemorySliceOutput.java
@@ -103,7 +103,8 @@ public class MemorySliceOutput {
return;
}
- int newCapacity = segment.size();
+ // Doubling never leaves zero, so an empty segment has to grow from
one byte.
+ int newCapacity = Math.max(segment.size(), 1);
int minNewCapacity = segment.size() + minWritableBytes;
while (newCapacity < minNewCapacity) {
newCapacity <<= 1;
diff --git
a/paimon-common/src/test/java/org/apache/paimon/memory/MemorySliceOutputTest.java
b/paimon-common/src/test/java/org/apache/paimon/memory/MemorySliceOutputTest.java
new file mode 100644
index 0000000000..d8d881d55f
--- /dev/null
+++
b/paimon-common/src/test/java/org/apache/paimon/memory/MemorySliceOutputTest.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.memory;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Test for {@link MemorySliceOutput}. */
+public class MemorySliceOutputTest {
+
+ @Test
+ public void testWriteAfterZeroInitialCapacity() throws
InterruptedException {
+ MemorySliceOutput out = new MemorySliceOutput(0);
+ AtomicReference<Throwable> failure = new AtomicReference<>();
+
+ // Growth doubled from the segment size, which never leaves zero, so
the first write
+ // spun on the CPU. JUnit's @Timeout in its default same-thread mode
only reports after
+ // the method returns, so the write runs on a daemon thread and the
test asserts that it
+ // finished; the daemon flag keeps a regression from holding the
surefire fork open.
+ Thread writer =
+ new Thread(
+ () -> {
+ try {
+ out.writeByte(5);
+ out.writeBytes(new byte[] {1, 2, 3}, 0, 3);
+ } catch (Throwable t) {
+ failure.set(t);
+ }
+ });
+ writer.setDaemon(true);
+ writer.start();
+ writer.join(10_000);
+
+ assertThat(writer.isAlive()).as("write did not terminate").isFalse();
+ assertThat(failure.get()).isNull();
+ assertThat(out.size()).isEqualTo(4);
+ assertThat(out.toSlice().readByte(0)).isEqualTo((byte) 5);
+ assertThat(out.toSlice().readByte(3)).isEqualTo((byte) 3);
+ }
+}