Skip to content

TopN dynamic filtering for Parquet reads from object storage and data lakes - #122109

Merged
alexey-milovidov merged 15 commits into
masterfrom
object-storage-top-k-dynamic-filtering
Oct 7, 2026
Merged

alexey-milovidov merged 15 commits into
masterfrom
object-storage-top-k-dynamic-filtering

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Extends TopN dynamic filtering (ORDER BY <column> LIMIT n, use_top_k_dynamic_filtering) to Parquet reads from object storage: s3, Azure, HDFS, and data lake tables (Iceberg, Delta Lake, ...). #115798 added it for local files only. As there, the Parquet reader drops the rows that cannot enter the top-K and skips whole row groups whose statistics prove the same.

ReadFromObjectStorageStep opts in through supportsTopKDynamicFilter with the same eligibility as ReadFromFile. The sort column must be one the format reads: Hive partition and virtual columns are appended after the reader, and a column with a default expression is recomputed above it by AddingDefaultsTransform.

Object storage builds the filter info per file, and data lakes rewrite the values a file's reader returns. So StorageObjectStorageSource decides per file, and keeps the filter off where the reader's value is not the value the query sorts by:

  • a file written under an older schema, or one with a schema transform. Columns are renamed and cast after the reader, and the file can store a different column under the name the query sorts by;
  • an Iceberg identity partition column, which takes the value the manifest defines for the file.

Deletes (positional, equality, deletion vectors) only remove rows after the reader, which is compatible with the filter: it only drops rows that cannot be in the result. Floating-point sort keys and the other exclusions of #115798 apply unchanged.

The test covers plain Parquet on S3 and Iceberg tables. For each it checks that fewer rows are read and that results are identical with the optimization on and off. It also includes an Iceberg table whose sort column got its name by a rename after the first file was written: that old file stores tiny values under the same name. Handing the filter to that file (checked by building it that way) returns 65408, 65407, 65406 instead of 99999, 99998, 99997.

On MinIO, SELECT * FROM s3(...) ORDER BY k LIMIT 10 over 20M rows in random key order takes 64 ms instead of 90 ms, with 6.1M instead of 20M rows reaching the sort.

A follow-up can bring the TopK query condition cache of #122064 to object storage, starting with data lakes, whose snapshot identifies the exact file set.

Related: #115798
Related: #122064
Related: #121876

Changelog category (leave one):

  • Performance Improvement

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

ORDER BY ... LIMIT n queries over Parquet files on object storage (s3, Azure, HDFS) and over data lake tables such as Iceberg now skip the rows and row groups that cannot be in the result, as they already did for local files.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1777-master (included in 26.10 and later)

`ReadFromObjectStorageStep` now accepts the `ORDER BY <column> LIMIT n` dynamic
filter, as `ReadFromFile` does since #115798, so the Parquet reader drops rows
and skips row groups that cannot enter the top-K for `s3`, Azure, HDFS and data
lake tables. The step checks the same eligibility (a column the format reads,
with no default expression), and `StorageObjectStorageSource` keeps the filter
off for the files whose columns a data lake rewrites after the reader: files
under an older schema, files with a schema transform, and an identity
partition column.

Related: #115798
Related: #121876

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@alexey-milovidov alexey-milovidov added the pr-performance Pull request with some performance improvements label Sep 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0621fc3]

Summary: ✅


AI Review

Summary

This PR extends TopN dynamic filtering to object-storage Parquet reads and data-lake tables, and the final patch now carries the necessary per-file safety gates for schema transforms, identity partitions, mixed-format snapshots, Hive partition columns, legacy Delta, and QueryConditionCache interaction. I re-checked the current diff, the current code, every existing review thread and reply, and the all-green CI report, and I do not see any remaining correctness, safety, or contract issues in the latest version.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 0621fc3.

Metric Baseline Current Δ
Lines 88.30% 88.20% -0.10%
Functions 92.10% 91.80% -0.30%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 81/87 (93.10%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the comp-object-storage Object storage connectivity (S3/GCS/Azure) including credentials, retries, multipart, etc. label Sep 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 0621fc36d534d32d3b970d2fedab8febcf84a44b was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception; stopped retrying after the 1250 s read budget).

See the job log for details.

@clickhouse-gh

clickhouse-gh Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 32 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 89aaa03c-757b-4788-ac56-6b9d63bb9fac
  • MIRAI run: d900bb8f-7aee-4c2a-b675-4c1e81f9fb81
  • PR check IDs:
    • clickbench_84776_1791328600
    • clickbench_84782_1791328601
    • clickbench_84788_1791328600
    • tpch_adapted_1_official_84799_1791328601
    • tpch_adapted_1_official_84811_1791328600
    • tpch_adapted_1_official_84846_1791328600

…entries

