Skip to content

[SPARK-57383][SQL][PYTHON][4.1] Honor configured Arrow zstd compression level when writing Arrow batches - #56451

Closed
viirya wants to merge 1 commit into
apache:branch-4.1from
viirya:SPARK-57383-branch-4.1
Closed

[SPARK-57383][SQL][PYTHON][4.1] Honor configured Arrow zstd compression level when writing Arrow batches#56451
viirya wants to merge 1 commit into
apache:branch-4.1from
viirya:SPARK-57383-branch-4.1

Conversation

@viirya

@viirya viirya commented Jun 11, 2026

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

Backport of #56444 (commit e33017a) to branch-4.1.

This fixes a bug where the zstd compression level configured via spark.sql.execution.arrow.compression.zstd.level was silently ignored everywhere Arrow batches are compressed. The affected code constructed new ZstdCompressionCodec(level) only to read its codec type, then rebuilt the codec through CompressionCodec.Factory.INSTANCE.createCodec(codecType). The codec type enum does not carry a level, so that single-argument factory overload always builds a codec at the zstd default level (3), dropping the configured one.

Compared to the master patch, this backport fixes one additional copy of the broken pattern: on branch-4.1 GroupedPythonArrowInput still has its own codec construction in createUnloaderForGroup (the SPARK-55328 deduplication that makes it reuse PythonArrowInput.codec is master/4.2-only). The four fixed sites are:

  • ArrowConverters.ArrowBatchIterator
  • PythonArrowInput
  • GroupedPythonArrowInput (branch-4.1 only; folded into PythonArrowInput on master)
  • CoGroupedArrowPythonRunner

All four now use the shared ArrowCompressionUtils.createCompressionCodec helper, which constructs the level-carrying codec instance directly. The level only matters on the write side; the read side looks up the codec by the type recorded in the IPC message, so reads are unaffected and the on-wire format is unchanged.

Why are the changes needed?

Users tuning spark.sql.execution.arrow.compression.zstd.level for Python UDF exchange or df.toArrow() got no effect at all: every level compressed identically at the default level 3, with no error or warning. Released 4.1.0/4.1.1/4.1.2 are affected.

Does this PR introduce any user-facing change?

Yes. The configured zstd level now actually takes effect; previously all levels behaved like the default level 3.

How was this patch tested?

ArrowCompressionUtilsSuite, brought over with the backport. The regression test compresses the same compressible-but-varying batch at zstd level -5 and level 19 and asserts level 19 produces a strictly smaller payload; against the old codec construction it fails with byte-identical sizes at both levels. Verified the suite passes on this branch.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code

…vel when writing Arrow batches

This PR fixes a bug where the zstd compression level configured via `spark.sql.execution.arrow.compression.zstd.level` was silently ignored everywhere Arrow batches are compressed. Three places shared the same broken pattern:

- `ArrowConverters.ArrowBatchIterator` (SPARK-54134)
- `PythonArrowInput` (SPARK-54226; also covers `GroupedPythonArrowInput`, which reuses this codec via SPARK-55328)
- `CoGroupedArrowPythonRunner` (SPARK-54226)

They constructed `new ZstdCompressionCodec(level)` only to read its codec type, then rebuilt the codec through `CompressionCodec.Factory.INSTANCE.createCodec(codecType)`. The codec type enum does not carry a level, so that single-argument factory overload always builds a codec at the zstd default level (3), dropping the configured one.

The codec construction is extracted into a shared `ArrowCompressionUtils.createCompressionCodec` helper that constructs the level-carrying codec instance directly (the helper lives in `sql/core` because `sql/api`, where `ArrowUtils` is, has no `arrow-compression` dependency). The level only matters on the write side; the read side looks up the codec by the type recorded in the IPC message, so reads are unaffected and the on-wire format is unchanged.

The same bug class was found by dbtsai during review of apache#56334 (apache#56334 (comment)); that PR fixes the cache-side instance of the pattern, and this PR fixes the remaining three pre-existing instances.

Users tuning `spark.sql.execution.arrow.compression.zstd.level` for Python UDF exchange or `df.toArrow()` got no effect at all: every level compressed identically at the default level 3, with no error or warning.

