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/26 15:18:04 UTC

[groovy] branch master updated: Tweak `lead` and `lag`

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 33b2a64  Tweak `lead` and `lag`
33b2a64 is described below

commit 33b2a6459e9befde32c1db969ad40233c113f790
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Dec 26 23:17:44 2020 +0800

    Tweak `lead` and `lag`
---
 .../ginq/provider/collection/GinqAstWalker.groovy  | 10 +++++-
 .../ginq/provider/collection/runtime/Window.java   | 36 +++++++++++++++++++---
 .../provider/collection/runtime/WindowImpl.java    | 11 ++++---
 .../groovy-ginq/src/spec/doc/ginq-userguide.adoc   | 11 +++++++
 .../test/org/apache/groovy/ginq/GinqTest.groovy    | 24 +++++++++++++++
 5 files changed, 82 insertions(+), 10 deletions(-)

diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
index 05ca72f..162166c 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
@@ -672,7 +672,8 @@ class GinqAstWalker implements GinqAstVisitor<Expression>, SyntaxErrorReportable
 
                             Expression result = null
                             if (windowFunctionMethodCallExpression.methodAsString in ['lead', 'lag', 'firstValue', 'lastValue']) {
-                                def windowFunctionLambdaCode = ((ArgumentListExpression) windowFunctionMethodCallExpression.arguments).getExpression(0)
+                                def argumentListExpression = (ArgumentListExpression) windowFunctionMethodCallExpression.arguments
+                                def windowFunctionLambdaCode = argumentListExpression.getExpression(0)
                                 def windowFunctionLambdaName = '__wfp'
                                 def rootObjectExpression = findRootObjectExpression(windowFunctionLambdaCode)
 
@@ -698,6 +699,13 @@ class GinqAstWalker implements GinqAstVisitor<Expression>, SyntaxErrorReportable
                                         block(stmt(windowFunctionLambdaCode))
                                 )
 
