morrySnow commented on code in PR #11805: URL: https://github.com/apache/doris/pull/11805#discussion_r971821575
########## fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/batch/SingleSidePredicateJob.java: ########## @@ -0,0 +1,41 @@ +// 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.jobs.batch; + +import org.apache.doris.nereids.CascadesContext; +import org.apache.doris.nereids.rules.rewrite.logical.SingleSidePredicate; + +import com.google.common.collect.ImmutableList; + +/** + * push down not slot reference expression job + */ +public class SingleSidePredicateJob extends BatchRulesJob { + + /** + * constructor + */ + public SingleSidePredicateJob(CascadesContext cascadesContext) { + super(cascadesContext); + rulesJob.addAll(ImmutableList.of( + topDownBatch(ImmutableList.of( + new SingleSidePredicate() Review Comment: add it into rewrite job ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/SingleSidePredicate.java: ########## @@ -0,0 +1,112 @@ +// 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.logical; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.EqualTo; +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.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * push down expression which is not slot reference + */ +public class SingleSidePredicate extends OneRewriteRuleFactory { + /* + * rewrite example: + * join(t1.a + 1 = t2.b + 2) join(c = d) + * / \ / \ + * / \ / \ + * / \ ====> / \ + * / \ / \ + * olapScan(t1) olapScan(t2) project(t1.a + 1 as c) project(t2.b + 2 as d) + * | | + * | | + * | | + * | | + * olapScan(t1) olapScan(t2) + *TODO: now t1.a + t2.a = t1.b is not in hashJoinConjuncts. The rule will not handle it. + */ + @Override + public Rule build() { + return logicalJoin() + .when(join -> join.getHashJoinConjuncts().stream().anyMatch(expr -> + expr.children().stream().anyMatch(expr1 -> !(expr1 instanceof Slot)))) + .then(join -> { + List<List<Expression>> exprsOfJoinRelation = + Lists.newArrayList(Lists.newArrayList(), Lists.newArrayList()); + Map<Expression, Alias> exprMap = Maps.newHashMap(); + Plan left = join.left(); + join.getHashJoinConjuncts().forEach(conjunct -> { + Preconditions.checkArgument(conjunct instanceof EqualTo); + Expression[] exprs = conjunct.children().toArray(new Expression[0]); Review Comment: why transform to array? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/SingleSidePredicate.java: ########## @@ -0,0 +1,112 @@ +// 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.logical; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.EqualTo; +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.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * push down expression which is not slot reference + */ +public class SingleSidePredicate extends OneRewriteRuleFactory { + /* + * rewrite example: + * join(t1.a + 1 = t2.b + 2) join(c = d) + * / \ / \ + * / \ / \ + * / \ ====> / \ + * / \ / \ + * olapScan(t1) olapScan(t2) project(t1.a + 1 as c) project(t2.b + 2 as d) + * | | + * | | + * | | + * | | + * olapScan(t1) olapScan(t2) + *TODO: now t1.a + t2.a = t1.b is not in hashJoinConjuncts. The rule will not handle it. + */ + @Override + public Rule build() { + return logicalJoin() + .when(join -> join.getHashJoinConjuncts().stream().anyMatch(expr -> + expr.children().stream().anyMatch(expr1 -> !(expr1 instanceof Slot)))) + .then(join -> { + List<List<Expression>> exprsOfJoinRelation = + Lists.newArrayList(Lists.newArrayList(), Lists.newArrayList()); + Map<Expression, Alias> exprMap = Maps.newHashMap(); + Plan left = join.left(); + join.getHashJoinConjuncts().forEach(conjunct -> { + Preconditions.checkArgument(conjunct instanceof EqualTo); + Expression[] exprs = conjunct.children().toArray(new Expression[0]); + // sometimes: t1 join t2 on t2.a + 1 = t1.a + 2, so check the situation, but actually it + // doesn't swap the two sides. + int tag = checkIfSwap(exprs[0], left); + exprsOfJoinRelation.get(0).add(exprs[tag]); + exprsOfJoinRelation.get(1).add(exprs[tag ^ 1]); + Arrays.stream(exprs).sequential().forEach(expr -> + exprMap.put(expr, new Alias(expr, "expr_" + expr.hashCode()))); + }); + Iterator<List<Expression>> iter = exprsOfJoinRelation.iterator(); + return join.withhashJoinConjunctsAndChildren( + join.getHashJoinConjuncts() + .stream().map(equalTo -> equalTo.withChildren(equalTo.children() + .stream().map(expr -> exprMap.get(expr).toSlot()) + .collect(Collectors.toList()))) + .collect(Collectors.toList()), + join.children().stream().map( + plan -> new LogicalProject<>(new ImmutableList.Builder<NamedExpression>() + .addAll(iter.next().stream().map(expr -> exprMap.get(expr)) + .collect(Collectors.toList())) + .addAll(getOutput(plan, join)).build(), plan)) + .collect(Collectors.toList())); + }).toRule(RuleType.PUSH_DOWN_NOT_SLOT_REFERENCE_EXPRESSION); + } + + int checkIfSwap(Expression left, Plan joinLeft) { + Set<Expression> joinOut = ImmutableSet.copyOf(joinLeft.getOutput()); + return left.anyMatch(expr -> (expr instanceof Slot) && joinOut.contains(expr)) ? 0 : 1; + } + + private List<Slot> getOutput(Plan plan, LogicalJoin join) { + Set<Slot> set = Sets.newHashSet(plan.getOutputSet()); + set.retainAll(join.getOutputSet()); + return Arrays.asList(set.toArray(new Slot[0])); Review Comment: if u wanna turn set into list. just use Lists.newArrayList(set); BTW, `set` is not a good name ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/SingleSidePredicate.java: ########## @@ -0,0 +1,112 @@ +// 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.logical; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.EqualTo; +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.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * push down expression which is not slot reference + */ +public class SingleSidePredicate extends OneRewriteRuleFactory { + /* + * rewrite example: + * join(t1.a + 1 = t2.b + 2) join(c = d) + * / \ / \ + * / \ / \ + * / \ ====> / \ + * / \ / \ + * olapScan(t1) olapScan(t2) project(t1.a + 1 as c) project(t2.b + 2 as d) + * | | + * | | + * | | + * | | + * olapScan(t1) olapScan(t2) + *TODO: now t1.a + t2.a = t1.b is not in hashJoinConjuncts. The rule will not handle it. + */ + @Override + public Rule build() { + return logicalJoin() + .when(join -> join.getHashJoinConjuncts().stream().anyMatch(expr -> + expr.children().stream().anyMatch(expr1 -> !(expr1 instanceof Slot)))) + .then(join -> { + List<List<Expression>> exprsOfJoinRelation = + Lists.newArrayList(Lists.newArrayList(), Lists.newArrayList()); Review Comment: a pair of list maybe better. BTW, `exprsOfJoinRelation` is not a good name ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/SingleSidePredicate.java: ########## @@ -0,0 +1,112 @@ +// 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.logical; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.EqualTo; +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.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * push down expression which is not slot reference + */ +public class SingleSidePredicate extends OneRewriteRuleFactory { Review Comment: ```suggestion public class PushDownExpressionsInHashCondition extends OneRewriteRuleFactory { ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/SingleSidePredicate.java: ########## @@ -0,0 +1,112 @@ +// 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.logical; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.EqualTo; +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.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * push down expression which is not slot reference + */ +public class SingleSidePredicate extends OneRewriteRuleFactory { + /* + * rewrite example: + * join(t1.a + 1 = t2.b + 2) join(c = d) + * / \ / \ + * / \ / \ + * / \ ====> / \ + * / \ / \ + * olapScan(t1) olapScan(t2) project(t1.a + 1 as c) project(t2.b + 2 as d) + * | | + * | | + * | | + * | | + * olapScan(t1) olapScan(t2) + *TODO: now t1.a + t2.a = t1.b is not in hashJoinConjuncts. The rule will not handle it. + */ + @Override + public Rule build() { + return logicalJoin() + .when(join -> join.getHashJoinConjuncts().stream().anyMatch(expr -> + expr.children().stream().anyMatch(expr1 -> !(expr1 instanceof Slot)))) Review Comment: ```suggestion .when(join -> join.getHashJoinConjuncts().stream().anyMatch(equalTo -> equalTo.children().stream().anyMatch(e -> !(e instanceof Slot)))) ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/SingleSidePredicate.java: ########## @@ -0,0 +1,112 @@ +// 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.logical; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.EqualTo; +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.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * push down expression which is not slot reference + */ +public class SingleSidePredicate extends OneRewriteRuleFactory { + /* + * rewrite example: + * join(t1.a + 1 = t2.b + 2) join(c = d) + * / \ / \ + * / \ / \ + * / \ ====> / \ + * / \ / \ + * olapScan(t1) olapScan(t2) project(t1.a + 1 as c) project(t2.b + 2 as d) + * | | + * | | + * | | + * | | + * olapScan(t1) olapScan(t2) + *TODO: now t1.a + t2.a = t1.b is not in hashJoinConjuncts. The rule will not handle it. + */ + @Override + public Rule build() { + return logicalJoin() + .when(join -> join.getHashJoinConjuncts().stream().anyMatch(expr -> + expr.children().stream().anyMatch(expr1 -> !(expr1 instanceof Slot)))) + .then(join -> { + List<List<Expression>> exprsOfJoinRelation = + Lists.newArrayList(Lists.newArrayList(), Lists.newArrayList()); + Map<Expression, Alias> exprMap = Maps.newHashMap(); + Plan left = join.left(); + join.getHashJoinConjuncts().forEach(conjunct -> { + Preconditions.checkArgument(conjunct instanceof EqualTo); + Expression[] exprs = conjunct.children().toArray(new Expression[0]); + // sometimes: t1 join t2 on t2.a + 1 = t1.a + 2, so check the situation, but actually it + // doesn't swap the two sides. + int tag = checkIfSwap(exprs[0], left); + exprsOfJoinRelation.get(0).add(exprs[tag]); + exprsOfJoinRelation.get(1).add(exprs[tag ^ 1]); Review Comment: i think `1 - tag` is better to understand ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/SingleSidePredicate.java: ########## @@ -0,0 +1,112 @@ +// 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.logical; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.EqualTo; +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.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * push down expression which is not slot reference + */ +public class SingleSidePredicate extends OneRewriteRuleFactory { + /* + * rewrite example: + * join(t1.a + 1 = t2.b + 2) join(c = d) + * / \ / \ + * / \ / \ + * / \ ====> / \ + * / \ / \ + * olapScan(t1) olapScan(t2) project(t1.a + 1 as c) project(t2.b + 2 as d) + * | | + * | | + * | | + * | | + * olapScan(t1) olapScan(t2) + *TODO: now t1.a + t2.a = t1.b is not in hashJoinConjuncts. The rule will not handle it. + */ + @Override + public Rule build() { + return logicalJoin() + .when(join -> join.getHashJoinConjuncts().stream().anyMatch(expr -> + expr.children().stream().anyMatch(expr1 -> !(expr1 instanceof Slot)))) + .then(join -> { + List<List<Expression>> exprsOfJoinRelation = + Lists.newArrayList(Lists.newArrayList(), Lists.newArrayList()); + Map<Expression, Alias> exprMap = Maps.newHashMap(); + Plan left = join.left(); + join.getHashJoinConjuncts().forEach(conjunct -> { + Preconditions.checkArgument(conjunct instanceof EqualTo); + Expression[] exprs = conjunct.children().toArray(new Expression[0]); + // sometimes: t1 join t2 on t2.a + 1 = t1.a + 2, so check the situation, but actually it + // doesn't swap the two sides. + int tag = checkIfSwap(exprs[0], left); + exprsOfJoinRelation.get(0).add(exprs[tag]); + exprsOfJoinRelation.get(1).add(exprs[tag ^ 1]); + Arrays.stream(exprs).sequential().forEach(expr -> + exprMap.put(expr, new Alias(expr, "expr_" + expr.hashCode()))); + }); + Iterator<List<Expression>> iter = exprsOfJoinRelation.iterator(); + return join.withhashJoinConjunctsAndChildren( + join.getHashJoinConjuncts() + .stream().map(equalTo -> equalTo.withChildren(equalTo.children() + .stream().map(expr -> exprMap.get(expr).toSlot()) + .collect(Collectors.toList()))) + .collect(Collectors.toList()), + join.children().stream().map( + plan -> new LogicalProject<>(new ImmutableList.Builder<NamedExpression>() + .addAll(iter.next().stream().map(expr -> exprMap.get(expr)) + .collect(Collectors.toList())) + .addAll(getOutput(plan, join)).build(), plan)) + .collect(Collectors.toList())); + }).toRule(RuleType.PUSH_DOWN_NOT_SLOT_REFERENCE_EXPRESSION); + } + + int checkIfSwap(Expression left, Plan joinLeft) { + Set<Expression> joinOut = ImmutableSet.copyOf(joinLeft.getOutput()); + return left.anyMatch(expr -> (expr instanceof Slot) && joinOut.contains(expr)) ? 0 : 1; + } Review Comment: we have similar code in JoinUtils, u could use it or put ur code into it -- 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