ColinLeeo commented on code in PR #18826:
URL: https://github.com/apache/iotdb/pull/18826#discussion_r4279852694


##########
iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/column/binary/BinaryColumnTransformer.java:
##########
@@ -54,21 +57,14 @@ public void evaluate() {
   }
 
   @Override
-  public void evaluateWithSelection(boolean[] selection) {
-    leftTransformer.evaluateWithSelection(selection);
-    // attention: get positionCount before calling getColumn
-    int positionCount = leftTransformer.getColumnCachePositionCount();
-    Column leftColumn = leftTransformer.getColumn();
-    // When the rightTransformer has the same constant as the leftTransformer, 
the leftTransformer's
-    // cache will be cleared, so need to getColumn before evaluate 
rightTransformer.
-    rightTransformer.evaluateWithSelection(selection);
-    Column rightColumn = rightTransformer.getColumn();
+  protected Column evaluateSelection(boolean[] selection) {
+    Column leftColumn = leftTransformer.getColumnWithSelection(selection);
+    Column rightColumn = rightTransformer.getColumnWithSelection(selection);
 
+    int positionCount = leftColumn.getPositionCount();
     ColumnBuilder builder = returnType.createColumnBuilder(positionCount);
     doTransform(leftColumn, rightColumn, builder, positionCount, selection);
-    initializeColumnCache(builder.build());
-    this.leftTransformer.clearCache();
-    this.rightTransformer.clearCache();
+    return builder.build();

Review Comment:
   Added per-input full-result retention to binary `DiffColumnTransformer`, 
including complete value/ignoreNull inputs and new-input reset. Added 
dynamic-argument unit and SQL regressions.



##########
iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/column/unary/scalar/DiffFunctionColumnTransformer.java:
##########
@@ -39,23 +37,46 @@ public class DiffFunctionColumnTransformer extends 
UnaryColumnTransformer {
   // indicate whether lastValue is null
   private boolean lastValueIsNull = true;
 
+  // DIFF must advance its state once per input, even when several 
masks/consumers request it.
+  private Column currentInputResult;
+
   public DiffFunctionColumnTransformer(
       Type returnType, ColumnTransformer childColumnTransformer, boolean 
ignoreNull) {
     super(returnType, childColumnTransformer);
     this.ignoreNull = ignoreNull;
   }
 
   @Override
-  public void evaluateWithSelection(boolean[] selection) {
-    // The DIFF function depends on forward and backward rows and cannot be 
short-circuited.
-    boolean[] selectionCopy = new boolean[selection.length];
-    Arrays.fill(selectionCopy, true);
-    childColumnTransformer.evaluateWithSelection(selectionCopy);
-    Column column = childColumnTransformer.getColumn();
-    ColumnBuilder columnBuilder = 
returnType.createColumnBuilder(column.getPositionCount());
-    doTransform(column, columnBuilder, selection);
-    initializeColumnCache(columnBuilder.build());
-    childColumnTransformer.clearCache();
+  public void evaluate() {
+    initializeColumnCache(evaluateCurrentInput());
+  }
+
+  @Override
+  protected Column evaluateSelection(boolean[] selection) {
+    // DIFF depends on all preceding rows, including rows rejected by a 
CASE/filter mask.
+    return evaluateCurrentInput();
+  }
+
+  private Column evaluateCurrentInput() {
+    if (currentInputResult == null) {
+      childColumnTransformer.tryEvaluate();
+      Column column = childColumnTransformer.getColumn();
+      ColumnBuilder builder = 
returnType.createColumnBuilder(column.getPositionCount());
+      doTransform(column, builder);
+      currentInputResult = builder.build();
+    }
+    return currentInputResult;
+  }
+
+  @Override
+  protected void resetForNewInput() {
+    currentInputResult = null;
+  }

Review Comment:
   Documented and tested the lifecycle: `clearCache()` keeps same-input DIFF 
results to avoid advancing state twice; `clearCacheForNewInput()` releases them 
before new input.



##########
iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/column/unary/scalar/DiffFunctionColumnTransformer.java:
##########
@@ -39,23 +37,46 @@ public class DiffFunctionColumnTransformer extends 
UnaryColumnTransformer {
   // indicate whether lastValue is null
   private boolean lastValueIsNull = true;
 
+  // DIFF must advance its state once per input, even when several 
masks/consumers request it.
+  private Column currentInputResult;
+
   public DiffFunctionColumnTransformer(
       Type returnType, ColumnTransformer childColumnTransformer, boolean 
ignoreNull) {
     super(returnType, childColumnTransformer);
     this.ignoreNull = ignoreNull;
   }
 
   @Override
-  public void evaluateWithSelection(boolean[] selection) {
-    // The DIFF function depends on forward and backward rows and cannot be 
short-circuited.
-    boolean[] selectionCopy = new boolean[selection.length];
-    Arrays.fill(selectionCopy, true);
-    childColumnTransformer.evaluateWithSelection(selectionCopy);
-    Column column = childColumnTransformer.getColumn();
-    ColumnBuilder columnBuilder = 
returnType.createColumnBuilder(column.getPositionCount());
-    doTransform(column, columnBuilder, selection);
-    initializeColumnCache(columnBuilder.build());
-    childColumnTransformer.clearCache();
+  public void evaluate() {
+    initializeColumnCache(evaluateCurrentInput());
+  }
+
+  @Override
+  protected Column evaluateSelection(boolean[] selection) {
+    // DIFF depends on all preceding rows, including rows rejected by a 
CASE/filter mask.
+    return evaluateCurrentInput();
+  }
+
+  private Column evaluateCurrentInput() {
+    if (currentInputResult == null) {
+      childColumnTransformer.tryEvaluate();
+      Column column = childColumnTransformer.getColumn();
+      ColumnBuilder builder = 
returnType.createColumnBuilder(column.getPositionCount());
+      doTransform(column, builder);
+      currentInputResult = builder.build();

Review Comment:
   Both peak and retained-memory estimates now include one page per distinct 
unary/binary DIFF node, deduplicated across phases. Added nested/independent 
DIFF and shared-node regressions.



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