Skip to content

Do not cache count-from-files results under row filters. - #122835

Open
ianton-ru wants to merge 7 commits into
ClickHouse:masterfrom
ianton-ru:fix-count-from-files-cache-row-filters
Open

ianton-ru wants to merge 7 commits into
ClickHouse:masterfrom
ianton-ru:fix-count-from-files-cache-row-filters

Conversation

@ianton-ru

@ianton-ru ianton-ru commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Row policies and PREWHERE reduce emitted rows while the cache key is file identity only. Treat them like filter DAGs in FormatFilterInfo::hasFilter.

hasFilter reported only the WHERE filter DAG, so a filtered count() could store a reduced cardinality and a later unrestricted count() would reuse it. hasFilter now also reports PREWHERE and row-policy filters. StorageFile and StorageURL already consult hasFilter before writing the cache. StorageObjectStorageSource checked filter_actions_dag directly and now uses hasFilter as well.

This is the FormatFilterInfo fix split out of #113561. Deletion-vector read support from that pull request landed separately in #110781.

Related: #113561
Related: #110781

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):

Do not store count-from-files cache entries for reads filtered by a row policy or PREWHERE. The cache key is file identity only, so a reduced count could be reused by a later unrestricted count().


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

Row policies and `PREWHERE` reduce emitted rows while the cache key is file identity only. Treat them like filter DAGs in `FormatFilterInfo::hasFilter`.

Co-authored-by: Cursor <[email protected]>
@scanhex12 scanhex12 self-assigned this Sep 29, 2026
@scanhex12 scanhex12 added the can be tested Allows running workflows for external contributors label Sep 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7f135f9]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL

AI Review

Summary

The PR includes row policies in the file-format query-condition-cache key and adds regressions for filtered count caching and cross-policy cache reuse. The earlier bot findings are addressed, and no remaining actionable findings were identified. Current regressions pass CI; comparison-build bugfix validation reports no reproduction.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 29, 2026
Comment thread src/Formats/FormatFilterInfo.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Sep 29, 2026
ianton-ru and others added 2 commits September 29, 2026 14:02
`ORDER BY ... LIMIT` drops rows inside the Parquet reader while the cache key is file identity only, so a reduced count could be reused by a later `count()`.

https: //github.com/ClickHouse/pull/122835
Co-authored-by: Cursor <[email protected]>
`getMatchedBuckets` records a row group as unmatched when `rows_pass` is zero, and the reader applies the row policy before that count. A `WHERE`-only key could skip those rows after the policy changed.

https: //github.com/ClickHouse/pull/122835
Co-authored-by: Cursor <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 7f135f9bb6741cb0c07cea85a94f59fa0dac8be3 was not compared: the CI logs cluster did not answer (exception 241, code 500: { "meta": [ { "name": "c", "type": "UInt64" } ], "data": […).

See the job log for details.

@ianton-ru
ianton-ru marked this pull request as ready for review September 29, 2026 12:50
Comment thread src/Formats/FormatFilterInfo.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Sep 30, 2026
Comment thread src/Formats/FormatFilterInfo.cpp Outdated
{
condition_hash = queryConditionCacheHash(
filter_actions_dag->getHash(), queryConditionCacheSettingsSalt(context_->getSettingsRef()));
dag_hash, queryConditionCacheSettingsSalt(context_->getSettingsRef()));

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.

Don't we need to do the same in MergeTree?

There is still getHash from ActionDAG

            size_t condition_hash = queryConditionCacheHash(
                filter_actions_dag->getOutputs()[0]->getHash(),
                queryConditionCacheSettingsSalt(read_from_merge_tree->getContext()->getSettingsRef()));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As far as I understand, in updateQueryConditionCache key calculation is already after applying row level filter, so with different row filters returns different keys.

…les-cache-row-filters

Keep `FormatFilterInfo::hasFilter` from master. Mix `row_level_filter` into `computeConditionHash` so the Parquet query-condition cache key still changes with the row policy, and keep `fileFormatPruningSettingsSalt`.

Co-authored-by: Cursor <[email protected]>
`SchemaCache` drops an entry when `st_mtime` is at least `registration_time`, and both are second resolution. An insert and a top-K read in the same second discarded a reduced count, so a later `count()` reread the file and passed with the `top_k_filter` guard removed.

https: //github.com/ClickHouse/pull/122835
Co-authored-by: Cursor <[email protected]>
`File(TSV)` keeps a row policy in a `FilterStep` above `StorageFileSource`, so the reader still sees every row and would cache the full count with the `hasFilter` guard removed. `File(Parquet)` pushes the policy as `row_level_filter`, and the file is backdated before the filtered `count()` so a reduced count stays reusable.

https: //github.com/ClickHouse/pull/122835
Co-authored-by: Cursor <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Dear @scanhex12, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

This branch has not been deployed

No deployments
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-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants