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/10/24 12:06:55 UTC

[GitHub] [doris] morrySnow commented on a diff in pull request #13416: [Feature](Nereids) Support materialized index selection.

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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalOlapScan.java:
##########
@@ -87,7 +87,7 @@ public LogicalOlapScan(RelationId id, Table table, List<String> qualifier,
         }
         this.partitionPruned = partitionPruned;
         this.candidateIndexIds = candidateIndexIds;
-        this.rollupSelected = rollupSelected;
+        this.indexSelected = rollupSelected;

Review Comment:
   ```suggestion
           this.indexSelected = indexSelected;
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Count.java:
##########
@@ -30,7 +31,7 @@
 import java.util.stream.Collectors;
 
 /** count agg function. */
-public class Count extends AggregateFunction implements AlwaysNotNullable {
+public class Count extends AggregateFunction implements UnaryExpression, AlwaysNotNullable {

Review Comment:
   when we use count(*), the count has no child, so maybe UnaryExpression is inappropriate



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/mv/SelectMaterializedIndexWithoutAggregate.java:
##########
@@ -0,0 +1,147 @@
+// 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.mv;
+
+import org.apache.doris.catalog.MaterializedIndex;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.RewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PreAggStatus;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+
+import java.util.List;
+import java.util.Set;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+/**
+ * Select materialized index, i.e., both for rollup and materialized view when aggregate is not present.
+ * <p>
+ * Scan OLAP table with aggregate is handled in {@link SelectMaterializedIndexWithAggregate}.
+ * <p>
+ * Note that we should first apply {@link SelectMaterializedIndexWithAggregate} and then
+ * {@link SelectMaterializedIndexWithoutAggregate}.
+ * Besides, these two rules should run in isolated batches, thus when enter this rule, it's guaranteed that there is
+ * no aggregation on top of the scan.
+ */
+public class SelectMaterializedIndexWithoutAggregate extends AbstractSelectMaterializedIndexRule
+        implements RewriteRuleFactory {
+
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.of(
+                // project with pushdown filter.
+                // Project(Filter(Scan))
+                logicalProject(logicalFilter(logicalOlapScan().whenNot(LogicalOlapScan::isIndexSelected)))
+                        .then(project -> {
+                            LogicalFilter<LogicalOlapScan> filter = project.child();
+                            LogicalOlapScan scan = filter.child();
+                            return project.withChildren(filter.withChildren(
+                                    select(scan, project::getInputSlots, filter::getConjuncts)));
+                        }).toRule(RuleType.MATERIALIZED_INDEX_PROJECT_FILTER_SCAN),
+
+                // project with filter that cannot be pushdown.
+                // Filter(Project(Scan))
+                logicalFilter(logicalProject(logicalOlapScan().whenNot(LogicalOlapScan::isIndexSelected)))
+                        .then(filter -> {
+                            LogicalProject<LogicalOlapScan> project = filter.child();
+                            LogicalOlapScan scan = project.child();
+                            return filter.withChildren(project.withChildren(
+                                    select(scan, project::getInputSlots, ImmutableList::of)
+                            ));
+                        }).toRule(RuleType.MATERIALIZED_INDEX_FILTER_PROJECT_SCAN),
+
+                // scan with filters could be pushdown.
+                // Filter(Scan)
+                logicalFilter(logicalOlapScan().whenNot(LogicalOlapScan::isIndexSelected))
+                        .then(filter -> {
+                            LogicalOlapScan scan = filter.child();
+                            return filter.withChildren(select(scan, ImmutableSet::of, filter::getConjuncts));
+                        })
+                        .toRule(RuleType.MATERIALIZED_INDEX_FILTER_SCAN),
+
+                // project and scan.
+                // Project(Scan)
+                logicalProject(logicalOlapScan().whenNot(LogicalOlapScan::isIndexSelected))
+                        .then(project -> {
+                            LogicalOlapScan scan = project.child();
+                            return project.withChildren(
+                                    select(scan, project::getInputSlots, ImmutableList::of));
+                        })
+                        .toRule(RuleType.MATERIALIZED_INDEX_PROJECT_SCAN),
+
+                // only scan.
+                logicalOlapScan()
+                        .whenNot(LogicalOlapScan::isIndexSelected)
+                        .then(scan -> select(scan, scan::getOutputSet, ImmutableList::of))
+                        .toRule(RuleType.MATERIALIZED_INDEX_SCAN)
+        );
+    }
+
+    /**
+     * Select materialized index when aggregate node is not present.
+     *
+     * @param scan Scan node.
+     * @param requiredScanOutputSupplier Supplier to get the required scan output.
+     * @param predicatesSupplier Supplier to get pushdown predicates.
+     * @return Result scan node.
+     */
+    private LogicalOlapScan select(
+            LogicalOlapScan scan,
+            Supplier<Set<Slot>> requiredScanOutputSupplier,
+            Supplier<List<Expression>> predicatesSupplier) {
+        switch (scan.getTable().getKeysType()) {
+            case AGG_KEYS:
+            case UNIQUE_KEYS:
+                OlapTable table = scan.getTable();
+                long baseIndexId = table.getBaseIndexId();
+                int baseIndexKeySize = table.getKeyColumnsByIndexId(table.getBaseIndexId()).size();
+                // No on aggregate on scan.

Review Comment:
   ```suggestion
                   // No aggregation on scan.
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalOlapScan.java:
##########
@@ -164,30 +164,31 @@ public long getSelectedIndexId() {
         return selectedIndexId;
     }
 
-    public boolean isRollupSelected() {
-        return rollupSelected;
+    public boolean isIndexSelected() {
+        return indexSelected;
     }
 
     public PreAggStatus getPreAggStatus() {
         return preAggStatus;
     }
 
     /**
-     * Should apply {@link SelectRollupWithAggregate} or not.
+     * Should apply {@link SelectMaterializedIndexWithAggregate} or not.
      */
-    public boolean shouldSelectRollup() {
+    public boolean shouldSelectIndex() {

Review Comment:
   do we need to remove this function and use isIndexSelected instead?



-- 
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