apache/druid · error · RE
Failed to parse expression: %s
Error message
Failed to parse expression: %s
What it means
Parser.parse walks the ANTLR parse tree and builds the Expr AST; if the listener produces no AST (parsed == null), the input could not be parsed into a valid expression and RE (RuntimeException) 'Failed to parse expression: <in>' is thrown. This indicates syntactically invalid or semantically empty expression text.
Source
Thrown at processing/src/main/java/org/apache/druid/math/expr/Parser.java:142
{
return parse(in, macroTable, true);
}
@VisibleForTesting
public static Expr parse(String in, ExprMacroTable macroTable, boolean withFlatten)
{
ExprLexer lexer = new ExprLexer(new ANTLRInputStream(in));
CommonTokenStream tokens = new CommonTokenStream(lexer);
ExprParser parser = new ExprParser(tokens);
parser.setBuildParseTree(true);
ParseTree parseTree = parser.expr();
ParseTreeWalker walker = new ParseTreeWalker();
ExprListenerImpl listener = new ExprListenerImpl(parseTree, macroTable);
walker.walk(listener, parseTree);
Expr parsed = listener.getAST();
if (parsed == null) {
throw new RE("Failed to parse expression: %s", in);
}
return withFlatten ? flatten(parsed) : parsed;
}
/**
* Create an {@link IdentifierExpr} for some identifier
*/
public static Expr identifier(String identifier)
{
return new IdentifierExpr(identifier);
}
/**
* Create a {@link StringExpr} for a string constant
*/
public static Expr constant(String constant)
{
return new StringExpr(constant);View on GitHub (pinned to 9b90983fd2)
Solutions
- Inspect the expression string for syntax errors (unbalanced parens, stray characters) and fix it.
- Validate the expression with a quick Parser.parse test or the web console expression editor before deploying the spec.
- Check any macros in the expression expand to valid text.
- Wrap parsing in try-catch if user-supplied expressions are accepted, returning a clear validation message.
Example fix
// before String in = "concat(x, )"; // invalid Expr e = Parser.parse(in, null); // after String in = "concat(x, 'y')"; Expr e = Parser.parse(in, null);
Defensive patterns
Strategy: validation
Validate before calling
try { Parser.parse(exprString, null); } catch (Exception e) { /* reject invalid expression up front */ } Try / catch
try { Expr e = Parser.parse(in, macroTable); } catch (RuntimeException e) { if (e.getMessage().startsWith("Failed to parse expression")) { /* surface user-friendly syntax error */ } } Prevention
- Validate user-supplied expressions at spec-submission time
- Use the Druid web console expression editor to check syntax
- Beware macros expanding to empty or malformed text
When it happens
Trigger: Calling Parser.parse(in, macroTable) or Parser.parse(in, flatten) with malformed input — unbalanced parentheses, unknown tokens, empty string after macro substitution, or invalid operator sequences that ANTLR recovers from but yield no AST.
Common situations: Typos in virtual-column/dimension expressions in ingestion specs or query filters; SQL-string quoting issues leaving stray characters; macros expanding to empty/invalid text; hand-edited JSON specs.
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.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- failed to parse address
- failed to parse address
- Expression %s has non-constant inputs.
- Function[%s] %s
- Unable to transform apply function:[%s]
AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07).
Data as JSON: /api/errors/1235e19a8af2ad52.
Report an issue: GitHub.