[jOOQ/jOOQ#12742] ListAgg not working with filter clause

This includes:

- [jOOQ/jOOQ#8543] Support FILTER for GROUP_CONCAT
This commit is contained in:
Lukas Eder 2021-12-15 17:14:18 +01:00
parent 5505d03f2c
commit 528e53b4f7
7 changed files with 41 additions and 6 deletions

View File

@ -283,7 +283,11 @@ implements
else
ctx.visit(wrap(args).map(arg -> DSL.when(filter, arg == ASTERISK ? one() : arg)).map(fun));
ctx.visit(wrap(args).map((arg, i) -> applyFilter(arg, i) ? DSL.when(filter, arg == ASTERISK ? one() : arg) : arg).map(fun));
}
boolean applyFilter(Field<?> arg, int i) {
return true;
}

View File

@ -37,6 +37,7 @@
*/
package org.jooq.impl;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
@ -142,6 +143,10 @@ interface BooleanConsumer {
@FunctionalInterface
interface ObjIntFunction<T, R> {
R apply(T t, int i);
default <V> ObjIntFunction<T, V> andThen(ObjIntFunction<? super R, ? extends V> after) {
return (t, i) -> after.apply(apply(t, i), i);
}
}
/**

View File

@ -85,6 +85,9 @@ implements
else
result = new ListAgg(distinct, field, inline(separator));
if (filter != null)
result.filterWhere(filter);

View File

@ -155,6 +155,11 @@ final class ListAgg extends AbstractAggregateFunction<String> implements UNotYet
super.acceptFunctionName(ctx);
}
@Override
boolean applyFilter(Field<?> arg, int i) {
return i == 0;
}
/**
* [#1273] <code>LIST_AGG</code> emulation for MySQL
*/
@ -171,6 +176,9 @@ final class ListAgg extends AbstractAggregateFunction<String> implements UNotYet
.visit(arguments.get(1));
ctx.sql(')');
acceptFilterClause(ctx);
acceptOverClause(ctx);
}
/**

View File

@ -70,10 +70,10 @@ import org.jooq.impl.QOM.MCollection;
*/
class QueryPartCollectionView<T extends QueryPart> extends AbstractQueryPart implements MCollection<T>, SimpleQueryPart, SeparatedQueryPart {
final Collection<T> wrapped;
Boolean qualify;
String separator;
Function<? super T, ? extends T> mapper;
final Collection<T> wrapped;
Boolean qualify;
String separator;
ObjIntFunction<? super T, ? extends T> mapper;
static final <T extends QueryPart> QueryPartCollectionView<T> wrap(Collection<T> wrapped) {
return new QueryPartCollectionView<>(wrapped);
@ -97,6 +97,10 @@ class QueryPartCollectionView<T extends QueryPart> extends AbstractQueryPart imp
}
QueryPartCollectionView<T> map(Function<? super T, ? extends T> newMapper) {
return map((t, i) -> newMapper.apply(t));
}
QueryPartCollectionView<T> map(ObjIntFunction<? super T, ? extends T> newMapper) {
this.mapper = mapper == null ? newMapper : mapper.andThen(newMapper);
return this;
}
@ -169,11 +173,12 @@ class QueryPartCollectionView<T extends QueryPart> extends AbstractQueryPart imp
for (T part : this) {
try {
int j0 = j;
if (!rendersContent.get(j++))
continue;
if (mapper != null)
part = mapper.apply(part);
part = mapper.apply(part, j0);
if (k++ > 0) {

View File

@ -76,6 +76,11 @@ class QueryPartList<T extends QueryPart> extends QueryPartListView<T> {
return (QueryPartList<T>) super.map(newMapper);
}
@Override
QueryPartList<T> map(ObjIntFunction<? super T, ? extends T> newMapper) {
return (QueryPartList<T>) super.map(newMapper);
}
@Override
QueryPartList<T> separator(String newSeparator) {
return (QueryPartList<T>) super.separator(newSeparator);

View File

@ -85,6 +85,11 @@ class QueryPartListView<T extends QueryPart> extends QueryPartCollectionView<T>
return (QueryPartListView<T>) super.map(newMapper);
}
@Override
QueryPartListView<T> map(ObjIntFunction<? super T, ? extends T> newMapper) {
return (QueryPartListView<T>) super.map(newMapper);
}
@Override
QueryPartListView<T> separator(String newSeparator) {
return (QueryPartListView<T>) super.separator(newSeparator);