Skip to content

Commit 703a745

Browse files
committed
1 parent 74e2a4b commit 703a745

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

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

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

1212
import net.sf.jsqlparser.JSQLParserException;
13-
import net.sf.jsqlparser.test.TestUtils;
13+
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.fail;
1416
import org.junit.Ignore;
1517
import org.junit.Test;
1618

@@ -22,22 +24,62 @@ public class NestedBracketsPerformanceTest {
2224

2325
@Test
2426
public void testIssue766() throws JSQLParserException {
25-
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat('1','2'),'3'),'4'),'5'),'6'),'7'),'8'),'9'),'10'),'11'),'12'),'13'),'14'),'15'),'16'),'17'),'18'),'19'),'20'),'21'),col1 FROM tbl t1", true);
27+
assertSqlCanBeParsedAndDeparsed("SELECT concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat(concat('1','2'),'3'),'4'),'5'),'6'),'7'),'8'),'9'),'10'),'11'),'12'),'13'),'14'),'15'),'16'),'17'),'18'),'19'),'20'),'21'),col1 FROM tbl t1", true);
2628
}
2729

2830
@Test
2931
public void testIssue766_2() throws JSQLParserException {
30-
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT concat(concat(concat('1', '2'), '3'), '4'), col1 FROM tbl t1");
32+
assertSqlCanBeParsedAndDeparsed("SELECT concat(concat(concat('1', '2'), '3'), '4'), col1 FROM tbl t1");
3133
}
3234

3335
@Test
3436
public void testIssue235() throws JSQLParserException {
35-
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT CASE WHEN ( CASE WHEN ( CASE WHEN ( CASE WHEN ( 1 ) THEN 0 END ) THEN 0 END ) THEN 0 END ) THEN 0 END FROM a", true);
37+
assertSqlCanBeParsedAndDeparsed("SELECT CASE WHEN ( CASE WHEN ( CASE WHEN ( CASE WHEN ( 1 ) THEN 0 END ) THEN 0 END ) THEN 0 END ) THEN 0 END FROM a", true);
3638
}
3739

3840
@Test(timeout = 100000)
3941
@Ignore
4042
public void testIssue496() throws JSQLParserException {
41-
TestUtils.assertSqlCanBeParsedAndDeparsed("select isNull(charLen(TEST_ID,0)+ isNull(charLen(TEST_DVC,0)+ isNull(charLen(TEST_NO,0)+ isNull(charLen(ATEST_ID,0)+ isNull(charLen(TESTNO,0)+ isNull(charLen(TEST_CTNT,0)+ isNull(charLen(TEST_MESG_CTNT,0)+ isNull(charLen(TEST_DTM,0)+ isNull(charLen(TEST_DTT,0)+ isNull(charLen(TEST_ADTT,0)+ isNull(charLen(TEST_TCD,0)+ isNull(charLen(TEST_PD,0)+ isNull(charLen(TEST_VAL,0)+ isNull(charLen(TEST_YN,0)+ isNull(charLen(TEST_DTACM,0)+ isNull(charLen(TEST_MST,0) from test_info_m");
43+
assertSqlCanBeParsedAndDeparsed("select isNull(charLen(TEST_ID,0)+ isNull(charLen(TEST_DVC,0)+ isNull(charLen(TEST_NO,0)+ isNull(charLen(ATEST_ID,0)+ isNull(charLen(TESTNO,0)+ isNull(charLen(TEST_CTNT,0)+ isNull(charLen(TEST_MESG_CTNT,0)+ isNull(charLen(TEST_DTM,0)+ isNull(charLen(TEST_DTT,0)+ isNull(charLen(TEST_ADTT,0)+ isNull(charLen(TEST_TCD,0)+ isNull(charLen(TEST_PD,0)+ isNull(charLen(TEST_VAL,0)+ isNull(charLen(TEST_YN,0)+ isNull(charLen(TEST_DTACM,0)+ isNull(charLen(TEST_MST,0) from test_info_m");
44+
}
45+
46+
/**
47+
* Try to avoid or border exceptionally big parsing time increments by adding more bracket constructs.
48+
*
49+
* @throws JSQLParserException
50+
*/
51+
@Test
52+
public void testIncreaseOfParseTime() throws JSQLParserException {
53+
long oldDurationTime = 1000;
54+
for (int i = 0; i < 20; i++) {
55+
String sql = "SELECT " + buildRecursiveBracketExpression("concat($1,'B')", "'A'", i) + " FROM mytbl";
56+
long startTime = System.currentTimeMillis();
57+
assertSqlCanBeParsedAndDeparsed(sql, true);
58+
long durationTime = System.currentTimeMillis() - startTime;
59+
60+
if (i > 0) {
61+
System.out.println("old duration " + oldDurationTime + " new duration time " + durationTime + " for " + sql);
62+
}
63+
64+
if (oldDurationTime * 4 < durationTime) {
65+
fail("too large increment of parsing time");
66+
}
67+
68+
oldDurationTime = Math.max(durationTime, 1);
69+
}
70+
}
71+
72+
@Test
73+
public void testRecursiveBracketExpression() {
74+
assertEquals("concat('A','B')", buildRecursiveBracketExpression("concat($1,'B')", "'A'", 0));
75+
assertEquals("concat(concat('A','B'),'B')", buildRecursiveBracketExpression("concat($1,'B')", "'A'", 1));
76+
assertEquals("concat(concat(concat('A','B'),'B'),'B')", buildRecursiveBracketExpression("concat($1,'B')", "'A'", 2));
77+
}
78+
79+
private String buildRecursiveBracketExpression(String template, String finalExpression, int depth) {
80+
if (depth == 0) {
81+
return template.replace("$1", finalExpression);
82+
}
83+
return template.replace("$1", buildRecursiveBracketExpression(template, finalExpression, depth - 1));
4284
}
4385
}

0 commit comments

Comments
 (0)