{"record":{"id":"1235e19a8af2ad52","repo":"apache/druid","slug":"failed-to-parse-expression-s","errorCode":null,"errorMessage":"Failed to parse expression: %s","messagePattern":"Failed to parse expression: (.+?)","errorType":"exception","errorClass":"RE","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/math/expr/Parser.java","lineNumber":142,"sourceCode":"  {\n    return parse(in, macroTable, true);\n  }\n\n\n  @VisibleForTesting\n  public static Expr parse(String in, ExprMacroTable macroTable, boolean withFlatten)\n  {\n    ExprLexer lexer = new ExprLexer(new ANTLRInputStream(in));\n    CommonTokenStream tokens = new CommonTokenStream(lexer);\n    ExprParser parser = new ExprParser(tokens);\n    parser.setBuildParseTree(true);\n    ParseTree parseTree = parser.expr();\n    ParseTreeWalker walker = new ParseTreeWalker();\n    ExprListenerImpl listener = new ExprListenerImpl(parseTree, macroTable);\n    walker.walk(listener, parseTree);\n    Expr parsed = listener.getAST();\n    if (parsed == null) {\n      throw new RE(\"Failed to parse expression: %s\", in);\n    }\n    return withFlatten ? flatten(parsed) : parsed;\n  }\n\n  /**\n   * Create an {@link IdentifierExpr} for some identifier\n   */\n  public static Expr identifier(String identifier)\n  {\n    return new IdentifierExpr(identifier);\n  }\n\n  /**\n   * Create a {@link StringExpr} for a string constant\n   */\n  public static Expr constant(String constant)\n  {\n    return new StringExpr(constant);","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/math/expr/Parser.java#L124-L160","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nString in = \"concat(x, )\"; // invalid\nExpr e = Parser.parse(in, null);\n// after\nString in = \"concat(x, 'y')\";\nExpr e = Parser.parse(in, null);","handlingStrategy":"validation","validationCode":"try { Parser.parse(exprString, null); } catch (Exception e) { /* reject invalid expression up front */ }","typeGuard":null,"tryCatchPattern":"try { Expr e = Parser.parse(in, macroTable); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"Failed to parse expression\")) { /* surface user-friendly syntax error */ } }","preventionTips":["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"],"tags":["expression","parse-error","syntax","druid"],"backgroundTag":"invalid-argument-format","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}