Skip to content

Estimate filter of iceberg read on manifest column statistics - #124448

Closed
antoniofilipovic wants to merge 37 commits into
afilipovic-iceberg-deleted-rowsfrom
afilipovic-iceberg-filter-selectivity
Closed

antoniofilipovic wants to merge 37 commits into
afilipovic-iceberg-deleted-rowsfrom
afilipovic-iceberg-filter-selectivity

Conversation

@antoniofilipovic

@antoniofilipovic antoniofilipovic commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Related: #120440
Related: #123085
Related: #123553
Related: #124395
Related: #124857

The fourth PR for #120440, stacked on #124395. With use_iceberg_manifest_statistics, the rows that the filter of an Iceberg read keeps are now estimated on the merged manifest statistics of its remaining data files, as for MergeTree tables with column statistics. Before, a filter that pruned no file made the read unknown, which join reordering costs as 1 row, and every join above it became unknown too; a filter that pruned files reported all rows of the remaining files.

What changes

  • ManifestColumnStatistics::finalize estimates the read's own filter once, right after the manifest walk. That is the read's filter_actions_dag, which holds the WHERE conditions, the PREWHERE and the row policy pushed into it before join reordering. It calls estimateRelationProfile with it on the estimator built from the remaining files, whose row count is the live rows after the deletes of Count deleted rows count for single files in iceberg join ordering #124395, so nothing needs scaling afterwards. The profile of the estimate becomes the filtered one (filter_estimated), with the numbers of distinct values of Include the column statistics of Iceberg reads from manifest files for join reordering #123553 clamped to the filtered rows. No estimator is kept in the estimate.
  • Equality and IN need a number of distinct values: Basic statistics answer = only for the default value, so without one they take the default 1%. The number of distinct values of rule 1 (identity partition values) and of rule 2 below the clamp (max - min + 1 of an integer or date column) is given to the estimator as a cardinality with the new ColumnStatistics::setCardinality, so = and IN divide the rows by it, as with a uniq statistic. Guessed numbers (rule 2 clamped to the rows, rules 3 and 4) are not.
  • Ranges (<, >, BETWEEN) interpolate between the merged min and max, as for MergeTree.
  • A filtered estimate is imprecise, also when it comes out as 0. Without statistics of any requested column, a filter that prunes no file still gives no estimate.

Measured

A catalog of 87 queries compares the Iceberg estimates with a MergeTree twin that holds the same data with the default column statistics (auto_statistics_types = 'basic, uniq_v2': min/max, NULL counts and a uniq sketch). The data is a TPC-H-like schema written with ClickHouse's Iceberg writer: 1M orders in 10 data files, 3M lineitems, 100k customers and smaller dimensions, plus a month-partitioned copy of orders and copies with 10%, 49% and 91% of the rows deleted. For each query the harness compares the estimate of every relation and of every join in the chosen plan with the actual count, and records the rows put into hash tables (JoinBuildTableRowCount). A query is off when the Iceberg estimate, or the hash table rows of its plan, is at least 3x worse than with the MergeTree twin. "Before" is this stack without this PR. The table was measured on the first version of this stack (2026-10-08), which estimated the filter in estimateReadRowsCount on every call; the numbers of 05326 are unchanged by the rework, the catalog was not re-run.

Query family Queries Within 3x of MergeTree, before → after
single-table filters: equality, IN, ranges, IS NULL, LIKE, functions, OR 22 5 → 20
the same filters on the month-partitioned table 7 3 → 7
deleted rows 7 7 → 7
join key types: dense and sparse integers, String, Float64, Decimal, Date, DateTime64, mostly NULL, expression, composite 17 11 → 11
2-way joins with a filtered side 7 1 → 3
multi-way shapes: star, chain, TPC-H Q3 and Q5 shapes, 6- and 7-way, LEFT JOIN, aggregation subquery 17 5 → 9
3- and 4-way joins sensitive to the number of distinct values of the keys 6 0 → 0
aggregation below a join 4 0 → 0
total 87 32 → 57

What stays off is the number of distinct values of keys other than identity partitions and dense integers and dates.