Yes. The configured zstd level now actually takes effect; previously all levels behaved like the default level 3. The bug exists in released Spark 4.1.0/4.1.1/4.1.2 (SPARK-54134 and SPARK-54226 were backported to branch-4.1) as well as 4.2.0 RCs and master, so this fix is a candidate for backporting to branch-4.1 and branch-4.2. Note that a branch-4.1 backport needs to fix a fourth copy of the pattern: there `GroupedPythonArrowInput` still has its own codec construction, since the SPARK-55328 deduplication is master-only.

New `ArrowCompressionUtilsSuite`. The regression test compresses the same compressible-but-varying batch at zstd level -5 and level 19 and asserts level 19 produces a strictly smaller payload. Against the old codec construction this test fails with byte-identical sizes at both levels (verified locally). A second test covers the `none` codec and the unsupported-codec error.

Generated-by: Claude Code

Closes apache#56444 from viirya/fix-arrow-zstd-level.

Authored-by: Liang-Chi Hsieh <viirya@gmail.com>
Signed-off-by: Liang-Chi Hsieh <viirya@gmail.com>
(cherry picked from commit e33017a)
@viirya
viirya requested a review from zhengruifeng June 11, 2026 16:08

@dongjoon-hyun dongjoon-hyun 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.

+1, LGTM (Pending CIs).

viirya added a commit that referenced this pull request Jun 11, 2026
…on level when writing Arrow batches

### What changes were proposed in this pull request?

Backport of #56444 (commit e33017a) to branch-4.1.

This fixes a bug where the zstd compression level configured via `spark.sql.execution.arrow.compression.zstd.level` was silently ignored everywhere Arrow batches are compressed. The affected code constructed `new ZstdCompressionCodec(level)` only to read its codec type, then rebuilt the codec through `CompressionCodec.Factory.INSTANCE.createCodec(codecType)`. The codec type enum does not carry a level, so that single-argument factory overload always builds a codec at the zstd default level (3), dropping the configured one.

Compared to the master patch, this backport fixes one additional copy of the broken pattern: on branch-4.1 `GroupedPythonArrowInput` still has its own codec construction in `createUnloaderForGroup` (the SPARK-55328 deduplication that makes it reuse `PythonArrowInput.codec` is master/4.2-only). The four fixed sites are:

- `ArrowConverters.ArrowBatchIterator`
- `PythonArrowInput`
- `GroupedPythonArrowInput` (branch-4.1 only; folded into `PythonArrowInput` on master)
- `CoGroupedArrowPythonRunner`

All four now use the shared `ArrowCompressionUtils.createCompressionCodec` helper, which constructs the level-carrying codec instance directly. The level only matters on the write side; the read side looks up the codec by the type recorded in the IPC message, so reads are unaffected and the on-wire format is unchanged.

### Why are the changes needed?

Users tuning `spark.sql.execution.arrow.compression.zstd.level` for Python UDF exchange or `df.toArrow()` got no effect at all: every level compressed identically at the default level 3, with no error or warning. Released 4.1.0/4.1.1/4.1.2 are affected.

### Does this PR introduce _any_ user-facing change?

Yes. The configured zstd level now actually takes effect; previously all levels behaved like the default level 3.

### How was this patch tested?

`ArrowCompressionUtilsSuite`, brought over with the backport. The regression test compresses the same compressible-but-varying batch at zstd level -5 and level 19 and asserts level 19 produces a strictly smaller payload; against the old codec construction it fails with byte-identical sizes at both levels. Verified the suite passes on this branch.

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code

Closes #56451 from viirya/SPARK-57383-branch-4.1.

Authored-by: Liang-Chi Hsieh <viirya@gmail.com>
Signed-off-by: Liang-Chi Hsieh <viirya@gmail.com>
@viirya viirya closed this Jun 11, 2026
@viirya

viirya commented Jun 11, 2026

Copy link
Copy Markdown
Member Author

Merged to branch-4.1.

Thanks @dongjoon-hyun

@viirya
viirya deleted the SPARK-57383-branch-4.1 branch June 11, 2026 19:43
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.

2 participants