This is an automated email from the ASF dual-hosted git repository. korlov pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/ignite-3.git
The following commit(s) were added to refs/heads/main by this push: new aba72be1b1 IGNITE-21950 Sql. Cover SQL T351(Bracketed SQL comments (/*...*/ comments)) feature by tests (#3727) aba72be1b1 is described below commit aba72be1b19da91edf1c255274b878abd88d44c6 Author: korlov42 <kor...@gridgain.com> AuthorDate: Fri May 10 18:15:07 2024 +0300 IGNITE-21950 Sql. Cover SQL T351(Bracketed SQL comments (/*...*/ comments)) feature by tests (#3727) --- .../sql/engine/sql/AbstractParserTest.java | 6 +- .../sql/engine/sql/CommentParsingTest.java | 87 ++++++++++++++++++++-- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/AbstractParserTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/AbstractParserTest.java index 8966b1ecde..670fd62610 100644 --- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/AbstractParserTest.java +++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/AbstractParserTest.java @@ -36,7 +36,7 @@ public abstract class AbstractParserTest { * @param stmt Statement to parse. * @return An AST. */ - protected SqlNode parse(String stmt) { + protected static SqlNode parse(String stmt) { StatementParseResult parseResult = IgniteSqlParser.parse(stmt, StatementParseResult.MODE); SqlNode statement = parseResult.statement(); @@ -53,7 +53,7 @@ public abstract class AbstractParserTest { * @param pred Addition check that would be applied to the object. * @return {@code true} in case the object if instance of the given class and matches the predicat. */ - protected <T> Matcher<T> ofTypeMatching(String desc, Class<T> cls, Predicate<T> pred) { + static <T> Matcher<T> ofTypeMatching(String desc, Class<T> cls, Predicate<T> pred) { return new CustomMatcher<>(desc) { /** {@inheritDoc} */ @Override @@ -67,7 +67,7 @@ public abstract class AbstractParserTest { * Compares the result of calling {@link SqlNode#unparse(SqlWriter, int, int)}} on the given node with the expected string. * Also compares the expected string on a cloned node. */ - protected static void expectUnparsed(SqlNode node, String expectedStmt) { + static void expectUnparsed(SqlNode node, String expectedStmt) { assertEquals(expectedStmt, unparse(node), "Unparsed does not match"); // Verify that clone works correctly. diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/CommentParsingTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/CommentParsingTest.java index 4714b77948..bc19dadca3 100644 --- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/CommentParsingTest.java +++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/CommentParsingTest.java @@ -36,6 +36,7 @@ import org.junit.jupiter.params.provider.EnumSource; * * <p>Covers:<ol> * <li>E161: SQL comments using leading double minus</li> + * <li>T351: Bracketed SQL comments (/*...*/ comments)</li> * </ol> * * <p>According to SQL standard, SQL text containing one or more instances of comment is equivalent to the same SQL text with the comment @@ -47,11 +48,29 @@ public class CommentParsingTest extends AbstractParserTest { private static final String NL = System.lineSeparator(); + private static final String SIMPLE_COMMENT = "-- this is simple comment "; + private static final String MULTILINE_COMMENT = "/* this" + NL + + "is" + NL + + "multiline" + NL + + "comment */"; + @ParameterizedTest @EnumSource(Statement.class) void leadingSimpleComment(Statement statement) { String originalQueryString = statement.text; - String queryWithComment = "-- this is comment " + NL + originalQueryString; + String queryWithComment = SIMPLE_COMMENT + NL + originalQueryString; + + assertQueries( + originalQueryString, + queryWithComment + ); + } + + @ParameterizedTest + @EnumSource(Statement.class) + void leadingMultilineComment(Statement statement) { + String originalQueryString = statement.text; + String queryWithComment = MULTILINE_COMMENT + NL + originalQueryString; assertQueries( originalQueryString, @@ -63,7 +82,19 @@ public class CommentParsingTest extends AbstractParserTest { @EnumSource(Statement.class) void trailingSimpleComment(Statement statement) { String originalQueryString = statement.text; - String queryWithComment = originalQueryString + NL + "-- this is comment"; + String queryWithComment = originalQueryString + NL + SIMPLE_COMMENT; + + assertQueries( + originalQueryString, + queryWithComment + ); + } + + @ParameterizedTest + @EnumSource(Statement.class) + void trailingMultilineComment(Statement statement) { + String originalQueryString = statement.text; + String queryWithComment = originalQueryString + NL + MULTILINE_COMMENT; assertQueries( originalQueryString, @@ -76,7 +107,16 @@ public class CommentParsingTest extends AbstractParserTest { assertThrowsSqlException( Sql.STMT_PARSE_ERR, "Failed to parse query", - () -> parse("-- this is comment") + () -> parse(SIMPLE_COMMENT) + ); + } + + @Test + void emptyStatementMultilineComment() { + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Failed to parse query", + () -> parse(MULTILINE_COMMENT) ); } @@ -107,7 +147,44 @@ public class CommentParsingTest extends AbstractParserTest { sb.app(line); if (lineNum++ == lineToInject) { - sb.app(" -- this is simple comment"); + sb.app(SIMPLE_COMMENT); + } + + sb.app(NL); + } + + assertQueries(statement.text, sb.toString()); + } + } + + /** + * This test injects simple comment before random line break. + */ + @ParameterizedTest + @EnumSource(Statement.class) + void infixMultilineComment(Statement statement) { + int iterations = 50; + long seed = ThreadLocalRandom.current().nextLong(); + + LOG.info("Seed is {}", seed); + + Random rnd = new Random(seed); + + // it's well-known query that has less than + // Integer.MAX_VALUE lines + @SuppressWarnings("NumericCastThatLosesPrecision") + int linesCount = (int) statement.text.lines().count(); + + for (int i = 0; i < iterations; i++) { + int lineToInject = Integer.min(statement.maxLineBreak, rnd.nextInt(linesCount)); + + int lineNum = 0; + IgniteStringBuilder sb = new IgniteStringBuilder(); + for (String line : statement.text.split(NL)) { + sb.app(line); + + if (lineNum++ == lineToInject) { + sb.app(MULTILINE_COMMENT); } sb.app(NL); @@ -117,7 +194,7 @@ public class CommentParsingTest extends AbstractParserTest { } } - private void assertQueries(String expected, String actual) { + private static void assertQueries(String expected, String actual) { SqlNode expectedAst; SqlNode actualAst; try {