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 2021/01/02 07:51:43 UTC

[groovy] branch master updated: Tweak validation for window function

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 7682851  Tweak validation for window function
7682851 is described below

commit 76828518c8230a4f0d97a58014c029ee206f075c
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Jan 2 15:51:16 2021 +0800

    Tweak validation for window function
---
 .../ginq/provider/collection/GinqAstWalker.groovy  | 13 ++++++++++++
 .../test/org/apache/groovy/ginq/GinqTest.groovy    | 22 ++++++++++++++++++++
 .../org/apache/groovy/ginq/GinqErrorTest.groovy    | 24 ++++++++++++++++++++++
 3 files changed, 59 insertions(+)

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 25f0e73..e0c416b 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
@@ -837,6 +837,19 @@ class GinqAstWalker implements GinqAstVisitor<Expression>, SyntaxErrorReportable
         }
 
         if (rangeExpr) {
+            if (!orderExpr) {
+                this.collectSyntaxError(new GinqSyntaxError(
+                        "`orderby` is expected when using `range`",
+                        rangeExpr.getLineNumber(), rangeExpr.getColumnNumber()
+                ))
+            }
+            if (((ArgumentListExpression) orderExpr).getExpressions().size() != 1) {
+                this.collectSyntaxError(new GinqSyntaxError(
+                        "Only one field is expected in the `orderby` clause when using `range`",
+                        orderExpr.getLineNumber(), orderExpr.getColumnNumber()
+                ))
+            }
+
             def valueBoundCtorCallExpression = ctorX(VALUEBOUND_TYPE, rangeExpr)
             argumentExpressionList << valueBoundCtorCallExpression
         }
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 e5cb322..5c7a247 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
@@ -5439,6 +5439,28 @@ class GinqTest {
         '''
     }
 
+    @Test
+    void "testGinq - window - 67"() {
+        assertGinqScript '''
+            assert [[1, 3, 4], [1, 3, 4], [2, 3, 4], [5, 2, 10], [5, 2, 10]] == GQ {
+                from n in [1, 1, 2, 5, 5]
+                select n, (count() over(orderby n range -1, 1)), 
+                          (sum(n) over(orderby n range -1, 1))
+            }.toList()
+        '''
+    }
+
+    @Test
+    void "testGinq - window - 68"() {
+        assertGinqScript '''
+            assert [[1, 3, 4], [1, 3, 4], [2, 3, 4], [5, 2, 10], [5, 2, 10]] == GQ {
+                from n in [1, 1, 2, 5, 5]
+                select n, (count() over(orderby n in desc range -1, 1)), 
+                          (sum(n) over(orderby n in desc range -1, 1))
+            }.toList()
+        '''
+    }
+
     private static void assertGinqScript(String script) {
         String deoptimizedScript = script.replaceAll(/\bGQ\s*[{]/, 'GQ(optimize:false) {')
         List<String> scriptList = [deoptimizedScript, script]
diff --git a/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/GinqErrorTest.groovy b/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/GinqErrorTest.groovy
index 70227fe..1e80fef 100644
--- a/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/GinqErrorTest.groovy
+++ b/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/GinqErrorTest.groovy
@@ -454,4 +454,28 @@ class GinqErrorTest {
 
         assert err.toString().contains('Unknown window clause: `order` @ line 3, column 51.')
     }
+
+    @Test
+    void "testGinq - window - 60"() {
+        def err = shouldFail '''\
+            GQ {
+                from n in [1, 1, 2, 2]
+                select n, (sum(n) over(range 1, 2))
+            }.toList()
+        '''
+
+        assert err.toString().contains('`orderby` is expected when using `range` @ line 3, column 46.')
+    }
+
+    @Test
+    void "testGinq - window - 61"() {
+        def err = shouldFail '''\
+            GQ {
+                from n in [1, 1, 2, 2]
+                select n, (sum(n) over(orderby n, n + 1 range 1, 2))
+            }.toList()
+        '''
+
+        assert err.toString().contains('Only one field is expected in the `orderby` clause when using `range` @ line 3, column 48.')
+    }
 }