|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2019 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.statement.select; |
| 11 | + |
| 12 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 13 | +import net.sf.jsqlparser.statement.Statement; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import java.time.Duration; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; |
| 20 | + |
| 21 | +public class JoinUsingDeParseTest { |
| 22 | + |
| 23 | + private static String usingSelect(int columnCount) { |
| 24 | + StringBuilder sb = new StringBuilder("SELECT * FROM a JOIN b USING ("); |
| 25 | + for (int i = 0; i < columnCount; i++) { |
| 26 | + if (i > 0) { |
| 27 | + sb.append(", "); |
| 28 | + } |
| 29 | + sb.append("c").append(i); |
| 30 | + } |
| 31 | + return sb.append(")").toString(); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void deParseOfLargeUsingColumnListIsLinear() throws Exception { |
| 36 | + // usingColumns is a LinkedList, so deparsing the clause through |
| 37 | + // Select.appendStringListTo used to read it by index (get(i)), which is O(n^2). |
| 38 | + String sql = usingSelect(200000); |
| 39 | + Statement statement = CCJSqlParserUtil.parse(sql); |
| 40 | + assertEquals(sql, assertTimeoutPreemptively(Duration.ofSeconds(5), statement::toString)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void deParseOfUsingColumnListIsUnchanged() throws Exception { |
| 45 | + String sql = "SELECT * FROM a JOIN b USING (c0, c1, c2)"; |
| 46 | + assertEquals(sql, CCJSqlParserUtil.parse(sql).toString()); |
| 47 | + } |
| 48 | +} |
0 commit comments