[jOOQ/jOOQ#12465] Move PRODUCT to API generator

This commit is contained in:
Lukas Eder 2021-09-23 13:57:46 +02:00
parent fa286e821f
commit efd74633cb
2 changed files with 86 additions and 62 deletions

View File

@ -19955,6 +19955,48 @@ public class DSL {
return new Min(field, true);
}
/**
* The <code>PRODUCT</code> function.
* <p>
* Get the sum over a numeric field: product(distinct field).
* <p>
* Few dialects currently support multiplicative aggregation natively. jOOQ
* emulates this using <code>exp(sum(log(arg)))</code> for strictly positive
* numbers, and does some additional handling for zero and negative numbers.
* <p>
* Note that this implementation may introduce rounding errors, even for
* integer multiplication.
* <p>
* More information here: <a href=
* "https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql">https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql</a>.
*/
@NotNull
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, YUGABYTE })
public static AggregateFunction<BigDecimal> product(Field<? extends Number> field) {
return new Product(field, false);
}
/**
* The <code>PRODUCT_DISTINCT</code> function.
* <p>
* Get the sum over a numeric field: product(distinct field).
* <p>
* Few dialects currently support multiplicative aggregation natively. jOOQ
* emulates this using <code>exp(sum(log(arg)))</code> for strictly positive
* numbers, and does some additional handling for zero and negative numbers.
* <p>
* Note that this implementation may introduce rounding errors, even for
* integer multiplication.
* <p>
* More information here: <a href=
* "https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql">https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql</a>.
*/
@NotNull
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, YUGABYTE })
public static AggregateFunction<BigDecimal> productDistinct(Field<? extends Number> field) {
return new Product(field, true);
}
/**
* The <code>REGR_AVGX</code> function.
* <p>
@ -25066,44 +25108,6 @@ public class DSL {
/**
* Get the product over a numeric field: product(field).
* <p>
* No database currently supports multiplicative aggregation natively. jOOQ
* emulates this using <code>exp(sum(log(arg)))</code> for strictly positive
* numbers, and does some additional handling for zero and negative numbers.
* <p>
* Note that this implementation may introduce rounding errors, even for
* integer multiplication.
* <p>
* More information here: <a href=
* "https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql">https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql</a>.
*/
@NotNull
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, YUGABYTE })
public static AggregateFunction<BigDecimal> product(Field<? extends Number> field) {
return new Product(false, Tools.nullSafe(field));
}
/**
* Get the sum over a numeric field: product(distinct field).
* <p>
* No database currently supports multiplicative aggregation natively. jOOQ
* emulates this using <code>exp(sum(log(arg)))</code> for strictly positive
* numbers, and does some additional handling for zero and negative numbers.
* <p>
* Note that this implementation may introduce rounding errors, even for
* integer multiplication.
* <p>
* More information here: <a href=
* "https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql">https://blog.jooq.org/2018/09/21/how-to-write-a-multiplication-aggregate-function-in-sql</a>.
*/
@NotNull
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, YUGABYTE })
public static AggregateFunction<BigDecimal> productDistinct(Field<? extends Number> field) {
return new Product(true, Tools.nullSafe(field));
}
/**
* The <code>mode(field)</code> aggregate function.

View File

@ -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 <code>PRODUCT</code> statement.
*/
final class Product extends AbstractAggregateFunction<BigDecimal> {
@SuppressWarnings({ "rawtypes", "unused" })
final class Product
extends
AbstractAggregateFunction<BigDecimal>
{
Product(boolean distinct, Field<?>... arguments) {
super(distinct, N_PRODUCT, NUMERIC, arguments);
Product(
Field<? extends Number> 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<BigDecimal> {
private final void acceptEmulation(Context<?> ctx) {
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "unchecked" })
final Field<Integer> f = (Field) DSL.field("{0}", arguments.get(0).getDataType(), arguments.get(0));
final Field<Integer> negatives = DSL.when(f.lt(zero()), inline(-1));
@ -150,4 +168,6 @@ final class Product extends AbstractAggregateFunction<BigDecimal> {
DSL.exp(logarithmsSum)
));
}
}