From efd74633cb2b4bf96d6c844d39d9cac14d73a9b4 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 23 Sep 2021 13:57:46 +0200 Subject: [PATCH] [jOOQ/jOOQ#12465] Move PRODUCT to API generator --- jOOQ/src/main/java/org/jooq/impl/DSL.java | 80 ++++++++++--------- jOOQ/src/main/java/org/jooq/impl/Product.java | 68 ++++++++++------ 2 files changed, 86 insertions(+), 62 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index dc32ad0b6c..8425a99d9d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -19955,6 +19955,48 @@ public class DSL { return new Min(field, true); } + /** + * The PRODUCT function. + *

+ * Get the sum over a numeric field: product(distinct field). + *

+ * Few dialects currently support multiplicative aggregation natively. jOOQ + * emulates this using exp(sum(log(arg))) for strictly positive + * numbers, and does some additional handling for zero and negative numbers. + *

+ * Note that this implementation may introduce rounding errors, even for + * integer multiplication. + *

+ * More information here: https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql. + */ + @NotNull + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, YUGABYTE }) + public static AggregateFunction product(Field field) { + return new Product(field, false); + } + + /** + * The PRODUCT_DISTINCT function. + *

+ * Get the sum over a numeric field: product(distinct field). + *

+ * Few dialects currently support multiplicative aggregation natively. jOOQ + * emulates this using exp(sum(log(arg))) for strictly positive + * numbers, and does some additional handling for zero and negative numbers. + *

+ * Note that this implementation may introduce rounding errors, even for + * integer multiplication. + *

+ * More information here: https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql. + */ + @NotNull + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, YUGABYTE }) + public static AggregateFunction productDistinct(Field field) { + return new Product(field, true); + } + /** * The REGR_AVGX function. *

@@ -25066,44 +25108,6 @@ public class DSL { - - /** - * Get the product over a numeric field: product(field). - *

- * No database currently supports multiplicative aggregation natively. jOOQ - * emulates this using exp(sum(log(arg))) for strictly positive - * numbers, and does some additional handling for zero and negative numbers. - *

- * Note that this implementation may introduce rounding errors, even for - * integer multiplication. - *

- * More information here: https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql. - */ - @NotNull - @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, YUGABYTE }) - public static AggregateFunction product(Field field) { - return new Product(false, Tools.nullSafe(field)); - } - - /** - * Get the sum over a numeric field: product(distinct field). - *

- * No database currently supports multiplicative aggregation natively. jOOQ - * emulates this using exp(sum(log(arg))) for strictly positive - * numbers, and does some additional handling for zero and negative numbers. - *

- * Note that this implementation may introduce rounding errors, even for - * integer multiplication. - *

- * More information here: https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql. - */ - @NotNull - @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, YUGABYTE }) - public static AggregateFunction productDistinct(Field field) { - return new Product(true, Tools.nullSafe(field)); - } /** * The mode(field) aggregate function. diff --git a/jOOQ/src/main/java/org/jooq/impl/Product.java b/jOOQ/src/main/java/org/jooq/impl/Product.java index 17c14d6ffd..43d497d0c3 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Product.java +++ b/jOOQ/src/main/java/org/jooq/impl/Product.java @@ -37,38 +37,56 @@ */ package org.jooq.impl; -// ... -// ... -// ... -import static org.jooq.impl.DSL.aggregate; -import static org.jooq.impl.DSL.aggregateDistinct; -import static org.jooq.impl.DSL.choose; -import static org.jooq.impl.DSL.inline; -import static org.jooq.impl.DSL.one; -import static org.jooq.impl.DSL.when; -import static org.jooq.impl.DSL.zero; -import static org.jooq.impl.Internal.imul; -import static org.jooq.impl.Names.N_MUL; -import static org.jooq.impl.Names.N_PRODUCT; -import static org.jooq.impl.SQLDataType.NUMERIC; -import static org.jooq.impl.Tools.EMPTY_FIELD; +import static org.jooq.impl.DSL.*; +import static org.jooq.impl.Internal.*; +import static org.jooq.impl.Keywords.*; +import static org.jooq.impl.Names.*; +import static org.jooq.impl.SQLDataType.*; +import static org.jooq.impl.Tools.*; +import static org.jooq.impl.Tools.BooleanDataKey.*; +import static org.jooq.impl.Tools.DataExtendedKey.*; +import static org.jooq.impl.Tools.DataKey.*; +import static org.jooq.SQLDialect.*; +import org.jooq.*; +import org.jooq.Record; +import org.jooq.conf.*; +import org.jooq.impl.*; +import org.jooq.tools.*; + +import java.util.*; +import java.util.function.*; +import java.util.stream.*; import java.math.BigDecimal; -import org.jooq.Context; -import org.jooq.Field; -import org.jooq.Name; -// ... /** - * @author Lukas Eder + * The PRODUCT statement. */ -final class Product extends AbstractAggregateFunction { +@SuppressWarnings({ "rawtypes", "unused" }) +final class Product +extends + AbstractAggregateFunction +{ - Product(boolean distinct, Field... arguments) { - super(distinct, N_PRODUCT, NUMERIC, arguments); + Product( + Field field, + boolean productDistinct + ) { + super( + productDistinct, + N_PRODUCT, + NUMERIC, + nullSafeNotNull(field, INTEGER) + ); } + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + + + @Override public final void accept(Context ctx) { switch (ctx.family()) { @@ -105,7 +123,7 @@ final class Product extends AbstractAggregateFunction { private final void acceptEmulation(Context ctx) { - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({ "unchecked" }) final Field f = (Field) DSL.field("{0}", arguments.get(0).getDataType(), arguments.get(0)); final Field negatives = DSL.when(f.lt(zero()), inline(-1)); @@ -150,4 +168,6 @@ final class Product extends AbstractAggregateFunction { DSL.exp(logarithmsSum) )); } + + }