Skip to content

Commit f18e92e

Browse files
sur5anAdhikesavan
andauthored
supporting/fixing unique inside sql function such as count eg - SELECT count(UNIQUE col2) FROM mytable (#1184)
Co-authored-by: Adhikesavan <radhikesavan@paypal.com>
1 parent 838379f commit f18e92e

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class AnalyticExpression extends ASTNodeAccessImpl implements Expression
3434
private KeepExpression keep = null;
3535
private AnalyticType type = AnalyticType.OVER;
3636
private boolean distinct = false;
37+
private boolean unique = false;
3738
private boolean ignoreNulls = false;
3839
private Expression filterExpression = null;
3940
private WindowElement windowElement = null;
@@ -45,6 +46,7 @@ public AnalyticExpression(Function function) {
4546
name = function.getName();
4647
allColumns = function.isAllColumns();
4748
distinct = function.isDistinct();
49+
unique = function.isUnique();
4850

4951
ExpressionList list = function.getParameters();
5052
if (list != null) {
@@ -157,6 +159,14 @@ public void setDistinct(boolean distinct) {
157159
this.distinct = distinct;
158160
}
159161

162+
public boolean isUnique() {
163+
return unique;
164+
}
165+
166+
public void setUnique(boolean unique) {
167+
this.unique = unique;
168+
}
169+
160170
public boolean isIgnoreNulls() {
161171
return ignoreNulls;
162172
}
@@ -284,6 +294,11 @@ public AnalyticExpression withDistinct(boolean distinct) {
284294
return this;
285295
}
286296

297+
public AnalyticExpression withUnique(boolean unique) {
298+
this.setUnique(unique);
299+
return this;
300+
}
301+
287302
public AnalyticExpression withIgnoreNulls(boolean ignoreNulls) {
288303
this.setIgnoreNulls(ignoreNulls);
289304
return this;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class Function extends ASTNodeAccessImpl implements Expression {
2727
private NamedExpressionList namedParameters;
2828
private boolean allColumns = false;
2929
private boolean distinct = false;
30+
private boolean unique = false;
3031
private boolean isEscaped = false;
3132
private Expression attribute;
3233
private String attributeName;
@@ -89,6 +90,19 @@ public void setDistinct(boolean b) {
8990
distinct = b;
9091
}
9192

93+
/**
94+
* true if the function is "unique"
95+
*
96+
* @return true if the function is "unique"
97+
*/
98+
public boolean isUnique() {
99+
return unique;
100+
}
101+
102+
public void setUnique(boolean b) {
103+
unique = b;
104+
}
105+
92106
/**
93107
* The list of parameters of the function (if any, else null) If the parameter is "*",
94108
* allColumns is set to true
@@ -164,6 +178,8 @@ public String toString() {
164178
b.append("(");
165179
if (isDistinct()) {
166180
b.append("DISTINCT ");
181+
} else if (isUnique()) {
182+
b.append("UNIQUE ");
167183
}
168184
if (isAllColumns()) {
169185
b.append("ALL ");
@@ -251,6 +267,11 @@ public Function withDistinct(boolean distinct) {
251267
return this;
252268
}
253269

270+
public Function withUnique(boolean unique) {
271+
this.setUnique(unique);
272+
return this;
273+
}
274+
254275
public List<OrderByElement> getOrderByElements() {
255276
return orderByElements;
256277
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ public void visit(Function function) {
493493
buffer.append("DISTINCT ");
494494
} else if (function.isAllColumns()) {
495495
buffer.append("ALL ");
496+
} else if (function.isUnique()) {
497+
useBracketsInExprList = false;
498+
buffer.append("(UNIQUE ");
496499
}
497500
if (function.getNamedParameters() != null) {
498501
visit(function.getNamedParameters());
@@ -704,6 +707,9 @@ public void visit(AnalyticExpression aexpr) {
704707
if (aexpr.isDistinct()) {
705708
buffer.append("DISTINCT ");
706709
}
710+
if (aexpr.isUnique()) {
711+
buffer.append("UNIQUE ");
712+
}
707713
if (expression != null) {
708714
expression.accept(this);
709715
if (offset != null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3874,7 +3874,7 @@ Function InternalFunction(Function retval) :
38743874
{
38753875
funcName = RelObjectNameList()
38763876

3877-
"(" [ [ LOOKAHEAD(2)(<K_DISTINCT> { retval.setDistinct(true); } | <K_ALL> { retval.setAllColumns(true); }) ]
3877+
"(" [ [ LOOKAHEAD(2)(<K_DISTINCT> { retval.setDistinct(true); } | <K_ALL> { retval.setAllColumns(true); } | <K_UNIQUE> { retval.setUnique(true); }) ]
38783878
( LOOKAHEAD(4)
38793879
"*" { retval.setAllColumns(true); }
38803880
|

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,11 @@ public void testCount2() throws JSQLParserException {
15901590
assertSqlCanBeParsedAndDeparsed("SELECT count(ALL col1 + col2) FROM mytable");
15911591
}
15921592

1593+
@Test
1594+
public void testCount3() throws JSQLParserException {
1595+
assertSqlCanBeParsedAndDeparsed("SELECT count(UNIQUE col) FROM mytable");
1596+
}
1597+
15931598
@Test
15941599
public void testMysqlQuote() throws JSQLParserException {
15951600
String statement = "SELECT `a.OWNERLASTNAME`, `OWNERFIRSTNAME` " + "FROM `ANTIQUEOWNERS` AS a, ANTIQUES AS b "

0 commit comments

Comments
 (0)