apache/druid · error · RE

Unrecognized binary operator %s

Error message

Unrecognized binary operator %s

What it means

ExprListenerImpl.exitAddSubExpr maps '+'/'-' (add/sub parse contexts) tokens to BinaryPlusExpr/BinaryMinusExpr. An unexpected operator token in that context falls to the default branch and throws 'Unrecognized binary operator %s', meaning grammar and listener disagree or the input is malformed.

Source

Thrown at processing/src/main/java/org/apache/druid/math/expr/ExprListenerImpl.java:140

            new BinPlusExpr(
                ctx.getChild(1).getText(),
                (Expr) nodes.get(ctx.getChild(0)),
                (Expr) nodes.get(ctx.getChild(2))
            )
        );
        break;
      case ExprParser.MINUS:
        nodes.put(
            ctx,
            new BinMinusExpr(
                ctx.getChild(1).getText(),
                (Expr) nodes.get(ctx.getChild(0)),
                (Expr) nodes.get(ctx.getChild(2))
            )
        );
        break;
      default:
        throw new RE("Unrecognized binary operator %s", ctx.getChild(1).getText());
    }
  }

  @Override
  public void exitLongExpr(ExprParser.LongExprContext ctx)
  {
    nodes.put(
        ctx,
        new BigIntegerExpr(new BigInteger(ctx.getText()))
    );
  }

  @Override
  public void exitLogicalAndOrExpr(ExprParser.LogicalAndOrExprContext ctx)
  {
    int opCode = ((TerminalNode) ctx.getChild(1)).getSymbol().getType();
    switch (opCode) {
      case ExprParser.AND:

View on GitHub (pinned to 9b90983fd2)

Solutions

  1. Fix the expression string to use '+' or '-' in additive positions
  2. Regenerate the ANTLR parser after any grammar change and add the missing case in exitAddSubExpr
  3. Upgrade to a stock Druid version with consistent grammar/listener

Example fix

// before
"a ~ b" in additive context
// after
"a + b"
Defensive patterns

Strategy: validation

Validate before calling

// ensure only + or - appear in additive positions of generated expressions
if (!generatedExpr.matches("[a-zA-Z0-9_+\- ()]*")) {
  throw new IllegalArgumentException("invalid operator in expression: " + generatedExpr);
}

Try / catch

try {
  expr = Expr.parse(exprStr);
} catch (RuntimeException e) {
  if (e.getMessage() != null && e.getMessage().startsWith("Unrecognized binary operator")) {
    throw new IllegalArgumentException("Invalid expression: " + exprStr, e);
  } else { throw e; }
}

Prevention

When it happens

Trigger: Parsing an expression where the add/sub rule sees a token other than '+' or '-', e.g. corrupt expression text or a grammar/listener mismatch after modifying the Expr.g4 grammar.

Common situations: Programmatically generated expression strings with bad operator tokens; custom Druid builds where the ANTLR grammar was extended but exitAddSubExpr wasn't updated; garbled query text from upstream tools.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/476928c7c1f3c7c5. Report an issue: GitHub.