Skip to content

Commit 5cf281f

Browse files
feat: ON OVERFLOW support for the ListAgg function (Oracle, DB2)
- fixes #2003 Signed-off-by: Andreas Reichel <[email protected]>
1 parent 3d48835 commit 5cf281f

File tree

5 files changed

+82
-8
lines changed

5 files changed

+82
-8
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class AnalyticExpression extends ASTNodeAccessImpl implements Expression
4040
private boolean ignoreNullsOutside = false; // IGNORE NULLS outside function parameters
4141
private Expression filterExpression = null;
4242
private List<OrderByElement> funcOrderBy = null;
43+
private String onOverflowTruncate = null;
44+
4345
private String windowName = null; // refers to an external window definition (paritionBy,
4446
// orderBy, windowElement)
4547
private WindowDefinition windowDef = new WindowDefinition();
@@ -78,6 +80,7 @@ public AnalyticExpression(Function function) {
7880
this.ignoreNullsOutside = function.isIgnoreNullsOutside();
7981
this.nullHandling = function.getNullHandling();
8082
this.funcOrderBy = function.getOrderByElements();
83+
this.onOverflowTruncate = function.getOnOverflowTruncate();
8184
this.limit = function.getLimit();
8285
this.keep = function.getKeep();
8386
}
@@ -96,6 +99,15 @@ public void setOrderByElements(List<OrderByElement> orderByElements) {
9699
windowDef.orderBy.setOrderByElements(orderByElements);
97100
}
98101

102+
public String getOnOverflowTruncate() {
103+
return onOverflowTruncate;
104+
}
105+
106+
public AnalyticExpression setOnOverflowTruncate(String onOverflowTruncate) {
107+
this.onOverflowTruncate = onOverflowTruncate;
108+
return this;
109+
}
110+
99111
public KeepExpression getKeep() {
100112
return keep;
101113
}
@@ -294,6 +306,10 @@ public String toString() {
294306
b.append(funcOrderBy.stream().map(OrderByElement::toString).collect(joining(", ")));
295307
}
296308

