[ 
https://issues.apache.org/jira/browse/GROOVY-12833?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18150635#comment-18150635
 ] 

ASF GitHub Bot commented on GROOVY-12833:
-----------------------------------------

paulk-asert opened a new pull request, #3360:
URL: https://github.com/apache/groovy/pull/3360

   …by select fails with BUG!




> GINQ: aliasing a method call on an aggregate in a groupby select fails with 
> "BUG! exception in phase 'conversion'"
> ------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12833
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12833
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Paul King
>            Priority: Major
>
> When a {{select}} clause following a {{groupby}} contains a method call 
> involving an aggregate function *and* that column is given an alias, 
> compilation fails with a {{BUG!}} exception instead of producing a result.
> {code:groovy}
> def nums = [1, 2, 3, 4]
> println GQ {
>     from n in nums
>     groupby n % 2 as parity
>     select parity, Math.round(avg(n)) as mean
> }
> {code}
> fails with:
> {noformat}
> BUG! exception in phase 'conversion' in source unit '...' Cannot cast object 
> 'org.codehaus.groovy.ast.expr.CastExpression@...[Math.round(this.avg(n)) as 
> mean]' with class 'org.codehaus.groovy.ast.expr.CastExpression' to class 
> 'org.codehaus.groovy.ast.expr.MethodCallExpression'
> Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: 
> Cannot cast object ...
> {noformat}
> The same happens for {{avg\(n).round() as mean}} and {{sum\(n).toString() as 
> total}}.
> Things that work:
> * without the alias: {{select parity, Math.round(avg\(n))}}
> * aggregate with alias: {{select parity, avg\(n) as mean}}
> * a cast instead of a method call: {{select parity, (avg\(n) as int) as mean}}
> The same crash also hides an intended syntax error. For a non-aggregate 
> method call with an alias:
> {code:groovy}
> def hello(x) { x }
> GQ { from n in [1, 2, 3] groupby n select n, hello(n) as h }.toList()
> {code}
> the expected error is "{{`this.hello\(n)` is not an aggregate function}}" (as 
> reported without the alias), but the same {{BUG!}} is thrown instead.
> *Cause:* in {{GinqAstWalker.validateGroupCols}}, {{isExpression(expr, 
> MethodCallExpression)}} unwraps the {{CastExpression}} that an alias creates, 
> but the following line casts the still-wrapped {{expr}} directly: 
> {{((MethodCallExpression) expr).implicitThis}}. The error message on the next 
> line already allows for a {{CastExpression}}, so only the unwrap is missing.
> Reproduced with 6.0.0 and 7.0.0-SNAPSHOT (master, 2026-11-24).
> The fix
> {code:groovy}
> def hello(x) { x }
> GQ { from n in [1, 2, 3] groupby n select n, hello(n) as h }.toList()
> {code}
> the expected error is "{{`this.hello\(n)` is not an aggregate function}}" (as 
> reported without the alias), but the same {{BUG!}} is thrown instead.
> *Cause:* in {{GinqAstWalker.validateGroupCols}}, {{isExpression(expr, 
> MethodCallExpression)}} unwraps the {{CastExpression}} that an alias creates, 
> but the following line casts the still-wrapped {{expr}} directly: 
> {{((MethodCallExpression) expr).implicitThis}}. The error message on the next 
> line already allows for a {{CastExpression}}, so only the unwrap is missing.
> Reproduced with 6.0.0 and 7.0.0-SNAPSHOT (master, 2026-11-24).



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to