Skip to content

Commit

Permalink
fixes #866
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Oct 16, 2019
1 parent 51c92d8 commit 25e1dcc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Also I would like to know about needed examples or documentation stuff.

## Extensions in the latest SNAPSHOT version 3.1

* introduced **FILTER** expression for window functions
* allow more complex expressions for **CASE**.
* allowed **start** as object name as column name or table name
* introduced more positions for **!** instead of **NOT**
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/AnalyticExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class AnalyticExpression extends ASTNodeAccessImpl implements Expression
private AnalyticType type = AnalyticType.OVER;
private boolean distinct = false;
private boolean ignoreNulls = false;
private Expression filterExpression = null;

public AnalyticExpression() {
}
Expand Down Expand Up @@ -82,6 +83,8 @@ public KeepExpression getKeep() {
public void setKeep(KeepExpression keep) {
this.keep = keep;
}



public ExpressionList getPartitionExpressionList() {
return partitionBy.getPartitionExpressionList();
Expand Down Expand Up @@ -189,6 +192,12 @@ public String toString() {
b.append(keep.toString()).append(" ");
}

if (filterExpression != null) {
b.append("FILTER (WHERE ");
b.append(filterExpression.toString());
b.append(") ");
}

switch (type) {
case WITHIN_GROUP:
b.append("WITHIN GROUP");
Expand All @@ -214,4 +223,11 @@ public void setAllColumns(boolean allColumns) {
this.allColumns = allColumns;
}

public Expression getFilterExpression() {
return filterExpression;
}

public void setFilterExpression(Expression filterExpression) {
this.filterExpression = filterExpression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,12 @@ public void visit(AnalyticExpression aexpr) {
keep.accept(this);
buffer.append(" ");
}

if (aexpr.getFilterExpression() != null) {
buffer.append("FILTER (WHERE ");
aexpr.getFilterExpression().accept(this);
buffer.append(") ");
}

switch (aexpr.getType()) {
case WITHIN_GROUP:
Expand Down
6 changes: 6 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_EXPLAIN:"EXPLAIN">
| <K_EXTRACT:"EXTRACT">
| <K_FETCH:"FETCH">
| <K_FILTER: "FILTER">
| <K_FIRST: "FIRST">
| <K_FALSE: "FALSE">
| <K_FOLLOWING: "FOLLOWING">
Expand Down Expand Up @@ -3115,8 +3116,12 @@ AnalyticExpression AnalyticExpression(Function function) :
//KeepExpression keep = null;
//boolean distinct = false;
boolean partitionByBrackets = false;
Expression filter = null;
}
{

[ <K_FILTER> "(" <K_WHERE> filter = Expression() ")" ]

(<K_OVER> {retval.setType(AnalyticType.OVER);}
| <K_WITHIN> <K_GROUP> {retval.setType(AnalyticType.WITHIN_GROUP);} )

Expand All @@ -3130,6 +3135,7 @@ AnalyticExpression AnalyticExpression(Function function) :
retval.setPartitionExpressionList(expressionList, partitionByBrackets);
retval.setOrderByElements(olist);
retval.setWindowElement(windowElement);
retval.setFilterExpression(filter);
}
")"
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,11 @@ public void testAnalyticFunctionProblem1b() throws JSQLParserException {
public void testAnalyticFunctionIssue670() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT last_value(some_column IGNORE NULLS) OVER (PARTITION BY some_other_column_1, some_other_column_2 ORDER BY some_other_column_3 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) column_alias FROM some_table");
}

@Test
public void testAnalyticFunctionFilterIssue866() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT COUNT(*) FILTER (WHERE name = 'Raj') OVER (PARTITION BY name ) FROM table");
}

@Test
public void testFunctionLeft() throws JSQLParserException {
Expand Down

0 comments on commit 25e1dcc

Please sign in to comment.