Skip to content

Commit 55f31a3

Browse files
authored
GROOVY-11695: [GINQ] groupby alias can not be found in switch expression of orderby (#2252)
1 parent 092c3b7 commit 55f31a3

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ import org.apache.groovy.ginq.provider.collection.runtime.ValueBound
4545
import org.apache.groovy.ginq.provider.collection.runtime.WindowDefinition
4646
import org.apache.groovy.util.Maps
4747
import org.codehaus.groovy.GroovyBugError
48+
import org.codehaus.groovy.ast.ClassCodeExpressionTransformer
4849
import org.codehaus.groovy.ast.ClassNode
4950
import org.codehaus.groovy.ast.CodeVisitorSupport
5051
import org.codehaus.groovy.ast.Parameter
5152
import org.codehaus.groovy.ast.expr.ArgumentListExpression
5253
import org.codehaus.groovy.ast.expr.BinaryExpression
5354
import org.codehaus.groovy.ast.expr.CastExpression
55+
import org.codehaus.groovy.ast.expr.ClosureExpression
5456
import org.codehaus.groovy.ast.expr.ConstantExpression
5557
import org.codehaus.groovy.ast.expr.ConstructorCallExpression
5658
import org.codehaus.groovy.ast.expr.DeclarationExpression
@@ -1199,9 +1201,13 @@ class GinqAstWalker implements GinqAstVisitor, SyntaxErrorReportable
11991201
// The synthetic lambda parameter `__t` represents the element from the result datasource of joining, e.g. `n1` innerJoin `n2`
12001202
// The element from first datasource(`n1`) is referenced via `_t.v1`
12011203
// and the element from second datasource(`n2`) is referenced via `_t.v2`
1202-
expr = ((ListExpression) (new ListExpression(Collections.singletonList(expr)).transformExpression(new ExpressionTransformer() {
1204+
expr = ((ListExpression) (new ListExpression(Collections.singletonList(expr)).transformExpression(new ClassCodeExpressionTransformer() {
12031205
@Override
12041206
Expression transform(Expression expression) {
1207+
if (expression instanceof ClosureExpression) {
1208+
expression.visit(this)
1209+
return expression
1210+
}
12051211
Expression transformedExpression = correctVars(dataSourceExpression, lambdaParamName, expression)
12061212
if (null == transformedExpression) {
12071213
return expression
@@ -1212,6 +1218,11 @@ class GinqAstWalker implements GinqAstVisitor, SyntaxErrorReportable
12121218

12131219
return expression.transformExpression(this)
12141220
}
1221+
1222+
@Override
1223+
SourceUnit getSourceUnit() {
1224+
return sourceUnit;
1225+
}
12151226
}))).getExpression(0)
12161227

12171228
tuple(declarationExpressionList, expr)

subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6361,6 +6361,21 @@ class GinqTest {
63616361
'''
63626362
}
63636363

6364+
@Test
6365+
void "testGinq - switch - 6"() {
6366+
assertGinqScript '''
6367+
assert [[4, 1], [1, 2], [3, 1]] == GQ {
6368+
from n in [1, 1, 3, 4]
6369+
groupby n as g
6370+
orderby switch (g) {
6371+
case 4 -> 0
6372+
default -> g
6373+
}
6374+
select g, count()
6375+
}.toList()
6376+
'''
6377+
}
6378+
63646379
@Test
63656380
void "testGinqMethod - GQ - 0"() {
63666381
assertScript '''

0 commit comments

Comments
 (0)