Skip to content

Allow parenthesized queries in EXPLAIN - #100136

Merged
alexey-milovidov merged 3 commits into
masterfrom
ast-inconsistency-for-explain-with-inner-settings
Mar 20, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
ast-inconsistency-for-explain-with-inner-settings

Conversation

@george-larionov

@george-larionov george-larionov commented Mar 19, 2026 •

Copy link
Copy Markdown
Member

Queries like the following were failing the double parse/format check (see f.e. here):

EXPLAIN AST graph = 1, optimize = 0 
        ALTER ROW POLICY p1 ON d0.`t57` RENAME TO p7 
SETTINGS schema_inference_use_cache_for_s3 = 0;

because the parsed/formatted query would add parentheses around the inner query (because of #99545). I think there is no reason to forbid parenthesized queries in EXPLAIN.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Version info

  • Merged into: 26.4.1.61

@clickhouse-gh

clickhouse-gh Bot commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [15d54a0]

Summary: ✅


AI Review

Summary

This PR fixes a parser/formatter round-trip inconsistency for EXPLAIN AST by allowing a parenthesized inner query and adds a regression test for the access-query case. It also aligns access-policy rename formatting to use backQuoteIfNeed instead of unconditional quoting. I did not find correctness, safety, compatibility, or performance issues that require changes.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Mar 19, 2026
@george-larionov
george-larionov marked this pull request as ready for review March 19, 2026 21:43
@george-larionov

Copy link
Copy Markdown
Member Author

Removed the backticks changes since they were causing test errors

@Ergus Ergus self-assigned this Mar 20, 2026
@Ergus

Ergus commented Mar 20, 2026

Copy link
Copy Markdown
Member

I see some tests failing and apparently they are related with AST

@Ergus Ergus 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.

The fix looks correct, however there are 2 failing tests.

04049_explain_ast_parenthesized_access_queries the ticks are needed in the reference is this intended?. Update the reference file for 04049 to use `p7` instead of p7

03786_AST_formatting_inconsistencies_in_debug_check this test was recently updated on master with new ALTER ROW POLICY and WITH CHECK cases... so let's merge master and try again

@george-larionov
george-larionov force-pushed the ast-inconsistency-for-explain-with-inner-settings branch from 3e89ec9 to 15d54a0 Compare March 20, 2026 18:15
@george-larionov

Copy link
Copy Markdown
Member Author

Should be good now

@george-larionov
george-larionov requested a review from Ergus March 20, 2026 18:17
@clickhouse-gh

clickhouse-gh Bot commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 24.00% 24.00% +0.00%
Branches 76.30% 76.40% +0.10%

PR changed lines: PR changed-lines coverage: 72.97% (27/37, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov
alexey-milovidov merged commit db2fcb2 into master Mar 20, 2026
148 checks passed
@alexey-milovidov
alexey-milovidov deleted the ast-inconsistency-for-explain-with-inner-settings branch March 20, 2026 23:14
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 20, 2026
clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request Oct 1, 2026
…ized query

`EXPLAIN AST` accepts a parenthesized query of any type (ClickHouse#100136), and the
`FORMAT`, `SETTINGS` and `INTO OUTFILE` after the closing parenthesis then
belong to the EXPLAIN. The formatter only re-emitted the parentheses for a
non-`ASTQueryWithOutput` inner query other than `INSERT` (ClickHouse#100174), so it
printed e.g. `EXPLAIN AST INSERT INTO t SELECT 1 FORMAT TSV`, and on re-parse
the inner query took the options: `ParserInsertQuery` takes a `FORMAT` after a
SELECT or INFILE and `SETTINGS` under `allow_settings_after_format_in_insert`,
`DELETE`/`UPDATE` take `SETTINGS`, and every statement parsed by
`ParserQueryWithOutput` (CREATE, ALTER, DROP, SHOW, ...) takes all of them.
Debug builds abort with `Inconsistent AST formatting` (STID 1941-26fa,
BuzzHouse), and in release builds `formatQuery` returns a different query.

For `EXPLAIN AST` with trailing output options, wrap every inner query except
a SELECT or EXPLAIN, which would re-parse as a subquery and already own their
options in a text parse. Inside the parentheses, format the query without the
trailing-options flags, because the closing parenthesis already separates it
(same as `ASTSubquery` and the AS-select of `ASTCreateQuery`). Other EXPLAIN
kinds keep the previous rule: they have no parenthesized form for non-SELECT
queries.

Not covered: a nested `EXPLAIN AST (EXPLAIN SYNTAX INSERT ...) FORMAT ...`,
where the wrap would collide with the `(EXPLAIN ...)` subquery rewrite.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

4 participants