Skip to content

Ignore inverted Parquet statistics instead of failing the query - #128336

Merged
fm4v merged 4 commits into
ClickHouse:masterfrom
groeneai:parquet-inverted-statistics-ignored
Nov 1, 2026
Merged

fm4v merged 4 commits into
ClickHouse:masterfrom
groeneai:parquet-inverted-statistics-ignored

Conversation

@groeneai

@groeneai groeneai commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #128302
Related: #128305

Changelog category (leave one):

  • Improvement

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

The native Parquet reader now ignores column chunk and page statistics whose min_value is greater than max_value (for example the UInt32 / UInt64 statistics that polars 0.20 writes in signed order) instead of failing the query with INCORRECT_DATA, so such files can be filtered and aggregated. Closes #128302.

Description

polars 0.20 writes UINT_32 / UINT_64 statistics in signed order while declaring TYPE_ORDER, so a row group or page with values on both sides of 2^31 / 2^63 has min_value > max_value. The native reader rejected the query with INCORRECT_DATA at four places: row group pruning, page pruning by the column index, the TopN row group shortcut and optimize_min_max_from_files; the only workaround was disabling the optimization for the whole query.

Reader::adjustRangeFromIndexIfNeeded now returns false for an inverted range instead of throwing, and each caller takes the fallback it already uses for other unusable statistics: no pruning for that row group or page, no TopN shortcut, reading the data for min / max. Integer statistics are also checked in their stored order before decoding: read as DateTime, the minimum 2^63 decodes to no bound, the decoded range [-inf, 1] no longer looked inverted, and row group and page pruning silently dropped the row (also on master). Nothing that was read before is pruned now.

It is enough for the polars files: if all values of a chunk share the top bit, signed and unsigned order agree; otherwise the signed minimum has the top bit set and the signed maximum does not, so the unsigned range is inverted and ignored.

