GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: bind ^ tighter than *, / and % by ybai08 · Pull Request #2831 · JSQLParser/JSqlParser

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ybai08
fix: bind ^ tighter than *, / and %
9b8ddbe
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

Uh oh!

There was an error while loading. Please reload this page.

@manticore-projects

manticore-projects commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Thank you for fixing this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ybai08 ybai08 mentioned this pull request Oct 18, 2026
fix: bind bitwise operators looser than + and - #2833
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@ybai08 @manticore-projects
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.