[#4842] Add support for RANGE clause in window functions
This commit is contained in:
parent
bdd7e0b0c7
commit
01c370715b
@ -134,4 +134,73 @@ public interface WindowRowsStep<T> extends WindowFinalStep<T> {
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowRowsAndStep<T> rowsBetweenFollowing(int number);
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE UNBOUNDED PRECEDING</code> frame clause to the window
|
||||
* function.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowFinalStep<T> rangeUnboundedPreceding();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE [number] PRECEDING</code> frame clause to the window
|
||||
* function.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowFinalStep<T> rangePreceding(int number);
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE CURRENT ROW</code> frame clause to the window function.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowFinalStep<T> rangeCurrentRow();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE UNBOUNDED FOLLOWING</code> frame clause to the window
|
||||
* function.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowFinalStep<T> rangeUnboundedFollowing();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE [number] FOLLOWING</code> frame clause to the window
|
||||
* function.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowFinalStep<T> rangeFollowing(int number);
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE BETWEEN UNBOUNDED PRECEDING ...</code> frame clause to
|
||||
* the window function.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowRowsAndStep<T> rangeBetweenUnboundedPreceding();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE BETWEEN [number] PRECEDING ...</code> frame clause to
|
||||
* the window function.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowRowsAndStep<T> rangeBetweenPreceding(int number);
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE BETWEEN CURRENT ROW ...</code> frame clause to
|
||||
* the window function.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowRowsAndStep<T> rangeBetweenCurrentRow();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE BETWEEN UNBOUNDED FOLLOWING ...</code> frame clause to
|
||||
* the window function.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowRowsAndStep<T> rangeBetweenUnboundedFollowing();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE BETWEEN [number] FOLLOWING ...</code> frame clause to
|
||||
* the window function.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowRowsAndStep<T> rangeBetweenFollowing(int number);
|
||||
}
|
||||
|
||||
@ -133,4 +133,73 @@ public interface WindowSpecificationRowsStep extends WindowSpecificationFinalSte
|
||||
@Support({ POSTGRES })
|
||||
WindowSpecificationRowsAndStep rowsBetweenFollowing(int number);
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE UNBOUNDED PRECEDING</code> frame clause to the window
|
||||
* specification.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowSpecificationFinalStep rangeUnboundedPreceding();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE [number] PRECEDING</code> frame clause to the window
|
||||
* specification.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowSpecificationFinalStep rangePreceding(int number);
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE CURRENT ROW</code> frame clause to the window
|
||||
* specification.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowSpecificationFinalStep rangeCurrentRow();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE UNBOUNDED FOLLOWING</code> frame clause to the window
|
||||
* specification.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowSpecificationFinalStep rangeUnboundedFollowing();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE [number] FOLLOWING</code> frame clause to the window
|
||||
* specification.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowSpecificationFinalStep rangeFollowing(int number);
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE BETWEEN UNBOUNDED PRECEDING ...</code> frame clause to
|
||||
* the window specification.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowSpecificationRowsAndStep rangeBetweenUnboundedPreceding();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE BETWEEN [number] PRECEDING ...</code> frame clause to
|
||||
* the window specification.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowSpecificationRowsAndStep rangeBetweenPreceding(int number);
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE BETWEEN CURRENT ROW ...</code> frame clause to the
|
||||
* window specification.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowSpecificationRowsAndStep rangeBetweenCurrentRow();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE BETWEEN UNBOUNDED FOLLOWING ...</code> frame clause to
|
||||
* the window specification.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowSpecificationRowsAndStep rangeBetweenUnboundedFollowing();
|
||||
|
||||
/**
|
||||
* Add a <code>RANGE BETWEEN [number] FOLLOWING ...</code> frame clause to
|
||||
* the window specification.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
WindowSpecificationRowsAndStep rangeBetweenFollowing(int number);
|
||||
}
|
||||
|
||||
@ -13392,6 +13392,86 @@ public class DSL {
|
||||
return new WindowSpecificationImpl().rowsBetweenFollowing(number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link WindowSpecification} with a <code>RANGE</code> clause.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static WindowSpecificationFinalStep rangeUnboundedPreceding() {
|
||||
return new WindowSpecificationImpl().rangeUnboundedPreceding();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link WindowSpecification} with a <code>RANGE</code> clause.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static WindowSpecificationFinalStep rangePreceding(int number) {
|
||||
return new WindowSpecificationImpl().rangePreceding(number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link WindowSpecification} with a <code>RANGE</code> clause.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static WindowSpecificationFinalStep rangeCurrentRow() {
|
||||
return new WindowSpecificationImpl().rangeCurrentRow();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link WindowSpecification} with a <code>RANGE</code> clause.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static WindowSpecificationFinalStep rangeUnboundedFollowing() {
|
||||
return new WindowSpecificationImpl().rangeUnboundedFollowing();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link WindowSpecification} with a <code>RANGE</code> clause.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static WindowSpecificationFinalStep rangeFollowing(int number) {
|
||||
return new WindowSpecificationImpl().rangeFollowing(number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link WindowSpecification} with a <code>RANGE</code> clause.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static WindowSpecificationRowsAndStep rangeBetweenUnboundedPreceding() {
|
||||
return new WindowSpecificationImpl().rangeBetweenUnboundedPreceding();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link WindowSpecification} with a <code>RANGE</code> clause.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static WindowSpecificationRowsAndStep rangeBetweenPreceding(int number) {
|
||||
return new WindowSpecificationImpl().rangeBetweenPreceding(number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link WindowSpecification} with a <code>RANGE</code> clause.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static WindowSpecificationRowsAndStep rangeBetweenCurrentRow() {
|
||||
return new WindowSpecificationImpl().rangeBetweenCurrentRow();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link WindowSpecification} with a <code>RANGE</code> clause.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static WindowSpecificationRowsAndStep rangeBetweenUnboundedFollowing() {
|
||||
return new WindowSpecificationImpl().rangeBetweenUnboundedFollowing();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link WindowSpecification} with a <code>RANGE</code> clause.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static WindowSpecificationRowsAndStep rangeBetweenFollowing(int number) {
|
||||
return new WindowSpecificationImpl().rangeBetweenFollowing(number);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX Window functions
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -759,6 +759,66 @@ class Function<T> extends AbstractField<T> implements
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowFinalStep<T> rangeUnboundedPreceding() {
|
||||
windowSpecification.rowsUnboundedPreceding();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowFinalStep<T> rangePreceding(int number) {
|
||||
windowSpecification.rowsPreceding(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowFinalStep<T> rangeCurrentRow() {
|
||||
windowSpecification.rowsCurrentRow();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowFinalStep<T> rangeUnboundedFollowing() {
|
||||
windowSpecification.rowsUnboundedFollowing();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowFinalStep<T> rangeFollowing(int number) {
|
||||
windowSpecification.rowsFollowing(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowRowsAndStep<T> rangeBetweenUnboundedPreceding() {
|
||||
windowSpecification.rowsBetweenUnboundedPreceding();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowRowsAndStep<T> rangeBetweenPreceding(int number) {
|
||||
windowSpecification.rowsBetweenPreceding(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowRowsAndStep<T> rangeBetweenCurrentRow() {
|
||||
windowSpecification.rowsBetweenCurrentRow();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowRowsAndStep<T> rangeBetweenUnboundedFollowing() {
|
||||
windowSpecification.rowsBetweenUnboundedFollowing();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowRowsAndStep<T> rangeBetweenFollowing(int number) {
|
||||
windowSpecification.rowsBetweenFollowing(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowFinalStep<T> andUnboundedPreceding() {
|
||||
windowSpecification.andUnboundedPreceding();
|
||||
|
||||
@ -45,6 +45,8 @@ import static org.jooq.SQLDialect.CUBRID;
|
||||
// ...
|
||||
// ...
|
||||
import static org.jooq.impl.DSL.one;
|
||||
import static org.jooq.impl.WindowSpecificationImpl.FrameUnits.RANGE;
|
||||
import static org.jooq.impl.WindowSpecificationImpl.FrameUnits.ROWS;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@ -75,8 +77,9 @@ class WindowSpecificationImpl extends AbstractQueryPart implements
|
||||
|
||||
private final QueryPartList<Field<?>> partitionBy;
|
||||
private final SortFieldList orderBy;
|
||||
private Integer rowsStart;
|
||||
private Integer rowsEnd;
|
||||
private Integer frameStart;
|
||||
private Integer frameEnd;
|
||||
private FrameUnits frameUnits;
|
||||
private boolean partitionByOne;
|
||||
|
||||
WindowSpecificationImpl() {
|
||||
@ -112,19 +115,19 @@ class WindowSpecificationImpl extends AbstractQueryPart implements
|
||||
glue = " ";
|
||||
}
|
||||
|
||||
if (rowsStart != null) {
|
||||
if (frameStart != null) {
|
||||
ctx.sql(glue);
|
||||
ctx.keyword("rows").sql(' ');
|
||||
ctx.keyword(frameUnits.keyword).sql(' ');
|
||||
|
||||
if (rowsEnd != null) {
|
||||
if (frameEnd != null) {
|
||||
ctx.keyword("between").sql(' ');
|
||||
toSQLRows(ctx, rowsStart);
|
||||
toSQLRows(ctx, frameStart);
|
||||
|
||||
ctx.sql(' ').keyword("and").sql(' ');
|
||||
toSQLRows(ctx, rowsEnd);
|
||||
toSQLRows(ctx, frameEnd);
|
||||
}
|
||||
else {
|
||||
toSQLRows(ctx, rowsStart);
|
||||
toSQLRows(ctx, frameStart);
|
||||
}
|
||||
|
||||
glue = " ";
|
||||
@ -193,31 +196,36 @@ class WindowSpecificationImpl extends AbstractQueryPart implements
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep rowsUnboundedPreceding() {
|
||||
rowsStart = Integer.MIN_VALUE;
|
||||
frameUnits = ROWS;
|
||||
frameStart = Integer.MIN_VALUE;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep rowsPreceding(int number) {
|
||||
rowsStart = -number;
|
||||
frameUnits = ROWS;
|
||||
frameStart = -number;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep rowsCurrentRow() {
|
||||
rowsStart = 0;
|
||||
frameUnits = ROWS;
|
||||
frameStart = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep rowsUnboundedFollowing() {
|
||||
rowsStart = Integer.MAX_VALUE;
|
||||
frameUnits = ROWS;
|
||||
frameStart = Integer.MAX_VALUE;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep rowsFollowing(int number) {
|
||||
rowsStart = number;
|
||||
frameUnits = ROWS;
|
||||
frameStart = number;
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -251,33 +259,109 @@ class WindowSpecificationImpl extends AbstractQueryPart implements
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep rangeUnboundedPreceding() {
|
||||
frameUnits = RANGE;
|
||||
frameStart = Integer.MIN_VALUE;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep rangePreceding(int number) {
|
||||
frameUnits = RANGE;
|
||||
frameStart = -number;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep rangeCurrentRow() {
|
||||
frameUnits = RANGE;
|
||||
frameStart = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep rangeUnboundedFollowing() {
|
||||
frameUnits = RANGE;
|
||||
frameStart = Integer.MAX_VALUE;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep rangeFollowing(int number) {
|
||||
frameUnits = RANGE;
|
||||
frameStart = number;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationRowsAndStep rangeBetweenUnboundedPreceding() {
|
||||
rangeUnboundedPreceding();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationRowsAndStep rangeBetweenPreceding(int number) {
|
||||
rangePreceding(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationRowsAndStep rangeBetweenCurrentRow() {
|
||||
rangeCurrentRow();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationRowsAndStep rangeBetweenUnboundedFollowing() {
|
||||
rangeUnboundedFollowing();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationRowsAndStep rangeBetweenFollowing(int number) {
|
||||
rangeFollowing(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep andUnboundedPreceding() {
|
||||
rowsEnd = Integer.MIN_VALUE;
|
||||
frameEnd = Integer.MIN_VALUE;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep andPreceding(int number) {
|
||||
rowsEnd = -number;
|
||||
frameEnd = -number;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep andCurrentRow() {
|
||||
rowsEnd = 0;
|
||||
frameEnd = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep andUnboundedFollowing() {
|
||||
rowsEnd = Integer.MAX_VALUE;
|
||||
frameEnd = Integer.MAX_VALUE;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final WindowSpecificationFinalStep andFollowing(int number) {
|
||||
rowsEnd = number;
|
||||
frameEnd = number;
|
||||
return this;
|
||||
}
|
||||
|
||||
enum FrameUnits {
|
||||
ROWS("rows"),
|
||||
RANGE("range");
|
||||
|
||||
private final String keyword;
|
||||
|
||||
private FrameUnits(String keyword) {
|
||||
this.keyword = keyword;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user