Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
65385ee
Parquet v3: materialize constant column chunks from statistics
UnamedRus Aug 5, 2026
349e1ee
Parquet v3: fix constant-column detection gates (never fired)
UnamedRus Aug 6, 2026
d0d384b
Parquet v3: materialize all-null column chunks from statistics
UnamedRus Aug 6, 2026
48cb87d
Parquet v3: materialize constant column chunks as ColumnConst
UnamedRus Aug 7, 2026
3c79bb3
Parquet v3: per-subgroup (page-level) constant-column detection [tier 2]
UnamedRus Aug 7, 2026
1c770fa
Parquet v3: skip prefetch of constant subgroup pages [tier 2 phase 3]
UnamedRus Aug 7, 2026
1f32c8f
Parquet v3: optional force-load of column index for constant detection
UnamedRus Aug 7, 2026
856e23f
Parquet v3: mixed-topology constant-page fill [experimental, default …
UnamedRus Aug 7, 2026
bfd6f7a
Parquet v3: skip prefetch of filled constant pages (mixed topology)
UnamedRus Aug 7, 2026
c173818
Parquet v3: allow string constants (drop min==max truncation guard)
UnamedRus Aug 7, 2026
25b664a
Parquet v3: handle all-null pages in mixed-topology fill (lift gate 1)
UnamedRus Aug 7, 2026
8537304
Parquet v3: allow page-pruned columns in mixed fill (lift gate 3)
UnamedRus Aug 7, 2026
b9c0a89
Parquet v3: filter-aware mixed fill under prewhere (lift gate 4)
UnamedRus Aug 7, 2026
634300e
Parquet v3: cast the materialized constant (future-proof needs_cast)
UnamedRus Aug 7, 2026
5ba632f
Parquet v3: fix tier-2 constant detection for non-nullable columns
UnamedRus Aug 7, 2026
26e75a1
Parquet v3: record constant-column settings in the settings history
ianton-ru Nov 30, 2026
6281ea1
Parquet v3: give the constant-column tests free numbers
ianton-ru Nov 30, 2026
e706314
Parquet v3: remove the old names of the constant-column tests
ianton-ru Nov 30, 2026
d35eca0
Parquet v3: decode constant candidates without a cast only
ianton-ru Nov 30, 2026
816b6c2
Parquet v3: skip constant-column optimization when checksums are veri…
ianton-ru Nov 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions docs/design/parquet-v3-page-level-constant-column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Parquet v3: per-subgroup (page-level) constant-column detection

## Motivation

The current constant-column optimization (`detectConstantColumn`) fires only when a whole
Parquet **column chunk** is single-valued (footer `min == max`, no nulls). Data that is sorted or
clustered on a column is often constant over long **runs of pages** without the whole row group
being constant, so chunk-level detection misses it.

Parquet's **Column Index** stores per-page `min_values` / `max_values` / `null_pages` /
`null_counts`. The v3 reader already loads and parses it (`applyColumnIndex`) for predicate
push-down, so per-page "is this page constant?" is available at zero extra I/O. This lets us mark a
column constant for the row range of an individual **row subgroup** (the unit that becomes one
output `Chunk`) and materialize it as a `ColumnConst`, skipping the covered pages' reads and decode.

## Tiers (both kept)

| Tier | Source | Always present? | Exact flag? | Granularity |
|------|--------|-----------------|-------------|-------------|
| 1 (existing) | footer `ColumnMetaData.statistics` | yes | yes (`is_min_value_exact`) | whole chunk |
| 2 (this doc) | Column Index (per page) | no (optional) | no | per subgroup |

Tier 1 stays the always-on baseline and the **only** safe detector for `BYTE_ARRAY` /
`FIXED_LEN_BYTE_ARRAY` (Column Index has no per-page exactness flag; 16-byte truncation can make two
distinct strings compare equal). Tier 2 is opportunistic: only for fixed-width numeric/date/time,
only when the Column Index is already loaded, only for chunks tier 1 did not already mark constant.

## Key decision: do NOT change subgroup sizing

Aligning subgroups to page boundaries would fragment the block stream into many tiny chunks
(pages are far smaller than a row group), and a subgroup carries all columns so its size is bounded
by the non-constant columns anyway. Instead, keep subgroup boundaries exactly as today and do
**per-subgroup, per-column** detection: a column is constant for a subgroup iff every Column-Index
page overlapping the subgroup's row range is constant with the *same* value (or every such page is
`null_pages`). Chunk count is unchanged; we just catch subgroups that sit inside a constant run.

