JackieTien97 commented on code in PR #13716:
URL: https://github.com/apache/iotdb/pull/13716#discussion_r1798654083


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/transformation/dag/column/AbstractCaseWhenThenColumnTransformer.java:
##########
@@ -55,108 +55,185 @@ public ColumnTransformer getElseTransformer() {
 
   @Override
   public void evaluate() {
-    int[] branchIndexForEachRow = new 
int[elseTransformer.getColumnCachePositionCount()];
+
+    List<Column> thenColumnList = new ArrayList<>();
+
+    // region evaluate first when
+    ColumnTransformer firstWhenColumnTransformer = 
whenThenTransformers.get(0).left;
+    firstWhenColumnTransformer.evaluate();
+    Column firstWhenColumn = firstWhenColumnTransformer.getColumn();
+
+    int positionCount = firstWhenColumn.getPositionCount();
+    boolean[] selection = new boolean[positionCount];
+    Arrays.fill(selection, true);
+
+    int[] branchIndexForEachRow = new int[positionCount];
     Arrays.fill(branchIndexForEachRow, -1);
 
-    doTransform(branchIndexForEachRow);
+    boolean[] selectionForThen = selection.clone();
+
+    // 根据第一个 whenColumn更新 branchIndexForEachRow
+    for (int i = 0; i < positionCount; i++) {
+      // 当前行没有匹配的 whenTransformer 时,才更新 selectionForThen 和 
branchIndexForEachRow
+      if (branchIndexForEachRow[i] == -1) {
+        if (!firstWhenColumn.isNull(i) && firstWhenColumn.getBoolean(i)) {
+          branchIndexForEachRow[i] = 0;
+          selectionForThen[i] = true;
+        } else {
+          selectionForThen[i] = false;
+        }
+      }
+    }
+
+    ColumnTransformer firstThenColumnTransformer = 
whenThenTransformers.get(0).right;
+    firstThenColumnTransformer.evaluateWithSelection(selectionForThen);
+    Column firstThenColumn = firstThenColumnTransformer.getColumn();
+    thenColumnList.add(firstThenColumn);
+
+    // endregion
+
+    // when and then columns
+    for (int i = 1; i < whenThenTransformers.size(); i++) {
+      ColumnTransformer whenColumnTransformer = 
whenThenTransformers.get(i).left;
+      whenColumnTransformer.evaluateWithSelection(selection);
+      Column whenColumn = whenColumnTransformer.getColumn();
+
+      selectionForThen = selection.clone();
+
+      // 初始化 selectionForThen
+      for (int j = 0; j < positionCount; j++) {
+        if (branchIndexForEachRow[j] == -1
+            || branchIndexForEachRow[j] == whenThenTransformers.size()) {
+          selectionForThen[j] = false;
+        }
+      }
+
+      // 根据第一个 whenColumn更新 branchIndexForEachRow
+      for (int j = 0; j < positionCount; j++) {
+        // 当前行没有匹配的 whenTransformer 时,才更新 selectionForThen 和 
branchIndexForEachRow
+        if (branchIndexForEachRow[j] == -1) {
+          if (!whenColumn.isNull(j) && whenColumn.getBoolean(j)) {
+            branchIndexForEachRow[j] = i;
+            selectionForThen[j] = true;
+          } else {
+            selectionForThen[j] = false;
+          }
+        }
+      }
+
+      ColumnTransformer thenColumnTransformer = 
whenThenTransformers.get(i).right;
+      thenColumnTransformer.evaluateWithSelection(selectionForThen);
+      Column thenColumn = thenColumnTransformer.getColumn();
+      thenColumnList.add(thenColumn);
+    }
+
+    // elseColumn
+    doTransform(branchIndexForEachRow, positionCount, thenColumnList);
   }
 
   @Override
   public void evaluateWithSelection(boolean[] selection) {
+
+    // region initialize branchIndexForEachRow.
+    // branchIndexForEachRow indicates the index of the WhenTransformer 
matched by each row.
     int[] branchIndexForEachRow = new int[selection.length];
+    // positionCount indicates the length of column
+    int positionCount = selection.length;
+
+    // 赋值为-1表示需要进行求值,否则表示不需要进行求值
     for (int i = 0; i < selection.length; i++) {
       if (selection[i]) {
         branchIndexForEachRow[i] = -1;
       } else {
         branchIndexForEachRow[i] = whenThenTransformers.size();
       }
     }
-    doTransform(branchIndexForEachRow);
-  }
+    // endregion
 
-  private void doTransform(int[] branch) {
-    int[] branchIndexForEachRow = null;
     List<Column> thenColumnList = new ArrayList<>();
 
     // when and then columns
     for (int i = 0; i < whenThenTransformers.size(); i++) {
       ColumnTransformer whenColumnTransformer = 
whenThenTransformers.get(i).left;
-      whenColumnTransformer.tryEvaluate();
+      whenColumnTransformer.evaluateWithSelection(selection);
       Column whenColumn = whenColumnTransformer.getColumn();
 
-      int positionCount = whenColumn.getPositionCount();
-      boolean[] selection = new boolean[positionCount];
+      boolean[] selectionForThen = selection.clone();
 
-      if (branchIndexForEachRow == null) {
-        // init branchIndexForEachRow if it is null
-        branchIndexForEachRow = new int[positionCount];
-        Arrays.fill(branchIndexForEachRow, -1);
-      } else {
-        // update selection with branchIndexForEachRow
-        for (int j = 0; j < branchIndexForEachRow.length; j++) {
-          if (branchIndexForEachRow[j] != -1) {
-            selection[j] = true;
-          }
+      // 初始化 selectionForThen
+      for (int j = 0; j < positionCount; j++) {
+        if (branchIndexForEachRow[j] == -1
+            || branchIndexForEachRow[j] == whenThenTransformers.size()) {
+          selectionForThen[j] = false;

Review Comment:
   why updating selectionForThen in two places? the following for-loop is also 
doing such thing.



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