Known limitations

  • String and floating-point columns have no counted number of distinct values, so = and IN on them take the default 1% (MergeTree uses its uniq statistic). The remaining gaps to MergeTree in the stress catalog all come from these missing numbers of distinct values.
  • ClickHouse's Iceberg writer writes no bounds for floating-point columns, so ranges on them take the default 33%.
  • The deleted rows are taken as independent of the filter.
  • Only the read's own filter is estimated: a condition that is not pushed into the read, such as one above a LIMIT, is not.
  • Rule 2 is an upper bound: an integer column with large gaps makes = more selective than it is.

Changelog category (leave one):

  • Improvement

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

With use_iceberg_manifest_statistics, join reordering estimates the rows kept by the filter of an Iceberg read from the manifest column statistics, as for MergeTree tables with column statistics, instead of treating a filter that prunes no data file as giving no estimate.


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

@antoniofilipovic
antoniofilipovic force-pushed the afilipovic-iceberg-filter-selectivity branch from 3472a16 to b3d01ee Compare October 7, 2026 15:01
@antoniofilipovic
antoniofilipovic force-pushed the afilipovic-iceberg-deleted-rows branch from 436f9ad to 64634df Compare October 7, 2026 15:01
@clickhouse-gh clickhouse-gh Bot added the comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. label Oct 7, 2026
@antoniofilipovic
antoniofilipovic force-pushed the afilipovic-iceberg-deleted-rows branch from 64634df to e552b36 Compare October 7, 2026 15:29
@antoniofilipovic
antoniofilipovic force-pushed the afilipovic-iceberg-filter-selectivity branch from b3d01ee to e54b543 Compare October 7, 2026 15:29
@scanhex12 scanhex12 self-assigned this Oct 7, 2026
@antoniofilipovic
antoniofilipovic marked this pull request as ready for review October 8, 2026 08:19
@clickhouse-gh

clickhouse-gh Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [efdc6f4]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_msan, flaky check) FAIL
Server liveness check failed FAIL cidb
MemorySanitizer: use-of-uninitialized-value (STID: 6001-5651) FAIL cidb
Stateless tests (amd_llvm_coverage, s3 storage, DBReplicated, parallel, 1/3) FAIL
05298_iceberg_manifest_stats_ndv FAIL cidb
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) FAIL
05298_iceberg_manifest_stats_ndv FAIL cidb
02918_optimize_count_for_merge_tables NOT_FAILED cidb
02156_storage_merge_prewhere NOT_FAILED cidb
02402_merge_engine_with_view NOT_FAILED cidb
Stateless tests (amd_llvm_coverage, AsyncInsert, s3 storage, parallel) FAIL
05298_iceberg_manifest_stats_ndv FAIL cidb
03008_deduplication_cases_from_docs NOT_FAILED cidb
02912_ingestion_mv_deduplication NOT_FAILED cidb
00952_insert_into_distributed_with_materialized_column NOT_FAILED cidb
03008_deduplication_insert_into_partitioned_table NOT_FAILED cidb
Stateless tests (amd_debug, parallel) FAIL
05298_iceberg_manifest_stats_ndv FAIL cidb
Stateless tests (amd_debug, distributed plan, s3 storage, parallel) FAIL
05298_iceberg_manifest_stats_ndv FAIL cidb
Stateless tests (arm_binary, parallel) FAIL
05298_iceberg_manifest_stats_ndv FAIL cidb
Stateless tests (arm_asan_ubsan, azure, parallel) FAIL
05298_iceberg_manifest_stats_ndv FAIL cidb
Stateless tests (amd_llvm_coverage, 1/3) FAIL
05298_iceberg_manifest_stats_ndv FAIL cidb
Stress test (amd_msan) FAIL
MemorySanitizer: use-of-uninitialized-value (STID: 6001-5651) FAIL cidb

AI Review

Summary

This PR teaches the Iceberg manifest-statistics path to estimate filter selectivity and to feed counted distinct-value information into the selectivity estimator, with new stateless coverage around equality, IN, ranges, NULLs, partitions, and delete-file scaling. The main improvement is still incomplete, though: after filter push-down, join reordering can see the predicate only through the source step’s pushed-down filter DAG, and the new Iceberg branch still ignores that shape when deciding whether it can estimate the filter.

Findings

