[feature](Nereids): remove True in Join condition (#26951)

Remove `True` in Join Condition like `SELECT * FROM t1 JOIN t2 on True`;
This commit is contained in:
jakevin
2023-11-15 15:58:33 +08:00
committed by GitHub
parent 760c6cdeab
commit 2f529c1c7b
7 changed files with 732 additions and 1 deletions

View File

@ -56,6 +56,7 @@ import org.apache.doris.nereids.rules.rewrite.EliminateAssertNumRows;
import org.apache.doris.nereids.rules.rewrite.EliminateDedupJoinCondition;
import org.apache.doris.nereids.rules.rewrite.EliminateEmptyRelation;
import org.apache.doris.nereids.rules.rewrite.EliminateFilter;
import org.apache.doris.nereids.rules.rewrite.EliminateJoinCondition;
import org.apache.doris.nereids.rules.rewrite.EliminateLimit;
import org.apache.doris.nereids.rules.rewrite.EliminateNotNull;
import org.apache.doris.nereids.rules.rewrite.EliminateNullAwareLeftAntiJoin;
@ -173,6 +174,7 @@ public class Rewriter extends AbstractBatchJobExecutor {
new EliminateLimit(),
new EliminateFilter(),
new EliminateAggregate(),
new EliminateJoinCondition(),
new EliminateAssertNumRows()
)
),

View File

@ -201,6 +201,8 @@ public enum RuleType {
ELIMINATE_LIMIT_ON_ONE_ROW_RELATION(RuleTypeClass.REWRITE),
ELIMINATE_LIMIT_ON_EMPTY_RELATION(RuleTypeClass.REWRITE),
ELIMINATE_FILTER(RuleTypeClass.REWRITE),
ELIMINATE_JOIN(RuleTypeClass.REWRITE),
ELIMINATE_JOIN_CONDITION(RuleTypeClass.REWRITE),
ELIMINATE_FILTER_ON_ONE_RELATION(RuleTypeClass.REWRITE),
ELIMINATE_NOT_NULL(RuleTypeClass.REWRITE),
ELIMINATE_UNNECESSARY_PROJECT(RuleTypeClass.REWRITE),

View File

@ -0,0 +1,49 @@
// 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.Expression;
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
import java.util.List;
import java.util.stream.Collectors;
/**
* Eliminate true Condition in Join Condition.
*/
public class EliminateJoinCondition extends OneRewriteRuleFactory {
@Override
public Rule build() {
return logicalJoin().then(join -> {
List<Expression> hashJoinConjuncts = join.getHashJoinConjuncts().stream()
.filter(expression -> !expression.equals(BooleanLiteral.TRUE))
.collect(Collectors.toList());
List<Expression> otherJoinConjuncts = join.getOtherJoinConjuncts().stream()
.filter(expression -> !expression.equals(BooleanLiteral.TRUE))
.collect(Collectors.toList());
if (hashJoinConjuncts.size() == join.getHashJoinConjuncts().size()
&& otherJoinConjuncts.size() == join.getOtherJoinConjuncts().size()) {
return null;
}
return join.withJoinConjuncts(hashJoinConjuncts, otherJoinConjuncts);
}).toRule(RuleType.ELIMINATE_JOIN_CONDITION);
}
}

View File

@ -61,7 +61,7 @@ public class BooleanLiteral extends Literal {
@Override
public String toString() {
return Boolean.valueOf(value).toString().toUpperCase();
return Boolean.toString(value).toUpperCase();
}
@Override

View File

@ -0,0 +1,51 @@
// 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.trees.expressions.literal.BooleanLiteral;
import org.apache.doris.nereids.trees.plans.JoinType;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.nereids.util.LogicalPlanBuilder;
import org.apache.doris.nereids.util.MemoPatternMatchSupported;
import org.apache.doris.nereids.util.MemoTestUtils;
import org.apache.doris.nereids.util.PlanChecker;
import org.apache.doris.nereids.util.PlanConstructor;
import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Test;
class EliminateJoinConditionTest implements MemoPatternMatchSupported {
private final LogicalOlapScan scan1 = PlanConstructor.newLogicalOlapScan(0, "t1", 0);
private final LogicalOlapScan scan2 = PlanConstructor.newLogicalOlapScan(1, "t2", 0);
@Test
void basicCase() {
LogicalPlan filterFalse = new LogicalPlanBuilder(scan1)
.join(scan2, JoinType.INNER_JOIN, ImmutableList.of(BooleanLiteral.TRUE),
ImmutableList.of(BooleanLiteral.TRUE))
.build();
PlanChecker.from(MemoTestUtils.createConnectContext(), filterFalse)
.applyTopDown(new EliminateJoinCondition())
.matches(
logicalJoin().when(join -> join.getHashJoinConjuncts().size() == 0
&& join.getOtherJoinConjuncts().size() == 0)
);
}
}