xzj7019 commented on code in PR #24944: URL: https://github.com/apache/doris/pull/24944#discussion_r1338373130
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushdownFilterThroughPTopN.java: ########## @@ -0,0 +1,93 @@ +// 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; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalPartitionTopN; + +import com.google.common.collect.Sets; + +import java.util.Set; + +/** + * Push down the 'filter' pass through the 'partitionTopN' if filter key is partitionTopN's partition key. + * Logical plan tree: + * any_node + * | + * filter (a <= 100) + * | + * partition topn (PARTITION BY a) + * | + * any_node + * transformed to: + * any_node + * | + * partition topn (PARTITION BY a) + * | + * filter (a <= 100) + * | + * any_node + */ + +public class PushdownFilterThroughPTopN extends OneRewriteRuleFactory { + + @Override + public Rule build() { + return logicalFilter(logicalPartitionTopN()).thenApply(ctx -> { + LogicalFilter<LogicalPartitionTopN<Plan>> filter = ctx.root; + LogicalPartitionTopN<Plan> partitionTopN = filter.child(); + // follow the similar checking and transformation rule as + // PushdownFilterThroughWindow + Set<Expression> bottomConjuncts = Sets.newHashSet(); + Set<Expression> upperConjuncts = Sets.newHashSet(); + for (Expression expr : filter.getConjuncts()) { + boolean pushed = false; + for (Expression partitionKey : partitionTopN.getPartitionKeys()) { + if (partitionKey instanceof SlotReference + && partitionKey.getInputSlots().containsAll(expr.getInputSlots())) { Review Comment: done -- 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