vlsi commented on a change in pull request #1756: [CALCITE-1824] GROUP_ID returns wrong result URL: https://github.com/apache/calcite/pull/1756#discussion_r366212096
########## File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java ########## @@ -3039,6 +3040,113 @@ protected final void createAggImpl( } } + /** + * The {@code GROUP_ID()} function is used to distinguish duplicate groups. + * However, as Aggregate normalizes group sets (i.e., sorting, redundancy removal), + * this information is lost in RelNode. Therefore, it is impossible to + * implement the function in runtime. + * + * To fill this gap, an aggregation query that contains {@code GROUP_ID()} function + * will generally be rewritten into UNION when converting to RelNode. + * + * Also see the discussion in JIRA + * <a href="https://issues.apache.org/jira/browse/CALCITE-1824">[CALCITE-1824] + * GROUP_ID returns wrong result</a>. + */ + private RelNode rewriteAggregateWithGroupId(Blackboard bb, + AggregatingSelectScope.Resolved r, AggConverter converter) { + final List<AggregateCall> aggregateCalls = converter.getAggCalls(); + final ImmutableBitSet groupSet = r.groupSet; + final Map<ImmutableBitSet, Integer> groupSetCount = r.groupSetCount; + + final List<String> fieldNamesIfNoRewrite = createAggregate(bb, groupSet, + r.groupSets, aggregateCalls).getRowType().getFieldNames(); + + // For every GROUP_ID value, collect its group sets in map + // E.g., GROUPING SETS (a, a, b, c, c, c, c), the map will be + // {0 -> (a, b, c), 1 -> (a, c), 2 -> (c), 3 -> (c)}, Review comment: Note: the current comment says *what* the code is doing. However, it would be better to clarify *why* it is doing like that. The map itself has no value here. The value comes from splitting the grouping sets into the least number of `union all`. This intention is not really represented. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services