Skip to content

Fix a filter on a Hive partition column pruning by the data file's column of the same name - #123903

Merged
PedroTadim merged 4 commits into
ClickHouse:masterfrom
groeneai:hive-partition-filter-not-on-file-column
Oct 9, 2026
Merged

PedroTadim merged 4 commits into
ClickHouse:masterfrom
groeneai:hive-partition-filter-not-on-file-column

Conversation

@groeneai

@groeneai groeneai commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #123717
Related: #123381

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a WHERE condition or a row policy on a Hive partition column (use_hive_partitioning = 1) returning too few rows when the Parquet data file also has a column with that name and other values: Parquet row-group, page and dictionary pruning used the file's values instead of the path's. The same applied to a virtual column such as _file when the data file has a physical column with that name. Affects file, url, s3 and other object storages.

Description

On 26.8 and 26.9, with key=9/data.parquet storing key = 10:

SET use_hive_partitioning = 1, optimize_count_from_files = 0;
SELECT key, count() FROM file('key=9/data.parquet', Parquet, 'key Int64, v Int64') GROUP BY key; -- 9 1000
SELECT count() FROM file('key=9/data.parquet', Parquet, 'key Int64, v Int64') WHERE key = 9;      -- 0, expected 1000

26.7 returns 1000. Partition and virtual columns are added by the source after the format reads the file, but file, url and object storage passed the whole pushed-down filter to the format. Since #104435, FormatFilterInfo adds every filter input missing from the format header to the key condition, so the Parquet reader resolved key to the file's own column and skipped every row group. #123381 covered only a structure of partition columns alone.

ReadFromFormatInfo::getFormatFilter now gives the format the filter without the conjuncts on Hive partition and requested virtual columns. key = 9 AND v < 10 still prunes on v; the file iterator and the Filter step keep the full condition. Iceberg keeps the _row_id / _last_updated_sequence_number conjuncts, which it reads from the data file.

With less filter reaching the format, the count() row cache must not be written when the format skipped rows through a PREWHERE, a join runtime filter or the top-K filter, so FormatFilterInfo::hasFilter now covers them. #122835 makes the same hasFilter change.

The test covers file, url, s3 with the query condition cache, _file, a row policy, the row count cache, and that pruning on file columns still happens. It fails on master in 15 queries and passes 50/50 randomized runs. A subcolumn of a partition column (key.null) is still read from the file; that is #123718.


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

Version info

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

groeneai and others added 4 commits October 4, 2026 17:35
…t the data file

A Hive partition column and a requested virtual column are added by the source
after the format produced the chunk, but the read steps of `file`, `url` and
object storage passed the whole pushed-down filter to the format. Since ClickHouse#104435
`FormatFilterInfo::initKeyConditionOnce` adds every filter input missing from
the format header to the KeyCondition, so the Parquet reader resolved such a
column to the data file's own column of the same name and pruned row groups,
pages and dictionaries by the file's values. When the file stores other values
than the path (or a physical `_file` column), `WHERE key = 9` on `key=9/` and a
row policy on the partition column returned no rows, while `SELECT key` showed
9 for every row. Regression since 26.8.

`ReadFromFormatInfo::getFormatFilter` now gives the format the filter without
the conjuncts that reference a Hive partition column or a requested virtual
column (`splitFilterDagForAllowedInputs` with a partial result, so `key = 9 AND
v < 10` still prunes on `v`). The files iterator keeps the full filter, and the
`Filter` step above still applies all of it. It replaces the
`formatReadsHivePartitionColumns() ? nullptr : filter_actions_dag` rule of
ClickHouse#123381, which covered only the structure with partition columns alone. Data
lakes keep their virtual-column conjuncts, because Iceberg reads its row
lineage columns from the data file; they have no Hive partition columns, so
their filter is unchanged.

Closes ClickHouse#123717

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`getFormatFilter` used `splitFilterDagForAllowedInputs`, which also drops
non-deterministic conjuncts. A PREWHERE or a pushed row policy is part of
`filter_actions_dag` and is applied by the format a second time through
`prewhere_info` / `row_level_filter`, so `PREWHERE v < 100 + 0 * rand() WHERE
key = 9` left the format with no filter while it still filtered rows: with only
`count()` requested the Parquet reader threw NOT_FOUND_COLUMN_IN_BLOCK, otherwise
the source cached the post-PREWHERE row count as the file's row count and a
later trivial `count()` returned it. The filter is now split into its AND atoms
and only the atoms that read a Hive partition or virtual column are dropped;
PREWHERE and row-policy atoms read only file columns, so they always survive.

Data lakes were exempt from the virtual-column strip as a whole, but Iceberg
reads only `_row_id` and `_last_updated_sequence_number` from the data file.
After `RENAME COLUMN _path TO old_path` (or `DROP COLUMN _path`) an old data
file still stores a `_path` column, and `WHERE _path != 'evil'` pruned every
row group on it. Only the row lineage columns are kept now.

The test is renumbered to 05325, gets an Iceberg arm, a count-from-files
cache arm and a query condition cache arm on an `S3` table (a table function
has no UUID, so the cache never engages for it).

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

The count-from-files cache was written whenever the format got no filter
DAG. A join runtime filter (moved into the read's PREWHERE after the DAG is
collected) and the top-K dynamic filter also make the format skip rows, so
once a WHERE on a partition or virtual column no longer reaches the format,
a later trivial count() returned the reduced row count. The top-K case was
already wrong on master without any WHERE. `FormatFilterInfo::hasFilter`
now also covers the row-level filter, PREWHERE and the top-K filter, and
the object storage gate uses it.

A structure of partition columns alone gets no format filter again: its
columns are in the format header, so an `indexHint` on them (whose
arguments are not DAG children) still pruned by the file's values.

Only Iceberg keeps the row lineage conjuncts; other data lakes add every
virtual column after the format.

The test gets `no-replicated-database` (the replicated DDL worker does not
see the temporary table `IcebergLocal` reads), an eviction-proof query
condition cache control, and waits a second after writing its files because
the count cache ignores an entry registered in the second the file was
written.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Restore master's wording of the formatReadsHivePartitionColumns() comment (it still skips filters).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-follow-up PR origin: groeneai's own out-of-scope finding during its PR work labels Oct 4, 2026
@groeneai

groeneai commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 3 rounds, 11 findings plus my own review; final head 6fa177e).

# Sev Finding Verdict Evidence / action
1 ❌ With the partition conjunct gone the format got no filter DAG, so the row-count cache stored the count reduced by a join runtime filter or the top-K filter AGREE, fixed @ 6fa177e FormatFilterInfo::hasFilter covers PREWHERE, row policy and top-K; all three cache gates use it
2 ⚠️ Dropping non-deterministic atoms left a PREWHERE without a format filter (wrong cached count, NOT_FOUND_COLUMN_IN_BLOCK) AGREE, fixed @ 8012399 strip by AND atoms, keep every atom that reads only file columns
3 ⚠️ Data lakes kept every virtual conjunct; an Iceberg _path renamed away still pruned AGREE, fixed @ 8012399, 6fa177e only Iceberg keeps _row_id / _last_updated_sequence_number
4 ⚠️ indexHint over a structure of partition columns alone pruned by the file AGREE, fixed @ 6fa177e that shape gets no format filter, as before
5 ⚠️ Test: query condition cache arm never used the cache; replicated database; same-second cache masking AGREE, fixed @ 8012399, 6fa177e ENGINE = S3 table, no-replicated-database, sleep(1) after the fixtures
6 ⚠️ Iceberg ORC files could prune on a physical _row_id DISAGREE only the Parquet reader resolves filter columns outside the header; ORC maps them to YES_NO_NULL (NativeORCBlockInputFormat.cpp:783-790)
7 ⚠️ The cache check accepts a miss DISAGREE the result line is asserted on every run; a hit assertion is flaky in a parallel test (every in-tree hit check is no-parallel), and the test stays parallel
8 💡 Top-K arm has no activation check; _file and row-policy arms have no negative arm noted both arms fail without the fix (measured); the proposed negative arms also pass on master

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-self-slot-58:20261004-211505

