Skip to content

Commit 6527662

Browse files
committed
fixes #593
1 parent ac34efb commit 6527662

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
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
@@ -48,6 +48,7 @@ public class AnalyticExpression extends ASTNodeAccessImpl implements Expression
4848
private WindowElement windowElement;
4949
private KeepExpression keep = null;
5050
private AnalyticType type = AnalyticType.OVER;
51+
private boolean distinct = false;
5152

5253
@Override
5354
public void accept(ExpressionVisitor expressionVisitor) {
@@ -126,11 +127,22 @@ public void setType(AnalyticType type) {
126127
this.type = type;
127128
}
128129

130+
public boolean isDistinct() {
131+
return distinct;
132+
}
133+
134+
public void setDistinct(boolean distinct) {
135+
this.distinct = distinct;
136+
}
137+
129138
@Override
130139
public String toString() {
131140
StringBuilder b = new StringBuilder();
132141

133142
b.append(name).append("(");
143+
if (isDistinct()) {
144+
b.append("DISTINCT ");
145+
}
134146
if (expression != null) {
135147
b.append(expression.toString());
136148
if (offset != null) {
@@ -146,7 +158,7 @@ public String toString() {
146158
if (keep != null) {
147159
b.append(keep.toString()).append(" ");
148160
}
149-
161+
150162
switch (type) {
151163
case WITHIN_GROUP:
152164
b.append("WITHIN GROUP");

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,9 @@ public void visit(AnalyticExpression aexpr) {
604604
WindowElement windowElement = aexpr.getWindowElement();
605605

606606
buffer.append(name).append("(");
607+
if (aexpr.isDistinct()) {
608+
buffer.append("DISTINCT ");
609+
}
607610
if (expression != null) {
608611
expression.accept(this);
609612
if (offset != null) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2616,10 +2616,14 @@ AnalyticExpression AnalyticExpression() :
26162616
Expression defaultValue = null;
26172617
WindowElement windowElement = null;
26182618
KeepExpression keep = null;
2619+
boolean distinct = false;
26192620
}
26202621
{
26212622
token=<S_IDENTIFIER> { retval.setName(token.image); }
2622-
"(" [ expr=SimpleExpression() ["," offset=SimpleExpression() ["," defaultValue=SimpleExpression() ]] | "*" { retval.setAllColumns(true); } ] ")"
2623+
"(" [
2624+
[ <K_DISTINCT> {distinct = true;} ]
2625+
(expr=SimpleExpression() ["," offset=SimpleExpression() ["," defaultValue=SimpleExpression() ]] | "*" { retval.setAllColumns(true); } )
2626+
] ")"
26232627

26242628
[ keep=KeepExpression() ]
26252629

@@ -2631,6 +2635,7 @@ AnalyticExpression AnalyticExpression() :
26312635
[olist=OrderByElements() [windowElement = WindowElement() ] ]
26322636

26332637
{
2638+
retval.setDistinct(distinct);
26342639
retval.setExpression(expr);
26352640
retval.setOffset(offset);
26362641
retval.setDefaultValue(defaultValue);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,11 @@ public void testAnalyticFunctionProblem1() throws JSQLParserException {
16181618
String statement = "SELECT last_value(s.revenue_hold) OVER (PARTITION BY s.id_d_insertion_order, s.id_d_product_ad_attr, trunc(s.date_id, 'mm') ORDER BY s.date_id) AS col FROM s";
16191619
assertSqlCanBeParsedAndDeparsed(statement);
16201620
}
1621+
1622+
public void testAnalyticFunction19() throws JSQLParserException {
1623+
String statement = "SELECT count(DISTINCT CASE WHEN client_organic_search_drop_flag = 1 THEN brand END) OVER (PARTITION BY client, category_1, category_2, category_3, category_4 ) AS client_brand_org_drop_count FROM sometable";
1624+
assertSqlCanBeParsedAndDeparsed(statement);
1625+
}
16211626

16221627
public void testAnalyticFunctionProblem1b() throws JSQLParserException {
16231628
String statement = "SELECT last_value(s.revenue_hold) OVER (PARTITION BY s.id_d_insertion_order, s.id_d_product_ad_attr, trunc(s.date_id, 'mm') ORDER BY s.date_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS col FROM s";

0 commit comments

Comments
 (0)