Repository navigation
Fix skip+unused_shards through Merge over Distributed in the analyzer - #106250
Conversation
When a `Distributed` table is wrapped in a `Merge` (engine or `merge()` table function) and the predicate is applied above the `Merge`, the analyzer was pushing the filter past `Distributed` instead of into it. As a result, `optimize_skip_unused_shards` was silently ignored, both shards were queried, and `force_optimize_skip_unused_shards = 2` threw `UNABLE_TO_SKIP_UNUSED_SHARDS`. `collectFiltersForAnalysis` only enabled the dummy-plan filter extraction when the top-level table expression was a `StorageDistributed` (or `View` / `ObjectStorageCluster`). For `StorageMerge`, no extraction happened and `query_info.filter_actions_dag` was empty by the time `StorageMerge::getQueryProcessingStage` forwarded it to each child's `StorageDistributed::getQueryProcessingStage`, so `skipUnusedShardsWithAnalyzer` had nothing to evaluate. Detect `StorageMerge` that wraps at least one `Distributed` child and run the filter collection in that case. The filter for the `Merge` table expression is then forwarded to children naturally via the existing `query_info` pass-through. Adds a `StorageMerge::hasDistributedTable` helper and uses it from `collectFiltersForAnalysis` in `Planner.cpp`.
|
Workflow [PR], commit [4364e2f] Summary: ✅
AI ReviewSummaryThis PR makes analyzer filter collection run for Findings
Tests
Final VerdictStatus: Minimum required action: handle nested |
…tabase
The previous .sql version created objects in `currentDatabase()` which is
unique per test invocation, but concurrent runs of the same test were
indistinguishable in CI's flaky check. Convert to .sh and derive a
dedicated database name from `${CLICKHOUSE_DATABASE}` so that 50 parallel
runs at concurrency 16 pass cleanly.
…storages The previous fix only enabled filter collection when a `Merge` had a direct `StorageDistributed` child, missing the case where a `Merge` wraps a `View` whose body reads from `Distributed`. Top-level `View` already triggers filter collection unconditionally, so it was inconsistent to ignore it under `Merge`: `SELECT ... FROM merge(..., '^view$') WHERE <sharding key> = ...` returned doubled counts and threw `UNABLE_TO_SKIP_UNUSED_SHARDS` under `force_optimize_skip_unused_shards = 2`. Generalize the criterion: extract a `storage_requires_filter_collection` lambda mirroring the top-level loop's checks (`Distributed`, `View`, `ObjectStorageCluster`, MergeTreeData under parallel-replicas estimation) and apply it both directly and via `StorageMerge::hasChildTable(predicate)`. The new `hasChildTable` replaces the narrower `hasDistributedTable` helper and takes a predicate so callers can stay in sync with the planner's notion of "what storages need filter analysis" without `StorageMerge` having to know about each type. Extend the regression test to cover `Merge` engine and `merge()` function wrappers over a `View` over `Distributed`.
| { | ||
| return traverseTablesUntil([&predicate](const StoragePtr & table) | ||
| { | ||
| return table && predicate(table); |
There was a problem hiding this comment.
hasChildTable only applies the predicate to the immediate tables matched by this Merge. If one of those tables is another StorageMerge, the predicate returns false before looking at that inner Merge's children, so a supported shape like Merge -> Merge -> Distributed still skips collectFiltersForAnalysis. Then the inner StorageDistributed receives query_info.filter_actions_dag == nullptr, and force_optimize_skip_unused_shards = 2 still raises UNABLE_TO_SKIP_UNUSED_SHARDS for a predicate on the sharding key. Please recurse into child StorageMerge instances, or otherwise make storage_requires_filter_collection account for nested Merge wrappers, and add a regression query for Merge over Merge over Distributed.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 29/29 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment. TL;DRFix skip+unused_shards through ClickGap verified: reproduced it with the script below on ReproductionBisect
Affects
Suggested next stepThe fix has landed on master; the affected release branches above still need the backport. Component: cc fix PR author: @KochetovNicolai ClickGap · Finding: |
Backport #106250 to 26.3: Fix skip+unused_shards through `Merge` over `Distributed` in the analyzer
Backport #106250 to 26.4: Fix skip+unused_shards through `Merge` over `Distributed` in the analyzer
Backport #106250 to 26.5: Fix skip+unused_shards through `Merge` over `Distributed` in the analyzer
…cess The client-side AST fuzzer loop swallowed every exception thrown during a fuzzing step: the `catch (...)` handler recorded the error, the loop bottom reset it, and fuzzing continued. When the failure mode is persistent - most importantly a dead server, where the reconnection attempt inside `processParsedSingleQuery` throws instantly - the client sprints through the remaining corpus at hundreds of steps per second without contacting the server and exits with code 0, which the job reports as "Fuzzer exited with success". Evidence from `AST fuzzer (amd_debug, targeted)` on PR ClickHouse#109061 (https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109061&sha=1419f6b7e95c81c812f945c06333998b92d56ad3&name_0=PR&name_1=AST+fuzzer+%28amd_debug%2C+targeted%29): `fuzzer.log` records 68972 fuzzing steps, of which 52837 produced no outcome (silent exception), while `server.log` shows the server received only 209 queries within a 47-second window before dying. On PR ClickHouse#106250 (https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106250&sha=latest&name_0=PR&name_1=AST+fuzzer+%28amd_debug%2C+targeted%29) a 5-file corpus with ~20 SELECT statements (about 20000 fuzzing steps, over an hour of work at the measured debug-build rate of 2-6 steps/s) "completed" in 74 seconds and the job passed. Overall, 13-33% of targeted AST fuzzer runs per month finish in under 8 minutes of their 30-minute budget. Client fixes (`programs/client/FuzzLoop.cpp`): - Print thrown exceptions in the fuzzing-step catch handlers instead of swallowing them silently, so `fuzzer.log` shows what actually happened. - After a thrown exception, check the connection via `tryToReconnect` (the same check `processASTFuzzerStep` does for errors received without throwing) and stop fuzzing when the server is gone instead of uselessly sprinting through the rest of the corpus. - Register query parameters generated by `QueryFuzzer` (from `getLastQueryParameters`) for client-side substitution, mirroring the server-side fuzzer in `executeASTFuzzerQueries`; previously any fuzzed query containing a substituted `{name:type}` parameter failed with "Substitution ... is not set" before reaching the server. Job fix (`ci/jobs/scripts/fuzzer/run-fuzzer.sh`): - When the AST fuzzer walks the whole corpus and exits 0 with a meaningful part of the FUZZ_TIME_LIMIT budget remaining, restart it (each pass uses a fresh random seed) instead of ending the job after a couple of minutes. This matters for the targeted variant, whose corpus is a handful of test files. The restart is gated on a server liveness probe so a dead server still terminates the job for classification.
…erge` over `Distributed` in the analyzer
…erge` over `Distributed` in the analyzer
…erge` over `Distributed` in the analyzer
…cess The client-side AST fuzzer loop swallowed every exception thrown during a fuzzing step: the `catch (...)` handler recorded the error, the loop bottom reset it, and fuzzing continued. When the failure mode is persistent - most importantly a dead server, where the reconnection attempt inside `processParsedSingleQuery` throws instantly - the client sprints through the remaining corpus at hundreds of steps per second without contacting the server and exits with code 0, which the job reports as "Fuzzer exited with success". Evidence from `AST fuzzer (amd_debug, targeted)` on PR ClickHouse#109061 (https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109061&sha=1419f6b7e95c81c812f945c06333998b92d56ad3&name_0=PR&name_1=AST+fuzzer+%28amd_debug%2C+targeted%29): `fuzzer.log` records 68972 fuzzing steps, of which 52837 produced no outcome (silent exception), while `server.log` shows the server received only 209 queries within a 47-second window before dying. On PR ClickHouse#106250 (https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106250&sha=latest&name_0=PR&name_1=AST+fuzzer+%28amd_debug%2C+targeted%29) a 5-file corpus with ~20 SELECT statements (about 20000 fuzzing steps, over an hour of work at the measured debug-build rate of 2-6 steps/s) "completed" in 74 seconds and the job passed. Overall, 13-33% of targeted AST fuzzer runs per month finish in under 8 minutes of their 30-minute budget. Client fixes (`programs/client/FuzzLoop.cpp`): - Print thrown exceptions in the fuzzing-step catch handlers instead of swallowing them silently, so `fuzzer.log` shows what actually happened. - After a thrown exception, check the connection via `tryToReconnect` (the same check `processASTFuzzerStep` does for errors received without throwing) and stop fuzzing when the server is gone instead of uselessly sprinting through the rest of the corpus. - Register query parameters generated by `QueryFuzzer` (from `getLastQueryParameters`) for client-side substitution, mirroring the server-side fuzzer in `executeASTFuzzerQueries`; previously any fuzzed query containing a substituted `{name:type}` parameter failed with "Substitution ... is not set" before reaching the server. Job fix (`ci/jobs/scripts/fuzzer/run-fuzzer.sh`): - When the AST fuzzer walks the whole corpus and exits 0 with a meaningful part of the FUZZ_TIME_LIMIT budget remaining, restart it (each pass uses a fresh random seed) instead of ending the job after a couple of minutes. This matters for the targeted variant, whose corpus is a handful of test files. The restart is gated on a server liveness probe so a dead server still terminates the job for classification.
CTE/AN/QP: Distributed read through Merge/merge() with optimize_skip_unused_shards -> perf / false exc. Effect in the production build a394e92: its upstream test(s) 04303_optimize_skip_unused_shards_in_merge fail on it and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv). Upstream patch unchanged, with its test(s): 04303_optimize_skip_unused_shards_in_merge. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude
When a `Distributed` table is wrapped in a `Merge` (engine or `merge()` table function) and the predicate is applied above the `Merge`, the analyzer was pushing the filter past `Distributed` instead of into it. As a result, `optimize_skip_unused_shards` was silently ignored, both shards were queried, and `force_optimize_skip_unused_shards = 2` threw `UNABLE_TO_SKIP_UNUSED_SHARDS`.collectFiltersForAnalysisonly enabled the dummy-plan filter extraction when the top-level table expression was aStorageDistributed(orView/ObjectStorageCluster). ForStorageMerge, no extraction happened andquery_info.filter_actions_dagwas empty by the timeStorageMerge::getQueryProcessingStageforwarded it to each child'sStorageDistributed::getQueryProcessingStage, soskipUnusedShardsWithAnalyzerhad nothing to evaluate.Detect
StorageMergethat wraps at least oneDistributedchild and run the filter collection in that case. The filter for theMergetable expression is then forwarded to children naturally via the existingquery_infopass-through.Adds a
StorageMerge::hasDistributedTablehelper and uses it fromcollectFiltersForAnalysisinPlanner.cpp.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
optimize_skip_unused_shardsdid not apply (and force_optimize_skip_unused_shards falsely failing) whena
Distributedtable is queried through aMergetable or themergetable function, with the predicate applied aboveit.
Version info
26.6.1.328(included in26.6and later)26.5.6.24,26.4.5.111,26.3.17.30