{"record":{"id":"eb0aaf9f619138e3","repo":"provectus/kafka-ui","slug":"error-parsing-ksql-query","errorCode":null,"errorMessage":"Error parsing ksql query: ","messagePattern":"Error parsing ksql query: ","errorType":"validation","errorClass":"ValidationException","httpStatus":400,"severity":"error","filePath":"kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/ksql/KsqlGrammar.java","lineNumber":61,"sourceCode":"\n  private static ksql.KsqlGrammarParser.StatementsContext parseStatements(final String sql) {\n    var lexer = new KsqlGrammarLexer(CaseInsensitiveStream.from(CharStreams.fromString(sql)));\n    var tokenStream = new CommonTokenStream(lexer);\n    var grammarParser = new ksql.KsqlGrammarParser(tokenStream);\n\n    lexer.addErrorListener(new BaseErrorListener() {\n      @Override\n      public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,\n                              int line, int charPositionInLine,\n                              String msg, RecognitionException e) {\n        throw new ValidationException(\"Invalid syntax: \" + msg);\n      }\n    });\n    grammarParser.getInterpreter().setPredictionMode(PredictionMode.LL);\n    try {\n      return grammarParser.statements();\n    } catch (Exception e) {\n      throw new ValidationException(\"Error parsing ksql query: \" + e.getMessage());\n    }\n  }\n\n  // impl copied from https://github.com/confluentinc/ksql/blob/master/ksqldb-parser/src/main/java/io/confluent/ksql/parser/CaseInsensitiveStream.java\n  @RequiredArgsConstructor\n  private static class CaseInsensitiveStream implements CharStream {\n    @Delegate\n    final CharStream stream;\n\n    public static CaseInsensitiveStream from(CharStream stream) {\n      // we only need to override LA method\n      return new CaseInsensitiveStream(stream) {\n        @Override\n        public int LA(final int i) {\n          final int result = stream.LA(i);\n          switch (result) {\n            case 0:\n            case IntStream.EOF:","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/provectus/kafka-ui/blob/83b5a60cc08501b570a0c4d0b4cdfceb1b88d6b7/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/ksql/KsqlGrammar.java#L43-L79","documentation":"KsqlGrammar.parseStatements catches any exception raised while driving the ANTLR parser over the ksql text (beyond listener-reported syntax errors) and rethrows it as ValidationException('Error parsing ksql query: ' + message). It is the fallback path for parser failures.","triggerScenarios":"parseStatements (exposed via KsqlGrammar.parsed) throwing a non-ANTLR exception during parsing — e.g. internal parser errors, PredictionMode.LL failures, stream/IO problems in the case-insensitive CharStream wrapper.","commonSituations":"Extremely large or exotic queries stressing the parser; grammar edge cases not handled by the copied Confluent implementation; corrupted input encoding.","solutions":["Read the wrapped cause message after 'Error parsing ksql query: ' to identify the parser failure","Simplify the query into smaller statements and parse them individually","Validate the query with the ksqlDB CLI/parser to isolate the failing construct","Report/upgrade if the bundled grammar lags behind your ksqlDB server version"],"exampleFix":"// before\nKsqlGrammar.parseStatements(complexQuery); // single opaque failure\n// after\nfor (String stmt : complexQuery.split(\";\")) {\n  KsqlGrammar.parseStatements(stmt + \";\"); // isolates failing statement\n}","handlingStrategy":"try-catch","validationCode":"// Pre-check: reject obviously malformed multi-statement input\nif (query == null || query.isBlank()) throw new IllegalArgumentException(\"ksql query is empty\");","typeGuard":null,"tryCatchPattern":"try {\n  var parser = KsqlGrammar.parsed(query);\n} catch (ValidationException e) {\n  if (e.getMessage().startsWith(\"Error parsing ksql query:\")) {\n    log.error(\"ksql parse failed: {}\", e.getMessage());\n  } else { throw e; }\n}","preventionTips":["Keep queries modest in size/complexity; split large scripts into statements","Ensure UTF-8 clean input without invisible characters","Test against the same kafka-ui/ANTLR grammar version in CI","Keep kafka-ui updated to track ksqlDB syntax changes"],"tags":["ksql","antlr","parsing","validation"],"backgroundTag":"json-parse-error","analyzedSha":"83b5a60cc08501b570a0c4d0b4cdfceb1b88d6b7","analyzedAt":"2026-09-08T04:35:39.002Z","contentChangedAt":"2026-09-08T04:35:39.002Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}