Skip to content

Commit f32fa61

Browse files
authored
support FILTER not only for window function (#1046)
* support FILTER not only for window function * Fixed imports
1 parent 3e84a37 commit f32fa61

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,15 @@ public String toString() {
195195
if (filterExpression != null) {
196196
b.append("FILTER (WHERE ");
197197
b.append(filterExpression.toString());
198-
b.append(") ");
198+
b.append(")");
199+
if (type != AnalyticType.FILTER_ONLY) {
200+
b.append(" ");
201+
}
199202
}
200203

201204
switch (type) {
205+
case FILTER_ONLY:
206+
return b.toString();
202207
case WITHIN_GROUP:
203208
b.append("WITHIN GROUP");
204209
break;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111

1212
public enum AnalyticType {
1313
OVER,
14-
WITHIN_GROUP
14+
WITHIN_GROUP,
15+
FILTER_ONLY
1516
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import net.sf.jsqlparser.expression.AllComparisonExpression;
1616
import net.sf.jsqlparser.expression.AnalyticExpression;
17+
import net.sf.jsqlparser.expression.AnalyticType;
1718
import net.sf.jsqlparser.expression.AnyComparisonExpression;
1819
import net.sf.jsqlparser.expression.ArrayExpression;
1920
import net.sf.jsqlparser.expression.BinaryExpression;
@@ -706,10 +707,15 @@ public void visit(AnalyticExpression aexpr) {
706707
if (aexpr.getFilterExpression() != null) {
707708
buffer.append("FILTER (WHERE ");
708709
aexpr.getFilterExpression().accept(this);
709-
buffer.append(") ");
710+
buffer.append(")");
711+
if (aexpr.getType() != AnalyticType.FILTER_ONLY) {
712+
buffer.append(" ");
713+
}
710714
}
711715

712716
switch (aexpr.getType()) {
717+
case FILTER_ONLY:
718+
return;
713719
case WITHIN_GROUP:
714720
buffer.append("WITHIN GROUP");
715721
break;

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3434,25 +3434,13 @@ KeepExpression KeepExpression() : {
34343434
}
34353435
}
34363436

3437-
AnalyticExpression AnalyticExpression(Function function) :
3438-
{
3439-
AnalyticExpression retval = new AnalyticExpression(function);
3440-
ExpressionList expressionList = null;
3441-
List<OrderByElement> olist = null;
3442-
Token token = null;
3443-
Expression expr = null;
3444-
Expression offset = null;
3445-
Expression defaultValue = null;
3446-
WindowElement windowElement = null;
3447-
//KeepExpression keep = null;
3448-
//boolean distinct = false;
3449-
boolean partitionByBrackets = false;
3450-
Expression filter = null;
3451-
}
3452-
{
3453-
3454-
[ <K_FILTER> "(" <K_WHERE> filter = Expression() ")" ]
34553437

3438+
void windowFun(AnalyticExpression retval):{
3439+
ExpressionList expressionList = null;
3440+
List<OrderByElement> olist = null;
3441+
WindowElement windowElement = null;
3442+
boolean partitionByBrackets = false;
3443+
} {
34563444
(<K_OVER> {retval.setType(AnalyticType.OVER);}
34573445
| <K_WITHIN> <K_GROUP> {retval.setType(AnalyticType.WITHIN_GROUP);} )
34583446

@@ -3463,15 +3451,24 @@ AnalyticExpression AnalyticExpression(Function function) :
34633451
]
34643452
[olist=OrderByElements() ]
34653453
[windowElement = WindowElement() ]
3454+
{
3455+
retval.setPartitionExpressionList(expressionList, partitionByBrackets);
3456+
retval.setOrderByElements(olist);
3457+
retval.setWindowElement(windowElement);
3458+
}
3459+
")"
3460+
}
34663461

3462+
AnalyticExpression AnalyticExpression(Function function) :
3463+
{
3464+
AnalyticExpression retval = new AnalyticExpression(function);
3465+
Expression filter = null;
3466+
}
3467+
{
3468+
((<K_FILTER> "(" <K_WHERE> {retval.setType(AnalyticType.FILTER_ONLY);} filter = Expression() ")" [ LOOKAHEAD(2) windowFun(retval) ] )
3469+
| windowFun(retval))
34673470
{
3468-
retval.setPartitionExpressionList(expressionList, partitionByBrackets);
3469-
retval.setOrderByElements(olist);
3470-
retval.setWindowElement(windowElement);
34713471
retval.setFilterExpression(filter);
3472-
}
3473-
")"
3474-
{
34753472
return retval;
34763473
}
34773474
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,11 @@ public void testAnalyticPartitionBooleanExpressionIssue864_2() throws JSQLParser
19781978
assertSqlCanBeParsedAndDeparsed("SELECT COUNT(*) OVER (PARTITION BY (event = 'admit' OR event = 'family visit') ) family_visits FROM patients");
19791979
}
19801980

1981+
@Test
1982+
public void testAnalyticFunctionFilterIssue934() throws JSQLParserException {
1983+
assertSqlCanBeParsedAndDeparsed("SELECT COUNT(*) FILTER (WHERE name = 'Raj') FROM table");
1984+
}
1985+
19811986
@Test
19821987
public void testFunctionLeft() throws JSQLParserException {
19831988
String statement = "SELECT left(table1.col1, 4) FROM table1";

0 commit comments

Comments
 (0)