## Phases

- **Phase 0** — retain per-page constant info. `applyColumnIndex` currently discards the parsed
`parq::ColumnIndex`. Keep a compact per-page summary on `ColumnChunk` (value + `is_const` +
`all_null`), plus the page→`first_row_index` map already in the Offset Index. Populate only for
eligible types.
- **Phase 1** — `detectConstantSubchunk(column, column_info, [start_row, end_row))`: scan the pages
overlapping the range; return constant + value when all are `is_const` and share one value;
all-null when all are `null_pages`.
- **Phase 2** — call it in `intersectColumnIndexResultsAndInitSubgroups` for each subgroup /
primitive column that tier 1 didn't already mark constant; set `subchunk.is_constant` /
`is_all_null` / `constant_value` (the same fields `decodePrimitiveColumn` propagates).
`formOutputColumn` needs no change (already emits `ColumnConst`).
- **Phase 3** — skip work for constant subchunks: `decodePrimitiveColumn` skips decode on
`subchunk.is_constant`; `determinePagesToPrefetch` skips fetching a page only when it is constant
in **every** subgroup that overlaps it.
- **Phase 4** — stateless tests (page-run constant, all-null-per-page, byte-array negative/truncation
guard, `GROUP BY` correctness), with a new `ParquetConstantColumnSubchunks` ProfileEvent to prove
tier 2 fired.
- **Phase 5** — ProfileEvents comparison on clustered data: expect further `S3GetObject` /
`ParquetFetchWaitTimeMicroseconds` drops with chunk count unchanged.

## Optional force-load

By default tier 2 only uses the Column Index when it is already loaded (columns with a predicate
push-down). `input_format_parquet_use_column_index_for_constant_columns` (default off) extends it:
the Column Index (+ Offset Index) is force-loaded for eligible read columns that have no predicate,
so tier 2 can also fire on them. Cost is a small extra read of the (tiny, tail-contiguous, coalesced)
index; worthwhile mainly for sorted / low-cardinality columns. `applyColumnIndex` records per-page
constant info but skips predicate pruning when the column has no condition. A future `auto` mode
could gate this on footer signals (row-group `sorting_columns`, low compressed-bytes-per-value,
dictionary encoding stats) instead of an all-or-nothing switch.

## Cast safety (future-proofing)

The optimization only fires when the stats decoder needs no value-transforming conversion
(`SchemaConverter` sets `allow_stats` accordingly), so today `input_type == output_type` for every
constant column and no cast is applied. To keep the constant path correct if `allow_stats` is ever
generalized to allow transforming casts, `formOutputColumn` builds the single-value constant in
`input_type` and runs the same `castColumn` the per-row decode uses when `needs_cast` is set - a
no-op today, O(1) on the `ColumnConst`, and it preserves const-ness. The all-null constant is
synthesized directly in the output domain (Null / output default), so it is not cast. The mixed fill
already goes through the normal `formOutputColumn` cast, so it is future-safe too. This does not
touch the `allow_stats` decision itself.

## Guardrails

- All types, including `BYTE_ARRAY` / `FIXED_LEN_BYTE_ARRAY` strings. No truncation guard is needed
for the `min == max` case: statistics/Column-Index bounds are always valid
(`min <= every value <= max`) and truncation only widens them, so `min == max` proves a single
exact value (a truncated or multi-valued page yields `min < max`). This holds at both the chunk
level (tier 1) and per page (tier 2), so neither needs the `is_*_value_exact` flag.
- Gate on the existing `input_format_parquet_use_constant_column_optimization` setting; the
force-load above is additionally gated by
`input_format_parquet_use_column_index_for_constant_columns`.
- Not applied when `input_format_parquet_verify_checksums` is enabled (its default). A constant
chunk or subchunk is materialized without reading its data pages, so their page checksums would
never be verified. With verification requested, such chunks are decoded normally, and the
optimization applies only when verification is turned off.
- Partial-page subgroup boundaries are fine: a partial overlap of a constant page still yields that
value, as long as every overlapping page is constant with the shared value.

