Closed
Description
Describe the bug
NotExpression is not parsed correctly.
To Reproduce
Steps to reproduce the behavior:
CCJSqlParserUtil.parseCondExpression("not not 1 = 1")generates following objects:NotExpression ( EqualsTo ( NotExpression ( 1 ) = 1 ) ). Instead, the result should be:NotExpression ( NotExpression ( EqualsTo (1 = 1) ) )CCJSqlParserUtil.parseCondExpression("not not not 1 = 1")yields Exception:net.sf.jsqlparser.JSQLParserException: Encountered unexpected token: "not" "NOT"
Expected behavior
Based on SQL operator precedence and associativity, NOT operator should be evaluated after equal (=), and NOT operator should be evaluated from right to left.
System
- Database you are using - irrelative
- Java Version - 1.8
- JSqlParser version - 4.0
Metadata
Metadata
Assignees
Labels
No labels
Activity
wumpz commentedon Apr 28, 2021
You are right, that not expressions are not truly hierarchical handled by JSqlParser. This would lead to heavy grammar refactoring, I think.
Is this a pressing feature you need?
fudianchn commentedon Sep 6, 2026
Fixed in #2556.
Root cause:
Condition()inJSqlParserCC.jjtconsumed at most one leadingNOT/!token. A second one leaked intoPrimaryExpression()and attached to the left operand, sonot not 1 = 1parsed asNOT (NOT 1) = 1instead ofNOT (NOT (1 = 1)), and a third one raised the reportedParseException. The prefix is now a loop that wraps oneNotExpressionper token, leftmost token outermost.Both reported cases still reproduce on current master and are fixed by the PR; adjacent single-
NOTforms (NOT a LIKE,NOT a IN, value-positiona = NOT 1, ...) produce identical trees as before.fix(parser): parse stacked NOT prefixes as nested NotExpression (JSQL…
fix(parser): parse stacked NOT prefixes as nested NotExpression (#1170)…