309+
if (onOverflowTruncate != null) {
310+
b.append(" ON OVERFLOW ").append(onOverflowTruncate);
311+
}
312+
297313
if (limit != null) {
298314
b.append(limit);
299315
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class Function extends ASTNodeAccessImpl implements Expression {
3838
private boolean ignoreNullsOutside = false; // IGNORE NULLS outside function parameters
3939
private Limit limit = null;
4040
private KeepExpression keep = null;
41+
private String onOverflowTruncate = null;
4142

4243
public Function() {}
4344

@@ -302,6 +303,11 @@ public String toString() {
302303
if (limit != null) {
303304
b.append(limit);
304305
}
306+
307+
if (onOverflowTruncate != null) {
308+
b.append(" ON OVERFLOW ").append(onOverflowTruncate);
309+
}
310+
305311
b.append(")");
306312
params = b.toString();
307313
} else {
@@ -399,6 +405,15 @@ public void setOrderByElements(List<OrderByElement> orderByElements) {
399405
this.orderByElements = orderByElements;
400406
}
401407

408+
public String getOnOverflowTruncate() {
409+
return onOverflowTruncate;
410+
}
411+
412+
public Function setOnOverflowTruncate(String onOverflowTruncate) {
413+
this.onOverflowTruncate = onOverflowTruncate;
414+
return this;
415+
}
416+
402417
public <E extends Expression> E getAttribute(Class<E> type) {
403418
return type.cast(getAttribute());
404419
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,11 @@ public <S> StringBuilder visit(Function function, S context) {
857857
orderByDeParser.deParseElement(orderByElement);
858858
}
859859
}
860+
861+
if (function.getOnOverflowTruncate() != null) {
862+
buffer.append(" ON OVERFLOW ").append(function.getOnOverflowTruncate());
863+
}
864+
860865
if (function.getLimit() != null) {
861866
new LimitDeparser(this, buffer).deParse(function.getLimit());
862867
}
@@ -1127,6 +1132,10 @@ public <S> StringBuilder visit(AnalyticExpression analyticExpression, S context)
11271132
.collect(joining(", ")));
11281133
}
11291134

1135+
if (analyticExpression.getOnOverflowTruncate() != null) {
1136+
buffer.append(" ON OVERFLOW ").append(analyticExpression.getOnOverflowTruncate());
1137+
}
1138+
11301139
if (analyticExpression.getLimit() != null) {
11311140
new LimitDeparser(this, buffer).deParse(analyticExpression.getLimit());
11321141
}

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
212212
| <K_CONSTRAINTS:"CONSTRAINTS">
213213
| <K_CONVERT:"CONVERT">
214214
| <K_COSTS: "COSTS">
215+
| <K_COUNT: "COUNT">
215216
| <K_CREATE:"CREATE">
216217
| <K_CROSS:"CROSS">
217218
| <K_CURRENT: "CURRENT">
@@ -241,12 +242,13 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
241242
| <K_DUPLICATE: "DUPLICATE">
242243
| <K_ELEMENTS: "ELEMENTS">
243244
| <K_ELSE:"ELSE">
244-
| <K_EMIT : "EMIT">
245-
| <K_ENABLE : "ENABLE">
246-
| <K_END:"END">
247-
| <K_ESCAPE:"ESCAPE">
248-
| <K_EXCEPT:"EXCEPT">
249-
| <K_EXCLUDE : "EXCLUDE">
245+
| <K_EMIT: "EMIT">
246+
| <K_ENABLE: "ENABLE">
247+
| <K_END: "END">
248+
| <K_ERROR: "ERROR">
249+
| <K_ESCAPE: "ESCAPE">
250+
| <K_EXCEPT: "EXCEPT">
251+
| <K_EXCLUDE: "EXCLUDE">
250252
| <K_EXCLUDES: "EXCLUDES"> /* Salesforce SOQL */
251253
| <K_EXEC: "EXEC">
252254
| <K_EXECUTE: "EXECUTE">
@@ -370,6 +372,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
370372
| <K_OUTER:"OUTER">
371373
| <K_OUTPUT:"OUTPUT">
372374
| <K_OVER:"OVER">
375+
| <K_OVERFLOW:"OVERFLOW">
373376
| <K_OVERLAPS:"OVERLAPS">
374377
| <K_OPTIMIZE: "OPTIMIZE" >
375378
| <K_PARALLEL:"PARALLEL">
@@ -2015,7 +2018,7 @@ String RelObjectNameWithoutValue() :
20152018
{ Token tk = null; }
20162019
{
20172020
( tk=<DATA_TYPE> | tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER> | tk=<K_DATE_LITERAL> | tk=<K_DATETIMELITERAL> | tk=<K_STRING_FUNCTION_NAME> | tk=<K_ISOLATION> | tk=<K_TIME_KEY_EXPR> | tk=<K_TEXT_LITERAL>
2018-
| tk="ACTION" | tk="ACTIVE" | tk="ADD" | tk="ADVANCE" | tk="ADVISE" | tk="AGAINST" | tk="ALGORITHM" | tk="ALTER" | tk="ANALYZE" | tk="APPLY" | tk="APPROXIMATE" | tk="ARCHIVE" | tk="ARRAY" | tk="ASC" | tk="AT" | tk="AUTHORIZATION" | tk="AUTO" | tk="BASE64" | tk="BEGIN" | tk="BERNOULLI" | tk="BINARY" | tk="BIT" | tk="BLOCK" | tk="BROWSE" | tk="BUFFERS" | tk="BY" | tk="BYTE" | tk="BYTES" | tk="CACHE" | tk="CALL" | tk="CASCADE" | tk="CASE" | tk="CAST" | tk="CHANGE" | tk="CHANGES" | tk="CHAR" | tk="CHARACTER" | tk="CHECKPOINT" | tk="CLOSE" | tk="COLLATE" | tk="COLUMN" | tk="COLUMNS" | tk="COMMENT" | tk="COMMIT" | tk="CONCURRENTLY" | tk="CONFLICT" | tk="CONSTRAINTS" | tk="CONVERT" | tk="COSTS" | tk="CS" | tk="CYCLE" | tk="DATA" | tk="DATABASE" | tk="DATETIME" | tk="DBA_RECYCLEBIN" | tk="DDL" | tk="DECLARE" | tk="DEFERRABLE" | tk="DELAYED" | tk="DELETE" | tk="DESC" | tk="DESCRIBE" | tk="DISABLE" | tk="DISCONNECT" | tk="DIV" | tk="DML" | tk="DO" | tk="DOMAIN" | tk="DROP" | tk="DUMP" | tk="DUPLICATE" | tk="ELEMENTS" | tk="EMIT" | tk="ENABLE" | tk="END" | tk="ESCAPE" | tk="EXCLUDE" | tk="EXEC" | tk="EXECUTE" | tk="EXPLAIN" | tk="EXPLICIT" | tk="EXTENDED" | tk="EXTRACT" | tk="FALSE" | tk="FILTER" | tk="FIRST" | tk="FLUSH" | tk="FN" | tk="FOLLOWING" | tk="FORMAT" | tk="FULLTEXT" | tk="FUNCTION" | tk="GRANT" | tk="GROUP_CONCAT" | tk="GUARD" | tk="HASH" | tk="HIGH_PRIORITY" | tk="HISTORY" | tk="HOPPING" | tk="INCLUDE" | tk="INCLUDE_NULL_VALUES" | tk="INCREMENT" | tk="INDEX" | tk="INSERT" | tk="INTERLEAVE" | tk="INTERPRET" | tk="INVALIDATE" | tk="ISNULL" | tk="JSON" | tk="JSON_ARRAY" | tk="JSON_ARRAYAGG" | tk="JSON_OBJECT" | tk="JSON_OBJECTAGG" | tk="KEEP" | tk="KEY" | tk="KEYS" | tk="LAST" | tk="LEADING" | tk="LINK" | tk="LOCAL" | tk="LOCKED" | tk="LOG" | tk="LONGTEXT" | tk="LOOP" | tk="LOW_PRIORITY" | tk="MATCH" | tk="MATCHED" | tk="MATERIALIZED" | tk="MAX" | tk="MAXVALUE" | tk="MEDIUMTEXT" | tk="MEMBER" | tk="MERGE" | tk="MIN" | tk="MINVALUE" | tk="MODIFY" | tk="MOVEMENT" | tk="NEXT" | tk="NO" | tk="NOCACHE" | tk="NOKEEP" | tk="NOLOCK" | tk="NOMAXVALUE" | tk="NOMINVALUE" | tk="NOORDER" | tk="NOTHING" | tk="NOTNULL" | tk="NOVALIDATE" | tk="NOWAIT" | tk="NULLS" | tk="OF" | tk="OFF" | tk="OPEN" | tk="OVER" | tk="OVERLAPS" | tk="PARALLEL" | tk="PARENT" | tk="PARTITION" | tk="PATH" | tk="PERCENT" | tk="PLACING" | tk="PLAN" | tk="PRECEDING" | tk="PRIMARY" | tk="PRIOR" | tk="PURGE" | tk="QUERY" | tk="QUICK" | tk="QUIESCE" | tk="RANGE" | tk="RAW" | tk="READ" | tk="RECURSIVE" | tk="RECYCLEBIN" | tk="REFERENCES" | tk="REFRESH" | tk="REGEXP" | tk="REGEXP_LIKE" | tk="REGISTER" | tk="REMOTE" | tk="RENAME" | tk="REPEATABLE" | tk="REPLACE" | tk="RESET" | tk="RESPECT" | tk="RESTART" | tk="RESTRICT" | tk="RESTRICTED" | tk="RESUMABLE" | tk="RESUME" | tk="RETURN" | tk="RLIKE" | tk="ROLLBACK" | tk="ROLLUP" | tk="ROOT" | tk="ROW" | tk="ROWS" | tk="RR" | tk="RS" | tk="SAFE_CAST" | tk="SAVEPOINT" | tk="SCHEMA" | tk="SECURE" | tk="SEED" | tk="SEPARATOR" | tk="SEQUENCE" | tk="SESSION" | tk="SETS" | tk="SHARE" | tk="SHOW" | tk="SHUTDOWN" | tk="SIBLINGS" | tk="SIGNED" | tk="SIMILAR" | tk="SIZE" | tk="SKIP" | tk="STORED" | tk="STRING" | tk="STRUCT" | tk="SUMMARIZE" | tk="SUSPEND" | tk="SWITCH" | tk="SYNONYM" | tk="SYSTEM" | tk="TABLE" | tk="TABLESPACE" | tk="TEMP" | tk="TEMPORARY" | tk="TEXT" | tk="THEN" | tk="TIMEOUT" | tk="TIMESTAMPTZ" | tk="TIMEZONE" | tk="TINYTEXT" | tk="TO" | tk="TRIGGER" | tk="TRUE" | tk="TRUNCATE" | tk="TRY_CAST" | tk="TUMBLING" | tk="TYPE" | tk="UNLOGGED" | tk="UNQIESCE" | tk="UNSIGNED" | tk="UPDATE" | tk="UPSERT" | tk="UR" | tk="USER" | tk="VALIDATE" | tk="VERBOSE" | tk="VIEW" | tk="VOLATILE" | tk="WAIT" | tk="WITHIN" | tk="WITHOUT" | tk="WITHOUT_ARRAY_WRAPPER" | tk="WORK" | tk="XML" | tk="XMLAGG" | tk="XMLDATA" | tk="XMLSCHEMA" | tk="XMLTEXT" | tk="XSINIL" | tk="YAML" | tk="YES" | tk="ZONE" )
2021+
| tk="ACTION" | tk="ACTIVE" | tk="ADD" | tk="ADVANCE" | tk="ADVISE" | tk="AGAINST" | tk="ALGORITHM" | tk="ALTER" | tk="ANALYZE" | tk="APPLY" | tk="APPROXIMATE" | tk="ARCHIVE" | tk="ARRAY" | tk="ASC" | tk="AT" | tk="AUTHORIZATION" | tk="AUTO" | tk="BASE64" | tk="BEGIN" | tk="BERNOULLI" | tk="BINARY" | tk="BIT" | tk="BLOCK" | tk="BROWSE" | tk="BUFFERS" | tk="BY" | tk="BYTE" | tk="BYTES" | tk="CACHE" | tk="CALL" | tk="CASCADE" | tk="CASE" | tk="CAST" | tk="CHANGE" | tk="CHANGES" | tk="CHAR" | tk="CHARACTER" | tk="CHECKPOINT" | tk="CLOSE" | tk="COLLATE" | tk="COLUMN" | tk="COLUMNS" | tk="COMMENT" | tk="COMMIT" | tk="CONCURRENTLY" | tk="CONFLICT" | tk="CONSTRAINTS" | tk="CONVERT" | tk="COSTS" | tk="COUNT" | tk="CS" | tk="CYCLE" | tk="DATA" | tk="DATABASE" | tk="DATETIME" | tk="DBA_RECYCLEBIN" | tk="DDL" | tk="DECLARE" | tk="DEFERRABLE" | tk="DELAYED" | tk="DELETE" | tk="DESC" | tk="DESCRIBE" | tk="DISABLE" | tk="DISCONNECT" | tk="DIV" | tk="DML" | tk="DO" | tk="DOMAIN" | tk="DROP" | tk="DUMP" | tk="DUPLICATE" | tk="ELEMENTS" | tk="EMIT" | tk="ENABLE" | tk="END" | tk="ERROR" | tk="ESCAPE" | tk="EXCLUDE" | tk="EXEC" | tk="EXECUTE" | tk="EXPLAIN" | tk="EXPLICIT" | tk="EXTENDED" | tk="EXTRACT" | tk="FALSE" | tk="FILTER" | tk="FIRST" | tk="FLUSH" | tk="FN" | tk="FOLLOWING" | tk="FORMAT" | tk="FULLTEXT" | tk="FUNCTION" | tk="GRANT" | tk="GROUP_CONCAT" | tk="GUARD" | tk="HASH" | tk="HIGH_PRIORITY" | tk="HISTORY" | tk="HOPPING" | tk="INCLUDE" | tk="INCLUDE_NULL_VALUES" | tk="INCREMENT" | tk="INDEX" | tk="INSERT" | tk="INTERLEAVE" | tk="INTERPRET" | tk="INVALIDATE" | tk="ISNULL" | tk="JSON" | tk="JSON_ARRAY" | tk="JSON_ARRAYAGG" | tk="JSON_OBJECT" | tk="JSON_OBJECTAGG" | tk="KEEP" | tk="KEY" | tk="KEYS" | tk="LAST" | tk="LEADING" | tk="LINK" | tk="LOCAL" | tk="LOCKED" | tk="LOG" | tk="LONGTEXT" | tk="LOOP" | tk="LOW_PRIORITY" | tk="MATCH" | tk="MATCHED" | tk="MATERIALIZED" | tk="MAX" | tk="MAXVALUE" | tk="MEDIUMTEXT" | tk="MEMBER" | tk="MERGE" | tk="MIN" | tk="MINVALUE" | tk="MODIFY" | tk="MOVEMENT" | tk="NEXT" | tk="NO" | tk="NOCACHE" | tk="NOKEEP" | tk="NOLOCK" | tk="NOMAXVALUE" | tk="NOMINVALUE" | tk="NOORDER" | tk="NOTHING" | tk="NOTNULL" | tk="NOVALIDATE" | tk="NOWAIT" | tk="NULLS" | tk="OF" | tk="OFF" | tk="OPEN" | tk="OVER" | tk="OVERFLOW" | tk="OVERLAPS" | tk="PARALLEL" | tk="PARENT" | tk="PARTITION" | tk="PATH" | tk="PERCENT" | tk="PLACING" | tk="PLAN" | tk="PRECEDING" | tk="PRIMARY" | tk="PRIOR" | tk="PURGE" | tk="QUERY" | tk="QUICK" | tk="QUIESCE" | tk="RANGE" | tk="RAW" | tk="READ" | tk="RECURSIVE" | tk="RECYCLEBIN" | tk="REFERENCES" | tk="REFRESH" | tk="REGEXP" | tk="REGEXP_LIKE" | tk="REGISTER" | tk="REMOTE" | tk="RENAME" | tk="REPEATABLE" | tk="REPLACE" | tk="RESET" | tk="RESPECT" | tk="RESTART" | tk="RESTRICT" | tk="RESTRICTED" | tk="RESUMABLE" | tk="RESUME" | tk="RETURN" | tk="RLIKE" | tk="ROLLBACK" | tk="ROLLUP" | tk="ROOT" | tk="ROW" | tk="ROWS" | tk="RR" | tk="RS" | tk="SAFE_CAST" | tk="SAVEPOINT" | tk="SCHEMA" | tk="SECURE" | tk="SEED" | tk="SEPARATOR" | tk="SEQUENCE" | tk="SESSION" | tk="SETS" | tk="SHARE" | tk="SHOW" | tk="SHUTDOWN" | tk="SIBLINGS" | tk="SIGNED" | tk="SIMILAR" | tk="SIZE" | tk="SKIP" | tk="STORED" | tk="STRING" | tk="STRUCT" | tk="SUMMARIZE" | tk="SUSPEND" | tk="SWITCH" | tk="SYNONYM" | tk="SYSTEM" | tk="TABLE" | tk="TABLESPACE" | tk="TEMP" | tk="TEMPORARY" | tk="TEXT" | tk="THEN" | tk="TIMEOUT" | tk="TIMESTAMPTZ" | tk="TIMEZONE" | tk="TINYTEXT" | tk="TO" | tk="TRIGGER" | tk="TRUE" | tk="TRUNCATE" | tk="TRY_CAST" | tk="TUMBLING" | tk="TYPE" | tk="UNLOGGED" | tk="UNQIESCE" | tk="UNSIGNED" | tk="UPDATE" | tk="UPSERT" | tk="UR" | tk="USER" | tk="VALIDATE" | tk="VERBOSE" | tk="VIEW" | tk="VOLATILE" | tk="WAIT" | tk="WITHIN" | tk="WITHOUT" | tk="WITHOUT_ARRAY_WRAPPER" | tk="WORK" | tk="XML" | tk="XMLAGG" | tk="XMLDATA" | tk="XMLSCHEMA" | tk="XMLTEXT" | tk="XSINIL" | tk="YAML" | tk="YES" | tk="ZONE" )
20192022
{ return tk.image; }
20202023
}
20212024

