Skip to content

Commit a7ea55d

Browse files
committed
test: isolate table traversal benchmark from parser timeout
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent 94f413d commit a7ea55d

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

‎src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java‎

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,26 @@
1818
import static org.junit.jupiter.api.Assertions.assertTrue;
1919

2020
import java.time.Duration;
21+
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.List;
2324
import java.util.Set;
2425
import net.sf.jsqlparser.JSQLParserException;
26+
import net.sf.jsqlparser.expression.Alias;
27+
import net.sf.jsqlparser.expression.LongValue;
2528
import net.sf.jsqlparser.expression.OracleHint;
2629
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
2730
import net.sf.jsqlparser.schema.Column;
2831
import net.sf.jsqlparser.schema.Table;
2932
import net.sf.jsqlparser.statement.DescribeStatement;
3033
import net.sf.jsqlparser.statement.Statement;
3134
import net.sf.jsqlparser.statement.comment.Comment;
35+
import net.sf.jsqlparser.statement.select.AllColumns;
36+
import net.sf.jsqlparser.statement.select.Join;
37+
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
3238
import net.sf.jsqlparser.statement.select.PlainSelect;
39+
import net.sf.jsqlparser.statement.select.SelectItem;
40+
import net.sf.jsqlparser.statement.select.WithItem;
3341
import net.sf.jsqlparser.test.TestUtils;
3442
import org.junit.jupiter.api.Test;
3543

@@ -1065,30 +1073,31 @@ void testUpdateOrderByLimitSubquery() throws JSQLParserException {
10651073
}
10661074

10671075
@Test
1068-
void testManyDerivedNamesDoNotScaleQuadratically() throws JSQLParserException {
1069-
// Each aliased WITH item is remembered in otherItemNames and every plain table
1070-
// reference used to be matched against it with a linear ArrayList.contains, so a
1071-
// statement with n CTE aliases and n tables cost O(n^2) to walk.
1076+
void testManyDerivedNamesDoNotScaleQuadratically() {
1077+
// Every plain table reference used to be matched against the CTE aliases with
1078+
// ArrayList.contains, so n aliases and n tables cost O(n^2) to walk.
10721079
final int n = 30000;
1073-
final StringBuilder sb = new StringBuilder("WITH ");
1080+
final PlainSelect statement = new PlainSelect()
1081+
.withSelectItems(new SelectItem<>(new AllColumns()))
1082+
.withFromItem(new Table("t0"));
1083+
final List<WithItem<?>> withItems = new ArrayList<>(n);
1084+
final List<Join> joins = new ArrayList<>(n - 1);
10741085
for (int i = 0; i < n; i++) {
1086+
PlainSelect body =
1087+
new PlainSelect().withSelectItems(new SelectItem<>(new LongValue(1)));
1088+
withItems.add(new WithItem<>(new ParenthesedSelect().withSelect(body),
1089+
new Alias("a" + i)));
10751090
if (i > 0) {
1076-
sb.append(", ");
1091+
joins.add(new Join().withSimple(true).withRightItem(new Table("t" + i)));
10771092
}
1078-
sb.append("a").append(i).append(" AS (SELECT 1)");
1079-
}
1080-
sb.append(" SELECT * FROM ");
1081-
for (int i = 0; i < n; i++) {
1082-
if (i > 0) {
1083-
sb.append(", ");
1084-
}
1085-
sb.append("t").append(i);
10861093
}
1094+
statement.setWithItemsList(withItems);
1095+
statement.setJoins(joins);
10871096

1088-
// Parsing is linear and left outside the timed region; only the table walk is measured.
1089-
final Statement statement = CCJSqlParserUtil.parse(sb.toString());
1097+
// Build the AST directly: this tests table traversal, and parsing a large SQL
1098+
// fixture can hit the parser's independent timeout before traversal even starts.
10901099
final Set<String> tables = assertTimeoutPreemptively(Duration.ofSeconds(2),
1091-
() -> new TablesNamesFinder<>().getTables(statement));
1100+
() -> new TablesNamesFinder<>().getTables((Statement) statement));
10921101
assertEquals(n, tables.size());
10931102
}
10941103

0 commit comments

Comments
 (0)