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

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

commit f6ec7814d5dde7df85e3a7e351a9e931c1b0e22d
Author: Daniel Sun <sun...@apache.org>
AuthorDate: Sat Jun 7 21:01:22 2025 +0900

    GROOVY-11695: [GINQ] `groupby` alias can not be found in `switch` 
expression of `orderby`
---
 .../groovy/ginq/provider/collection/GinqAstWalker.groovy  | 13 ++++++++++++-
 .../src/spec/test/org/apache/groovy/ginq/GinqTest.groovy  | 15 +++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

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 a658b96ac6..9417f939f0 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
@@ -45,12 +45,14 @@ import 
org.apache.groovy.ginq.provider.collection.runtime.ValueBound
 import org.apache.groovy.ginq.provider.collection.runtime.WindowDefinition
 import org.apache.groovy.util.Maps
 import org.codehaus.groovy.GroovyBugError
+import org.codehaus.groovy.ast.ClassCodeExpressionTransformer
 import org.codehaus.groovy.ast.ClassNode
 import org.codehaus.groovy.ast.CodeVisitorSupport
 import org.codehaus.groovy.ast.Parameter
 import org.codehaus.groovy.ast.expr.ArgumentListExpression
 import org.codehaus.groovy.ast.expr.BinaryExpression
 import org.codehaus.groovy.ast.expr.CastExpression
+import org.codehaus.groovy.ast.expr.ClosureExpression
 import org.codehaus.groovy.ast.expr.ConstantExpression
 import org.codehaus.groovy.ast.expr.ConstructorCallExpression
 import org.codehaus.groovy.ast.expr.DeclarationExpression
@@ -1199,9 +1201,13 @@ class GinqAstWalker implements 
GinqAstVisitor<Expression>, SyntaxErrorReportable
         // 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`
-        expr = ((ListExpression) (new 
ListExpression(Collections.singletonList(expr)).transformExpression(new 
ExpressionTransformer() {
+        expr = ((ListExpression) (new 
ListExpression(Collections.singletonList(expr)).transformExpression(new 
ClassCodeExpressionTransformer() {
             @Override
             Expression transform(Expression expression) {
+                if (expression instanceof ClosureExpression) {
+                    expression.visit(this)
+                    return expression
+                }
                 Expression transformedExpression = 
correctVars(dataSourceExpression, lambdaParamName, expression)
                 if (null == transformedExpression) {
                     return expression
@@ -1212,6 +1218,11 @@ class GinqAstWalker implements 
GinqAstVisitor<Expression>, SyntaxErrorReportable
 
                 return expression.transformExpression(this)
             }
+
+            @Override
+            SourceUnit getSourceUnit() {
+                return sourceUnit;
+            }
         }))).getExpression(0)
 
         tuple(declarationExpressionList, expr)
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 dad032f2c6..ea382d959b 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
@@ -6361,6 +6361,21 @@ class GinqTest {
         '''
     }
 
+    @Test
+    void "testGinq - switch - 6"() {
+        assertGinqScript '''
+            assert [[4, 1], [1, 2], [3, 1]] == GQ {
+                from n in [1, 1, 3, 4]
+                groupby n as g
+                orderby switch (g) {
+                    case 4 -> 0
+                    default -> g
+                }
+                select g, count()
+            }.toList()
+        '''
+    }
+
     @Test
     void "testGinqMethod - GQ - 0"() {
         assertScript '''

Reply via email to