Skip to content

Fix null pointer dereference when logging a distributed plan local task - #107570

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-distributed-plan-local-null-ast-reland
Jun 16, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-distributed-plan-local-null-ast-reland

Conversation

@groeneai

@groeneai groeneai commented Jun 15, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #107079

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix a crash (null pointer dereference) that could happen when a distributed query plan was executed locally (make_distributed_plan + distributed_plan_execute_locally) with log_formatted_queries = 1.

Description

A local distributed-plan task fragment is built from a serialized query plan, not parsed, so it has no query AST. doExecuteTask (src/QueryPipeline/DistributedPlanExecutor.cpp) passed an empty ASTSelectQuery() to logQueryStart only to carry query_kind. An empty ASTSelectQuery has no SELECT expression list, so with log_formatted_queries = 1 the log path formatted it and ASTSelectQuery::formatImpl dereferenced the absent SELECT (select()->format()), a member call on a null IAST, and the server aborts.

Fix: pass a null ASTPtr instead of the malformed stub. logQueryStart, logQueryFinish and logQueryException already treat a null AST as QueryKind::Select (the kind the stub was constructed to provide), so behavior is unchanged for query_kind while the formatting crash is avoided. The now-dead include of Parsers/ASTSelectQuery.h is removed.

This bug was fixed once before in PR #107079, but that fix was closed obsolete when the Multi-stage distributed queries feature was reverted (#107122) and the feature was then re-landed with the buggy stub intact, so the fix is re-applied here against current master (the surrounding per-task context/thread-group code changed in the re-land).

CI provenance:

Regression test 04327_distributed_plan_local_log_formatted_queries runs the affected path; it aborts the server before this change and passes after.

Version info

  • Merged into: 26.6.1.880

A local distributed-plan task fragment is built from a serialized query
plan, not parsed, so it has no query AST. doExecuteTask passed an empty
ASTSelectQuery() to logQueryStart only to carry query_kind. An empty
ASTSelectQuery has no SELECT expression list, so with
log_formatted_queries = 1 the log path formatted it and
ASTSelectQuery::formatImpl dereferenced the absent SELECT
(select()->format()), a member call on a null IAST. The server aborts.

Fix: pass a null ASTPtr instead of the malformed stub. logQueryStart,
logQueryFinish and logQueryException already treat a null AST as
QueryKind::Select (the kind the stub was constructed to provide), so
behavior is unchanged for query_kind while the formatting crash is
avoided. Remove the now-dead include of Parsers/ASTSelectQuery.h.