@groeneai

groeneai commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. key=9/data.parquet storing key = 10, WHERE key = 9 with use_hive_partitioning = 1, optimize_count_from_files = 0: 0 on master, 26.8.10.6 and 26.9.1.1629, 1000 on 26.7.17.7.
b Root cause explained? Partition and virtual columns are added after the format, but the whole pushed-down filter reached FormatFilterInfo; since #104435 it turns every filter input missing from the header into a key-condition column, which the Parquet reader resolves to the file's same-named column and prunes by its values. Deleting that loop alone gives 1000.
c Fix matches root cause? Yes. The format gets the filter minus exactly the AND conjuncts that read a partition or virtual column; every other conjunct, including a non-deterministic PREWHERE or row-policy one, is kept as is. The file iterator and the Filter step keep the full condition. No pushdown setting is toggled. The count-from-files cache is written only when the format may not skip rows at all (no filter, row policy, PREWHERE or top-K filter).
d Test intent preserved / new tests added? New test 05325_hive_partitioning_filter_on_partition_column, one query per mechanism (incl. an Iceberg table after RENAME COLUMN _path, a query condition cache hit, the count-from-files cache after a PREWHERE, a join runtime filter and the top-K filter, and indexHint over a partition-only structure), plus a check that file-column pruning still fires. 05316_hive_partitioning_read_only_partition_columns unchanged and passing.
e Both directions demonstrated? Yes, on Build-ID-verified servers: master fails 15 queries of the new test, the previous head fails the 3 new ones, the fix passes it and 50/50 randomized runs.
f Fix is general across code paths? One helper used by all three steps that pass the user filter to a format (file, url, object storage incl. *Cluster and S3/Azure/HDFS tables). Per-file object-storage copies derive from the stripped filter; ORC was already correct; Iceberg keeps only the _row_id / _last_updated_sequence_number conjuncts (it reads them from the file); a structure of partition columns alone gets no format filter, as before.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked Int64, Nullable, String, LowCardinality(String), Date, inferred structure; =, IN, NOT, OR, AND, range, IS NOT NULL, PREWHERE + WHERE (also non-deterministic), row policy, indexHint, join runtime filter, top-K with and without WHERE; _file, _path, _row_number; Iceberg rename and drop of _path.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; a filter without partition or virtual columns reaches the format unchanged. partition OR file-column loses format pruning, as before 26.8.
i Invariants and contracts preserved? The format filter only becomes weaker (a subset of AND conjuncts). The count-from-files cache is written only when the format had no filter, row policy, PREWHERE or top-K filter, so a count the format reduced is never cached; a plain read still caches it. Row-level filter, PREWHERE and the top-K guard are unchanged.

Session id: cron:clickhouse-impl-self-slot-57:20261004-204000

@clickhouse-gh

clickhouse-gh Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [634c083]

Summary: ✅


AI Review

Summary

This PR fixes a real regression in file-like and object-storage reads with use_hive_partitioning = 1: format-level pruning was allowed to resolve Hive partition or reader-added virtual columns against same-named physical file columns, which could incorrectly prune matching rows. The patch narrows the format-visible filter to only conjuncts that read true file columns, keeps Iceberg row-lineage columns as an explicit exception, and plugs the related row-count cache hazard when filtering happens through PREWHERE, row policies, or Top-K. The new stateless coverage exercises the affected file, url, s3, query-condition-cache, row-policy, and count-cache paths, and the current Praktika report is all-green. I did not find an unresolved correctness or contract issue in the current diff.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 634c083.

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

