Skip to content

Parquet v3: materialize constant column chunks from statistics - #2976

Draft
ianton-ru wants to merge 20 commits into
antalya-26.8from
feature/antalya-26.8/parquet-constant-columns
Draft

ianton-ru wants to merge 20 commits into
antalya-26.8from
feature/antalya-26.8/parquet-constant-columns

Conversation

@ianton-ru

Copy link
Copy Markdown

Part of #2181 (Parquet v3 reader). A Parquet column chunk whose min/max statistics are equal, or that is entirely null, is returned as a constant (ColumnConst) instead of being read and decoded. When the Column Index is available, row subgroups that are single-valued according to it are handled the same way. input_format_parquet_use_column_index_for_constant_columns forces the Column Index load for this purpose; it is off by default.

The optimization is not applied while input_format_parquet_verify_checksums is enabled, which is its default. The data pages of a constant chunk are not read, so their checksums would not be verified; with verification on, such chunks are decoded normally. In effect the optimization is active only when verification is disabled. This keeps the page-level checksum check, covered by 03408_parquet_checksums, from being skipped silently.

Settings:

  • input_format_parquet_use_constant_column_optimization (default on; effective only with verification disabled)
  • input_format_parquet_use_column_index_for_constant_columns (default off)
  • input_format_parquet_fill_constant_pages (experimental, default off)

Design notes: docs/design/parquet-v3-page-level-constant-column.md.

Related: #2181

Changelog category (leave one):

  • Performance Improvement

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

Parquet v3 reader: a column chunk whose statistics show a single value, or only nulls, is materialized from the statistics without reading its data pages. This applies only when input_format_parquet_verify_checksums is disabled.

Documentation entry for user-facing changes

The settings are documented in their declarations (input_format_parquet_use_constant_column_optimization, input_format_parquet_use_column_index_for_constant_columns, input_format_parquet_fill_constant_pages). The design notes are in docs/design/parquet-v3-page-level-constant-column.md.

CI/CD Options

Exclude tests:

  • Fast test
  • Integration Tests
  • Stateless tests
  • Stateful tests
  • Unit tests
  • Performance tests
  • Aarch64 tests
  • All with ASAN
  • All with TSAN
  • All with MSAN
  • All with UBSAN
  • All with Coverage
  • All Regression
  • Disable CI Cache

Regression jobs to run:

  • Fast suites (mostly <1h)
  • Aggregate Functions (2h)
  • Alter (1.5h)
  • Benchmark (30m)
  • CAS (content-addressed storage; Antalya only)
  • ClickHouse Keeper (1h)
  • Iceberg (2h)
  • LDAP (1h)
  • OAuth (5m)
  • Parquet (1.5h)
  • RBAC (1.5h)
  • SSL Server (1h)
  • S3 (2h)
  • S3 Export (2h)
  • Swarms (30m)
  • Tiered Storage (2h)

UnamedRus and others added 20 commits November 30, 2026 13:22
When a Parquet column chunk provably holds a single value in every row -
its min/max statistics have `min_value == max_value`, no nulls, and the
value is exact - the reader no longer fetches or decodes that chunk's
data pages. Instead `detectConstantColumn` records the value and
`decodePrimitiveColumn`/`formOutputColumn` materialize it directly.

This skips the offset index, column index, dictionary page and data page
reads for such chunks (the row group already passed the key condition via
its `min == max` hyperrectangle), which is a byte-level I/O win for wide
constant columns, plus the decode/decompression CPU.

Restricted to flat, top-level primitive columns with no element nulls.
For `BYTE_ARRAY`/`FIXED_LEN_BYTE_ARRAY` the writer may truncate min/max,
so `min == max` is trusted only when `is_min_value_exact` and
`is_max_value_exact` are both set; fixed-width numeric types are never
truncated.

The value is taken from `PageDecoderInfo::decodeField`, which yields it
in the final output (post-cast) domain - e.g. `DateTime` written as
`TIMESTAMP_MILLIS` decodes to seconds, not the raw millisecond
`decoded_type`. So the constant is materialized directly in the output
type, bypassing the `decoded_type` column and `castColumn`.

