-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature](Nereids): eliminate semi join (#28588)
Eliminate Semi/Anti Join which is FALSE or TRUE.
- Loading branch information
Showing
4 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateSemiJoin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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.StatementScopeIdGenerator; | ||
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.LogicalEmptyRelation; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Eliminate Semi/Anti Join which is FALSE or TRUE. | ||
*/ | ||
public class EliminateSemiJoin extends OneRewriteRuleFactory { | ||
@Override | ||
public Rule build() { | ||
return logicalJoin() | ||
// right will be converted to left | ||
.when(join -> join.getJoinType().isLeftSemiOrAntiJoin()) | ||
.when(join -> join.getHashJoinConjuncts().isEmpty()) | ||
.then(join -> { | ||
List<Expression> otherJoinConjuncts = join.getOtherJoinConjuncts(); | ||
JoinType joinType = join.getJoinType(); | ||
|
||
boolean condition; | ||
if (otherJoinConjuncts.isEmpty()) { | ||
condition = true; | ||
} else if (otherJoinConjuncts.size() == 1) { | ||
if (otherJoinConjuncts.get(0).equals(BooleanLiteral.TRUE)) { | ||
condition = true; | ||
} else if (otherJoinConjuncts.get(0).equals(BooleanLiteral.FALSE)) { | ||
condition = false; | ||
} else { | ||
return null; | ||
} | ||
} else { | ||
return null; | ||
} | ||
if (joinType == JoinType.LEFT_SEMI_JOIN && condition | ||
|| (joinType == JoinType.LEFT_ANTI_JOIN && !condition)) { | ||
return join.left(); | ||
} else if (joinType == JoinType.LEFT_SEMI_JOIN && !condition | ||
|| (joinType == JoinType.LEFT_ANTI_JOIN && condition)) { | ||
return new LogicalEmptyRelation(StatementScopeIdGenerator.newRelationId(), join.getOutput()); | ||
} else { | ||
throw new IllegalStateException("Unexpected join type: " + joinType); | ||
} | ||
}) | ||
.toRule(RuleType.ELIMINATE_SEMI_JOIN); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/EliminateSemiJoinTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// 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.util.MemoPatternMatchSupported; | ||
import org.apache.doris.nereids.util.PlanChecker; | ||
import org.apache.doris.utframe.TestWithFeService; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class EliminateSemiJoinTest extends TestWithFeService implements MemoPatternMatchSupported { | ||
|
||
@Override | ||
protected void runBeforeAll() throws Exception { | ||
createDatabase("test"); | ||
|
||
connectContext.setDatabase("test"); | ||
|
||
createTable("CREATE TABLE t (" | ||
+ "id int not null" | ||
+ ")\n" | ||
+ "DISTRIBUTED BY HASH(id)\n" | ||
+ "BUCKETS 1\n" | ||
+ "PROPERTIES(\n" | ||
+ " \"replication_num\"=\"1\"\n" | ||
+ ");"); | ||
} | ||
|
||
@Test | ||
void semiTrue() { | ||
String sql = "select * from t t1 left semi join t t2 on true"; | ||
|
||
PlanChecker.from(connectContext) | ||
.analyze(sql) | ||
.rewrite() | ||
.matches( | ||
logicalResultSink( | ||
logicalOlapScan() | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void semiFalse() { | ||
String sql = "select * from t t1 left semi join t t2 on false"; | ||
|
||
PlanChecker.from(connectContext) | ||
.analyze(sql) | ||
.rewrite() | ||
.matches( | ||
logicalEmptyRelation() | ||
); | ||
} | ||
|
||
@Test | ||
void antiTrue() { | ||
String sql = "select * from t t1 left anti join t t2 on true"; | ||
|
||
PlanChecker.from(connectContext) | ||
.analyze(sql) | ||
.rewrite() | ||
.matches( | ||
logicalEmptyRelation() | ||
); | ||
} | ||
|
||
@Test | ||
void antiFalse() { | ||
String sql = "select * from t t1 left anti join t t2 on false"; | ||
|
||
PlanChecker.from(connectContext) | ||
.analyze(sql) | ||
.rewrite() | ||
.matches( | ||
logicalResultSink( | ||
logicalOlapScan() | ||
) | ||
); | ||
} | ||
|
||
} |