Found by the BuzzHouse fuzzer (amd_msan), STID 2257-2710, on the
make_distributed_plan + distributed_plan_execute_locally path. The same
bug was fixed once before (PR ClickHouse#107079) but that fix was closed obsolete
when the feature was reverted, and the feature was then re-landed with
the buggy stub intact, so the fix is re-applied here.

Regression test 04327_distributed_plan_local_log_formatted_queries runs
the affected path; it aborts the server before this change and passes
after.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. SELECT count() FROM t AS x, t AS y WHERE x.id = y.a SETTINGS make_distributed_plan=1, enable_parallel_replicas=0, distributed_plan_execute_locally=1, log_formatted_queries=1 aborts the server every time on master HEAD (build id 0345c487). It is the regression test 04327_distributed_plan_local_log_formatted_queries.
b Root cause explained? Yes. A local distributed-plan task fragment is built from a serialized query plan, so it has no AST. doExecuteTask constructed an empty ASTSelectQuery() only to carry query_kind and passed it to logQueryStart. With log_formatted_queries=1, logQueryStart formats the AST; ASTSelectQuery::formatImpl does select()->format(), but the empty stub has no SELECT expression list, so select() is null and operator->() is a member call on a null IAST -> SIGABRT/SIGSEGV.
c Fix matches root cause? Yes. The fix removes the malformed stub entirely and passes a null ASTPtr. The three log helpers (logQueryStart, logQueryFinish, logQueryException) already default query_kind to QueryKind::Select when the AST is null (executeQuery.cpp:454, :355, :890) which is exactly the kind the stub was hand-built to provide. So query_kind is unchanged and the format path is never entered for a null AST (guarded by if (query_ast && ...) at executeQuery.cpp:451).
d Test intent preserved / new tests added? New test added: 04327_distributed_plan_local_log_formatted_queries (.sql + .reference), tagged no-old-analyzer (make_distributed_plan requires the analyzer). It exercises the exact crash path and asserts the correct result (1000). No existing test changed.
e Both directions demonstrated? Yes. Without the fix (build id 0345c487): server aborts (signal 6), stack ASTSelectQuery::formatImpl -> IAST::format -> formatWithSecretsOneLine -> logQueryStart (executeQuery.cpp:452) -> doExecuteTask (DistributedPlanExecutor.cpp:673), byte-for-byte the CIDB STID 2257-2710 frames. With the fix (build id d29c8e42): the query returns 1000, server stays alive, test passes 25/25 (5 no-random + 20 randomized).
f Fix is general, not a narrow patch? Yes. This is the root-cause fix, not a defensive guard: the bad value (empty AST) is removed at its origin rather than guarding the deref site in the parser. ASTSelectQuery (the empty-stub anti-pattern) appears nowhere else in DistributedPlanExecutor.cpp; the other two logQueryStart callers on master (AsynchronousInsertQueue, RefreshTask) pass fully-populated real ASTs, so they are unaffected. All three log call sites in doExecuteTask (logQueryStart/logQueryFinish/logQueryException) are switched to the null ASTPtr together.

Note: this is a re-application of PR #107079 (closed obsolete after the feature was reverted in #107122 and then re-landed with the buggy stub). The surrounding per-task context/thread-group code changed in the re-land, so the fix was re-applied against current master rather than cherry-picked.

Session id: cron:clickhouse-worker-slot-0:20260615-221900

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @davenger @KochetovNicolai — could you review this? It re-applies the fix for the null-AST crash in the distributed-plan local task executor: doExecuteTask passed an empty ASTSelectQuery() stub to logQueryStart, which crashes in ASTSelectQuery::formatImpl under log_formatted_queries=1. The fix passes a null ASTPtr (the log helpers already default query_kind to Select). Same as #107079, re-applied against the re-landed feature. Found by BuzzHouse (amd_msan), STID 2257-2710.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Jun 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [407aa6c]

Summary: ✅


AI Review

Summary

This PR removes the malformed empty ASTSelectQuery used only for query logging in distributed-plan worker tasks and passes a null ASTPtr through start, finish, and exception logging instead. The null-AST path preserves QueryKind::Select accounting in the logging helpers while avoiding formatting a structurally invalid AST, and the added stateless test covers make_distributed_plan with local execution and log_formatted_queries.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.10% 85.10% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.30% 77.30% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 18/18 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🟢 AI verdict: improvement — 3 query(s) improved out of 38 analysed

This PR only changes query-logging plumbing in the distributed-plan executor (it replaces an unnecessary AST stub with a null pointer) and adds one test. Neither ClickBench nor the TPC-H suite runs with make_distributed_plan enabled, so the diff cannot plausibly change any of these queries' execution time. On that basis the +15.76% on TPC-H Q8 and the high-variance -21% on ClickBench Q22 are read as run-to-run variance and downgraded. The remaining large, consistent improvements (ClickBench Q23, TPC-H Q7 and Q20) are too big to override, but they sit on a code path this PR doesn't touch and most likely reflect build/version baseline differences rather than the change itself.

clickbench

🟢 1 improved · ⚠️ 1 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
🟢 23 improvement 297 203 -31.6% <0.0001 Large, consistent -31% well outside noise, but this PR is logging-only and off ClickBench's path; delta likely reflects
⚠️ 22 not_sure 334 263 -21.3% <0.0001 PR only edits distributed-plan query logging; ClickBench never runs that path. High-variance -21% reads as run-to-run no

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_official

🟢 2 improved · ⚠️ 1 inconclusive

Flagged queries (3 of 22)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
🟢 7 improvement 113 73 -35.4% <0.0001 join_operator: -35% is large and consistent, but PR changes distributed-plan logging only, not join execution. History noisy.
🟢 20 improvement 248 125 -49.6% <0.0001 -49.6% is large but base CV ~88% is very noisy; PR is logging-only/off-path, so delta likely build baseline.
⚠️ 8 not_sure 156 180 +15.8% <0.0001 PR edits distributed-plan logging only; +15.76% on this query is off-path and read as variance despite trending history.

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
  • StressHouse run: f981757b-7c71-4b2c-9def-171842a2978e
  • MIRAI run: 90c6d410-1454-468b-8779-640f502a2550
  • PR check IDs:
    • clickbench_235184_1781612681
    • clickbench_235281_1781612743
    • clickbench_235368_1781612773
    • tpch_adapted_1_official_235478_1781612808
    • tpch_adapted_1_official_235600_1781613000
    • tpch_adapted_1_official_235699_1781613042

@alexey-milovidov
alexey-milovidov merged commit d03beec into ClickHouse:master Jun 16, 2026
325 of 328 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default 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.

5 participants