[#596] Add support for VARIANCE() and STDDEV() OVER() window functions

[#874] Reduce the number of internal classes for dialect-specific function aliases
This commit is contained in:
Lukas Eder 2011-10-16 12:46:47 +00:00
parent 9223364ee9
commit 87ce687450
9 changed files with 191 additions and 238 deletions

View File

@ -6547,6 +6547,47 @@ public abstract class jOOQAbstractTest<
column = 0;
// STDDEV_POP(), STDDEV_SAMP(), VAR_POP(), VAR_SAMP()
result =
create().select(TBook_ID(),
TBook_ID().stddevPopOver()
.partitionByOne(),
TBook_ID().stddevSampOver()
.partitionByOne(),
TBook_ID().varPopOver()
.partitionByOne(),
TBook_ID().varSampOver()
.partitionByOne(),
TBook_ID().stddevPopOver()
.partitionBy(TBook_AUTHOR_ID()),
TBook_ID().stddevSampOver()
.partitionBy(TBook_AUTHOR_ID()),
TBook_ID().varPopOver()
.partitionBy(TBook_AUTHOR_ID()),
TBook_ID().varSampOver()
.partitionBy(TBook_AUTHOR_ID()))
.from(TBook())
.orderBy(TBook_ID().asc())
.fetch();
// Overall STDDEV_POP(), STDDEV_SAMP(), VAR_POP(), VAR_SAMP()
assertEquals("1.118", result.getValueAsString(0, 1).substring(0, 5));
assertEquals(1.25, result.getValueAsDouble(0, 3));
// Partitioned STDDEV_POP(), STDDEV_SAMP(), VAR_POP(), VAR_SAMP()
assertEquals(0.5, result.getValueAsDouble(0, 5));
assertEquals(0.25, result.getValueAsDouble(0, 7));
// DB2 only knows STDDEV_POP / VAR_POP
if (getDialect() != SQLDialect.DB2) {
assertEquals("1.290", result.getValueAsString(0, 2).substring(0, 5));
assertEquals("1.666", result.getValueAsString(0, 4).substring(0, 5));
assertEquals("0.707", result.getValueAsString(0, 6).substring(0, 5));
assertEquals(0.5, result.getValueAsDouble(0, 8));
}
column = 0;
if (getDialect() == SQLDialect.SQLSERVER) {
log.info("SKIPPING", "ROWS UNBOUNDED PRECEDING and similar tests");
return;

View File

@ -833,6 +833,38 @@ public interface Field<T> extends NamedTypeProviderQueryPart<T>, AliasProvider<F
*/
WindowIgnoreNullsStep<T> lag(int offset, Field<T> defaultValue);
/**
* The <code>stddev_pop(field) over ([analytic clause])</code> function.
* <p>
* Window functions are supported in DB2, Postgres, Oracle, SQL Server and
* Sybase.
*/
WindowPartitionByStep<BigDecimal> stddevPopOver();
/**
* The <code>stddev_samp(field) over ([analytic clause])</code> function.
* <p>
* Window functions are supported in DB2, Postgres, Oracle, SQL Server and
* Sybase.
*/
WindowPartitionByStep<BigDecimal> stddevSampOver();
/**
* The <code>var_pop(field) over ([analytic clause])</code> function.
* <p>
* Window functions are supported in DB2, Postgres, Oracle, SQL Server and
* Sybase.
*/
WindowPartitionByStep<BigDecimal> varPopOver();
/**
* The <code>var_samp(field) over ([analytic clause])</code> function.
* <p>
* Window functions are supported in DB2, Postgres, Oracle, SQL Server and
* Sybase.
*/
WindowPartitionByStep<BigDecimal> varSampOver();
// ------------------------------------------------------------------------
// String functions created from this field
// ------------------------------------------------------------------------

View File

@ -367,6 +367,26 @@ abstract class AbstractField<T> extends AbstractNamedTypeProviderQueryPart<T> im
return new WindowFunction<T>("lag", getDataType(), this, literal(offset), defaultValue);
}
@Override
public final WindowPartitionByStep<BigDecimal> stddevPopOver() {
return new WindowFunction<BigDecimal>(Term.STDDEV_POP, SQLDataType.NUMERIC, this);
}
@Override
public final WindowPartitionByStep<BigDecimal> stddevSampOver() {
return new WindowFunction<BigDecimal>(Term.STDDEV_SAMP, SQLDataType.NUMERIC, this);
}
@Override
public final WindowPartitionByStep<BigDecimal> varPopOver() {
return new WindowFunction<BigDecimal>(Term.VAR_POP, SQLDataType.NUMERIC, this);
}
@Override
public final WindowPartitionByStep<BigDecimal> varSampOver() {
return new WindowFunction<BigDecimal>(Term.VAR_SAMP, SQLDataType.NUMERIC, this);
}
// ------------------------------------------------------------------------
// Functions created from this field
// ------------------------------------------------------------------------
@ -408,22 +428,22 @@ abstract class AbstractField<T> extends AbstractNamedTypeProviderQueryPart<T> im
@Override
public final Field<BigDecimal> stddevPop() {
return new StddevPop(this);
return new Function<BigDecimal>(Term.STDDEV_POP, SQLDataType.NUMERIC, this);
}
@Override
public final Field<BigDecimal> stddevSamp() {
return new StddevSamp(this);
return new Function<BigDecimal>(Term.STDDEV_SAMP, SQLDataType.NUMERIC, this);
}
@Override
public final Field<BigDecimal> varPop() {
return new VarPop(this);
return new Function<BigDecimal>(Term.VAR_POP, SQLDataType.NUMERIC, this);
}
@Override
public final Field<BigDecimal> varSamp() {
return new VarSamp(this);
return new Function<BigDecimal>(Term.VAR_SAMP, SQLDataType.NUMERIC, this);
}
@Override

View File

@ -47,20 +47,30 @@ import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.NamedQueryPart;
import org.jooq.RenderContext;
import org.jooq.SQLDialect;
/**
* @author Lukas Eder
*/
class Function<T> extends AbstractField<T> {
private static final long serialVersionUID = 347252741712134044L;
private static final long serialVersionUID = 347252741712134044L;
private final List<Field<?>> arguments;
private final Term term;
Function(String name, DataType<T> type, Field<?>... arguments) {
super(name, type);
this.arguments = Arrays.asList(arguments);
this.term = null;
}
Function(Term term, DataType<T> type, Field<?>... arguments) {
super(term.name().toLowerCase(), type);
this.arguments = Arrays.asList(arguments);
this.term = term;
}
@Override
@ -71,7 +81,7 @@ class Function<T> extends AbstractField<T> {
@Override
public final void toSQL(RenderContext context) {
context.sql(getFNPrefix());
context.sql(getName());
context.sql(getFNName(context.getDialect()));
context.sql(getArgumentListDelimiter(context, "("));
String separator = "";
@ -86,6 +96,15 @@ class Function<T> extends AbstractField<T> {
context.sql(getFNSuffix());
}
private final String getFNName(SQLDialect dialect) {
if (term != null) {
return term.translate(dialect);
}
else {
return getName();
}
}
/**
* Subclasses may override this method to add an additional prefix in the
* function SQL string

View File

@ -1,70 +0,0 @@
/**
* Copyright (c) 2009-2011, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.impl;
import java.math.BigDecimal;
import org.jooq.Configuration;
import org.jooq.Field;
/**
* @author Lukas Eder
*/
class StddevSamp extends AbstractFunction<BigDecimal> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
StddevSamp(Field<?> arg) {
super("stddev_samp", SQLDataType.NUMERIC, arg);
}
@Override
final Field<BigDecimal> getFunction0(Configuration configuration) {
switch (configuration.getDialect()) {
case DB2:
return new Function<BigDecimal>("stddev", SQLDataType.NUMERIC, getArguments());
case SQLSERVER:
return new Function<BigDecimal>("stdev", SQLDataType.NUMERIC, getArguments());
default:
return new Function<BigDecimal>("stddev_samp", SQLDataType.NUMERIC, getArguments());
}
}
}

View File

@ -35,36 +35,66 @@
*/
package org.jooq.impl;
import java.math.BigDecimal;
import org.jooq.Configuration;
import org.jooq.Field;
import org.jooq.SQLDialect;
/**
* The term dictionary enumerates standard expressions and their
* dialect-specific variants if applicable
*
* @author Lukas Eder
*/
class StddevPop extends AbstractFunction<BigDecimal> {
enum Term {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
STDDEV_POP,
STDDEV_SAMP,
VAR_POP,
VAR_SAMP
StddevPop(Field<?> arg) {
super("stddev_pop", SQLDataType.NUMERIC, arg);
}
;
@Override
final Field<BigDecimal> getFunction0(Configuration configuration) {
switch (configuration.getDialect()) {
case DB2:
return new Function<BigDecimal>("stddev", SQLDataType.NUMERIC, getArguments());
public final String translate(SQLDialect dialect) {
switch (this) {
case STDDEV_POP:
switch (dialect) {
case DB2:
return "stddev";
case SQLSERVER:
return "stdevp";
default:
return "stddev_pop";
}
case SQLSERVER:
return new Function<BigDecimal>("stdevp", SQLDataType.NUMERIC, getArguments());
case STDDEV_SAMP:
switch (dialect) {
case DB2:
return "stddev";
case SQLSERVER:
return "stdev";
default:
return "stddev_samp";
}
default:
return new Function<BigDecimal>("stddev_pop", SQLDataType.NUMERIC, getArguments());
case VAR_POP:
switch (dialect) {
case DB2:
return "variance";
case SQLSERVER:
return "varp";
default:
return "var_pop";
}
case VAR_SAMP:
switch (dialect) {
case DB2:
return "variance";
case SQLSERVER:
return "var";
default:
return "var_samp";
}
}
return null;
}
}

View File

@ -1,70 +0,0 @@
/**
* Copyright (c) 2009-2011, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.impl;
import java.math.BigDecimal;
import org.jooq.Configuration;
import org.jooq.Field;
/**
* @author Lukas Eder
*/
class VarPop extends AbstractFunction<BigDecimal> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
VarPop(Field<?> arg) {
super("var_pop", SQLDataType.NUMERIC, arg);
}
@Override
final Field<BigDecimal> getFunction0(Configuration configuration) {
switch (configuration.getDialect()) {
case DB2:
return new Function<BigDecimal>("variance", SQLDataType.NUMERIC, getArguments());
case SQLSERVER:
return new Function<BigDecimal>("varp", SQLDataType.NUMERIC, getArguments());
default:
return new Function<BigDecimal>("var_pop", SQLDataType.NUMERIC, getArguments());
}
}
}

View File

@ -1,70 +0,0 @@
/**
* Copyright (c) 2009-2011, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.impl;
import java.math.BigDecimal;
import org.jooq.Configuration;
import org.jooq.Field;
/**
* @author Lukas Eder
*/
class VarSamp extends AbstractFunction<BigDecimal> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
VarSamp(Field<?> arg) {
super("var_samp", SQLDataType.NUMERIC, arg);
}
@Override
final Field<BigDecimal> getFunction0(Configuration configuration) {
switch (configuration.getDialect()) {
case DB2:
return new Function<BigDecimal>("variance", SQLDataType.NUMERIC, getArguments());
case SQLSERVER:
return new Function<BigDecimal>("var", SQLDataType.NUMERIC, getArguments());
default:
return new Function<BigDecimal>("var_samp", SQLDataType.NUMERIC, getArguments());
}
}
}

View File

@ -73,6 +73,8 @@ implements
*/
private static final long serialVersionUID = 5505202722420252635L;
private final Term term;
private final FieldList arguments;
private final FieldList partitionBy;
private final SortFieldList orderBy;
@ -90,6 +92,16 @@ implements
this.partitionBy = new FieldList();
this.orderBy = new SortFieldList();
this.arguments = new FieldList(Arrays.asList(arguments));
this.term = null;
}
public WindowFunction(Term term, DataType<T> type, Field<?>... arguments) {
super(term.name().toLowerCase(), type);
this.partitionBy = new FieldList();
this.orderBy = new SortFieldList();
this.arguments = new FieldList(Arrays.asList(arguments));
this.term = term;
}
// -------------------------------------------------------------------------
@ -103,7 +115,7 @@ implements
@Override
public final void toSQL(RenderContext context) {
context.sql(getName());
context.sql(getFNName(context.getDialect()));
context.sql("(");
if (!arguments.isEmpty()) {
@ -190,6 +202,15 @@ implements
context.sql(")");
}
private final String getFNName(SQLDialect dialect) {
if (term != null) {
return term.translate(dialect);
}
else {
return getName();
}
}
private void toSQLRows(RenderContext context, Integer rows) {
if (rows == Integer.MIN_VALUE) {
context.sql("unbounded preceding");