Skip to content

Explain analyze - #106586

Merged
Fgrtue merged 87 commits into
masterfrom
explain-analyze
Jul 10, 2026
Merged

Fgrtue merged 87 commits into
masterfrom
explain-analyze

Conversation

@Fgrtue

@Fgrtue Fgrtue commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Adding EXPLAIN ANALYZE v1 for examination of query performance using metrics rendered in the familiar query plan format.
...

Version info

  • Merged into: 26.7.1.750 (included in 26.7 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f3d226f]

Summary: ❌

job_name test_name status info comment
Stress test (amd_debug) FAIL
Logical error: 'Inconsistent AST formatting: the query: (STID: 1941-26fa) FAIL cidb, issue ISSUE EXISTS

AI Review

Summary

This PR adds EXPLAIN ANALYZE v1 with per-step runtime metrics rendered in the plan tree. The main local execution path is in place, but there are still a few correctness gaps around quota/accounting behavior and runtime sub-pipelines, so I would not merge it yet.

Findings

❌ Blockers

  • [src/Interpreters/InterpreterExplainQuery.cpp:817-821] ignoreLimits still mirrors the inner SELECT, so outer executeQuery can apply max_result_rows / max_result_bytes to the textual EXPLAIN ANALYZE output after the inner pipeline has already enforced the real query limits. A query whose actual result is within limits can still fail because the printed plan has more rows. Suggested fix: return true for executable EXPLAIN ANALYZE once the inner pipeline is enforcing the inner limits, or otherwise keep the outer-result limit decision separate from the inner query. [dismissed by author -- https://github.com/Explain analyze #106586#discussion_r3483464820]

  • [src/Processors/Executors/ExecutingGraph.cpp:197-199] EXPLAIN ANALYZE still cannot account runtime snapshot sub-pipelines correctly. MergeTreeCommitOrderSequentialSource builds a fresh local QueryPlan, turns it into processors with their temporary plan-step pointers intact, and then returns them from updatePipeline (src/Storages/MergeTree/Streaming/MergeTreeCommitOrderSequentialSource.cpp:161-167, src/Storages/MergeTree/Streaming/MergeTreeCommitOrderSequentialSource.cpp:438-476). Because ExecutingGraph only inherits the parent step when getQueryPlanStep() is null, those processors do not match the top-level StepWallClockRegistry: debug/sanitizer builds can still trip the chassert(clock) path, and release builds silently lose stage timing for streaming reads. Suggested fix: either reject streaming/dynamic snapshot sub-pipelines in v1, or retag every runtime-added processor to a top-level owning step before execution.

⚠️ Majors

  • [src/Processors/QueryPlan/AnalyzePlanStats.cpp:17-25] Step stats are still built only from the final pipeline.getProcessors() list, but MergeTreeCommitOrderSequentialSource::updatePipeline removes the previous snapshot processors on each reconfiguration (src/Storages/MergeTree/Streaming/MergeTreeCommitOrderSequentialSource.cpp:453). Even after the step-tagging issue above is fixed, time and I/O from earlier snapshots disappear from the final report, so long-running streaming queries under EXPLAIN ANALYZE under-report their work. Suggested fix: accumulate stats for retired processors before erasing them, or keep an execution-time registry separate from the mutable pipeline processor list. [dismissed by author -- https://github.com/Explain analyze #106586#discussion_r3415663671]

  • [src/Interpreters/executeQuery.cpp:1843-1848] The quota gate still treats EXPLAIN ANALYZE as executable based only on AST shape and the session-level make_distributed_plan flag. Queries that are later rejected as unsupported distributed execution, such as remote/delayed-source plans, still consume QUERY_SELECTS quota before InterpreterExplainQuery::executeImpl throws. Suggested fix: make isExecutableAnalyze fail closed for remote/distributed shapes as well, or defer the QUERY_SELECTS charge until after the same runtime guard that decides the query is actually executable. [dismissed by author -- https://github.com/Explain analyze #106586#discussion_r3482804977]

  • [src/Interpreters/InterpreterExplainQuery.cpp:1182] The recent quota-hash fix only stops using 0; it still uses the outer EXPLAIN ANALYZE context hash. The inner planning contexts are copies of that outer hash (src/Interpreters/Context.cpp:1298-1300), and the select interpreters only forward context->getNormalizedQueryHash() into their pipelines (src/Interpreters/InterpreterSelectQueryAnalyzer.cpp:381, src/Interpreters/IInterpreterUnionOrSelectQuery.cpp:149). Per-NORMALIZED_QUERY_HASH quotas therefore still do not match the wrapped SELECT. Suggested fix: compute and install the normalized hash for ast.getExplainedQuery() before attaching limits/quota to the inner pipeline.

  • [src/Processors/Sources/LazyReadReplacingFinalSource.cpp:341-342] The runtime child plan built for LazyReadReplacingFinalStep is still rewritten onto the parent lazy step before execution, while LazyReadReplacingFinalStep::getChildPlans prints the real child plan tree (src/Processors/QueryPlan/LazyReadReplacingFinalStep.cpp:55-65). EXPLAIN ANALYZE therefore shows child-plan nodes that never receive the runtime stats they appear to promise, and instead folds their time into the parent lazy step. Suggested fix: either keep the child processors tagged to the cached child-plan steps, or stop exposing that child plan in EXPLAIN ANALYZE until the runtime and rendered step identities match. [dismissed by author -- https://github.com/Explain analyze #106586#discussion_r3482423390]

Final Verdict

❌ Changes requested.

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jun 5, 2026
Comment thread src/Processors/QueryPlan/QueryPlan.h Resolved
@nickitat nickitat self-assigned this Jun 8, 2026

@nickitat nickitat left a comment

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.

I think it'd be valuable to split each step into more logical groups. In the sorting example, it might be "pre-sorting" and "final merge". For aggregation, it might be "pre-aggregation" and "final merge" again.
Also, I think it'd make sense to extend the time metrics. Usually, when I query processors_profile_log, I query min, median, max and sum for all processors with the same name. So, in theory, when you look at this profile, you want to be able to answer three questions: the single most expensive part of the plan, parallelism and the critical path. We should aim to provide enough information for that.

Comment thread src/QueryPipeline/QueryPipeline.h Resolved
Comment thread src/Processors/QueryPlan/JoinStep.cpp Resolved
Comment thread src/Processors/QueryPlan/QueryPlan.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 81.80% 85.80% +4.00%
Functions 92.30% 92.70% +0.40%
Branches 73.80% 77.90% +4.10%

Changed lines: Changed C/C++ lines covered: 743/830 (89.52%) · Uncovered code

Full report · Diff report

@Fgrtue
Fgrtue added this pull request to the merge queue Jul 10, 2026
Merged via the queue into master with commit 918a0d3 Jul 10, 2026
174 of 176 checks passed
@Fgrtue
Fgrtue deleted the explain-analyze branch July 10, 2026 13:23
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 10, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Reverting due to the failed assertion.

@alexey-milovidov

Copy link
Copy Markdown
Member

alexey-milovidov added a commit that referenced this pull request Jul 12, 2026
…list

The revert of #106586 removed the `ANALYZE`, `ESTIMATE`, and `TABLE OVERRIDE`
bullets from the `EXPLAIN Types` overview list, but `ESTIMATE` and
`TABLE OVERRIDE` are still advertised in the syntax block and still have their
own `### EXPLAIN ESTIMATE` and `### EXPLAIN TABLE OVERRIDE` sections later in
the same document. Re-add those two bullets (but not `ANALYZE`, which is
removed by this revert) so the page is internally consistent again.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
alexey-milovidov added a commit that referenced this pull request Jul 12, 2026
alexey-milovidov added a commit that referenced this pull request Jul 12, 2026
Reintroduces the `EXPLAIN ANALYZE` feature (originally #106586) by reverting
its revert #110162 (merge commit 178267d, reverted with `-m 1`).

The feature was reverted only because of a `Logical error: 'clock'` abort in
`EXPLAIN ANALYZE` over a streaming (`FROM ... STREAM`) read in debug and
sanitizer builds. The following commit fixes that bug, so the feature can be
reintroduced.

This restores the query-executor instrumentation (`StepWallClock`,
`StepWallClockRegistry`, `AnalyzePlanStats`, per-step wall-clock and per-port
data accounting on `IProcessor`, per-stage processor grouping on
`SortingStep`/`AggregatingStep`/`JoinStep`), the `ANALYZE` keyword and its
handling in `ParserExplainQuery`/`InterpreterExplainQuery`,
`StreamLocalLimits::forQueryResult`, and the associated tests and documentation.

Related: #106586
Related: #110162

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
pull Bot pushed a commit to Spencerx/ClickHouse that referenced this pull request Jul 12, 2026
This reverts the `EXPLAIN ANALYZE` feature added in
ClickHouse#106586 (merge commit
918a0d3), a revert of that merge with `-m 1`.

It removes the query-executor instrumentation (`StepWallClock`,
`StepWallClockRegistry`, `AnalyzePlanStats`, per-step wall-clock and per-port
data accounting on `IProcessor`, per-stage processor grouping on
`SortingStep`/`AggregatingStep`/`JoinStep`), the `ANALYZE` keyword and its
handling in `ParserExplainQuery`/`InterpreterExplainQuery`,
`StreamLocalLimits::forQueryResult`, and the associated tests and
documentation.

There are no merged follow-up pull requests to revert. The only follow-up,
ClickHouse#110077 ("Reject EXPLAIN ANALYZE
for streaming (STREAM) queries"), is still open and not part of `master`.

The single revert conflict, in
`tests/queries/0_stateless/03269_explain_unique_ids.reference`, was resolved to
keep the unrelated node-id renumbering introduced later by "Fix Not-ready Set
exception by making in-place set build non-destructive" (5583c9a) while
reverting only the `EXPLAIN ANALYZE` step-id change.

Co-Authored-By: Claude Fable 5 <[email protected]>
@Fgrtue

Fgrtue commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov but I took the ownership of the issue here #109176 and did not merge my pull request until I was sure that the problem is resolved properly. In other words I did not leave CI in the state that it was when I was merging, making sure that the problem gets addressed by the person in whose interest it is to merge the pull request (me). Is this considered criminal behavior toward our CI?

filimonov added a commit to filimonov/ClickHouse that referenced this pull request Jul 14, 2026
…wed-threadgroup-async-uaf-fix

Conflict in src/Interpreters/InterpreterExplainQuery.cpp: master reverted the
`EXPLAIN ANALYZE` feature in 93ee2f3 ("Revert 'Explain analyze'", PR ClickHouse#106586),
removing the borrowed-`ThreadGroup` caller that our build fix added
`createForExplainAnalyze` for. Followed the revert: took master's version of the
file and dropped the now-orphaned `createForExplainAnalyze` factory.
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
`EXPLAIN ANALYZE` builds a `StepWallClockRegistry` and asserts (`chassert`) that
every executing processor carrying a query-plan step has a matching wall clock.
The registry is populated by walking the plan via `node->children` and
`step->getChildPlans()`.

When a join's right side is a dictionary (or `StorageJoin`) that must be scanned
(`RIGHT`/`FULL JOIN`), the right-side reading plan is held inside the private
`JoinStepLogicalLookup::child_plan` and its pipeline is built in
`initializePipeline`. `JoinStepLogicalLookup` did not override `getChildPlans`, so
the registry never descended into `child_plan`; the dictionary-reading processors
then carried a step the registry never saw, `find` returned `nullptr`, and
`chassert(clock)` aborted in debug/sanitizer builds (release silently skipped it).

Override `JoinStepLogicalLookup::getChildPlans` to expose `child_plan`, mirroring
`ReadFromMerge`. This registers the sub-plan's steps for wall-clock timing and
makes the dictionary reading sub-plan visible in `EXPLAIN`. Neither pipeline
building nor optimization route through the virtual `getChildPlans`, so there is
no double build or re-optimization. The `FilledJoin`/`DirectKeyValueJoin` cases
drop the lookup node from the plan tree, so their `EXPLAIN` output is unchanged.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106586&sha=63f54b5980fcd8cddb2ed80b37e1434a098eeee0&name_0=PR&name_1=Stress%20test%20%28arm_ubsan%29
Related: ClickHouse#106586

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
This reverts the `EXPLAIN ANALYZE` feature added in
ClickHouse#106586 (merge commit
918a0d3), a revert of that merge with `-m 1`.

It removes the query-executor instrumentation (`StepWallClock`,
`StepWallClockRegistry`, `AnalyzePlanStats`, per-step wall-clock and per-port
data accounting on `IProcessor`, per-stage processor grouping on
`SortingStep`/`AggregatingStep`/`JoinStep`), the `ANALYZE` keyword and its
handling in `ParserExplainQuery`/`InterpreterExplainQuery`,
`StreamLocalLimits::forQueryResult`, and the associated tests and
documentation.

There are no merged follow-up pull requests to revert. The only follow-up,
ClickHouse#110077 ("Reject EXPLAIN ANALYZE
for streaming (STREAM) queries"), is still open and not part of `master`.

The single revert conflict, in
`tests/queries/0_stateless/03269_explain_unique_ids.reference`, was resolved to
keep the unrelated node-id renumbering introduced later by "Fix Not-ready Set
exception by making in-place set build non-destructive" (5583c9a) while
reverting only the `EXPLAIN ANALYZE` step-id change.

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…list

The revert of ClickHouse#106586 removed the `ANALYZE`, `ESTIMATE`, and `TABLE OVERRIDE`
bullets from the `EXPLAIN Types` overview list, but `ESTIMATE` and
`TABLE OVERRIDE` are still advertised in the syntax block and still have their
own `### EXPLAIN ESTIMATE` and `### EXPLAIN TABLE OVERRIDE` sections later in
the same document. Re-add those two bullets (but not `ANALYZE`, which is
removed by this revert) so the page is internally consistent again.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants