GoncaloCoutoDosSantos commented on code in PR #4983:
URL: https://github.com/apache/calcite/pull/4983#discussion_r3348892093
##########
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAggregateBase.java:
##########
@@ -145,6 +146,32 @@ protected void implementLambdaFactory(BlockBuilder builder,
requireNonNull(agg.accumulatorAdder,
"agg.accumulatorAdder")))));
continue;
}
+ // Check if all collation field indices are within the input row type
bounds.
+ // When using LISTAGG(DISTINCT col1) WITHIN GROUP (ORDER BY col2)
where col2 is not
+ // a GROUP BY column, col2's index may exceed the re-grouped input row
type bounds.
+ // In such cases, we fall back to non-sorted aggregation instead of
crashing.
+ boolean validCollation = true;
+ for (final RelFieldCollation fieldCollation :
agg.call.collation.getFieldCollations()) {
+ if (fieldCollation.getFieldIndex() >=
inputPhysType.getRowType().getFieldCount()) {
+ validCollation = false;
+ break;
+ }
+ }
+
+ if (!validCollation) {
Review Comment:
FYI, if you're going to change AggregateExpandDistinctAggregatesRule to fix
this bug, you'll also should to update rewriteUsingGroupingSets, since the same
issue can be triggered by a query like:
```
SELECT deptno,
LISTAGG(DISTINCT ename) WITHIN GROUP (ORDER BY sal),
LISTAGG(DISTINCT deptno) WITHIN GROUP (ORDER BY ename)
FROM emp
GROUP BY deptno;```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]