ygerzhedovich commented on code in PR #3716: URL: https://github.com/apache/ignite-3/pull/3716#discussion_r1595218619
########## modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/CommentParsingTest.java: ########## @@ -0,0 +1,203 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.sql.engine.sql; + +import static org.apache.ignite.internal.sql.engine.util.SqlTestUtils.assertThrowsSqlException; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; +import org.apache.calcite.sql.SqlNode; +import org.apache.ignite.internal.lang.IgniteStringBuilder; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.lang.ErrorGroups.Sql; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +/** + * Tests to verify parsing of comments. + * + * <p>Covers:<ol> + * <li>E161: SQL comments using leading double minus</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 + * replaced with newline. + */ +public class CommentParsingTest extends AbstractParserTest { + private static final IgniteLogger LOG = Loggers.forClass(CommentParsingTest.class); + + private static final String NL = System.lineSeparator(); + + @ParameterizedTest + @EnumSource(Statement.class) + void leadingSimpleComment(Statement statement) { + String originalQueryString = statement.text; + String queryWithComment = "-- this is comment " + NL + originalQueryString; + + assertQueries( + originalQueryString, + queryWithComment + ); + } + + @ParameterizedTest + @EnumSource(Statement.class) + void trailingSimpleComment(Statement statement) { + String originalQueryString = statement.text; + String queryWithComment = originalQueryString + NL + "-- this is comment"; + + assertQueries( + originalQueryString, + queryWithComment + ); + } + + @Test + void emptyStatementSimpleComment() { + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Failed to parse query", + () -> parse("-- this is comment") + ); + } + + /** + * This test injects simple comment before random line break. + */ + @ParameterizedTest + @EnumSource(Statement.class) + void infixSimpleComment(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(" -- this is simple comment"); + } + + sb.app(NL); + } + + assertQueries(statement.text, sb.toString()); + } + } + + private void assertQueries(String expected, String actual) { + SqlNode expectedAst; + SqlNode actualAst; + try { + expectedAst = parse(expected); + } catch (RuntimeException ex) { + System.err.println(expected); + + throw ex; + } + + try { + actualAst = parse(actual); + } catch (RuntimeException ex) { + System.err.println(expected); Review Comment: ok, understand. But why not `LOG.error(expected);` ? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
