morrySnow commented on code in PR #22465: URL: https://github.com/apache/doris/pull/22465#discussion_r1281855620
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/OrExpansion.java: ########## @@ -0,0 +1,154 @@ +// 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.exploration; + +import org.apache.doris.common.Pair; +import org.apache.doris.nereids.StatementContext; +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.expressions.EqualTo; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.Not; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEAnchor; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; +import org.apache.doris.nereids.util.ExpressionUtils; +import org.apache.doris.nereids.util.JoinUtils; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * https://blogs.oracle.com/optimizer/post/optimizer-transformations-or-expansion + * NLJ (cond1 or cond2) UnionAll + * => / \ + * HJ(cond1) HJ(cond2 and !cond1) + */ +public class OrExpansion extends OneExplorationRuleFactory { + + @Override + public Rule build() { + return logicalJoin() + .when(JoinUtils::shouldNestedLoopJoin) + .when(join -> join.getJoinType().isInnerJoin()) + .thenApply(ctx -> { + LogicalJoin<? extends Plan, ? extends Plan> join = ctx.root; + Preconditions.checkArgument(join.getHashJoinConjuncts().isEmpty(), + "Only Expansion nest loop join without hashCond"); + List<Expression> disjunctions = null; + List<Expression> otherConditions = Lists.newArrayList(join.getOtherJoinConjuncts()); + + // We pick the first or condition that can be split to EqualTo expressions. + for (Expression expr : otherConditions) { + Pair<List<Expression>, List<Expression>> pair = expandExpr(expr, join); + // TODO: Now we don't support expand condition with complex expression + if (pair.second.isEmpty() && pair.first.stream() + .noneMatch(e -> !((EqualTo) e).left().isSlot() + && !((EqualTo) e).right().isSlot())) { + disjunctions = pair.first; + otherConditions.remove(expr); + break; + } + } + // If there are expression that is not EqualTo, it means there is nlj child + // Therefore refuse this case + if (disjunctions == null) { + return join; + } + //Construct CTE with the children + LogicalCTEProducer<? extends Plan> leftProducer = new LogicalCTEProducer<>( + ctx.statementContext.getNextCTEId(), join.left()); + LogicalCTEProducer<? extends Plan> rightProducer = new LogicalCTEProducer<>( + ctx.statementContext.getNextCTEId(), join.right()); + // expand join to hash join with CTE + List<Plan> joins = expandJoin(ctx.statementContext, disjunctions, otherConditions, join, + leftProducer, + rightProducer); + + LogicalUnion union = new LogicalUnion(Qualifier.ALL, new ArrayList<>(join.getOutput()), + ImmutableList.of(), + false, joins); + LogicalCTEAnchor<? extends Plan, ? extends Plan> intermediateAnchor = new LogicalCTEAnchor<>( + rightProducer.getCteId(), rightProducer, union); + return new LogicalCTEAnchor<Plan, Plan>(leftProducer.getCteId(), leftProducer, intermediateAnchor); + }).toRule(RuleType.OR_EXPANSION); + } + + private Pair<List<Expression>, List<Expression>> expandExpr(Expression otherExpr, + LogicalJoin<? extends Plan, ? extends Plan> join) { + List<Expression> disjunctions = ExpressionUtils.extractDisjunction(otherExpr); + return JoinUtils.extractExpressionForHashTable(join.left().getOutput(), join.right().getOutput(), disjunctions); + } + + private List<Plan> expandJoin(StatementContext ctx, List<Expression> disjunctions, List<Expression> otherConditions, + LogicalJoin<? extends Plan, ? extends Plan> join, LogicalCTEProducer<? extends Plan> leftProducer, + LogicalCTEProducer<? extends Plan> rightProducer) { + List<Expression> notExprs = disjunctions.stream().map(Not::new).collect(Collectors.toList()); + List<Plan> joins = Lists.newArrayList(); + + for (int i = 0; i < disjunctions.size(); i++) { + // construct other condition and hash condition + Pair<List<Expression>, List<Expression>> pair = extractCondition(i, disjunctions, notExprs); Review Comment: use more sense var name ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/OrExpansion.java: ########## @@ -0,0 +1,154 @@ +// 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.exploration; + +import org.apache.doris.common.Pair; +import org.apache.doris.nereids.StatementContext; +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.expressions.EqualTo; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.Not; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEAnchor; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; +import org.apache.doris.nereids.util.ExpressionUtils; +import org.apache.doris.nereids.util.JoinUtils; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * https://blogs.oracle.com/optimizer/post/optimizer-transformations-or-expansion + * NLJ (cond1 or cond2) UnionAll + * => / \ + * HJ(cond1) HJ(cond2 and !cond1) + */ +public class OrExpansion extends OneExplorationRuleFactory { + + @Override + public Rule build() { + return logicalJoin() + .when(JoinUtils::shouldNestedLoopJoin) + .when(join -> join.getJoinType().isInnerJoin()) + .thenApply(ctx -> { + LogicalJoin<? extends Plan, ? extends Plan> join = ctx.root; + Preconditions.checkArgument(join.getHashJoinConjuncts().isEmpty(), + "Only Expansion nest loop join without hashCond"); + List<Expression> disjunctions = null; + List<Expression> otherConditions = Lists.newArrayList(join.getOtherJoinConjuncts()); + + // We pick the first or condition that can be split to EqualTo expressions. + for (Expression expr : otherConditions) { + Pair<List<Expression>, List<Expression>> pair = expandExpr(expr, join); + // TODO: Now we don't support expand condition with complex expression + if (pair.second.isEmpty() && pair.first.stream() + .noneMatch(e -> !((EqualTo) e).left().isSlot() + && !((EqualTo) e).right().isSlot())) { + disjunctions = pair.first; + otherConditions.remove(expr); + break; + } + } + // If there are expression that is not EqualTo, it means there is nlj child + // Therefore refuse this case + if (disjunctions == null) { + return join; + } + //Construct CTE with the children + LogicalCTEProducer<? extends Plan> leftProducer = new LogicalCTEProducer<>( + ctx.statementContext.getNextCTEId(), join.left()); + LogicalCTEProducer<? extends Plan> rightProducer = new LogicalCTEProducer<>( + ctx.statementContext.getNextCTEId(), join.right()); + // expand join to hash join with CTE + List<Plan> joins = expandJoin(ctx.statementContext, disjunctions, otherConditions, join, + leftProducer, + rightProducer); + + LogicalUnion union = new LogicalUnion(Qualifier.ALL, new ArrayList<>(join.getOutput()), + ImmutableList.of(), + false, joins); + LogicalCTEAnchor<? extends Plan, ? extends Plan> intermediateAnchor = new LogicalCTEAnchor<>( + rightProducer.getCteId(), rightProducer, union); + return new LogicalCTEAnchor<Plan, Plan>(leftProducer.getCteId(), leftProducer, intermediateAnchor); + }).toRule(RuleType.OR_EXPANSION); + } + + private Pair<List<Expression>, List<Expression>> expandExpr(Expression otherExpr, + LogicalJoin<? extends Plan, ? extends Plan> join) { + List<Expression> disjunctions = ExpressionUtils.extractDisjunction(otherExpr); + return JoinUtils.extractExpressionForHashTable(join.left().getOutput(), join.right().getOutput(), disjunctions); + } + + private List<Plan> expandJoin(StatementContext ctx, List<Expression> disjunctions, List<Expression> otherConditions, + LogicalJoin<? extends Plan, ? extends Plan> join, LogicalCTEProducer<? extends Plan> leftProducer, + LogicalCTEProducer<? extends Plan> rightProducer) { + List<Expression> notExprs = disjunctions.stream().map(Not::new).collect(Collectors.toList()); + List<Plan> joins = Lists.newArrayList(); + + for (int i = 0; i < disjunctions.size(); i++) { + // construct other condition and hash condition + Pair<List<Expression>, List<Expression>> pair = extractCondition(i, disjunctions, notExprs); + pair.second.addAll(otherConditions); + + LogicalCTEConsumer left = new LogicalCTEConsumer(ctx.getNextRelationId(), leftProducer.getCteId(), "", + leftProducer); + LogicalCTEConsumer right = new LogicalCTEConsumer(ctx.getNextRelationId(), rightProducer.getCteId(), "", + rightProducer); + + //rewrite conjuncts to replace the old slots with CTE slots + Map<Slot, Slot> replaced = new HashMap<>(left.getProducerToConsumerOutputMap()); + replaced.putAll(right.getProducerToConsumerOutputMap()); + List<Expression> hashCond = pair.first.stream() + .map(e -> e.rewriteUp(s -> replaced.containsKey(s) ? replaced.get(s) : s)) + .collect(Collectors.toList()); + List<Expression> otherCond = pair.second.stream() + .map(e -> e.rewriteUp(s -> replaced.containsKey(s) ? replaced.get(s) : s)) + .collect(Collectors.toList()); + + // TODO: normalize join condition + LogicalJoin<? extends Plan, ? extends Plan> newJoin = join.withJoinConjuncts(hashCond, otherCond) + .withChildren(Lists.newArrayList(left, right)); + joins.add(newJoin); + } + return joins; + } + + // For disjunctions: a or b or c + // we can get: (b and !a) union all (c and !a and !b) + private Pair<List<Expression>, List<Expression>> extractCondition(int index, List<Expression> equal, Review Comment: explain the result Pair ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/OrExpansion.java: ########## @@ -0,0 +1,154 @@ +// 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.exploration; + +import org.apache.doris.common.Pair; +import org.apache.doris.nereids.StatementContext; +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.expressions.EqualTo; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.Not; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEAnchor; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; +import org.apache.doris.nereids.util.ExpressionUtils; +import org.apache.doris.nereids.util.JoinUtils; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * https://blogs.oracle.com/optimizer/post/optimizer-transformations-or-expansion + * NLJ (cond1 or cond2) UnionAll + * => / \ + * HJ(cond1) HJ(cond2 and !cond1) + */ +public class OrExpansion extends OneExplorationRuleFactory { + + @Override + public Rule build() { + return logicalJoin() + .when(JoinUtils::shouldNestedLoopJoin) + .when(join -> join.getJoinType().isInnerJoin()) + .thenApply(ctx -> { + LogicalJoin<? extends Plan, ? extends Plan> join = ctx.root; + Preconditions.checkArgument(join.getHashJoinConjuncts().isEmpty(), + "Only Expansion nest loop join without hashCond"); + List<Expression> disjunctions = null; + List<Expression> otherConditions = Lists.newArrayList(join.getOtherJoinConjuncts()); + + // We pick the first or condition that can be split to EqualTo expressions. + for (Expression expr : otherConditions) { + Pair<List<Expression>, List<Expression>> pair = expandExpr(expr, join); + // TODO: Now we don't support expand condition with complex expression + if (pair.second.isEmpty() && pair.first.stream() + .noneMatch(e -> !((EqualTo) e).left().isSlot() + && !((EqualTo) e).right().isSlot())) { + disjunctions = pair.first; + otherConditions.remove(expr); + break; + } + } + // If there are expression that is not EqualTo, it means there is nlj child + // Therefore refuse this case + if (disjunctions == null) { + return join; + } + //Construct CTE with the children + LogicalCTEProducer<? extends Plan> leftProducer = new LogicalCTEProducer<>( + ctx.statementContext.getNextCTEId(), join.left()); + LogicalCTEProducer<? extends Plan> rightProducer = new LogicalCTEProducer<>( + ctx.statementContext.getNextCTEId(), join.right()); + // expand join to hash join with CTE + List<Plan> joins = expandJoin(ctx.statementContext, disjunctions, otherConditions, join, + leftProducer, + rightProducer); + + LogicalUnion union = new LogicalUnion(Qualifier.ALL, new ArrayList<>(join.getOutput()), + ImmutableList.of(), + false, joins); + LogicalCTEAnchor<? extends Plan, ? extends Plan> intermediateAnchor = new LogicalCTEAnchor<>( + rightProducer.getCteId(), rightProducer, union); + return new LogicalCTEAnchor<Plan, Plan>(leftProducer.getCteId(), leftProducer, intermediateAnchor); + }).toRule(RuleType.OR_EXPANSION); + } + + private Pair<List<Expression>, List<Expression>> expandExpr(Expression otherExpr, Review Comment: add comment to explain the result Pair -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
