Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,8 @@ primaryExpression
| ROW '(' expression (',' expression)* ')' #rowConstructor
| name=LISTAGG '(' setQuantifier? expression (',' string)?
(ON OVERFLOW listAggOverflowBehavior)? ')'
(WITHIN GROUP '(' ORDER BY sortItem (',' sortItem)* ')') #listagg
(WITHIN GROUP '(' ORDER BY sortItem (',' sortItem)* ')')
filter? #listagg
| processingMode? qualifiedName '(' (label=identifier '.')? ASTERISK ')'
filter? over? #functionCall
| processingMode? qualifiedName '(' (setQuantifier? expression (',' expression)*)?
Expand Down
13 changes: 13 additions & 0 deletions core/trino-main/src/test/java/io/trino/sql/query/TestListagg.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,17 @@ public void testListaggQueryGroupingOverflowTruncateWithCountAndWithOverflowFill
" (1, VARCHAR '" + largeValue + ",everything,.....(2)')," +
" (2, VARCHAR 'listagg,string joiner')");
}

@Test
void testFilter()
{
assertThat(assertions.query(
"""
SELECT listagg(value, ',') WITHIN GROUP (ORDER BY id) FILTER (WHERE id % 2 = 0)
FROM (
VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')
) t(id, value)
"""))
.matches("VALUES VARCHAR 'b,d'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,10 @@ private String visitListagg(FunctionCall node)
.append(')');
}

if (node.getFilter().isPresent()) {
builder.append(" FILTER ").append(visitFilter(node.getFilter().get(), null));
}

return builder.toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2457,7 +2457,7 @@ else if (listaggCountIndicationContext.WITHOUT() != null) {
Optional.of(getLocation(context)),
QualifiedName.of("LISTAGG"),
window,
Optional.empty(),
visitIfPresent(context.filter(), Expression.class),
Optional.of(orderBy),
distinct,
Optional.empty(),
Expand Down