[#3870] Add support for aggregate FILTER clause

This commit is contained in:
Lukas Eder 2014-12-19 17:11:46 +01:00
parent cb209bab1a
commit e1d93bedc5
6 changed files with 202 additions and 124 deletions

View File

@ -0,0 +1,118 @@
/**
* Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* 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.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq;
import java.util.Collection;
import org.jooq.impl.DSL;
/**
* The step in the specification of aggregate functions where the SQL:2003
* standard <code>FILTER clause</code> can be added.
*
* @author Lukas Eder
*/
public interface AggregateFilterStep<T> extends WindowBeforeOverStep<T> {
/**
* Add a <code>FILTER clause</code> to the aggregate function, connecting
* conditions with each other with {@link Operator#AND}.
*/
@Support
WindowBeforeOverStep<T> filterWhere(Condition... conditions);
/**
* Add a <code>FILTER clause</code> to the aggregate function, connecting
* conditions with each other with {@link Operator#AND}.
*/
@Support
WindowBeforeOverStep<T> filterWhere(Collection<? extends Condition> conditions);
/**
* Add a <code>FILTER clause</code> to the aggregate function, connecting
* conditions with each other with {@link Operator#AND}.
*/
@Support
WindowBeforeOverStep<T> filterWhere(Field<Boolean> field);
/**
* Add a <code>FILTER clause</code> to the aggregate function, connecting
* conditions with each other with {@link Operator#AND}.
* <p>
* <b>NOTE</b>: When inserting plain SQL into jOOQ objects, you must
* guarantee syntax integrity. You may also create the possibility of
* malicious SQL injection. Be sure to properly use bind variables and/or
* escape literals when concatenated into SQL clauses!
*
* @see DSL#condition(String)
*/
@Support
WindowBeforeOverStep<T> filterWhere(String sql);
/**
* Add a <code>FILTER clause</code> to the aggregate function, connecting
* conditions with each other with {@link Operator#AND}.
* <p>
* <b>NOTE</b>: When inserting plain SQL into jOOQ objects, you must
* guarantee syntax integrity. You may also create the possibility of
* malicious SQL injection. Be sure to properly use bind variables and/or
* escape literals when concatenated into SQL clauses!
*
* @see DSL#condition(String, Object...)
*/
@Support
WindowBeforeOverStep<T> filterWhere(String sql, Object... bindings);
/**
* Add a <code>FILTER clause</code> to the aggregate function, connecting
* conditions with each other with {@link Operator#AND}.
* <p>
* <b>NOTE</b>: When inserting plain SQL into jOOQ objects, you must
* guarantee syntax integrity. You may also create the possibility of
* malicious SQL injection. Be sure to properly use bind variables and/or
* escape literals when concatenated into SQL clauses!
*
* @see DSL#condition(String, QueryPart...)
*/
@Support
WindowBeforeOverStep<T> filterWhere(String sql, QueryPart... parts);
}

View File

@ -40,14 +40,6 @@
*/
package org.jooq;
import static org.jooq.SQLDialect.CUBRID;
// ...
import static org.jooq.SQLDialect.H2;
// ...
// ...
// ...
import static org.jooq.SQLDialect.POSTGRES;
// ...
// ...
import java.util.Collection;
@ -60,86 +52,7 @@ import java.util.Collection;
*
* @author Lukas Eder
*/
public interface AggregateFunction<T> extends Field<T>, WindowOverStep<T> {
/**
* Turn this aggregate function into a window function.
* <p>
* An example: <code><pre>
* MAX(id) OVER (PARTITION BY 1)
* </code></pre>
* <p>
* Window functions are supported in CUBRID, DB2, Postgres, Oracle, SQL
* Server and Sybase.
*/
@Override
@Support({ CUBRID, H2, POSTGRES })
WindowPartitionByStep<T> over();
/**
* Turn this aggregate function into a window function referencing a window
* name.
* <p>
* An example: <code><pre>
* MAX(id) OVER my_window
* </code></pre>
* <p>
* Window functions are supported in CUBRID, DB2, Postgres, Oracle, SQL
* Server and Sybase. If the <code>WINDOW</code> clause is not supported
* (see {@link SelectWindowStep#window(WindowDefinition...)}, then
* referenced windows will be inlined.
*/
@Override
@Support({ CUBRID, POSTGRES })
WindowFinalStep<T> over(Name name);
/**
* Turn this aggregate function into a window function referencing a window
* name.
* <p>
* An example: <code><pre>
* MAX(id) OVER my_window
* </code></pre>
* <p>
* Window functions are supported in CUBRID, DB2, Postgres, Oracle, SQL
* Server and Sybase. If the <code>WINDOW</code> clause is not supported
* (see {@link SelectWindowStep#window(WindowDefinition...)}, then
* referenced windows will be inlined.
*/
@Override
@Support({ CUBRID, POSTGRES })
WindowFinalStep<T> over(String name);
/**
* Turn this aggregate function into a window function.
* <p>
* An example: <code><pre>
* MAX(id) OVER (PARTITION BY 1)
* </code></pre>
* <p>
* Window functions are supported in CUBRID, DB2, Postgres, Oracle, SQL
* Server and Sybase.
*/
@Override
@Support({ CUBRID, POSTGRES })
WindowFinalStep<T> over(WindowSpecification specification);
/**
* Turn this aggregate function into a window function referencing a window
* definition.
* <p>
* An example: <code><pre>
* MAX(id) OVER my_window
* </code></pre>
* <p>
* Window functions are supported in CUBRID, DB2, Postgres, Oracle, SQL
* Server and Sybase. If the <code>WINDOW</code> clause is not supported
* (see {@link SelectWindowStep#window(WindowDefinition...)}, then
* referenced windows will be inlined.
*/
@Override
@Support({ CUBRID, POSTGRES })
WindowFinalStep<T> over(WindowDefinition definition);
public interface AggregateFunction<T> extends AggregateFilterStep<T> {
/* [pro] xx
xxx
@ -153,7 +66,7 @@ public interface AggregateFunction<T> extends Field<T>, WindowOverStep<T> {
x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx
xx
xxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx
xxx
x xxxxxxxx xxxx xxxxxxxxx xxxxxxxx xx xxxxxxxxxxxxxxxxxx xxxxxx
@ -166,7 +79,7 @@ public interface AggregateFunction<T> extends Field<T>, WindowOverStep<T> {
x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx
xx
xxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx
xxx
x xxxxxxxx xxxx xxxxxxxxx xxxxxxxx xx xxxxxxxxxxxxxxxxxx xxxxxx
@ -179,7 +92,7 @@ public interface AggregateFunction<T> extends Field<T>, WindowOverStep<T> {
x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx
xx
xxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxx xxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxx xxxxxxxx
xxx
x xxxxxxxx xxxx xxxxxxxxx xxxxxxxx xx xxxxxxxxxxxxxxxxxx xxxxxx
@ -192,7 +105,7 @@ public interface AggregateFunction<T> extends Field<T>, WindowOverStep<T> {
x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx
xx
xxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx
xxx
x xxxxxxxx xxxx xxxxxxxxx xxxxxxxx xx xxxxxxxxxxxxxxxxxx xxxxxx
@ -205,7 +118,7 @@ public interface AggregateFunction<T> extends Field<T>, WindowOverStep<T> {
x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx
xx
xxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx
xxx
x xxxxxxxx xxxx xxxxxxxxx xxxxxxxx xx xxxxxxxxxxxxxxxxxx xxxxxx
@ -218,6 +131,6 @@ public interface AggregateFunction<T> extends Field<T>, WindowOverStep<T> {
x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx
xx
xxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxx xxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxx xxxxxxxx
xx [/pro] */
}

View File

@ -83,19 +83,19 @@ public interface OrderedAggregateFunction<T> {
* aggregate function
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
AggregateFunction<T> withinGroupOrderBy(Field<?>... fields);
AggregateFilterStep<T> withinGroupOrderBy(Field<?>... fields);
/**
* Add an <code>WITHIN GROUP (ORDER BY ..)</code> clause to the ordered
* aggregate function
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
AggregateFunction<T> withinGroupOrderBy(SortField<?>... fields);
AggregateFilterStep<T> withinGroupOrderBy(SortField<?>... fields);
/**
* Add an <code>WITHIN GROUP (ORDER BY ..)</code> clause to the ordered
* aggregate function
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
AggregateFunction<T> withinGroupOrderBy(Collection<? extends SortField<?>> fields);
AggregateFilterStep<T> withinGroupOrderBy(Collection<? extends SortField<?>> fields);
}

View File

@ -40,16 +40,6 @@
*/
package org.jooq;
import static org.jooq.SQLDialect.CUBRID;
// ...
import static org.jooq.SQLDialect.DERBY;
import static org.jooq.SQLDialect.H2;
import static org.jooq.SQLDialect.HSQLDB;
// ...
// ...
import static org.jooq.SQLDialect.POSTGRES;
// ...
// ...
/**
@ -73,11 +63,4 @@ import static org.jooq.SQLDialect.POSTGRES;
*/
public interface WindowBeforeOverStep<T> extends WindowOverStep<T>, Field<T> {
/**
* Add an <code>OVER</code> clause
*/
@Override
@Support({ CUBRID, DERBY, H2, HSQLDB, POSTGRES })
WindowPartitionByStep<T> over();
}

View File

@ -9984,7 +9984,7 @@ public class DSL {
*/
@Support
public static AggregateFunction<Integer> count() {
return count(field("*", Integer.class));
return count(Function.ASTERISK);
}
/**

View File

@ -49,8 +49,11 @@ import static org.jooq.SQLDialect.HSQLDB;
import static org.jooq.SQLDialect.MARIADB;
import static org.jooq.SQLDialect.MYSQL;
import static org.jooq.SQLDialect.POSTGRES;
import static org.jooq.SQLDialect.POSTGRES_9_4;
// ...
import static org.jooq.impl.DSL.condition;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.one;
import static org.jooq.impl.Term.LIST_AGG;
import static org.jooq.impl.Term.ROW_NUMBER;
import static org.jooq.impl.Utils.DATA_LOCALLY_SCOPED_DATA_MAP;
@ -60,7 +63,9 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import org.jooq.AggregateFilterStep;
import org.jooq.AggregateFunction;
import org.jooq.Condition;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
@ -92,8 +97,6 @@ class Function<T> extends AbstractField<T> implements
// Cascading interface implementations for aggregate function behaviour
OrderedAggregateFunction<T>,
AggregateFunction<T>,
WindowBeforeOverStep<T>,
// and for window function behaviour
WindowIgnoreNullsStep<T>,
WindowPartitionByStep<T>,
@ -103,6 +106,8 @@ class Function<T> extends AbstractField<T> implements
private static final long serialVersionUID = 347252741712134044L;
static final Field<Integer> ASTERISK = DSL.field("*", Integer.class);
// Mutually exclusive attributes: super.getName(), this.name, this.term
private final Name name;
private final Term term;
@ -112,6 +117,7 @@ class Function<T> extends AbstractField<T> implements
private final boolean distinct;
private final SortFieldList withinGroupOrderBy;
private final SortFieldList keepDenseRankOrderBy;
private Condition filter;
private WindowSpecificationImpl windowSpecification;
private WindowDefinitionImpl windowDefinition;
private Name windowName;
@ -188,6 +194,7 @@ class Function<T> extends AbstractField<T> implements
}
else if (term == LIST_AGG && asList(POSTGRES).contains(ctx.family())) {
toSQLStringAgg(ctx);
toSQLFilterClause(ctx);
toSQLOverClause(ctx);
}
/* [pro] xx
@ -199,6 +206,7 @@ class Function<T> extends AbstractField<T> implements
toSQLArguments(ctx);
toSQLKeepDenseRankOrderByClause(ctx);
toSQLWithinGroupClause(ctx);
toSQLFilterClause(ctx);
toSQLOverClause(ctx);
}
}
@ -308,6 +316,18 @@ class Function<T> extends AbstractField<T> implements
ctx.sql(")");
}
final void toSQLFilterClause(Context<?> ctx) {
if (filter != null && POSTGRES_9_4.precedes(ctx.dialect())) {
ctx.sql(" ")
.keyword("filter")
.sql(" (")
.keyword("where")
.sql(" ")
.visit(filter)
.sql(")");
}
}
final void toSQLOverClause(Context<?> ctx) {
QueryPart window = window(ctx);
@ -408,7 +428,18 @@ class Function<T> extends AbstractField<T> implements
}
if (!arguments.isEmpty()) {
ctx.visit(arguments);
if (filter == null || POSTGRES_9_4.precedes(ctx.dialect())) {
ctx.visit(arguments);
}
else {
QueryPartList<Field<?>> expressions = new QueryPartList<Field<?>>();
for (QueryPart argument : arguments) {
expressions.add(DSL.decode().when(filter, argument == ASTERISK ? one() : argument));
}
ctx.visit(expressions);
}
}
if (distinct) {
@ -483,41 +514,74 @@ class Function<T> extends AbstractField<T> implements
/* [pro] xx
xxxxxxxxx
xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx x
xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx x
xxxxx x xxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxx xxxxx
x
xxxxxxxxx
xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx x
xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx x
xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x
xxxxxxxxx
xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxx xxxxxxx x
xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxx xxxxxxx x
xxxxx x xxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxx xxxxx
x
xxxxxxxxx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