Validation: the new test writes a two-row-group file and swaps min_value / max_value of the first row group in the footer statistics and the column index (the polars bytes). On master all four paths fail with INCORRECT_DATA for UInt64 and UInt32 and the DateTime reads return nothing; with the fix the results are correct and the second row group is still pruned by its valid statistics. The 9 polars 0.20.31 rows of 05474_parquet_corpus_writers.reference (#128305) that recorded INCORRECT_DATA now return the count the writer reads back.

🤖 Generated with Claude Code


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

Version info

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

polars 0.20 writes the statistics of UINT_32 / UINT_64 columns in signed
order while declaring TYPE_ORDER, so a column chunk or page holding values
on both sides of 2^31 / 2^63 has min_value > max_value. The native reader
rejected the whole query with INCORRECT_DATA at four places: row group
pruning, page pruning by the column index, the TopN row group shortcut and
`optimize_min_max_from_files`.

`adjustRangeFromIndexIfNeeded` now reports an inverted range instead of
throwing, and every caller takes the fallback it already takes for other
unusable statistics: a whole-universe range for row group and page pruning,
no shortcut for TopN, reading the data for min/max. Nothing that was read
before is pruned now, so results can only get more complete.

This is sufficient for the polars case: if all values of a chunk share the
top bit, signed and unsigned order agree and the statistics are correct;
otherwise the signed minimum has the top bit set and the signed maximum does
not, so the unsigned range is always inverted and gets ignored.

The test builds the polars footer in place: a ClickHouse-written file with
two row groups whose first row group gets its Statistics and ColumnIndex
min/max swapped, so the second row group still proves that valid statistics
keep pruning.

Closes ClickHouse#128302

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

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

Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review (2 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ The TopN and optimize_min_max_from_files arms of 05479 check results only, not the optimization counters. DISAGREE The non-inverted path is unchanged; valid statistics staying in use is pinned by 05317, 05372 (ParquetTopKSkippedRowGroups) and 05332 (ParquetReadMinMaxFromStatistics), all passing with the fix; on this file both counters are 0 for any implementation, so they would not discriminate.
2 💡 Test length (95 lines, mostly the inline Thrift patcher). noted, not blocking Same pattern as the other Parquet byte-patching tests; no shared helper exists.
3 💡 An inverted covering.bbox range now becomes the whole universe instead of staying in the hyperrectangle. noted, not blocking Correct by construction, not claimed in the description.

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-9:20261031-015109

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05479_parquet_inverted_statistics_ignored.sh builds the polars-shaped file in the test; on master every query (row group, column index, TopN, min/max; UInt64 and UInt32) fails with INCORRECT_DATA on every run.
b Root cause explained? Reader::adjustRangeFromIndexIfNeeded and a copy of its check in readMinMaxFromStatistics threw on min_value > max_value, so broken optional statistics failed the whole read instead of only disabling pruning.
c Fix matches root cause? Yes. The check stays; an inverted range now takes the fallback each caller already has for unusable statistics (whole-universe range, no TopN shortcut, read the data).
d Test intent preserved / new tests added? New test, red on master for all four paths and both types. It also asserts that the valid second row group is still pruned (ParquetPrunedRowGroups, ParquetPrunedPages). No existing test asserted the removed error.
e Both directions demonstrated? Yes, on Build-ID-checked binaries: pristine HEAD fails every arm, the fix matches the reference. Also a real polars 0.20.31 file: same results as with the optimizations disabled. 50/50 randomized runs pass.
f Fix is general across code paths? All four consumers of the check are changed. Bloom/dictionary filters and the file-skip cache read the same hyperrectangle, so a whole-universe entry never prunes there either.
g Fix generalizes across inputs (params/datatypes/wrappers)? The check compares decoded Fields and is type-agnostic; tested for Nullable(UInt64) and Nullable(UInt32) across the 2^63 / 2^31 boundary, the shape polars writes. Decoding of other types and wrappers is unchanged.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change: queries that failed now succeed, and nothing that succeeded changes.
i Invariants and contracts preserved? On false the range is left untouched and every caller replaces or discards it, so no inverted range reaches checkInHyperrectangle, topKShouldSkipRowGroup or the min/max result.

Session id: cron:clickhouse-impl-slot-5:20261031-010400

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @al13n321 @scanhex12, could you review this and add the can be tested label so CI runs? It replaces the Statistics have min_value > max_value throw with a fallback: such statistics (polars 0.20 writes UInt32/UInt64 in signed order) are treated as missing, so row group pruning, the column index, the TopN shortcut and min/max from statistics read the data instead of failing the query.

@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0b4da4c]

Summary: ✅


AI Review

Summary

The change treats inverted Parquet statistics as unavailable for row-group pruning, page pruning, TopN shortcuts, and optimize_min_max_from_files, allowing affected polars files to be read. No actionable findings in the current head, 0b4da4c972dc615fd15b5beb9c690bb652215ea1.

Missing context / blind spots
  • ⚠️ Runtime validation was not independently reproduced: no local ClickHouse binary is available, and the focused stateless CI report has no results yet. Confirm 05479_parquet_inverted_statistics_ignored passes on the current head.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-parquet-reader-v3 Issues related to the native Parquet reader. labels Oct 31, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 0b4da4c97 with master 9f79cc172 (stripped binary size, per-symbol sizes and ThinLTO time; 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.

Compile time of recompiled translation units

17 translation units recompiled, 167 s compile time in total, 17 of them have a recent master baseline.

Job report

@fm4v
fm4v added this pull request to the merge queue Nov 1, 2026
groeneai and others added 2 commits November 1, 2026 11:14
The corpus from ClickHouse#128305 records `INCORRECT_DATA` for the
`WHERE u64 > 9223372036854775807` checks of the polars 0.20.31 files,
whose UInt64 statistics are written in signed order and are inverted.
With inverted statistics ignored, these 9 checks now return the count
the writer itself reads back (`ok`). No other line of the corpus tests
(05359, 05473-05478) changes.

The master merge before this commit brings in the corpus, which did not
exist on the branch; without it the merge queue fails 05474.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 1, 2026
@fm4v
fm4v added this pull request to the merge queue Nov 1, 2026
The inversion check ran on the decoded range. Decoding leaves a bound at
infinity when the requested type cannot hold it, so under a type hint the
inverted polars statistics can stop looking inverted: a UInt64 chunk with
min_value 2^63 and max_value 1 read as `DateTime` decodes to [-inf, 1], and
row group and page pruning dropped the row that the read saturates to the
`DateTime` maximum (`WHERE u64 > toDateTime(100)` returned nothing; it
returns that row with the pruning disabled). Same on master.

`IntConverter` now compares the stored values in the column's sort order
(signed or unsigned per the logical type), and the row group and column
index paths ignore statistics that are inverted there. The TopN and
min/max-from-statistics paths already give up when a bound does not
decode. The check on the decoded range stays for the other converters.

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

groeneai commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

@fm4v the queue failed on 05474_parquet_corpus_writers: #128305 records the 9 polars 0.20 WHERE u64 > 9223372036854775807 rows as INCORRECT_DATA, which is what this PR fixes. I merged master (the corpus was not on the branch) and updated those rows in a87c586.

I also added c61dbb6: read with a DateTime type hint, the out-of-range minimum 2^63 decodes to no bound, so the inverted range no longer looked inverted and row group and page pruning dropped the matching row (also on master). Integer statistics are now checked in their stored order, with two new test lines. The PR is still queued with the new head.

@groeneai

groeneai commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

Correction to my comment above: the merge queue merged this PR at its enqueue-time head 0b4da4c, so a87c586 and c61dbb6 did not reach master. They are re-landed in #128494 (the 05474 reference rows) and #128508 (the stored-order inversion check).

buggins pushed a commit to buggins/ClickHouse that referenced this pull request Nov 1, 2026
ClickHouse#128336 ignores inverted Parquet statistics, so the 9 polars 0.20.31
`WHERE u64 > 9223372036854775807` checks that ClickHouse#128305 recorded as
`INCORRECT_DATA` now return the count the writer reads back. The merge
queue landed ClickHouse#128336 without its commit that updated these rows
(a87c586), so master fails this test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-parquet-reader-v3 Issues related to the native Parquet reader. 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.

Parquet: signed-order UINT_64 / UINT_32 statistics written by polars 0.20 fail the query with min_value > max_value instead of being ignored

4 participants