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/19 04:04:33 UTC

[groovy] branch master updated: Tweak validation for `groupby`

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 157f200  Tweak validation for `groupby`
157f200 is described below

commit 157f200054a0d53d397491247d06fcbd3b2e7d34
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Dec 19 12:04:13 2020 +0800

    Tweak validation for `groupby`
---
 .../ginq/provider/collection/GinqAstWalker.groovy  | 18 ++++++++++++++----
 .../test/org/apache/groovy/ginq/GinqTest.groovy    | 22 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 4 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 b5c4b2f..bc6d757 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
@@ -660,6 +660,10 @@ class GinqAstWalker implements GinqAstVisitor<Expression>, SyntaxErrorReportable
                             return expr
                         }
                         if (isExpression(expr, VariableExpression, PropertyExpression)) {
+                            def text = expr instanceof PropertyExpression ? ((PropertyExpression) expr).propertyAsString : expr.text
+                            if (Character.isUpperCase(text.charAt(0))) {
+                                return expr
+                            }
                             GinqAstWalker.this.collectSyntaxError(new GinqSyntaxError(
                                     "`${expr.text}` is not in the `groupby` clause",
                                     expr.getLineNumber(), expr.getColumnNumber()
@@ -668,10 +672,16 @@ class GinqAstWalker implements GinqAstVisitor<Expression>, SyntaxErrorReportable
                             if (isAggregateFunction(expr)) {
                                 return expr
                             } else {
-                                GinqAstWalker.this.collectSyntaxError(new GinqSyntaxError(
-                                        "`${expr instanceof CastExpression ? expr.expression.text : expr.text}` is not an aggregate function",
-                                        expr.getLineNumber(), expr.getColumnNumber()
-                                ))
+                                def mce = (MethodCallExpression) expr
+                                def objectExpression = mce.objectExpression
+                                def objectExpressionText = objectExpression instanceof PropertyExpression ? ((PropertyExpression) objectExpression).propertyAsString : objectExpression.text
+                                def staticMethodCall = !mce.implicitThis && Character.isUpperCase(objectExpressionText.charAt(0))
+                                if (!staticMethodCall) {
+                                    GinqAstWalker.this.collectSyntaxError(new GinqSyntaxError(
+                                            "`${expr instanceof CastExpression ? expr.expression.text : expr.text}` is not an aggregate function",
+                                            expr.getLineNumber(), expr.getColumnNumber()
+                                    ))
+                                }
                             }
                         } else if (isExpression(expr, AbstractGinqExpression)) {
                             GinqAstWalker.this.collectSyntaxError(new GinqSyntaxError(
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 5e2ef05..c589a96 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
@@ -4420,6 +4420,28 @@ class GinqTest {
     }
 
     @Test
+    void "testGinq - aggregate function - 1"() {
+        assertGinqScript '''
+            assert [3, 2, 1] == GQ {
+                from n in [-1, -2, -3]
+                groupby n
+                select Math.abs(max(n))
+            }.toList()
+        '''
+    }
+
+    @Test
+    void "testGinq - aggregate function - 2"() {
+        assertGinqScript '''
+            assert [3, 2, 1] == GQ {
+                from n in [-1, -2, -3]
+                groupby n
+                select java.lang.Math.abs(max(n))
+            }.toList()
+        '''
+    }
+
+    @Test
     void "testGinq - parallel - 1"() {
         assertGinqScript '''
 // tag::ginq_tips_08[]