Gated by the new setting
`input_format_parquet_use_constant_column_optimization` (default on). A
new `ParquetConstantColumnChunks` ProfileEvent counts materialized
chunks.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Two gate bugs made detectConstantColumn reject every column, so the
optimization never triggered on real files. Found by building and testing
against CH-written Parquet.

1. Null gate. The CH writer omits `null_count` for physically non-nullable
   (`REQUIRED`) columns, but the gate required `null_count` to be present
   and zero, rejecting every non-nullable column. A `REQUIRED` column can
   have no nulls regardless of the statistic, so only require zero
   `null_count` when the column is physically nullable (max definition
   level > 0).

2. Structural gate. `levels[0]` is a synthetic root sentinel with
   `is_array = true`, so a flat `REQUIRED` column's `levels.back()` IS that
   root (`size() == 1`, `is_array == true`) and a `Nullable` column has
   `size() == 2`. The old `levels.size() == 1 && !levels.back().is_array`
   test therefore rejected exactly the flat columns it meant to accept.
   Replace with the correct flatness signals (`levels.back().rep == 0` and
   `max_array_def == 0`), keep the `group_nullable` exclusion, and require
   the output column to be primitive (top-level, not a Tuple/Map/Array
   leaf) via `output_columns`.

Verified on a CH-written file (1M rows, constant Int64/String/DateTime/
Nullable columns): the optimization now fires (`ParquetConstantColumnChunks`
= columns x row groups), results are correct (including the
TIMESTAMP_MILLIS -> DateTime seconds path), reading the constant columns
drops `ParquetPrefetcherReadRandomRead` 10 -> 1 and `ParquetDecodingTasks`
96 -> 64, and a varying column is correctly not treated as constant.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Extends the constant-column optimization to the all-null case. When a
Parquet column chunk provably holds only nulls - its `null_count` statistic
equals `num_values` on a physically nullable leaf - the reader no longer
fetches or decodes that chunk's dictionary or data pages. `detectConstantColumn`
marks the chunk constant (and `is_all_null`), and `formOutputColumn`
materializes the result directly: `Null` for a Nullable output, or the output
default when `input_format_null_as_default` substitutes nulls for a
non-nullable output. A non-nullable output without null substitution cannot
represent the result, so such a chunk is left to the normal decode path.

Unlike the single-value case this needs no value decode, so it sidesteps the
min/max exactness and `BYTE_ARRAY` truncation checks entirely; it only reads
the `null_count` count. `formOutputColumn` records every row of an all-null
chunk in `block_missing_values` (the single-value case has no nulls and records
nothing), matching the normal decode path's null-map bookkeeping so
`input_format_null_as_default` stays correct.

All-null wide chunks are common in schema-evolved files (a column added later
is all-null in older row groups), so this skips dictionary, data page, offset
index and column index reads for a frequent real-world shape. Reuses the
existing `input_format_parquet_use_constant_column_optimization` setting and
the `ParquetConstantColumnChunks` ProfileEvent.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
formOutputColumn previously expanded a constant column chunk with
insertMany(Field, num_rows) - an O(rows) per-row Field-dispatch fill that
showed up as a consistent ~20% UserTime increase in profiling (the baseline
decoded the same near-constant chunk cheaply via RLE/dictionary).

Emit a ColumnConst instead: O(1) to build, and the const-ness propagates
through the pipeline. A PREWHERE/WHERE predicate computes its result from the
value without expanding the stored column, and GROUP BY / aggregation over the
column get a const key. The value is already in the output (post-cast) domain.
The all-null block_missing_values bookkeeping is unchanged.

The reader path preserves the const: getOrFormOutputColumn returns it as-is,
ColumnConst::size() == rows_pass satisfies the delivery checks, and
ColumnConst::filter keeps it const through multistage PREWHERE.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
Extends the constant-column optimization from whole-chunk (tier 1, footer
statistics) to per-row-subgroup granularity using the Column Index per-page
min/max/null_pages, which the reader already loads for predicate push-down.
Catches columns constant over a run of pages without the whole row group being
constant (common in sorted/clustered data), with no change to subgroup/chunk
sizing.

- constColumnMaterializationEligible: shared flat-top-level-primitive gate,
  factored out of detectConstantColumn.
