Skip to content

Commit

Permalink
[#11061] [#11070] [#11091] REGR_XYZ functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Dec 15, 2020
1 parent dc0fbbd commit 1428a0b
Show file tree
Hide file tree
Showing 12 changed files with 922 additions and 171 deletions.
243 changes: 90 additions & 153 deletions jOOQ/src/main/java/org/jooq/impl/DSL.java
Expand Up @@ -17026,6 +17026,96 @@ public static Field<XML> xmlforest(Collection<? extends Field<?>> fields) {
return new Xmlforest(fields);
}

/**
* The <code>MEDIAN</code> function.
*/
@NotNull
@Support({ CUBRID, H2, HSQLDB, MARIADB, POSTGRES })
public static AggregateFunction<BigDecimal> median(Field<? extends Number> field) {
return new Median(field);
}

/**
* The <code>REGR_AVG_X</code> function.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrAvgX(Field<? extends Number> y, Field<? extends Number> x) {
return new RegrAvgx(y, x);
}

/**
* The <code>REGR_AVG_Y</code> function.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrAvgY(Field<? extends Number> y, Field<? extends Number> x) {
return new RegrAvgy(y, x);
}

/**
* The <code>REGR_COUNT</code> function.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrCount(Field<? extends Number> y, Field<? extends Number> x) {
return new RegrCount(y, x);
}

/**
* The <code>REGR_INTERCEPT</code> function.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrIntercept(Field<? extends Number> y, Field<? extends Number> x) {
return new RegrIntercept(y, x);
}

/**
* The <code>REGR_R2</code> function.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrR2(Field<? extends Number> y, Field<? extends Number> x) {
return new RegrR2(y, x);
}

/**
* The <code>REGR_SLOPE</code> function.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrSlope(Field<? extends Number> y, Field<? extends Number> x) {
return new RegrSlope(y, x);
}

/**
* The <code>REGR_S_X_X</code> function.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrSXX(Field<? extends Number> y, Field<? extends Number> x) {
return new RegrSxx(y, x);
}

/**
* The <code>REGR_S_X_Y</code> function.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrSXY(Field<? extends Number> y, Field<? extends Number> x) {
return new RegrSxy(y, x);
}

/**
* The <code>REGR_S_Y_Y</code> function.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrSYY(Field<? extends Number> y, Field<? extends Number> x) {
return new RegrSyy(y, x);
}

/**
* The <code>STDDEV_POP</code> function.
*/
Expand Down Expand Up @@ -21902,159 +21992,6 @@ public static <T> AggregateFunction<T> mode(Field<T> field) {
return new Mode(Tools.nullSafe(field));
}

/**
* Get the median over a numeric field: median(field).
*/
@NotNull
@Support({ CUBRID, H2, HSQLDB, MARIADB, POSTGRES })
public static AggregateFunction<BigDecimal> median(Field<? extends Number> field) {
return new Median(Tools.nullSafe(field));
}

/**
* Get the <code>REGR_SLOPE</code> linear regression function.
* <p>
* The linear regression functions fit an ordinary-least-squares regression
* line to a set of number pairs. You can use them as both aggregate and
* window functions, where this is supported.
* <p>
* Note that {@link SQLDialect#DB2} does not support linear regression
* window functions.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrSlope(Field<? extends Number> y, Field<? extends Number> x) {
return new DefaultAggregateFunction<>("regr_slope", SQLDataType.NUMERIC, Tools.nullSafe(y), Tools.nullSafe(x));
}

/**
* Get the <code>REGR_INTERCEPT</code> linear regression function.
* <p>
* The linear regression functions fit an ordinary-least-squares regression
* line to a set of number pairs. You can use them as both aggregate and
* window functions, where this is supported.
* <p>
* Note that {@link SQLDialect#DB2} does not support linear regression
* window functions.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrIntercept(Field<? extends Number> y, Field<? extends Number> x) {
return new DefaultAggregateFunction<>("regr_intercept", SQLDataType.NUMERIC, Tools.nullSafe(y), Tools.nullSafe(x));
}

/**
* Get the <code>REGR_COUNT</code> linear regression function.
* <p>
* The linear regression functions fit an ordinary-least-squares regression
* line to a set of number pairs. You can use them as both aggregate and
* window functions, where this is supported.
* <p>
* Note that {@link SQLDialect#DB2} does not support linear regression
* window functions.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrCount(Field<? extends Number> y, Field<? extends Number> x) {
return new DefaultAggregateFunction<>("regr_count", SQLDataType.NUMERIC, Tools.nullSafe(y), Tools.nullSafe(x));
}

/**
* Get the <code>REGR_R2</code> linear regression function.
* <p>
* The linear regression functions fit an ordinary-least-squares regression
* line to a set of number pairs. You can use them as both aggregate and
* window functions, where this is supported.
* <p>
* Note that {@link SQLDialect#DB2} does not support linear regression
* window functions.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrR2(Field<? extends Number> y, Field<? extends Number> x) {
return new DefaultAggregateFunction<>("regr_r2", SQLDataType.NUMERIC, Tools.nullSafe(y), Tools.nullSafe(x));
}

/**
* Get the <code>REGR_AVGX</code> linear regression function.
* <p>
* The linear regression functions fit an ordinary-least-squares regression
* line to a set of number pairs. You can use them as both aggregate and
* window functions, where this is supported.
* <p>
* Note that {@link SQLDialect#DB2} does not support linear regression
* window functions.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrAvgX(Field<? extends Number> y, Field<? extends Number> x) {
return new DefaultAggregateFunction<>("regr_avgx", SQLDataType.NUMERIC, Tools.nullSafe(y), Tools.nullSafe(x));
}

/**
* Get the <code>REGR_AVGY</code> linear regression function.
* <p>
* The linear regression functions fit an ordinary-least-squares regression
* line to a set of number pairs. You can use them as both aggregate and
* window functions, where this is supported.
* <p>
* Note that {@link SQLDialect#DB2} does not support linear regression
* window functions.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrAvgY(Field<? extends Number> y, Field<? extends Number> x) {
return new DefaultAggregateFunction<>("regr_avgy", SQLDataType.NUMERIC, Tools.nullSafe(y), Tools.nullSafe(x));
}

/**
* Get the <code>REGR_SXX</code> linear regression function.
* <p>
* The linear regression functions fit an ordinary-least-squares regression
* line to a set of number pairs. You can use them as both aggregate and
* window functions, where this is supported.
* <p>
* Note that {@link SQLDialect#DB2} does not support linear regression
* window functions.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrSXX(Field<? extends Number> y, Field<? extends Number> x) {
return new DefaultAggregateFunction<>("regr_sxx", SQLDataType.NUMERIC, Tools.nullSafe(y), Tools.nullSafe(x));
}

/**
* Get the <code>REGR_SYY</code> linear regression function.
* <p>
* The linear regression functions fit an ordinary-least-squares regression
* line to a set of number pairs. You can use them as both aggregate and
* window functions, where this is supported.
* <p>
* Note that {@link SQLDialect#DB2} does not support linear regression
* window functions.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrSYY(Field<? extends Number> y, Field<? extends Number> x) {
return new DefaultAggregateFunction<>("regr_syy", SQLDataType.NUMERIC, Tools.nullSafe(y), Tools.nullSafe(x));
}

/**
* Get the <code>REGR_SXY</code> linear regression function.
* <p>
* The linear regression functions fit an ordinary-least-squares regression
* line to a set of number pairs. You can use them as both aggregate and
* window functions, where this is supported.
* <p>
* Note that {@link SQLDialect#DB2} does not support linear regression
* window functions.
*/
@NotNull
@Support({ POSTGRES })
public static AggregateFunction<BigDecimal> regrSXY(Field<? extends Number> y, Field<? extends Number> x) {
return new DefaultAggregateFunction<>("regr_sxy", SQLDataType.NUMERIC, Tools.nullSafe(y), Tools.nullSafe(x));
}

/**
* Get the aggregated concatenation for a field.
* <p>
Expand Down
57 changes: 39 additions & 18 deletions jOOQ/src/main/java/org/jooq/impl/Median.java
Expand Up @@ -37,39 +37,60 @@
*/
package org.jooq.impl;

// ...
import static org.jooq.SQLDialect.POSTGRES;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.percentileCont;
import static org.jooq.impl.Names.N_MEDIAN;
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.SQLDialect.*;

import org.jooq.*;
import org.jooq.impl.*;
import org.jooq.tools.*;

import java.util.*;
import java.math.BigDecimal;
import java.util.Set;

import org.jooq.Context;
import org.jooq.Field;
import org.jooq.SQLDialect;

/**
* @author Lukas Eder
* The <code>MEDIAN</code> statement.
*/
final class Median extends DefaultAggregateFunction<BigDecimal> {
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
final class Median
extends
DefaultAggregateFunction<BigDecimal>
{

/**
* Generated UID
*/
private static final long serialVersionUID = -7378732863724089028L;
private static final Set<SQLDialect> EMULATE_WITH_PERCENTILES = SQLDialect.supportedBy(POSTGRES);
private static final long serialVersionUID = 1L;

Median(Field<? extends Number> arg) {
super(false, N_MEDIAN, SQLDataType.NUMERIC, arg);
Median(
Field<? extends Number> field
) {
super(
false,
N_MEDIAN,
NUMERIC,
nullSafeNotNull(field, INTEGER)
);
}

// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------



private static final Set<SQLDialect> EMULATE_WITH_PERCENTILES = SQLDialect.supportedBy(POSTGRES);

@Override
public final void accept(Context<?> ctx) {
if (EMULATE_WITH_PERCENTILES.contains(ctx.dialect()))
ctx.visit(percentileCont(inline(new BigDecimal("0.5"))).withinGroupOrderBy(arguments));
else
super.accept(ctx);
}


}
10 changes: 10 additions & 0 deletions jOOQ/src/main/java/org/jooq/impl/Names.java
Expand Up @@ -209,6 +209,16 @@ final class Names {
static final Name N_RAWTOHEX = unquotedName("rawtohex");
static final Name N_REGEXP_REPLACE = unquotedName("regexp_replace");
static final Name N_REGEX_REPLACE = unquotedName("regex_replace");

static final Name N_REGR_AVGX = unquotedName("regr_avgx");
static final Name N_REGR_AVGY = unquotedName("regr_avgy");
static final Name N_REGR_COUNT = unquotedName("regr_count");
static final Name N_REGR_INTERCEPT = unquotedName("regr_intercept");
static final Name N_REGR_R2 = unquotedName("regr_r2");
static final Name N_REGR_SLOPE = unquotedName("regr_slope");
static final Name N_REGR_SXX = unquotedName("regr_sxx");
static final Name N_REGR_SXY = unquotedName("regr_sxy");
static final Name N_REGR_SYY = unquotedName("regr_syy");
static final Name N_REPEAT = unquotedName("repeat");
static final Name N_REPLACE = unquotedName("replace");
static final Name N_REPLICATE = unquotedName("replicate");
Expand Down

0 comments on commit 1428a0b

Please sign in to comment.