Repository navigation
Conversation
… distributed queries FunctionToSubcolumnsPass never applied through a Distributed table: the initiator declines it because StorageDistributed does not support the optimization (ClickHouse#109188 - the initiator cannot see the shards' schema and indexes), and shards skip all optimization passes to keep the block header expected by the initiator stable. As a result, e.g. map['key'] read and decoded the whole Map column on every shard instead of a single key subcolumn. Rewrites inside WHERE and PREWHERE cannot change the header, so apply them on the shard, where the real storage metadata is known: the pass gets an only_filter_clauses mode that restricts all rewrites to WHERE/PREWHERE, and the interpreter runs it after the resolve passes for queries executed on shards of distributed queries. Closes: ClickHouse#114174 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [39a1abe] Summary: ✅
AI ReviewSummaryThis PR moves Findings
Final Verdict
LLVM Coverage ReportMeasured on commit 39a1abe.
Changed lines: Changed C/C++ lines covered: 40/42 (95.24%) · Uncovered code |
Co-Authored-By: Claude Fable 5 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: Release-transition baseline: compared against the last 26.8 master because no 26.9 master baseline exists at or before the trusted base-commit anchor. This PR narrowly scopes the function-to-subcolumns optimizer so that distributed-query shards can rewrite Map/JSON accessors inside WHERE/PREWHERE (plus a StorageTableProxy snapshot fix for lazy-loaded tables). None of the flagged ClickBench or TPC-H queries execute that path — they contain no Map/JSON subcolumn filters through Distributed tables — so the code change cannot plausibly move their medians in either direction. Every flagged row also carries a thin baseline (as few as 11-33 base samples) and, for most, a noisy history, which is why the deltas look large; the extreme ones (TPCH Q4 ×20.9 faster on n=12, Q7 ×6.9 faster on n=25) are classic thin-baseline artifacts. All flagged rows are downgraded to not-sure: the real behavior this PR targets is only visible in the dedicated map_subcolumns_prewhere distributed test, not in these standard benchmarks. clickbenchFlagged queries (10 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_officialFlagged queries (6 of 22)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
The guard collected primary key and secondary index columns from the resolution-time storage snapshot. For a table in a database with lazy_load_tables = 1, that snapshot is built from the StorageTableProxy stub, which has columns only - no primary key and no secondary indices - so no column was index-protected and rewrites broke index analysis. Read the storage's current metadata instead: the nested storage is materialized by the time the pass runs, so the proxy forwards the real metadata. The pre-existing bug was observable on master with a direct query on a freshly attached lazy table (verified on 26.8.1.1235: INDEX_NOT_USED with force_data_skipping_indices). Running the pass on shards extended the exposure to Distributed queries, caught by 02346_text_index_bug108874. Co-Authored-By: Claude Fable 5 <[email protected]>
…h in test With only_filter_clauses, the chained branch of the second visitor (e.g. the JSON array rewrite for json.a[1].b) never fired because it accepted only identifiers from the everywhere bucket, which the mode empties. Honor filter_only inside WHERE/PREWHERE there, same as the direct branch. The test's serialized-plan case used prefer_localhost_replica = 1, which takes the local-stream branch and never exercises the build_logical_plan path. Add a case with prefer_localhost_replica = 0 and a chained JSON rewrite case (both validated on 26.8.1.1250: only the read-amplification booleans fail pre-fix). Also trim overly verbose comments. Co-Authored-By: Claude Fable 5 <[email protected]>
| if (is_query_on_shard && !select_query_options.is_create_view) | ||
| { | ||
| QueryTreePassManager function_to_subcolumns_pass_manager(context); | ||
| function_to_subcolumns_pass_manager.addPass(std::make_unique<FunctionToSubcolumnsPass>(/*only_filter_clauses=*/ true)); |
There was a problem hiding this comment.
Adding only FunctionToSubcolumnsPass here still leaves the existing empty-string filter form on the old full-column path. On a direct query, name != '' / name = '' first goes through ConvertEmptyStringComparisonToFunctionPass (notEquals/equals -> notEmpty/empty), and only then can FunctionToSubcolumnsPass rewrite it to name.size; this shard-only branch never runs that earlier pass, so a distributed query like 04510_functions_to_subcolumns_distributed_type_mismatch.sql stays as notEquals(name, '') and never gets the subcolumn read. If the goal is parity for shard-side WHERE/PREWHERE optimization, this path needs the same filter-scoped empty-string conversion (or equivalent direct handling in FunctionToSubcolumnsPass).
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units10 translation units recompiled, 36 s compile time in total, 10 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.09 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
|
The Known issue, failing daily on master and on dozens of unrelated PRs over the last 90 days per CIDB. @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114204&sha=833228d5fdaee4da8246fb5d39aa55bb77f0dad5&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. |
|
Investigated. The fix is already in progress: #113558 (@ alexey-milovidov, open). Measured, it covers this exact shape, so nothing new is needed from my side here. The failing query on your run is WITH t AS MATERIALIZED (SELECT number AS c FROM numbers(257))
SELECT count() FROM merge(REGEXP('.*'), '^') WHERE (x IN (t)) OR (x IN (t))
A/B on your carrier query, debug build, master
Controls: the same Two notes. #113558 no longer merges cleanly onto current master: one textual conflict in |
|
The Filed: #114819 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114204&sha=833228d5fdaee4da8246fb5d39aa55bb77f0dad5&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. |
|
Investigated. The fix for that failure is already merged: #113794 (merged today at 09:09Z). It is a test-isolation defect, not a Your run predates the fix: the job's Full artifact timeline and the family numbers are in #114819 (comment) - no need to duplicate them here. |
…ributed # Conflicts: # src/Analyzer/Passes/FunctionToSubcolumnsPass.cpp
…Snapshot Move the fix for stub metadata of lazily loaded tables from FunctionToSubcolumnsPass into StorageProxy::getStorageSnapshot. The resolution-time snapshot was built around caller-supplied base_metadata, which for a not-yet-materialized StorageTableProxy is the stub (columns only - no keys, no indices, no virtuals), even though getNested() materializes the nested table in the same call. Build the snapshot from the nested storage's metadata instead, so every consumer of the snapshot sees real metadata, and the pass reads storage_snapshot->metadata like the rest of the code again. Co-Authored-By: Claude Fable 5 <[email protected]>
Building the snapshot from nested metadata in the StorageProxy base broke CREATE TABLE ... AS table_function() with an explicit structure (01457_create_as_table_function_structure): StorageTableFunctionProxy declares a structure intentionally different from the nested storage and converts on read, so its base_metadata is authoritative. Only StorageTableProxy, whose stub describes the same table, should use the nested storage's metadata. Co-Authored-By: Claude Fable 5 <[email protected]>
Under flaky-check randomization the test drew index_granularity = 1, making the 10000-row insert write one granule per row and exceed the 180s limit. Randomized min_bytes_for_wide_part could also force compact parts, where subcolumn reads may not reduce read_bytes and the read-amplification assertions would not hold. Co-Authored-By: Claude Fable 5 <[email protected]>
|
The Filed: #116200 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114204&sha=845dbd64b87c2c764a9660ced6b06c9e819a44e5&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. |
|
The Filed: #116207 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114204&sha=845dbd64b87c2c764a9660ced6b06c9e819a44e5&name_0=PR&name_1=Stress%20test%20%28amd_msan%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. |
|
#116200 ( #116207 ( |
|
The @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114204&sha=845dbd64b87c2c764a9660ced6b06c9e819a44e5&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. |
|
The tests did run. Correcting the premise first, because it changes what needs fixing.
The parallel shard's 1. One real test failure. 2. The job-level In progress for this half: #109673 labels exactly this signature Also relevant to why the shard is exposed at all: #116209 splits these two un-batched coverage parallel shards into 2 batches. This job ran 132 min. Not caused by this PR: the diff touches none of One more correction: the memory-tracker block in the tail is a diagnostic, not a cause. |
|
Fixing PR for the first defect: #116242 (adds One correction to my earlier comment. I said 04411 was The second defect, the job-level |
04411_memory_profiler_sample_probability_sql_settings runs a query with memory_profiler_sample_probability = 1, then SYSTEM FLUSH LOGS trace_log, then asserts a MemorySample row is visible. SystemLogQueue::waitFlush uses a hardcoded timeout_seconds = 180 (src/Common/SystemLogBase.cpp:191) and waits for getLastLogIndex(), a server-global queue position, so the flush drains the trace rows of every concurrently running parallel test rather than only this test's. Under LLVM source-based coverage instrumentation the server is slow enough that this crosses 180 s, waitFlush throws at :213 with Code: 159. Timeout exceeded (180 s) while flushing system log 'DB::SystemLogQueue<DB::TraceLogElement>'. (TIMEOUT_EXCEEDED) and the following SELECT count() > 0 then reads 0, so the coverage lane reports a false negative rather than only contaminating stderr. Over 30 days of coverage-lane runs that PASSED, this test has p50 9.6 s and p95 183.6 s (max 202.3 s) against p50 5.1 s / p95 11.1 s in every other lane, and 2307 of 37648 coverage runs already exceed 180 s. Two crossed it on the flush: Stateless tests (amd_llvm_coverage, 3/3) on 2026-08-20 (PR ClickHouse#115623) and (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) on 2026-07-23 (PR ClickHouse#109174). Report for the 2026-08-20 failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115623&sha=9b52646c82721f24b7dfb1ddc58d949beef5a7d0&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%203%2F3%29 no-llvm-coverage is the established remedy for this mechanism: 36 stateless tests carry it, and commit 36decd6 ("Mark tests that timed out under LLVM coverage as no-llvm-coverage") applied it to five tests including this test's direct sibling 02818_memory_profiler_sample_min_max_allocation_size, whose shape is identical. 04411 was created two months after that commit, which is why it was missed. Coverage is relocated, not lost: the inverse filter at tests/clickhouse-test:2967-2971 makes Stateless tests (amd_binary_excluded_from_llvm) run exactly the tagged complement, and the two flags are mutually exclusive by construction (ci/jobs/functional_tests.py:608-612). Over 14 days in that job the tagged siblings 02818 and 01473_event_time_microseconds are OK 4367 and 4365 times while 04411 is SKIPPED 4367 times. The same intervention on 01473 (ClickHouse#103219, merged 2026-08-04) took its coverage-lane failures from 5 to 0. Raising or parameterizing the 180 s constant was rejected: it is a server-wide change for one test, the constant is already a deliberate concession to parallel-test load, and because the drain is global a larger value only moves the threshold. Validated with the real runner. With the tag, --llvm-coverage skips and --excluded-from-llvm runs; with the tag removed both verdicts invert, which is the liveness proof that the arms measure the tag. Without any coverage flag the test still runs, 10/10. Related: ClickHouse#114204
|
Hey @SmitaRKulkarni are you able to give this a review? |
|
@PedroTadim are you able to take a look at this? |
|
@andyzzhao I am no expert in this area to review 😞 |
|
Hey @Avogar, do you think you can review this? |
Closes: #114174
Related: #109188
Related: #99200
Related: #107988
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Applies
optimize_functions_to_subcolumnsinsideWHERE/PREWHEREon the shards of distributed queries, so that e.g.map['key']reads a single key subcolumn instead of the wholeMapcolumn. Previously the optimization never applied when querying through aDistributedtable.Documentation entry for user-facing changes
Description
The optimization never applies through a
Distributedtable: the initiator declines it (StorageDistributed::supportsOptimizationToSubcolumnsisfalse— it cannot see the shards' real schema #108299 or skipping indexes #108874), and shards run only the resolve passes. So e.g.WHERE tags['k0'] != ''reads and decodes the wholeMapon every shard instead of a single key subcolumn.Fix: shards run
FunctionToSubcolumnsPassin a newonly_filter_clausesmode that restricts all rewrites (direct and chained, e.g.json.a[1].b) toWHERE/PREWHERE, where they cannot change the header expected by the initiator. Applied on remote shard queries (SECONDARY_QUERY), theprefer_localhost_replicalocal plan, and the serialized query plan path. On the shard the pass sees the realMergeTreemetadata, so the index guard and subcolumn resolution are safe where the initiator-side rewrite was not.Also fixes stale metadata of lazily loaded tables (
lazy_load_tables = 1), which would let the rewrite defeat skipping indexes:StorageTableProxy's storage snapshot was built from the pre-materialization stub (columns only — no keys, indices, or virtuals) although building it materializes the nested table anyway; it now uses the nested storage's metadata.The stateless tests check correctness, header stability, and read amplification (via
system.query_log) across all three shard paths and the lazy-table case;tests/performance/map_subcolumns_prewhere.xmlgets aremote()case.Workflow [PR]
Sync PR [sync-upstream/pr/114204]