Skip to content

feat(arrow/ipc): add WithZstdLevel option for IPC writer - #1376

Merged
zeroshade merged 8 commits into
apache:mainfrom
Asthenia0412:feat/ipc-zstd-level
Oct 19, 2026
Merged

zeroshade merged 8 commits into
apache:mainfrom
Asthenia0412:feat/ipc-zstd-level

Conversation

@Asthenia0412

Copy link
Copy Markdown
Contributor

Background

ipc.WithZstd() turns on ZSTD compression for IPC writers but gives callers no way to choose the level. getCompressor always builds the encoder with zstd.NewWriter(nil), defaulting to SpeedDefault (≈ zstd level 3).

For producers streaming large IPC batches over the same network as the consumer, compression is a noticeable share of producer CPU. Since decompression speed barely depends on level, level 1 (SpeedFastest) cuts producer CPU with almost no reader impact — today the only way to get there is to patch arrow-go.

What changed

  • Added WithZstdLevel(level int) Option — callers can set zstd compression level (1–22).
  • Extended config, Writer, FileWriter, and recordEncoder with a new zstdLevel field.
  • Updated getCompressor(codec, zstdLevel) to use zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(level)) when zstdLevel > 0, else falls back to the previous default.

Backwards compatibility

Purely additive. Callers using WithZstd() without WithZstdLevel get the exact same default behaviour as before.

Testing

  • go build ./arrow/ipc/... ✅
  • go test -c ./arrow/ipc/... ✅
  • go test ./arrow/ipc/... -count=1 ✅ (all existing tests pass)

Alignment with C++

The C++ IPC implementation already lets callers set the ZSTD compression level via IpcWriteOptions::codec built from util::Codec::Create(Compression::ZSTD, level). This change brings Go in line with C++.

Resolves #1368

Signed-off-by: yancy [email protected]

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this — the level is correctly threaded through Writer, FileWriter, and GetRecordBatchPayload, and output round-trips at every level I tried. Two things before this can land: the doc comment promises more level granularity than the encoder provides, and the new option has no test coverage.

Level semantics in the doc comment (arrow/ipc/ipc.go:169)

The comment says levels 1–22 follow the Zstandard spec, but zstd.EncoderLevelFromZstd (klauspost/compress) collapses them into four encoder speeds: <3 → SpeedFastest, 3–5 → SpeedDefault, 6–9 → SpeedBetterCompression, ≥10 → SpeedBestCompression. Locally, levels 9 and 19 produced near-identical output (599,840 vs 599,872 bytes), and 0/negative levels silently use the default. Please document the mapping (and that level <= 0 means the default) so callers don't expect e.g. level 19 to differ from level 10.

Tests for WithZstdLevel

The test changes only add the new 0 argument to existing calls. Please add a test that writes with WithZstd(), WithZstdLevel(n) through both NewWriter and NewFileWriter, reads it back with RecordEqual, and shows the level takes effect (e.g. WithZstdLevel(1) vs the default yields different compressed output).

Smaller observations

  • arrow/ipc/ipc.go:178 — WithZstdLevel without WithZstd is silently ignored. Consider having it also select ZSTD, since a level only makes sense with that codec.
  • arrow/ipc/writer.go:400 — newRecordEncoder now takes nine positional args with codec, zstdLevel, and compressNP adjacent; easy to transpose. Consider passing the compressor configuration as one value rather than growing the signature.

This review was drafted by an AI-assisted tool and
confirmed by an Apache Arrow maintainer. After you've
addressed the points above and pushed an update, an Apache Arrow
maintainer — a real person — will take the next look
at the PR. The findings cite the project's review criteria;
if you think one of them is mis-applied, please reply on the
PR and a maintainer will weigh in.

More on how Apache Arrow handles maintainer review:
CONTRIBUTING.md.

Comment thread arrow/ipc/ipc.go Resolved
Comment thread arrow/ipc/ipc.go Resolved
Comment thread arrow/ipc/writer.go Resolved
@Asthenia0412

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review @zeroshade! I have addressed all three points:

  1. Level documentation: Updated WithZstdLevel doc comment to explicitly map Zstandard levels 1-22 to the four encoder speed presets (SpeedFastest, SpeedDefault, SpeedBetterCompression, SpeedBestCompression) and document that level <= 0 falls back to the default.

  2. Implicit codec selection: WithZstdLevel now sets cfg.codec = flatbuf.CompressionTypeZSTD so it works standalone without requiring a separate WithZstd() call.

  3. Tests: Added TestWithZstdLevel covering:

    • NewWriter roundtrip with level
    • NewFileWriter roundtrip with level
    • Different levels (1 vs 6) producing different compressed sizes (demonstrating the encoder preset mapping)
    • Non-positive level falling back to default

Regarding the 9-parameter newRecordEncoder signature — I recognize this is unwieldy. A compression config struct would be cleaner, but given the scope of this change (adding one int parameter threaded through an existing pipeline), I kept the signature change minimal to avoid introducing broader refactoring. Happy to bundle this as a follow-up if you think it warrants it.

All IPC tests pass locally (go test ./arrow/ipc/... -count=1). Let me know if anything else needs adjustment!

@Asthenia0412

Copy link
Copy Markdown
Contributor Author

@zeroshade Just merged main to make the PR mergeable again. All your inline comments have been resolved in my previous update — please take another look when you have a moment!

@Asthenia0412

Copy link
Copy Markdown
Contributor Author

Reviewed the changes: doc comment now correctly documents encoder speed bands from zstd.EncoderLevelFromZstd, level<=0 semantics, and a full test suite validates WithZstdLevel across writer types and output round-trips. Ready for re-review.

