Skip to content

MySQL bitwise |, &, and shifts still share one parser precedence tier #2834

Open

Description

@pbottine

Reproduction

At current master commit f6ac2571e6e9777d6a5ee942a4ea53cd91153e4d, compile and run this with the built JAR on the classpath:

import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.Select;
public class Repro {
  static String shape(Expression e) {
    if (e instanceof BinaryExpression b)
      return "(" + shape(b.getLeftExpression()) + " " + e.getClass().getSimpleName()
          + " " + shape(b.getRightExpression()) + ")";
    return e.toString();
  }
  public static void main(String[] args) throws Exception {
    for (String x : new String[]{"1 | 2 & 4", "3 & 1 << 2"}) {
      Select s = (Select) CCJSqlParserUtil.parse("SELECT " + x + " AS x",
          p -> p.withDialect(Dialect.MYSQL));
      System.out.println(x + " => " + shape(s.getPlainSelect().getSelectItem(0).getExpression()));
    }
  }
}

Observed on 2026-10-19: 1 | 2 & 4 becomes (1 | 2) & 4, and 3 & 1 << 2 becomes (3 & 1) << 2. MySQL's precedence table requires 1 | (2 & 4) and 3 & (1 << 2). These paired groupings evaluate to 0 versus 1, and 4 versus 0, respectively.

The arithmetic parser loop assigns precedence 4 to |, &, <<, and >> under the MySQL dialect. PR #2833 corrected their tier relative to addition and explicitly notes that MySQL separates these bitwise operators. This issue tracks that remaining MySQL-specific grouping difference.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions