Skip to content

Commit d36fd2b

Browse files
committed
wildcard extension for analytic expression
1 parent 27be9c5 commit d36fd2b

File tree

3 files changed

+24
-41
lines changed

3 files changed

+24
-41
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
/**
88
* Analytic function. The name of the function is variable but the parameters
99
* following the special analytic function path. e.g. row_number() over (order
10-
* by test)
10+
* by test). Additional there can be an expression for an analytical aggregate
11+
* like sum(col) or the "all collumns" wildcard like count(*).
1112
*
1213
* @author tw
1314
*/
@@ -17,6 +18,7 @@ public class AnalyticExpression implements Expression {
1718
private List<OrderByElement> orderByElements;
1819
private String name;
1920
private Expression expression;
21+
private boolean allColumns = false;
2022

2123
@Override
2224
public void accept(ExpressionVisitor expressionVisitor) {
@@ -62,6 +64,8 @@ public String toString() {
6264
b.append(name).append("(");
6365
if (expression != null) {
6466
b.append(expression.toString());
67+
} else if (isAllColumns()) {
68+
b.append("*");
6569
}
6670
b.append(") OVER (");
6771
if (partitionByColumns != null && !partitionByColumns.isEmpty()) {
@@ -89,4 +93,12 @@ public String toString() {
8993

9094
return b.toString();
9195
}
96+
97+
public boolean isAllColumns() {
98+
return allColumns;
99+
}
100+
101+
public void setAllColumns(boolean allColumns) {
102+
this.allColumns = allColumns;
103+
}
92104
}

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

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -639,45 +639,6 @@ SetOperationList SetOperationList():
639639
}
640640
}
641641

642-
/*
643-
Union Union():
644-
{
645-
Union union = new Union();
646-
List orderByElements = null;
647-
Limit limit = null;
648-
PlainSelect select = null;
649-
ArrayList selects = new ArrayList();
650-
651-
this is not 100% right, since multiple UNION could have different ALL/DISTINCT clauses...
652-
653-
}
654-
{
655-
(
656-
657-
(
658-
"(" select=PlainSelect() {selects.add(select);} ")"
659-
<K_UNION> [ <K_ALL> { union.setAll(true); } | <K_DISTINCT> { union.setDistinct(true); } ]
660-
"(" select=PlainSelect() {selects.add(select);} ")"
661-
( <K_UNION> [ <K_ALL> | <K_DISTINCT> ] "(" select=PlainSelect() {selects.add(select);} ")" )*
662-
[orderByElements=OrderByElements() {union.setOrderByElements(orderByElements);} ]
663-
[limit=Limit() {union.setLimit(limit);} ]
664-
)
665-
|
666-
(
667-
select=PlainSelect() {selects.add(select);}
668-
<K_UNION> [ <K_ALL> { union.setAll(true); } | <K_DISTINCT> { union.setDistinct(true); } ]
669-
select=PlainSelect() {selects.add(select);}
670-
( <K_UNION> [ <K_ALL> | <K_DISTINCT> ] select=PlainSelect() {selects.add(select);} )*
671-
672-
)
673-
)
674-
675-
{
676-
union.setPlainSelects(selects);
677-
return union;
678-
}
679-
}
680-
*/
681642

682643
List<WithItem> WithList():
683644
{
@@ -1569,7 +1530,7 @@ AnalyticExpression AnalyticExpression() :
15691530
Expression expr = null;
15701531
}
15711532
{
1572-
token=<S_IDENTIFIER> { retval.setName(token.image); } "(" [ expr=SimpleExpression() ] ")" <K_OVER> "("
1533+
token=<S_IDENTIFIER> { retval.setName(token.image); } "(" [ expr=SimpleExpression() | "*" { retval.setAllColumns(true); } ] ")" <K_OVER> "("
15731534
[<K_PARTITION> <K_BY> (column=Column() {plist.add(column);} )+ ]
15741535
[olist=OrderByElements() ]
15751536
{

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,16 @@ public void testProblemSqlAnalytic6AggregateColumnValue() throws JSQLParserExcep
661661
String stmt = "SELECT a, sum(b + 5) OVER (ORDER BY a) AS n FROM table1";
662662
assertSqlCanBeParsedAndDeparsed(stmt);
663663
}
664+
665+
public void testProblemSqlAnalytic7Count() throws JSQLParserException {
666+
String stmt = "SELECT count(*) OVER () AS n FROM table1";
667+
assertSqlCanBeParsedAndDeparsed(stmt);
668+
}
669+
670+
public void testProblemSqlAnalytic8Complex() throws JSQLParserException {
671+
String stmt = "SELECT ID, NAME, SALARY, SUM(SALARY) OVER () AS SUM_SAL, AVG(SALARY) OVER () AS AVG_SAL, MIN(SALARY) OVER () AS MIN_SAL, MAX(SALARY) OVER () AS MAX_SAL, COUNT(*) OVER () AS ROWS FROM STAFF WHERE ID < 60 ORDER BY ID";
672+
assertSqlCanBeParsedAndDeparsed(stmt);
673+
}
664674

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

0 commit comments

Comments
 (0)