[Java][IPC] AbstractCompressionCodec.compress() writes prefix=0 for empty buffers, incompatible with C++/Python readers

未關閉 適合新手
#1,196 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
68/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
冷清
技術堆疊
java

研究方向

從 vector/src/main/java/org/apache/arrow/vector/compression/AbstractCompressionCodec.java 中的空緩衝區分支開始,將其前綴處理與文件中記載的 -1 sentinel 進行比較。使用 LZ4_FRAME 或 ZSTD,透過一個僅包含空字串的 string 欄位重現此情況,然後驗證 C++ 和 Python reader 能夠讀取產生的 IPC 串流,且不會發生解壓縮錯誤。

由索引模型根據 Issue 內容生成。

描述

Type: bug
Describe the bug

When a buffer has writerIndex == 0 (e.g., a string column where all values
are empty string ""), AbstractCompressionCodec.compress() writes an 8-byte
buffer with uncompressed_length = 0 as a "shortcut for empty buffer":

https://github.com/apache/arrow-java/blob/main/vector/src/main/java/org/apache/arrow/vector/compression/AbstractCompressionCodec.java#L32-L39

if (uncompressedBuffer.writerIndex() == 0L) {
    // shortcut for empty buffer
    compressedBuffer.setLong(0, 0);  // prefix = 0
    ...
}

This has been present since the initial implementation (ARROW-11899, 2021).

The Java decompress() handles this correctly (if size==0 return empty),
but C++ and Python Arrow readers do not recognize prefix=0. They attempt
to decompress 0 bytes of data, which fails:

  • C++ (Arrow 1.0.0 ~ latest): IOError: Lz4 compressed input contains less than one frame
  • PyArrow 21.0: same error
Reproduction

Write an Arrow IPC stream with LZ4_FRAME (or ZSTD) compression where one
string column has all values = ""
(empty string, not null). The string data
buffer has writerIndex = 0, triggering the empty buffer path.

// Writer
ArrowStreamWriter writer = new ArrowStreamWriter(root, null, channel,
    IpcOption.DEFAULT, CommonsCompressionFactory.INSTANCE, CodecType.LZ4_FRAME);

// All rows: stringVector.setSafe(i, "".getBytes());

Reading with C++ or Python fails at the first RecordBatch.

Root cause

The Arrow IPC compression format defines:

  • prefix > 0: compressed data follows, decompress to prefix bytes
  • prefix = -1: buffer stored uncompressed (sentinel)
  • prefix = 0: undefined — not in spec, not handled by C++/Python

Java writes prefix=0 for empty buffers, but only Java itself knows how to
read it back. C++/Python treat it as "0 bytes to decompress" → fail.

Suggested fix

Change the empty buffer path to use -1 sentinel (which all readers support):

if (uncompressedBuffer.writerIndex() == 0L) {
    ArrowBuf compressedBuffer = allocator.buffer(SIZE_OF_UNCOMPRESSED_LENGTH);
    compressedBuffer.setLong(0, -1L);  // Use -1 instead of 0
    compressedBuffer.writerIndex(SIZE_OF_UNCOMPRESSED_LENGTH);
    uncompressedBuffer.close();
    return compressedBuffer;
}

When a reader sees prefix=-1, it returns an empty/zero-length slice — which
is correct for an originally empty buffer.

Environment
  • Affected: All Arrow Java versions with IPC compression (1.0.0+)
  • Readers that fail: Arrow C++ (all versions), PyArrow (all versions)
  • Codec: Both LZ4_FRAME and ZSTD
Related
  • #1116 — similar symptom (prefix=0) but different root cause (race condition
    in vector reuse, not the intentional empty buffer path)
  • apache/arrow#15102 — C++ DecompressBuffer fix for prefix=-1 (does not
    handle prefix=0)
主要語言
Java
星號
95
分支
154
平均合併
2 天 16 小時
30 天內合併 PR
9

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

apache/arrow-java 的其他 Issue

查看 apache/arrow-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。