Skip to content

Add an option to stop treating blank lines as a statement separator - #2800

Merged
manticore-projects merged 4 commits into
JSQLParser:masterfrom
edeadington:fix/optional-blank-line-statement-separator
Oct 13, 2026
Merged

manticore-projects merged 4 commits into
JSQLParser:masterfrom
edeadington:fix/optional-blank-line-statement-separator

Conversation

@edeadington

@edeadington edeadington commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

ST_SEMICOLON matches "\n\n\n", so three newlines in a row end a statement, the same as ;. No SQL dialect ends a statement at a blank line (the closest is SQL*Plus, which does so unless SQLBLANKLINES is on), but generated or hand-formatted SQL often has blank lines inside a single statement, and that SQL then fails to parse at whatever comes after the gap.

This has come up a few times (#1891, #1988, #2156, #2346, #2352). Those were closed because the behaviour is by design, so this PR leaves it alone. Instead it adds Feature.allowBlankLinesAsStatementSeparator, which is enabled by default. Callers who want blank lines treated as plain whitespace can turn it off with withBlankLinesAsSeparator(false):

CCJSqlParserUtil.parse(sql, parser -> parser.withBlankLinesAsSeparator(false));

The ST_SEMICOLON token is unchanged. When the option is off, SimpleCharStream.BeginToken() replaces the first newline of a \n\n\n run with a space before the token manager sees it, as allowHashLineComments already does. Unlike stripping blank lines before parsing, this leaves string literals alone and keeps error line numbers accurate. ;, go and / still separate statements.

SQL Default (unchanged) withBlankLinesAsSeparator(false)
SELECT a\n\n\nFROM t Encountered: <K_FROM> at line 4 SELECT a FROM t
SELECT a FROM t\n\n\nWHERE b = 1 Encountered: <K_WHERE> at line 4 SELECT a FROM t WHERE b = 1
SELECT a FROM t WHERE b = 1\n\n\nAND c = 2 Encountered: <K_AND> at line 4 SELECT a FROM t WHERE b = 1 AND c = 2
SELECT a FROM t\n\n\nUNION\nSELECT b FROM u Encountered: <K_UNION> at line 4 SELECT a FROM t UNION SELECT b FROM u
parseStatements("SELECT 1\n\n\nSELECT 2") 2 statements Parse error, since this is one statement with no separator

testBlankLinesAsSeparatorDisabled covers blank lines in each clause, trailing blank lines, a string literal containing \n\n\n, the other separators, and the reported error position.

edeadington and others added 2 commits October 13, 2026 09:12
Three consecutive newlines end a statement, like `;`. Add
Feature.allowBlankLinesAsStatementSeparator (enabled by default) and
withBlankLinesAsSeparator(boolean) so callers can parse blank lines as
plain whitespace instead. Default behaviour is unchanged.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@edeadington
edeadington marked this pull request as ready for review October 13, 2026 09:19
edeadington and others added 2 commits October 13, 2026 13:04
…line-statement-separator

# Conflicts:
#	src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java
#	src/main/java/net/sf/jsqlparser/parser/feature/Feature.java
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@manticore-projects
manticore-projects merged commit 8f0bd0c into JSQLParser:master Oct 13, 2026
10 checks passed
@manticore-projects

Copy link
Copy Markdown
Contributor

Smart trick, appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants