Skip to content

Commit 99b46bf

Browse files
author
Jonathan Burnhams
committed
Add support for lag and lead with offset and default value parameters
1 parent 33f4f66 commit 99b46bf

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

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

100644100755
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
*/
2222
package net.sf.jsqlparser.expression;
2323

24-
import java.util.List;
2524
import net.sf.jsqlparser.schema.Column;
2625
import net.sf.jsqlparser.statement.select.OrderByElement;
2726

27+
import java.util.List;
28+
2829
/**
2930
* Analytic function. The name of the function is variable but the parameters
3031
* following the special analytic function path. e.g. row_number() over (order
@@ -39,6 +40,8 @@ public class AnalyticExpression implements Expression {
3940
private List<OrderByElement> orderByElements;
4041
private String name;
4142
private Expression expression;
43+
private Expression offset;
44+
private Expression defaultValue;
4245
private boolean allColumns = false;
4346

4447
@Override
@@ -78,13 +81,35 @@ public void setExpression(Expression expression) {
7881
this.expression = expression;
7982
}
8083

81-
@Override
84+
public Expression getOffset() {
85+
return offset;
86+
}
87+
88+
public void setOffset(Expression offset) {
89+
this.offset = offset;
90+
}
91+
92+
public Expression getDefaultValue() {
93+
return defaultValue;
94+
}
95+
96+
public void setDefaultValue(Expression defaultValue) {
97+
this.defaultValue = defaultValue;
98+
}
99+
100+
@Override
82101
public String toString() {
83102
StringBuilder b = new StringBuilder();
84103

85104
b.append(name).append("(");
86105
if (expression != null) {
87106
b.append(expression.toString());
107+
if (offset != null) {
108+
b.append(", ").append(offset.toString());
109+
if (defaultValue != null) {
110+
b.append(", ").append(defaultValue.toString());
111+
}
112+
}
88113
} else if (isAllColumns()) {
89114
b.append("*");
90115
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1765,13 +1765,18 @@ AnalyticExpression AnalyticExpression() :
17651765
Token token = null;
17661766
Column column = null;
17671767
Expression expr = null;
1768+
Expression offset = null;
1769+
Expression defaultValue = null;
17681770
}
17691771
{
1770-
token=<S_IDENTIFIER> { retval.setName(token.image); } "(" [ expr=SimpleExpression() | "*" { retval.setAllColumns(true); } ] ")" <K_OVER> "("
1772+
token=<S_IDENTIFIER> { retval.setName(token.image); }
1773+
"(" [ expr=SimpleExpression() ["," offset=SimpleExpression() ["," defaultValue=SimpleExpression() ]] | "*" { retval.setAllColumns(true); } ] ")" <K_OVER> "("
17711774
[<K_PARTITION> <K_BY> column=Column() {plist.add(column);} ("," column=Column() {plist.add(column);} )* ]
17721775
[olist=OrderByElements() ]
17731776
{
17741777
retval.setExpression(expr);
1778+
retval.setOffset(offset);
1779+
retval.setDefaultValue(defaultValue);
17751780
retval.setPartitionByColumns(plist);
17761781
retval.setOrderByElements(olist);
17771782
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,16 @@ public void testProblemSqlAnalytic9CommaListPartition() throws JSQLParserExcepti
775775
assertSqlCanBeParsedAndDeparsed(stmt);
776776
}
777777

778+
public void testProblemSqlAnalytic10Lag() throws JSQLParserException {
779+
String stmt = "SELECT a, lag(a, 1) OVER (PARTITION BY c ORDER BY a, b) AS n FROM table1";
780+
assertSqlCanBeParsedAndDeparsed(stmt);
781+
}
782+
783+
public void testProblemSqlAnalytic11Lag() throws JSQLParserException {
784+
String stmt = "SELECT a, lag(a, 1, 0) OVER (PARTITION BY c ORDER BY a, b) AS n FROM table1";
785+
assertSqlCanBeParsedAndDeparsed(stmt);
786+
}
787+
778788
public void testOracleJoin() throws JSQLParserException {
779789
String stmt = "SELECT * FROM tabelle1, tabelle2 WHERE tabelle1.a = tabelle2.b(+)";
780790
assertSqlCanBeParsedAndDeparsed(stmt);

0 commit comments

Comments
 (0)