Skip to content

feat(parser,ast): preserve parenthesized expressions (fix #519) - #532

Open
basili4-1982 wants to merge 1 commit into
ajitpratap0:mainfrom
basili4-1982:feat/parens-in-where
Open

feat(parser,ast): preserve parenthesized expressions (fix #519)#532
basili4-1982 wants to merge 1 commit into
ajitpratap0:mainfrom
basili4-1982:feat/parens-in-where

Conversation

@basili4-1982

@basili4-1982 basili4-1982 commented Aug 21, 2026

Copy link
Copy Markdown

Fixes #519.

Summary

Parenthesized expressions in WHERE (and anywhere an expression appears) were
silently dropped during parsing, so group-precedence was lost on re-render:

input  : SELECT * FROM t WHERE (a = 1 OR b = 2) AND c = 3
current: SELECT * FROM t WHERE a = 1 OR b = 2 AND c = 3

This silently changes the meaning of the predicate.

Root cause

parsePrimaryExpression parsed (expr) and returned the bare expr, discarding
the grouping. There was no AST node to represent parentheses, so round-trip
could not preserve them.

Changes

  • pkg/sql/ast/ast_expressions.go: new ParenthesizedExpression node
    (Expr, Pos).
  • pkg/sql/ast/sql.go: (*ParenthesizedExpression).SQL() emits (...).
  • pkg/sql/parser/expressions_literal.go: single parenthesized primary
    expressions now wrap in a ParenthesizedExpression (tuple (a, b, c)
    and subquery (SELECT ...) paths unchanged).
  • pkg/sql/ast/pool_expression_release.go: release child expr; the node itself
    needs no dedicated pool.
  • pkg/sql/ast/roundtrip_test.go: grouped-OR and nested-parens cases.
  • pkg/sql/parser/parenthesized_expression_test.go: regression tests.

Test plan

  • New regression tests TestParenthesizedExpressionRoundtrip pass
  • go test -short ./pkg/sql/ast/ ./pkg/sql/parser/ ./pkg/gosqlx/ ./pkg/formatter/ green
  • gosqlx.Format(...) preserves parentheses
  • Nested parens ((a AND b) OR c) AND d round-trip unchanged

Note: TestPerformanceRegression fails in this environment against its
baseline (5 cases) — unrelated to this change, reproduced on unmodified main.

…atap0#519)

Add a ParenthesizedExpression AST node so grouped predicates round-trip
unchanged. Previously parentheses were dropped during parsing, silently
changing AND/OR precedence: (a OR b) AND c re-rendered as a OR b AND c.

- ast_expressions.go: new ParenthesizedExpression node (Expr, Pos)
- ast/sql.go: SQL() emits (...)
- expressions_literal.go: wrap single parenthesized primary expressions
- pool_expression_release.go: release child expr; node needs no pool
- roundtrip_test.go: grouped OR and nested-parens cases
- parenthesized_expression_test.go: regression tests (CH dialect)
@vercel

vercel Bot commented Aug 21, 2026

Copy link
Copy Markdown

@basili4-1982 is attempting to deploy a commit to the coolajitpratapsingh0-9932's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added parser ast AST node changes labels Aug 21, 2026
@basili4-1982

Copy link
Copy Markdown
Author

Проверено, готово к мержу.

  • mergeable: MERGEABLE — без конфликтов с main.
  • Тесты зелёные: go test -short ./pkg/sql/ast/ ./pkg/sql/parser/ ./pkg/formatter/ ./pkg/gosqlx/ (включая TestParenthesizedExpressionRoundtrip), gosqlx.Format тоже сохраняет скобки.
  • Единственный красный check — Vercel (Authorization required to deploy): деплой форка, не связан с кодом. Apply labels и GitGuardian проходят.

Прошу ревью/мерж. Фикс закрывает #519 (скобки в WHERE не теряются).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ast AST node changes parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] GoSQLX does not support parentheses

1 participant