You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2022/12/12 02:59:01 UTC

[GitHub] [flink] swuferhong opened a new pull request, #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

swuferhong opened a new pull request, #21487:
URL: https://github.com/apache/flink/pull/21487

   <!--
   *Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
     Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ## What is the purpose of the change
   
   This pr is a bug fix pr to fix `FlinkJoinToMultiJoinRule` incorrectly combines Left/Right outer join to `MultiJoin` error.  while `FlinkJoinToMultiJoinRule` consider whether a left/right outer join node can be combined into one `MultiJoin` node, it not only need to consider the join type, but also need to consider whether the join keys are null generate field.
   
   
   ## Brief change log
   
   - Change rule `FlinkJoinToMultiJoinRule` to consider whether the join keys are null generate field.
   - Add UT test in `FlinkJoinToMultiJoinRuleTest`.
   
   
   ## Verifying this change
   
   - Add UT test in `FlinkJoinToMultiJoinRuleTest`.
   
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): no
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: no
     - The serializers: no
     - The runtime per-record code paths (performance sensitive):  no
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
     - The S3 file system connector: no
   
   ## Documentation
   
     - Does this pull request introduce a new feature? no
     - If yes, how is the feature documented? no docs.
   


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] flinkbot commented on pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
flinkbot commented on PR #21487:
URL: https://github.com/apache/flink/pull/21487#issuecomment-1345795937

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "296461b819412af4af13a8ddce0eb01bf6880254",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "296461b819412af4af13a8ddce0eb01bf6880254",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 296461b819412af4af13a8ddce0eb01bf6880254 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] swuferhong commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
swuferhong commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057053036


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -178,6 +193,60 @@ public void onMatch(RelOptRuleCall call) {
         call.transformTo(multiJoin);
     }
 
+    private void buildInputNullGenFieldList(
+            RelNode left, RelNode right, JoinRelType joinType, List<Boolean> isNullGenFieldList) {
+        if (joinType == JoinRelType.INNER) {
+            buildNullGenFieldList(left, isNullGenFieldList);
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.LEFT) {
+            // left judge.
+            buildNullGenFieldList(left, isNullGenFieldList);
+
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        } else if (joinType == JoinRelType.RIGHT) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+
+            // right judge.
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.FULL) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        }

Review Comment:
   Done!



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] godfreyhe commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
godfreyhe commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1055192928


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -178,6 +193,60 @@ public void onMatch(RelOptRuleCall call) {
         call.transformTo(multiJoin);
     }
 
+    private void buildInputNullGenFieldList(
+            RelNode left, RelNode right, JoinRelType joinType, List<Boolean> isNullGenFieldList) {
+        if (joinType == JoinRelType.INNER) {
+            buildNullGenFieldList(left, isNullGenFieldList);
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.LEFT) {
+            // left judge.
+            buildNullGenFieldList(left, isNullGenFieldList);
+
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        } else if (joinType == JoinRelType.RIGHT) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+
+            // right judge.
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.FULL) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        }
+    }
+
+    private void buildNullGenFieldList(RelNode rel, List<Boolean> isNullGenFieldList) {
+        MultiJoin multiJoin = rel instanceof MultiJoin ? (MultiJoin) rel : null;
+        if (multiJoin == null) {
+            // other operator.
+            for (int i = 0; i < rel.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(false);
+            }
+        } else {
+            List<RelNode> inputs = multiJoin.getInputs();
+            List<JoinRelType> joinTypes = multiJoin.getJoinTypes();
+            for (int i = 0; i < inputs.size() - 1; i++) {
+                if (joinTypes.get(i) == JoinRelType.RIGHT) {

Review Comment:
   can you  explain why we should just handle RIGHT here?



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -378,25 +513,53 @@ private List<RexNode> combineJoinFilters(Join join, RelNode left, RelNode right)
      * @param nullGenerating true if the input is null generating
      * @return true if the input can be combined into a parent MultiJoin
      */
-    private boolean canCombine(RelNode input, JoinRelType joinType, boolean nullGenerating) {
+    private boolean canCombine(
+            RelNode input,
+            ImmutableIntList joinKeys,
+            JoinRelType joinType,
+            boolean nullGenerating,
+            boolean isLeft,
+            List<Boolean> inputNullGenFieldList,
+            int beginIndex) {
+        if (inputNullGenFieldList == null) {
+            // semi and anti join

Review Comment:
   inputNullGenFieldList is never null



##########
flink-table/flink-table-planner/src/test/resources/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRuleTest.xml:
##########
@@ -548,6 +549,46 @@ LogicalProject(a=[$0], b=[$1], c=[$2], d=[$3])
    :  +- LogicalTableScan(table=[[default_catalog, default_database, T2, source: [TestTableSource(c, d)]]])
    +- LogicalProject(e=[$0])
       +- LogicalTableScan(table=[[default_catalog, default_database, T3, source: [TestTableSource(e, f)]]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testMultiLeftOuterJoinWithAllKeyInLeft">
+    <Resource name="sql">
+      <![CDATA[
+SELECT * FROM T1 LEFT OUTER JOIN T2 ON a = c LEFT OUTER JOIN 
+(SELECT * FROM T3) ON a = e LEFT OUTER JOIN
+(SELECT * FROM T4) ON a = g LEFT OUTER JOIN
+(SELECT * FROM T5) ON a = i

Review Comment:
   the query can be simplied as:
   
   SELECT * FROM T1 
   LEFT OUTER JOIN T2 ON a = c 
   LEFT OUTER JOIN T3 ON a = e
   LEFT OUTER JOIN T4 ON a = g 
   LEFT OUTER JOIN T5 ON a = i



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -178,6 +193,60 @@ public void onMatch(RelOptRuleCall call) {
         call.transformTo(multiJoin);
     }
 
+    private void buildInputNullGenFieldList(
+            RelNode left, RelNode right, JoinRelType joinType, List<Boolean> isNullGenFieldList) {
+        if (joinType == JoinRelType.INNER) {
+            buildNullGenFieldList(left, isNullGenFieldList);
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.LEFT) {
+            // left judge.
+            buildNullGenFieldList(left, isNullGenFieldList);
+
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        } else if (joinType == JoinRelType.RIGHT) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+
+            // right judge.
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.FULL) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        }

Review Comment:
   nit: it's better we could add some comments to explain what's the behavior is the `else` branch



##########
flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRuleTest.scala:
##########
@@ -268,4 +291,19 @@ class FlinkJoinToMultiJoinRuleTest extends TableTestBase {
         """.stripMargin
     util.verifyRelPlan(sqlQuery)
   }
+
+  @Test
+  def testMultiLeftOuterJoinWithAllKeyInLeft: Unit = {

Review Comment:
   is any test with semi/anti to verify the change ?



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -348,22 +456,49 @@ private void copyOuterJoinInfo(
      * @param right Right input of the join
      * @return combined join filters AND-ed together
      */
-    private List<RexNode> combineJoinFilters(Join join, RelNode left, RelNode right) {
+    private List<RexNode> combineJoinFilters(
+            Join join, RelNode left, RelNode right, List<Boolean> inputNullGenFieldList) {
         JoinRelType joinType = join.getJoinType();
+        JoinInfo joinInfo = join.analyzeCondition();
+        ImmutableIntList leftKeys = joinInfo.leftKeys;
+        ImmutableIntList rightKeys = joinInfo.rightKeys;
 
         // AND the join condition if this isn't a left or right outer join; In those cases, the
         // outer join condition is already tracked separately.
         final List<RexNode> filters = new ArrayList<>();
         if ((joinType != JoinRelType.LEFT) && (joinType != JoinRelType.RIGHT)) {
             filters.add(join.getCondition());
         }
-        if (canCombine(left, joinType, joinType.generatesNullsOnLeft())) {
-            filters.add(((MultiJoin) left).getJoinFilter());
+        if (canCombine(
+                left,
+                leftKeys,
+                joinType,
+                joinType.generatesNullsOnLeft(),
+                true,
+                inputNullGenFieldList,
+                0)) {
+            final MultiJoin multiJoin =
+                    left instanceof Project

Review Comment:
   can we handle the Projection now ? does any tests cover the branch ?



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] swuferhong commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
swuferhong commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057024501


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -178,6 +193,60 @@ public void onMatch(RelOptRuleCall call) {
         call.transformTo(multiJoin);
     }
 
+    private void buildInputNullGenFieldList(
+            RelNode left, RelNode right, JoinRelType joinType, List<Boolean> isNullGenFieldList) {
+        if (joinType == JoinRelType.INNER) {
+            buildNullGenFieldList(left, isNullGenFieldList);
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.LEFT) {
+            // left judge.
+            buildNullGenFieldList(left, isNullGenFieldList);
+
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        } else if (joinType == JoinRelType.RIGHT) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+
+            // right judge.
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.FULL) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        }
+    }
+
+    private void buildNullGenFieldList(RelNode rel, List<Boolean> isNullGenFieldList) {
+        MultiJoin multiJoin = rel instanceof MultiJoin ? (MultiJoin) rel : null;
+        if (multiJoin == null) {
+            // other operator.
+            for (int i = 0; i < rel.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(false);
+            }
+        } else {
+            List<RelNode> inputs = multiJoin.getInputs();
+            List<JoinRelType> joinTypes = multiJoin.getJoinTypes();
+            for (int i = 0; i < inputs.size() - 1; i++) {
+                if (joinTypes.get(i) == JoinRelType.RIGHT) {

Review Comment:
   > can you explain why we should just handle RIGHT here?
   
   When building a `multiJoin`, the right join node is a special one. In joinType list `multiJoin.joinRelType`, the right join nodes will be addad as `[RIGHT, INNER]` in joinType list. however, the inner join nodes and left join nodes will be added as `[INNER, INNER]` and `[INNER, LEFT]` perspectively. Therefore, we just handle right join nodes here to obtain the join type in index `i`.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] swuferhong commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
swuferhong commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057034318


##########
flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRuleTest.scala:
##########
@@ -268,4 +291,19 @@ class FlinkJoinToMultiJoinRuleTest extends TableTestBase {
         """.stripMargin
     util.verifyRelPlan(sqlQuery)
   }
+
+  @Test
+  def testMultiLeftOuterJoinWithAllKeyInLeft: Unit = {

Review Comment:
   semi and anti now can not join reorder, so it will not go into `onMatch()` method because of `origJoin.getJoinType().projectsRight()` in `matches()`. So I think the branch `inputNullGenFieldList.isEmpty()` in `canCombine()` is useless and I will remove it.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] godfreyhe commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
godfreyhe commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057046977


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -178,6 +193,60 @@ public void onMatch(RelOptRuleCall call) {
         call.transformTo(multiJoin);
     }
 
+    private void buildInputNullGenFieldList(
+            RelNode left, RelNode right, JoinRelType joinType, List<Boolean> isNullGenFieldList) {
+        if (joinType == JoinRelType.INNER) {
+            buildNullGenFieldList(left, isNullGenFieldList);
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.LEFT) {
+            // left judge.
+            buildNullGenFieldList(left, isNullGenFieldList);
+
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        } else if (joinType == JoinRelType.RIGHT) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+
+            // right judge.
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.FULL) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        }
+    }
+
+    private void buildNullGenFieldList(RelNode rel, List<Boolean> isNullGenFieldList) {
+        MultiJoin multiJoin = rel instanceof MultiJoin ? (MultiJoin) rel : null;
+        if (multiJoin == null) {
+            // other operator.
+            for (int i = 0; i < rel.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(false);
+            }
+        } else {
+            List<RelNode> inputs = multiJoin.getInputs();
+            List<JoinRelType> joinTypes = multiJoin.getJoinTypes();
+            for (int i = 0; i < inputs.size() - 1; i++) {
+                if (joinTypes.get(i) == JoinRelType.RIGHT) {

Review Comment:
   add the comments to the code



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] godfreyhe closed pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
godfreyhe closed pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error
URL: https://github.com/apache/flink/pull/21487


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] swuferhong commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
swuferhong commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057032129


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -378,25 +513,53 @@ private List<RexNode> combineJoinFilters(Join join, RelNode left, RelNode right)
      * @param nullGenerating true if the input is null generating
      * @return true if the input can be combined into a parent MultiJoin
      */
-    private boolean canCombine(RelNode input, JoinRelType joinType, boolean nullGenerating) {
+    private boolean canCombine(
+            RelNode input,
+            ImmutableIntList joinKeys,
+            JoinRelType joinType,
+            boolean nullGenerating,
+            boolean isLeft,
+            List<Boolean> inputNullGenFieldList,
+            int beginIndex) {
+        if (inputNullGenFieldList == null) {
+            // semi and anti join

Review Comment:
   > inputNullGenFieldList is never null
   
   Done!



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] godfreyhe commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
godfreyhe commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057047679


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -178,6 +193,60 @@ public void onMatch(RelOptRuleCall call) {
         call.transformTo(multiJoin);
     }
 
+    private void buildInputNullGenFieldList(
+            RelNode left, RelNode right, JoinRelType joinType, List<Boolean> isNullGenFieldList) {
+        if (joinType == JoinRelType.INNER) {
+            buildNullGenFieldList(left, isNullGenFieldList);
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.LEFT) {
+            // left judge.
+            buildNullGenFieldList(left, isNullGenFieldList);
+
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        } else if (joinType == JoinRelType.RIGHT) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+
+            // right judge.
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.FULL) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        }

Review Comment:
   we should throw exception for `else` branch



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] swuferhong commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
swuferhong commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057056150


##########
flink-table/flink-table-planner/src/test/resources/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRuleTest.xml:
##########
@@ -548,6 +549,46 @@ LogicalProject(a=[$0], b=[$1], c=[$2], d=[$3])
    :  +- LogicalTableScan(table=[[default_catalog, default_database, T2, source: [TestTableSource(c, d)]]])
    +- LogicalProject(e=[$0])
       +- LogicalTableScan(table=[[default_catalog, default_database, T3, source: [TestTableSource(e, f)]]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testMultiLeftOuterJoinWithAllKeyInLeft">
+    <Resource name="sql">
+      <![CDATA[
+SELECT * FROM T1 LEFT OUTER JOIN T2 ON a = c LEFT OUTER JOIN 
+(SELECT * FROM T3) ON a = e LEFT OUTER JOIN
+(SELECT * FROM T4) ON a = g LEFT OUTER JOIN
+(SELECT * FROM T5) ON a = i

Review Comment:
   > the query can be simplied as:
   > 
   > SELECT * FROM T1 LEFT OUTER JOIN T2 ON a = c LEFT OUTER JOIN T3 ON a = e LEFT OUTER JOIN T4 ON a = g LEFT OUTER JOIN T5 ON a = i
   
   Done!



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] swuferhong commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
swuferhong commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057024501


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -178,6 +193,60 @@ public void onMatch(RelOptRuleCall call) {
         call.transformTo(multiJoin);
     }
 
+    private void buildInputNullGenFieldList(
+            RelNode left, RelNode right, JoinRelType joinType, List<Boolean> isNullGenFieldList) {
+        if (joinType == JoinRelType.INNER) {
+            buildNullGenFieldList(left, isNullGenFieldList);
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.LEFT) {
+            // left judge.
+            buildNullGenFieldList(left, isNullGenFieldList);
+
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        } else if (joinType == JoinRelType.RIGHT) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+
+            // right judge.
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.FULL) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        }
+    }
+
+    private void buildNullGenFieldList(RelNode rel, List<Boolean> isNullGenFieldList) {
+        MultiJoin multiJoin = rel instanceof MultiJoin ? (MultiJoin) rel : null;
+        if (multiJoin == null) {
+            // other operator.
+            for (int i = 0; i < rel.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(false);
+            }
+        } else {
+            List<RelNode> inputs = multiJoin.getInputs();
+            List<JoinRelType> joinTypes = multiJoin.getJoinTypes();
+            for (int i = 0; i < inputs.size() - 1; i++) {
+                if (joinTypes.get(i) == JoinRelType.RIGHT) {

Review Comment:
   > can you explain why we should just handle RIGHT here?
   
   When building a `multiJoin`, the right join node is a special one. In joinType list `multiJoin.joinRelType`, the right join nodes will be addad as `[RIGHT, INNER]` in joinType list. however, the inner join nodes and right join nodes will be added as `[INNER, INNER]` and `[INNER, LEFT]` perspectively. Therefore, we just handle right join nodes here to obtain the join type in index `i`.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] swuferhong commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
swuferhong commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057024501


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -178,6 +193,60 @@ public void onMatch(RelOptRuleCall call) {
         call.transformTo(multiJoin);
     }
 
+    private void buildInputNullGenFieldList(
+            RelNode left, RelNode right, JoinRelType joinType, List<Boolean> isNullGenFieldList) {
+        if (joinType == JoinRelType.INNER) {
+            buildNullGenFieldList(left, isNullGenFieldList);
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.LEFT) {
+            // left judge.
+            buildNullGenFieldList(left, isNullGenFieldList);
+
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        } else if (joinType == JoinRelType.RIGHT) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+
+            // right judge.
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.FULL) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        }
+    }
+
+    private void buildNullGenFieldList(RelNode rel, List<Boolean> isNullGenFieldList) {
+        MultiJoin multiJoin = rel instanceof MultiJoin ? (MultiJoin) rel : null;
+        if (multiJoin == null) {
+            // other operator.
+            for (int i = 0; i < rel.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(false);
+            }
+        } else {
+            List<RelNode> inputs = multiJoin.getInputs();
+            List<JoinRelType> joinTypes = multiJoin.getJoinTypes();
+            for (int i = 0; i < inputs.size() - 1; i++) {
+                if (joinTypes.get(i) == JoinRelType.RIGHT) {

Review Comment:
   > can you explain why we should just handle RIGHT here?
   
   When building a `multiJoin`, the right join node is a special one. In joinType list `multiJoin.joinRelType`, the right join nodes will be addad as `[RIGHT, INNER]` in joinType list. however, the inner join nodes and left join nodes will be added as `[INNER, INNER]` and `[INNER, LEFT]` respectively. Therefore, we just handle right join nodes here to obtain the join type in index `i`.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] swuferhong commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
swuferhong commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057031860


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -348,22 +456,49 @@ private void copyOuterJoinInfo(
      * @param right Right input of the join
      * @return combined join filters AND-ed together
      */
-    private List<RexNode> combineJoinFilters(Join join, RelNode left, RelNode right) {
+    private List<RexNode> combineJoinFilters(
+            Join join, RelNode left, RelNode right, List<Boolean> inputNullGenFieldList) {
         JoinRelType joinType = join.getJoinType();
+        JoinInfo joinInfo = join.analyzeCondition();
+        ImmutableIntList leftKeys = joinInfo.leftKeys;
+        ImmutableIntList rightKeys = joinInfo.rightKeys;
 
         // AND the join condition if this isn't a left or right outer join; In those cases, the
         // outer join condition is already tracked separately.
         final List<RexNode> filters = new ArrayList<>();
         if ((joinType != JoinRelType.LEFT) && (joinType != JoinRelType.RIGHT)) {
             filters.add(join.getCondition());
         }
-        if (canCombine(left, joinType, joinType.generatesNullsOnLeft())) {
-            filters.add(((MultiJoin) left).getJoinFilter());
+        if (canCombine(
+                left,
+                leftKeys,
+                joinType,
+                joinType.generatesNullsOnLeft(),
+                true,
+                inputNullGenFieldList,
+                0)) {
+            final MultiJoin multiJoin =
+                    left instanceof Project

Review Comment:
   > can we handle the Projection now ? does any tests cover the branch ?
   
   No, I'm sorry, this part is not cleaned up from the POC code. This PR does not support Projection and other operators now. Supporting these operators is a complicated work, and many cases need to be considered. It may not be supported in 1.17 in my view. Thanks.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] swuferhong commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
swuferhong commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057030213


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -178,6 +193,60 @@ public void onMatch(RelOptRuleCall call) {
         call.transformTo(multiJoin);
     }
 
+    private void buildInputNullGenFieldList(
+            RelNode left, RelNode right, JoinRelType joinType, List<Boolean> isNullGenFieldList) {
+        if (joinType == JoinRelType.INNER) {
+            buildNullGenFieldList(left, isNullGenFieldList);
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.LEFT) {
+            // left judge.
+            buildNullGenFieldList(left, isNullGenFieldList);
+
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        } else if (joinType == JoinRelType.RIGHT) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+
+            // right judge.
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.FULL) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        }

Review Comment:
   > nit: it's better we could add some comments to explain what's the behavior is the `else` branch
   
   Done!
   



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] swuferhong commented on a diff in pull request #21487: [FLINK-30270][table-planner] Fix FlinkJoinToMultiJoinRule incorrectly combines Left/Right outer join to MultiJoin error

Posted by GitBox <gi...@apache.org>.
swuferhong commented on code in PR #21487:
URL: https://github.com/apache/flink/pull/21487#discussion_r1057051388


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkJoinToMultiJoinRule.java:
##########
@@ -178,6 +193,60 @@ public void onMatch(RelOptRuleCall call) {
         call.transformTo(multiJoin);
     }
 
+    private void buildInputNullGenFieldList(
+            RelNode left, RelNode right, JoinRelType joinType, List<Boolean> isNullGenFieldList) {
+        if (joinType == JoinRelType.INNER) {
+            buildNullGenFieldList(left, isNullGenFieldList);
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.LEFT) {
+            // left judge.
+            buildNullGenFieldList(left, isNullGenFieldList);
+
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        } else if (joinType == JoinRelType.RIGHT) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+
+            // right judge.
+            buildNullGenFieldList(right, isNullGenFieldList);
+        } else if (joinType == JoinRelType.FULL) {
+            for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+            for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(true);
+            }
+        }
+    }
+
+    private void buildNullGenFieldList(RelNode rel, List<Boolean> isNullGenFieldList) {
+        MultiJoin multiJoin = rel instanceof MultiJoin ? (MultiJoin) rel : null;
+        if (multiJoin == null) {
+            // other operator.
+            for (int i = 0; i < rel.getRowType().getFieldCount(); i++) {
+                isNullGenFieldList.add(false);
+            }
+        } else {
+            List<RelNode> inputs = multiJoin.getInputs();
+            List<JoinRelType> joinTypes = multiJoin.getJoinTypes();
+            for (int i = 0; i < inputs.size() - 1; i++) {
+                if (joinTypes.get(i) == JoinRelType.RIGHT) {

Review Comment:
   > 
   
   already done!



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org