Changed lines: Changed C/C++ lines covered: 51/53 (96.23%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 4, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-table-functions Table functions for temporary read/write to external systems (FROM ... tableFunction()). label Oct 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 634c08308 with master 7cbb8ad28 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 796d29517; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

128 translation units recompiled, 1196 s compile time in total, 128 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×0.95 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -69.2 s (-5%) in total before that adjustment.

Job report

@PedroTadim PedroTadim self-assigned this Oct 5, 2026
@PedroTadim

Copy link
Copy Markdown
Member

@al13n321 maybe I need some help. This PR is a little too large for me, but maybe ok for you

bool FormatFilterInfo::hasFilter() const
{
return filter_actions_dag != nullptr;
return filter_actions_dag || row_level_filter || prewhere_info || top_k_filter;

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.

@groeneai can you explain this line change?

@PedroTadim PedroTadim Oct 6, 2026 •

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.

what about the join runtime filters? should it be included here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

hasFilter() decides whether the source may store the number of rows it read as the file's row count in the count-from-files cache (StorageFile.cpp, StorageURL.cpp, StorageObjectStorageSource.cpp). A later trivial count() returns that number, so it is right only when the format returned every row of the file. Besides filter_actions_dag, the format also drops rows by the row policy, PREWHERE and the top-K threshold.

Before this PR, the WHERE kept the DAG non-null in such queries. Now the conjuncts on Hive partition and virtual columns are stripped, so WHERE key = 9 on a partition column plus a PREWHERE, a join runtime filter or top-K reaches the format with no DAG, while those still drop rows. On the previous commit 8012399 (without this line), after ... JOIN ... ON f.v = t.v WHERE f.key = 9 the trivial count() of the 1000-row file returned 10, and after WHERE key = 9 ORDER BY v LIMIT 3 it returned 100. The last two blocks of 05325_hive_partitioning_filter_on_partition_column.sql cover all three. Without any WHERE the top-K case is already wrong on master (#123716), and this line fixes it too; #122835 makes the same change.

Join runtime filters: yes, they are covered, through prewhere_info. The runtime filter is added (optimizeTree.cpp:349) after the read's filter_actions_dag is collected (:273), so it is never in the DAG. optimizePrewhere (:448) then moves it into the read's PREWHERE, and the format applies it from prewhere_info (master's comment at FormatFilterInfo.cpp:77-79 relies on the same order). When it is not moved, it stays a Filter step above the read and the format returns every row, so the cached count is right. The test asserts that __applyFilter is in the file read's PREWHERE before checking the count.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 10, 2026
… revert

The PR's sequential stateless lanes failed in 05243_query_condition_cache_top_k_parquet,
a master regression from ClickHouse#123903. Master reverted it in ClickHouse#124977 (c380b02), so this
merge brings the revert in. Master did not touch any file this PR changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
clickhouse-gh Bot pushed a commit that referenced this pull request Oct 10, 2026
Edited by the nightly changelog CI job following .claude/skills/edit-changelog/SKILL.md.

Changelog-revert: 124977 123903 Revert "Fix a filter on a Hive partition column pruning by the data file's column of the same name"
Changelog-deleted-entry: 123903 [Bug Fix (user-visible misbehavior in an official stable release)] [raw] * Fixed a `WHERE` condition or a row policy on a Hive partition column (`use_hive_partitioning = 1`) returning too few rows when the Parquet data file also has a column with that name and other values: Parquet row-group, page and dictionary pruning used the file's values instead of the path's. The same applied to a virtual column such as `_file` when the data file has a physical column with that name. Affects `file`, `url`, `s3` and other object storages. [#123903](#123903) ([Groene AI](https://github.com/groeneai)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-table-functions Table functions for temporary read/write to external systems (FROM ... tableFunction()). groeneai-origin-follow-up PR origin: groeneai's own out-of-scope finding during its PR work pr-bugfix Pull request with bugfix, not backported by default 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.

Hive partition filter wrongly prunes Parquet row groups by same-name file column

3 participants