+                                if (windowFunctionMethodCallExpression.methodAsString in ['lead', 'lag']) {
+                                    List<Expression> exprList = argumentListExpression.getExpressions()
+                                    if (exprList.size() > 1) {
+                                        argumentExpressionList.addAll(exprList.subList(1, exprList.size()))
+                                    }
+                                }
+
                                 def windowDefinitionFactoryMethodCallExpression = constructWindowDefinitionFactoryMethodCallExpression(expression, dataSourceExpression)
                                 Expression newObjectExpression = callX(wqVar, 'over', args(
                                         currentRecordVar,
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Window.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Window.java
index cb32452..7df13e9 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Window.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Window.java
@@ -44,7 +44,20 @@ public interface Window<T> extends Queryable<T> {
      * @since 4.0.0
      */
     default <V> V lead(Function<? super T, ? extends V> extractor) {
-        return lead(extractor, 1);
+        return lead(extractor, 1, null);
+    }
+
+    /**
+     * Returns the next value in the window, similar to SQL's {@code lead()}
+     *
+     * @param extractor field extractor
+     * @param lead the offset
+     * @param <V> the type of field
+     * @return the next value
+     * @since 4.0.0
+     */
+    default <V> V lead(Function<? super T, ? extends V> extractor, long lead) {
+        return lead(extractor, lead, null);
     }
 
     /**
@@ -53,10 +66,11 @@ public interface Window<T> extends Queryable<T> {
      * @param <V> the type of field
      * @param extractor field extractor
      * @param lead the offset
+     * @param def the default value
      * @return the next value by {@code lead}
      * @since 4.0.0
      */
-    <V> V lead(Function<? super T, ? extends V> extractor, long lead);
+    <V> V lead(Function<? super T, ? extends V> extractor, long lead, V def);
 
     /**
      * Returns the previous value in the window, similar to SQL's {@code lag()}
@@ -67,7 +81,20 @@ public interface Window<T> extends Queryable<T> {
      * @since 4.0.0
      */
     default <V> V lag(Function<? super T, ? extends V> extractor) {
-        return lag(extractor, 1);
+        return lag(extractor, 1, null);
+    }
+
+    /**
+     * Returns the previous value in the window, similar to SQL's {@code lag()}
+     *
+     * @param extractor field extractor
+     * @param lag the offset
+     * @param <V> the type of field
+     * @return the previous value
+     * @since 4.0.0
+     */
+    default <V> V lag(Function<? super T, ? extends V> extractor, long lag) {
+        return lag(extractor, lag, null);
     }
 
     /**
@@ -76,10 +103,11 @@ public interface Window<T> extends Queryable<T> {
      * @param <V> the type of field
      * @param extractor field extractor
      * @param lag the offset
+     * @param def the default value
      * @return the previous value by {@code lag}
      * @since 4.0.0
      */
-    <V> V lag(Function<? super T, ? extends V> extractor, long lag);
+    <V> V lag(Function<? super T, ? extends V> extractor, long lag, V def);
 
     /**
      * Returns the first value in the window
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowImpl.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowImpl.java
index 8e5ec92..536e06f 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowImpl.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowImpl.java
@@ -65,20 +65,21 @@ class WindowImpl<T, U extends Comparable<? super U>> extends QueryableCollection
     }
 
     @Override
-    public <V> V lead(Function<? super T, ? extends V> extractor, long lead) {
-        V field = null;
+    public <V> V lead(Function<? super T, ? extends V> extractor, long lead, V def) {
+        V field;
         if (0 == lead) {
             field = extractor.apply(currentRecord);
         } else if (0 <= index + lead && index + lead < this.size()) {
             field = extractor.apply(this.toList().get((int) index + (int) lead));
+        } else {
+            field = def;
         }
-
         return field;
     }
 
     @Override
-    public <V> V lag(Function<? super T, ? extends V> extractor, long lag) {
-        return lead(extractor, -lag);
+    public <V> V lag(Function<? super T, ? extends V> extractor, long lag, V def) {
+        return lead(extractor, -lag, def);
     }
 
     @Override
diff --git a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
index 647cd2f..a2bac66 100644
--- a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
+++ b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
@@ -456,6 +456,17 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_05
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_06,indent=0]
 ----
 
+The offset can be specified other than the default offset `1`:
+[source, groovy]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_20,indent=0]
+----
+
+The default value can be returned when the index specified by offset is out of window, e.g. `'NONE'`:
+[source, groovy]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_21,indent=0]
+----
 ==== `firstValue` & `lastValue`
 [source, groovy]
 ----
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 1b209ba..d75bd63 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
@@ -5094,6 +5094,30 @@ class GinqTest {
         '''
     }
 
+    @Test
+    void "testGinq - window - 39"() {
+        assertGinqScript '''
+// tag::ginq_winfunction_20[]
+            assert [[2, null, null], [1, 3, null], [3, null, 1]] == GQ {
+                from n in [2, 1, 3]
+                select n, (lead(n, 2) over(orderby n)), (lag(n, 2) over(orderby n))
+            }.toList()
+// end::ginq_winfunction_20[]
+        '''
+    }
+
+    @Test
+    void "testGinq - window - 40"() {
+        assertGinqScript '''
+// tag::ginq_winfunction_21[]
+            assert [[2, 'NONE', 'NONE'], [1, 3, 'NONE'], [3, 'NONE', 1]] == GQ {
+                from n in [2, 1, 3]
+                select n, (lead(n, 2, 'NONE') over(orderby n)), (lag(n, 2, 'NONE') over(orderby n))
+            }.toList()
+// end::ginq_winfunction_21[]
+        '''
+    }
+
     private static void assertGinqScript(String script) {
         String deoptimizedScript = script.replaceAll(/\bGQ\s*[{]/, 'GQ(optimize:false) {')
         List<String> scriptList = [deoptimizedScript, script]