[#7440] Add support for the NTH_VALUE(..) [ FROM { FIRST | LAST } ] clause

This commit is contained in:
lukaseder 2018-04-26 15:46:28 +02:00
parent bb55b3b069
commit 961ff4feb1
5 changed files with 140 additions and 31 deletions

View File

@ -754,7 +754,9 @@ keep = 'KEEP' '(' 'DENSE_RANK' ( 'FIRST' | 'LAST' ) 'ORDER BY' sortFields ')'
filter = 'FILTER' '(' 'WHERE' condition ')'
;
over = [ 'RESPECT NULLS' | 'IGNORE NULLS' ]
over =
[ 'FROM FIRST' | 'FROM LAST' ]
[ 'RESPECT NULLS' | 'IGNORE NULLS' ]
'OVER'
(
identifier

View File

@ -0,0 +1,76 @@
/*
* 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;
// ...
// ...
/**
* This type is used for the window function DSL API.
* <p>
* Example: <code><pre>
* field.firstValue()
* .ignoreNulls()
* .over()
* .partitionBy(AUTHOR_ID)
* .orderBy(PUBLISHED_IN.asc())
* .rowsBetweenUnboundedPreceding()
* .andUnboundedFollowing()
* </pre></code>
*
* @param <T> The function return type
* @author Lukas Eder
*/
public interface WindowFromFirstLastStep<T> extends WindowIgnoreNullsStep<T> {
}

View File

@ -311,6 +311,7 @@ import org.jooq.UDTRecord;
import org.jooq.Update;
import org.jooq.UpdateSetFirstStep;
import org.jooq.User;
import org.jooq.WindowFromFirstLastStep;
import org.jooq.WindowIgnoreNullsStep;
import org.jooq.WindowOverStep;
import org.jooq.WindowSpecification;
@ -17945,7 +17946,7 @@ public class DSL {
* The <code>nth_value(field) over ([analytic clause])</code> function.
*/
@Support({ FIREBIRD_3_0, MYSQL_8_0, POSTGRES })
public static <T> WindowIgnoreNullsStep<T> nthValue(Field<T> field, int nth) {
public static <T> WindowFromFirstLastStep<T> nthValue(Field<T> field, int nth) {
return nthValue(field, val(nth));
}
@ -17953,7 +17954,7 @@ public class DSL {
* The <code>nth_value(field) over ([analytic clause])</code> function.
*/
@Support({ FIREBIRD_3_0, MYSQL_8_0, POSTGRES })
public static <T> WindowIgnoreNullsStep<T> nthValue(Field<T> field, Field<Integer> nth) {
public static <T> WindowFromFirstLastStep<T> nthValue(Field<T> field, Field<Integer> nth) {
return new org.jooq.impl.Function<T>("nth_value", nullSafeDataType(field), nullSafe(field), nullSafe(nth));
}

View File

@ -38,6 +38,8 @@
package org.jooq.impl;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
// ...
import static org.jooq.SQLDialect.CUBRID;
// ...
@ -58,6 +60,7 @@ import static org.jooq.impl.Keywords.K_DENSE_RANK;
import static org.jooq.impl.Keywords.K_DISTINCT;
import static org.jooq.impl.Keywords.K_FILTER;
import static org.jooq.impl.Keywords.K_FIRST;
import static org.jooq.impl.Keywords.K_FROM;
import static org.jooq.impl.Keywords.K_IGNORE_NULLS;
import static org.jooq.impl.Keywords.K_KEEP;
import static org.jooq.impl.Keywords.K_LAST;
@ -95,6 +98,7 @@ import org.jooq.SQLDialect;
import org.jooq.WindowBeforeOverStep;
import org.jooq.WindowDefinition;
import org.jooq.WindowFinalStep;
import org.jooq.WindowFromFirstLastStep;
import org.jooq.WindowIgnoreNullsStep;
import org.jooq.WindowOrderByStep;
import org.jooq.WindowOverStep;
@ -117,7 +121,7 @@ class Function<T> extends AbstractField<T> implements
ArrayAggOrderByStep<T>,
AggregateFunction<T>,
// and for window function behaviour
WindowIgnoreNullsStep<T>,
WindowFromFirstLastStep<T>,
WindowPartitionByStep<T>,
WindowRowsStep<T>,
WindowRowsAndStep<T>
@ -147,8 +151,8 @@ class Function<T> extends AbstractField<T> implements
private Name windowName;
private boolean first;
private boolean ignoreNulls;
private boolean respectNulls;
private Boolean ignoreNulls;
private Boolean fromLast;
// -------------------------------------------------------------------------
// XXX Constructors
@ -440,12 +444,10 @@ class Function<T> extends AbstractField<T> implements
ctx.visit(K_DISTINCT);
// [#2883] PostgreSQL can use the DISTINCT keyword with formal row value expressions.
if (ctx.family() == POSTGRES && args.size() > 1) {
if (ctx.family() == POSTGRES && args.size() > 1)
ctx.sql('(');
}
else {
else
ctx.sql(' ');
}
}
if (!args.isEmpty()) {
@ -466,22 +468,25 @@ class Function<T> extends AbstractField<T> implements
if (ctx.family() == POSTGRES && args.size() > 1)
ctx.sql(')');
if (ignoreNulls) {
ctx.sql(' ').visit(K_IGNORE_NULLS);
}
else if (respectNulls) {
ctx.sql(' ').visit(K_RESPECT_NULLS);
}
}
final void toSQLFunctionName(Context<?> ctx) {
@ -549,6 +554,16 @@ class Function<T> extends AbstractField<T> implements

View File

@ -384,6 +384,7 @@ import org.jooq.User;
// ...
import org.jooq.WindowBeforeOverStep;
import org.jooq.WindowDefinition;
import org.jooq.WindowFromFirstLastStep;
import org.jooq.WindowIgnoreNullsStep;
import org.jooq.WindowOverStep;
import org.jooq.WindowSpecification;
@ -5962,7 +5963,7 @@ final class ParserImpl implements Parser {
parse(ctx, '(');
if (parseIf(ctx, ')'))
return parseWindowFunction(ctx, null, rank());
return parseWindowFunction(ctx, null, null, rank());
// Hypothetical set function
List<Field<?>> args = parseFields(ctx);
@ -5978,7 +5979,7 @@ final class ParserImpl implements Parser {
parse(ctx, '(');
if (parseIf(ctx, ')'))
return parseWindowFunction(ctx, null, denseRank());
return parseWindowFunction(ctx, null, null, denseRank());
// Hypothetical set function
List<Field<?>> args = parseFields(ctx);
@ -5994,7 +5995,7 @@ final class ParserImpl implements Parser {
parse(ctx, '(');
if (parseIf(ctx, ')'))
return parseWindowFunction(ctx, null, percentRank());
return parseWindowFunction(ctx, null, null, percentRank());
// Hypothetical set function
List<Field<?>> args = parseFields(ctx);
@ -6010,7 +6011,7 @@ final class ParserImpl implements Parser {
parse(ctx, '(');
if (parseIf(ctx, ')'))
return parseWindowFunction(ctx, null, cumeDist());
return parseWindowFunction(ctx, null, null, cumeDist());
// Hypothetical set function
List<Field<?>> args = parseFields(ctx);
@ -6025,7 +6026,7 @@ final class ParserImpl implements Parser {
if (parseFunctionNameIf(ctx, "ROW_NUMBER")) {
parse(ctx, '(');
parse(ctx, ')');
return parseWindowFunction(ctx, null, rowNumber());
return parseWindowFunction(ctx, null, null, rowNumber());
}
return null;
@ -6036,7 +6037,7 @@ final class ParserImpl implements Parser {
parse(ctx, '(');
int number = (int) (long) parseUnsignedInteger(ctx);
parse(ctx, ')');
return parseWindowFunction(ctx, null, ntile(number));
return parseWindowFunction(ctx, null, null, ntile(number));
}
return null;
@ -6060,7 +6061,7 @@ final class ParserImpl implements Parser {
}
}
parse(ctx, ')');
return parseWindowFunction(ctx, lead
return parseWindowFunction(ctx, null, lead
? f2 == null
? lead(f1)
: f3 == null
@ -6081,7 +6082,7 @@ final class ParserImpl implements Parser {
parse(ctx, '(');
Field<Void> arg = (Field) parseField(ctx);
parse(ctx, ')');
return parseWindowFunction(ctx, firstValue(arg), null);
return parseWindowFunction(ctx, null, firstValue(arg), null);
}
return null;
@ -6092,7 +6093,7 @@ final class ParserImpl implements Parser {
parse(ctx, '(');
Field<Void> arg = (Field) parseField(ctx);
parse(ctx, ')');
return parseWindowFunction(ctx, lastValue(arg), null);
return parseWindowFunction(ctx, null, lastValue(arg), null);
}
return null;
@ -6105,15 +6106,29 @@ final class ParserImpl implements Parser {
parse(ctx, ',');
int f2 = (int) (long) parseUnsignedInteger(ctx);
parse(ctx, ')');
return parseWindowFunction(ctx, nthValue(f1, f2), null);
return parseWindowFunction(ctx, nthValue(f1, f2), null, null);
}
return null;
}
private static final Field<?> parseWindowFunction(ParserContext ctx, WindowIgnoreNullsStep s1, WindowOverStep<?> s2) {
private static final Field<?> parseWindowFunction(ParserContext ctx, WindowFromFirstLastStep s1, WindowIgnoreNullsStep s2, WindowOverStep<?> s3) {
if (s1 != null) {
if (parseKeywordIf(ctx, "FROM FIRST") && ctx.requireProEdition())
;
else if (parseKeywordIf(ctx, "FROM LAST") && ctx.requireProEdition())
;
else
s2 = s1;
}
if (s2 != null) {
if (parseKeywordIf(ctx, "RESPECT NULLS") && ctx.requireProEdition())
@ -6125,7 +6140,7 @@ final class ParserImpl implements Parser {
;
else
s2 = s1;
s3 = s2;
}
parseKeyword(ctx, "OVER");
@ -6133,10 +6148,10 @@ final class ParserImpl implements Parser {
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=494897
Field<?> result = (nameOrSpecification instanceof Name)
? s2.over((Name) nameOrSpecification)
? s3.over((Name) nameOrSpecification)
: (nameOrSpecification instanceof WindowSpecification)
? s2.over((WindowSpecification) nameOrSpecification)
: s2.over();
? s3.over((WindowSpecification) nameOrSpecification)
: s3.over();
return result;
}