Skip to content

Commit

Permalink
Add support for lag and lead with offset and default value parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Burnhams committed Aug 1, 2013
1 parent 33f4f66 commit 99b46bf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/main/java/net/sf/jsqlparser/expression/AnalyticExpression.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
*/
package net.sf.jsqlparser.expression;

import java.util.List;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.OrderByElement;

import java.util.List;

/**
* Analytic function. The name of the function is variable but the parameters
* following the special analytic function path. e.g. row_number() over (order
Expand All @@ -39,6 +40,8 @@ public class AnalyticExpression implements Expression {
private List<OrderByElement> orderByElements;
private String name;
private Expression expression;
private Expression offset;
private Expression defaultValue;
private boolean allColumns = false;

@Override
Expand Down Expand Up @@ -78,13 +81,35 @@ public void setExpression(Expression expression) {
this.expression = expression;
}

@Override
public Expression getOffset() {
return offset;
}

public void setOffset(Expression offset) {
this.offset = offset;
}

public Expression getDefaultValue() {
return defaultValue;
}

public void setDefaultValue(Expression defaultValue) {
this.defaultValue = defaultValue;
}

@Override
public String toString() {
StringBuilder b = new StringBuilder();

b.append(name).append("(");
if (expression != null) {
b.append(expression.toString());
if (offset != null) {
b.append(", ").append(offset.toString());
if (defaultValue != null) {
b.append(", ").append(defaultValue.toString());
}
}
} else if (isAllColumns()) {
b.append("*");
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj
Original file line number Diff line number Diff line change
Expand Up @@ -1765,13 +1765,18 @@ AnalyticExpression AnalyticExpression() :
Token token = null;
Column column = null;
Expression expr = null;
Expression offset = null;
Expression defaultValue = null;
}
{
token=<S_IDENTIFIER> { retval.setName(token.image); } "(" [ expr=SimpleExpression() | "*" { retval.setAllColumns(true); } ] ")" <K_OVER> "("
token=<S_IDENTIFIER> { retval.setName(token.image); }
"(" [ expr=SimpleExpression() ["," offset=SimpleExpression() ["," defaultValue=SimpleExpression() ]] | "*" { retval.setAllColumns(true); } ] ")" <K_OVER> "("
[<K_PARTITION> <K_BY> column=Column() {plist.add(column);} ("," column=Column() {plist.add(column);} )* ]
[olist=OrderByElements() ]
{
retval.setExpression(expr);
retval.setOffset(offset);
retval.setDefaultValue(defaultValue);
retval.setPartitionByColumns(plist);
retval.setOrderByElements(olist);
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/net/sf/jsqlparser/test/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,16 @@ public void testProblemSqlAnalytic9CommaListPartition() throws JSQLParserExcepti
assertSqlCanBeParsedAndDeparsed(stmt);
}

public void testProblemSqlAnalytic10Lag() throws JSQLParserException {
String stmt = "SELECT a, lag(a, 1) OVER (PARTITION BY c ORDER BY a, b) AS n FROM table1";
assertSqlCanBeParsedAndDeparsed(stmt);
}

public void testProblemSqlAnalytic11Lag() throws JSQLParserException {
String stmt = "SELECT a, lag(a, 1, 0) OVER (PARTITION BY c ORDER BY a, b) AS n FROM table1";
assertSqlCanBeParsedAndDeparsed(stmt);
}

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

0 comments on commit 99b46bf

Please sign in to comment.