- applyColumnIndex: retain per-page constant info (value / is_const / all_null)
  in ColumnChunk::page_const_info. Fixed-width numeric/date/time only for value
  constants (the Column Index has no per-page exactness flag, so a truncated
  BYTE_ARRAY min==max is untrustworthy); all_null is a plain flag, always safe.
- detectConstantSubchunk: a column is constant for a subgroup iff every page
  overlapping the subgroup row range is all-null, or all hold the same value.
- Wired into intersectColumnIndexResultsAndInitSubgroups; sets the subchunk
  constant fields so decodePrimitiveColumn skips decode and formOutputColumn
  materializes a ColumnConst. New ParquetConstantColumnSubchunks ProfileEvent.

Tier 1 stays the always-on baseline and the only detector for BYTE_ARRAY. Tier 2
is opportunistic: only where the Column Index is already loaded (predicate
push-down columns), never force-fetched.

Deferred (follow-up): skipping the prefetch of constant subgroups pages (the I/O
win). Currently those pages are still fetched and skipped forward by the next
subgroups skipToRowOrNextPage; correct but does not yet save the read.

Design: docs/design/parquet-v3-page-level-constant-column.md

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
Completes tier-2: a per-subgroup constant column (detectConstantSubchunk) now
also skips fetching its data pages, not just decoding them. In
determinePagesToPrefetch, a constant subchunk claims no pages; a page fully
inside it is released (never fetched), while a page shared with a neighbouring
non-constant subgroup is left for that subgroup to claim.

Safe because tier 2 only exists when the Column Index (hence Offset Index) is
loaded: a constant subgroup never calls skipToRowOrNextPage, and a non-constant
subgroup jumps directly via the offset index to its own claimed pages, so a
released constant page between them is never accessed. The whole-chunk
data_pages_prefetch is still split (likely_to_be_used=false), so only claimed
pages are actually read.

This turns the tier-2 CPU/memory win into an I/O win as well (fewer S3 GETs /
ParquetPrefetcherReadRandomRead), matching tier 1 but at page granularity.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
Adds input_format_parquet_use_column_index_for_constant_columns (default off).
By default tier-2 per-subgroup constant detection only runs where the Column
Index is already loaded (columns with a predicate). This setting force-loads the
Column Index (and Offset Index) for eligible read columns without a predicate,
so tier-2 can skip single-valued page runs on them too - worthwhile for sorted /
low-cardinality columns, at the cost of a small extra (tail-contiguous,
coalesced) index read.

A force-loaded column takes the same load path as a predicate column
(use_column_index = true); applyColumnIndex records per-page constant info but
skips page-level predicate pruning when the column has no condition (prev_row_idx
stays 0 -> whole chunk selected -> no restriction).

Setting is declared in FormatFactorySettings, plumbed through FormatSettings /
FormatFactory, and recorded in SettingsChangesHistory (26.6).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
…off]

Approach B: when only PART of a row subgroup is single-valued (a constant run
shorter than, or straddling, a subgroup), fill those pages from the per-page
Column Index while decoding only the varying pages - producing a full column
where whole-subgroup tier-2 cannot make a ColumnConst.

Gated behind input_format_parquet_fill_constant_pages (default off). Additional
gates keep it correct and simple:
- output column must not need a post-decode cast (we fill the decoded_type
  column with the Column Index value, valid only when decoded == output value
  type);
- no predicate on the column (so no page pruning; data_pages == all pages);
- no prewhere filtering in the subgroup (avoids filter/range intersection);
- at least one single-value page and no all-null page in the subgroup (all-null
  pages fall back to the standard decode).

fillConstantPagesAndDecodeRest walks the subgroup page by page: a single-value
page is filled via insertMany (+ null-map zeros) without reading it; a varying
page is decoded with the existing skipToRowOrNextPage + readRowsInPage, which
jumps over the filled pages via the offset index without loading them.

First cut is decode-fill only: constant pages are still prefetched (correct,
some wasted I/O). Skipping their prefetch needs cross-subgroup coordination and
is a follow-up.

