You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by yi...@apache.org on 2024/01/25 13:34:36 UTC
(doris) branch branch-2.1 updated (a707ac65ec9 -> e8a20d8899f)
This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a change to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
discard a707ac65ec9 Fix be cmake coverage linker flag (#30387)
omit c048a20a414 [fix](meta) remove default_cluster prefix in recover db log (#30350)
omit 8c5c31ce061 Fix statistics p0. (#30351)
omit c88960b4b03 [improve](routine-load) optimize error msg when failed to fetch Kafka info #30298
omit dcdaf9a5689 [feat](Nereids): eliminate inner join by pk fk when comparing mv (#30258)
omit 7258e394cb0 [fix](Nereids): when predicate contains right output, don't convert outer to anti join (#30276)
omit bca91c3da4b [refactor](cmake) Refactor be CMakeLists ENABLE_CLANG_COVERAGE CMAKE_CXX_FLAGS byadd_compile_options (#30333)
new 0a5c375068a [fix](Nereids): when predicate contains right output, don't convert outer to anti join (#30276)
new 0f81ecf4152 [feat](Nereids): eliminate inner join by pk fk when comparing mv (#30258)
new 92d4ce31ae5 [improve](routine-load) optimize error msg when failed to fetch Kafka info #30298
new 9174ac921b3 Fix statistics p0. (#30351)
new e8a20d8899f [fix](meta) remove default_cluster prefix in recover db log (#30350)
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (a707ac65ec9)
\
N -- N -- N refs/heads/branch-2.1 (e8a20d8899f)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
be/CMakeLists.txt | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org
(doris) 04/05: Fix statistics p0. (#30351)
Posted by yi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
commit 9174ac921b394ac2888f75704ee4be421dfea8ee
Author: Jibing-Li <64...@users.noreply.github.com>
AuthorDate: Thu Jan 25 14:30:17 2024 +0800
Fix statistics p0. (#30351)
---
regression-test/suites/statistics/analyze_stats.groovy | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/regression-test/suites/statistics/analyze_stats.groovy b/regression-test/suites/statistics/analyze_stats.groovy
index fc047ac42d2..78a477f9ab5 100644
--- a/regression-test/suites/statistics/analyze_stats.groovy
+++ b/regression-test/suites/statistics/analyze_stats.groovy
@@ -2753,7 +2753,7 @@ PARTITION `p599` VALUES IN (599)
}
// Test analyze default full.
- sql """analyze table trigger_test"""
+ sql """analyze table trigger_test with sync"""
def result = sql """show column stats trigger_test"""
assertEquals(2, result.size())
assertEquals("4.0", result[0][1])
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org
(doris) 01/05: [fix](Nereids): when predicate contains right output, don't convert outer to anti join (#30276)
Posted by yi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
commit 0a5c375068ad708966df2e5755546db5c1a66d79
Author: 谢健 <ji...@gmail.com>
AuthorDate: Thu Jan 25 14:01:27 2024 +0800
[fix](Nereids): when predicate contains right output, don't convert outer to anti join (#30276)
---
.../rules/rewrite/ConvertOuterJoinToAntiJoin.java | 6 +-
.../rewrite/ConvertOuterJoinToAntiJoinTest.java | 82 +++++++++++++++++++++-
2 files changed, 84 insertions(+), 4 deletions(-)
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ConvertOuterJoinToAntiJoin.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ConvertOuterJoinToAntiJoin.java
index ebc4630d78e..74bd7e29142 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ConvertOuterJoinToAntiJoin.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ConvertOuterJoinToAntiJoin.java
@@ -81,7 +81,8 @@ public class ConvertOuterJoinToAntiJoin extends OneRewriteRuleFactory {
&& rightAlwaysNullSlots.containsAll(p.getInputSlots())))
.collect(ImmutableSet.toImmutableSet());
boolean containRightSlot = predicates.stream()
- .anyMatch(s -> join.right().getOutputSet().containsAll(s.getInputSlots()));
+ .flatMap(p -> p.getInputSlots().stream())
+ .anyMatch(join.right().getOutputSet()::contains);
if (!containRightSlot) {
res = join.withJoinType(JoinType.LEFT_ANTI_JOIN);
res = predicates.isEmpty() ? res : filter.withConjuncts(predicates).withChildren(res);
@@ -94,7 +95,8 @@ public class ConvertOuterJoinToAntiJoin extends OneRewriteRuleFactory {
&& leftAlwaysNullSlots.containsAll(p.getInputSlots())))
.collect(ImmutableSet.toImmutableSet());
boolean containLeftSlot = predicates.stream()
- .anyMatch(s -> join.left().getOutputSet().containsAll(s.getInputSlots()));
+ .flatMap(p -> p.getInputSlots().stream())
+ .anyMatch(join.left().getOutputSet()::contains);
if (!containLeftSlot) {
res = join.withJoinType(JoinType.RIGHT_ANTI_JOIN);
res = predicates.isEmpty() ? res : filter.withConjuncts(predicates).withChildren(res);
diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/ConvertOuterJoinToAntiJoinTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/ConvertOuterJoinToAntiJoinTest.java
index 49960c77ee4..20b36d3272e 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/ConvertOuterJoinToAntiJoinTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/ConvertOuterJoinToAntiJoinTest.java
@@ -18,8 +18,12 @@
package org.apache.doris.nereids.rules.rewrite;
import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.trees.expressions.And;
+import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.IsNull;
+import org.apache.doris.nereids.trees.expressions.Or;
import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
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;
@@ -30,6 +34,7 @@ import org.apache.doris.nereids.util.PlanChecker;
import org.apache.doris.nereids.util.PlanConstructor;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Sets;
import org.junit.jupiter.api.Test;
class ConvertOuterJoinToAntiJoinTest implements MemoPatternMatchSupported {
@@ -48,7 +53,7 @@ class ConvertOuterJoinToAntiJoinTest implements MemoPatternMatchSupported {
LogicalPlan plan = new LogicalPlanBuilder(scan1)
.join(scan2, JoinType.LEFT_OUTER_JOIN, Pair.of(0, 0)) // t1.id = t2.id
.filter(new IsNull(scan2.getOutput().get(0)))
- .project(ImmutableList.of(0, 1))
+ .projectExprs(ImmutableList.copyOf(scan1.getOutput()))
.build();
PlanChecker.from(MemoTestUtils.createConnectContext(), plan)
@@ -63,7 +68,7 @@ class ConvertOuterJoinToAntiJoinTest implements MemoPatternMatchSupported {
LogicalPlan plan = new LogicalPlanBuilder(scan1)
.join(scan2, JoinType.RIGHT_OUTER_JOIN, Pair.of(0, 0)) // t1.id = t2.id
.filter(new IsNull(scan1.getOutput().get(0)))
- .project(ImmutableList.of(2, 3))
+ .projectExprs(ImmutableList.copyOf(scan2.getOutput()))
.build();
PlanChecker.from(MemoTestUtils.createConnectContext(), plan)
@@ -72,4 +77,77 @@ class ConvertOuterJoinToAntiJoinTest implements MemoPatternMatchSupported {
.printlnTree()
.matches(logicalJoin().when(join -> join.getJoinType().isRightAntiJoin()));
}
+
+ @Test
+ void testEliminateLeftWithLeftPredicate() {
+ LogicalPlan plan = new LogicalPlanBuilder(scan1)
+ .join(scan2, JoinType.LEFT_OUTER_JOIN, Pair.of(0, 0)) // t1.id = t2.id
+ .filter(Sets.newHashSet(
+ new IsNull(scan2.getOutput().get(0)),
+ new EqualTo(scan1.getOutput().get(0), new IntegerLiteral(1)))
+ )
+ .projectExprs(ImmutableList.copyOf(scan1.getOutput()))
+ .build();
+
+ PlanChecker.from(MemoTestUtils.createConnectContext(), plan)
+ .applyTopDown(new InferFilterNotNull())
+ .applyTopDown(new ConvertOuterJoinToAntiJoin())
+ .printlnTree()
+ .matches(logicalJoin().when(join -> join.getJoinType().isLeftAntiJoin()));
+ }
+
+ @Test
+ void testEliminateLeftWithRightPredicate() {
+ LogicalPlan plan = new LogicalPlanBuilder(scan1)
+ .join(scan2, JoinType.LEFT_OUTER_JOIN, Pair.of(0, 0)) // t1.id = t2.id
+ .filter(Sets.newHashSet(
+ new IsNull(scan2.getOutput().get(0)),
+ new EqualTo(scan2.getOutput().get(0), new IntegerLiteral(1)))
+ )
+ .projectExprs(ImmutableList.copyOf(scan1.getOutput()))
+ .build();
+
+ PlanChecker.from(MemoTestUtils.createConnectContext(), plan)
+ .applyTopDown(new InferFilterNotNull())
+ .applyTopDown(new ConvertOuterJoinToAntiJoin())
+ .printlnTree()
+ .matches(logicalJoin().when(join -> join.getJoinType().isLeftOuterJoin()));
+ }
+
+ @Test
+ void testEliminateLeftWithOrPredicate() {
+ LogicalPlan plan = new LogicalPlanBuilder(scan1)
+ .join(scan2, JoinType.LEFT_OUTER_JOIN, Pair.of(0, 0)) // t1.id = t2.id
+ .filter(Sets.newHashSet(
+ new IsNull(scan1.getOutput().get(0)),
+ new Or(new IsNull(scan1.getOutput().get(0)), new IsNull(scan2.getOutput().get(0))))
+ )
+ .projectExprs(ImmutableList.copyOf(scan1.getOutput()))
+ .build();
+
+ PlanChecker.from(MemoTestUtils.createConnectContext(), plan)
+ .applyTopDown(new InferFilterNotNull())
+ .applyTopDown(new ConvertOuterJoinToAntiJoin())
+ .printlnTree()
+ .matches(logicalJoin().when(join -> join.getJoinType().isLeftOuterJoin()));
+ }
+
+ @Test
+ void testEliminateLeftWithAndPredicate() {
+ LogicalPlan plan = new LogicalPlanBuilder(scan1)
+ .join(scan2, JoinType.LEFT_OUTER_JOIN, Pair.of(0, 0)) // t1.id = t2.id
+ .filter(Sets.newHashSet(
+ new IsNull(scan1.getOutput().get(0)),
+ new And(new EqualTo(scan1.getOutput().get(0), new IntegerLiteral(1)),
+ new EqualTo(scan1.getOutput().get(0), new IntegerLiteral(1))))
+ )
+ .project(ImmutableList.of(2, 3))
+ .build();
+
+ PlanChecker.from(MemoTestUtils.createConnectContext(), plan)
+ .applyTopDown(new InferFilterNotNull())
+ .applyTopDown(new ConvertOuterJoinToAntiJoin())
+ .printlnTree()
+ .matches(logicalJoin().when(join -> join.getJoinType().isLeftOuterJoin()));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org
(doris) 03/05: [improve](routine-load) optimize error msg when failed to fetch Kafka info #30298
Posted by yi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
commit 92d4ce31ae50ca0d2453a655c4d83ed560fb4012
Author: HHoflittlefish777 <77...@users.noreply.github.com>
AuthorDate: Thu Jan 25 14:08:40 2024 +0800
[improve](routine-load) optimize error msg when failed to fetch Kafka info #30298
---
.../load/routineload/KafkaRoutineLoadJob.java | 8 ++--
.../routine_load/test_routine_load_error.groovy | 43 ++++++++++++++++++++++
2 files changed, 48 insertions(+), 3 deletions(-)
diff --git a/fe/fe-core/src/main/java/org/apache/doris/load/routineload/KafkaRoutineLoadJob.java b/fe/fe-core/src/main/java/org/apache/doris/load/routineload/KafkaRoutineLoadJob.java
index faad0a0248a..77171b6a4cb 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/load/routineload/KafkaRoutineLoadJob.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/load/routineload/KafkaRoutineLoadJob.java
@@ -325,13 +325,15 @@ public class KafkaRoutineLoadJob extends RoutineLoadJob {
try {
this.newCurrentKafkaPartition = getAllKafkaPartitions();
} catch (Exception e) {
+ String msg = e.getMessage()
+ + " may be Kafka properties set in job is error"
+ + " or no partition in this topic that should check Kafka";
LOG.warn(new LogBuilder(LogKey.ROUTINE_LOAD_JOB, id)
- .add("error_msg", "Job failed to fetch all current partition with error " + e.getMessage())
+ .add("error_msg", msg)
.build(), e);
if (this.state == JobState.NEED_SCHEDULE) {
unprotectUpdateState(JobState.PAUSED,
- new ErrorReason(InternalErrorCode.PARTITIONS_ERR,
- "Job failed to fetch all current partition with error " + e.getMessage()),
+ new ErrorReason(InternalErrorCode.PARTITIONS_ERR, msg),
false /* not replay */);
}
}
diff --git a/regression-test/suites/load_p0/routine_load/test_routine_load_error.groovy b/regression-test/suites/load_p0/routine_load/test_routine_load_error.groovy
index 191ea4381fd..5b42e84be00 100644
--- a/regression-test/suites/load_p0/routine_load/test_routine_load_error.groovy
+++ b/regression-test/suites/load_p0/routine_load/test_routine_load_error.groovy
@@ -248,4 +248,47 @@ suite("test_routine_load_error","p0") {
sql """ DROP TABLE IF EXISTS ${tableName} """
}
}
+
+ // test failed to fetch all current partition
+ if (enabled != null && enabled.equalsIgnoreCase("true")) {
+ def jobName = "invalid_topic"
+ try {
+ sql """
+ CREATE ROUTINE LOAD ${jobName}
+ COLUMNS TERMINATED BY "|"
+ PROPERTIES
+ (
+ "max_batch_interval" = "5",
+ "max_batch_rows" = "300000",
+ "max_batch_size" = "209715200"
+ )
+ FROM KAFKA
+ (
+ "kafka_broker_list" = "${externalEnvIp}:${kafka_port}",
+ "kafka_topic" = "invalid_topic",
+ "property.kafka_default_offsets" = "OFFSET_BEGINNING"
+ );
+ """
+ sql "sync"
+
+ def count = 0
+ while (true) {
+ sleep(1000)
+ def res = sql "show routine load for ${jobName}"
+ def state = res[0][8].toString()
+ if (state != "PAUSED") {
+ count++
+ if (count > 60) {
+ assertEquals(1, 2)
+ }
+ continue;
+ }
+ log.info("reason of state changed: ${res[0][17].toString()}".toString())
+ assertTrue(res[0][17].toString().contains("may be Kafka properties set in job is error or no partition in this topic that should check Kafka"))
+ break;
+ }
+ } finally {
+ sql "stop routine load for ${jobName}"
+ }
+ }
}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org
(doris) 05/05: [fix](meta) remove default_cluster prefix in recover db log (#30350)
Posted by yi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
commit e8a20d8899f3f1804b45dd4466aaf4c79464a418
Author: Mingyu Chen <mo...@163.com>
AuthorDate: Thu Jan 25 14:30:53 2024 +0800
[fix](meta) remove default_cluster prefix in recover db log (#30350)
---
fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java | 3 ++-
.../main/java/org/apache/doris/datasource/InternalCatalog.java | 6 +-----
.../src/main/java/org/apache/doris/persist/RecoverInfo.java | 9 ++++++++-
3 files changed, 11 insertions(+), 7 deletions(-)
diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java
index 96db31af430..3975fe8b388 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java
@@ -216,7 +216,8 @@ public class Database extends MetaObject implements Writable, DatabaseIf<Table>
public void setNameWithLock(String newName) {
writeLock();
try {
- this.fullQualifiedName = newName;
+ // ClusterNamespace.getNameFromFullName should be removed in 3.0
+ this.fullQualifiedName = ClusterNamespace.getNameFromFullName(newName);
for (Table table : idToTable.values()) {
table.setQualifiedDbName(fullQualifiedName);
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
index 8832aa25dc8..c977b61be2f 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
@@ -649,7 +649,7 @@ public class InternalCatalog implements CatalogIf<Database> {
String tableName = recoverStmt.getTableName();
String newTableName = recoverStmt.getNewTableName();
- Database db = (Database) getDbOrDdlException(dbName);
+ Database db = getDbOrDdlException(dbName);
db.writeLockOrDdlException();
try {
if (Strings.isNullOrEmpty(newTableName)) {
@@ -698,10 +698,6 @@ public class InternalCatalog implements CatalogIf<Database> {
}
}
- public void replayEraseDatabase(long dbId) throws DdlException {
- Env.getCurrentRecycleBin().replayEraseDatabase(dbId);
- }
-
public void replayRecoverDatabase(RecoverInfo info) {
long dbId = info.getDbId();
String newDbName = info.getNewDbName();
diff --git a/fe/fe-core/src/main/java/org/apache/doris/persist/RecoverInfo.java b/fe/fe-core/src/main/java/org/apache/doris/persist/RecoverInfo.java
index 7809b05d0e6..15764a99b43 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/persist/RecoverInfo.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/persist/RecoverInfo.java
@@ -18,9 +18,11 @@
package org.apache.doris.persist;
import org.apache.doris.catalog.Env;
+import org.apache.doris.cluster.ClusterNamespace;
import org.apache.doris.common.FeMetaVersion;
import org.apache.doris.common.io.Text;
import org.apache.doris.common.io.Writable;
+import org.apache.doris.persist.gson.GsonPostProcessable;
import org.apache.doris.persist.gson.GsonUtils;
import com.google.gson.annotations.SerializedName;
@@ -29,7 +31,7 @@ import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
-public class RecoverInfo implements Writable {
+public class RecoverInfo implements Writable, GsonPostProcessable {
@SerializedName(value = "dbId")
private long dbId;
@SerializedName(value = "newDbName")
@@ -102,4 +104,9 @@ public class RecoverInfo implements Writable {
tableId = in.readLong();
partitionId = in.readLong();
}
+
+ @Override
+ public void gsonPostProcess() throws IOException {
+ newDbName = ClusterNamespace.getNameFromFullName(newDbName);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org
(doris) 02/05: [feat](Nereids): eliminate inner join by pk fk when comparing mv (#30258)
Posted by yi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
commit 0f81ecf41528e2f2bb39dba5956302057463fff2
Author: 谢健 <ji...@gmail.com>
AuthorDate: Thu Jan 25 14:01:34 2024 +0800
[feat](Nereids): eliminate inner join by pk fk when comparing mv (#30258)
---
.../doris/nereids/jobs/executor/Rewriter.java | 2 +-
.../joinorder/hypergraph/node/StructInfoNode.java | 23 --
.../rules/exploration/mv/HyperGraphComparator.java | 53 +++-
.../nereids/rules/rewrite/EliminateJoinByFK.java | 319 +++++----------------
.../rules/rewrite/EliminateJoinByUnique.java | 1 -
.../nereids/rules/rewrite/ForeignKeyContext.java | 184 ++++++++++++
.../org/apache/doris/nereids/util/JoinUtils.java | 42 ++-
.../rules/exploration/mv/EliminateJoinTest.java | 103 +++++++
8 files changed, 443 insertions(+), 284 deletions(-)
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Rewriter.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Rewriter.java
index 4304933f752..4b488b6cfee 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Rewriter.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Rewriter.java
@@ -303,7 +303,7 @@ public class Rewriter extends AbstractBatchJobExecutor {
// this rule should invoke after infer predicate and push down distinct, and before push down limit
topic("eliminate join according unique or foreign key",
- custom(RuleType.ELIMINATE_JOIN_BY_FOREIGN_KEY, EliminateJoinByFK::new),
+ bottomUp(new EliminateJoinByFK()),
topDown(new EliminateJoinByUnique())
),
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/joinorder/hypergraph/node/StructInfoNode.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/joinorder/hypergraph/node/StructInfoNode.java
index ba31ff823a6..93e13e59da5 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/joinorder/hypergraph/node/StructInfoNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/joinorder/hypergraph/node/StructInfoNode.java
@@ -19,7 +19,6 @@ package org.apache.doris.nereids.jobs.joinorder.hypergraph.node;
import org.apache.doris.common.Pair;
import org.apache.doris.nereids.jobs.joinorder.hypergraph.edge.Edge;
-import org.apache.doris.nereids.properties.FunctionalDependencies;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.plans.GroupPlan;
import org.apache.doris.nereids.trees.plans.Plan;
@@ -29,11 +28,8 @@ import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanVisitor;
-import org.apache.doris.nereids.util.JoinUtils;
import org.apache.doris.nereids.util.Utils;
-import com.google.common.base.Supplier;
-import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableSet;
@@ -51,36 +47,17 @@ import javax.annotation.Nullable;
public class StructInfoNode extends AbstractNode {
private final List<Set<Expression>> expressions;
private final Set<CatalogRelation> relationSet;
- private final Supplier<Boolean> eliminateSupplier;
public StructInfoNode(int index, Plan plan, List<Edge> edges) {
super(extractPlan(plan), index, edges);
relationSet = plan.collect(CatalogRelation.class::isInstance);
expressions = collectExpressions(plan);
- eliminateSupplier = Suppliers.memoize(this::computeElimination);
}
public StructInfoNode(int index, Plan plan) {
this(index, plan, new ArrayList<>());
}
- private boolean computeElimination() {
- if (getJoinEdges().isEmpty()) {
- return false;
- }
- return getJoinEdges().stream().allMatch(e -> {
- if (e.getRightExtendedNodes() == getNodeMap()) {
- return JoinUtils.canEliminateByLeft(e.getJoin(), FunctionalDependencies.EMPTY_FUNC_DEPS,
- plan.getLogicalProperties().getFunctionalDependencies());
- }
- return false;
- });
- }
-
- public boolean canEliminate() {
- return eliminateSupplier.get();
- }
-
private @Nullable List<Set<Expression>> collectExpressions(Plan plan) {
Pair<Boolean, Builder<Set<Expression>>> collector = Pair.of(true, ImmutableList.builder());
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/HyperGraphComparator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/HyperGraphComparator.java
index 1e65ad74913..3339d009c79 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/HyperGraphComparator.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/HyperGraphComparator.java
@@ -29,7 +29,9 @@ import org.apache.doris.nereids.rules.rewrite.PushDownFilterThroughJoin;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.plans.JoinType;
+import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.util.ExpressionUtils;
+import org.apache.doris.nereids.util.JoinUtils;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -143,10 +145,57 @@ public class HyperGraphComparator {
}
private boolean tryEliminateNodesAndEdge() {
- for (int i : LongBitmap.getIterator(eliminateViewNodesMap)) {
- if (!((StructInfoNode) viewHyperGraph.getNode(i)).canEliminate()) {
+ boolean hasFilterEdgeAbove = viewHyperGraph.getFilterEdges().stream()
+ .filter(e -> LongBitmap.getCardinality(e.getReferenceNodes()) == 1)
+ .anyMatch(e -> LongBitmap.isSubset(e.getReferenceNodes(), eliminateViewNodesMap));
+ if (hasFilterEdgeAbove) {
+ // If there is some filter edge above the eliminated node, we should rebuild a plan
+ // Right now, just refuse it.
+ return false;
+ }
+ for (JoinEdge joinEdge : viewHyperGraph.getJoinEdges()) {
+ if (!LongBitmap.isOverlap(joinEdge.getReferenceNodes(), eliminateViewNodesMap)) {
+ continue;
+ }
+ // eliminate by unique
+ if (joinEdge.getJoinType().isLeftOuterJoin()) {
+ long eliminatedRight =
+ LongBitmap.newBitmapIntersect(joinEdge.getRightExtendedNodes(), eliminateViewNodesMap);
+ if (LongBitmap.getCardinality(eliminatedRight) != 1) {
+ return false;
+ }
+ Plan rigthPlan = viewHyperGraph
+ .getNode(LongBitmap.lowestOneIndex(joinEdge.getRightExtendedNodes())).getPlan();
+ return JoinUtils.canEliminateByLeft(joinEdge.getJoin(),
+ rigthPlan.getLogicalProperties().getFunctionalDependencies());
+ }
+ // eliminate by pk fk
+ if (joinEdge.getJoinType().isInnerJoin()) {
+ if (!joinEdge.isSimple()) {
+ return false;
+ }
+ long eliminatedLeft =
+ LongBitmap.newBitmapIntersect(joinEdge.getLeftExtendedNodes(), eliminateViewNodesMap);
+ long eliminatedRight =
+ LongBitmap.newBitmapIntersect(joinEdge.getRightExtendedNodes(), eliminateViewNodesMap);
+ if (LongBitmap.getCardinality(eliminatedLeft) == 0
+ && LongBitmap.getCardinality(eliminatedRight) == 1) {
+ Plan foreign = viewHyperGraph
+ .getNode(LongBitmap.lowestOneIndex(joinEdge.getLeftExtendedNodes())).getPlan();
+ Plan primary = viewHyperGraph
+ .getNode(LongBitmap.lowestOneIndex(joinEdge.getRightExtendedNodes())).getPlan();
+ return JoinUtils.canEliminateByFk(joinEdge.getJoin(), primary, foreign);
+ } else if (LongBitmap.getCardinality(eliminatedLeft) == 1
+ && LongBitmap.getCardinality(eliminatedRight) == 0) {
+ Plan foreign = viewHyperGraph
+ .getNode(LongBitmap.lowestOneIndex(joinEdge.getRightExtendedNodes())).getPlan();
+ Plan primary = viewHyperGraph
+ .getNode(LongBitmap.lowestOneIndex(joinEdge.getLeftExtendedNodes())).getPlan();
+ return JoinUtils.canEliminateByFk(joinEdge.getJoin(), primary, foreign);
+ }
return false;
}
+
}
return true;
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByFK.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByFK.java
index b4a6eac207b..c8eba2e4f31 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByFK.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByFK.java
@@ -17,297 +17,106 @@
package org.apache.doris.nereids.rules.rewrite;
-import org.apache.doris.catalog.Column;
-import org.apache.doris.catalog.TableIf;
-import org.apache.doris.nereids.jobs.JobContext;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.Expression;
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.Slot;
-import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.functions.ExpressionTrait;
import org.apache.doris.nereids.trees.plans.Plan;
-import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
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.LogicalRelation;
-import org.apache.doris.nereids.trees.plans.visitor.CustomRewriter;
-import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanRewriter;
import org.apache.doris.nereids.util.ImmutableEqualSet;
+import org.apache.doris.nereids.util.JoinUtils;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
-import java.util.HashMap;
-import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
-import java.util.stream.Collectors;
import javax.annotation.Nullable;
/**
* Eliminate join by foreign.
*/
-public class EliminateJoinByFK extends DefaultPlanRewriter<JobContext> implements CustomRewriter {
- @Override
- public Plan rewriteRoot(Plan plan, JobContext jobContext) {
- EliminateJoinByFKHelper helper = new EliminateJoinByFKHelper();
- return helper.rewriteRoot(plan, jobContext);
- }
-
- private static class EliminateJoinByFKHelper
- extends DefaultPlanRewriter<ForeignKeyContext> implements CustomRewriter {
-
- @Override
- public Plan rewriteRoot(Plan plan, JobContext jobContext) {
- return plan.accept(this, new ForeignKeyContext());
- }
-
- @Override
- public Plan visit(Plan plan, ForeignKeyContext context) {
- Plan newPlan = visitChildren(this, plan, context);
- // always expire primary key except filter, project and join.
- // always keep foreign key alive
- context.expirePrimaryKey(plan);
- return newPlan;
- }
-
- @Override
- public Plan visitLogicalRelation(LogicalRelation relation, ForeignKeyContext context) {
- if (!(relation instanceof LogicalCatalogRelation)) {
- return relation;
- }
- context.putAllForeignKeys(((LogicalCatalogRelation) relation).getTable());
- relation.getOutput().stream()
- .filter(SlotReference.class::isInstance)
- .map(SlotReference.class::cast)
- .forEach(context::putSlot);
- return relation;
- }
-
- private boolean canEliminate(LogicalJoin<?, ?> join, Map<Slot, Slot> primaryToForeign,
- ForeignKeyContext context) {
- if (!join.getOtherJoinConjuncts().isEmpty()) {
- return false;
- }
- if (!join.getJoinType().isInnerJoin() && !join.getJoinType().isSemiJoin()) {
- return false;
- }
- return context.satisfyConstraint(primaryToForeign, join);
- }
+public class EliminateJoinByFK extends OneRewriteRuleFactory {
- private @Nullable Map<Expression, Expression> tryMapOutputToForeignPlan(Plan foreignPlan,
- Set<Slot> output, Map<Slot, Slot> primaryToForeign) {
- Set<Slot> residualPrimary = Sets.difference(output, foreignPlan.getOutputSet());
- ImmutableMap.Builder<Expression, Expression> builder = new ImmutableMap.Builder<>();
- for (Slot slot : residualPrimary) {
- if (primaryToForeign.containsKey(slot)) {
- builder.put(slot, primaryToForeign.get(slot));
- } else {
- return null;
- }
- }
- return builder.build();
- }
-
- private Plan applyNullCompensationFilter(Plan child, Set<Slot> childSlots) {
- Set<Expression> predicates = childSlots.stream()
- .filter(ExpressionTrait::nullable)
- .map(s -> new Not(new IsNull(s)))
- .collect(ImmutableSet.toImmutableSet());
- if (predicates.isEmpty()) {
- return child;
- }
- return new LogicalFilter<>(predicates, child);
- }
-
- private Plan tryEliminatePrimaryPlan(LogicalProject<LogicalJoin<?, ?>> project,
- Plan foreignPlan, Set<Slot> foreignKeys,
- Map<Slot, Slot> primaryToForeign, ForeignKeyContext context) {
- Set<Slot> output = project.getInputSlots();
- Map<Expression, Expression> outputToForeign =
- tryMapOutputToForeignPlan(foreignPlan, output, primaryToForeign);
- if (outputToForeign != null && canEliminate(project.child(), primaryToForeign, context)) {
- List<NamedExpression> newProjects = project.getProjects().stream()
- .map(e -> outputToForeign.containsKey(e)
- ? new Alias(e.getExprId(), outputToForeign.get(e), e.toSql())
- : (NamedExpression) e.rewriteUp(s -> outputToForeign.getOrDefault(s, s)))
- .collect(ImmutableList.toImmutableList());
- return project.withProjects(newProjects)
- .withChildren(applyNullCompensationFilter(foreignPlan, foreignKeys));
- }
- return project;
- }
-
- private @Nullable Map<Slot, Slot> mapPrimaryToForeign(ImmutableEqualSet<Slot> equivalenceSet,
- Set<Slot> foreignKeys) {
- ImmutableMap.Builder<Slot, Slot> builder = new ImmutableMap.Builder<>();
- for (Slot foreignSlot : foreignKeys) {
- Set<Slot> primarySlots = equivalenceSet.calEqualSet(foreignSlot);
- if (primarySlots.size() != 1) {
- return null;
- }
- builder.put(primarySlots.iterator().next(), foreignSlot);
- }
- return builder.build();
- }
-
- // Right now we only support eliminate inner join, which should meet the following condition:
- // 1. only contain null-reject equal condition, and which all meet fk-pk constraint
- // 2. only output foreign table output or can be converted to foreign table output
- // 4. if foreign key is null, add a isNotNull predicate for null-reject join condition
- private Plan eliminateJoin(LogicalProject<LogicalJoin<?, ?>> project, ForeignKeyContext context) {
- LogicalJoin<?, ?> join = project.child();
+ // Right now we only support eliminate inner join, which should meet the following condition:
+ // 1. only contain null-reject equal condition, and which all meet fk-pk constraint
+ // 2. only output foreign table output or can be converted to foreign table output
+ // 3. if foreign key is null, add a isNotNull predicate for null-reject join condition
+ @Override
+ public Rule build() {
+ return logicalProject(
+ logicalJoin().when(join -> join.getJoinType().isInnerJoin())
+ ).then(project -> {
+ LogicalJoin<Plan, Plan> join = project.child();
ImmutableEqualSet<Slot> equalSet = join.getEqualSlots();
- Set<Slot> leftSlots = Sets.intersection(join.left().getOutputSet(), equalSet.getAllItemSet());
- Set<Slot> rightSlots = Sets.intersection(join.right().getOutputSet(), equalSet.getAllItemSet());
- if (context.isForeignKey(leftSlots) && context.isPrimaryKey(rightSlots)) {
- Map<Slot, Slot> primaryToForeignSlot = mapPrimaryToForeign(equalSet, leftSlots);
- if (primaryToForeignSlot != null) {
- return tryEliminatePrimaryPlan(project, join.left(), leftSlots, primaryToForeignSlot, context);
- }
- } else if (context.isForeignKey(rightSlots) && context.isPrimaryKey(leftSlots)) {
- Map<Slot, Slot> primaryToForeignSlot = mapPrimaryToForeign(equalSet, rightSlots);
- if (primaryToForeignSlot != null) {
- return tryEliminatePrimaryPlan(project, join.right(), rightSlots, primaryToForeignSlot, context);
- }
- }
- return project;
- }
-
- @Override
- public Plan visitLogicalProject(LogicalProject<?> project, ForeignKeyContext context) {
- project = visitChildren(this, project, context);
- for (NamedExpression expression : project.getProjects()) {
- if (expression instanceof Alias && expression.child(0) instanceof Slot) {
- context.putAlias(expression.toSlot(), (Slot) expression.child(0));
- }
+ Set<Slot> residualSlot = Sets.difference(project.getInputSlots(), equalSet.getAllItemSet());
+ Plan res = null;
+ if (join.left().getOutputSet().containsAll(residualSlot)) {
+ res = tryEliminatePrimary(project, equalSet, join.right(), join.left());
}
- if (project.child() instanceof LogicalJoin<?, ?>) {
- return eliminateJoin((LogicalProject<LogicalJoin<?, ?>>) project, context);
+ if (res == null && join.right().getOutputSet().containsAll(residualSlot)) {
+ res = tryEliminatePrimary(project, equalSet, join.left(), join.right());
}
- return project;
- }
-
- @Override
- public Plan visitLogicalJoin(LogicalJoin<?, ?> join, ForeignKeyContext context) {
- Plan plan = visitChildren(this, join, context);
- context.addJoin(join);
- return plan;
- }
-
- @Override
- public Plan visitLogicalFilter(LogicalFilter<?> filter, ForeignKeyContext context) {
- Plan plan = visitChildren(this, filter, context);
- context.addFilter(filter);
- return plan;
- }
+ return res;
+ }).toRule(RuleType.ELIMINATE_JOIN_BY_UK);
}
- private static class ForeignKeyContext {
- Set<Map<Column, Column>> constraints = new HashSet<>();
- Set<Column> foreignKeys = new HashSet<>();
- Set<Column> primaryKeys = new HashSet<>();
- Map<Slot, Column> slotToColumn = new HashMap<>();
- Map<Slot, Set<LogicalJoin<?, ?>>> slotWithJoin = new HashMap<>();
- Map<Slot, Set<Expression>> slotWithPredicates = new HashMap<>();
-
- public void putAllForeignKeys(TableIf table) {
- table.getForeignKeyConstraints().forEach(c -> {
- Map<Column, Column> constraint = c.getForeignToPrimary(table);
- constraints.add(c.getForeignToPrimary(table));
- foreignKeys.addAll(constraint.keySet());
- primaryKeys.addAll(constraint.values());
- });
- }
-
- public boolean isForeignKey(Set<Slot> key) {
- return foreignKeys.containsAll(
- key.stream().map(s -> slotToColumn.get(s)).collect(Collectors.toSet()));
- }
-
- public boolean isPrimaryKey(Set<Slot> key) {
- return primaryKeys.containsAll(
- key.stream().map(s -> slotToColumn.get(s)).collect(Collectors.toSet()));
- }
-
- public void putSlot(SlotReference slot) {
- if (!slot.getColumn().isPresent()) {
- return;
- }
- Column c = slot.getColumn().get();
- slotToColumn.put(slot, c);
- }
-
- public void putAlias(Slot newSlot, Slot originSlot) {
- if (slotToColumn.containsKey(originSlot)) {
- slotToColumn.put(newSlot, slotToColumn.get(originSlot));
- }
- }
-
- public void addFilter(LogicalFilter<?> filter) {
- filter.getOutput().stream()
- .filter(slotToColumn::containsKey)
- .forEach(slot -> {
- slotWithPredicates.computeIfAbsent(slot, v -> new HashSet<>());
- slotWithPredicates.get(slot).addAll(filter.getConjuncts());
- });
- }
-
- public void addJoin(LogicalJoin<?, ?> join) {
- join.getOutput().stream()
- .filter(slotToColumn::containsKey)
- .forEach(slot ->
- slotWithJoin.computeIfAbsent(slot, v -> Sets.newHashSet((join))));
- }
-
- public void expirePrimaryKey(Plan plan) {
- plan.getOutput().stream()
- .filter(slotToColumn::containsKey)
- .map(s -> slotToColumn.get(s))
- .forEach(primaryKeys::remove);
- }
+ private @Nullable Plan tryEliminatePrimary(LogicalProject<LogicalJoin<Plan, Plan>> project,
+ ImmutableEqualSet<Slot> equalSet, Plan primary, Plan foreign) {
+ if (!JoinUtils.canEliminateByFk(project.child(), primary, foreign)) {
+ return null;
+ }
+ Set<Slot> output = project.getInputSlots();
+ Set<Slot> foreignKeys = Sets.intersection(foreign.getOutputSet(), equalSet.getAllItemSet());
+ Map<Expression, Expression> outputToForeign =
+ tryMapOutputToForeignPlan(foreign, output, equalSet);
+ if (outputToForeign != null) {
+ List<NamedExpression> newProjects = project.getProjects().stream()
+ .map(e -> outputToForeign.containsKey(e)
+ ? new Alias(e.getExprId(), outputToForeign.get(e), e.toSql())
+ : (NamedExpression) e.rewriteUp(s -> outputToForeign.getOrDefault(s, s)))
+ .collect(ImmutableList.toImmutableList());
+ return project.withProjects(newProjects)
+ .withChildren(applyNullCompensationFilter(foreign, foreignKeys));
+ }
+ return project;
+ }
- public boolean satisfyConstraint(Map<Slot, Slot> primaryToForeign, LogicalJoin<?, ?> join) {
- Map<Column, Column> foreignToPrimary = primaryToForeign.entrySet().stream()
- .collect(ImmutableMap.toImmutableMap(
- e -> slotToColumn.get(e.getValue()),
- e -> slotToColumn.get(e.getKey())));
- // The primary key can only contain join that may be eliminated
- if (!primaryToForeign.keySet().stream().allMatch(p ->
- slotWithJoin.get(p).size() == 1 && slotWithJoin.get(p).iterator().next() == join)) {
- return false;
- }
- // The foreign key's filters must contain primary filters
- if (!isPredicateCompatible(primaryToForeign)) {
- return false;
+ private @Nullable Map<Expression, Expression> tryMapOutputToForeignPlan(Plan foreignPlan,
+ Set<Slot> output, ImmutableEqualSet<Slot> equalSet) {
+ Set<Slot> residualPrimary = Sets.difference(output, foreignPlan.getOutputSet());
+ ImmutableMap.Builder<Expression, Expression> builder = new ImmutableMap.Builder<>();
+ for (Slot primarySlot : residualPrimary) {
+ Optional<Slot> replacedForeign = equalSet.calEqualSet(primarySlot).stream()
+ .filter(foreignPlan.getOutputSet()::contains)
+ .findFirst();
+ if (!replacedForeign.isPresent()) {
+ return null;
}
- return constraints.contains(foreignToPrimary);
+ builder.put(primarySlot, replacedForeign.get());
}
+ return builder.build();
+ }
- // When predicates of foreign keys is a subset of that of primary keys
- private boolean isPredicateCompatible(Map<Slot, Slot> primaryToForeign) {
- return primaryToForeign.entrySet().stream().allMatch(pf -> {
- // There is no predicate in primary key
- if (!slotWithPredicates.containsKey(pf.getKey()) || slotWithPredicates.get(pf.getKey()).isEmpty()) {
- return true;
- }
- // There are some predicates in primary key but there is no predicate in foreign key
- if (slotWithPredicates.containsKey(pf.getValue()) && slotWithPredicates.get(pf.getValue()).isEmpty()) {
- return false;
- }
- Set<Expression> primaryPredicates = slotWithPredicates.get(pf.getKey()).stream()
- .map(e -> e.rewriteUp(
- s -> s instanceof Slot ? primaryToForeign.getOrDefault(s, (Slot) s) : s))
- .collect(Collectors.toSet());
- return slotWithPredicates.get(pf.getValue()).containsAll(primaryPredicates);
- });
+ private Plan applyNullCompensationFilter(Plan child, Set<Slot> childSlots) {
+ Set<Expression> predicates = childSlots.stream()
+ .filter(ExpressionTrait::nullable)
+ .map(s -> new Not(new IsNull(s)))
+ .collect(ImmutableSet.toImmutableSet());
+ if (predicates.isEmpty()) {
+ return child;
}
+ return new LogicalFilter<>(predicates, child);
}
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByUnique.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByUnique.java
index 009b5efc552..25f157c7bdb 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByUnique.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByUnique.java
@@ -37,7 +37,6 @@ public class EliminateJoinByUnique extends OneRewriteRuleFactory {
return project;
}
if (!JoinUtils.canEliminateByLeft(join,
- join.left().getLogicalProperties().getFunctionalDependencies(),
join.right().getLogicalProperties().getFunctionalDependencies())) {
return project;
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ForeignKeyContext.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ForeignKeyContext.java
new file mode 100644
index 00000000000..9cbac5cf09c
--- /dev/null
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ForeignKeyContext.java
@@ -0,0 +1,184 @@
+// 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.catalog.Column;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.nereids.trees.expressions.Alias;
+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.expressions.SlotReference;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.logical.LogicalRelation;
+import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanVisitor;
+
+import com.google.common.collect.ImmutableMap;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Record Foreign Key Context
+ */
+public class ForeignKeyContext {
+ Set<Map<Column, Column>> constraints = new HashSet<>();
+ Set<Column> foreignKeys = new HashSet<>();
+ Set<Column> primaryKeys = new HashSet<>();
+ Map<Slot, Column> slotToColumn = new HashMap<>();
+ Map<Slot, Set<Expression>> slotWithPredicates = new HashMap<>();
+
+ /**
+ * Collect Foreign Key Constraint From this Plan
+ */
+ public ForeignKeyContext collectForeignKeyConstraint(Plan plan) {
+ plan.accept(new DefaultPlanVisitor<Void, ForeignKeyContext>() {
+ @Override
+ public Void visit(Plan plan, ForeignKeyContext context) {
+ super.visit(plan, context);
+ // always expire primary key except filter, project and join.
+ // always keep foreign key alive
+ context.expirePrimaryKey(plan);
+ return null;
+ }
+
+ @Override
+ public Void visitLogicalRelation(LogicalRelation relation, ForeignKeyContext context) {
+ if (relation instanceof LogicalCatalogRelation) {
+ context.putAllForeignKeys(((LogicalCatalogRelation) relation).getTable());
+ relation.getOutput().stream()
+ .filter(SlotReference.class::isInstance)
+ .map(SlotReference.class::cast)
+ .forEach(context::putSlot);
+ }
+ return null;
+ }
+
+ @Override
+ public Void visitLogicalProject(LogicalProject<?> project, ForeignKeyContext context) {
+ super.visit(project, context);
+ for (NamedExpression expression : project.getProjects()) {
+ if (expression instanceof Alias && expression.child(0) instanceof Slot) {
+ context.putAlias(expression.toSlot(), (Slot) expression.child(0));
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Void visitLogicalFilter(LogicalFilter<?> filter, ForeignKeyContext context) {
+ super.visit(filter, context);
+ context.addFilter(filter);
+ return null;
+ }
+ }, this);
+ return this;
+ }
+
+ void putAllForeignKeys(TableIf table) {
+ table.getForeignKeyConstraints().forEach(c -> {
+ Map<Column, Column> constraint = c.getForeignToPrimary(table);
+ constraints.add(c.getForeignToPrimary(table));
+ foreignKeys.addAll(constraint.keySet());
+ primaryKeys.addAll(constraint.values());
+ });
+ }
+
+ public boolean isForeignKey(Set<Slot> key) {
+ return foreignKeys.containsAll(
+ key.stream().map(s -> slotToColumn.get(s)).collect(Collectors.toSet()));
+ }
+
+ public boolean isPrimaryKey(Set<Slot> key) {
+ return primaryKeys.containsAll(
+ key.stream().map(s -> slotToColumn.get(s)).collect(Collectors.toSet()));
+ }
+
+ void putSlot(SlotReference slot) {
+ if (!slot.getColumn().isPresent()) {
+ return;
+ }
+ Column c = slot.getColumn().get();
+ slotToColumn.put(slot, c);
+ }
+
+ void putAlias(Slot newSlot, Slot originSlot) {
+ if (slotToColumn.containsKey(originSlot)) {
+ slotToColumn.put(newSlot, slotToColumn.get(originSlot));
+ }
+ }
+
+ private void addFilter(LogicalFilter<?> filter) {
+ filter.getOutput().stream()
+ .filter(slotToColumn::containsKey)
+ .forEach(slot -> {
+ slotWithPredicates.computeIfAbsent(slot, v -> new HashSet<>());
+ slotWithPredicates.get(slot).addAll(filter.getConjuncts());
+ });
+ }
+
+ private void expirePrimaryKey(Plan plan) {
+ plan.getOutput().stream()
+ .filter(slotToColumn::containsKey)
+ .map(s -> slotToColumn.get(s))
+ .forEach(primaryKeys::remove);
+ }
+
+ /**
+ * Check whether the given mapping relation satisfies any constraints
+ */
+ public boolean satisfyConstraint(Map<Slot, Slot> primaryToForeign) {
+ Map<Column, Column> foreignToPrimary = primaryToForeign.entrySet().stream()
+ .collect(ImmutableMap.toImmutableMap(
+ e -> slotToColumn.get(e.getValue()),
+ e -> slotToColumn.get(e.getKey())));
+ if (primaryToForeign.isEmpty()) {
+ return false;
+ }
+ // The foreign key's filters must contain primary filters
+ if (!isPredicateCompatible(primaryToForeign)) {
+ return false;
+ }
+ return constraints.contains(foreignToPrimary);
+ }
+
+ // When predicates of foreign keys is a subset of that of primary keys
+ private boolean isPredicateCompatible(Map<Slot, Slot> primaryToForeign) {
+ return primaryToForeign.entrySet().stream().allMatch(pf -> {
+ // There is no predicate in primary key
+ if (!slotWithPredicates.containsKey(pf.getKey()) || slotWithPredicates.get(pf.getKey()).isEmpty()) {
+ return true;
+ }
+ // There are some predicates in primary key but there is no predicate in foreign key
+ if (slotWithPredicates.containsKey(pf.getValue()) && slotWithPredicates.get(pf.getValue()).isEmpty()) {
+ return false;
+ }
+ Set<Expression> primaryPredicates = slotWithPredicates.get(pf.getKey()).stream()
+ .map(e -> e.rewriteUp(
+ s -> s instanceof Slot ? primaryToForeign.getOrDefault(s, (Slot) s) : s))
+ .collect(Collectors.toSet());
+ return slotWithPredicates.get(pf.getValue()).containsAll(primaryPredicates);
+ });
+ }
+}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/JoinUtils.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/JoinUtils.java
index 8ed077ba759..9beaa29c433 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/JoinUtils.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/JoinUtils.java
@@ -25,6 +25,7 @@ import org.apache.doris.nereids.properties.DistributionSpecHash;
import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
import org.apache.doris.nereids.properties.DistributionSpecReplicated;
import org.apache.doris.nereids.properties.FunctionalDependencies;
+import org.apache.doris.nereids.rules.rewrite.ForeignKeyContext;
import org.apache.doris.nereids.trees.expressions.EqualPredicate;
import org.apache.doris.nereids.trees.expressions.ExprId;
import org.apache.doris.nereids.trees.expressions.Expression;
@@ -43,7 +44,9 @@ import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.SessionVariable;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
import java.util.HashSet;
import java.util.List;
@@ -279,11 +282,46 @@ public class JoinUtils {
.collect(ImmutableList.toImmutableList());
}
+ private static Map<Slot, Slot> mapPrimaryToForeign(ImmutableEqualSet<Slot> equivalenceSet,
+ Set<Slot> foreignKeys) {
+ ImmutableMap.Builder<Slot, Slot> builder = new ImmutableMap.Builder<>();
+ for (Slot foreignSlot : foreignKeys) {
+ Set<Slot> primarySlots = equivalenceSet.calEqualSet(foreignSlot);
+ if (primarySlots.size() != 1) {
+ return ImmutableMap.of();
+ }
+ builder.put(primarySlots.iterator().next(), foreignSlot);
+ }
+ return builder.build();
+ }
+
+ /**
+ * Check whether the given join can be eliminated by pk-fk
+ */
+ public static boolean canEliminateByFk(LogicalJoin<?, ?> join, Plan primaryPlan, Plan foreignPlan) {
+ if (!join.getJoinType().isInnerJoin() || !join.getOtherJoinConjuncts().isEmpty() || join.isMarkJoin()) {
+ return false;
+ }
+
+ ForeignKeyContext context = new ForeignKeyContext();
+ context.collectForeignKeyConstraint(primaryPlan);
+ context.collectForeignKeyConstraint(foreignPlan);
+
+ ImmutableEqualSet<Slot> equalSet = join.getEqualSlots();
+ Set<Slot> primaryKey = Sets.intersection(equalSet.getAllItemSet(), primaryPlan.getOutputSet());
+ Set<Slot> foreignKey = Sets.intersection(equalSet.getAllItemSet(), foreignPlan.getOutputSet());
+ if (!context.isForeignKey(foreignKey) || !context.isPrimaryKey(primaryKey)) {
+ return false;
+ }
+
+ Map<Slot, Slot> primaryToForeignKey = mapPrimaryToForeign(equalSet, foreignKey);
+ return context.satisfyConstraint(primaryToForeignKey);
+ }
+
/**
* can this join be eliminated by its left child
*/
- public static boolean canEliminateByLeft(LogicalJoin<?, ?> join, FunctionalDependencies leftFuncDeps,
- FunctionalDependencies rightFuncDeps) {
+ public static boolean canEliminateByLeft(LogicalJoin<?, ?> join, FunctionalDependencies rightFuncDeps) {
if (join.getJoinType().isLeftOuterJoin()) {
Pair<Set<Slot>, Set<Slot>> njHashKeys = join.extractNullRejectHashKeys();
if (!join.getOtherJoinConjuncts().isEmpty() || njHashKeys == null) {
diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/mv/EliminateJoinTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/mv/EliminateJoinTest.java
index f76d6334b28..3e245741178 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/mv/EliminateJoinTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/mv/EliminateJoinTest.java
@@ -27,6 +27,7 @@ import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.util.PlanChecker;
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
class EliminateJoinTest extends SqlTestBase {
@@ -55,6 +56,7 @@ class EliminateJoinTest extends SqlTestBase {
HyperGraph h2 = HyperGraph.builderForMv(p2).buildAll().get(0);
ComparisonResult res = HyperGraphComparator.isLogicCompatible(h1, h2, constructContext(p1, p2));
Assertions.assertTrue(!res.isInvalid());
+ Assertions.assertTrue(res.getViewExpressions().isEmpty());
}
@Test
@@ -83,6 +85,107 @@ class EliminateJoinTest extends SqlTestBase {
HyperGraph h2 = HyperGraph.builderForMv(p2).buildAll().get(0);
ComparisonResult res = HyperGraphComparator.isLogicCompatible(h1, h2, constructContext(p1, p2));
Assertions.assertTrue(!res.isInvalid());
+ Assertions.assertTrue(res.getViewExpressions().isEmpty());
+ dropConstraint("alter table T2 drop constraint uk");
+ }
+
+ @Test
+ void testLOJWithPKFK() throws Exception {
+ connectContext.getSessionVariable().setDisableNereidsRules("INFER_PREDICATES");
+ CascadesContext c1 = createCascadesContext(
+ "select * from T1",
+ connectContext
+ );
+ Plan p1 = PlanChecker.from(c1)
+ .analyze()
+ .rewrite()
+ .getPlan().child(0);
+ addConstraint("alter table T2 add constraint pk primary key (id)");
+ addConstraint("alter table T1 add constraint fk foreign key (id) references T2(id)");
+ CascadesContext c2 = createCascadesContext(
+ "select * from T1 inner join T2 "
+ + "on T1.id = T2.id ",
+ connectContext
+ );
+ Plan p2 = PlanChecker.from(c2)
+ .analyze()
+ .rewrite()
+ .applyExploration(RuleSet.BUSHY_TREE_JOIN_REORDER)
+ .getAllPlan().get(0).child(0);
+ HyperGraph h1 = HyperGraph.builderForMv(p1).buildAll().get(0);
+ HyperGraph h2 = HyperGraph.builderForMv(p2).buildAll().get(0);
+ ComparisonResult res = HyperGraphComparator.isLogicCompatible(h1, h2, constructContext(p1, p2));
+ Assertions.assertTrue(!res.isInvalid());
+ Assertions.assertTrue(res.getViewExpressions().isEmpty());
+ dropConstraint("alter table T2 drop constraint pk");
+ }
+
+ @Disabled
+ @Test
+ void testLOJWithPKFKAndUK1() throws Exception {
+ connectContext.getSessionVariable().setDisableNereidsRules("INFER_PREDICATES");
+ CascadesContext c1 = createCascadesContext(
+ "select * from T1",
+ connectContext
+ );
+ Plan p1 = PlanChecker.from(c1)
+ .analyze()
+ .rewrite()
+ .getPlan().child(0);
+ addConstraint("alter table T2 add constraint pk primary key (id)");
+ addConstraint("alter table T1 add constraint fk foreign key (id) references T2(id)");
+ addConstraint("alter table T3 add constraint uk unique (id)");
+ CascadesContext c2 = createCascadesContext(
+ "select * from (select T1.*, T3.id as id3 from T1 left outer join T3 on T1.id = T3.id) T1 inner join T2 "
+ + "on T1.id = T2.id ",
+ connectContext
+ );
+ Plan p2 = PlanChecker.from(c2)
+ .analyze()
+ .rewrite()
+ .applyExploration(RuleSet.BUSHY_TREE_JOIN_REORDER)
+ .getAllPlan().get(0).child(0);
+ HyperGraph h1 = HyperGraph.builderForMv(p1).buildAll().get(0);
+ HyperGraph h2 = HyperGraph.builderForMv(p2).buildAll().get(0);
+ ComparisonResult res = HyperGraphComparator.isLogicCompatible(h1, h2, constructContext(p1, p2));
+ Assertions.assertTrue(!res.isInvalid());
+ Assertions.assertTrue(res.getViewExpressions().isEmpty());
+ dropConstraint("alter table T2 drop constraint pk");
+ dropConstraint("alter table T3 drop constraint uk");
+ }
+
+ @Disabled
+ @Test
+ void testLOJWithPKFKAndUK2() throws Exception {
+ connectContext.getSessionVariable().setDisableNereidsRules("INFER_PREDICATES");
+ CascadesContext c1 = createCascadesContext(
+ "select * from T1",
+ connectContext
+ );
+ Plan p1 = PlanChecker.from(c1)
+ .analyze()
+ .rewrite()
+ .getPlan().child(0);
+ addConstraint("alter table T2 add constraint pk primary key (id)");
+ addConstraint("alter table T1 add constraint fk foreign key (id) references T2(id)");
+ addConstraint("alter table T3 add constraint uk unique (id)");
+ CascadesContext c2 = createCascadesContext(
+ "select * from (select T1.*, T2.id as id2 from T1 inner join T2 on T1.id = T2.id) T1 left outer join T3 "
+ + "on T1.id = T3.id ",
+ connectContext
+ );
+ Plan p2 = PlanChecker.from(c2)
+ .analyze()
+ .rewrite()
+ .applyExploration(RuleSet.BUSHY_TREE_JOIN_REORDER)
+ .getAllPlan().get(0).child(0);
+ HyperGraph h1 = HyperGraph.builderForMv(p1).buildAll().get(0);
+ HyperGraph h2 = HyperGraph.builderForMv(p2).buildAll().get(0);
+ ComparisonResult res = HyperGraphComparator.isLogicCompatible(h1, h2, constructContext(p1, p2));
+ Assertions.assertTrue(!res.isInvalid());
+ Assertions.assertTrue(res.getViewExpressions().isEmpty());
+ dropConstraint("alter table T2 drop constraint pk");
+ dropConstraint("alter table T3 drop constraint uk");
}
LogicalCompatibilityContext constructContext(Plan p1, Plan p2) {
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org