Skip to content

Commit 9d62c21

Browse files
committed
analytic expressions updated
1 parent 15fc834 commit 9d62c21

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed
Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
package net.sf.jsqlparser.expression;
22

33
import java.util.List;
4+
import net.sf.jsqlparser.schema.Column;
5+
import net.sf.jsqlparser.statement.select.OrderByElement;
46

57
/**
68
* Analytic function. The name of the function is variable but the parameters
7-
* following the special analytic function path.
8-
* e.g. row_number() over (order by test)
9+
* following the special analytic function path. e.g. row_number() over (order
10+
* by test)
11+
*
912
* @author tw
1013
*/
1114
public class AnalyticExpression implements Expression {
1215

13-
private List partitionByColumns;
14-
private List orderByElements;
16+
private List<Column> partitionByColumns;
17+
private List<OrderByElement> orderByElements;
1518
private String name;
16-
19+
private Expression expression;
20+
1721
@Override
1822
public void accept(ExpressionVisitor expressionVisitor) {
1923
expressionVisitor.visit(this);
2024
}
2125

22-
public List getOrderByElements() {
26+
public List<OrderByElement> getOrderByElements() {
2327
return orderByElements;
2428
}
2529

26-
public void setOrderByElements(List orderByElements) {
30+
public void setOrderByElements(List<OrderByElement> orderByElements) {
2731
this.orderByElements = orderByElements;
2832
}
2933

30-
public List getPartitionByColumns() {
34+
public List<Column> getPartitionByColumns() {
3135
return partitionByColumns;
3236
}
3337

34-
public void setPartitionByColumns(List partitionByColumns) {
38+
public void setPartitionByColumns(List<Column> partitionByColumns) {
3539
this.partitionByColumns = partitionByColumns;
3640
}
3741

@@ -42,35 +46,47 @@ public String getName() {
4246
public void setName(String name) {
4347
this.name = name;
4448
}
45-
49+
50+
public Expression getExpression() {
51+
return expression;
52+
}
53+
54+
public void setExpression(Expression expression) {
55+
this.expression = expression;
56+
}
57+
4658
@Override
4759
public String toString() {
4860
StringBuilder b = new StringBuilder();
49-
50-
b.append(name).append("() OVER (");
51-
if (partitionByColumns!=null && !partitionByColumns.isEmpty()) {
61+
62+
b.append(name).append("(");
63+
if (expression != null) {
64+
b.append(expression.toString());
65+
}
66+
b.append(") OVER (");
67+
if (partitionByColumns != null && !partitionByColumns.isEmpty()) {
5268
b.append("PARTITION BY ");
53-
for (int i=0;i<partitionByColumns.size();i++) {
54-
if (i>0) {
69+
for (int i = 0; i < partitionByColumns.size(); i++) {
70+
if (i > 0) {
5571
b.append(", ");
5672
}
5773
b.append(partitionByColumns.get(i).toString());
5874
}
5975
b.append(" ");
6076
}
61-
62-
if (orderByElements!=null && !orderByElements.isEmpty()) {
77+
78+
if (orderByElements != null && !orderByElements.isEmpty()) {
6379
b.append("ORDER BY ");
64-
for (int i=0;i<orderByElements.size();i++) {
65-
if (i>0) {
80+
for (int i = 0; i < orderByElements.size(); i++) {
81+
if (i > 0) {
6682
b.append(", ");
6783
}
6884
b.append(orderByElements.get(i).toString());
6985
}
7086
}
71-
72-
b.append(")");
73-
87+
88+
b.append(")");
89+
7490
return b.toString();
75-
}
91+
}
7692
}

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,10 +1566,14 @@ AnalyticExpression AnalyticExpression() :
15661566
List<OrderByElement> olist = null;
15671567
Token token = null;
15681568
Column column = null;
1569+
Expression expr = null;
15691570
}
15701571
{
1571-
token=<S_IDENTIFIER> { retval.setName(token.image); } "(" ")" <K_OVER> "(" [<K_PARTITION> <K_BY> (column=Column() {plist.add(column);} )+ ] olist=OrderByElements()
1572+
token=<S_IDENTIFIER> { retval.setName(token.image); } "(" [ expr=SimpleExpression() ] ")" <K_OVER> "("
1573+
[<K_PARTITION> <K_BY> (column=Column() {plist.add(column);} )+ ]
1574+
[olist=OrderByElements() ]
15721575
{
1576+
retval.setExpression(expr);
15731577
retval.setPartitionByColumns(plist);
15741578
retval.setOrderByElements(olist);
15751579
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,11 @@ public void testProblemSqlAnalytic5AggregateColumnValue() throws JSQLParserExcep
656656
String stmt = "SELECT a, sum(b) OVER () AS n FROM table1";
657657
assertSqlCanBeParsedAndDeparsed(stmt);
658658
}
659+
660+
public void testProblemSqlAnalytic6AggregateColumnValue() throws JSQLParserException {
661+
String stmt = "SELECT a, sum(b + 5) OVER (ORDER BY a) AS n FROM table1";
662+
assertSqlCanBeParsedAndDeparsed(stmt);
663+
}
659664

660665
public void testOracleJoin() throws JSQLParserException {
661666
String stmt = "SELECT * FROM tabelle1, tabelle2 WHERE tabelle1.a = tabelle2.b(+)";

0 commit comments

Comments
 (0)