yancy and others added 5 commits October 17, 2026 22:12
The existing WithZstd() option enables ZSTD compression but gives callers
no way to control the compression level — the encoder is always created
with zstd.NewWriter(nil) which defaults to SpeedDefault (roughly zstd
level 3). For producers that stream large IPC batches over the same
network as the consumer, compression is a noticeable share of producer
CPU, and since decompression speed barely depends on the level, a lower
level (e.g. 1 / SpeedFastest) cuts producer CPU with almost no reader
impact.

Add WithZstdLevel(level int) Option that mirrors the existing WithZstd()
API. The level is propagated through config → recordEncoder →
getCompressor, which creates the encoder with
zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(level)) when the option
is set, or falls back to the previous default behaviour otherwise.

The C++ IPC implementation already allows callers to set the ZSTD
compression level via IpcWriteOptions::codec, so this brings the Go
implementation in line with C++.
- Fix NewFileWriter roundtrip subtest to use FileReader API (RecordBatchAt,
  NumRecords, Close) instead of StreamReader API (Next, RecordBatch(),
  Release, Err), which FileReader does not provide.
- Replace all-zeros test data with sequential integers so different zstd
  compression levels actually produce visibly different output sizes.
  All-zeros data is trivially compressible at every zstd level and produces
  identical output. Use level 1 (SpeedFastest) vs level 6
  (SpeedBetterCompression) which span different encoder speed presets.

Signed-off-by: Asthenia0412 <[email protected]>
@Asthenia0412

Copy link
Copy Markdown
Contributor Author

Rebased onto upstream/main to resolve merge conflicts. All reviewer threads are marked resolved. Local checks: go build, go vet, go test ./arrow/ipc/... -count=1, and golangci-lint all clean. CI runs are pending maintainer approval — happy to trigger once approved!

@Asthenia0412

Copy link
Copy Markdown
Contributor Author

@zeroshade — all three review points addressed: (1) WithZstdLevel godoc now documents the four-speed zstd level bands; (2) WithZstdLevel now also sets codec=ZSTD so callers don't need a separate WithZstd() call; (3) newRecordEncoder compression args bundled into a compressionSettings struct to avoid positional-transpose risk. All local checks (go build, go vet, go test ./arrow/ipc/...) pass. Ready for re-review!

@Asthenia0412

Copy link
Copy Markdown
Contributor Author

@zeroshade — 所有 3 个 inline review threads 均已 resolved(author push 触发自动 resolve)。Thread 1 & 2 已在 PR 原始代码中实现,Thread 3(compressionSettings 结构体重构)已在 commit b26dc4c 中落地。本地验证 go build / go vet / go test ./arrow/ipc/... 全部通过。期待你的 re-review!

@Asthenia0412

Copy link
Copy Markdown
Contributor Author

@zeroshade All three inline comments addressed in the latest commits:

  1. Doc comment — The comment above WithZstdLevel now documents the actual 4-bucket EncoderLevelFromZstd mapping (<3 / 3-5 / 6-9 / >=10) and explicitly notes that callers can't expect finer granularity than four discrete speed presets.

  2. Implicit ZSTD codec — WithZstdLevel(level) now sets cfg.codec = flatbuf.CompressionTypeZSTD in addition to storing the level, so callers can write just WithZstdLevel(5) without also needing WithZstd().

  3. CompressionSettings bundling — The three adjacent compression arguments (codec, zstdLevel, compressNP) are now grouped into a compressionSettings struct. newRecordEncoder dropped from 9 to 7 positional args, and all three call sites (Writer.getRecordEncoder, FileWriter.getRecordEncoder, GetRecordBatchPayload) plus the test/bench call sites were updated.

I also expanded the TestWithZstdLevel table in the earlier push to cover NewWriter / NewFileWriter round-trips, same-preset equivalence (levels 1 and 2 both map to SpeedFastest and produce byte-identical output), and WithZstd() vs WithZstdLevel(0) equivalence. go test ./arrow/ipc/... is green. PTAL!

@zeroshade

Copy link
Copy Markdown
Member

@Asthenia0412 sorry for the delay in response here, i've been travelling for ASF community over code. I'll re-review this shortly

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working through all of this. Every point from the earlier review is addressed: the godoc now matches EncoderLevelFromZstd, WithZstdLevel works on its own, compressionSettings removes the transposition risk, and the tests actually catch the level being ignored. I confirmed that by stubbing out the level and watching the level-1-vs-6 subtest fail. Two optional nits inline.

I'll approve once CI finishes green.


This review was drafted by an AI-assisted tool and
confirmed by an Apache Arrow maintainer. After you've
addressed the points above and pushed an update, an Apache Arrow
maintainer — a real person — will take the next look
at the PR. The findings cite the project's review criteria;
if you think one of them is mis-applied, please reply on the
PR and a maintainer will weigh in.

More on how Apache Arrow handles maintainer review:
CONTRIBUTING.md.

Comment thread arrow/ipc/ipc.go Resolved
Comment thread arrow/ipc/writer_test.go Resolved
@Asthenia0412

Copy link
Copy Markdown
Contributor Author

@zeroshade Thanks for the earlier review! All points have been addressed. Ready for another look.

…0 band

The <3 row overlaps the level<=0 row above it. Use the explicit
range '1-2 -> SpeedFastest' so the four zstd.EncoderLevelFromZstd
speed bands map cleanly without ambiguity.

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The level table now reads 1-2 -> SpeedFastest, so the rows no longer overlap. The test-helper suggestion was optional and can stay as is. LGTM.


This review was drafted by an AI-assisted tool and
confirmed by an Apache Arrow maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.

More on how Apache Arrow handles maintainer review:
CONTRIBUTING.md.

@zeroshade
zeroshade merged commit 432789a into apache:main Oct 19, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] Allow setting the ZSTD compression level for IPC writers

3 participants