feiniaofeiafei commented on code in PR #49096: URL: https://github.com/apache/doris/pull/49096#discussion_r2197497714
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/SaltJoin.java: ########## @@ -0,0 +1,383 @@ +// 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.hint.DistributeHint; +import org.apache.doris.nereids.hint.Hint.HintStatus; +import org.apache.doris.nereids.pattern.MatchingContext; +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.rules.exploration.join.JoinReorderContext; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.Cast; +import org.apache.doris.nereids.trees.expressions.ComparisonPredicate; +import org.apache.doris.nereids.trees.expressions.EqualPredicate; +import org.apache.doris.nereids.trees.expressions.EqualTo; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.InPredicate; +import org.apache.doris.nereids.trees.expressions.IsNull; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Not; +import org.apache.doris.nereids.trees.expressions.NullSafeEqual; +import org.apache.doris.nereids.trees.expressions.Or; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.functions.Function; +import org.apache.doris.nereids.trees.expressions.functions.generator.ExplodeNumbers; +import org.apache.doris.nereids.trees.expressions.functions.scalar.If; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Random; +import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral; +import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral; +import org.apache.doris.nereids.trees.expressions.literal.Literal; +import org.apache.doris.nereids.trees.expressions.literal.NullLiteral; +import org.apache.doris.nereids.trees.plans.DistributeType; +import org.apache.doris.nereids.trees.plans.JoinType; +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.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalGenerate; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.IntegerType; +import org.apache.doris.nereids.types.TinyIntType; +import org.apache.doris.nereids.util.TypeCoercionUtils; +import org.apache.doris.nereids.util.Utils; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import org.jetbrains.annotations.Nullable; + +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Current capabilities and limitations of SaltJoin rewrite handling: + * - Supports single-side skew in INNER JOIN, NOT support double-side (both tables) skew + * - Supports left table skew and NOT support right table skew in LEFT JOIN + * - Supports right table skew and Not support left table skew in RIGHT JOIN + * + * INNER JOIN and LEFT JOIN use case: + * Applicable when left table is skewed and right table is too large for broadcast + * + * Here are some examples in rewrite: + * case1: + * LogicalJoin(type:inner, t1.a=t2.a, hint:skew(t1.a(null,1,2))) + * +--LogicalOlapScan(t1) + * +--LogicalOlapScan(t2) + * -> + * LogicalJoin (type:inner, t1.a=t2.a and r1=r2) + * |--LogicalProject (t1.a, if (t1.a IN (1, 2), random(0, 999), DEFAULT_SALT_VALUE) AS r1)) + * | +--LogicalFilter(t1.a is not null) + * | +--LogicalOlapScan(t1) + * +--LogicalProject (projections: t2.a, if(explodeNumber IS NULL, DEFAULT_SALT_VALUE, explodeNumber) as r2) + * +--LogicalJoin (type=right_outer_join, t2.a = skewValue) + * |--LogicalGenerate(generators=[explode_numbers(1000)], generatorOutput=[explodeNumber]) + * | +--LogicalUnion(outputs=[skewValue], constantExprsList(1,2)) + * +--LogicalFilter(t2.a is not null) + * +--LogicalOlapScan(t2) + * + * case2: + * LogicalJoin(type:inner, t1.a=t2.a, hint:skew(t1.a(1,2))) + * +--LogicalOlapScan(t1) + * +--LogicalOlapScan(t2) + * -> + * LogicalJoin (type:inner, t1.a=t2.a and r1=r2) + * |--LogicalProject (t1.a, if (t1.a IN (1, 2), random(0, 999), DEFAULT_SALT_VALUE) AS r1)) + * | +--LogicalOlapScan(t1) + * +--LogicalProject (projections: t2.a, if(explodeNumber IS NULL, DEFAULT_SALT_VALUE, explodeNumber) as r2) + * +--LogicalJoin (type=right_outer_join, t2.a = skewValue) + * |--LogicalGenerate(generators=[explode_numbers(1000)], generatorOutput=[explodeNumber]) + * | +--LogicalUnion(outputs=[skewValue], constantExprsList(1,2)) + * +--LogicalOlapScan(t2) + * + * case3: not optimize, because rows will not be output in join when join key is null + * LogicalJoin(type:inner, t1.a=t2.a, hint:skew(t1.a(null))) + * |--LogicalOlapScan(t1) + * +--LogicalOlapScan(t2) + * -> + * LogicalJoin(type:inner, t1.a=t2.a) + * |--LogicalFilter(t1.a is not null) + * | +--LogicalOlapScan(t1) + * +--LogicalFilter(t2.a is not null) + * +--LogicalOlapScan(t2) + * */ +public class SaltJoin extends OneRewriteRuleFactory { + private static final String RANDOM_COLUMN_NAME_LEFT = "r1"; + private static final String RANDOM_COLUMN_NAME_RIGHT = "r2"; + private static final String SKEW_VALUE_COLUMN_NAME = "skewValue"; + private static final String EXPLODE_NUMBER_COLUMN_NAME = "explodeColumn"; + private static final int SALT_FACTOR = 4; + private static final int DEFAULT_SALT_VALUE = 0; + + @Override + public Rule build() { + return logicalJoin() + .when(join -> join.getJoinType().isOneSideOuterJoin() || join.getJoinType().isInnerJoin()) + .when(join -> join.getDistributeHint() != null && join.getDistributeHint().getSkewInfo() != null) + .whenNot(LogicalJoin::isMarkJoin) + .whenNot(join -> join.getDistributeHint().isSuccessInSkewRewrite()) + .thenApply(SaltJoin::transform).toRule(RuleType.SALT_JOIN); + } + + private static Plan transform(MatchingContext<LogicalJoin<Plan, Plan>> ctx) { + LogicalJoin<Plan, Plan> join = ctx.root; + DistributeHint hint = join.getDistributeHint(); + if (hint.distributeType != DistributeType.SHUFFLE_RIGHT) { + return null; + } + Expression skewExpr = hint.getSkewExpr(); + if (!skewExpr.isSlot()) { + return null; + } + if ((join.getJoinType().isLeftOuterJoin() || join.getJoinType().isInnerJoin()) + && !join.left().getOutput().contains((Slot) skewExpr) + || join.getJoinType().isRightOuterJoin() && !join.right().getOutput().contains((Slot) skewExpr)) { + return null; + } + int factor = getSaltFactor(ctx); + Optional<Expression> literalType = TypeCoercionUtils.characterLiteralTypeCoercion(String.valueOf(factor), + TinyIntType.INSTANCE); + if (!literalType.isPresent()) { + return null; + } + Expression leftSkewExpr = null; + Expression rightSkewExpr = null; + Expression skewConjunct = null; + for (Expression conjunct : join.getHashJoinConjuncts()) { + if (skewExpr.equals(conjunct.child(0)) || skewExpr.equals(conjunct.child(1))) { + if (join.left().getOutputSet().contains((Slot) conjunct.child(0)) + && join.right().getOutputSet().contains((Slot) conjunct.child(1))) { + skewConjunct = conjunct; + } else if (join.left().getOutputSet().contains((Slot) conjunct.child(1)) + && join.right().getOutputSet().contains((Slot) conjunct.child(0))) { + skewConjunct = ((ComparisonPredicate) conjunct).commute(); + } else { + return null; + } + leftSkewExpr = skewConjunct.child(0); + rightSkewExpr = skewConjunct.child(1); + break; + } + } + if (leftSkewExpr == null || rightSkewExpr == null) { + return null; + } + List<Expression> skewValues = join.getDistributeHint().getSkewValues(); + Set<Expression> skewValuesSet = new HashSet<>(skewValues); + List<Expression> expandSideValues = getSaltedSkewValuesForExpandSide(skewConjunct, skewValuesSet); + List<Expression> skewSideValues = getSaltedSkewValuesForSkewSide(skewConjunct, skewValuesSet, join); + if (skewSideValues.isEmpty()) { Review Comment: When there is only a null in skew values list, the explandSideValues is empty, expandSkewValueRows() has a path when explandSideValues is empty. -- 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]
