Skip to content

parseStatements returns null instead of throwing JSQLParserException when withAllowComplexParsing(false) and the input does not parse #2576

Description

@stbischof

CCJSqlParserUtil.parseStatements(sql, consumer) returns null instead of throwing JSQLParserException when complex parsing is switched off and the input does not parse. A caller that relies on the declared exception dereferences the result and fails with a NullPointerException of its own; a guard that treats "no exception" as "parsed" is misled.

Version: com.manticore-projects.jsqlformatter:jsqlparser:5.4.351-SNAPSHOT (build 5.4.351-20260910.100601-1), master at b2115ac8.

Reproduce

// default configuration: the documented exception
CCJSqlParserUtil.parseStatements("PRAGMA foo");
// -> JSQLParserException: Encountered: <S_IDENTIFIER> / "PRAGMA", at line 1, column 1 ...

// complex parsing off: null, no exception
Statements s = CCJSqlParserUtil.parseStatements("PRAGMA foo", p -> p.withAllowComplexParsing(false));
// s == null

// same for a broken script and for an empty statement between separators
CCJSqlParserUtil.parseStatements("SELECT * FROM foo; PRAGMA x", p -> p.withAllowComplexParsing(false)); // null
CCJSqlParserUtil.parseStatements("SELECT * FROM foo;;",         p -> p.withAllowComplexParsing(false)); // null

// the single-statement entry point throws as expected with the same option
CCJSqlParserUtil.parse("PRAGMA foo", p -> p.withAllowComplexParsing(false));
// -> JSQLParserException

Also observed: parseStatements("/* nothing */") (default configuration) returns an empty Statements while parse("/* nothing */") throws JSQLParserException; the two entry points disagree on comment-only input.

Where

CCJSqlParserUtil.parseStatements(String, ExecutorService, Consumer) (CCJSqlParserUtil.java:471-501): the simple-parse attempt fails, the complex re-parse is skipped because allowComplexParsing is false, and the method falls through returning the unassigned result instead of rethrowing the first ParseException.

Suggestion

Rethrow the first parse failure as JSQLParserException when the complex re-parse is disabled (the way parse(String, ExecutorService, Consumer) does), and align the two entry points on comment-only input, either both empty or both throwing.

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