Repository navigation
Estimate filter of iceberg read on manifest column statistics - #124448
antoniofilipovic wants to merge 37 commits into
Conversation
3472a16 to
b3d01ee
Compare
436f9ad to
64634df
Compare
64634df to
e552b36
Compare
b3d01ee to
e54b543
Compare
|
Workflow [PR], commit [efdc6f4] Summary: ❌
AI ReviewSummaryThis 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, Findings❌ Blockers
Final Verdict❌ Changes requested — the core Iceberg filter-selectivity improvement still disappears once the predicate is fully pushed into the source step. LLVM Coverage ReportMeasured on commit efdc6f4.
Changed lines: Changed C/C++ lines covered: 217/237 (91.56%) · Uncovered code |
| 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); |
There was a problem hiding this comment.
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.
fba9e03 to
efdc6f4
Compare
e552b36 to
407ddbd
Compare
| 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)); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
- if key is identity partition value then we can set to number of rows in the file, exactly,
- max - min + 1 of the bounds
- column_sizes / type width
- 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.
efdc6f4 to
12243f8
Compare
05e80d2 to
dca73bf
Compare
12243f8 to
6849d13
Compare
…ceberg row estimate
`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.
6849d13 to
04f7496
Compare
dca73bf to
72671f0
Compare
…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)
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.
72671f0 to
564d984
Compare
|
Moved to #123553 |
…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)
…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)
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.
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::finalizeestimates the read's own filter once, right after the manifest walk. That is the read'sfilter_actions_dag, which holds theWHEREconditions, thePREWHEREand the row policy pushed into it before join reordering. It callsestimateRelationProfilewith 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.INneed a number of distinct values:Basicstatistics 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 + 1of an integer or date column) is given to the estimator as a cardinality with the newColumnStatistics::setCardinality, so=andINdivide the rows by it, as with auniqstatistic. Guessed numbers (rule 2 clamped to the rows, rules 3 and 4) are not.<,>,BETWEEN) interpolate between the merged min and max, as for MergeTree.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 auniqsketch). 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 inestimateReadRowsCounton every call; the numbers of 05326 are unchanged by the rework, the catalog was not re-run.IN, ranges,IS NULL,LIKE, functions,ORString,Float64,Decimal,Date,DateTime64, mostly NULL, expression, compositeLEFT JOIN, aggregation subqueryWhat stays off is the number of distinct values of keys other than identity partitions and dense integers and dates.
Known limitations
Stringand floating-point columns have no counted number of distinct values, so=andINon them take the default 1% (MergeTree uses itsuniqstatistic). The remaining gaps to MergeTree in the stress catalog all come from these missing numbers of distinct values.LIMIT, is not.=more selective than it is.Changelog category (leave one):
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]