julianhyde commented on code in PR #4495:
URL: https://github.com/apache/calcite/pull/4495#discussion_r2264075998
##########
core/src/main/java/org/apache/calcite/tools/RelBuilder.java:
##########
@@ -2849,55 +2877,112 @@ private RelBuilder
rewriteAggregateWithDuplicateGroupSets(
maxGroupId = groupId;
}
for (int i = 0; i <= groupId; i++) {
- groupIdToGroupSets.computeIfAbsent(i,
- k -> Sets.newTreeSet(ImmutableBitSet.COMPARATOR))
+ groupIdToGroupSets.computeIfAbsent(i, k ->
Sets.newTreeSet(ImmutableBitSet.COMPARATOR))
.add(entry.getElement());
}
}
- // AggregateCall list without GROUP_ID function
- final List<AggCall> aggregateCallsWithoutGroupId =
- new ArrayList<>(aggregateCalls);
- aggregateCallsWithoutGroupId.removeIf(RelBuilder::isGroupId);
+ // Create aggregate for each GROUP_ID value
+ for (Map.Entry<Integer, Set<ImmutableBitSet>> entry :
groupIdToGroupSets.entrySet()) {
+ createAggregate(entry.getValue(), groupSet, groupCount, aggregateCalls,
+ fieldNamesIfNoRewrite, mapping, frame, entry.getKey());
+ }
- // For each group id value, we first construct an Aggregate without
- // GROUP_ID() function call, and then create a Project node on top of it.
- // The Project adds literal value for group id in right position.
- final Frame frame = stack.pop();
- for (int groupId = 0; groupId <= maxGroupId; groupId++) {
- // Create the Aggregate node without GROUP_ID() call
- stack.push(frame);
- aggregate(groupKey(groupSet,
castNonNull(groupIdToGroupSets.get(groupId))),
- aggregateCallsWithoutGroupId);
-
- final List<RexNode> selectList = new ArrayList<>();
- final int groupExprLength = groupSet.cardinality();
- // Project fields in group by expressions
- for (int i = 0; i < groupExprLength; i++) {
- selectList.add(field(i));
- }
- // Project fields in aggregate calls
- int groupIdCount = 0;
- for (int i = 0; i < aggregateCalls.size(); i++) {
- if (isGroupId(aggregateCalls.get(i))) {
- selectList.add(
- getRexBuilder().makeExactLiteral(BigDecimal.valueOf(groupId),
- getTypeFactory().createSqlType(SqlTypeName.BIGINT)));
- groupIdCount++;
- } else {
- selectList.add(field(groupExprLength + i - groupIdCount));
+ return union(true, groupIdToGroupSets.size());
+ }
+
+ /**
+ * Create aggregate.
Review Comment:
Creates an
--
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]