Kikyou1997 commented on code in PR #11964: URL: https://github.com/apache/doris/pull/11964#discussion_r954451674
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PruneOlapScanPartition.java: ########## @@ -0,0 +1,166 @@ +// 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.analysis.LiteralExpr; +import org.apache.doris.catalog.OlapTable; +import org.apache.doris.catalog.PartitionInfo; +import org.apache.doris.catalog.PartitionItem; +import org.apache.doris.catalog.PartitionType; +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.ComparisonPredicate; +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.LessThanEqual; +import org.apache.doris.nereids.trees.expressions.Or; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.literal.Literal; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan; +import org.apache.doris.nereids.util.ExpressionUtils; +import org.apache.doris.nereids.util.Utils; +import org.apache.doris.planner.ColumnBound; +import org.apache.doris.planner.ColumnRange; +import org.apache.doris.planner.PartitionPruner; +import org.apache.doris.planner.RangePartitionPrunerV2; +import org.apache.doris.planner.ScanNode.ColumnRanges; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Range; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Used to prune partition of olap scan, should execute after SwapProjectAndFilter, MergeConsecutiveFilters, + * MergeConsecutiveProjects and all predicate push down related rules. + */ +public class PruneOlapScanPartition extends OneRewriteRuleFactory { + + @Override + public Rule build() { + return logicalFilter(logicalOlapScan()).thenApply(ctx -> { + LogicalFilter<LogicalOlapScan> filter = ctx.root; + LogicalOlapScan scan = filter.child(); + Expression predicate = filter.getPredicates(); + List<Expression> expressionList = ExpressionUtils.extractConjunction(predicate); + OlapTable table = scan.getTable(); + Set<String> partitionColumnNameSet = Utils.execWithReturnVal(table::getPartitionColumnNames); + PartitionInfo partitionInfo = table.getPartitionInfo(); + // TODO: 1. support grammar: SELECT * FROM tbl PARTITION(p1,p2) + // 2. support list partition + if (partitionColumnNameSet.isEmpty() || !partitionInfo.getType().equals(PartitionType.RANGE)) { + return ctx.root; + } + // TODO: Process all partition column for now, better to process required column only. + Map<String, ColumnRange> columnNameToRange = Maps.newHashMap(); + for (String colName : partitionColumnNameSet) { + ColumnRange columnRange = createColumnRange(colName, expressionList); + columnNameToRange.put(colName, columnRange); + } + + Map<Long, PartitionItem> keyItemMap = partitionInfo.getIdToItem(false); + PartitionPruner partitionPruner = new RangePartitionPrunerV2(keyItemMap, + partitionInfo.getPartitionColumns(), columnNameToRange); + Collection<Long> selectedPartitionId = Utils.execWithReturnVal(partitionPruner::prune); + scan.getSelectedPartitionIds().retainAll(selectedPartitionId); + return ctx.root; + }).toRule(RuleType.PARTITION_PRUNE); + } + + private ColumnRange createColumnRange(String colName, List<Expression> expressionList) { + ColumnRange result = ColumnRange.create(); + for (Expression expression : expressionList) { + Expression leftMostChild = expression.leftMostNode(); + if (!(leftMostChild instanceof SlotReference)) { + continue; + } + SlotReference slotRef = (SlotReference) leftMostChild; + if (!slotRef.boundToColumn(colName)) { + continue; + } + if (expression instanceof Or) { + ColumnRanges ranges = exprToRanges(expression, colName); + switch (ranges.type) { + case IS_NULL: + result.setHasConjunctiveIsNull(true); + break; + case CONVERT_SUCCESS: + result.intersect(ranges.ranges); + break; + case CONVERT_FAILURE: + default: + break; + } + } else { + ColumnRanges ranges = exprToRanges(expression, colName); + switch (ranges.type) { + case IS_NULL: + result.setHasConjunctiveIsNull(true); + break; + case CONVERT_SUCCESS: + result.intersect(ranges.ranges); + break; + case CONVERT_FAILURE: + default: + break; + } + } + } + return result; + } + + private ColumnRanges exprToRanges(Expression expression, String colName) { + // TODO: process in/is null expression Review Comment: It's determined on how the the partition is set for tpch table. -- 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