You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/12/27 13:17:51 UTC

[groovy] branch master updated: Tweak window definition and doco

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new bca998c  Tweak window definition and doco
bca998c is described below

commit bca998c083b2fb5b0f6b4a93a31b39fc352cd8f9
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Dec 27 21:17:25 2020 +0800

    Tweak window definition and doco
---
 .../provider/collection/runtime/WindowDefinition.java    | 14 +++++++++++++-
 .../collection/runtime/WindowDefinitionImpl.java         |  9 +++++++++
 subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc | 16 ++++++++++------
 .../src/spec/test/org/apache/groovy/ginq/GinqTest.groovy | 15 +++++++++++++++
 4 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinition.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinition.java
index 8ff7b5c..b975629 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinition.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinition.java
@@ -34,6 +34,18 @@ public interface WindowDefinition<T, U extends Comparable<? super U>> {
     /**
      * Factory method to create {@link WindowDefinition} instance
      *
+     * @param <T> the type of {@link Queryable} element
+     * @param <U> the type of field to sort
+     * @return the {@link WindowDefinition} instance
+     * @since 4.0.0
+     */
+    static <T, U extends Comparable<? super U>> WindowDefinition<T, U> of() {
+        return new WindowDefinitionImpl<>();
+    }
+
+    /**
+     * Factory method to create {@link WindowDefinition} instance
+     *
      * @param partitionBy partition definition
      * @param <T> the type of {@link Queryable} element
      * @param <U> the type of field to sort
@@ -110,7 +122,7 @@ public interface WindowDefinition<T, U extends Comparable<? super U>> {
      * @since 4.0.0
      */
     static <T, U extends Comparable<? super U>> WindowDefinition<T, U> of(List<Queryable.Order<? super T, ? extends U>> orderBy, RowBound rows) {
-        return new WindowDefinitionImpl<>((T t) -> Queryable.NULL, orderBy, rows);
+        return new WindowDefinitionImpl<>(orderBy, rows);
     }
 
     /**
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinitionImpl.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinitionImpl.java
index 0e5ff5b..4a767d4 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinitionImpl.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinitionImpl.java
@@ -44,6 +44,10 @@ class WindowDefinitionImpl<T, U extends Comparable<? super U>> implements Window
         this.range = range;
     }
 
+    public WindowDefinitionImpl() {
+        this((T t) -> Queryable.NULL, Collections.emptyList(), RowBound.DEFAULT, null);
+    }
+
     public WindowDefinitionImpl(Function<? super T, ?> partitionBy) {
         this(partitionBy, Collections.emptyList(), RowBound.DEFAULT, null);
     }
@@ -56,6 +60,11 @@ class WindowDefinitionImpl<T, U extends Comparable<? super U>> implements Window
         this(partitionBy, orderBy, RowBound.DEFAULT, null);
     }
 
+    public WindowDefinitionImpl(List<Queryable.Order<? super T, ? extends U>> orderBy,
+                                RowBound rows) {
+        this((T t) -> Queryable.NULL, orderBy, rows, null);
+    }
+
     public WindowDefinitionImpl(Function<? super T, ?> partitionBy, List<Queryable.Order<? super T, ? extends U>> orderBy,
                                 RowBound rows) {
         this(partitionBy, orderBy, rows, null);
diff --git a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
index 8e5fba1..5260034 100644
--- a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
+++ b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
@@ -280,7 +280,7 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_grouping_09,in
 ----
 
 ===== Aggregate Functions
-GINQ supports some built-in aggregate functions, e.g.
+GINQ provides some built-in aggregate functions, e.g.
 `count`, `min`, `max`, `sum`, `avg`, `median` and the most powerful function `agg`.
 [NOTE]
 `count(...)`, `min(...)`, `max(...)`, `avg(...)` and `median(...)` just operate on non-`null` values,
@@ -415,11 +415,11 @@ Window can be defined by `partitionby`, `orderby` and `rows`:
 ```sql
 over(
     [partitionby <expression> (, <expression>)*]
-    [orderby <expression> (, <expression>)*]
-    [rows <expression>, <expression>]
+    [orderby <expression> (, <expression>)*
+       [rows <expression>, <expression>]]
 )
 ```
-Also, GINQ supports some built-in window functions, e.g.
+Also, GINQ provides some built-in window functions, e.g.
 `rowNumber`, `rank`, `denseRank`, `lead`, `lag`, `firstValue`, `lastValue`, `min`, `max`, `count`, `sum`, `avg`, `median`, etc.
 
 ===== `rowNumber`
@@ -552,18 +552,22 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_15
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_11,indent=0]
 ----
 
-===== `min` and `max`
+===== `min`, `max`, `count`, `sum`, `avg` and `median`
 [source, groovy]
 ----
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_22,indent=0]
 ----
 
-===== `count`, `sum`, `avg` and `median`
 [source, groovy]
 ----
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_23,indent=0]
 ----
 
+[source, groovy]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_26,indent=0]
+----
+
 === GINQ Tips
 ==== Row Number
 `_rn` is the implicit variable representing row number for each record in the result set. It starts with `0`
diff --git a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
index 2b44b17..163f53e 100644
--- a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
+++ b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
@@ -5175,6 +5175,21 @@ class GinqTest {
         '''
     }
 
+    @Test
+    void "testGinq - window - 45"() {
+        assertGinqScript '''
+// tag::ginq_winfunction_26[]
+            assert [[2, 6, 3, 1, 3], [1, 6, 3, 1, 3], [3, 6, 3, 1, 3]] == GQ {
+                from n in [2, 1, 3]
+                select n, (sum(n) over()), 
+                          (max(n) over()), 
+                          (min(n) over()),
+                          (count(n) over())
+            }.toList()
+// end::ginq_winfunction_26[]
+        '''
+    }
+
     private static void assertGinqScript(String script) {
         String deoptimizedScript = script.replaceAll(/\bGQ\s*[{]/, 'GQ(optimize:false) {')
         List<String> scriptList = [deoptimizedScript, script]