924060929 commented on code in PR #14863:
URL: https://github.com/apache/doris/pull/14863#discussion_r1052900899


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/CountDistinctRewrite.java:
##########
@@ -0,0 +1,73 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.rules.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.BitmapUnionCount;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
+import 
org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * Rewrite count distinct for bitmap and hll type value.
+ * <p>
+ * count(distinct bitmap_col) -> bitmap_union_count(bitmap col)
+ * todo: add support for HLL type.
+ */
+public class CountDistinctRewrite extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate().then(agg -> {
+            List<NamedExpression> output = agg.getOutputExpressions()
+                    .stream()
+                    .map(CountDistinctRewriter::rewrite)
+                    .map(NamedExpression.class::cast)
+                    .collect(ImmutableList.toImmutableList());
+            return new LogicalAggregate<>(agg.getGroupByExpressions(), output,
+                    agg.isDisassembled(), agg.isNormalized(),
+                    agg.isFinalPhase(), agg.getAggPhase(), 
agg.getSourceRepeat(), agg.child());
+        }).toRule(RuleType.COUNT_DISTINCT_REWRITE);
+    }
+
+    private static class CountDistinctRewriter extends 
DefaultExpressionRewriter<Void> {
+        private static final CountDistinctRewriter INSTANCE = new 
CountDistinctRewriter();
+
+        public static Expression rewrite(Expression expr) {
+            return expr.accept(INSTANCE, null);
+        }
+
+        @Override
+        public Expression visitCount(Count count, Void context) {
+            if (count.isDistinct()) {
+                Expression child = count.child(0);

Review Comment:
   count distinct can support multi columns, like `count(distinct id, value)`, 
so you should check the arity



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/CountDistinctRewrite.java:
##########
@@ -0,0 +1,73 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.rules.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.BitmapUnionCount;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
+import 
org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * Rewrite count distinct for bitmap and hll type value.
+ * <p>
+ * count(distinct bitmap_col) -> bitmap_union_count(bitmap col)
+ * todo: add support for HLL type.
+ */
+public class CountDistinctRewrite extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate().then(agg -> {
+            List<NamedExpression> output = agg.getOutputExpressions()
+                    .stream()
+                    .map(CountDistinctRewriter::rewrite)
+                    .map(NamedExpression.class::cast)
+                    .collect(ImmutableList.toImmutableList());
+            return new LogicalAggregate<>(agg.getGroupByExpressions(), output,
+                    agg.isDisassembled(), agg.isNormalized(),
+                    agg.isFinalPhase(), agg.getAggPhase(), 
agg.getSourceRepeat(), agg.child());
+        }).toRule(RuleType.COUNT_DISTINCT_REWRITE);
+    }
+
+    private static class CountDistinctRewriter extends 
DefaultExpressionRewriter<Void> {

Review Comment:
   a shorter rewriter:
   ```java
   ExpressionUtils.rewriteDownShortCircuit(agg.getOutputExpressions(), expr -> {
       if (expr instanceof Count && expr.arity() == 1 && ((Count) 
expr).isDistinct()
               && expr.child(0).getDataType().isBitmapType()) {
           return new BitmapUnionCount(expr.child(0));
       }
       return expr;
   })
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/CountDistinctRewrite.java:
##########
@@ -0,0 +1,73 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.rules.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.BitmapUnionCount;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
+import 
org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * Rewrite count distinct for bitmap and hll type value.
+ * <p>
+ * count(distinct bitmap_col) -> bitmap_union_count(bitmap col)
+ * todo: add support for HLL type.
+ */
+public class CountDistinctRewrite extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate().then(agg -> {
+            List<NamedExpression> output = agg.getOutputExpressions()
+                    .stream()
+                    .map(CountDistinctRewriter::rewrite)
+                    .map(NamedExpression.class::cast)
+                    .collect(ImmutableList.toImmutableList());
+            return new LogicalAggregate<>(agg.getGroupByExpressions(), output,
+                    agg.isDisassembled(), agg.isNormalized(),
+                    agg.isFinalPhase(), agg.getAggPhase(), 
agg.getSourceRepeat(), agg.child());

Review Comment:
   why not invoke `agg.withAggOutput(output)`?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/Command.java:
##########
@@ -83,6 +83,11 @@ default List<Slot> getOutput() {
         throw new RuntimeException("Command do not implement getOutput");
     }
 
+    @Override
+    default List<Slot> getNonUserVisibleOutput() {
+        throw new RuntimeException("Command do not implement 
getNonUserVisibleOutput");

Review Comment:
   why not supply an empty list?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/util/ExpressionUtils.java:
##########
@@ -234,12 +235,20 @@ public static Slot selectMinimumColumn(List<Slot> slots) {
      *         Otherwise, return empty optional result.
      */
     public static Optional<ExprId> isSlotOrCastOnSlot(Expression expr) {
+        return extractSlotOnCastOnSlot(expr).map(Slot::getExprId);
+    }
+
+    /**
+     * Check whether the input expression is a {@link 
org.apache.doris.nereids.trees.expressions.Slot}
+     * or at least one {@link Cast} on a {@link 
org.apache.doris.nereids.trees.expressions.Slot}
+     */
+    public static Optional<Slot> extractSlotOnCastOnSlot(Expression expr) {

Review Comment:
   ```suggestion
       public static Optional<Slot> extractSlotOrCastOnSlot(Expression expr) {
   ```



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to