Skip to content

Commit 5b78153

Browse files
committed
fixes #869
1 parent 144e60b commit 5b78153

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

src/main/java/net/sf/jsqlparser/expression/AnalyticExpression.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class AnalyticExpression extends ASTNodeAccessImpl implements Expression
3636
private boolean distinct = false;
3737
private boolean ignoreNulls = false;
3838
private Expression filterExpression = null;
39+
private WindowElement windowElement = null;
3940

4041
public AnalyticExpression() {
4142
}
@@ -83,8 +84,6 @@ public KeepExpression getKeep() {
8384
public void setKeep(KeepExpression keep) {
8485
this.keep = keep;
8586
}
86-
87-
8887

8988
public ExpressionList getPartitionExpressionList() {
9089
return partitionBy.getPartitionExpressionList();
@@ -93,10 +92,11 @@ public ExpressionList getPartitionExpressionList() {
9392
public void setPartitionExpressionList(ExpressionList partitionExpressionList) {
9493
setPartitionExpressionList(partitionExpressionList, false);
9594
}
95+
9696
public void setPartitionExpressionList(ExpressionList partitionExpressionList, boolean brackets) {
9797
partitionBy.setPartitionExpressionList(partitionExpressionList, brackets);
9898
}
99-
99+
100100
public boolean isPartitionByBrackets() {
101101
return partitionBy.isBrackets();
102102
}
@@ -134,11 +134,11 @@ public void setDefaultValue(Expression defaultValue) {
134134
}
135135

136136
public WindowElement getWindowElement() {
137-
return orderBy.getWindowElement();
137+
return windowElement;
138138
}
139139

140140
public void setWindowElement(WindowElement windowElement) {
141-
orderBy.setWindowElement(windowElement);
141+
this.windowElement = windowElement;
142142
}
143143

144144
public AnalyticType getType() {
@@ -197,7 +197,7 @@ public String toString() {
197197
b.append(filterExpression.toString());
198198
b.append(") ");
199199
}
200-
200+
201201
switch (type) {
202202
case WITHIN_GROUP:
203203
b.append("WITHIN GROUP");
@@ -210,6 +210,13 @@ public String toString() {
210210
partitionBy.toStringPartitionBy(b);
211211
orderBy.toStringOrderByElements(b);
212212

213+
if (windowElement != null) {
214+
if (orderBy != null && orderBy.getOrderByElements()!=null) {
215+
b.append(' ');
216+
}
217+
b.append(windowElement);
218+
}
219+
213220
b.append(")");
214221

215222
return b.toString();

src/main/java/net/sf/jsqlparser/expression/OrderByClause.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
public class OrderByClause {
1717
private List<OrderByElement> orderByElements;
18-
private WindowElement windowElement;
1918

2019
public List<OrderByElement> getOrderByElements() {
2120
return orderByElements;
@@ -25,14 +24,6 @@ public void setOrderByElements(List<OrderByElement> orderByElements) {
2524
this.orderByElements = orderByElements;
2625
}
2726

28-
public WindowElement getWindowElement() {
29-
return windowElement;
30-
}
31-
32-
public void setWindowElement(WindowElement windowElement) {
33-
this.windowElement = windowElement;
34-
}
35-
3627
void toStringOrderByElements(StringBuilder b) {
3728
if (orderByElements != null && !orderByElements.isEmpty()) {
3829
b.append("ORDER BY ");
@@ -42,11 +33,6 @@ void toStringOrderByElements(StringBuilder b) {
4233
}
4334
b.append(orderByElements.get(i).toString());
4435
}
45-
46-
if (windowElement != null) {
47-
b.append(' ');
48-
b.append(windowElement);
49-
}
5036
}
5137
}
5238
}

src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ public void visit(AnalyticExpression aexpr) {
642642
keep.accept(this);
643643
buffer.append(" ");
644644
}
645-
645+
646646
if (aexpr.getFilterExpression() != null) {
647647
buffer.append("FILTER (WHERE ");
648648
aexpr.getFilterExpression().accept(this);
@@ -685,11 +685,13 @@ public void visit(AnalyticExpression aexpr) {
685685
}
686686
orderByDeParser.deParseElement(orderByElements.get(i));
687687
}
688+
}
688689

689-
if (windowElement != null) {
690+
if (windowElement != null) {
691+
if (orderByElements != null && !orderByElements.isEmpty()) {
690692
buffer.append(' ');
691-
buffer.append(windowElement);
692693
}
694+
buffer.append(windowElement);
693695
}
694696

695697
buffer.append(")");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3129,7 +3129,8 @@ AnalyticExpression AnalyticExpression(Function function) :
31293129
[<K_PARTITION> <K_BY>
31303130
(LOOKAHEAD(3) expressionList=SimpleExpressionList() | "(" {partitionByBrackets = true;} expressionList=SimpleExpressionList() ")" )
31313131
]
3132-
[olist=OrderByElements() [windowElement = WindowElement() ] ]
3132+
[olist=OrderByElements() ]
3133+
[windowElement = WindowElement() ]
31333134

31343135
{
31353136
retval.setPartitionExpressionList(expressionList, partitionByBrackets);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3897,4 +3897,9 @@ public void testWithAsRecursiveIssue874() throws JSQLParserException {
38973897
public void testSessionKeywordIssue876() throws JSQLParserException {
38983898
assertSqlCanBeParsedAndDeparsed("SELECT ID_COMPANY FROM SESSION.COMPANY");
38993899
}
3900+
3901+
@Test
3902+
public void testWindowClauseWithoutOrderByIssue869() throws JSQLParserException {
3903+
assertSqlCanBeParsedAndDeparsed("SELECT subject_id, student_id, mark, sum(mark) OVER (PARTITION BY (subject_id) ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM marks");
3904+
}
39003905
}

0 commit comments

Comments
 (0)