morrySnow commented on code in PR #22264: URL: https://github.com/apache/doris/pull/22264#discussion_r1278752673
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushdownMinMaxThroughJoin.java: ########## @@ -0,0 +1,183 @@ +// 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.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; +import org.apache.doris.nereids.trees.expressions.functions.agg.Max; +import org.apache.doris.nereids.trees.expressions.functions.agg.Min; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Related paper "Eager aggregation and lazy aggregation". + * <pre> + * aggregate: Min/Max(x) + * | + * join + * | \ + * | * + * (x) + * -> + * aggregate: Min/Max(min1) + * | + * join + * | \ + * | * + * aggregate: Min/Max(x) as min1 + * </pre> + */ +public class PushdownMinMaxThroughJoin implements RewriteRuleFactory { + @Override + public List<Rule> buildRules() { + return ImmutableList.of( + logicalAggregate(innerLogicalJoin()) + .when(agg -> agg.child().getOtherJoinConjuncts().size() == 0) + .whenNot(agg -> agg.child().children().stream().anyMatch(p -> p instanceof LogicalAggregate)) + .when(agg -> agg.getGroupByExpressions().stream().allMatch(e -> e instanceof Slot)) + .when(agg -> { + Set<AggregateFunction> funcs = agg.getAggregateFunctions(); + return !funcs.isEmpty() && funcs.stream() + .allMatch(f -> f instanceof Min || f instanceof Max && f.child(0) instanceof Slot); Review Comment: ```suggestion .allMatch((f -> f instanceof Min || f instanceof Max) && f.child(0) instanceof Slot); ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushdownMinMaxThroughJoin.java: ########## @@ -0,0 +1,183 @@ +// 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.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; +import org.apache.doris.nereids.trees.expressions.functions.agg.Max; +import org.apache.doris.nereids.trees.expressions.functions.agg.Min; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Related paper "Eager aggregation and lazy aggregation". + * <pre> + * aggregate: Min/Max(x) + * | + * join + * | \ + * | * + * (x) + * -> + * aggregate: Min/Max(min1) + * | + * join + * | \ + * | * + * aggregate: Min/Max(x) as min1 + * </pre> + */ +public class PushdownMinMaxThroughJoin implements RewriteRuleFactory { + @Override + public List<Rule> buildRules() { + return ImmutableList.of( + logicalAggregate(innerLogicalJoin()) + .when(agg -> agg.child().getOtherJoinConjuncts().size() == 0) + .whenNot(agg -> agg.child().children().stream().anyMatch(p -> p instanceof LogicalAggregate)) + .when(agg -> agg.getGroupByExpressions().stream().allMatch(e -> e instanceof Slot)) + .when(agg -> { + Set<AggregateFunction> funcs = agg.getAggregateFunctions(); + return !funcs.isEmpty() && funcs.stream() + .allMatch(f -> f instanceof Min || f instanceof Max && f.child(0) instanceof Slot); + }) + .then(agg -> pushMinMax(agg, agg.child(), ImmutableList.of())) + .toRule(RuleType.PUSHDOWN_MIN_MAX_THROUGH_JOIN), + logicalAggregate(logicalProject(innerLogicalJoin())) + .when(agg -> agg.child().isAllSlots()) + .when(agg -> agg.child().child().getOtherJoinConjuncts().size() == 0) + .whenNot(agg -> agg.child().children().stream().anyMatch(p -> p instanceof LogicalAggregate)) + .when(agg -> agg.getGroupByExpressions().stream().allMatch(e -> e instanceof Slot)) + .when(agg -> { + Set<AggregateFunction> funcs = agg.getAggregateFunctions(); + return !funcs.isEmpty() && funcs.stream() + .allMatch(f -> f instanceof Min || f instanceof Max && f.child(0) instanceof Slot); + }) + .then(agg -> pushMinMax(agg, agg.child().child(), agg.child().getProjects())) + .toRule(RuleType.PUSHDOWN_MIN_MAX_THROUGH_JOIN) + ); + } + + private LogicalAggregate<Plan> pushMinMax(LogicalAggregate<? extends Plan> agg, + LogicalJoin<Plan, Plan> join, List<NamedExpression> projects) { + List<Slot> leftOutput = join.left().getOutput(); + List<Slot> rightOutput = join.right().getOutput(); + + List<AggregateFunction> leftFuncs = new ArrayList<>(); + List<AggregateFunction> rightFuncs = new ArrayList<>(); + for (AggregateFunction func : agg.getAggregateFunctions()) { + Slot slot = (Slot) func.child(0); + if (leftOutput.contains(slot)) { + leftFuncs.add(func); + } else if (rightOutput.contains(slot)) { + rightFuncs.add(func); + } else { + throw new IllegalStateException("Slot " + slot + " not found in join output"); + } + } + + Set<Slot> leftGroupBy = new HashSet<>(); + Set<Slot> rightGroupBy = new HashSet<>(); + agg.getGroupByExpressions().forEach(e -> { + Slot slot = (Slot) e; + if (leftOutput.contains(slot)) { + leftGroupBy.add(slot); + } else if (rightOutput.contains(slot)) { + rightGroupBy.add(slot); + } else { + throw new IllegalStateException("Slot " + slot + " not found in join output"); Review Comment: group by 1 will throw exception here ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushdownMinMaxThroughJoin.java: ########## @@ -0,0 +1,183 @@ +// 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.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; +import org.apache.doris.nereids.trees.expressions.functions.agg.Max; +import org.apache.doris.nereids.trees.expressions.functions.agg.Min; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Related paper "Eager aggregation and lazy aggregation". + * <pre> + * aggregate: Min/Max(x) + * | + * join + * | \ + * | * + * (x) + * -> + * aggregate: Min/Max(min1) + * | + * join + * | \ + * | * + * aggregate: Min/Max(x) as min1 + * </pre> + */ +public class PushdownMinMaxThroughJoin implements RewriteRuleFactory { + @Override + public List<Rule> buildRules() { + return ImmutableList.of( + logicalAggregate(innerLogicalJoin()) + .when(agg -> agg.child().getOtherJoinConjuncts().size() == 0) + .whenNot(agg -> agg.child().children().stream().anyMatch(p -> p instanceof LogicalAggregate)) + .when(agg -> agg.getGroupByExpressions().stream().allMatch(e -> e instanceof Slot)) + .when(agg -> { + Set<AggregateFunction> funcs = agg.getAggregateFunctions(); + return !funcs.isEmpty() && funcs.stream() + .allMatch(f -> f instanceof Min || f instanceof Max && f.child(0) instanceof Slot); + }) + .then(agg -> pushMinMax(agg, agg.child(), ImmutableList.of())) + .toRule(RuleType.PUSHDOWN_MIN_MAX_THROUGH_JOIN), + logicalAggregate(logicalProject(innerLogicalJoin())) + .when(agg -> agg.child().isAllSlots()) + .when(agg -> agg.child().child().getOtherJoinConjuncts().size() == 0) + .whenNot(agg -> agg.child().children().stream().anyMatch(p -> p instanceof LogicalAggregate)) + .when(agg -> agg.getGroupByExpressions().stream().allMatch(e -> e instanceof Slot)) + .when(agg -> { + Set<AggregateFunction> funcs = agg.getAggregateFunctions(); + return !funcs.isEmpty() && funcs.stream() + .allMatch(f -> f instanceof Min || f instanceof Max && f.child(0) instanceof Slot); Review Comment: ```suggestion .allMatch((f -> f instanceof Min || f instanceof Max) && f.child(0) instanceof Slot); ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushdownSumThroughJoin.java: ########## @@ -0,0 +1,196 @@ +// 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.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.Multiply; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; +import org.apache.doris.nereids.trees.expressions.functions.agg.Count; +import org.apache.doris.nereids.trees.expressions.functions.agg.Sum; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Related paper "Eager aggregation and lazy aggregation". + * <pre> + * aggregate: Sum(x) + * | + * join + * | \ + * | * + * (x) + * -> + * aggregate: Sum(min1) + * | + * join + * | \ + * | * + * aggregate: Sum(x) as min1 + * </pre> + */ +public class PushdownSumThroughJoin implements RewriteRuleFactory { + @Override + public List<Rule> buildRules() { + return ImmutableList.of( + logicalAggregate(innerLogicalJoin()) + .when(agg -> agg.child().getOtherJoinConjuncts().size() == 0) + .whenNot(agg -> agg.child().children().stream().anyMatch(p -> p instanceof LogicalAggregate)) + .when(agg -> agg.getGroupByExpressions().stream().allMatch(e -> e instanceof Slot)) + .when(agg -> { + Set<AggregateFunction> funcs = agg.getAggregateFunctions(); + return !funcs.isEmpty() && funcs.stream() + .allMatch(f -> f instanceof Sum && f.child(0) instanceof Slot); + }) + .then(agg -> pushSum(agg, agg.child(), ImmutableList.of())) + .toRule(RuleType.PUSHDOWN_SUM_THROUGH_JOIN), + logicalAggregate(logicalProject(innerLogicalJoin())) + .when(agg -> agg.child().isAllSlots()) + .when(agg -> agg.child().child().getOtherJoinConjuncts().size() == 0) + .whenNot(agg -> agg.child().children().stream().anyMatch(p -> p instanceof LogicalAggregate)) + .when(agg -> agg.getGroupByExpressions().stream().allMatch(e -> e instanceof Slot)) + .when(agg -> { + Set<AggregateFunction> funcs = agg.getAggregateFunctions(); + return !funcs.isEmpty() && funcs.stream() + .allMatch(f -> f instanceof Sum && f.child(0) instanceof Slot); + }) + .then(agg -> pushSum(agg, agg.child().child(), agg.child().getProjects())) + .toRule(RuleType.PUSHDOWN_SUM_THROUGH_JOIN) + ); + } + + private LogicalAggregate<Plan> pushSum(LogicalAggregate<? extends Plan> agg, + LogicalJoin<Plan, Plan> join, List<NamedExpression> projects) { + List<Slot> leftOutput = join.left().getOutput(); + List<Slot> rightOutput = join.right().getOutput(); + + List<Sum> leftSums = new ArrayList<>(); + List<Sum> rightSums = new ArrayList<>(); + for (AggregateFunction f : agg.getAggregateFunctions()) { + Sum sum = (Sum) f; + Slot slot = (Slot) sum.child(); + if (leftOutput.contains(slot)) { + leftSums.add(sum); + } else if (rightOutput.contains(slot)) { + rightSums.add(sum); + } else { + throw new IllegalStateException("Slot " + slot + " not found in join output"); + } + } + if (leftSums.isEmpty() && rightSums.isEmpty() + || (!leftSums.isEmpty() && !rightSums.isEmpty())) { + return null; + } + + Set<Slot> leftGroupBy = new HashSet<>(); + Set<Slot> rightGroupBy = new HashSet<>(); + agg.getGroupByExpressions().forEach(e -> { + Slot slot = (Slot) e; + if (leftOutput.contains(slot)) { + leftGroupBy.add(slot); + } else if (rightOutput.contains(slot)) { + rightGroupBy.add(slot); + } else { + throw new IllegalStateException("Slot " + slot + " not found in join output"); Review Comment: group by 1 will throw exception -- 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