Repository navigation
Fix cubic complexity of planning a JOIN with a merge table - #113140
Conversation
`ReadFromMerge::createChildrenPlans` builds a separate child query tree for
every source table. When the `Merge` table takes part in a JOIN, this goes
through `replaceTableExpressionAndRemoveJoin`, which rebuilds the projection
list from the required column names. Every column there was resolved by its own
`QueryAnalysisPass` run, and every such run rebuilt `AnalysisTableExpressionData`
for all the columns of the `Merge` table. The structure of a `Merge` table is
the union of the structures of its source tables, so planning was cubic in the
number of source tables.
Resolve all the required columns with a single `QueryAnalysisPass` run instead.
For 40 tables with 20 distinct columns each, a `SELECT *` joined with `merge`
goes from 18 seconds down to about a second in a release build.
This is what made the `Hung check` fail in a stress test: the AST fuzzer
produced `SELECT * FROM (SELECT number AS a FROM numbers(11)) AS t1 PASTE JOIN
merge('system', '') AS t2 LIMIT 294`, and the query spent more than 16 minutes
inside `QueryPlan::optimize` under TSan, responding neither to
`max_execution_time` nor to the cancellation request.
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=dd1e53dfe2715c22d4b7e63c2459bc9eec994581&name_0=MasterCI&name_1=Stress%20test%20%28arm_tsan%29
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [ecb7c27] Summary: ✅
AI ReviewSummaryThis PR changes Final Verdict✅ No remaining review findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 22/26 (84.62%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: This PR is scoped entirely to StorageMerge: it batches column resolution in replaceTableExpressionAndRemoveJoin so a JOIN against a merge() table no longer pays cubic query-planning cost. None of the flagged ClickBench (single hits table) or TPC-H (regular MergeTree joins) queries use merge() tables, so this change cannot plausibly touch any of their execution or planning paths. All six flagged improvements are therefore downgraded to not_sure as off-path deltas; the largest and cleanest ones (Q7 ×8.2 faster, Q23 ×2.3 faster) most likely reflect base-build or environment differences rather than this PR. The real effect of this fix would only show up on queries that JOIN wide merge() tables, which this benchmark set does not include. clickbenchFlagged queries (3 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 (2 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 flaky check under TSan and debug builds killed the test after 180 seconds: 40 tables with 20 columns each are too heavy for sanitizer builds even with the fix. Reduce to 20 tables with 10 columns each; the whole test takes about half a second in a release build now, while a cubic planning regression would still blow past the limit. The run with the old analyzer failed with `UNKNOWN_IDENTIFIER`: the old analyzer cannot resolve columns of a `Merge` table that are missing from some of the source tables, so pass `--enable_analyzer 1` explicitly for the `SELECT` queries. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113140&sha=c60ed76704e5b13d618c8784613a5f878406b5bf&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20flaky%20check%29 Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 The The remaining failures were caused by the new test and are addressed: it now uses 20 tables × 10 columns (the whole test takes ~0.5 s in a release build, so it fits the 180 s flaky-check limit in TSan/debug builds too) and passes |
|
🕵 Both remaining CI failures on 304d237 are unrelated to this PR:
Neither fix has landed on master yet, so re-merging master would not clear these. Everything else is green; no unresolved review threads. |
|
🕵 Both CI failures on c2fe2bd are unrelated to this PR — they are the fleet-wide stress regressions caused by #113772 (PromQL materialized-CTE subqueries), affecting
Re-merging master would not clear these until the fixes land. @groeneai, #114291 still has no fix PR — please provide a fix in a separate PR (or link it here if one is already in progress). |
|
#114291 does have a fix in progress: #113397 (open, not merged). I measured it A/B on the reproduced leak this morning: 5/5 runs leak on an ASAN build of master, 5/5 clean with that patch applied, and a 34-test materialized-CTE/PromQL run gives identical results on both arms, so I am not opening a competing PR for it. Measurement details are on the issue: #114291 (comment) |
|
🕵 Merged |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 26 s compile time in total, 7 of them have a recent master baseline. |
|
🕵 The only CI failure on 0cb1d1e, |
Planning a query that joins a
Mergetable with something else could take minutes and hundreds of megabytes of memory, and it was interruptible neither bymax_execution_timenor byKILL QUERY, because all of it happens insideQueryPlan::optimize.ReadFromMerge::createChildrenPlansbuilds a separate child query tree for every source table. When theMergetable takes part in a JOIN, this goes throughreplaceTableExpressionAndRemoveJoin, which rebuilds the projection list from the required column names. Every column there was resolved by its ownQueryAnalysisPassrun, and every such run rebuiltAnalysisTableExpressionDatafor all the columns of theMergetable. The structure of aMergetable is the union of the structures of its source tables, so the cost of planning was cubic in the number of source tables.All the required columns are now resolved with a single
QueryAnalysisPassrun. For 40 tables with 20 distinct columns each, aSELECT *joined withmergegoes from 18 seconds down to about a second in a release build; the results are byte-identical before and after.This is what made the
Hung checkfail in a stress test: the AST fuzzer producedSELECT * FROM (SELECT number AS a FROM numbers(11)) AS t1 PASTE JOIN merge('system', '') AS t2 LIMIT 294for02933_paste_join.sql, and the query spent more than 16 minutes insideQueryPlan::optimizeunder TSan.https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=dd1e53dfe2715c22d4b7e63c2459bc9eec994581&name_0=MasterCI&name_1=Stress%20test%20%28arm_tsan%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed cubic complexity of query planning for a JOIN with a
Mergetable over many tables with different structures. Such queries could previously spend minutes in query planning without responding tomax_execution_timeorKILL QUERY.Documentation entry for user-facing changes
Version info
26.8.1.1462(included in26.8and later)