Repository navigation
Support Query Condition Cache for local Parquet files - #109247
Conversation
The Query Condition Cache previously only applied to Parquet files read through object storage (`StorageObjectStorageSource`). Local files read via the `file()` table function or the `File` table engine (`StorageFile`) computed the predicate `condition_hash` and `KeyCondition` (so in-query row-group / page pruning already worked) but never consulted or populated the cache, so the pruning result was recomputed on every query. This wires the cache into `StorageFileSource::generate`, mirroring the object-storage read/write paths: on read, look up the surviving row groups for the file and predicate and restrict the reader with `setBucketsToRead` (or skip the whole file); when a file is exhausted, record the row groups that produced no matching rows via `getMatchedBuckets`. The file version token (`current_file_cache_version`: sub-second mtime, inode and size - already computed for the Parquet footer metadata cache) is folded into the cache key, so an in-place file rewrite yields a cache miss rather than stale results. Only Parquet reports matched buckets, so other formats naturally miss. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [7b39426] Summary: ✅
AI ReviewSummaryThis PR wires Findings❌ Blockers
Final VerdictNeeds revision before merge: the previously reported stale-read race in |
The `Style check` was the only red job (it drops all downstream builds/tests), failing on three issues in the new test: - a test-numbering gap (`05020` was far ahead of the highest existing number), - a global `SYSTEM FLUSH LOGS` (must name the log: `SYSTEM FLUSH LOGS query_log`), - `SYSTEM DROP QUERY CONDITION CACHE` without a `no-parallel` tag. While verifying the test end-to-end (CI never built it, since it stopped at the style stage) two functional problems surfaced: - The original test asserted on `read_rows`, but that cannot observe the cache: Parquet bloom-filter pruning already skips the absent (odd) value on the very first query, so the cached and uncached reads are identical. The reliable signal is the `QueryConditionCacheMisses` / `QueryConditionCacheHits` profile events, as used by the existing object-storage test (`test_storage_iceberg_with_spark/test_query_condition_cache.py`). - The `file()` table function has a Nil table UUID, and `QueryConditionCache` disables itself for storages without a UUID (issues #92863 / #92864), so the cache is inert for it - the same limitation the `s3()` table function has. The cache is populated and reused for file-backed tables that do have a UUID, i.e. the `File` table engine. Rewrite the test to use a `File` engine table and assert on the cache miss/hit profile events (first query = miss, second = hit), plus a correctness check that a matching predicate is not over-skipped. The table gets a unique UUID per test run, so the cache keys are isolated and no global cache reset is needed, keeping the test parallel-safe.
|
While fixing the
I renumbered the test to Open question: the description highlights the |
…ables Add a shared helper `QueryConditionCache::makeFilePartName(path, version_token)` that composes the `part_name` component of a cache key from a file's full path and a content-version token, separated by a NUL byte (which cannot occur in either, so the encoding is unambiguous). Using the full path (rather than the base name) keeps files that share a name in different directories from colliding, and folding in a version token makes an in-place rewrite of a file yield a different key instead of a stale hit. Use it for the local-file path in `StorageFile` (version token = modification time, inode and size), replacing the ad-hoc `path + "::" + version` concatenation. The same helper is reused for the object-storage path (ETag as the version token) in a follow-up. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Fable 5 <[email protected]>
|
Resolving my open question above in the (b) direction, since the AI review flags the same I've updated the PR description and Changelog entry so they describe what this PR actually ships — the Query Condition Cache now engages for UUID-bearing I deliberately did not take the AI's option (a) (extend the shared cache keying to Nil-UUID storages): that reverses the deliberate "no UUID → no cache" policy from #92863 / #92864 and would also change |
Address review feedback on the `04498_query_condition_cache_local_files` test: the previous version only proved miss->hit reuse on an unchanged file, leaving the main stale-read failure mode untested. The whole point of the file version token folded into the cache key via `QueryConditionCache::makeFilePartName` (sub-second mtime + inode + size) is that rewriting the same `File` table must not reuse the old skip decision. Add a focused rewrite step: populate the cache on even-only data (so `WHERE b = 3` records "no matching row group"), overwrite the table in place so `3` now exists, then assert the next query returns the matching row and records a cache miss rather than a hit. If a future refactor dropped the version token from the key, this step would return `0` (a stale cache hit reusing the "nothing matches" decision) and fail, instead of silently skipping a row group that now contains the searched value. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The query condition cache keyed local `File` reads by an `(mtime, inode, size)` version token, but filesystem timestamps are coarser than the wall clock (one ~1-10 ms clock tick on most Linux filesystems, a full second on ext3, two seconds on FAT), so an in-place rewrite that keeps the inode and the byte size and lands in the same timestamp tick as the previous write produces an identical token, and a query could reuse a stale skip decision, wrongly skipping row groups that now contain matches. Trust the token only once the file has settled: the last modification must be at least 3 seconds in the past (a future mtime, e.g. due to clock skew on a network mount, also fails the check). Otherwise the query condition cache is bypassed for that read - neither consulted nor populated. The format metadata cache is not touched: its token and consumers predate this change. The `04498` test now pins mtimes explicitly instead of relying on wall-clock timing: a settled (past) mtime engages the cache (miss, then hit), an unsettled (future) mtime deterministically exercises the fail-close bypass, and a rewrite followed by a distinct settled mtime proves invalidation plus re-population. The row group size moved to a table-level setting on the CREATE TABLE: the `File` sink writes with the format settings captured on the table, so the query-level SETTINGS clause on the INSERT never reached the Parquet writer and the file had a single row group; a single-row-group file with a matching row leaves no unmatched row groups to cache, which would make the repeated-query hit assertion impossible. Addresses the review finding on `StorageFile.cpp` about coarse mtime granularity reusing stale query-condition-cache entries. Co-Authored-By: Claude Fable 5 <[email protected]>
`current_file_cache_version` was sampled once before `createReadBuffer` opened the file and trusted for the rest of the read, but a second `File` table pointing at the same path is guarded by its own independent `rwlock`, not ours, and can truncate and rewrite the file strictly between that `stat` and the open, or while the row groups are being scanned. In that window the cache would consult or populate a stale entry keyed by a token that no longer describes the bytes being read, silently skipping a rewritten file. Re-stat the path immediately after opening (closing the window before the read starts) and again immediately before writing to the Query Condition Cache (closing the window spanning the read itself); on any mismatch treat the version token as unsettled and fail close for both the read and the write. `04498_query_condition_cache_local_files` and `04207_parquet_metadata_cache_local_file` verified passing (3/3 and 1/1 runs) against a server built from this branch. #109247 (comment)
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 96/100 (96.00%) · Uncovered code |
`current_file_cache_version` was sampled once before `createReadBuffer` opened the file and trusted for the rest of the read, but a second `File` table pointing at the same path is guarded by its own independent `rwlock`, not ours, and can truncate and rewrite the file strictly between that `stat` and the open, or while the row groups are being scanned. In that window the cache would consult or populate a stale entry keyed by a token that no longer describes the bytes being read, silently skipping a rewritten file. Re-stat the path immediately after opening (closing the window before the read starts) and again immediately before writing to the Query Condition Cache (closing the window spanning the read itself); on any mismatch treat the version token as unsettled and fail close for both the read and the write. `04498_query_condition_cache_local_files` and `04207_parquet_metadata_cache_local_file` verified passing (3/3 and 1/1 runs) against a server built from this branch. ClickHouse#109247 (comment)
…cache-local-files Support Query Condition Cache for local Parquet files
Extends the Query Condition Cache to local Parquet files read via the
Filetable engine.Until now the Query Condition Cache only applied to Parquet files read through object storage (
StorageObjectStorageSource, added in #102115). Local files read viaStorageFilealready computed the predicatecondition_hashandKeyCondition— so in-query row-group/page pruning worked — but they never consulted or populated the cache, so the pruning result was recomputed on every query.This wires the cache into
StorageFileSource::generate, mirroring the object-storage read/write paths:setBucketsToRead(or skip the whole file);getMatchedBuckets.The file version token (
current_file_cache_version: sub-second mtime, inode and size — already computed for the Parquet footer metadata cache) is folded into the cache key, so an in-place file rewrite yields a cache miss rather than stale results. Only Parquet reports matched buckets, so other formats naturally miss. Controlled by the existinguse_query_condition_cachesetting.Like the object-storage path, the cache is keyed by the table UUID, so it engages for UUID-bearing
File-engine tables (the common case in anAtomicdatabase). Thefiletable function shares the same read path but constructs aStorageIDwithout a persistent UUID, so the Query Condition Cache disables itself there (issues #92863 / #92864) — the same limitation thes3()table function has. Extending the cache to UUID-less storages (which would also coverfile()ands3()) is left as a possible follow-up.Related: #102115
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Support the Query Condition Cache for local Parquet files read via the
Filetable engine (previously supported only for object storage and data lakes).Documentation entry for user-facing changes
Version info
26.7.1.1042(included in26.7and later)