You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2019/11/04 04:00:02 UTC

[GitHub] [calcite] hsyuan commented on a change in pull request #1496: [CALCITE-3400] Implement left/right/semi/anti/full join in Interpreter

hsyuan commented on a change in pull request #1496: [CALCITE-3400] Implement left/right/semi/anti/full join in Interpreter
URL: https://github.com/apache/calcite/pull/1496#discussion_r341899066
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/interpreter/JoinNode.java
 ##########
 @@ -47,28 +50,121 @@ public JoinNode(Compiler compiler, Join rel) {
   }
 
   public void run() throws InterruptedException {
-    List<Row> rightList = null;
-    final int leftCount = rel.getLeft().getRowType().getFieldCount();
-    final int rightCount = rel.getRight().getRowType().getFieldCount();
-    context.values = new Object[rel.getRowType().getFieldCount()];
-    Row left;
-    Row right;
-    while ((left = leftSource.receive()) != null) {
-      System.arraycopy(left.getValues(), 0, context.values, 0, leftCount);
-      if (rightList == null) {
-        rightList = new ArrayList<>();
-        while ((right = rightSource.receive()) != null) {
-          rightList.add(right);
+
+    final int fieldCount = rel.getLeft().getRowType().getFieldCount()
+        + rel.getRight().getRowType().getFieldCount();
+    context.values = new Object[fieldCount];
+
+    // source for the outer relation of nested loop
+    Source outerSource = leftSource;
+    // source for the inner relation of nested loop
+    Source innerSource = rightSource;
+    if (rel.getJoinType() == JoinRelType.RIGHT) {
+      outerSource = rightSource;
+      innerSource = leftSource;
+    }
+
+    // row from outer source
+    Row outerRow = null;
+    // rows from inner source
+    List<Row> innerRows = null;
+    Set<Row> matchRowSet = new HashSet<>();
+    while ((outerRow = outerSource.receive()) != null) {
+      if (innerRows == null) {
+        innerRows = new ArrayList<Row>();
+        Row innerRow = null;
+        while ((innerRow = innerSource.receive()) != null) {
+          innerRows.add(innerRow);
         }
       }
-      for (Row right2 : rightList) {
-        System.arraycopy(right2.getValues(), 0, context.values, leftCount,
-            rightCount);
-        final Boolean execute = (Boolean) condition.execute(context);
-        if (execute != null && execute) {
+      matchRowSet.addAll(doJoin(outerRow, innerRows, rel.getJoinType()));
+    }
+    if (rel.getJoinType() == JoinRelType.FULL) {
+      // send un-match rows for full join on right source
+      List<Row> empty = new ArrayList<>();
+      for (Row row: innerRows) {
+        if (matchRowSet.contains(row)) {
+          continue;
+        }
+        doSend(row, empty, JoinRelType.RIGHT);
+      }
+    }
+  }
+
+  /**
+   * Execution of the join action, returns the matched rows for the outer source row.
+   */
+  private List<Row> doJoin(Row outerRow, List<Row> innerRows,
+      JoinRelType joinRelType) throws InterruptedException {
+    boolean outerRowOnLeft = joinRelType != JoinRelType.RIGHT;
+    copyToContext(outerRow, outerRowOnLeft);
+    List<Row> matchInnerRows = new ArrayList<>();
+    for (Row innerRow: innerRows) {
+      copyToContext(innerRow, !outerRowOnLeft);
+      final Boolean execute = (Boolean) condition.execute(context);
+      if (execute != null && execute) {
+        matchInnerRows.add(innerRow);
+      }
+    }
+    doSend(outerRow, matchInnerRows, joinRelType);
+    return ImmutableList.copyOf(matchInnerRows);
 
 Review comment:
   Do wen need ImmutableList copy? matchRowSet.addAll() will copy them again, right?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services