Skip to content

Name a rejected compression level or setting, and describe bzip2 and xz errors - #129519

Merged
PedroTadim merged 7 commits into
ClickHouse:masterfrom
groeneai:groeneai/compression-level-out-of-range-message
Nov 6, 2026
Merged

PedroTadim merged 7 commits into
ClickHouse:masterfrom
groeneai:groeneai/compression-level-out-of-range-message

Conversation

@groeneai

@groeneai groeneai commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #126287

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

A compression level that gzip, deflate, xz or a BACKUP to a zip archive does not accept (for example output_format_compression_level = 18 for a .gz file or for gzip-compressed Iceberg metadata) now fails with Compression level 18 is out of range for gzip instead of a codec library message such as deflateInit2 failed: stream error. A rejected bz2 level or zstd_window_log_max is now named in the error, for example zstd_stream_decoder init failed for zstd_window_log_max = 9: Parameter is out of bound. bz2 and xz errors describe the library status (for example Not a bzip2 stream (BZ_DATA_ERROR_MAGIC)) instead of a number. Error codes are unchanged.

Description

Item 3 of #126287. output_format_compression_level (and http_zlib_compression_level) is passed to the codec as is. zstd, lz4 and brotli clamp an out-of-range level; zlib, liblzma and bzip2 refuse it, and the error named neither the level nor the codec:

INSERT INTO FUNCTION file('t.csv.gz', 'CSV') SELECT 1 SETTINGS output_format_compression_level = 18
before: deflateInit2 failed: stream error; zlib version: 1.3.1.zlib-ng. (ZLIB_DEFLATE_FAILED)
after:  Compression level 18 is out of range for gzip. (ZLIB_DEFLATE_FAILED)

Same for .xz, .bz2, gzip Iceberg metadata, BACKUP to a zip archive with compression_method deflate/bzip2/lzma/xz (Couldn't pack zip archive: Code = -111), and a zstd read with zstd_window_log_max outside 10..31 (Parameter is out of bound).

bzip2 and liblzma have no zError, so their other statuses printed as numbers (bzip2 stream decoder failed: error code: -5 for a damaged file); they now print a description and the status name, with the encoder meaning of LZMA_DATA_ERROR/LZMA_BUF_ERROR in the xz writer. Two zstd encoder sites use ZSTD_getErrorName instead of the raw size_t, a third no longer says "decoding", and the xz reader no longer prints LZMA_STREAM_END as error code: 1.

Change: where the library's text for the "invalid value" status says nothing (Z_STREAM_ERROR is stream error, lzma_lzma_preset returns a bool, minizip-ng returns MZ_OPEN_ERROR for a level outside -1..9), the error is now Compression level N is out of range for <codec>; bzip2 and the zstd decoder name the value next to the library status. The library still decides what is valid.

Not changed: up-front validation against getCompressionLevelRange would newly reject gzip 0 and xz 0; the codec default for Iceberg metadata would silently ignore the setting. Items 1 and 2 are a separate PR.

Validation: 05508_compression_level_out_of_range covers the level, setting and damaged-input cases; it fails on master, passes with the fix, 50/50 randomized runs. Accepted levels round-trip identically on master and here.


Workflow [PR]
Sync PR [sync-upstream/pr/129519]

Version info

  • Merged into: 26.11.1.1497-master (included in 26.11 and later)

groeneai and others added 2 commits November 5, 2026 14:49
…compression level

`output_format_compression_level` (and `http_zlib_compression_level`) is passed
to the codec as is. zstd, lz4 and brotli clamp an out-of-range level, but zlib,
liblzma and bzip2 refuse it when the writer is created, and the writers turned
the library status into its own text, naming neither the level nor the codec:

    deflateInit2 failed: stream error; zlib version: 1.3.1.zlib-ng. (ZLIB_DEFLATE_FAILED)
    lzma preset failed: lzma version: 5.8.4. (LZMA_STREAM_ENCODER_FAILED)
    bzip2 stream encoder init failed: error code: -2. (BZIP2_STREAM_ENCODER_FAILED)

This is what a gzip-compressed Iceberg metadata write at a zstd-valid level
(18) reported (item 3 of ClickHouse#126287).

With the fixed arguments these writers pass (method Z_DEFLATED, windowBits
15/31, memLevel 8, default strategy; bzip2 workFactor 0, verbosity 0; a
non-null stream), Z_STREAM_ERROR from deflateInit2, a failed lzma_lzma_preset
and BZ_PARAM_ERROR from BZ2_bzCompressInit each mean exactly "the level is
invalid". Those branches now throw "Compression level N is out of range for
<codec>" with the same error codes. The library still decides validity, so no
level that works today is rejected (gzip 0 and -1, xz 0 keep working), and no
range is quoted: the accepted set depends on the codec and on the
libdeflate/zlib split in createWriteCompressedWrapper, while
getCompressionLevelRange is the narrower documented INTO OUTFILE range.

Related: ClickHouse#126287

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…of_range

`check()` grepped only the message text and dropped the client's exit status,
so the test asserted neither that the statement fails nor that the error code
is unchanged. It now matches `<message>. (<CODE>)` and prints
`Unexpected success` when the client exits 0. The fast-test tag reason no
longer lists xz, which is built unconditionally.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex) plus my own review, 2 rounds.

# Sev Finding Verdict Evidence / action
1 ⚠️ The test checked the message text but neither the failure nor the unchanged error code AGREE, fixed @ 892aa5e each arm asserts <message>. (<CODE>); a statement that succeeds prints Unexpected success
2 ⚠️ An invalid http_zlib_compression_level gives a plain-text error body labeled Content-Encoding: gzip DISAGREE (out of scope) Same on master with the old text: HTTPHandler.cpp sets the header before the writer is built. Different code from this PR, which only changes the message; the body and X-ClickHouse-Exception-Code carry the new text and the old code
3 💡 The no-fasttest reason listed xz, which the fast-test build has AGREE, fixed @ 892aa5e reason now names bzip2 and IcebergLocal only
4 💡 The message quotes no accepted range DISAGREE the accepted gzip levels depend on the build (libdeflate takes 1..12, zlib -1..9), so naming the level and the codec is the statement that is always true

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-48:20261105-152600

@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05508_compression_level_out_of_range (gz 18, deflate 18, xz 10, bz2 0, IcebergLocal CREATE with gzip metadata at 18) fails on master every time.
b Root cause explained? The level is checked only inside zlib, liblzma and bzip2 when the writer is created; the writers forwarded the library status text, which names neither the level nor the codec.
c Fix matches root cause? Yes. The message is built where the library's "invalid level" status (Z_STREAM_ERROR, failed lzma_lzma_preset, BZ_PARAM_ERROR) is detected; the library still decides validity.
d Test intent preserved / new tests added? New test with only the arms whose output changes.
e Both directions demonstrated? Master snapshot: [ FAIL ], empty output. Fix: [ OK ]; 50/50 randomized runs, 100/100 with -j 16.
f Fix is general across code paths? All three writers that refuse a level are covered. libdeflate only receives 1..12, zstd/lz4/brotli clamp, snappy has no level.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked gzip 0/1/9/12/13/18, deflate 12/18, xz 0/9/10, bz2 0/1/9/10, zstd 0/30, lz4/brotli/snappy 100, HTTP -1/18, Parquet gzip 12/18: valid levels behave as on master.
h Backward compatible? (maintainer-approved exception only) Yes. Only the text of errors that are already thrown changes; error codes, settings and accepted levels are unchanged.
i Invariants and contracts preserved? Yes. The generic error branches still handle memory/version errors, and the throw happens at the same point as before.

Session id: cron:clickhouse-impl-slot-5:20261105-143400

@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

cc @PedroTadim @rienath, could you review this and add the can be tested label so CI runs? zlib, liblzma and bzip2 report an invalid level only through their init status, so the gzip, deflate, xz and bz2 writers now turn that status into Compression level N is out of range for <codec> with the same error code (item 3 of #126287).

@clickhouse-gh

clickhouse-gh Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e868ea7]

Summary: ✅


AI Review

Summary

PR #129519 improves diagnostics for rejected compression parameters and codec failures while preserving existing error codes and accepted values. Reviewed the current diff, surrounding callers, pinned library implementations, and all prior discussion. No actionable findings remain.

Missing context / blind spots
  • ⚠️ CI for e868ea75 is still running. The added 05508_compression_level_out_of_range test is excluded from Fast test; its full-build stateless result is pending. The author reports repeated local passes, but this checkout has no built binary for an independent rerun.
Final Verdict

Status: ✅ Approve. No code changes requested; the full CI result remains pending.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 5, 2026
@PedroTadim PedroTadim self-assigned this Nov 5, 2026
Comment thread src/IO/Bzip2WriteBuffer.h Outdated

int ret = BZ2_bzCompressInit(&stream, compression_level, 0, 0);

if (ret == BZ_PARAM_ERROR)

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.

@groeneai just quickly check if we are missing any other possible error check from the compression/codec libraries/submodules that we use

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I checked every user-tunable parameter that reaches a compression library by running it (file() writes for each codec at levels up to 2^32, Parquet with both encoders, CODEC(...) arguments, BACKUP to zip, zstd window logs). Two more places returned the bare library status. Both are fixed in df49f80 the same way (the library decides, the message names the value, the error code is kept), and the description and changelog entry are updated:

  • reading a .zst file with zstd_window_log_max outside 10..31: zstd_stream_decoder init failed: Parameter is out of bound -> Setting zstd_window_log_max = 9 is out of range for zstd
  • BACKUP ... TO Disk(..., 'x.zip') with compression_method deflate/bzip2/lzma/xz and compression_level outside -1..9: Couldn't pack zip archive: Code = -111 -> Compression level 10 is out of range for deflate

The rest needs nothing: zstd, lz4 and brotli clamp the level, snappy has none, and output_format_compression_zstd_window_log, codec arguments and network_zstd_compression_level (through the codec factory) already fail with a clear message. Not touched: output_format_compression_level is UInt64 and the callers cast it to int, so a value above 2^31 wraps before any library sees it (4294967296 becomes level 0).

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Nov 5, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-codecs Compression/encoding codecs (LZ4/ZSTD/etc.), archive formats (tar/zip/7z), codec selection and IO... label Nov 5, 2026
Two more places reported an invalid user-supplied compression parameter with
the library status only:

    zstd_stream_decoder init failed: Parameter is out of bound. (ZSTD_DECODER_FAILED)
    Couldn't pack zip archive: Code = -111, filename='metadata/default/t.sql'. (CANNOT_PACK_ARCHIVE)

The first is `zstd_window_log_max` outside 10..31 (0 is the default) on a read
of a zstd file: on a fresh decoder context,
ZSTD_DCtx_setParameter(ZSTD_d_windowLogMax) fails only with
parameter_outOfBound. The second is BACKUP to a zip archive with
`compression_method` deflate, bzip2, lzma or xz and a `compression_level`
outside -1..9: minizip-ng maps -1 to the default and 0 to store, and returns
MZ_OPEN_ERROR when zlib, bzip2 or liblzma refuses the level. zstd clamps the
level and store ignores it, so neither is touched.

They now say "Setting zstd_window_log_max = 9 is out of range for zstd" and
"Compression level 10 is out of range for deflate", with the same error codes.
The library still decides, so no value that works today is rejected.

The other user-tunable parameters were checked by running them: zstd, lz4
and brotli clamp the level, snappy has none, and
`output_format_compression_zstd_window_log`, column codecs and
`network_zstd_compression_level` (codec factory) already fail with a clear
message.

Related: ClickHouse#126287

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai changed the title Name the level and the codec when gzip, deflate, xz or bz2 rejects a compression level Name the value and the codec when a compression library rejects a level or zstd_window_log_max Nov 5, 2026
Comment thread src/IO/Bzip2WriteBuffer.h Resolved
Asked in review: errors from the bzip2 and xz readers and writers printed the bare
library return code (`bzip2 stream decoder failed: error code: -5`, `lzma_stream_decoder
failed: error code: error code LZMA_FORMAT_ERROR`), since neither library has a
counterpart of zlib's zError(). Each status now has a description, worded after the
library's own command-line tool, followed by the status name:
`bzip2 stream decoder failed: Not a bzip2 stream (BZ_DATA_ERROR_MAGIC)`.

Statuses a user can reach by reading a damaged file: BZ_DATA_ERROR, BZ_DATA_ERROR_MAGIC,
LZMA_DATA_ERROR, LZMA_FORMAT_ERROR, LZMA_BUF_ERROR (truncated input) and
LZMA_MEMLIMIT_ERROR (a stream needing more than the decoder's 500 MiB limit). The
others are init or sequence failures and get the same treatment for uniformity.

The two zstd encoder sites that printed the size_t return value now use
ZSTD_getErrorName(), like the other zstd sites. zlib already uses zError(). Error codes
are unchanged. Also fixes the bzip2 reader calling itself an encoder in its messages.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai changed the title Name the value and the codec when a compression library rejects a level or zstd_window_log_max Name a rejected compression level or setting, and describe bzip2 and xz errors Nov 5, 2026
Comment thread src/IO/Bzip2WriteBuffer.h Resolved
Comment thread src/IO/ZstdInflatingReadBuffer.cpp Resolved
…sage

Asked in review: with bzip2ErrorToString() and ZSTD_getErrorName() describing the
library status, the branches special-casing BZ_PARAM_ERROR after BZ2_bzCompressInit()
and parameter_outOfBound after ZSTD_DCtx_setParameter(windowLogMax) are not needed.
Those are the only statuses the two calls can return for a user value (the other
bzip2 arguments are fixed, the decoder context is fresh), so the remaining message
now names the value instead:

  bzip2 stream encoder init failed for compression level 0: Invalid parameter (BZ_PARAM_ERROR)
  zstd_stream_decoder init failed for zstd_window_log_max = 9: Parameter is out of bound

The gzip, deflate, xz and zip branches stay: zlib describes Z_STREAM_ERROR only as
"stream error", lzma_lzma_preset() returns a bool, and minizip-ng reports the refused
level as MZ_OPEN_ERROR. Error codes are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e868ea750 with master fdec21d42 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 5fad0b448; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

14 object files changed (-44.30 KiB total), 1 added.

Object file Master PR Δ
src/CMakeFiles/clickhouse_common_io.dir/IO/LZMADeflatingWriteBuffer.cpp.o 87.62 KiB 51.21 KiB -36.41 KiB (-41.56%)
src/CMakeFiles/clickhouse_common_io.dir/IO/LZMAInflatingReadBuffer.cpp.o 96.62 KiB 63.09 KiB -33.54 KiB (-34.71%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

19 translation units recompiled, 22 s compile time in total, 18 of them have a recent master baseline.

Job report

@PedroTadim

Copy link
Copy Markdown
Member

🕵️ @groeneai Looks good. Three small things:

  1. ZipArchiveWriter: for bzip2 the valid range is 1..9, not -1..9. minizip-ng maps only -1 to 6 (mz_strm_bzip.c) and passes 0 through to BZ2_bzCompressInit, which returns BZ_PARAM_ERROR. So BACKUP ... SETTINGS compression_method = 'bzip2', compression_level = 0 still ends in Couldn't pack zip archive: Code = -111, while a .bz2 file at level 0 now names the level. The lower bound needs to depend on the method.
  2. lzmaErrorToString is shared with the encoder, but the LZMA_DATA_ERROR and LZMA_BUF_ERROR texts describe the decoder meaning. From lzma_code in LZMADeflatingWriteBuffer, they mean the output would exceed the format's size limits and that no progress is possible. "Compressed data is corrupt" and "Unexpected end of input" would mislead there. An is_encoder flag, or neutral wording, would fix it.
  3. Two messages next to the ones you fixed still have the old problems:
    • ZstdDeflatingAppendableWriteBuffer.cpp:69 says "ZSTD stream decoding failed: error code: ..." in the encoder.
    • LZMAInflatingReadBuffer.cpp:89 prints error code: 1, which is always LZMA_STREAM_END there, in "input stream has not exceeded". Something like "Trailing data after the end of the xz stream" would say what happened.

groeneai and others added 2 commits November 5, 2026 21:40
…messages

liblzma gives LZMA_DATA_ERROR and LZMA_BUF_ERROR different meanings in
encoders (the output would exceed the xz format's size limits; no progress
is possible) and decoders (corrupt data; unexpected end of input), so
lzmaErrorToString takes an is_encoder flag.

ZstdDeflatingAppendableWriteBuffer said "decoding" in the encoder, and the
xz reader printed LZMA_STREAM_END as "error code: 1" when the decoder
finishes before the input ends; it now says there is trailing data.

The zip writer comment now says why -1..9 is the refused range for every
method: minizip-ng stores an entry at level 0 (mz_zip.c), so bzip2 never
sees level 0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks. Points 2 and 3 are fixed in d689af4, test in e868ea7.

  1. bzip2 at level 0 does not reach BZ2_bzCompressInit. mz_zip_entry_write_open switches a level-0 entry to STORE before it opens the codec stream (mz_zip.c:2033), so BACKUP ... SETTINGS compression_method = 'bzip2', compression_level = 0 succeeds on master and here, with all 13 entries stored as method 0. That makes -1..9 the accepted range for all four methods, so I kept the bound and made the comment say why. 05508 now checks each of the four methods at 10 and bzip2 at -2.
  2. lzmaErrorToString takes an is_encoder flag. The xz writer now says Data exceeds the size limits of the xz format (LZMA_DATA_ERROR) and No progress is possible (LZMA_BUF_ERROR). The reader texts are unchanged.
  3. ZstdDeflatingAppendableWriteBuffer now says ZSTD stream encoding failed: <name>, and the xz reader says Trailing data after the end of the xz stream. With LZMA_CONCATENATED, that branch cannot actually be reached: liblzma returns LZMA_STREAM_END only under LZMA_FINISH once all input is consumed (stream_decoder.c:339-349). Trailing bytes are parsed as the next stream instead and end in Unexpected end of input (LZMA_BUF_ERROR) or Compressed data is corrupt (LZMA_DATA_ERROR), the same verdicts as xz -t.

@PedroTadim
PedroTadim added this pull request to the merge queue Nov 6, 2026
Merged via the queue into ClickHouse:master with commit 8ed7391 Nov 6, 2026
185 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-codecs Compression/encoding codecs (LZ4/ZSTD/etc.), archive formats (tar/zip/7z), codec selection and IO... pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants