Skip to content

fix: bind ^ tighter than *, / and % - #2831

Merged
manticore-projects merged 1 commit into
JSQLParser:masterfrom
ybai08:fix/caret-precedence
Oct 18, 2026
Merged

manticore-projects merged 1 commit into
JSQLParser:masterfrom
ybai08:fix/caret-precedence

Conversation

@ybai08

@ybai08 ybai08 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Since the Pratt rewrite (5.4), ^ has the same precedence as *, /, % and DIV, so 2 * 3 ^ 2 parses as (2 * 3) ^ 2. Both readings of ^ disagree with that:

Expression PostgreSQL 17 (power) MySQL 8.4 (XOR) JSqlParser master This PR
2 * 3 ^ 2 18 = 2 * (3 ^ 2) 2 = 2 * (3 ^ 2) (2 * 3) ^ 2 2 * (3 ^ 2)
10 / 2 ^ 2 2.5 = 10 / (2 ^ 2) NULL = 10 / (2 ^ 2) (10 / 2) ^ 2 10 / (2 ^ 2)
2 ^ 3 * 2 16 2 (2 ^ 3) * 2 unchanged
2 ^ 3 ^ 2 64, left-associative (2 ^ 3) ^ 2 unchanged

5.3 got this right: BitwiseXor was the operand of MultiplicativeExpression. A PostgreSQL query like SELECT pi() * r ^ 2 now gives visitors a BitwiseXor at the top of the tree with a Multiplication inside it. The SQL text still round-trips, because the deparser prints the tree back in the same token order.

The change moves OP_CARET in prattArithRest to the level of ** (prec 7). That is above * / % DIV and still left-associative. ** already sits at that level, so in DuckDB ^ and ** (both power) now share a level, as they do in DuckDB itself. #2795 had pointed out that the two differed. No productions or lookaheads change. Only one precedence constant moves.

One thing I'm unsure about: in T-SQL, ^ sits at the + - level, below *. So with Dialect.SQLSERVER, 2 * 3 ^ 2 was right before this change and is wrong after it. 1 + 2 ^ 3 was already wrong there and stays wrong. Release 5.3 behaved like this PR. If you'd like, I can add a Dialect.SQLSERVER branch that keeps ^ at prec 5.

Tests: two new cases in ExpressionPrecedenceTest, covering *, / and % against ^, ^ on the left, left associativity, and a parse/deparse round trip.

What was run:

  • ./gradlew test --tests ExpressionPrecedenceTest without the grammar change: testCaretBindsTighterThanMultiplication fails (expected: <Multiplication> but was: <BitwiseXor>). With the change, it passes.
  • ./gradlew check (JDK 21, with a JDK 17 toolchain): BUILD SUCCESSFUL. That covers 10,747 tests with 0 failures, plus checkstyle, PMD, spotless, spotbugs and jacoco coverage verification.
  • A differential check against PostgreSQL 17. About 5,700 random integer and boolean expressions using + - * / % ^ & | # << >>, comparisons, BETWEEN, IN, IS NULL, NOT, CASE and ::int were parsed with JSqlParser. Each tree was re-printed fully parenthesised, and PostgreSQL evaluated both forms. With this change, every remaining mismatch involves & | # << >> against + -, which is a separate issue. None is caused by ^.

Not run: gradle jmh. The change only renumbers a precedence constant, so I didn't benchmark it. checkGrammarAmbiguity: no productions changed.

Since the Pratt rewrite, ^ shares the precedence of the multiplicative
operators, so 2 * 3 ^ 2 parses as (2 * 3) ^ 2. PostgreSQL (power) and
MySQL (XOR) both evaluate it as 2 * (3 ^ 2), as did JSqlParser 5.3, where
BitwiseXor was an operand of MultiplicativeExpression.

Move ^ to the level of **, above * / % DIV and still left-associative.
@manticore-projects
manticore-projects merged commit ea892ea into JSQLParser:master Oct 18, 2026
10 checks passed
@manticore-projects

Copy link
Copy Markdown
Contributor

Thank you for fixing this.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants