gustavodemorais commented on code in PR #26702:
URL: https://github.com/apache/flink/pull/26702#discussion_r2157018521


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/JoinToMultiJoinRule.java:
##########
@@ -546,12 +557,140 @@ private boolean canCombine(
             } else {
                 return false;
             }
-            return true;
+            return haveCommonJoinKey(origJoin, join);
         } else {
             return false;
         }
     }
 
+    /**
+     * Checks if original Join and child multi-join have common join keys.
+     *
+     * @param origJoin original Join
+     * @param otherJoin child MultiJoin
+     * @return true if original Join and child multi-join have at least one 
common JoinKey
+     */
+    private boolean haveCommonJoinKey(Join origJoin, MultiJoin otherJoin) {
+        Set<String> origJoinKeys = getJoinKeys(origJoin);
+        Set<String> otherJoinKeys = getJoinKeys(otherJoin);
+
+        origJoinKeys.retainAll(otherJoinKeys);
+
+        return !origJoinKeys.isEmpty();
+    }
+
+    /**
+     * Gives set of join keys as strings like [table_name.field_name].
+     *
+     * @param join Join or MultiJoin node
+     * @return set of all the join keys (keys from join conditions)
+     */
+    public Set<String> getJoinKeys(RelNode join) {
+        Set<String> joinKeys = new HashSet<>();
+        List<RexCall> conditions = Collections.emptyList();
+        List<RelNode> inputs = join.getInputs();
+
+        if (join instanceof Join) {
+            conditions = collectConjunctions(((Join) join).getCondition());
+        } else if (join instanceof MultiJoin) {
+            conditions =
+                    ((MultiJoin) join)
+                            .getOuterJoinConditions().stream()
+                                    .flatMap(cond -> 
collectConjunctions(cond).stream())
+                                    .collect(Collectors.toList());
+        }
+
+        RelMetadataQuery mq = join.getCluster().getMetadataQuery();
+
+        for (RexCall condition : conditions) {
+            for (RexNode operand : condition.getOperands()) {
+                if (operand instanceof RexInputRef) {
+                    addJoinKeysByOperand((RexInputRef) operand, inputs, mq, 
joinKeys);
+                }
+            }
+        }
+
+        return joinKeys;
+    }
+
+    /**
+     * Retrieves conjunctions from joinCondition.
+     *
+     * @param joinCondition join condition
+     * @return List of RexCalls representing conditions
+     */
+    private List<RexCall> collectConjunctions(RexNode joinCondition) {

Review Comment:
   ~We need to filter out non equiJoin conditions here or in 
addJoinKeysByOperand~ Actually this already happens when you do `(operand 
instanceof RexInputRef)`



-- 
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]

Reply via email to