This is an automated email from the ASF dual-hosted git repository.

caogaofei pushed a commit to branch beyyes/fix_mergesort_exist_null
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to 
refs/heads/beyyes/fix_mergesort_exist_null by this push:
     new 4cbefcfd2b8 fix left right join key positions usage
4cbefcfd2b8 is described below

commit 4cbefcfd2b8ae39025e56f2e24bc0cbb316f875e
Author: Beyyes <[email protected]>
AuthorDate: Tue Dec 10 15:16:45 2024 +0800

    fix left right join key positions usage
---
 .../relational/AbstractMergeSortJoinOperator.java  | 69 +++++++++++++---------
 .../relational/MergeSortFullOuterJoinOperator.java | 39 ++++++++++--
 .../relational/MergeSortInnerJoinOperator.java     | 16 ++++-
 3 files changed, 89 insertions(+), 35 deletions(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/AbstractMergeSortJoinOperator.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/AbstractMergeSortJoinOperator.java
index 27c325bd663..82c7b69e4af 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/AbstractMergeSortJoinOperator.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/AbstractMergeSortJoinOperator.java
@@ -212,7 +212,7 @@ public abstract class AbstractMergeSortJoinOperator extends 
AbstractOperator {
   // TODO introduce joinKey list, if join key size equals to 1, can return 
true in inner join
   protected boolean allRightLessThanLeft() {
     for (int i = 0; i < comparators.size(); i++) {
-      if (!comparators
+      if (comparators
           .get(i)
           .lessThan(
               rightBlockList.get(rightBlockList.size() - 1),
@@ -222,16 +222,16 @@ public abstract class AbstractMergeSortJoinOperator 
extends AbstractOperator {
               leftJoinKeyPositions[i],
               leftIndex)
           .orElse(false)) {
-        return false;
+        return true;
       }
     }
-    return true;
+    return false;
   }
 
   // check if the last value of the left is less than right
   protected boolean allLeftLessThanRight() {
     for (int i = 0; i < comparators.size(); i++) {
-      if (!comparators
+      if (comparators
           .get(i)
           .lessThan(
               leftBlock,
@@ -241,10 +241,10 @@ public abstract class AbstractMergeSortJoinOperator 
extends AbstractOperator {
               rightJoinKeyPositions[i],
               rightIndex)
           .orElse(false)) {
-        return false;
+        return true;
       }
     }
-    return true;
+    return false;
   }
 
   /**
@@ -256,13 +256,17 @@ public abstract class AbstractMergeSortJoinOperator 
extends AbstractOperator {
   protected boolean currentRoundNeedStop() {
     if (lessThan(
             rightBlockList.get(0),
+            rightJoinKeyPositions,
             rightBlockList.get(0).getPositionCount() - 1,
             rightBlockList.get(rightBlockListIdx),
+            rightJoinKeyPositions,
             rightIndex)
         || lessThan(
             rightBlockList.get(0),
+            rightJoinKeyPositions,
             rightBlockList.get(0).getPositionCount() - 1,
             leftBlock,
+            leftJoinKeyPositions,
             leftIndex)) {
       for (int i = 0; i < rightBlockListIdx; i++) {
         long size = rightBlockList.get(i).getRetainedSizeInBytes();
@@ -348,7 +352,12 @@ public abstract class AbstractMergeSortJoinOperator 
extends AbstractOperator {
         // to
         // rightBlockList
         if (equalsTo(
-            block, 0, rightBlockList.get(0), 
rightBlockList.get(0).getPositionCount() - 1)) {
+            block,
+            rightJoinKeyPositions,
+            0,
+            rightBlockList.get(0),
+            rightJoinKeyPositions,
+            rightBlockList.get(0).getPositionCount() - 1)) {
           addRightBlockWithMemoryReservation(block);
         } else {
           cachedNextRightBlock = block;
@@ -392,7 +401,13 @@ public abstract class AbstractMergeSortJoinOperator 
extends AbstractOperator {
     int tmpBlockIdx = rightBlockListIdx;
     int tmpIdx = rightIndex;
     boolean hasMatched = false;
-    while (equalsTo(leftBlock, leftIndex, rightBlockList.get(tmpBlockIdx), 
tmpIdx)) {
+    while (equalsTo(
+        leftBlock,
+        leftJoinKeyPositions,
+        leftIndex,
+        rightBlockList.get(tmpBlockIdx),
+        rightJoinKeyPositions,
+        tmpIdx)) {
       hasMatched = true;
       recordsWhenDataMatches();
       appendValueToResultWhenMatches(tmpBlockIdx, tmpIdx);
@@ -410,35 +425,35 @@ public abstract class AbstractMergeSortJoinOperator 
extends AbstractOperator {
     return hasMatched;
   }
 
-  protected boolean lessThan(TsBlock leftBlock, int lIndex, TsBlock 
rightBlock, int rIndex) {
+  protected boolean lessThan(
+      TsBlock leftBlock,
+      int[] leftPositions,
+      int lIndex,
+      TsBlock rightBlock,
+      int[] rightPositions,
+      int rIndex) {
     for (int i = 0; i < comparators.size(); i++) {
-      if (!comparators
+      if (comparators
           .get(i)
-          .lessThan(
-              leftBlock,
-              leftJoinKeyPositions[i],
-              lIndex,
-              rightBlock,
-              rightJoinKeyPositions[i],
-              rIndex)
+          .lessThan(leftBlock, leftPositions[i], lIndex, rightBlock, 
rightPositions[i], rIndex)
           .orElse(false)) {
-        return false;
+        return true;
       }
     }
-    return true;
+    return false;
   }
 
-  protected boolean equalsTo(TsBlock leftBlock, int lIndex, TsBlock 
rightBlock, int rIndex) {
+  protected boolean equalsTo(
+      TsBlock leftBlock,
+      int[] leftPositions,
+      int lIndex,
+      TsBlock rightBlock,
+      int[] rightPositions,
+      int rIndex) {
     for (int i = 0; i < comparators.size(); i++) {
       if (!comparators
           .get(i)
-          .equalsTo(
-              leftBlock,
-              leftJoinKeyPositions[i],
-              lIndex,
-              rightBlock,
-              rightJoinKeyPositions[i],
-              rIndex)
+          .equalsTo(leftBlock, leftPositions[i], lIndex, rightBlock, 
rightPositions[i], rIndex)
           .orElse(false)) {
         return false;
       }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/MergeSortFullOuterJoinOperator.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/MergeSortFullOuterJoinOperator.java
index 0e36c10927d..88ea2007b68 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/MergeSortFullOuterJoinOperator.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/MergeSortFullOuterJoinOperator.java
@@ -37,10 +37,10 @@ public class MergeSortFullOuterJoinOperator extends 
AbstractMergeSortJoinOperato
   private static final long INSTANCE_SIZE =
       
RamUsageEstimator.shallowSizeOfInstance(MergeSortFullOuterJoinOperator.class);
 
-  private final BiFunction<Column[], Integer, TsBlock> 
updateLastMatchedRowFunction;
-
   // stores last row matched join criteria, only used in outer join
   private TsBlock lastMatchedRightBlock = null;
+  private final int[] lastMatchedBlockPositions;
+  private final BiFunction<Column[], Integer, TsBlock> 
updateLastMatchedRowFunction;
 
   public MergeSortFullOuterJoinOperator(
       OperatorContext operatorContext,
@@ -63,6 +63,10 @@ public class MergeSortFullOuterJoinOperator extends 
AbstractMergeSortJoinOperato
         rightOutputSymbolIdx,
         joinKeyComparators,
         dataTypes);
+    lastMatchedBlockPositions = new int[joinKeyComparators.size()];
+    for (int i = 0; i < lastMatchedBlockPositions.length; i++) {
+      lastMatchedBlockPositions[i] = i;
+    }
     this.updateLastMatchedRowFunction = updateLastMatchedRowFunction;
   }
 
@@ -117,12 +121,24 @@ public class MergeSortFullOuterJoinOperator extends 
AbstractMergeSortJoinOperato
       }
     }
     // continue right < left, until right >= left
-    while (lessThan(rightBlockList.get(rightBlockListIdx), rightIndex, 
leftBlock, leftIndex)) {
+    while (lessThan(
+        rightBlockList.get(rightBlockListIdx),
+        rightJoinKeyPositions,
+        rightIndex,
+        leftBlock,
+        leftJoinKeyPositions,
+        leftIndex)) {
       if (lastMatchedRightBlock == null) {
         appendOneRightRowWithEmptyLeft();
       } else {
         // CurrentRight can only be greater than or equal to lastMatchedRight.
-        if (lessThan(lastMatchedRightBlock, 0, 
rightBlockList.get(rightBlockListIdx), rightIndex)) {
+        if (lessThan(
+            lastMatchedRightBlock,
+            lastMatchedBlockPositions,
+            0,
+            rightBlockList.get(rightBlockListIdx),
+            rightJoinKeyPositions,
+            rightIndex)) {
           appendOneRightRowWithEmptyLeft();
         }
       }
@@ -143,7 +159,13 @@ public class MergeSortFullOuterJoinOperator extends 
AbstractMergeSortJoinOperato
       }
     }
     // continue left < right, until left >= right
-    while (lessThan(leftBlock, leftIndex, 
rightBlockList.get(rightBlockListIdx), rightIndex)) {
+    while (lessThan(
+        leftBlock,
+        leftJoinKeyPositions,
+        leftIndex,
+        rightBlockList.get(rightBlockListIdx),
+        rightJoinKeyPositions,
+        rightIndex)) {
       appendOneLeftRowWithEmptyRight();
       if (leftFinishedWithIncIndex()) {
         return true;
@@ -186,7 +208,12 @@ public class MergeSortFullOuterJoinOperator extends 
AbstractMergeSortJoinOperato
       // result.
       if (lastMatchedRightBlock == null
           || lessThan(
-              lastMatchedRightBlock, 0, rightBlockList.get(rightBlockListIdx), 
rightIndex)) {
+              lastMatchedRightBlock,
+              lastMatchedBlockPositions,
+              0,
+              rightBlockList.get(rightBlockListIdx),
+              rightJoinKeyPositions,
+              rightIndex)) {
         for (int i = 0; i < leftOutputSymbolIdx.length; i++) {
           ColumnBuilder columnBuilder = resultBuilder.getColumnBuilder(i);
           columnBuilder.appendNull();
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/MergeSortInnerJoinOperator.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/MergeSortInnerJoinOperator.java
index 45ced1d82a9..fd61b0fde64 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/MergeSortInnerJoinOperator.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/MergeSortInnerJoinOperator.java
@@ -91,7 +91,13 @@ public class MergeSortInnerJoinOperator extends 
AbstractMergeSortJoinOperator {
       }
     }
     // continue right < left, until right >= left
-    while (lessThan(rightBlockList.get(rightBlockListIdx), rightIndex, 
leftBlock, leftIndex)) {
+    while (lessThan(
+        rightBlockList.get(rightBlockListIdx),
+        rightJoinKeyPositions,
+        rightIndex,
+        leftBlock,
+        leftJoinKeyPositions,
+        leftIndex)) {
       if (rightFinishedWithIncIndex()) {
         return true;
       }
@@ -107,7 +113,13 @@ public class MergeSortInnerJoinOperator extends 
AbstractMergeSortJoinOperator {
       }
     }
     // continue left < right, until left >= right
-    while (lessThan(leftBlock, leftIndex, 
rightBlockList.get(rightBlockListIdx), rightIndex)) {
+    while (lessThan(
+        leftBlock,
+        leftJoinKeyPositions,
+        leftIndex,
+        rightBlockList.get(rightBlockListIdx),
+        rightJoinKeyPositions,
+        rightIndex)) {
       if (leftFinishedWithIncIndex()) {
         return true;
       }

Reply via email to