❌ Blockers

  • [src/Processors/QueryPlan/Optimizations/RelationStatisticsEstimator.cpp:241] The new Iceberg path only sets estimate_filter from the recursive filter argument and prewhere_node, but join reordering runs after pushDownFilter, which can replace the original FilterStep with an ExpressionStep and leave the predicate only in reading->getFilterActionsDAG(). In that plan shape has_filter is still true, yet estimate_filter is false, so a filter that prunes no file still returns unknown, and a filter that prunes some files falls back to the full surviving-file row count. That misses a common optimized shape of the exact feature this PR is adding. Suggested fix: pass the pushed-down source filter DAG (and the row-level filter DAG as well) into estimateRelationProfile, and add a regression where the read keeps only a fully pushed-down filter.
Final Verdict

❌ Changes requested — the core Iceberg filter-selectivity improvement still disappears once the predicate is fully pushed into the source step.

LLVM Coverage Report

Measured on commit efdc6f4.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 217/237 (91.56%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 8, 2026
if (const auto prewhere_info = reading->getPrewhereInfo())
prewhere_node = prewhere_info->prewhere_actions.tryFindInOutputs(prewhere_info->prewhere_column_name);
/// As for MergeTree, column statistics estimate the filter on the remaining files.
const bool estimate_filter = estimate->column_statistics && (filter || prewhere_node);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

estimate_filter only looks at the recursive filter argument and prewhere_node, but after the main pushDownFilter pass the predicate can live only in reading->getFilterActionsDAG() — when everything was pushed, filterPushDown.cpp replaces the original FilterStep with an ExpressionStep. estimateReadFromDataLakeMetadata() already prunes manifests against that DAG (ReadFromObjectStorageStep.cpp:124), so in this shape the new branch either returns unknown for filters that prune no file or falls back to estimateRelationProfile() with no predicate and reports the full surviving-file row count. That misses the common optimized plan shape this PR is trying to improve. Please feed the source filter DAG (and ideally the row-level filter as well) into estimateRelationProfile, and add a regression where the read keeps only a pushed-down filter.

if (auto it = estimate->num_distinct_values.find(name); it != estimate->num_distinct_values.end())
column.num_distinct_values = it->second;
stats.column_stats = std::move(profile.column_stats);
column.num_distinct_values = std::clamp<UInt64>(it->second, 1, std::max<UInt64>(estimated_rows, 1));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we estimate the number of different values as just number of rows from filter? Can't we estimate it as size of dictionary encoding in parquet file, not number of rows?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for late reply, I was dealing with other comments in PRs before in the stack, so it took some time.

Aha, the NDV is not number of rows. What happens here is that we only put a cap on NDV to the number of rows.
For NDV value we do one of the following, we set NDV depending on the following case:

  1. if key is identity partition value then we can set to number of rows in the file, exactly,
  2. max - min + 1 of the bounds
  3. column_sizes / type width
  4. otherwise MergeTree's 10% of number of rows, which by itself is a guess

The cap (std::clamp) keeps it at most the rows left after the filter, since a column can't have physically more distinct values than number of rows.

MergeTree does the same under a filter (std::min(result.rows, estimator.estimateCardinality()) in ConditionSelectivityEstimator::estimateRelationProfileImpl). For example, WHERE a IN (1, 2, 3) on 10000 rows estimates 300 rows; a keeps its NDV of 100, and only k, whose NDV is 1000, is capped at 300.

From what I understand from claude, the parquet dictionary size isn't available at planning without reading the data files. Iceberg manifests don't carry it (distinct_counts in the spec is deprecated, "Do not write"). In Parquet it's num_values of the dictionary page header, which is in the column chunk, not the footer, so a read per column chunk of every file.

`ColumnStatistics::setCardinality` stores a number of distinct values known outside the statistics objects, and
  `hasCardinality`, `estimateCardinality` and `estimateEqual` use it as they use a `uniq` sketch. `ManifestColumnStatistics`
  sets it for identity partition values and for `max - min + 1` below the clamp, so equality and `IN` can divide the rows by
  it. No visible change yet: the estimator is still called without a filter, and the NDV override gives the same numbers.
With `use_iceberg_manifest_column_statistics`, `estimateReadRowsCount` estimates the filter and `PREWHERE` of an Iceberg
  read on the merged statistics of its remaining data files, as for MergeTree tables with column statistics, and scales the
  result by the share of live rows. A filter that prunes no file gives no estimate only without column statistics.
  New test 05326; 05292 T5 now shows the filtered estimate and checks the data lake hint line without column statistics.
@antoniofilipovic
antoniofilipovic force-pushed the afilipovic-iceberg-filter-selectivity branch from 6849d13 to 04f7496 Compare October 19, 2026 10:24
@antoniofilipovic
antoniofilipovic force-pushed the afilipovic-iceberg-deleted-rows branch from dca73bf to 72671f0 Compare October 19, 2026 10:24
@antoniofilipovic
antoniofilipovic removed this pull request from stack #124571 October 20, 2026 15:57
antoniofilipovic added a commit that referenced this pull request Oct 20, 2026
…tistics

As MergeTree does with its column statistics, `estimateReadRowsCount` estimates the filter above an Iceberg read and its
`PREWHERE`, and also its row policy, on the merged manifest statistics of the data files left after pruning by the read's
own filter. The estimate keeps the estimator (`DataLakeReadEstimate::estimator`), so the conditions of each caller are
estimated without reading the manifests again. A filter above a `LIMIT`, an aggregation or a window function, which the
read does not hold, is now estimated too. Without column statistics, conditions that prune no file still give no estimate.

The read's own filter is not ANDed with them: it repeats the filter above the read, and a condition that is not a range
would be counted twice.

The test of #124448 (05326 there) moves here as 05428, without the deletes
case and with a filter above `LIMIT`, a row policy and a condition counted once. 05292 T5 and 05293 T3, T5, T8 now show
the estimated filter.

Addresses #123553 (comment)
antoniofilipovic added a commit that referenced this pull request Oct 20, 2026
The filter of a read is estimated on the rows left after the deleted rows the metadata counts: 2000 rows, 500 deleted by
a file-scoped delete, `v = 1` with 5 values gives 300. This case was in the test of
#124448, which moved into the PR below without it.
@antoniofilipovic
antoniofilipovic force-pushed the afilipovic-iceberg-deleted-rows branch from 72671f0 to 564d984 Compare October 20, 2026 15:57
@antoniofilipovic

Copy link
Copy Markdown
Member Author

Moved to #123553

antoniofilipovic added a commit that referenced this pull request Oct 23, 2026
…tistics

As MergeTree does with its column statistics, `estimateReadRowsCount` estimates the filter above an Iceberg read and its
`PREWHERE`, and also its row policy, on the merged manifest statistics of the data files left after pruning by the read's
own filter. The estimate keeps the estimator (`DataLakeReadEstimate::estimator`), so the conditions of each caller are
estimated without reading the manifests again. A filter above a `LIMIT`, an aggregation or a window function, which the
read does not hold, is now estimated too. Without column statistics, conditions that prune no file still give no estimate.

The read's own filter is not ANDed with them: it repeats the filter above the read, and a condition that is not a range
would be counted twice.

The test of #124448 (05326 there) moves here as 05428, without the deletes
case and with a filter above `LIMIT`, a row policy and a condition counted once. 05292 T5 and 05293 T3, T5, T8 now show
the estimated filter.

Addresses #123553 (comment)
w62 pushed a commit to w62/ClickHouse that referenced this pull request Nov 1, 2026
…tistics

As MergeTree does with its column statistics, `estimateReadRowsCount` estimates the filter above an Iceberg read and its
`PREWHERE`, and also its row policy, on the merged manifest statistics of the data files left after pruning by the read's
own filter. The estimate keeps the estimator (`DataLakeReadEstimate::estimator`), so the conditions of each caller are
estimated without reading the manifests again. A filter above a `LIMIT`, an aggregation or a window function, which the
read does not hold, is now estimated too. Without column statistics, conditions that prune no file still give no estimate.

The read's own filter is not ANDed with them: it repeats the filter above the read, and a condition that is not a range
would be counted twice.

The test of ClickHouse#124448 (05326 there) moves here as 05428, without the deletes
case and with a filter above `LIMIT`, a row policy and a condition counted once. 05292 T5 and 05293 T3, T5, T8 now show
the estimated filter.

Addresses ClickHouse#123553 (comment)
fm4v pushed a commit to BoloniniD/ClickHouse that referenced this pull request Nov 3, 2026
The filter of a read is estimated on the rows left after the deleted rows the metadata counts: 2000 rows, 500 deleted by
a file-scoped delete, `v = 1` with 5 values gives 300. This case was in the test of
ClickHouse#124448, which moved into the PR below without it.
@clickhouse-gh clickhouse-gh Bot added comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... and removed comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants