From 2b31e906aa1e18dc039843844f9de59869e3d579 Mon Sep 17 00:00:00 2001 From: tanjiro Kamado Date: Thu, 19 Nov 2026 23:01:45 +0530 Subject: [PATCH] fix: lex // as a line comment only for H2 and Snowflake LINE_COMMENT matched // in every dialect, but only H2 and Snowflake have // comments. Everywhere else //* is a division followed by a block comment, so SELECT 6 //*x*/ 2, (SELECT max(pw) FROM secrets) parsed as SELECT 6 while MySQL, MariaDB, PostgreSQL, SQL Server and SQLite run the subquery. // comments are now a lexer switch like # comments (Feature.allowDoubleSlashLineComments, withDoubleSlashLineComments), turned on by the H2 and SNOWFLAKE presets. --- .../jsqlparser/parser/AbstractJSqlParser.java | 24 ++++++++--- .../jsqlparser/parser/SimpleCharStream.java | 19 ++++++-- .../sf/jsqlparser/parser/feature/Feature.java | 6 +++ .../net/sf/jsqlparser/parser/JSqlParserCC.jjt | 6 ++- src/site/sphinx/usage.rst | 6 ++- .../parser/CCJSqlParserUtilTest.java | 43 ++++++++++++++++++- .../parser/LeadingCommentsTest.java | 3 +- 7 files changed, 92 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java b/src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java index 28d969ed16..04c61aec34 100644 --- a/src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java +++ b/src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java @@ -42,15 +42,17 @@ public enum Dialect { Feature.allowSquareBracketQuotation), POSTGRESQL( AdjacentStringLiterals.NEWLINE, Feature.allowDollarQuotedStringTags, - Feature.allowDigitSeparators), H2, EXASOL, BIGQUERY( - AdjacentStringLiterals.WHITESPACE, - Feature.allowDoubleQuotedStrings, - Feature.allowHashLineComments, - Feature.allowBackslashEscapeCharacter), DATABRICKS( + Feature.allowDigitSeparators), H2( + Feature.allowDoubleSlashLineComments), EXASOL, BIGQUERY( AdjacentStringLiterals.WHITESPACE, Feature.allowDoubleQuotedStrings, - Feature.allowBackslashEscapeCharacter), SNOWFLAKE( - Feature.allowBackslashEscapeCharacter), INFORMIX, SPANNER, DORIS, COCKROACHDB, TERADATA, SQLITE; + Feature.allowHashLineComments, + Feature.allowBackslashEscapeCharacter), DATABRICKS( + AdjacentStringLiterals.WHITESPACE, + Feature.allowDoubleQuotedStrings, + Feature.allowBackslashEscapeCharacter), SNOWFLAKE( + Feature.allowBackslashEscapeCharacter, + Feature.allowDoubleSlashLineComments), INFORMIX, SPANNER, DORIS, COCKROACHDB, TERADATA, SQLITE; private final Set lexerFeatures; private final AdjacentStringLiterals adjacentStringLiterals; @@ -197,6 +199,14 @@ public P withHashLineComments(boolean allowHashLineComments) { return withFeature(Feature.allowHashLineComments, allowHashLineComments); } + public P withDoubleSlashLineComments() { + return withFeature(Feature.allowDoubleSlashLineComments, true); + } + + public P withDoubleSlashLineComments(boolean allowDoubleSlashLineComments) { + return withFeature(Feature.allowDoubleSlashLineComments, allowDoubleSlashLineComments); + } + public P withBlankLinesAsSeparator(boolean allowBlankLinesAsStatementSeparator) { return withFeature(Feature.allowBlankLinesAsStatementSeparator, allowBlankLinesAsStatementSeparator); diff --git a/src/main/java/net/sf/jsqlparser/parser/SimpleCharStream.java b/src/main/java/net/sf/jsqlparser/parser/SimpleCharStream.java index 5b6c5e8259..b4791c5808 100644 --- a/src/main/java/net/sf/jsqlparser/parser/SimpleCharStream.java +++ b/src/main/java/net/sf/jsqlparser/parser/SimpleCharStream.java @@ -10,10 +10,10 @@ /* Generated By:JavaCC: Do not edit this line by hand. SimpleCharStream.java Version 8.0.0 */ /* * Warning: this file is maintained by hand on top of the JavaCC template. Among other manual - * adjustments it carries the in-buffer '#' and blank line rewrites in BeginToken(), which - * Feature.allowHashLineComments and Feature.allowBlankLinesAsStatementSeparator depend on. Never - * overwrite it with a freshly generated SimpleCharStream, that would silently drop the rewrites and - * break both features. + * adjustments it carries the in-buffer '#', '//' and blank line rewrites in BeginToken(), which + * Feature.allowHashLineComments, Feature.allowDoubleSlashLineComments and + * Feature.allowBlankLinesAsStatementSeparator depend on. Never overwrite it with a freshly + * generated SimpleCharStream, that would silently drop the rewrites and break these features. */ package net.sf.jsqlparser.parser; @@ -66,6 +66,10 @@ public class SimpleCharStream { static final char HASH_SUBSTITUTION = '\u0001'; static final char MYSQL_MINUS_SUBSTITUTION = '\u0002'; static final char MYSQL_COMMENT_ERROR = '\u0003'; + // H2 and Snowflake `//` line comments: the same rewrite for the first `/` + // of a token-start `//` under Feature.allowDoubleSlashLineComments. Without + // it no rule matches `//`, so `//*` is a division and a block comment. + static final char DOUBLE_SLASH_SUBSTITUTION = '\u0004'; FeatureConfiguration featureConfiguration; private boolean mySqlExecutableComment; String mySqlCommentError; @@ -241,6 +245,11 @@ public final char BeginToken() throws java.io.IOException { && featureConfiguration.getAsBoolean(Feature.allowHashLineComments)) { buffer[bufpos] = HASH_SUBSTITUTION; c = HASH_SUBSTITUTION; + } else if (c == '/' && featureConfiguration != null + && featureConfiguration.getAsBoolean(Feature.allowDoubleSlashLineComments) + && peekCharacters(1).equals("/")) { + buffer[bufpos] = DOUBLE_SLASH_SUBSTITUTION; + c = DOUBLE_SLASH_SUBSTITUTION; } else if ((c == '-' || c == '/' || c == '*') && featureConfiguration != null && AbstractJSqlParser.Dialect.MYSQL.name() .equals(featureConfiguration.getValue(Feature.dialect))) { @@ -526,6 +535,8 @@ public String GetImage() { image = "#" + image.substring(1); } else if (!image.isEmpty() && image.charAt(0) == MYSQL_MINUS_SUBSTITUTION) { image = "-" + image.substring(1); + } else if (!image.isEmpty() && image.charAt(0) == DOUBLE_SLASH_SUBSTITUTION) { + image = "/" + image.substring(1); } return image; } diff --git a/src/main/java/net/sf/jsqlparser/parser/feature/Feature.java b/src/main/java/net/sf/jsqlparser/parser/feature/Feature.java index 60f8850dc7..5937352126 100644 --- a/src/main/java/net/sf/jsqlparser/parser/feature/Feature.java +++ b/src/main/java/net/sf/jsqlparser/parser/feature/Feature.java @@ -1426,6 +1426,12 @@ public enum Feature { */ allowHashLineComments(false), + /** + * allows H2 and Snowflake `//` line comments; disabled by default, where `//*` is a division + * followed by a block comment as in MySQL, PostgreSQL, SQL Server and SQLite + */ + allowDoubleSlashLineComments(false), + /** * treats three consecutive newlines as a statement separator (enabled by default). When * disabled, blank lines are plain whitespace, so a statement containing them is parsed whole diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index 0d61a3f086..0925d05111 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -6855,9 +6855,13 @@ TOKEN : /* Numeric Constants */ | < #HEX_DIGIT: ["0"-"9","A"-"F"] > } +// `//` starts a line comment only under Feature.allowDoubleSlashLineComments +// (H2, Snowflake), where the stream rewrites its first `/` to +// SimpleCharStream.DOUBLE_SLASH_SUBSTITUTION as for `#` below. Elsewhere +// `//*` is a division followed by a block comment. SPECIAL_TOKEN: { - < LINE_COMMENT: ("--" | "//") (~["\r","\n"])*> + < LINE_COMMENT: ("--" | "\u0004/") (~["\r","\n"])*> } // MySQL `#` line comment (#2499), flag-gated. Under diff --git a/src/site/sphinx/usage.rst b/src/site/sphinx/usage.rst index bbf0f1f907..7c23c74223 100644 --- a/src/site/sphinx/usage.rst +++ b/src/site/sphinx/usage.rst @@ -1745,7 +1745,9 @@ One grammar covers every supported RDBMS, but a few pieces of syntax mean differ * - ``DATABRICKS`` - ``withDoubleQuotedStrings``, ``withBackslashEscapeCharacter``, any-whitespace rule for adjacent string literals * - ``SNOWFLAKE`` - - ``withBackslashEscapeCharacter`` only, double quotes stay quoted identifiers + - ``withBackslashEscapeCharacter`` and ``withDoubleSlashLineComments``, double quotes stay quoted identifiers + * - ``H2`` + - ``withDoubleSlashLineComments`` * - ``INFORMIX`` - Informix ``ALTER TABLE ... ADD CONSTRAINT`` definitions with optional trailing constraint names * - ``SPANNER`` @@ -2097,6 +2099,8 @@ The individual features - ``".."`` reads as a string literal instead of a quoted identifier (BigQuery, Spark/Databricks, MySQL default ``sql_mode``); combined with ``withBackslashEscapeCharacter``, ``\\"`` does not end the literal, as in MySQL ``"it\\"s"`` * - ``withHashLineComments`` - ``#`` starts a line comment + * - ``withDoubleSlashLineComments`` + - ``//`` starts a line comment (H2, Snowflake); otherwise ``//*`` is a division followed by a block comment * - ``withBlankLinesAsSeparator`` - ``false`` stops two empty lines (``\n\n\n``) ending a statement, so they read as plain whitespace * - ``withAdjacentStringLiterals`` diff --git a/src/test/java/net/sf/jsqlparser/parser/CCJSqlParserUtilTest.java b/src/test/java/net/sf/jsqlparser/parser/CCJSqlParserUtilTest.java index bf74eb6a3c..8f8efe97b5 100644 --- a/src/test/java/net/sf/jsqlparser/parser/CCJSqlParserUtilTest.java +++ b/src/test/java/net/sf/jsqlparser/parser/CCJSqlParserUtilTest.java @@ -367,7 +367,8 @@ public void testNestingDepth() throws Exception { @Test public void testParseStatementIssue1250() throws Exception { Statement result = CCJSqlParserUtil.parse( - "Select test.* from (Select * from sch.PERSON_TABLE // root test\n) as test"); + "Select test.* from (Select * from sch.PERSON_TABLE // root test\n) as test", + parser -> parser.withDoubleSlashLineComments()); assertEquals("SELECT test.* FROM (SELECT * FROM sch.PERSON_TABLE) AS test", result.toString()); } @@ -668,6 +669,46 @@ public void testHashLineCommentsMySQLStatementSemantics() throws Exception { CCJSqlParserUtil.parse("SELECT 1 # comment").toString()); } + @Test + public void testDoubleSlashLineCommentsFeature() throws Exception { + // default (flag off): `//*` is a division followed by a block comment, as on MySQL, + // MariaDB, PostgreSQL, SQL Server and SQLite, which all return the subquery here + String sql = "SELECT 6 //*x*/ 2, (SELECT max(pw) FROM secrets)"; + assertEquals("SELECT 6 / 2, (SELECT max(pw) FROM secrets)", + CCJSqlParserUtil.parse(sql).toString()); + for (AbstractJSqlParser.Dialect dialect : new AbstractJSqlParser.Dialect[] { + AbstractJSqlParser.Dialect.MYSQL, AbstractJSqlParser.Dialect.POSTGRESQL, + AbstractJSqlParser.Dialect.SQLSERVER, AbstractJSqlParser.Dialect.SQLITE}) { + assertEquals("SELECT 6 / 2, (SELECT max(pw) FROM secrets)", + CCJSqlParserUtil.parse(sql, p -> p.withDialect(dialect)).toString()); + } + assertEquals(2, + CCJSqlParserUtil.parseStatements("SELECT 1 //**/ 1; DROP TABLE users").size()); + assertThrows(JSQLParserException.class, + () -> CCJSqlParserUtil.parse("SELECT 1 // comment")); + + // with the flag or the H2 and SNOWFLAKE presets: `//` to the end of the line, `//*` + // included, as H2 and Snowflake read it + for (Consumer consumer : List.>of( + p -> p.withDoubleSlashLineComments(), + p -> p.withDialect(AbstractJSqlParser.Dialect.H2), + p -> p.withDialect(AbstractJSqlParser.Dialect.SNOWFLAKE))) { + assertEquals("SELECT 6", CCJSqlParserUtil.parse(sql, consumer).toString()); + assertEquals("SELECT 1 FROM t", + CCJSqlParserUtil.parse("SELECT 1 // comment\nFROM t", consumer).toString()); + assertEquals("SELECT 'a//b'", + CCJSqlParserUtil.parse("SELECT 'a//b'", consumer).toString()); + } + assertEquals(List.of("// leading"), + ((PlainSelect) CCJSqlParserUtil.parse("// leading\nSELECT 1", + p -> p.withDoubleSlashLineComments())).getLeadingComments()); + // an explicit switch after the preset wins + assertEquals("SELECT 6 / 2", CCJSqlParserUtil.parse("SELECT 6 //*x*/ 2", + p -> p.withDialect(AbstractJSqlParser.Dialect.H2) + .withDoubleSlashLineComments(false)) + .toString()); + } + @Test public void testDialectPresets() throws Exception { // MYSQL and MARIADB: backslash escapes and # line comments (MySQL 8 diff --git a/src/test/java/net/sf/jsqlparser/parser/LeadingCommentsTest.java b/src/test/java/net/sf/jsqlparser/parser/LeadingCommentsTest.java index 6b83c46cb5..dbad643cf7 100644 --- a/src/test/java/net/sf/jsqlparser/parser/LeadingCommentsTest.java +++ b/src/test/java/net/sf/jsqlparser/parser/LeadingCommentsTest.java @@ -42,7 +42,8 @@ public void lineCommentsExcludeLineTerminators(String lineTerminator) @Test public void mixedCommentsRetainExactImagesAndSourceOrder() throws JSQLParserException { Select select = (Select) CCJSqlParserUtil - .parse("/* a */ -- b\r\n// c\r/* d */ SELECT 1"); + .parse("/* a */ -- b\r\n// c\r/* d */ SELECT 1", + parser -> parser.withDoubleSlashLineComments()); assertEquals(List.of("/* a */", "-- b", "// c", "/* d */"), select.getLeadingComments()); }