fix(deparser): preserve parentheses around binary expressions in unary operators#288
Merged
pyramation merged 1 commit intomainfrom Mar 15, 2026
Merged
Conversation
…y operators Fixes #285. The unary operator handler in A_Expr did not wrap a binary A_Expr rexpr in parentheses, causing -(a - b) to deparse as - a - b, which changes operator precedence and produces incorrect math. Also affects TypeCast conversions: (-(a - b))::numeric became CAST(- a - b AS numeric) instead of CAST(- (a - b) AS numeric).
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(deparser): preserve parentheses around binary expressions in unary operators
Summary
Fixes #285. The unary operator branch in the
A_Exprhandler did not wrap binary sub-expressions in parentheses, causing expressions like-(a - b)to deparse as- a - b— silently changing operator precedence and producing mathematically incorrect results.This also affected
TypeCastconversions:(-(a - b))::numericbecameCAST(- a - b AS numeric)instead ofCAST(- (a - b) AS numeric).Fix: When a unary
AEXPR_OPhas anrexprthat is itself a binaryAEXPR_OP(i.e., it has alexpr), wrap the visited sub-expression in parentheses. Unary-inside-unary (e.g.,- -x) is correctly left unwrapped since the inner expression has nolexpr.Two test fixtures added for the exact SQL from the issue report.
Review & Testing Checklist for Human
AEXPR_OPkind with alexpr. OtherA_Exprkinds (AEXPR_IN,AEXPR_BETWEEN,AEXPR_LIKE, etc.) inside a unary operator are not parenthesized by this change. Confirm these don't produce ambiguous output (they likely don't since they use keyword syntax, but worth a quick mental check).parse → deparseon expressions like-(a + b * c),-(a IS NOT NULL),- -x,(-x)::intto verify no over/under-parenthesization.Notes
AEXPR_OPinside unaryAEXPR_OP— to minimize risk of over-parenthesization.Link to Devin run: https://app.devin.ai/sessions/3f7a5f172eff49b388c44b148691911c
Requested by: @pyramation