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 55f31a3aec GROOVY-11695: [GINQ] `groupby` alias can not be found in
`switch` expression of `orderby` (#2252)
55f31a3aec is described below
commit 55f31a3aec6cdab1f80aa76d27af08e2c621876d
Author: Daniel Sun <[email protected]>
AuthorDate: Sun Jun 8 17:34:15 2025 +0800
GROOVY-11695: [GINQ] `groupby` alias can not be found in `switch`
expression of `orderby` (#2252)
---
.../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 '''