fix(optimizer): enhance simplify_parens to handle additional parent t…#7339
Open
MuSilk wants to merge 1 commit intotobymao:mainfrom
Open
fix(optimizer): enhance simplify_parens to handle additional parent t…#7339MuSilk wants to merge 1 commit intotobymao:mainfrom
MuSilk wants to merge 1 commit intotobymao:mainfrom
Conversation
VaggelisD
reviewed
Mar 19, 2026
| parent_is_predicate | ||
| or isinstance( | ||
| parent, | ||
| (exp.Neg, exp.Add, exp.Sub, exp.Mul, exp.Div, exp.Mod, exp.Pow), |
Collaborator
There was a problem hiding this comment.
We should instance-check against exp.Binary instead of each one individually e.g isinstance(parent, exp.Neg, exp.Binary)
| or ( | ||
| isinstance(this, exp.Predicate) | ||
| and not (parent_is_predicate or isinstance(parent, exp.Neg)) | ||
| and not ( |
Collaborator
There was a problem hiding this comment.
Can we also add tests in simplify.sql that exercise a couple of these paths? Even something simple as SELECT * FROM A WHERE a - (b < c) >= 0 AND a + (b < c) >= 0
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.
Fixes #7338
Problem
The sqlglot.optimizer.simplify.simplify_parens() function incorrectly removes parentheses around a comparison (exp.Predicate) when it is used as an operand in an arithmetic expression.
In dialects like MySQL, boolean results are treated as integers (0 or 1). Consequently, an expression like a - (b < c) is valid and meaningful. However, the current optimizer logic only checks if the parent is exp.Neg before preserving parentheses for predicates. It fails to account for other binary arithmetic operators (Add, Sub, Mul, etc.).
This leads to incorrect SQL generation where operator precedence is altered, changing the logic of the query.
Reproduction
Input SQL:
Current Incorrect Output:
-- Parentheses around (b < c) are dropped incorrectly
-- This changes precedence: subtraction happens before comparison
SELECT * FROM A WHERE a - b < c >= 0
Note: a - b < c >= 0 is logically equivalent to (a - b) < c AND c >= 0 (depending on dialect chaining rules), which is completely different from a - (result_of_comparison) >= 0.
Root Cause
In sqlglot/optimizer/simplify.py, the simplify_parens function contains a condition:
This logic assumes that only Neg requires parentheses around a predicate, ignoring binary arithmetic operators where the predicate acts as a numeric term.
Solution
Expand the check to include all binary arithmetic operators that have higher or equal precedence implications when a predicate is cast to a number. The fix adds exp.Add, exp.Sub, exp.Mul, exp.Div, exp.Mod, and exp.Pow to the exclusion list.