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

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

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

   …roperty name starts with a capital letter




> GINQ: implicit aggregates give wrong results when the property name starts 
> with a capital letter
> ------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12847
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12847
>             Project: Groovy
>          Issue Type: Bug
>          Components: GINQ
>    Affects Versions: 6.0.1
>            Reporter: Paul King
>            Priority: Major
>
> Implicit GINQ aggregates ({{sum}}, {{avg}}, {{min}}, {{max}}, {{count}}) over 
> a property whose name starts with a capital letter silently give wrong 
> results. Capitalised names are common with data such as CSV files with header 
> rows.
> h3. Reproducer
> {code:groovy}
> def rows = [[k: 'a', v: 3.0, V: 3.0], [k: 'a', v: 1.0, V: 1.0]]
> assert GQ { from r in rows select sum(r.v) }.toList() == [4.0]     // ok
> assert GQ { from r in rows select sum(r.V) }.toList() == [4.0]     // fails: 
> [6.0]
> {code}
> {{groupby}} isn't required, but the same happens with it. Results on 6.0.1, 
> using the rows above (with {{groupby r.k}}) unless noted:
> ||Query||lowercase {{v}}||capital {{V}}||
> |{{sum}}|4.0|6.0 (wrong)|
> |{{min}}|1.0|3.0 (wrong)|
> |{{count}}, rows null then 5.0|1|0 (wrong)|
> |{{sum}} after an inner join of the rows with themselves on {{k}}|8.0|12.0 
> (wrong)|
> |{{select r.k, r.x}} where {{x}} is not in the {{groupby}}|compile error, as 
> expected|accepted, returns 3.0|
> {{where}}, {{groupby r.V}}, {{over()}} windows and {{agg}} gave the expected 
> results.
> h3. Cause
> From reading the source, {{GinqAstWalker.correctVars}} has:
> {code:groovy}
> if (expression instanceof PropertyExpression) {
>     if (Character.isUpperCase(expression.propertyAsString.charAt(0))) {
>         return transformedExpression   // null
>     }
>     if (isExternalVariable(findRootObjectExpression(expression))) {
>         return transformedExpression
>     }
> }
> {code}
> Returning {{null}} tells the caller to leave the expression and its children 
> alone. So in {{sum(r.V)}} the {{r}} isn't rewritten to the aggregate lambda's 
> parameter. It still compiles because the enclosing lambda declares {{r}} from 
> {{__sourceRecord}}, so every iteration reads the group's representative row.
> The check was added in 5d6680a05a, apparently so that the {{java.lang.Math}} 
> part of {{max(java.lang.Math.abs\(n))}} is left alone. The 
> {{isExternalVariable}} check added shortly after in d1951f6873 seems to cover 
> that case, as {{java}} isn't a query alias.
> The last row of the table appears to come from a similar check in 
> {{validateGroupCols}}.
> The same code is on master, GROOVY_6_0_X, GROOVY_5_0_X and GROOVY_4_0_X, 
> though only 6.0.x has been run.
> h3. Possible fix
> Remove the capital letter check in {{correctVars}}, relying on 
> {{isExternalVariable}}, and review whether the one in {{validateGroupCols}} 
> can also rely on its existing alias check. Not yet tested.
> h3. Workarounds
> {code:groovy}
> assert GQ { from r in rows groupby r.k into g select g.sum(r -> r.V) 
> }.toList() == [4.0]
> assert GQ { from r in rows select sum(r['V']) }.toList() == [4.0]
> {code}



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

Reply via email to