kangkaisen commented on a change in pull request #2821: The new materialized view selector URL: https://github.com/apache/incubator-doris/pull/2821#discussion_r375241330
########## File path: fe/src/main/java/org/apache/doris/planner/MaterializedViewSelector.java ########## @@ -0,0 +1,453 @@ +// 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.planner; + +import org.apache.doris.analysis.Analyzer; +import org.apache.doris.analysis.CastExpr; +import org.apache.doris.analysis.Expr; +import org.apache.doris.analysis.FunctionCallExpr; +import org.apache.doris.analysis.SelectStmt; +import org.apache.doris.analysis.SlotRef; +import org.apache.doris.analysis.TableRef; +import org.apache.doris.catalog.Column; +import org.apache.doris.catalog.KeysType; +import org.apache.doris.catalog.OlapTable; +import org.apache.doris.catalog.Table; +import org.apache.doris.common.UserException; +import org.apache.doris.qe.ConnectContext; + +import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +/** + * The new materialized view selector supports SPJ<->SPJG. + * At the same time, it is compatible with all the features of the old version. + * The SPJ query is "Select Projection and Join" such as: + * select t1.c1 from t1, t2 where t1.c2=t2.c2 and t1.c3=1; + * The SPJG query is "Select Projection Join and Group-by" such as: + * select t1.c1, sum(t2.c1) from t1, t2 where t1.c2=t2.c2 and t1.c3=1 group by t1.c1; + */ +public class MaterializedViewSelector { + private static final Logger LOG = LogManager.getLogger(MaterializedViewSelector.class); + + private final SelectStmt selectStmt; + private final Analyzer analyzer; + + private Map<String, Set<String>> columnNamesInPredicates = Maps.newHashMap(); + private boolean isSPJQuery; + private Map<String, Set<String>> columnNamesInGrouping = Maps.newHashMap(); + private Map<String, Set<AggregatedColumn>> aggregateColumnsInQuery = Maps.newHashMap(); + private Map<String, Set<String>> columnNamesInQueryOutput = Maps.newHashMap(); + + private boolean disableSPJGView; + private String reasonOfDisable; + private boolean isPreAggregation = true; + + public MaterializedViewSelector(SelectStmt selectStmt, Analyzer analyzer) { + this.selectStmt = selectStmt; + this.analyzer = analyzer; + init(); + } + + /** + * There are two stages to choosing the best MV. + * Phase 1: Predicates + * According to aggregation and column information in the select stmt, + * the candidate MVs that meets the query conditions are selected. + * Phase 2: Priorities + * According to prefix index and row count in candidate MVs, + * the best MV is selected. + * + * @param scanNode + * @return + */ + public void selectBestMV(ScanNode scanNode) throws UserException { + long start = System.currentTimeMillis(); + Preconditions.checkState(scanNode instanceof OlapScanNode); + OlapScanNode olapScanNode = (OlapScanNode) scanNode; + Map<Long, List<Column>> candidateIndexIdToSchema = predicates(olapScanNode); + long bestIndexId = priorities(olapScanNode, candidateIndexIdToSchema); + LOG.info("The best materialized view is {} for scan node {} in query {}, cost {}", + bestIndexId, scanNode.getId(), selectStmt.toSql(), (System.currentTimeMillis() - start)); + olapScanNode.updateScanRangeInfo(bestIndexId, isPreAggregation, reasonOfDisable); + } + + private Map<Long, List<Column>> predicates(OlapScanNode scanNode) { + // Step1: all of predicates is compensating predicates Review comment: OK. I see. ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org