Design: docs/design/parquet-v3-page-level-constant-column.md

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
Completes the mixed-topology fill (input_format_parquet_fill_constant_pages,
default off): a subgroup that fills its single-value pages from the Column Index
no longer prefetches them. determinePagesToPrefetch consults
willFillConstantPages (the same deterministic predicate the decode path uses) and,
for a fill subgroup, does not claim its is_const pages - so a page overlapped
only by fill subgroups is released and never read, while a page also decoded
normally by another subgroup is still fetched.

Also adds a no-prewhere / no-row-level-filter gate to willFillConstantPages so
rows_pass == rows_total holds identically at prefetch time and decode time,
keeping the two decisions consistent.

This turns the mixed-topology fill from a decode-CPU-only win into an I/O win
too (fewer page reads), matching tier-2 at sub-subgroup granularity.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
min_value == max_value provably means a single exact value for every physical
type, including BYTE_ARRAY / FIXED_LEN_BYTE_ARRAY. Statistics and Column Index
bounds are always valid (min_value <= every value <= max_value) and truncation
only ever widens them, so a truncated value - or any page/chunk with two
distinct values - yields min_value < max_value. Equality therefore requires a
single value short enough to be stored exactly; the is_*_value_exact flags are
implied and, for the Column Index (which has no per-page exact flag), never
needed.

Remove the BYTE_ARRAY/FIXED_LEN exclusion from tier 1 (detectConstantColumn) and
the per-page recording in tier 2 (applyColumnIndex), so constant string columns
and string constant page runs get the optimization too. Also fixes tier 1 for
writers that omit is_*_value_exact (previously any such BYTE_ARRAY constant was
skipped even when short and exact).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
Previously a mixed-topology subgroup (input_format_parquet_fill_constant_pages)
bailed to normal decode if any overlapping page was all-null. Now fill them too:
an all-null page appends no non-null values and marks its rows null in the null
map; the existing expand() + Nullable-wrap / null_as_default tail finalizes them
exactly as the normal decode does (the column holds compact non-null values, the
null map covers all rows). determinePagesToPrefetch also skips prefetching
all-null pages, matching the fill.

willFillConstantPages now accepts all-null pages as fillable, but still bails
when an all-null page is present and the output can represent neither null
(non-Nullable output) nor a default (no null_as_default) - the standard decode
raises the usual not-null error there.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
Drop the column_index_condition gate from willFillConstantPages. A page-pruning
predicate on the column (column_index_condition) is safe for the fill: subgroups
are built only over contiguous surviving row ranges, so a pruned page never
overlaps a subgroup and the fill never walks it; the prefetch-skip indexes
page_const_info by each page global position (page.meta into page_locations),
which is correct regardless of the data_pages subset. A column carrying an
actual prewhere/row-level filter is still blocked by the no-prewhere gate.

Gate 2 (output needs_cast) is intentionally left in place: whether decodeField
yields the decoded or the output value domain is ambiguous from static reading
(SchemaConverter/convertField vs the tier-1 is_constant branch), and guessing
wrong is silent wrong data. It needs a build + a needs_cast test to resolve
(and that test would also confirm tier-1/tier-2 on hint-cast columns).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
The mixed-topology fill now walks the rows that pass the filter
(row_subgroup.filter) instead of the whole subgroup range, so it works under
PREWHERE / row-level filters: it iterates passing ranges (like the standard
row-range decode) and, within each, fills constant / all-null pages for their
overlapping passing rows and decodes varying pages for the contiguous passing
sub-range. It therefore produces exactly rows_pass values for any filter, and
willFillConstantPages no longer depends on rows_pass - so the decision is
identical at prefetch time and decode time and the prefetch-skip can never drop
a page the decode needs.

Removes the no-prewhere and rows_pass == rows_total gates from
willFillConstantPages. The only remaining gate is needs_cast (build-gated; see
the decodeField value-domain question).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
formOutputColumn now builds the single-value constant in input_type and applies
the same castColumn the per-row decode uses when needs_cast is set, instead of
inserting the value straight into an output_type column and skipping the cast.

Today this is a no-op: the optimization only fires when the stats decoder needs
no value-transforming conversion (SchemaConverter gates allow_stats on that), so
input_type == output_type for every constant column. The change keeps the
constant path correct if allow_stats is ever generalized to allow transforming
casts (e.g. decimal rescale) - the constant would then go through the same cast
as the normal decode. Casting a ColumnConst is O(1) (one value) and preserves
const-ness, so no cost.

