Skip to content

Conversation

@kyleconroy
Copy link
Collaborator

The 02711_trim_aliases test expects OR chains like "a OR b OR c" to be
flattened to or(a, b, c) with 3 children. However, tests like 01661_join_complex
expect "(a OR b) OR c" to remain nested as or(or(a,b), c) with 2 children.

Both SQL patterns produce identical parse trees due to left-associative parsing.
Our parser doesn't track explicit parenthesization, but ClickHouse's EXPLAIN
output distinguishes between them based on the original SQL syntax.

Since we cannot tell implicit grouping apart from explicit parentheses at the
AST level, any fix for 02711 would break 01661 (and vice versa).

The 02711_trim_aliases test remains in explain_todo as this is a fundamental
limitation of our parser architecture.

The 02711_trim_aliases test expects OR chains like "a OR b OR c" to be
flattened to or(a, b, c) with 3 children. However, tests like 01661_join_complex
expect "(a OR b) OR c" to remain nested as or(or(a,b), c) with 2 children.

Both SQL patterns produce identical parse trees due to left-associative parsing.
Our parser doesn't track explicit parenthesization, but ClickHouse's EXPLAIN
output distinguishes between them based on the original SQL syntax.

Since we cannot tell implicit grouping apart from explicit parentheses at the
AST level, any fix for 02711 would break 01661 (and vice versa).

The 02711_trim_aliases test remains in explain_todo as this is a fundamental
limitation of our parser architecture.
…ization

Add a Parenthesized field to BinaryExpr in the AST to track when an
expression was wrapped in explicit parentheses. This allows the EXPLAIN
output to correctly distinguish between:

- "a OR b OR c" → or(a, b, c) [flattened, no explicit parens]
- "(a OR b) OR c" → or(or(a,b), c) [nested, explicit parens preserved]

Changes:
- ast/ast.go: Add Parenthesized field to BinaryExpr
- parser/expression.go: Set Parenthesized=true when parsing grouped expressions
- internal/explain/expressions.go: Add collectLogicalOperands() that respects
  the Parenthesized flag when flattening OR/AND chains

This fixes the 02711_trim_aliases test and many other tests that were
previously in explain_todo due to incorrect OR/AND chain handling.
@kyleconroy kyleconroy merged commit a1a6e64 into main Dec 30, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants