Fix inconsistent formatting by remembering if an expression was parenthesized#92340
Conversation
|
Workflow [PR], commit [451882c] Summary: ❌
AI ReviewSummaryThe PR makes formatting round-tripping more consistent by preserving whether expressions were explicitly parenthesized in PR Metadata
Findings💡 Nits
ClickHouse Rules
Final Verdict
|
…-an-expression-was-parenthesed
…-an-expression-was-parenthesed
…-an-expression-was-parenthesed
…lag through QueryTree This commit extends the parentheses preservation to work with EXPLAIN SYNTAX, which uses the Analyzer and converts AST -> QueryTree -> AST. Changes: - Add `parenthesized` flag to `IQueryTreeNode` base class with getter/setter - Propagate the flag when building QueryTree from AST in `QueryTreeBuilder` - Set the flag on converted AST in `IQueryTreeNode::toAST` - Copy the flag during `cloneAndReplace` - Add space between consecutive negate operators to avoid `--` ambiguity Now EXPLAIN SYNTAX preserves parentheses just like `clickhouse-format` and client echoing do. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…-an-expression-was-parenthesed
…rsing-by-remembering-if-an-expression-was-parenthesed # Conflicts: # src/Parsers/ASTAssignment.h # src/Parsers/ASTFunction.cpp # src/Parsers/ASTSelectWithUnionQuery.cpp # tests/queries/0_stateless/03773_join_on_formatting.reference # tests/queries/0_stateless/03786_AST_formatting_inconsistencies_in_debug_check.reference
…ssion-was-parenthesed
…ssion-was-parenthesed
…rsing-by-remembering-if-an-expression-was-parenthesed Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
|
||
| void IAST::formatImpl(WriteBuffer & ostr, const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const | ||
| { | ||
| if (parenthesized) |
There was a problem hiding this comment.
IAST::IAST(const IAST & other) does not copy the new parenthesized field. Since many AST nodes clone via make_intrusive<...>(*this), this drops parentheses information on clone paths and can reintroduce inconsistent formatting/round-tripping after transformations.
Please copy parenthesized in both the copy constructor and assignment operator:
- add
, parenthesized(other.parenthesized)in the constructor initializer list; - add
parenthesized = other.parenthesized;inoperator=.
…rsing-by-remembering-if-an-expression-was-parenthesed # Conflicts: # tests/queries/0_stateless/03701_optimize_inverse_dictionary_lookup_basic.reference
The field was accidentally removed from the struct definition while it is still referenced by 34 call sites across 10 files (ASTFunction, ASTCreateQuery, ASTTTLElement, etc.), causing the build to fail. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ignment operator Without this, AST clones (via `make_intrusive<...>(*this)`) would lose the parenthesized flag, reintroducing inconsistent formatting. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
| public: | ||
|
|
||
| /// If the element has extra parentheses around it, e.g., in "a + (b)", b has extra parentheses. | ||
| bool parenthesized = false; |
There was a problem hiding this comment.
This change is user-visible (SELECT ... FORMAT SQL output and AST round-tripping behavior change), so the PR metadata should not use CI Fix or Improvement. Please switch Changelog category to Bug Fix (user-visible misbehavior in an official stable release) so users can discover this fix in release notes.
…-an-expression-was-parenthesed
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix inconsistent formatting by remembering if an expression was parenthesized.
See examples: https://pastila.nl/?012beb19/a57b360b2732ae0c828c9c364a9d67ce#wVKsIzKfqrLVizUOWpVuNw==