xzj7019 commented on code in PR #28816:
URL: https://github.com/apache/doris/pull/28816#discussion_r1437613158
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByFK.java:
##########
@@ -143,47 +127,56 @@ private Plan applyNullCompensationFilter(Plan child,
Set<Slot> childSlots) {
return new LogicalFilter<>(predicates, child);
}
- private @Nullable Plan
tryConstructPlanWithJoinChild(LogicalProject<LogicalJoin<?, ?>> project, Plan
child,
- BiMap<Slot, Slot> equalSlots, ForeignKeyContext context) {
+ private Plan tryEliminatePrimaryPlan(LogicalProject<LogicalJoin<?, ?>>
project,
+ Plan foreignPlan, Set<Slot> foreignKeys,
+ Map<Slot, Slot> primaryToForeign, ForeignKeyContext context) {
Set<Slot> output = project.getInputSlots();
- Set<Slot> keySet =
child.getOutputSet().containsAll(equalSlots.keySet())
- ? equalSlots.keySet()
- : equalSlots.values();
- Map<Expression, Expression> outputToRight =
tryGetOutputToChildMap(child, output, equalSlots);
- if (outputToRight != null && isForeignKeyAndUnique(child, keySet,
context)) {
+ Map<Expression, Expression> outputToForeign =
+ tryMapOutputToForeignPlan(foreignPlan, output,
primaryToForeign);
+ if (outputToForeign != null && canEliminate(project.child(),
primaryToForeign, context)) {
List<NamedExpression> newProjects =
project.getProjects().stream()
- .map(e -> outputToRight.containsKey(e)
- ? new Alias(e.getExprId(),
outputToRight.get(e), e.toSql())
- : (NamedExpression) e.rewriteUp(s ->
outputToRight.getOrDefault(s, s)))
+ .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(child,
keySet));
+ .withChildren(applyNullCompensationFilter(foreignPlan,
foreignKeys));
}
- return null;
+ return project;
+ }
+
+ private @Nullable Map<Slot, Slot>
mapPrimaryToForeign(ImmutableEquivalenceSet<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
- // 3. foreign key is unique
// 4. if foreign key is null, add a isNotNull predicate for
null-reject join condition
Review Comment:
should be 3.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]