You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When formatting SQL, libpgfmt currently drops user-supplied grouping parentheses in WHERE clauses and other boolean expressions. For example, an expression like:
WHERE (sp.cancelled_at IS NULLORsp.cancelled_at>CURRENT_TIMESTAMP) ANDf.feature_name='notifications'
is formatted as:
WHEREsp.cancelled_at IS NULLORsp.cancelled_at>CURRENT_TIMESTAMPANDf.feature_name='notifications'
This matches the reference behavior of the Python pgfmt project (which uses pgparse/libpg_query and normalizes away such parentheses at the AST level). However, since libpgfmt uses tree-sitter-postgres (a concrete syntax tree), it is technically feasible to detect and preserve these user-supplied grouping parentheses.
Raised by @gmr — dropping parentheses is current intentional behavior to match Python pgfmt, with preservation as a planned future improvement.
Goal
Update the expression formatter so that when an OR node (or any lower-precedence boolean operator) is nested inside a higher-precedence boolean context (e.g., AND), the parentheses originally present in the source are emitted in the formatted output to preserve the intended evaluation order and semantics.
Notes
The tree-sitter-postgres CST retains the parenthesized grouping nodes, making this feasible without external heuristics.
Care should be taken to only restore parentheses that were present in the original source, not to add new ones based on operator precedence alone.
Summary
When formatting SQL,
libpgfmtcurrently drops user-supplied grouping parentheses inWHEREclauses and other boolean expressions. For example, an expression like:is formatted as:
This matches the reference behavior of the Python pgfmt project (which uses pgparse/libpg_query and normalizes away such parentheses at the AST level). However, since
libpgfmtusestree-sitter-postgres(a concrete syntax tree), it is technically feasible to detect and preserve these user-supplied grouping parentheses.Background
Discussed in PR #1 (#1) at comment #1 (comment).
Raised by @gmr — dropping parentheses is current intentional behavior to match Python pgfmt, with preservation as a planned future improvement.
Goal
Update the expression formatter so that when an
ORnode (or any lower-precedence boolean operator) is nested inside a higher-precedence boolean context (e.g.,AND), the parentheses originally present in the source are emitted in the formatted output to preserve the intended evaluation order and semantics.Notes
tree-sitter-postgresCST retains the parenthesized grouping nodes, making this feasible without external heuristics.