Skip to content

fix: Count parquet size statistics once when the dictionary falls back - #120027

Merged
alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
cloudquery:parquet-fallback-size-statistics
Sep 19, 2026
Merged

alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
cloudquery:parquet-fallback-size-statistics

Conversation

@erezrokah

@erezrokah erezrokah commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Related: #119300
Related: #120028

When the dictionary grows past output_format_parquet_max_dictionary_size, writeColumnImpl
discards what it has encoded and restarts the column chunk without one. It reset the offsets but not
what the discarded pass had accumulated, so the size statistics counted it twice. The reader sizes
its reads from unencoded_byte_array_data_bytes (Reader::estimateAverageStringLengthPerRow).

ParquetMetadata now also reports size_statistics per column chunk, with have_size_statistics
beside it, which is what makes the field observable at all.

column that falls back master this PR true value
toString(number), numbers(200000) 1678196 1088890 1088890
repeat(toString(number), 300), numbers(20000) 28791600 26667000 26667000
Array(Nullable(String)) repetition histogram [25854,19389] [20000,15000] [20000,15000]
the same, definition histogram [6464,19390,19389] [5000,15000,15000] [5000,15000,15000]

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed Parquet writing size statistics twice for a column chunk that falls back from dictionary encoding, which made unencoded_byte_array_data_bytes and the level histograms too large. The ParquetMetadata format now also reports size_statistics for each column chunk.

When the dictionary grows past output_format_parquet_max_dictionary_size the writer
discards everything it has encoded and restarts the column chunk without a dictionary.
It reset the offsets but not what the discarded pass had accumulated, so
unencoded_byte_array_data_bytes, the repetition and definition level histograms and
the page and chunk statistics were all counted twice. A String column of
toString(number) over numbers(200000) reported 1678196 unencoded bytes where the
values hold 1088890.

The reader uses that field to size its reads (Reader::estimateAverageStringLengthPerRow),
so an inflated value makes it over-estimate the column.

ParquetMetadata now also reports size_statistics per column chunk, which is what makes
the field observable at all.
@clickhouse-gh clickhouse-gh Bot added comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). external Author is not a member of the ClickHouse organization labels Sep 14, 2026
02718_parquet_metadata_format dumps the whole schema with SELECT *, in two places, so
adding size_statistics to the output changes its reference.
@rschu1ze rschu1ze added the can be tested Allows running workflows for external contributors label Sep 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [31299b3]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

This PR fixes Parquet dictionary-fallback accounting by clearing discarded-pass size_statistics and page/chunk statistics before re-encoding, exposes size_statistics through ParquetMetadata with an explicit have_size_statistics presence bit, and adds focused regression coverage for both unencoded_byte_array_data_bytes and the level histograms. I reviewed the current diff and prior discussion threads, and the issues previously raised by clickhouse-gh are fixed in the current head; the current Praktika report for 31299b3b is also all green.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 31299b3.

Metric Baseline Current Δ
Lines 88.40% 88.40% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 80.60% 80.60% +0.00%

Changed lines: Changed C/C++ lines covered: 57/58 (98.28%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 16, 2026
The test wrote 05176_fallback.parquet into user_files under a fixed name, so the runs the
flaky check starts in parallel truncated and read each other's file, and the test failed
with an empty file or timed out. It now writes into CLICKHOUSE_TMP under a name derived
from CLICKHOUSE_DATABASE, which is unique per run, and uses clickhouse-local rather than
the shared user_files directory.
SizeStatistics is optional in the footer, and arrow only builds one when
__isset.size_statistics is set, so a file written before it existed has none. Reporting the
tuple unconditionally made that indistinguishable from a column whose histograms are
legitimately empty, which for a nested column is wrong metadata.

ParquetMetadata now reports have_size_statistics next to the tuple, the way it already
reports have_statistics. 02718_parquet_metadata_format reads fixtures that predate the
field and now pins it as false for them.
The test only wrote a flat String column, where max_rep and max_def are 0, so the histograms
stayed empty and nothing checked that a discarded dictionary pass stops leaking into them.

It now also writes an Array(Nullable(String)), which has both kinds of level. The histograms
are filled a page at a time, so the leak only appears when the dictionary outgrows its limit
after some pages have been written, which is what the small data page size arranges. Without
the reset the column reports [25854,19389] and [6464,19390,19389] where it holds
[20000,15000] and [5000,15000,15000].
The fields were described by the shape of the column, saying the histograms are empty when
it has no levels of that kind. ParquetMetadata reads files from any writer, and one of the
fixtures in 02718_parquet_metadata_format has a flat required column whose histograms are
[5] and [5] rather than empty. What the arrays hold is whatever the footer recorded.
@alexey-milovidov alexey-milovidov self-assigned this Sep 19, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 19, 2026
Merged via the queue into ClickHouse:master with commit 3698bff Sep 19, 2026
175 of 177 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 19, 2026
alexey-milovidov pushed a commit to groeneai/ClickHouse that referenced this pull request Sep 19, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 21, 2026
Resolves one conflict in src/Processors/Formats/Impl/Parquet/Write.cpp.
Master's ClickHouse#120027 (061fa0b) independently added the same
page_statistics.clear() / total_statistics.clear() at the dictionary
fallback site, plus reset_size_statistics(); its side is a strict superset
of the branch's hunk, so master's version is taken verbatim and this
branch no longer carries that hunk.

KeyCondition.cpp/h and Reader.cpp auto-merged. Verified by hand that the
bounds_may_hide_nan check still sits after master's NaN-bound handling in
the restructured FUNCTION_IN_RANGE arm, where key_column is still
element.getKeyColumn(), so the mask stays indexed by the source key
column position it is built for.
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-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default 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.

4 participants