Skip to content

Commit 59f7ec2

Browse files
committed
test for issue 265
1 parent f0b350d commit 59f7ec2

File tree

2 files changed

+220
-20
lines changed

2 files changed

+220
-20
lines changed

src/test/java/net/sf/jsqlparser/test/select/SelectTest.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
import java.io.*;
1515
import java.util.*;
16-
import java.util.logging.Level;
17-
import java.util.logging.Logger;
1816

1917
import static net.sf.jsqlparser.test.TestUtils.*;
18+
import net.sf.jsqlparser.util.TablesNamesFinder;
2019

2120
public class SelectTest extends TestCase {
2221

@@ -398,12 +397,12 @@ public void testTop() throws JSQLParserException {
398397

399398
Select select = (Select) parserManager.parse(new StringReader(statement));
400399

401-
assertEquals(3, ((LongValue)((PlainSelect) select.getSelectBody()).getTop().getExpression()).getValue());
400+
assertEquals(3, ((LongValue) ((PlainSelect) select.getSelectBody()).getTop().getExpression()).getValue());
402401
assertStatementCanBeDeparsedAs(select, statement);
403402

404403
statement = "select top 5 foo from bar";
405404
select = (Select) parserManager.parse(new StringReader(statement));
406-
assertEquals(5, ((LongValue)((PlainSelect) select.getSelectBody()).getTop().getExpression()).getValue());
405+
assertEquals(5, ((LongValue) ((PlainSelect) select.getSelectBody()).getTop().getExpression()).getValue());
407406
}
408407

409408
public void testTopWithParenthesis() throws JSQLParserException {
@@ -1202,7 +1201,7 @@ public void testCastTypeProblem7() throws JSQLParserException {
12021201
String stmt = "SELECT CAST('test' AS character varying) FROM tabelle1";
12031202
assertSqlCanBeParsedAndDeparsed(stmt);
12041203
}
1205-
1204+
12061205
public void testCastTypeProblem8() throws JSQLParserException {
12071206
String stmt = "SELECT CAST('123' AS double precision) FROM tabelle1";
12081207
assertSqlCanBeParsedAndDeparsed(stmt);
@@ -1834,7 +1833,7 @@ public void testSelectInto1() throws JSQLParserException {
18341833
public void testSelectForUpdate() throws JSQLParserException {
18351834
assertSqlCanBeParsedAndDeparsed("SELECT * FROM user_table FOR UPDATE");
18361835
}
1837-
1836+
18381837
public void testSelectForUpdate2() throws JSQLParserException {
18391838
assertSqlCanBeParsedAndDeparsed("SELECT * FROM emp WHERE empno = ? FOR UPDATE");
18401839
}
@@ -2146,13 +2145,13 @@ public void testWhereIssue240_1() throws JSQLParserException {
21462145
public void testWhereIssue240_0() throws JSQLParserException {
21472146
assertSqlCanBeParsedAndDeparsed("SELECT count(*) FROM mytable WHERE 0");
21482147
}
2149-
2148+
21502149
public void testWhereIssue240_notBoolean() {
21512150
try {
21522151
CCJSqlParserUtil.parse("SELECT count(*) FROM mytable WHERE 5");
21532152
fail("should not be parsed");
21542153
} catch (JSQLParserException ex) {
2155-
2154+
21562155
}
21572156
}
21582157

@@ -2163,53 +2162,59 @@ public void testWhereIssue240_true() throws JSQLParserException {
21632162
public void testWhereIssue240_false() throws JSQLParserException {
21642163
assertSqlCanBeParsedAndDeparsed("SELECT count(*) FROM mytable WHERE false");
21652164
}
2166-
2165+
21672166
public void testWhereIssue241KeywordEnd() throws JSQLParserException {
21682167
assertSqlCanBeParsedAndDeparsed("SELECT l.end FROM lessons l");
21692168
}
2170-
2169+
21712170
public void testSpeedTestIssue235() throws JSQLParserException {
21722171
assertSqlCanBeParsedAndDeparsed("SELECT * FROM tbl WHERE (ROUND((((((period_diff(date_format(tbl.CD, '%Y%m'), date_format(SUBTIME(CURRENT_TIMESTAMP(), 25200), '%Y%m')) + month(SUBTIME(CURRENT_TIMESTAMP(), 25200))) - MONTH('2012-02-01')) - 1) / 3) - ROUND((((month(SUBTIME(CURRENT_TIMESTAMP(),25200)) - MONTH('2012-02-01')) - 1) / 3)))) = -3)", true);
21732172
}
2174-
2173+
21752174
public void testSpeedTestIssue235_2() throws IOException, JSQLParserException {
21762175
String stmt = IOUtils.toString(SelectTest.class.getResourceAsStream("large-sql-issue-235.txt"));
21772176
assertSqlCanBeParsedAndDeparsed(stmt, true);
21782177
}
2179-
2178+
21802179
public void testCastVarCharMaxIssue245() throws JSQLParserException {
21812180
assertSqlCanBeParsedAndDeparsed("SELECT CAST('foo' AS NVARCHAR (MAX))");
21822181
}
2183-
2182+
21842183
public void testNestedFunctionCallIssue253() throws JSQLParserException {
21852184
assertSqlCanBeParsedAndDeparsed("SELECT (replace_regex(replace_regex(replace_regex(get_json_string(a_column, 'value'), '\\n', ' '), '\\r', ' '), '\\\\', '\\\\\\\\')) FROM a_table WHERE b_column = 'value'");
21862185
}
2187-
2186+
21882187
public void testEscapedBackslashIssue253() throws JSQLParserException {
21892188
assertSqlCanBeParsedAndDeparsed("SELECT replace_regex('test', '\\\\', '\\\\\\\\')");
21902189
}
2191-
2190+
21922191
public void testKeywordTableIssue261() throws JSQLParserException {
21932192
assertSqlCanBeParsedAndDeparsed("SELECT column_value FROM table(VARCHAR_LIST_TYPE())");
21942193
}
2195-
2194+
21962195
public void testTopExpressionIssue243() throws JSQLParserException {
21972196
assertSqlCanBeParsedAndDeparsed("SELECT TOP (? + 1) * FROM MyTable");
21982197
}
2199-
2198+
22002199
public void testTopExpressionIssue243_2() throws JSQLParserException {
22012200
assertSqlCanBeParsedAndDeparsed("SELECT TOP (CAST(? AS INT)) * FROM MyTable");
22022201
}
2203-
2202+
22042203
public void testFunctionIssue284() throws JSQLParserException {
22052204
assertSqlCanBeParsedAndDeparsed("SELECT NVL((SELECT 1 FROM DUAL), 1) AS A FROM TEST1");
22062205
}
2207-
2206+
22082207
public void testFunctionDateTimeValues() throws JSQLParserException {
22092208
assertSqlCanBeParsedAndDeparsed("SELECT * FROM tab1 WHERE a > TIMESTAMP '2004-04-30 04:05:34.56'");
22102209
}
2211-
2210+
22122211
public void testUniqueInsteadOfDistinctIssue299() throws JSQLParserException {
22132212
assertSqlCanBeParsedAndDeparsed("SELECT UNIQUE trunc(timez(ludate)+ 8/24) bus_dt, j.object j_name , timez(j.starttime) START_TIME , timez(j.endtime) END_TIME FROM TEST_1 j", true);
22142213
}
2214+
2215+
public void testProblemSqlIssue265() throws IOException, JSQLParserException {
2216+
String sqls = IOUtils.toString(SelectTest.class.getResourceAsStream("large-sql-with-issue-265.txt"));
2217+
Statements stmts = CCJSqlParserUtil.parseStatements(sqls);
2218+
assertEquals(2, stmts.getStatements().size());
2219+
}
22152220
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
with cross_items as
2+
(select i_item_sk ss_item_sk
3+
from item,
4+
(select iss.i_brand_id brand_id
5+
,iss.i_class_id class_id
6+
,iss.i_category_id category_id
7+
from store_sales
8+
,item iss
9+
,date_dim d1
10+
where ss_item_sk = iss.i_item_sk
11+
and ss_sold_date_sk = d1.d_date_sk
12+
and d1.d_year between 1999 AND 1999 + 2
13+
intersect
14+
select ics.i_brand_id
15+
,ics.i_class_id
16+
,ics.i_category_id
17+
from catalog_sales
18+
,item ics
19+
,date_dim d2
20+
where cs_item_sk = ics.i_item_sk
21+
and cs_sold_date_sk = d2.d_date_sk
22+
and d2.d_year between 1999 AND 1999 + 2
23+
intersect
24+
select iws.i_brand_id
25+
,iws.i_class_id
26+
,iws.i_category_id
27+
from web_sales
28+
,item iws
29+
,date_dim d3
30+
where ws_item_sk = iws.i_item_sk
31+
and ws_sold_date_sk = d3.d_date_sk
32+
and d3.d_year between 1999 AND 1999 + 2) x
33+
where i_brand_id = brand_id
34+
and i_class_id = class_id
35+
and i_category_id = category_id
36+
),
37+
avg_sales as
38+
(select avg(quantitylist_price) average_sales
39+
from (select ss_quantity quantity
40+
,ss_list_price list_price
41+
from store_sales
42+
,date_dim
43+
where ss_sold_date_sk = d_date_sk
44+
and d_year between 1999 and 2001
45+
union all
46+
select cs_quantity quantity
47+
,cs_list_price list_price
48+
from catalog_sales
49+
,date_dim
50+
where cs_sold_date_sk = d_date_sk
51+
and d_year between 1998 and 1998 + 2
52+
union all
53+
select ws_quantity quantity
54+
,ws_list_price list_price
55+
from web_sales
56+
,date_dim
57+
where ws_sold_date_sk = d_date_sk
58+
and d_year between 1998 and 1998 + 2) x)
59+
select channel, i_brand_id,i_class_id,i_category_id,sum(sales), sum(number_sales)
60+
from(
61+
select 'store' channel, i_brand_id,i_class_id
62+
,i_category_id,sum(ss_quantityss_list_price) sales
63+
, count() number_sales
64+
from store_sales
65+
,item
66+
,date_dim
67+
where ss_item_sk in (select ss_item_sk from cross_items)
68+
and ss_item_sk = i_item_sk
69+
and ss_sold_date_sk = d_date_sk
70+
and d_year = 1998+2
71+
and d_moy = 11
72+
group by i_brand_id,i_class_id,i_category_id
73+
having sum(ss_quantityss_list_price) > (select average_sales from avg_sales)
74+
union all
75+
select 'catalog' channel, i_brand_id,i_class_id,i_category_id, sum(cs_quantitycs_list_price) sales, count() number_sales
76+
from catalog_sales
77+
,item
78+
,date_dim
79+
where cs_item_sk in (select ss_item_sk from cross_items)
80+
and cs_item_sk = i_item_sk
81+
and cs_sold_date_sk = d_date_sk
82+
and d_year = 1998+2
83+
and d_moy = 11
84+
group by i_brand_id,i_class_id,i_category_id
85+
having sum(cs_quantitycs_list_price) > (select average_sales from avg_sales)
86+
union all
87+
select 'web' channel, i_brand_id,i_class_id,i_category_id, sum(ws_quantityws_list_price) sales , count() number_sales
88+
from web_sales
89+
,item
90+
,date_dim
91+
where ws_item_sk in (select ss_item_sk from cross_items)
92+
and ws_item_sk = i_item_sk
93+
and ws_sold_date_sk = d_date_sk
94+
and d_year = 1998+2
95+
and d_moy = 11
96+
group by i_brand_id,i_class_id,i_category_id
97+
having sum(ws_quantityws_list_price) > (select average_sales from avg_sales)
98+
) y
99+
group by rollup (channel, i_brand_id,i_class_id,i_category_id)
100+
order by channel,i_brand_id,i_class_id,i_category_id
101+
limit 100;
102+
with cross_items as
103+
(select i_item_sk ss_item_sk
104+
from item,
105+
(select iss.i_brand_id brand_id
106+
,iss.i_class_id class_id
107+
,iss.i_category_id category_id
108+
from store_sales
109+
,item iss
110+
,date_dim d1
111+
where ss_item_sk = iss.i_item_sk
112+
and ss_sold_date_sk = d1.d_date_sk
113+
and d1.d_year between 1999 AND 1999 + 2
114+
intersect
115+
select ics.i_brand_id
116+
,ics.i_class_id
117+
,ics.i_category_id
118+
from catalog_sales
119+
,item ics
120+
,date_dim d2
121+
where cs_item_sk = ics.i_item_sk
122+
and cs_sold_date_sk = d2.d_date_sk
123+
and d2.d_year between 1999 AND 1999 + 2
124+
intersect
125+
select iws.i_brand_id
126+
,iws.i_class_id
127+
,iws.i_category_id
128+
from web_sales
129+
,item iws
130+
,date_dim d3
131+
where ws_item_sk = iws.i_item_sk
132+
and ws_sold_date_sk = d3.d_date_sk
133+
and d3.d_year between 1999 AND 1999 + 2) x
134+
where i_brand_id = brand_id
135+
and i_class_id = class_id
136+
and i_category_id = category_id
137+
),
138+
avg_sales as
139+
(select avg(quantitylist_price) average_sales
140+
from (select ss_quantity quantity
141+
,ss_list_price list_price
142+
from store_sales
143+
,date_dim
144+
where ss_sold_date_sk = d_date_sk
145+
and d_year between 1998 and 1998 + 2
146+
union all
147+
select cs_quantity quantity
148+
,cs_list_price list_price
149+
from catalog_sales
150+
,date_dim
151+
where cs_sold_date_sk = d_date_sk
152+
and d_year between 1998 and 1998 + 2
153+
union all
154+
select ws_quantity quantity
155+
,ws_list_price list_price
156+
from web_sales
157+
,date_dim
158+
where ws_sold_date_sk = d_date_sk
159+
and d_year between 1998 and 1998 + 2) x)
160+
select * from
161+
(select 'store' channel, i_brand_id,i_class_id,i_category_id
162+
,sum(ss_quantityss_list_price) sales, count() number_sales
163+
from store_sales
164+
,item
165+
,date_dim
166+
where ss_item_sk in (select ss_item_sk from cross_items)
167+
and ss_item_sk = i_item_sk
168+
and ss_sold_date_sk = d_date_sk
169+
and d_week_seq = (select d_week_seq
170+
from date_dim
171+
where d_year = 1998 + 1
172+
and d_moy = 12
173+
and d_dom = 16)
174+
group by i_brand_id,i_class_id,i_category_id
175+
having sum(ss_quantityss_list_price) > (select average_sales from avg_sales)) this_year,
176+
(select 'store' channel, i_brand_id,i_class_id
177+
,i_category_id, sum(ss_quantityss_list_price) sales, count() number_sales
178+
from store_sales
179+
,item
180+
,date_dim
181+
where ss_item_sk in (select ss_item_sk from cross_items)
182+
and ss_item_sk = i_item_sk
183+
and ss_sold_date_sk = d_date_sk
184+
and d_week_seq = (select d_week_seq
185+
from date_dim
186+
where d_year = 1998
187+
and d_moy = 12
188+
and d_dom = 16)
189+
group by i_brand_id,i_class_id,i_category_id
190+
having sum(ss_quantity*ss_list_price) > (select average_sales from avg_sales)) last_year
191+
where this_year.i_brand_id= last_year.i_brand_id
192+
and this_year.i_class_id = last_year.i_class_id
193+
and this_year.i_category_id = last_year.i_category_id
194+
order by this_year.channel, this_year.i_brand_id, this_year.i_class_id, this_year.i_category_id
195+
limit 100;

0 commit comments

Comments
 (0)