Skip to content

Commit 36d0b74

Browse files
committed
fixes #1013 - refactored fromitem grammar to drastically improve performance
1 parent 05a6f4b commit 36d0b74

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,7 @@ List<Table> IntoClause():
19351935
FromItem FromItem():
19361936
{
19371937
FromItem fromItem = null;
1938+
FromItem fromItem2 = null;
19381939
Pivot pivot = null;
19391940
UnPivot unpivot = null;
19401941
Alias alias = null;
@@ -1950,14 +1951,13 @@ FromItem FromItem():
19501951
(
19511952
"("
19521953
(
1953-
LOOKAHEAD(SubJoin())
1954-
fromItem=SubJoin()
1955-
|
1956-
LOOKAHEAD(3)
1954+
LOOKAHEAD(3) fromItem2=FromItem()
1955+
{ fromItem = new ParenthesisFromItem(fromItem2); }
1956+
[ fromItem = SubJoin(fromItem2) ]
1957+
/* LOOKAHEAD(SubJoin())
1958+
fromItem=SubJoin() */
1959+
|
19571960
fromItem=SubSelect()
1958-
|
1959-
fromItem=FromItem()
1960-
{ fromItem = new ParenthesisFromItem(fromItem); }
19611961
)
19621962
")"
19631963
[ LOOKAHEAD(2) unpivot=UnPivot() { fromItem.setUnPivot(unpivot); } ]
@@ -2049,19 +2049,17 @@ LateralSubSelect LateralSubSelect():
20492049
}
20502050
}
20512051

2052-
FromItem SubJoin():
2052+
FromItem SubJoin(FromItem fromItem):
20532053
{
2054-
FromItem fromItem = null;
20552054
Join join = null;
20562055
List<Join> joinList = null;
20572056
}
20582057
{
2059-
fromItem=FromItem()
2060-
joinList=SubJoinsList()
2058+
joinList=SubJoinsList()
20612059
{
2062-
SubJoin subJoin = new SubJoin();
2063-
subJoin.setLeft(fromItem);
2064-
subJoin.setJoinList(joinList);
2060+
SubJoin subJoin = new SubJoin();
2061+
subJoin.setLeft(fromItem);
2062+
subJoin.setJoinList(joinList);
20652063
return subJoin;
20662064
}
20672065
}

src/test/java/net/sf/jsqlparser/statement/select/NestedBracketsPerformanceTest.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package net.sf.jsqlparser.statement.select;
1111

12+
import java.util.logging.Logger;
1213
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
1314
import org.junit.Ignore;
1415
import org.junit.Test;
@@ -48,29 +49,57 @@ public void testIssue856() throws JSQLParserException {
4849
String sql = "SELECT " + buildRecursiveBracketExpression("if(month(today()) = 3, sum(\"Table5\".\"Month 002\"), $1)", "0", 5) + " FROM mytbl";
4950
assertSqlCanBeParsedAndDeparsed(sql);
5051
}
51-
52+
5253
@Test
5354
public void testRecursiveBracketExpressionIssue1019() {
5455
assertEquals("IF(1=1, 1, 2)", buildRecursiveBracketExpression("IF(1=1, $1, 2)", "1", 0));
5556
assertEquals("IF(1=1, IF(1=1, 1, 2), 2)", buildRecursiveBracketExpression("IF(1=1, $1, 2)", "1", 1));
5657
assertEquals("IF(1=1, IF(1=1, IF(1=1, 1, 2), 2), 2)", buildRecursiveBracketExpression("IF(1=1, $1, 2)", "1", 2));
5758
}
58-
59+
5960
@Test
6061
public void testRecursiveBracketExpressionIssue1019_2() throws JSQLParserException {
6162
doIncreaseOfParseTimeTesting("IF(1=1, $1, 2)", "1", 10);
6263
}
6364

65+
@Test
66+
public void testIssue1013() throws JSQLParserException {
67+
assertSqlCanBeParsedAndDeparsed("SELECT ((((((((((((((((tblA)))))))))))))))) FROM mytable");
68+
}
69+
70+
@Test
71+
public void testIssue1013_2() throws JSQLParserException {
72+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM ((((((((((((((((tblA))))))))))))))))");
73+
}
74+
75+
@Test
76+
public void testIssue1013_3() throws JSQLParserException {
77+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM (((tblA)))");
78+
}
79+
80+
@Test
81+
public void testIssue1013_4() throws JSQLParserException {
82+
String s = "tblA";
83+
for (int i = 1; i < 100; i++) {
84+
s = "(" + s + ")";
85+
}
86+
String sql = "SELECT * FROM " + s;
87+
LOG.info("testing " + sql);
88+
assertSqlCanBeParsedAndDeparsed(sql);
89+
}
90+
private static final Logger LOG = Logger.getLogger(NestedBracketsPerformanceTest.class.getName());
91+
6492
/**
65-
* Try to avoid or border exceptionally big parsing time increments by adding more bracket constructs.
93+
* Try to avoid or border exceptionally big parsing time increments by
94+
* adding more bracket constructs.
6695
*
6796
* @throws JSQLParserException
6897
*/
6998
@Test
7099
public void testIncreaseOfParseTime() throws JSQLParserException {
71100
doIncreaseOfParseTimeTesting("concat($1,'B')", "'A'", 20);
72101
}
73-
102+
74103
private void doIncreaseOfParseTimeTesting(String template, String finalExpression, int maxDepth) throws JSQLParserException {
75104
long oldDurationTime = 1000;
76105
int countProblematic = 0;
@@ -100,7 +129,7 @@ public void testRecursiveBracketExpression() {
100129
assertEquals("concat(concat('A','B'),'B')", buildRecursiveBracketExpression("concat($1,'B')", "'A'", 1));
101130
assertEquals("concat(concat(concat('A','B'),'B'),'B')", buildRecursiveBracketExpression("concat($1,'B')", "'A'", 2));
102131
}
103-
132+
104133
private String buildRecursiveBracketExpression(String template, String finalExpression, int depth) {
105134
if (depth == 0) {
106135
return template.replace("$1", finalExpression);

0 commit comments

Comments
 (0)