@@ -5823,6 +5826,8 @@ Function InternalFunction(boolean escaped):
58235826
Expression attributeExpression = null;
58245827
Column attributeColumn = null;
58255828
List<OrderByElement> orderByList;
5829+
String onOverflowTruncate = null;
5830+
Token overflowToken = null;
58265831
Limit limit;
58275832
}
58285833
{
@@ -5843,7 +5848,18 @@ Function InternalFunction(boolean escaped):
58435848
|
58445849
LOOKAHEAD( AllTableColumns() ) expr=AllTableColumns() { expressionList = new ExpressionList(expr); }
58455850
|
5846-
LOOKAHEAD(3) expressionList=ExpressionList() [ orderByList = OrderByElements() { retval.setOrderByElements(orderByList); } ]
5851+
LOOKAHEAD(3) expressionList=ExpressionList()
5852+
[ orderByList = OrderByElements() { retval.setOrderByElements(orderByList); } ]
5853+
5854+
// https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/LISTAGG.html
5855+
[
5856+
<K_ON> <K_OVERFLOW> ( overflowToken=<K_TRUNCATE> | overflowToken=<K_ERROR> ) { onOverflowTruncate=overflowToken.image; }
5857+
[
5858+
overflowToken = <S_CHAR_LITERAL> { onOverflowTruncate+= " " + overflowToken.image; }
5859+
[ ( overflowToken=<K_WITH> | overflowToken=<K_WITHOUT> ) <K_COUNT> { onOverflowTruncate+=" " + overflowToken.image + " COUNT"; }]
5860+
]
5861+
] { retval.setOnOverflowTruncate(onOverflowTruncate); }
5862+
58475863
|
58485864
LOOKAHEAD({ !getAsBoolean(Feature.allowUnparenthesizedSubSelects) }) expr = Select() { expressionList = new ExpressionList(expr); }
58495865
)

