Repository navigation
Do not prune Parquet row groups and pages on integer statistics the requested type reorders - #121033
Conversation
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex) plus a separate cold read of the
All five sub-expressions of the guard now have a measured reddening arm, each mutation rebuilt and Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-49:20260919-140100 |
|
Workflow [PR], commit [d3b54e6] Summary: ✅
AI ReviewSummaryThis PR tightens Parquet min/max pruning for integer-backed type hints whose cast can reorder or otherwise invalidate the stored statistics, and it also closes the Final Verdict
LLVM Coverage ReportMeasured on commit d3b54e6.
Changed lines: Changed C/C++ lines covered: 102/108 (94.44%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units1042 translation units recompiled, 6550 s compile time in total, 1042 of them have a recent master baseline. |
…equested type reorders
A Parquet column stored as a plain INT32/INT64 has signed-order min/max
statistics. Read with an unsigned type hint, a point lookup silently
returned no rows at default settings, on a file ClickHouse wrote itself:
INSERT INTO FUNCTION file('i32.parquet', Parquet, 'x Int32')
SELECT arrayJoin([toInt32(-1), toInt32(5)]) AS x;
SELECT count() FROM file('i32.parquet', Parquet, 'x UInt32')
WHERE x = 4294967295; -- 0, wrong; 1 with both push-downs off
IntConverter::convertField decodes one endpoint at a time: it
sign-extends under input_signed (the stored type's signedness) and emits
a Field under field_signed (the requested type's). Where the two
interpretations disagree in sign it answers std::nullopt, and
PageDecoderInfo::decodeField turns that into "leave that side of the
range at infinity". That is sound only if the surviving pair is still an
interval in the Field order, and it is not when the pair straddles the
sign boundary. For the row group {-1, 5} the signed-order pair is
min = -1, max = 5; as UInt32 the values are {4294967295, 5}, so the true
bounds are [5, 4294967295]. min = -1 becomes nullopt, correctly
unbounding the left side, but max = 5 is kept, and 5 is not an upper
bound in unsigned order. KeyCondition::checkInHyperrectangle gets
(-inf, 5], and both the row group and the page holding the matching row
are pruned - two legs, so disabling either one alone still returned 0.
dispatch_int_stats_converter authorised this by taking signedness from
the output type alone and never comparing it with the stored type's, so
allow_stats contradicted its own documented contract in Decoding.h:
"False if type hint requires a nontrivial cast that's either not
monotonic or not supported". A signedness-flipping reinterpretation is
exactly such a cast - castColumn wraps the values while the endpoints
were compared in the stored order.
The guard withholds the statistics unless the cast to the requested type
preserves the stored order for every value the stored type can hold,
which is FunctionConvert::getMonotonicityForRange's "always monotonic"
class: the same signedness needs an equal or wider output, and a
signedness flip needs an unsigned stored type and a strictly wider
signed output. Everything else reorders. A flip maps the negative half
above the positive one, and a narrowing cast wraps, so a UINT_32 column
holding {65535, 65536} read as UInt16 produces {65535, 0} and the stored
max is again not an upper bound. stored_bits is the declared width
(output_size is set to 1 or 2 exactly when the INTEGER annotation is 8
or 16 bits), and 1 for physical BOOLEAN, whose single bit fits every
native integer. It sits in the one lambda all five integer sites call,
so no site can be forgotten, and the single flag covers the row group
leg, the page index leg and the ColumnIndex prefetch.
Cost is zero on every healthy read: an un-hinted read infers the
matching signedness, ClickHouse writes UInt* with UINT_* annotations,
and the order-preserving widenings (UINT_32 to Int64, UINT_8 to Int16 or
wider, BOOLEAN to Int8) keep their statistics. The new test has a
pruning-preservation control for each, asserting a non-zero
ParquetPrunedRowGroups.
Two alternatives were rejected. Dropping only a straddling pair at the
Reader level needs a pair-level API (convertField sees one endpoint and
cannot know the other), a fail-closed rule for a lone endpoint, and a
data-dependent branch in both legs, to keep pruning in a configuration
that is a deliberate reinterpretation. Reinterpreting the endpoint into
the output order cannot work at all: the value set is then a union of
two disjoint intervals, [0, max] and [min + 2^N, 2^N - 1], which no
single Range expresses, and emitting the reinterpreted min inverts the
pair so adjustRangeFromIndexIfNeeded reports a valid file as
INCORRECT_DATA - the same dead end measured on ClickHouse#118387.
Scope, measured rather than argued. The narrowing and 8-bit arms are
fixed in the min/max legs only. With both hash push-downs off and both
min/max legs on, a UINT_32 column read as UInt16 and an INT32 column
read as Int16 each returned 0 rows where 1 is correct, on master and on
the signedness-only form of this guard alike, and 1 here. At default
settings they are still lost, because the bloom and dictionary filters
hash both sides after a static_cast to the parquet physical type, so the
two hashes agree only if the low sizeof(physical) bits do: the count is
0 at defaults, still 0 with both min/max push-downs off, and 1 only once
the dictionary and bloom push-downs are off too. That leg is left to a
follow-up, so the bound removed here is unsound and its removal a strict
improvement, but not a complete fix for those arms. The Enum8/Enum16 and
Date32 carriers of this lambda have the same min/max hole and stay with
that follow-up too: they change the value space or the unit rather than
the width, so the width rule above is not the right predicate for them.
The IPv4 arm is left to ClickHouse#117017, which fail-closes IPv4 statistics for
the same reason. DateTime and TIMESTAMP_MILLIS hints stay sound and
untouched: those conversions saturate rather than wrap
(ToDateTimeTransform64Signed returns 0 for a negative input, and an
out-of-range positive lands on UINT32_MAX), and saturation is monotonic,
so the converted endpoints still bound the converted values; both were
measured to return the row at default settings. A Date hint over an
integer column, however, joins the Enum and Date32 carriers rather than
being sound: an integer past the day-number range is reinterpreted as a
Unix timestamp, so a plain INT32 column holding {65536, 65537} decodes
to two 1970-01-01 values while its statistics stay [65536, 65537], and a
lookup of 1970-01-01 returns 0 of 2 rows with only the min/max legs
active. The width rule above cannot separate that from its sound path,
since a parquet DATE column read as Date must keep pruning, so it goes
to the same follow-up.
Closes: ClickHouse#118376
Co-Authored-By: Claude Opus 5 <[email protected]>
813089a to
91bb8ca
Compare
CI finish ledger - 91bb8caEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Notes on the 5/6 line: #120923 (merged 2026-09-19 18:34Z) was the candidate fix for this class and None of the three failures is caused by this diff, which is confined to Parquet integer statistics Session id: cron:our-pr-ci-monitor:20260920-043144 |
Fixing PR for the
|
|
@groeneai what about other data types where the sort order could differ if the user passes a different data type? I'm thinking about a Enum8 as an example |
|
There are more, and Enum8 is the sharpest one. What decides a case is whether the cast from the stored type to the requested type preserves order for every value the stored type can hold. The guard in this PR only reaches requested types that Measured at this head against master. A
That is worse than the table in the description, which used point lookups where the hash filters lose the row anyway: with a range predicate the min/max leg drops the rows on its own, at defaults. So I am folding The rest of the surface, so the scope paragraph is not vague:
|
An Enum column orders and compares by its underlying signed integer, so an
Enum hint over a parquet integer column reinterprets the stored statistics
exactly like the native integer of that width, and the guard added in the
previous commit has to cover it. It did not: Enum8 and Enum16 left the
requested type through the `switch` below, which granted statistics
unconditionally, so the same reordering the guard rejects for Int8 and Int16
was still accepted for Enum8 and Enum16.
This is reachable at completely default settings, which the min/max legs
alone decide for a range predicate. A UINT_8 column {5, 200} has statistics
[5, 200] in unsigned order and reads back as {5, -56} in 8-bit signed order,
so the stored max is not an upper bound. Measured, correct answer 1 in all
four lines, nothing disabled:
UINT_8 {5, 200} as Int8 x < 0 0 before the previous commit, 1 after
UINT_8 {5, 200} as Enum8 x < 'lo' 0 before, 0 after, 1 here
UINT_16 {5, 40000} as Int16 x < 0 0 before, 1 after
UINT_16 {5, 40000} as Enum16 x < 'lo' 0 before, 0 after, 1 here
The point-lookup form of those two arms is additionally lost to the bloom and
dictionary filters, which is why the previous commit's measured table read as
if the Enum carriers were unreachable through this leg. A range predicate is
served by neither hash filter, so it isolates the min/max legs at defaults and
shows that closing them is a complete fix for that predicate class.
The predicate itself does not change, only which requested types enter it:
Enum8 counts as 8 signed bits and Enum16 as 16, both via
getSizeOfValueInMemory, and field_signed becomes true for them, which is what
the `switch` arm already left in place by default. Nothing sound is withheld:
an INT_8 column read as Enum8 is the identity and a UINT_8 column read as
Enum16 is an always-monotonic widening flip, and both keep pruning under a new
control apiece. Dropping the enum clause from the field_signed assignment
alone reddens both new arms and the INT_8 to Enum8 control (2 row groups read,
0 pruned), so neither half of the change is redundant.
Date32 stays in the `switch`: it is the same label group but not the same
question, since its reinterpretation is bounded by the target's valid day
window rather than by a width, and a parquet DATE column read as Date must
keep pruning. That carrier, the Date one, and the bloom and dictionary hash
legs remain the follow-up named in the description.
One claim of the previous commit message needs correcting. It said a DateTime
hint over a plain integer column returns its row at default settings because
those conversions saturate and saturation is monotonic. The monotonicity
argument does not hold as stated: the statistics are not converted along with
the values, they stay in the raw integer domain. The conclusion survives on the
legs this PR touches, but only by measurement. An INT64 column {2^32, 2^32+1}
read as DateTime decodes to two 2106-02-07 06:28:15 values, and a lookup of
that timestamp returns 2 rows with the dictionary and bloom push-downs off,
with either min/max leg alone, and 0 at default settings. So the min/max legs
are sound for that hint, and its default-settings loss belongs to the hash
legs, with the rest of that family.
Co-Authored-By: Claude Opus 5 <[email protected]>
|
Folded in as 3485e08. One correction to what I told you about |
CI finish ledger - 3485e08Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260922-043144 |
|
@groeneai there are still other data types where this bug can manifest. Can you explain and the others? |
|
Three more, and I measured each one instead of arguing it. Two are the same defect on the same leg and
The The rest of that surface is sound, and I checked rather than assumed:
The cost is the trade this PR already makes elsewhere: a plain |
`Date` and `Date32` reach the integer statistics through the reinterpreting arm
of `dispatch_int_stats_converter`, which never checked whether the requested
type keeps the endpoints' order. A day number outside the target's window is
reinterpreted rather than carried over, so the raw endpoints stop bounding the
values:
- a plain `INT32` or `INT64` column {65536, 65537} read as `Date` holds two
1970-01-01 values under statistics that stay [65536, 65537], so a lookup of
1970-01-01 was pruned away, 0 rows instead of 2;
- a plain `INT64` column {100000, 2^32} read as `Date32` holds a 2106 day under
a kept min of 2243-10-17, so `x < '2243-10-17'` was pruned away, 0 instead
of 1;
- a plain `INT32` column {2932896, 2932897} read as `Date32` straddles the day
2932896 limit above which the cast reads the number as seconds instead, so
9999-12-31 and 1970-02-03 arrive in that order and `x < '2000-01-01'` was
pruned away, 0 instead of 1.
All measured with the dictionary and bloom push-downs off, so that only the
min/max legs were live. At completely default settings those rows are lost to
the hash filters as well, which is a separate defect.
The width and signedness test is hoisted into `stats_order_preserved` and
applied to `Date`, whose window is the whole UInt16 domain
(DATE_LUT_MAX_DAY_NUM is 0xFFFF), so passing that test already means no stored
value leaves the window. `Date32` stops at day 2932896, well below INT32_MAX, so
matching widths prove nothing there and it now requires the day-range check
instead. That check is `date_overflow_behavior`, set on the parquet `DATE` path,
where `convertField` drops every endpoint outside the window and inside it the
day number is the output value, so a `DATE` column read as `Date` or `Date32`
keeps pruning.
Applying that check turned out to require a fix of its own: `convertField` ran it
only for a signed Field, and a `Date` target asks for an unsigned one, so the
parquet `DATE` path never range-checked its endpoints either. Days past
2149-06-06 read as `Date` with `date_time_overflow_behavior = 'saturate'`
therefore arrived as 2149-06-06 while their raw bounds survived, and the lookup
of that day was pruned away, 0 rows instead of 2. The check now runs before the
Field is built, for both signednesses.
The `IPv4` and `DateTime` arms keep their current behaviour, and both are
unsound in the same way: `IPv4` over a signed carrier hides `Conversion from
Int32 to IPv4 is not supported` behind an empty result, and a `UINT_64` column
{2^32, 2^32+1} read as `DateTime` decodes to two 2106-02-07 06:28:15 values
whose lookup each min/max leg discards. ClickHouse#117017 fixes both by range-checking
those endpoints, which preserves the pruning that a width test would give up, so
those arms are better left to it.
The cost is the trade the rest of this change already makes. A plain `INT32`
column whose values all sit inside the Date window is no longer pruned under a
`Date` hint, and no integer carrier is pruned under a `Date32` hint, because the
endpoints have to bound every value the stored type can hold, not only the ones
present.
|
Folded in as 2bfa4e4.
That day-range check was not actually running for a And That last one and the Test: 5 new arms, each reddening on the previous head and nothing else; 11 |
CI finish ledger - 2bfa4e4Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Measured at this head rather than inherited: 313,511 CIDB rows over 135 checks, 0 Not reachable from this diff: the change is Parquet integer-statistics pruning in Session id: cron:our-pr-ci-monitor:20260923-073136 |
|
@groeneai it also looks like we might still have issues of erroneous row pruning with DateTime and IPv4 types as well when used in combination with UInt64s. Here's a bit from claude on the topic
Could you verify first before making any changes? |
|
Verified on master (
Both claims hold, and there is a third case: a What I'm changing:
These are the rules #117017 proposes for the same two arms. I had left them to it to avoid a conflict, but it has been idle since early September, so I'll fold them in here; whichever merges second resolves trivially. The |
…overflow error
Three more cases where Parquet min/max pruning answered differently from a read
with push-down off, found in review:
- `IPv4` over a signed carrier has no cast at all, so pruning turned the
NOT_IMPLEMENTED error into an empty result; over a UINT_64 carrier the cast
wraps, so a row group {3, 2^32+1, 2^32+2} arrives as 0.0.0.3, 0.0.0.1,
0.0.0.2 under a stored min of 3 and `x = '0.0.0.1'` returned 0 instead of 1.
`IPv4` now takes statistics only through the same order test as the integer
arms, i.e. from an unsigned carrier of at most 32 bits.
- `DateTime` saturates above UINT32_MAX, so a UINT_64 row group {2^32, 2^32+1}
arrives as two 2106-02-07 06:28:15 values outside the stored bounds. Such an
endpoint now bounds nothing. Same rule as ClickHouse#117017 for these two arms.
- A parquet `DATE` value outside the requested type's window fails the read
under the default `date_time_overflow_behavior`, but the out-of-window
endpoint was only dropped, so the other side could still prune the chunk and
`x < '2000-01-01'` over {2149-06-06, 2149-06-07} read as `Date` returned 0
instead of VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE. Under `throw` such an
endpoint now voids the chunk's statistics (row group and page index), so the
chunk is read and raises the same error; `saturate` keeps the one-sided
widening. Since every row-group statistics field is optional, such a row
group is bounded under `throw` only when both min and max are present. The
window check also moves before the signed-to-unsigned check, because a
negative day is outside the `Date` window too.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Pushed
Every new defect arm fails on the previous head and passes here; the new controls pass on both. 40 randomized runs and runs under |
…_load_2 00900_long_parquet_load_2 loads every file in data_parquet/ and compares the output with its reference, so the two hand-crafted fixtures added for 05230_parquet_date_partial_statistics showed up as unexpected blocks. They only make sense for that test, so add them to the exclusion list, like the other test-specific fixtures there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - d3b54e6Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
146 of 146 check runs completed (127 passed, 18 not selected, 1 red: the job above), Session id: cron:our-pr-ci-monitor:20261001-080315 |
|
Fixing PR for the Stress test (arm_release) hung check (05258 |
1ea026f
The plain INT64 branch of SchemaConverter::processPrimitiveColumn turned the statistics off for a DateTime target, so a Parquet INT64 column without a logical type (EventTime in ClickBench's hits.parquet) got no row group or page pruning and no top-K row group skipping. The plain INT32 and annotated INTEGER(64) branches already allow it: a bound outside the DateTime range is not used, and IPv4 over a 64-bit integer is still refused. This is the remaining part of ClickHouse#117017; ClickHouse#121033 already added those rules. Co-authored-by: wudidapaopao <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #118376
Related: #117017
Related: #118387
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a Parquet reader bug where a filter on an integer column read with a type hint that reorders the file's statistics (for example
UInt32over a plainINT32column,Enum8over aUINT_8one, orIPv4over aUINT_64one) could silently return fewer rows than the file holds. Such statistics are no longer used for row group or page pruning, and aDATEvalue outside the requested type's range now raises the same error with pruning as without it. Closes #118376.Description
The native Parquet reader is the default since 25.11. At default settings a point lookup on an integer column read with an unsigned hint silently returned no rows, on a file ClickHouse wrote itself:
Root cause. Min/max are ordered as the stored type, and
convertFieldunbounds only the endpoint the reinterpretation cannot represent. Read asUInt32, the keptmax = 5of{-1, 5}is not an upper bound, so both are pruned.Change. One guard in that lambda, called by all five integer sites, keeps the statistics only when the cast preserves the stored order for every stored value: an equal or wider output of the same signedness, or unsigned to a strictly wider signed type. It covers the native integers,
Enum8/Enum16,DateandIPv4, which order by their underlying integer.Date32keeps statistics only on the parquetDATEpath, whose day-range check confines the endpoints. ADateTimeendpoint aboveUINT32_MAXbounds nothing, since that cast saturates. Underthrow, an out-of-rangeDATEendpoint voids the chunk's statistics so the read raises its error.Scope. Left open, measured below: a point lookup on a narrowing hint is still lost to the bloom and dictionary filters (#121077). The
IPv4andDateTimehunks are the rules #117017 proposes for those arms.Validation. New stateless tests: both legs, both widths and directions, a widening flip, narrowing,
Enum,Date,Date32,IPv4andDateTimehints, bothDATEoverflow modes per leg, footers with only a min or a max, and aParquetPrunedRowGroupscontrol per kept widening. Each published head reddens exactly the arms its round adds; randomized, plain and non-UTC runs are clean.Measured: the Enum arms, and what a min/max guard cannot reach
count()for a predicate that matches, so a low number is silent row loss.A range predicate is served by neither hash filter, so it isolates the min/max legs at completely default settings:
UINT_8 {5, 200}asInt8,x < 0UINT_8 {5, 200}asEnum8('lo' = 5, 'hi' = -56),x < 'lo'UINT_16 {5, 40000}asEnum16('lo' = 5, 'hi' = -25536),x < 'lo'A point lookup is a different story. (a) all defaults; (b)
input_format_parquet_filter_push_down = 0, input_format_parquet_page_filter_push_down = 0; (c) (b) plus the dictionary and bloom push-downs off.UInt32file read asUInt16UInt8file read asInt8UInt32file read asDate32Int32file{65536, 65537}read asDate(b) is load-bearing: with both min/max legs off the row is still lost, so no min/max guard recovers those point lookups. The filters hash both sides after a
static_castto the physical type, so a naivesizeof(output) >= sizeof(physical)rule would disable them for everyUInt8/UInt16/Int8/Int16column, which ClickHouse writes as physicalINT32.The
Daterows above are fixed here on the min/max side: with only those legs live,{65536, 65537}read asDatereturns 2. Also fixed on those legs: aUINT_64column{2^32, 2^32+1}read asDateTimedecodes to two2106-02-07 06:28:15values while the raw bounds survived (0, now 2);{3, 2^32+1, 2^32+2}read asIPv4wraps to 0.0.0.3, 0.0.0.1, 0.0.0.2 (0, now 1); and anIPv4hint over a signed column, or aDATEvalue out of range, now raises its error instead of an empty result.Workflow [PR]
Sync PR [sync-upstream/pr/121033]
Version info
26.10.1.1243-master(included in26.10and later)