## Mixed-topology fill (Approach B) — implemented, default off

`input_format_parquet_fill_constant_pages` handles constant runs *shorter* than / straddling a
subgroup: `fillConstantPagesAndDecodeRest` fills single-value pages from the Column Index and decodes
only the varying ones, and `determinePagesToPrefetch` skips prefetching the filled pages (a page
shared with a subgroup that decodes it normally is still fetched — `willFillConstantPages` is
deterministic so both paths agree). The fill walks the rows that pass the filter (`row_subgroup.filter`), so it works under PREWHERE /
row-level filters and page-pruning predicates too - it produces exactly `rows_pass` values for any
filter, and because the decision no longer depends on `rows_pass` it is identical at prefetch time
and decode time (so the prefetch-skip can never drop a page the decode needs). All-null pages are
filled (nulls via the compact values + null map + `expand` path, or the output default under
`null_as_default`); a subgroup with an all-null page falls back only when the output can represent
neither null nor a default. The one remaining gate is `needs_cast`: the fill writes the Column Index
value into the `decoded_type` column, valid only when no post-decode cast applies (resolving whether
`decodeField` yields the decoded or the output value domain is build-gated). Experimental, off by
default; needs a build + correctness tests before it can be trusted.
2 changes: 2 additions & 0 deletions src/Common/ProfileEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,8 @@ The server successfully detected this situation and will download merged part fr
M(ParquetPrunedRowGroups, "The total number of row groups pruned from parquet data", ValueType::Number) \
M(ParquetReadPages, "The total number of Parquet data pages read", ValueType::Number) \
M(ParquetPrunedPages, "The total number of pages pruned from parquet data via column index", ValueType::Number) \
M(ParquetConstantColumnChunks, "The total number of parquet column chunks materialized from a single value in their min/max statistics, without reading their data pages", ValueType::Number) \
M(ParquetConstantColumnSubchunks, "The total number of parquet column subchunks (per row subgroup) materialized from a single value in their per-page column-index statistics, without decoding their data pages", ValueType::Number) \
M(ParquetDecodingTasks, "Tasks issued by parquet reader", ValueType::Number) \
M(ParquetDecodingTaskBatches, "Task groups sent to a thread pool by parquet reader", ValueType::Number) \
M(ParquetPrefetcherReadRandomRead, "The total number of reads with ReadMode::RandomRead by DB::Parquet::Prefetcher", ValueType::Number) \
Expand Down
9 changes: 9 additions & 0 deletions src/Core/FormatFactorySettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ When reading GeoParquet files, skip whole row groups and, together with `input_f
)", 0) \
DECLARE(Bool, input_format_parquet_use_offset_index, true, R"(
Minor tweak to how pages are read from parquet file when no page filtering is used.
)", 0) \
DECLARE(Bool, input_format_parquet_use_constant_column_optimization, true, R"(
When a Parquet column chunk provably holds a single value in every row (according to its min/max statistics), materialize that value directly instead of reading and decoding the column's data pages. Not applied when `input_format_parquet_verify_checksums` is enabled, because the data pages of such chunks are not read and their checksums would not be verified.
)", 0) \
DECLARE(Bool, input_format_parquet_use_column_index_for_constant_columns, false, R"(
Load the Parquet Column Index for read columns that have no predicate of their own, so the constant-column optimization can also skip data pages that are single-valued over a row subgroup (not just over a whole column chunk). Costs a small extra read of the (tiny) Column Index; only worthwhile when columns are sorted or low-cardinality. Applies only when `input_format_parquet_use_constant_column_optimization` is enabled.
)", 0) \
DECLARE(Bool, input_format_parquet_fill_constant_pages, false, R"(
Experimental. When a Parquet column is single-valued over some data pages but not the whole row subgroup, fill those pages' rows from the per-page Column Index statistics instead of reading and decoding them (mixed-topology subgroups). Extends the constant-column optimization below the subgroup granularity. Requires `input_format_parquet_use_constant_column_optimization`; disabled by default.
)", 0) \
DECLARE(Bool, input_format_parquet_verify_checksums, true, R"(
Verify page checksums when reading parquet files.
Expand Down
3 changes: 3 additions & 0 deletions src/Core/SettingsChangesHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ const VersionToSettingsChangesMap & getSettingsChangesHistory()
{
{"export_merge_tree_part_check_destination_schema_staleness_ms", 0, 5000, "New setting: how stale the latest Iceberg destination metadata version may be when each part export task checks that the destination schema and partition spec did not change since the export was scheduled."},
{"export_merge_tree_part_allow_lossy_cast", false, false, "The setting is obsolete. `EXPORT PART` and `EXPORT PARTITION` cast values the way `INSERT ... SELECT` does."},
{"input_format_parquet_use_constant_column_optimization", false, true, "New setting: when a Parquet column chunk provably holds a single value in every row (per its min/max statistics), materialize that value directly instead of reading and decoding the column's data pages."},
{"input_format_parquet_use_column_index_for_constant_columns", false, false, "New setting: load the Parquet Column Index for read columns without a predicate so the constant-column optimization can also skip data pages that are single-valued over a row subgroup."},
{"input_format_parquet_fill_constant_pages", false, false, "New experimental setting: when a Parquet column is single-valued over some data pages of a row subgroup, fill those pages from the Column Index statistics instead of reading and decoding them."},
});
addSettingsChanges(settings_changes_history, "26.8.35.20001.altinityantalya",
{
Expand Down
3 changes: 3 additions & 0 deletions src/Formats/FormatFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ FormatSettings getFormatSettings(const ContextPtr & context, const Settings & se
format_settings.parquet.page_filter_push_down = settings[Setting::input_format_parquet_page_filter_push_down];
format_settings.parquet.spatial_filter_push_down = settings[Setting::input_format_parquet_spatial_filter_push_down];
format_settings.parquet.use_offset_index = settings[Setting::input_format_parquet_use_offset_index];
format_settings.parquet.use_constant_column_optimization = settings[Setting::input_format_parquet_use_constant_column_optimization];
format_settings.parquet.use_column_index_for_constant_columns = settings[Setting::input_format_parquet_use_column_index_for_constant_columns];
format_settings.parquet.fill_constant_pages = settings[Setting::input_format_parquet_fill_constant_pages];

format_settings.parquet.enable_json_parsing = settings[Setting::input_format_parquet_enable_json_parsing];
format_settings.parquet.memory_low_watermark = settings[Setting::input_format_parquet_memory_low_watermark];
Expand Down
3 changes: 3 additions & 0 deletions src/Formats/FormatSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ struct FormatSettings
size_t footer_read_size = 0;
bool page_filter_push_down = true;
bool use_offset_index = true;
bool use_constant_column_optimization = true;
bool use_column_index_for_constant_columns = false;
bool fill_constant_pages = false;

bool enable_json_parsing = true;
bool preserve_order = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/Formats/Impl/Parquet/ReadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ void ReadManager::scheduleTask(Task task, bool is_first_in_group, MemoryUsageDif
/// Queue this subgroup's data-page reads; startPrefetch (below) issues them and charges
/// compressed bytes to the ColumnDataPrefetch budget, separate from the decode budget,
/// so many row groups prefetch ahead while only a few decode at once.
reader.determinePagesToPrefetch(column, row_subgroup, row_group, prefetches);
reader.determinePagesToPrefetch(column, row_subgroup.columns.at(task.column_idx), reader.primitive_columns.at(task.column_idx), row_subgroup, row_group, prefetches);

/// Side note: would be nice to avoid reading the dictionary if all dictionary-encoded
/// pages were filtered out (e.g. if it's a 100 MB column chunk with unique long strings,
Expand Down
Loading
Loading