feiniaofeiafei commented on code in PR #49096: URL: https://github.com/apache/doris/pull/49096#discussion_r2055644741
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/JoinSkewAddSalt.java: ########## @@ -0,0 +1,212 @@ +// 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.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.CaseWhen; +import org.apache.doris.nereids.trees.expressions.Cast; +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.Slot; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.WhenClause; +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.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.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 com.google.common.collect.ImmutableList; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * 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, CASE WHEN t1.a IS NULL THEN random(0, 999) + * WHEN t1.a IN (1, 2) THEN random(0, 999) ELSE 0 END AS r1)) + * | +--LogicalOlapScan(t1) + * +--LogicalProject (projections: t2.a, if(explodeNumber IS NULL, 0, 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) + * */ +public class JoinSkewAddSalt 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"; + + @Override + public Rule build() { + return logicalJoin().thenApply(JoinSkewAddSalt::transform).toRule(RuleType.JOIN_SKEW_ADD_SALT); + } + + private static Plan transform(MatchingContext<LogicalJoin<Plan, Plan>> ctx) { + LogicalJoin<Plan, Plan> join = ctx.root; + int factor = ctx.connectContext.getStatementContext().getConnectContext() + .getSessionVariable().joinSkewAddSaltExplodeFactor; + if (join.getJoinType() != JoinType.INNER_JOIN && join.getJoinType() != JoinType.LEFT_OUTER_JOIN + && join.getJoinType() != JoinType.RIGHT_OUTER_JOIN) { + return null; + } + if (join.isMarkJoin()) { + return null; + } + DistributeHint hint = join.getDistributeHint(); + if (hint.isSuccessInSkew()) { + return null; + } + Expression skewExpr = hint.getSkewExpr(); + if (hint.distributeType != DistributeType.SHUFFLE_RIGHT || skewExpr == null) { + return null; + } + if (!skewExpr.isSlot()) { + return null; + } + if (!join.left().getOutput().contains((Slot) skewExpr)) { + return null; + } + + Expression leftSkewExpr = null; + Expression rightSkewExpr = null; + for (Expression conjunct : join.getHashJoinConjuncts()) { + if (skewExpr.equals(conjunct.child(0))) { + leftSkewExpr = conjunct.child(0); + rightSkewExpr = conjunct.child(1); + break; + } else if (skewExpr.equals(conjunct.child(1))) { + leftSkewExpr = conjunct.child(1); + rightSkewExpr = conjunct.child(1); + break; + } + } + if (leftSkewExpr == null || rightSkewExpr == null) { + return null; + } + Optional<Expression> literalType = TypeCoercionUtils.characterLiteralTypeCoercion(String.valueOf(factor), + TinyIntType.INSTANCE); + if (!literalType.isPresent()) { + throw new RuntimeException(); + } + DataType type = literalType.get().getDataType(); + List<Expression> skewValues = join.getDistributeHint().getSkewValues(); + skewValues = skewValues.stream().filter(e -> !(e instanceof NullLiteral)).collect(Collectors.toList()); Review Comment: i think you are right. -- 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