You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/07/27 07:43:30 UTC

[GitHub] [doris] morrySnow commented on a diff in pull request #11179: [feature](nereids) Add stats derive framework for new optimizer

morrySnow commented on code in PR #11179:
URL: https://github.com/apache/doris/pull/11179#discussion_r930710682


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java:
##########
@@ -45,17 +45,16 @@ public SlotReference(String name, DataType dataType, boolean nullable, List<Stri
      * Constructor for SlotReference.
      *
      * @param exprId UUID for this slot reference
-     * @param name slot reference name
      * @param dataType slot reference logical data type
      * @param nullable true if nullable
      * @param qualifier slot reference qualifier
      */
     public SlotReference(ExprId exprId, String name, DataType dataType, boolean nullable, List<String> qualifier) {
         this.exprId = exprId;
-        this.name = name;
         this.dataType = dataType;
         this.qualifier = qualifier;
         this.nullable = nullable;
+        this.name = name;

Review Comment:
   move back?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/PlanVisitor.java:
##########
@@ -59,8 +59,8 @@ public R visitLogicalRelation(LogicalRelation relation, C context) {
     }
 
 
-    public R visitLogicalAggregate(LogicalAggregate<Plan> relation, C context) {
-        return visit(relation, context);
+    public R visitLogicalAggregate(LogicalAggregate<Plan> agg, C context) {
+        return visit(agg, context);

Review Comment:
   ```suggestion
       public R visitLogicalAggregate(LogicalAggregate<Plan> aggregate, C context) {
           return visit(aggregate, context);
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java:
##########
@@ -45,17 +45,16 @@ public SlotReference(String name, DataType dataType, boolean nullable, List<Stri
      * Constructor for SlotReference.
      *
      * @param exprId UUID for this slot reference
-     * @param name slot reference name

Review Comment:
   add back?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/FilterSelectivityCalculator.java:
##########
@@ -0,0 +1,101 @@
+// 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.stats;
+
+import org.apache.doris.nereids.trees.expressions.ComparisonPredicate;
+import org.apache.doris.nereids.trees.expressions.CompoundPredicate;
+import org.apache.doris.nereids.trees.expressions.EqualTo;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.GreaterThan;
+import org.apache.doris.nereids.trees.expressions.GreaterThanEqual;
+import org.apache.doris.nereids.trees.expressions.LessThan;
+import org.apache.doris.nereids.trees.expressions.Literal;
+import org.apache.doris.nereids.trees.expressions.Or;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionVisitor;
+import org.apache.doris.statistics.ColumnStats;
+
+import java.util.Map;
+
+
+/**
+ * Calculate selectivity of the filter.
+ */
+public class FilterSelectivityCalculator extends DefaultExpressionVisitor<Double, Void> {
+
+    private final Map<Slot, ColumnStats> slotRefToStats;
+
+    public FilterSelectivityCalculator(Map<Slot, ColumnStats> slotRefToStats) {
+        this.slotRefToStats = slotRefToStats;
+    }
+
+    /**
+     * Do estimate for the given expr.
+     */
+    public double estimate(Expression expression) {
+        // For a comparison predicate, only when it's left side is a slot and right side is a literal, we would
+        // consider it as a valid predicate.
+        if (expression instanceof ComparisonPredicate
+                && !(expression.child(0) instanceof SlotReference
+                && expression.child(1) instanceof Literal)) {
+            return 1.0;
+        }
+        return expression.accept(this, null);
+    }
+
+    @Override
+    public Double visitCompoundPredicate(CompoundPredicate compoundPredicate, Void context) {
+        Expression leftExpr = compoundPredicate.child(0);
+        Expression rightExpr = compoundPredicate.child(1);
+        double leftSel = 1;
+        double rightSel = 1;
+        leftSel =  estimate(leftExpr);
+        rightSel = estimate(rightExpr);
+        return compoundPredicate instanceof Or ? leftSel + rightSel - leftSel * rightSel : leftSel * rightSel;
+    }
+
+    // TODO: If right value greater than the max value or less than min value in column stats, return 0.0 .
+    @Override
+    public Double visitEqualTo(EqualTo equalTo, Void context) {
+        SlotReference left = (SlotReference) equalTo.left();
+        ColumnStats columnStats = slotRefToStats.get(left);
+        if (columnStats == null) {
+            return Expression.DEFAULT_SELECTIVITY;

Review Comment:
   i think it is better put DEFAULT_SELECTIVITY in this class



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/memo/GroupExpression.java:
##########
@@ -57,7 +59,7 @@ public GroupExpression(Plan plan) {
      * @param plan {@link Plan} to reference
      * @param children children groups in memo
      */
-    public GroupExpression(Plan plan, List<Group> children) {
+    public GroupExpression(Plan plan, @NotNull List<Group> children) {

Review Comment:
   why add this annotation?



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/ColumnStats.java:
##########
@@ -76,6 +76,22 @@ public class ColumnStats {
     private LiteralExpr minValue;
     private LiteralExpr maxValue;
 
+    public ColumnStats(ColumnStats other) {

Review Comment:
   we should let it immutable later



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/FilterSelectivityCalculator.java:
##########
@@ -0,0 +1,101 @@
+// 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.stats;
+
+import org.apache.doris.nereids.trees.expressions.ComparisonPredicate;
+import org.apache.doris.nereids.trees.expressions.CompoundPredicate;
+import org.apache.doris.nereids.trees.expressions.EqualTo;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.GreaterThan;
+import org.apache.doris.nereids.trees.expressions.GreaterThanEqual;
+import org.apache.doris.nereids.trees.expressions.LessThan;
+import org.apache.doris.nereids.trees.expressions.Literal;
+import org.apache.doris.nereids.trees.expressions.Or;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionVisitor;
+import org.apache.doris.statistics.ColumnStats;
+
+import java.util.Map;
+
+
+/**
+ * Calculate selectivity of the filter.
+ */
+public class FilterSelectivityCalculator extends DefaultExpressionVisitor<Double, Void> {
+
+    private final Map<Slot, ColumnStats> slotRefToStats;
+
+    public FilterSelectivityCalculator(Map<Slot, ColumnStats> slotRefToStats) {
+        this.slotRefToStats = slotRefToStats;

Review Comment:
   check null



-- 
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-unsubscribe@doris.apache.org

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


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