Skip to content

Commit 25e1dcc

Browse files
committed
fixes #866
1 parent 51c92d8 commit 25e1dcc

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Also I would like to know about needed examples or documentation stuff.
6565

6666
## Extensions in the latest SNAPSHOT version 3.1
6767

68+
* introduced **FILTER** expression for window functions
6869
* allow more complex expressions for **CASE**.
6970
* allowed **start** as object name as column name or table name
7071
* introduced more positions for **!** instead of **NOT**

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class AnalyticExpression extends ASTNodeAccessImpl implements Expression
3535
private AnalyticType type = AnalyticType.OVER;
3636
private boolean distinct = false;
3737
private boolean ignoreNulls = false;
38+
private Expression filterExpression = null;
3839

3940
public AnalyticExpression() {
4041
}
@@ -82,6 +83,8 @@ public KeepExpression getKeep() {
8283
public void setKeep(KeepExpression keep) {
8384
this.keep = keep;
8485
}
86+
87+
8588

8689
public ExpressionList getPartitionExpressionList() {
8790
return partitionBy.getPartitionExpressionList();
@@ -189,6 +192,12 @@ public String toString() {
189192
b.append(keep.toString()).append(" ");
190193
}
191194

195+
if (filterExpression != null) {
196+
b.append("FILTER (WHERE ");
197+
b.append(filterExpression.toString());
198+
b.append(") ");
199+
}
200+
192201
switch (type) {
193202
case WITHIN_GROUP:
194203
b.append("WITHIN GROUP");
@@ -214,4 +223,11 @@ public void setAllColumns(boolean allColumns) {
214223
this.allColumns = allColumns;
215224
}
216225

226+
public Expression getFilterExpression() {
227+
return filterExpression;
228+
}
229+
230+
public void setFilterExpression(Expression filterExpression) {
231+
this.filterExpression = filterExpression;
232+
}
217233
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,12 @@ public void visit(AnalyticExpression aexpr) {
642642
keep.accept(this);
643643
buffer.append(" ");
644644
}
645+
646+
if (aexpr.getFilterExpression() != null) {
647+
buffer.append("FILTER (WHERE ");
648+
aexpr.getFilterExpression().accept(this);
649+
buffer.append(") ");
650+
}
645651

646652
switch (aexpr.getType()) {
647653
case WITHIN_GROUP:

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
175175
| <K_EXPLAIN:"EXPLAIN">
176176
| <K_EXTRACT:"EXTRACT">
177177
| <K_FETCH:"FETCH">
178+
| <K_FILTER: "FILTER">
178179
| <K_FIRST: "FIRST">
179180
| <K_FALSE: "FALSE">
180181
| <K_FOLLOWING: "FOLLOWING">
@@ -3115,8 +3116,12 @@ AnalyticExpression AnalyticExpression(Function function) :
31153116
//KeepExpression keep = null;
31163117
//boolean distinct = false;
31173118
boolean partitionByBrackets = false;
3119+
Expression filter = null;
31183120
}
31193121
{
3122+
3123+
[ <K_FILTER> "(" <K_WHERE> filter = Expression() ")" ]
3124+
31203125
(<K_OVER> {retval.setType(AnalyticType.OVER);}
31213126
| <K_WITHIN> <K_GROUP> {retval.setType(AnalyticType.WITHIN_GROUP);} )
31223127

@@ -3130,6 +3135,7 @@ AnalyticExpression AnalyticExpression(Function function) :
31303135
retval.setPartitionExpressionList(expressionList, partitionByBrackets);
31313136
retval.setOrderByElements(olist);
31323137
retval.setWindowElement(windowElement);
3138+
retval.setFilterExpression(filter);
31333139
}
31343140
")"
31353141
{

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,11 @@ public void testAnalyticFunctionProblem1b() throws JSQLParserException {
18591859
public void testAnalyticFunctionIssue670() throws JSQLParserException {
18601860
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");
18611861
}
1862+
1863+
@Test
1864+
public void testAnalyticFunctionFilterIssue866() throws JSQLParserException {
1865+
assertSqlCanBeParsedAndDeparsed("SELECT COUNT(*) FILTER (WHERE name = 'Raj') OVER (PARTITION BY name ) FROM table");
1866+
}
18621867

18631868
@Test
18641869
public void testFunctionLeft() throws JSQLParserException {

0 commit comments

Comments
 (0)