With the TopN filter a row group can end up without a returned row only
because the running threshold had excluded it, and the object-storage cache
key covers just the predicate: after `SELECT ... WHERE p ORDER BY k LIMIT 3`,
a plain `SELECT count() ... WHERE p` skipped row groups and returned 54509
instead of 100000. As for local files, TopN reads now never write entries,
and consult them only while `use_query_condition_cache_for_top_k` is on.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
alexey-milovidov and others added 2 commits September 25, 2026 03:37
`schema_changed` is also set for an Iceberg file that only carries equality
deletes: it gets an initial schema, but no schema transform, and is read
under the current names. Gate the filter on the schema transform instead,
and keep it in the filter info built for such a file.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
With `allow_delta_kernel_rs = 0` the partition columns stay in the format
header and are replaced in the chunks the format returns, so the reader
compared what a data file stores under that name against the threshold: a
table whose files also store the partition column returned the rows of the
wrong partition for `ORDER BY p LIMIT 3`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`deltaLakeLocal` needs delta-kernel-rs, which is not built under MSan, so
the flaky check failed with `Unknown table function deltaLakeLocal`. The
new test carries `no-msan`, like `05237_delta_lake_legacy_partition_column_type`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122109&sha=6b8e64c332c4f9423ffc43d5fbfd1ee4673767ee&name_0=PR

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The query condition cache gates in `StorageObjectStorageSource` looked at the
shared `format_filter_info->top_k_filter`, but whether the filter is applied is
decided per file: data lake files with a schema transform, or sorted by an
identity partition column, are read without it. Such files were still kept
from writing cache entries, and with `use_query_condition_cache_for_top_k = 0`
also from reading them, which lost row-group pruning on exactly the files that
opted out of the optimization.

The per-file decision now lives in one lambda used by both the cache read and
the reader construction, and `ReaderHolder` records whether its reader applies
the filter, which gates the cache write in `generate`.

Address review: #122109 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 2 commits October 5, 2026 09:58
…ge read

A data lake table configured as `Parquet` can contain files of other formats,
such as ORC in Iceberg. `is_top_k_filter_allowed` now also requires the actual
file format to be `Parquet`, so such a file is treated as read without TopN and
keeps consulting the query condition cache with
`use_query_condition_cache_for_top_k = 0`.

Address review: #122109 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failures: 04819_dynamic_aggregate_function_state_version (https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122109&sha=2f757289d14394740df8a1ad9a8bdd7910219c7f&name_0=PR&name_1=Stateless%20tests%20(arm_binary%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel), 1/26 reruns failed; #116341 was closed but it still flakes) and 05237_with_expression_alias_scope_descendant (https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122109&sha=2f757289d14394740df8a1ad9a8bdd7910219c7f&name_0=PR&name_1=Stateless%20tests%20(arm_binary%2C%20s3%20storage%2C%20DBReplicated%2C%20parallel%2C%201%2F2)), and provide a fix in a separate PR. If the fix is already in progress, link it here. Both are unrelated to this PR. The Build (amd_fuzzers) relocation overflow is fixed on master by #123738, which is now merged into this branch.

@groeneai

groeneai commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Both have fixing PRs:

… column

Since f5ca04b a structure of only Hive partition columns makes the
format read them from the data file, while the query takes their values
from the path. `ReadFromObjectStorageStep::supportsTopKDynamicFilter`
then armed the filter on the file's values, so `ORDER BY key LIMIT 3`
over `b=1/key={10,9}/` returned `10` instead of `9`. Mirror the
`formatReadsHivePartitionColumns` guard of `ReadFromFile` and add a test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… reader declines TopN for

The Parquet reader does not install `__topKFilter` for a file that does not
store the sort column, so such a file is read as without TopN. Decide whether
a file may populate the query condition cache by asking the input format
(`IInputFormat::isTopKFilterApplied`) instead of by the presence of
`FormatFilterInfo::top_k_filter`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 3 commits October 6, 2026 21:23
…lumn`

Use an `S3` table instead of the `s3` table function, whose UUID changes
with every query, so that the query condition cache entries are found, and
write `Int64` columns matching the declared structure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@alexey-milovidov alexey-milovidov left a comment

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.

This is good.

@alexey-milovidov alexey-milovidov self-assigned this Oct 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 7, 2026
Merged via the queue into master with commit 15c874a Oct 7, 2026
150 checks passed
@alexey-milovidov
alexey-milovidov deleted the object-storage-top-k-dynamic-filtering branch October 7, 2026 22:10
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 7, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@alexey-milovidov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 20, 2026
Conflicts with the TopN query condition cache key (ClickHouse#122109 and the
`StorageFile` TopK key), the `Parquet` columns cache and the lazy column
chunk metadata (ClickHouse#124364):

- `StorageFile`: row blocks are read only from the plain predicate key,
  in the same consultation as its row groups (one Hits/Misses event), so
  `readFile` gained `increment_profile_events`.
- `StorageFile`: master moved the `!top_k_filter` check out of the
  write-side condition to write TopN entries under the TopK key. The
  row-block write keeps it, otherwise a TopN read would store
  threshold-dependent row blocks under the plain key.
- `StorageObjectStorageSource` merged cleanly: the row-block write is
  inside the per-file `!isTopKFilterApplied()` block of ClickHouse#122109.
- `Reader::file_metadata` is an accessor now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-object-storage Object storage connectivity (S3/GCS/Azure) including credentials, retries, multipart, etc. pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants