[jOOQ/jOOQ#9930] Extracted AbstractAggregateFunction

- Extracted Mode
- Removed Term.MODE
This commit is contained in:
Lukas Eder 2020-03-10 15:25:45 +01:00
parent 54c5ff3567
commit 8085600a8c
6 changed files with 99 additions and 38 deletions

View File

@ -18575,7 +18575,7 @@ public class DSL {
*/
@Support({ H2, POSTGRES })
public static <T> AggregateFunction<T> mode(Field<T> field) {
return new DefaultAggregateFunction<>(Term.MODE, nullSafeDataType(field), field);
return new Mode(nullSafe(field));
}
/**
@ -18890,7 +18890,7 @@ public class DSL {
*/
@Support({ H2, POSTGRES })
public static OrderedAggregateFunctionOfDeferredType mode() {
return new Mode();
return new ModeDeferred();
}
/**

View File

@ -38,10 +38,6 @@
package org.jooq.impl;
// ...
import static org.jooq.SQLDialect.H2;
import static org.jooq.SQLDialect.POSTGRES;
import static org.jooq.impl.DSL.mode;
import static org.jooq.impl.Keywords.K_DENSE_RANK;
import static org.jooq.impl.Keywords.K_FIRST;
import static org.jooq.impl.Keywords.K_KEEP;
@ -49,7 +45,6 @@ import static org.jooq.impl.Keywords.K_LAST;
import static org.jooq.impl.Keywords.K_NULL;
import static org.jooq.impl.Keywords.K_ORDER_BY;
import static org.jooq.impl.Keywords.K_WITHIN_GROUP;
import static org.jooq.impl.Term.MODE;
import org.jooq.Context;
import org.jooq.DataType;
@ -107,16 +102,11 @@ class DefaultAggregateFunction<T> extends AbstractAggregateFunction<T> {
@Override
public /* final */ void accept(Context<?> ctx) {
if (term == MODE && ( ctx.family() == H2 || ctx.family() == POSTGRES)) {
ctx.visit(mode().withinGroupOrderBy(DSL.field("{0}", arguments.get(0))));
}
else {
toSQLArguments(ctx);
acceptKeepDenseRankOrderByClause(ctx);
acceptWithinGroupClause(ctx);
acceptFilterClause(ctx);
acceptOverClause(ctx);
}
toSQLArguments(ctx);
acceptKeepDenseRankOrderByClause(ctx);
acceptWithinGroupClause(ctx);
acceptFilterClause(ctx);
acceptOverClause(ctx);
}
/**

View File

@ -39,6 +39,7 @@ 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;
@ -66,13 +67,8 @@ final class Median extends DefaultAggregateFunction<BigDecimal> {
@Override
public final void accept(Context<?> ctx) {
if (EMULATE_WITH_PERCENTILES.contains(ctx.dialect())) {
Field<?>[] fields = new Field[arguments.size()];
for (int i = 0; i < fields.length; i++)
fields[i] = DSL.field("{0}", arguments.get(i));
ctx.visit(percentileCont(new BigDecimal("0.5")).withinGroupOrderBy(fields));
}
if (EMULATE_WITH_PERCENTILES.contains(ctx.dialect()))
ctx.visit(percentileCont(inline(new BigDecimal("0.5"))).withinGroupOrderBy(arguments));
else
super.accept(ctx);
}

View File

@ -37,26 +37,38 @@
*/
package org.jooq.impl;
import org.jooq.AggregateFilterStep;
import org.jooq.DataType;
// ...
import static org.jooq.SQLDialect.H2;
import static org.jooq.SQLDialect.POSTGRES;
import static org.jooq.impl.DSL.mode;
import static org.jooq.impl.Names.N_MODE;
import java.util.Set;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.OrderField;
import org.jooq.OrderedAggregateFunctionOfDeferredType;
import org.jooq.SQLDialect;
/**
* @author Lukas Eder
*/
final class Mode implements OrderedAggregateFunctionOfDeferredType {
final class Mode<T> extends DefaultAggregateFunction<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = 5204073215694477981L;
private static final Set<SQLDialect> EMULATE_AS_ORDERED_SET_AGG = SQLDialect.supportedBy(H2, POSTGRES);
Mode(Field<T> arg) {
super(false, N_MODE, arg.getDataType(), arg);
}
@SuppressWarnings("unchecked")
@Override
public final <T> AggregateFilterStep<T> withinGroupOrderBy(OrderField<T> field) {
DataType<T> type = field instanceof SortFieldImpl
? ((SortFieldImpl<T>) field).getField().getDataType()
: field instanceof Field
? ((AbstractField<T>) field).getDataType()
: (DataType<T>) SQLDataType.NUMERIC;
return new DefaultAggregateFunction<>("mode", type).withinGroupOrderBy(field);
public void accept(Context<?> ctx) {
if (EMULATE_AS_ORDERED_SET_AGG.contains(ctx.dialect()))
ctx.visit(mode().withinGroupOrderBy(arguments.get(0)));
else
super.accept(ctx);
}
}

View File

@ -0,0 +1,62 @@
/*
* 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
*
* http://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
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import org.jooq.AggregateFilterStep;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.OrderField;
import org.jooq.OrderedAggregateFunctionOfDeferredType;
/**
* @author Lukas Eder
*/
final class ModeDeferred implements OrderedAggregateFunctionOfDeferredType {
@SuppressWarnings("unchecked")
@Override
public final <T> AggregateFilterStep<T> withinGroupOrderBy(OrderField<T> field) {
DataType<T> type = field instanceof SortFieldImpl
? ((SortFieldImpl<T>) field).getField().getDataType()
: field instanceof Field
? ((AbstractField<T>) field).getDataType()
: (DataType<T>) SQLDataType.NUMERIC;
return new DefaultAggregateFunction<>("mode", type).withinGroupOrderBy(field);
}
}

View File

@ -95,6 +95,7 @@ final class Names {
static final Name N_MD5 = DSL.unquotedName("md5");
static final Name N_MEDIAN = DSL.unquotedName("median");
static final Name N_MOD = DSL.unquotedName("mod");
static final Name N_MODE = DSL.unquotedName("mode");
static final Name N_NEXTVAL = DSL.unquotedName("nextval");
static final Name N_NOT = DSL.unquotedName("not");
static final Name N_NTILE = DSL.unquotedName("ntile");