diff --git a/jOOQ/src/main/java/org/jooq/impl/BinaryListAgg.java b/jOOQ/src/main/java/org/jooq/impl/BinaryListAgg.java new file mode 100644 index 0000000000..162aa709a8 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/BinaryListAgg.java @@ -0,0 +1,140 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * Apache-2.0 license and offer limited warranties, support, maintenance, and + * commercial database integrations. + * + * For more information, please visit: https://www.jooq.org/legal/licensing + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + +// ... +// ... +// ... +import static org.jooq.SQLDialect.DUCKDB; +// ... +// ... +import static org.jooq.SQLDialect.POSTGRES; +// ... +// ... +import static org.jooq.impl.DSL.inline; +import static org.jooq.impl.Names.N_LISTAGG; +import static org.jooq.impl.Names.N_STRING_AGG; +import static org.jooq.impl.QueryPartListView.wrap; +import static org.jooq.impl.SQLDataType.VARBINARY; +import static org.jooq.impl.SQLDataType.VARCHAR; +import static org.jooq.impl.Tools.castIfNeeded; + +import java.util.Set; + +import org.jooq.Context; +import org.jooq.Field; +import org.jooq.SQLDialect; +import org.jooq.impl.QOM.UNotYetImplemented; + +/** + * @author Lukas Eder + */ +final class BinaryListAgg +extends + AbstractAggregateFunction +implements + UNotYetImplemented +{ + + static final Set SUPPORT_STRING_AGG = SQLDialect.supportedBy(DUCKDB, POSTGRES); + static final Field DEFAULT_SEPARATOR = DSL.inline(",".getBytes()); + + BinaryListAgg(boolean distinct, Field arg) { + super(distinct, N_LISTAGG, VARBINARY, arg); + } + + BinaryListAgg(boolean distinct, Field arg, Field separator) { + super(distinct, N_LISTAGG, VARBINARY, arg, separator); + } + + // ------------------------------------------------------------------------- + // XXX QueryPart API + // ------------------------------------------------------------------------- + + @Override + public final void accept(Context ctx) { + if (SUPPORT_STRING_AGG.contains(ctx.dialect())) { + acceptStringAgg(ctx); + acceptFilterClause(ctx); + acceptOverClause(ctx); + } + else + super.accept(ctx); + } + + @Override + void acceptFunctionName(Context ctx) { + + + + + + super.acceptFunctionName(ctx); + } + + @Override + final boolean applyFilterToArgument(Context ctx, Field arg, int i) { + return i == 0; + } + + /** + * [#1275] LIST_AGG emulation for Postgres, Sybase + */ + private final void acceptStringAgg(Context ctx) { + ctx.visit(N_STRING_AGG); + ctx.sql('('); + + // The explicit cast is needed in Postgres + QueryPartListView> args = wrap( + arguments.get(0).getDataType().isBinary() + ? arguments.get(0) + : arguments.get(0).getDataType().isString() + ? arguments.get(0).cast(VARBINARY) + : arguments.get(0).cast(VARCHAR).cast(VARBINARY) + ); + acceptArguments1(ctx, args); + + if (arguments.size() > 1) + ctx.sql(", ").visit(arguments.get(1)); + else + ctx.sql(", ").visit(inline("".getBytes())); + + acceptOrderBy(ctx); + ctx.sql(')'); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index a2124044be..bbff2085bc 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -98,6 +98,8 @@ import static org.jooq.impl.DSL.avgDistinct; import static org.jooq.impl.DSL.begin; import static org.jooq.impl.DSL.binaryBitLength; import static org.jooq.impl.DSL.binaryLength; +import static org.jooq.impl.DSL.binaryListAgg; +import static org.jooq.impl.DSL.binaryListAggDistinct; import static org.jooq.impl.DSL.binaryLtrim; import static org.jooq.impl.DSL.binaryMd5; import static org.jooq.impl.DSL.binaryOctetLength; @@ -13056,8 +13058,8 @@ final class DefaultParseContext extends AbstractScope implements ParseContext { : new InverseDistributionFunction(null, f.apply(f2).withinGroupOrderBy(f1)); } - private final OrderedAggregateFunction parseListaggFunctionIf() { - OrderedAggregateFunction ordered; + private final OrderedAggregateFunction parseListaggFunctionIf() { + OrderedAggregateFunction ordered; if (parseFunctionNameIf("LISTAGG", "STRING_AGG")) { parse('('); @@ -13065,9 +13067,21 @@ final class DefaultParseContext extends AbstractScope implements ParseContext { Field field = parseField(); if (parseIf(',')) - ordered = distinct ? listAggDistinct(field, (Field) parseField()) : listAgg(field, (Field) parseField()); + ordered = distinct + ? binary(field) + ? binaryListAggDistinct(field, (Field) parseField()) + : listAggDistinct(field, (Field) parseField()) + : binary(field) + ? binaryListAgg(field, (Field) parseField()) + : listAgg(field, (Field) parseField()); else - ordered = distinct ? listAggDistinct(field) : listAgg(field); + ordered = distinct + ? binary(field) + ? binaryListAggDistinct(field) + : listAggDistinct(field) + : binary(field) + ? binaryListAgg(field) + : listAgg(field); if (parseKeywordIf("ORDER BY")) ordered.withinGroupOrderBy(parseList(',', c -> c.parseSortField()));