The all-null constant is synthesized in the output domain (Null / output
default) and is not cast. The mixed fill already casts via the normal path, so
it is future-safe too. The allow_stats / SchemaConverter logic is untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: UnamedRus <dtitmoav@gmail.com>
Tier-2 per-subgroup constant detection (`detectConstantSubchunk`, gated by
`input_format_parquet_use_column_index_for_constant_columns`) never fired for
required (non-nullable) columns.

`applyColumnIndex` computed `can_be_null` purely from the Column Index
`null_counts`: `!__isset.null_counts || null_counts[page] != 0`. ClickHouse's
own Parquet writer emits `null_counts` only for nullable columns (`Write.cpp`,
`has_null_count = max_def == 1 && max_rep == 0`), so for a required column
`null_counts` is absent and `can_be_null` was spuriously `true` on every page.
That disabled the per-page `is_const` branch, so `page_const_info` was never
populated and tier-2 detection never triggered - exactly for the common case of
a non-nullable constant column.

A required column (max def level 0) can never contain nulls regardless of
whether the Column Index carries `null_counts`, so fold the schema `nullable`
flag into `can_be_null`. `adjustRangeFromIndexIfNeeded` already gates its own
use on `nullable && can_be_null`, so required-column predicate-pruning behavior
is unchanged.

Verified on a real build (sha 60841f9): an otherwise identical file with a
nullable `run` column yields `ParquetConstantColumnSubchunks = 22` while the
non-nullable variant yielded 0; this is the same null_count-omission pattern
already fixed for tier-1 whole-chunk detection.

Signed-off-by: UnamedRus <dtitmoav@gmail.com>
The cherry-picked constant-column commits carried a settings history
hunk from the antalya-26.6 line, which added an unrelated `26.8` block
and the back-pressure setting. Restore the file from `antalya-26.8` and
record only the constant-column settings, in the current
`26.8.39.20001.altinityantalya` block.

Co-authored-by: Cursor <cursoragent@cursor.com>
Move the constant-column tests from 04811 and 04812 to the next free
numbers, 05815 and 05816, as add-test would assign them. The comment in
the constant-column test now says that the `c_dt` column needs a cast,
so it is decoded normally and not materialized.

Co-authored-by: Cursor <cursoragent@cursor.com>
The tests were renamed to 05815 and 05816 in the previous commit; the
old files are removed here, since the rename was staged separately.

Co-authored-by: Cursor <cursoragent@cursor.com>
A constant column chunk is materialized from its decoded value, and
the Column Index path (tier 2) relies on the same invariant: the value
is the output value only when no post-decode cast is applied. Make the
shared eligibility gate exclude columns that need a cast, so tier 1
and tier 2 agree. Decode the statistics value in the decoded type, since
the three-argument `decodeField` overload no longer exists on this
branch. A column that needs a cast, such as a `DateTime` written as
`TIMESTAMP_MILLIS`, is now read and decoded normally.

Co-authored-by: Cursor <cursoragent@cursor.com>
…fied

A constant column chunk or subchunk is materialized from footer and Column Index
statistics without reading its data pages, so page checksums (`input_format_parquet_verify_checksums`,
default true) were silently not verified for those chunks. The regression showed up in
`03408_parquet_checksums`, where a corrupted single-value column no longer reported the CRC error.

Constant detection is now disabled when verification is requested, so such chunks are decoded normally.
The optimization applies only when verification is turned off.

Update `05815_parquet_constant_column_optimization` and `05816_parquet_all_null_column_optimization`
to disable verification in their queries that exercise the optimization, and add
`05817_parquet_constant_column_checksums` covering the default and the disabled cases.
Document the interaction in `input_format_parquet_use_constant_column_optimization` and the design doc.

Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions

Copy link
Copy Markdown

Workflow [PR], commit [816b6c2]

@ianton-ru ianton-ru added antalya performance Antalya Roadmap: Performance port-antalya PRs to be ported to all new Antalya releases antalya-26.8 labels Nov 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

antalya antalya-26.8 performance Antalya Roadmap: Performance port-antalya PRs to be ported to all new Antalya releases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants