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/10/22 02:39:23 UTC

[groovy] branch GROOVY-8258 updated: GROOVY-9787: transform variables in `orderby` properly

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

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


The following commit(s) were added to refs/heads/GROOVY-8258 by this push:
     new 1c3a710  GROOVY-9787: transform variables in `orderby` properly
1c3a710 is described below

commit 1c3a7104aff374665ac31a98889e5ca431c827bc
Author: Daniel Sun <su...@apache.org>
AuthorDate: Thu Oct 22 10:38:49 2020 +0800

    GROOVY-9787: transform variables in `orderby` properly
---
 .../linq/provider/collection/GinqAstWalker.groovy  | 10 ++-
 .../groovy/org/apache/groovy/linq/GinqTest.groovy  | 75 ++++++++++++++++++++++
 2 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/provider/collection/GinqAstWalker.groovy b/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/provider/collection/GinqAstWalker.groovy
index 2393501..f158ef5 100644
--- a/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/provider/collection/GinqAstWalker.groovy
+++ b/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/provider/collection/GinqAstWalker.groovy
@@ -433,6 +433,8 @@ class GinqAstWalker implements GinqVisitor<Object>, SyntaxErrorReportable {
         def correctVars = { Expression expression ->
             Expression transformedExpression = null
             if (expression instanceof VariableExpression) {
+                if (expression.isThisExpression()) return expression
+
                 if (isGroup) { //  groupby
                     // in #1, we will correct receiver of built-in aggregate functions
                     // the correct receiver is `__t.v2`, so we should not replace `__t` here
@@ -454,7 +456,7 @@ class GinqAstWalker implements GinqVisitor<Object>, SyntaxErrorReportable {
                 if (isGroup) { // groupby
                     if (expression.implicitThis) {
                         String methodName = expression.methodAsString
-                        if ('count' == methodName && ((TupleExpression) expression.arguments).getExpressions().isEmpty()) {
+                        if ('count' == methodName && ((TupleExpression) expression.arguments).getExpressions().isEmpty()) { // Similar to count(*) in SQL
                             visitingAggregateFunction = true
                             expression.objectExpression = propX(new VariableExpression(lambdaParamName), 'v2')
                             transformedExpression = expression
@@ -480,6 +482,10 @@ class GinqAstWalker implements GinqVisitor<Object>, SyntaxErrorReportable {
             return expression
         }
 
+        // (1) correct itself
+        expr = correctVars(expr)
+
+        // (2) correct its children nodes
         // The synthetic lambda parameter `__t` represents the element from the result datasource of joining, e.g. `n1` innerJoin `n2`
         // The element from first datasource(`n1`) is referenced via `_t.v1`
         // and the element from second datasource(`n2`) is referenced via `_t.v2`
@@ -495,7 +501,7 @@ class GinqAstWalker implements GinqVisitor<Object>, SyntaxErrorReportable {
             }
         })
 
-        return correctVars(expr)
+        return expr
     }
 
     private static Expression findRootObjectExpression(Expression expression) {
diff --git a/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/GinqTest.groovy b/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/GinqTest.groovy
index 30763b7..5cdbc48 100644
--- a/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/GinqTest.groovy
+++ b/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/GinqTest.groovy
@@ -1537,6 +1537,81 @@ class GinqTest {
     }
 
     @Test
+    void "testGinq - from groupby select - 4"() {
+        assertScript '''
+            @groovy.transform.EqualsAndHashCode
+            class Person {
+                String name
+                int weight
+                String gender
+                
+                Person(String name, int weight, String gender) {
+                    this.name = name
+                    this.weight = weight
+                    this.gender = gender
+                }
+            }
+            def persons = [new Person('Linda', 100, 'Female'), new Person('Daniel', 135, 'Male'), new Person('David', 121, 'Male')]
+            assert [['Female', 1], ['Male', 2]] == GINQ {
+                from p in persons
+                groupby p.gender
+                orderby count(p)
+                select p.gender, count(p)
+            }.toList()
+        '''
+    }
+
+    @Test
+    void "testGinq - from groupby select - 5"() {
+        assertScript '''
+            @groovy.transform.EqualsAndHashCode
+            class Person {
+                String name
+                int weight
+                String gender
+                
+                Person(String name, int weight, String gender) {
+                    this.name = name
+                    this.weight = weight
+                    this.gender = gender
+                }
+            }
+            def persons = [new Person('Linda', 100, 'Female'), new Person('Daniel', 135, 'Male'), new Person('David', 121, 'Male')]
+            assert [['Female', 1], ['Male', 2]] == GINQ {
+                from p in persons
+                groupby p.gender
+                orderby count(p.gender)
+                select p.gender, count(p.gender)
+            }.toList()
+        '''
+    }
+
+    @Test
+    void "testGinq - from groupby select - 6"() {
+        assertScript '''
+            @groovy.transform.EqualsAndHashCode
+            class Person {
+                String name
+                int weight
+                String gender
+                
+                Person(String name, int weight, String gender) {
+                    this.name = name
+                    this.weight = weight
+                    this.gender = gender
+                }
+            }
+            def persons = [new Person('Linda', 100, 'Female'), new Person('Daniel', 135, 'Male'), new Person('David', 121, 'Male')]
+            assert [['Female', 1], ['Male', 2]] == GINQ {
+                from p in persons
+                groupby p.gender
+                orderby count(p.name)
+                select p.gender, count(p.name)
+            }.toList()
+        '''
+    }
+
+    @Test
     void "testGinq - from where groupby select - 1"() {
         assertScript '''
             assert [[1, 2], [6, 3]] == GINQ {