diff --git a/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.11.txt b/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.11.txt index 7c1a1f7fe8..4e9ba77ac0 100644 --- a/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.11.txt +++ b/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.11.txt @@ -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 diff --git a/jOOQ/src/main/java/org/jooq/WindowFromFirstLastStep.java b/jOOQ/src/main/java/org/jooq/WindowFromFirstLastStep.java new file mode 100644 index 0000000000..db0edd15d8 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/WindowFromFirstLastStep.java @@ -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. + *

+ * Example:

+ * field.firstValue()
+ *      .ignoreNulls()
+ *      .over()
+ *      .partitionBy(AUTHOR_ID)
+ *      .orderBy(PUBLISHED_IN.asc())
+ *      .rowsBetweenUnboundedPreceding()
+ *      .andUnboundedFollowing()
+ * 
+ * + * @param The function return type + * @author Lukas Eder + */ +public interface WindowFromFirstLastStep extends WindowIgnoreNullsStep { + + + + + + + + + + + + + + + + +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 5091fdfbfb..cdbbfc75e1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -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 nth_value(field) over ([analytic clause]) function. */ @Support({ FIREBIRD_3_0, MYSQL_8_0, POSTGRES }) - public static WindowIgnoreNullsStep nthValue(Field field, int nth) { + public static WindowFromFirstLastStep nthValue(Field field, int nth) { return nthValue(field, val(nth)); } @@ -17953,7 +17954,7 @@ public class DSL { * The nth_value(field) over ([analytic clause]) function. */ @Support({ FIREBIRD_3_0, MYSQL_8_0, POSTGRES }) - public static WindowIgnoreNullsStep nthValue(Field field, Field nth) { + public static WindowFromFirstLastStep nthValue(Field field, Field nth) { return new org.jooq.impl.Function("nth_value", nullSafeDataType(field), nullSafe(field), nullSafe(nth)); } diff --git a/jOOQ/src/main/java/org/jooq/impl/Function.java b/jOOQ/src/main/java/org/jooq/impl/Function.java index b9fcf3a34b..0d0736f753 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Function.java +++ b/jOOQ/src/main/java/org/jooq/impl/Function.java @@ -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 extends AbstractField implements ArrayAggOrderByStep, AggregateFunction, // and for window function behaviour - WindowIgnoreNullsStep, + WindowFromFirstLastStep, WindowPartitionByStep, WindowRowsStep, WindowRowsAndStep @@ -147,8 +151,8 @@ class Function extends AbstractField 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 extends AbstractField 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 extends AbstractField 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 extends AbstractField implements + + + + + + + + + + diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index 9a89ab47de..576394c0a4 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -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> 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> 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> 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> 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 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 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; }