src/test/java/net/sf/jsqlparser/expression/FunctionTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import net.sf.jsqlparser.test.TestUtils;
1414
import org.junit.jupiter.api.Disabled;
1515
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.params.ParameterizedTest;
17+
import org.junit.jupiter.params.provider.ValueSource;
1618

1719
class FunctionTest {
1820
@Test
@@ -90,4 +92,20 @@ void testSimpleFunctionIssue2059() throws JSQLParserException {
9092
parser.withAllowComplexParsing(false);
9193
});
9294
}
95+
96+
@ParameterizedTest
97+
@ValueSource(strings = {
98+
"select LISTAGG(field, ',' on overflow truncate '...') from dual",
99+
"select LISTAGG(field, ',' on overflow truncate '...' with count) from dual",
100+
"select LISTAGG(field, ',' on overflow truncate '...' without count) from dual",
101+
"select LISTAGG(field, ',' on overflow error) from dual", "SELECT department, \n" +
102+
" LISTAGG(name, ', ' ON OVERFLOW TRUNCATE '... (truncated)' WITH COUNT) WITHIN GROUP (ORDER BY name)\n"
103+
+
104+
" AS employee_names\n" +
105+
"FROM employees\n" +
106+
"GROUP BY department;"
107+
})
108+
void testListAggOnOverflow(String sqlStr) throws Exception {
109+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
110+
}
93111
}

0 commit comments

Comments
 (0)