danny0405 commented on a change in pull request #1741: [CALCITE-3721] Filter of 
distinct aggregate call is lost after applyi…
URL: https://github.com/apache/calcite/pull/1741#discussion_r366250026
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/rel/rules/AggregateExpandDistinctAggregatesRule.java
 ##########
 @@ -402,15 +400,27 @@ private void rewriteUsingGroupingSets(RelOptRuleCall 
call,
       Aggregate aggregate) {
     final Set<ImmutableBitSet> groupSetTreeSet =
         new TreeSet<>(ImmutableBitSet.ORDERING);
+    // groupSet to distinct filter arg map,
+    // filterArg will be -1 for groupSet of non-distinct agg.
+    final Map<ImmutableBitSet, Set<Integer>> distinctFilterArgMap = new 
HashMap<>();
     for (AggregateCall aggCall : aggregate.getAggCallList()) {
+      ImmutableBitSet groupSet;
+      int filterArg;
       if (!aggCall.isDistinct()) {
+        filterArg = -1;
+        groupSet = aggregate.getGroupSet();
         groupSetTreeSet.add(aggregate.getGroupSet());
       } else {
-        groupSetTreeSet.add(
+        filterArg = aggCall.filterArg;
+        groupSet =
             ImmutableBitSet.of(aggCall.getArgList())
-                .setIf(aggCall.filterArg, aggCall.filterArg >= 0)
-                .union(aggregate.getGroupSet()));
+                .setIf(filterArg, filterArg >= 0)
+                .union(aggregate.getGroupSet());
+        groupSetTreeSet.add(groupSet);
       }
+      Set<Integer> filterList = distinctFilterArgMap
+          .computeIfAbsent(groupSet, g -> new HashSet<>());
+      filterList.add(filterArg);
 
 Review comment:
   Add document to explain why we need a `Set` to keep the filter arg.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to