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


##########
integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBCaseDeviceCacheIT.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.relational.it.schema;
+
+import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.it.framework.IoTDBTestRunner;
+import org.apache.iotdb.itbase.category.TableClusterIT;
+import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
+import org.apache.iotdb.itbase.env.BaseEnv;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+
+import static org.apache.iotdb.db.it.utils.TestUtils.assertResultSetEqual;
+import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData;
+
+@RunWith(IoTDBTestRunner.class)
+@Category({TableLocalStandaloneIT.class, TableClusterIT.class})
+public class IoTDBCaseDeviceCacheIT {
+  private static final String DATABASE = "case_device_cache";
+  private static final String CASE_RESULT = "CASE WHEN src = 'match' THEN 
'match' ELSE 'other' END";
+
+  @BeforeClass
+  public static void setUp() throws Exception {
+    // Keep every device in the same schema region and force a new batch for 
each device.
+    
EnvFactory.getEnv().getConfig().getCommonConfig().setDefaultSchemaRegionGroupNumPerDatabase(1);
+    
EnvFactory.getEnv().getConfig().getCommonConfig().setMaxTsBlockLineNumber(1);
+    EnvFactory.getEnv().initClusterEnvironment();
+    prepareTableData(new String[] {"CREATE DATABASE " + DATABASE});
+  }
+
+  @AfterClass
+  public static void tearDown() throws Exception {
+    EnvFactory.getEnv().cleanClusterEnvironment();
+  }
+
+  @Test
+  public void testDevicePredicatesAcrossBatches() throws Exception {
+    try (Connection connection = 
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
+        Statement statement = connection.createStatement()) {
+      createTable(statement, "predicate_cache");
+      String predicate = CASE_RESULT + " = src";
+      assertResultSetEqual(
+          statement.executeQuery("SHOW DEVICES FROM predicate_cache WHERE " + 
predicate),
+          "id,src,a,b,",
+          new HashSet<>(
+              Arrays.asList(
+                  "d0,match,null,null,",
+                  "d1,match,null,null,",
+                  "d2,other,null,null,",
+                  "d3,other,null,null,")));
+      assertResultSetEqual(
+          statement.executeQuery("COUNT DEVICES FROM predicate_cache WHERE " + 
predicate),
+          "count(devices),",
+          Collections.singleton("4,"));
+    }
+  }
+
+  @Test
+  public void testAttributeProjectionAcrossBatches() throws Exception {
+    try (Connection connection = 
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
+        Statement statement = connection.createStatement()) {
+      createTable(statement, "projection_cache");
+      statement.execute("UPDATE projection_cache SET a = " + CASE_RESULT + ", 
b = src");
+      assertResultSetEqual(
+          statement.executeQuery("SHOW DEVICES FROM projection_cache"),
+          "id,src,a,b,",
+          new HashSet<>(
+              Arrays.asList(
+                  "d0,match,match,match,",
+                  "d1,match,match,match,",
+                  "d2,other,other,other,",
+                  "d3,other,other,other,")));
+    }
+  }
+
+  @Test
+  public void testAttributeCommonExpressionClearedByCase() throws Exception {
+    try (Connection connection = 
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
+        Statement statement = connection.createStatement()) {
+      createTable(statement, "common_cache");
+      statement.execute(
+          "UPDATE common_cache SET a = src "
+              + "WHERE CASE WHEN src = 'match' THEN true ELSE false END");

Review Comment:
   Changed the UPDATE regression to share `upper(src)` between SET and WHERE, 
and added the guarded-cast UPDATE regression.



##########
integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBCaseDeviceCacheIT.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.relational.it.schema;
+
+import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.it.framework.IoTDBTestRunner;
+import org.apache.iotdb.itbase.category.TableClusterIT;
+import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
+import org.apache.iotdb.itbase.env.BaseEnv;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+
+import static org.apache.iotdb.db.it.utils.TestUtils.assertResultSetEqual;
+import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData;
+
+@RunWith(IoTDBTestRunner.class)
+@Category({TableLocalStandaloneIT.class, TableClusterIT.class})
+public class IoTDBCaseDeviceCacheIT {
+  private static final String DATABASE = "case_device_cache";
+  private static final String CASE_RESULT = "CASE WHEN src = 'match' THEN 
'match' ELSE 'other' END";
+
+  @BeforeClass
+  public static void setUp() throws Exception {
+    // Keep every device in the same schema region and force a new batch for 
each device.
+    
EnvFactory.getEnv().getConfig().getCommonConfig().setDefaultSchemaRegionGroupNumPerDatabase(1);
+    
EnvFactory.getEnv().getConfig().getCommonConfig().setMaxTsBlockLineNumber(1);
+    EnvFactory.getEnv().initClusterEnvironment();
+    prepareTableData(new String[] {"CREATE DATABASE " + DATABASE});
+  }
+
+  @AfterClass
+  public static void tearDown() throws Exception {
+    EnvFactory.getEnv().cleanClusterEnvironment();
+  }
+
+  @Test
+  public void testDevicePredicatesAcrossBatches() throws Exception {
+    try (Connection connection = 
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
+        Statement statement = connection.createStatement()) {
+      createTable(statement, "predicate_cache");
+      String predicate = CASE_RESULT + " = src";

Review Comment:
   Added both predicate orientations to the SHOW/COUNT DEVICES integration 
tests, plus the `concat(src, CASE ...)` UPDATE regression.



##########
iotdb-core/calc-commons/src/test/java/org/apache/iotdb/calc/execution/operator/process/FilterAndProjectOperatorTest.java:
##########
@@ -0,0 +1,555 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.calc.execution.operator.process;
+
+import org.apache.iotdb.calc.execution.operator.CommonOperatorContext;
+import org.apache.iotdb.calc.execution.operator.Operator;
+import org.apache.iotdb.calc.plan.planner.memory.MemoryReservationManager;
+import org.apache.iotdb.calc.transformation.dag.column.ColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.FailFunctionColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.TableCaseWhenThenColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.binary.ArithmeticAdditionColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.binary.CompareGreaterThanColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.leaf.ConstantColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.leaf.IdentityColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.leaf.LeafColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.leaf.NullColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.multi.MappableUDFColumnTransformer;
+import org.apache.iotdb.calc.transformation.dag.udf.UDTFExecutor;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeId;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.block.column.ColumnBuilder;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.read.common.block.TsBlock;
+import org.apache.tsfile.read.common.block.column.DoubleColumn;
+import org.apache.tsfile.read.common.block.column.RunLengthEncodedColumn;
+import org.apache.tsfile.read.common.block.column.TimeColumn;
+import org.junit.Test;
+
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Optional;
+import java.util.function.Consumer;
+
+import static org.apache.tsfile.read.common.type.BooleanType.BOOLEAN;
+import static org.apache.tsfile.read.common.type.DoubleType.DOUBLE;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class FilterAndProjectOperatorTest {
+
+  @Test
+  public void testProjectionDoesNotReuseColumnsAcrossDifferentSizedBlocks() 
throws Exception {
+    Graph graph = new Graph();
+    ColumnTransformer input = graph.input(0);
+    ColumnTransformer caseWhen =
+        graph.caseWhen(graph.greaterThan(input, 0), graph.constant(1), 
graph.nullValue());
+    try (Operator operator =
+        project(graph, new TsBlock[] {block(1), block(-2, 3), block(4)}, 
caseWhen, input)) {
+      assertBlock(operator.next(), new Double[] {1.0}, new Double[] {1.0});
+      assertBlock(operator.next(), new Double[] {null, 1.0}, new Double[] 
{-2.0, 3.0});
+      assertBlock(operator.next(), new Double[] {1.0}, new Double[] {4.0});
+    }
+  }
+
+  @Test
+  public void testProjectionDoesNotReuseColumnsAcrossEqualSizedBlocks() throws 
Exception {
+    Graph graph = new Graph();
+    ColumnTransformer input = graph.input(0);
+    ColumnTransformer caseWhen =
+        graph.caseWhen(graph.greaterThan(input, 0), graph.constant(1), 
graph.nullValue());
+    try (Operator operator =
+        project(graph, new TsBlock[] {block(1, 2), block(-3, 4)}, caseWhen, 
input)) {
+      assertBlock(operator.next(), new Double[] {1.0, 1.0}, new Double[] {1.0, 
2.0});
+      assertBlock(operator.next(), new Double[] {null, 1.0}, new Double[] 
{-3.0, 4.0});
+    }
+  }
+
+  @Test
+  public void testProjectionHandlesEmptyBlocks() throws Exception {
+    Graph graph = new Graph();
+    ColumnTransformer input = graph.input(0);
+    ColumnTransformer caseWhen =
+        graph.caseWhen(graph.greaterThan(input, 0), graph.constant(1), 
graph.nullValue());
+    try (Operator operator =
+        project(graph, new TsBlock[] {block(1), block(), block(-2)}, caseWhen, 
input)) {
+      assertBlock(operator.next(), new Double[] {1.0}, new Double[] {1.0});
+      assertBlock(operator.next(), new Double[0], new Double[0]);
+      assertBlock(operator.next(), new Double[] {null}, new Double[] {-2.0});
+    }
+  }
+
+  @Test
+  public void testProjectionClearsSharedIntermediateExpressions() throws 
Exception {
+    Graph graph = new Graph();
+    ColumnTransformer shared = graph.add(graph.input(0), 0);
+    ColumnTransformer caseWhen =
+        graph.caseWhen(graph.greaterThan(shared, 0), graph.constant(1), 
graph.nullValue());
+    try (Operator operator =
+        project(graph, new TsBlock[] {block(1), block(-2)}, caseWhen, 
graph.add(shared, 1))) {
+      assertBlock(operator.next(), new Double[] {1.0}, new Double[] {2.0});
+      assertBlock(operator.next(), new Double[] {null}, new Double[] {-1.0});
+    }
+  }
+
+  @Test
+  public void testFilterDoesNotReuseColumnsAcrossBlocks() throws Exception {
+    Graph filter = new Graph();
+    ColumnTransformer input = filter.input(0);
+    ColumnTransformer caseWhen =
+        filter.caseWhen(filter.greaterThan(input, 0), filter.constant(1), 
filter.constant(0));
+    ColumnTransformer predicate = filter.greaterThan(filter.add(caseWhen, 
input), 0);
+    Graph projection = new Graph();
+    try (Operator operator =
+        operator(
+            new TsBlock[] {block(1), block(-2, 3), block(-4)},
+            filter,
+            predicate,
+            Collections.emptyList(),
+            projection,
+            projection.input(0))) {
+      assertBlock(operator.next(), new Double[] {1.0});
+      assertBlock(operator.next(), new Double[] {3.0});
+      assertBlock(operator.next(), new Double[0]);
+    }
+  }
+
+  @Test
+  public void testFilterCommonExpressionsDoNotCrossBlocks() throws Exception {
+    for (boolean runLengthEncodedTime : new boolean[] {false, true}) {
+      Graph filter = new Graph();
+      ColumnTransformer shared = filter.add(filter.input(0), 0);
+      ColumnTransformer caseWhen =
+          filter.caseWhen(filter.greaterThan(shared, 0), filter.constant(1), 
filter.constant(0));
+      ColumnTransformer predicate = filter.greaterThan(caseWhen, 0);
+      Graph projection = new Graph();
+      try (Operator operator =
+          operator(
+              new TsBlock[] {
+                block(runLengthEncodedTime, 1),
+                block(runLengthEncodedTime, -2, 3),
+                block(runLengthEncodedTime, 4, -5)
+              },
+              filter,
+              predicate,
+              Collections.singletonList(shared),
+              projection,
+              projection.input(0),
+              projection.input(1))) {
+        assertBlock(operator.next(), new Double[] {1.0}, new Double[] {1.0});
+        assertBlock(operator.next(), new Double[] {3.0}, new Double[] {3.0});
+        assertBlock(operator.next(), new Double[] {4.0}, new Double[] {4.0});
+      }
+    }
+  }
+
+  @Test
+  public void testProjectionClearsCachedDescendantsOfNestedCase() throws 
Exception {
+    Graph graph = new Graph();
+    ColumnTransformer shared = graph.add(graph.input(0), 0);
+    ColumnTransformer innerCase =
+        graph.caseWhen(graph.greaterThan(shared, 0), graph.constant(1), 
graph.constant(0));
+    ColumnTransformer outerCase =
+        graph.caseWhen(graph.greaterThan(innerCase, 0), graph.constant(2), 
graph.constant(0));
+    // Simulate an intermediate cache retained from the previous input block. 
Clearing only the
+    // outer CASE itself must not allow this cached value to reach the new 
block's WHEN condition.
+    shared.initializeColumnCache(doubleColumn(1));
+    try (Operator operator = project(graph, new TsBlock[] {block(-2)}, 
outerCase)) {
+      assertBlock(operator.next(), new Double[] {0.0});
+    }
+  }
+
+  @Test
+  public void testThenExpressionIsRecomputedWithoutSelectionForLaterOutput() 
throws Exception {
+    Graph graph = new Graph();
+    ColumnTransformer input = graph.input(0);
+    ColumnTransformer shared = graph.add(input, 1);
+    ColumnTransformer caseWhen =
+        graph.caseWhen(graph.greaterThan(input, 0), shared, graph.constant(0));
+    try (Operator operator =
+        project(graph, new TsBlock[] {block(-2, 3), block(-4, 5)}, caseWhen, 
shared)) {
+      assertBlock(operator.next(), new Double[] {0.0, 4.0}, new Double[] 
{-1.0, 4.0});
+      assertBlock(operator.next(), new Double[] {0.0, 6.0}, new Double[] 
{-3.0, 6.0});
+    }
+  }
+
+  @Test
+  public void testCaseStillSkipsUnusedFailBranch() throws Exception {
+    Graph graph = new Graph();
+    ColumnTransformer caseWhen =
+        graph.caseWhen(
+            graph.greaterThan(graph.input(0), 0),
+            graph.constant(1),
+            new FailFunctionColumnTransformer(DOUBLE, "The unused ELSE branch 
was evaluated"));
+    try (Operator operator = project(graph, new TsBlock[] {block(1, 2), 
block(3)}, caseWhen)) {
+      assertBlock(operator.next(), new Double[] {1.0, 1.0});
+      assertBlock(operator.next(), new Double[] {1.0});
+    }
+  }
+
+  @Test
+  public void testProjectionReusesSharedExpressionsWithinEachBlock() throws 
Exception {
+    Graph graph = new Graph();
+    CountingIdentityColumnTransformer input = new 
CountingIdentityColumnTransformer();
+    graph.leaves.add(input);
+    ColumnTransformer shared = graph.add(input, 0);
+    try (Operator operator =
+        project(graph, new TsBlock[] {block(1), block(2)}, shared, 
graph.add(shared, 1))) {
+      assertBlock(operator.next(), new Double[] {1.0}, new Double[] {2.0});
+      assertEquals(1, input.evaluationCount);
+      assertBlock(operator.next(), new Double[] {2.0}, new Double[] {3.0});
+      assertEquals(2, input.evaluationCount);
+    }
+  }
+
+  @Test
+  public void testCopiedOperatorReusesCollectedTransformers() throws Exception 
{
+    Graph graph = new Graph();
+    CountingIdentityColumnTransformer input = new 
CountingIdentityColumnTransformer();
+    graph.leaves.add(input);
+    try (FilterAndProjectOperator original = project(graph, new TsBlock[0], 
graph.add(input, 1));
+        FilterAndProjectOperator copy =
+            new FilterAndProjectOperator(
+                original, new TsBlockSourceOperator(new TsBlock[] {block(1), 
block(2)}))) {
+      assertEquals(1, input.childTraversalCount);
+      assertBlock(copy.next(), new Double[] {2.0});
+      assertBlock(copy.next(), new Double[] {3.0});
+      assertEquals(1, input.childTraversalCount);
+    }
+  }
+
+  @Test
+  public void testProjectionCollectsSharedDagOnlyOnce() throws Exception {
+    Graph graph = new Graph();
+    CountingIdentityColumnTransformer input = new 
CountingIdentityColumnTransformer();
+    graph.leaves.add(input);
+    ColumnTransformer shared = input;
+    // Each level references the same child twice. A traversal without 
deduplication would visit
+    // the input 4096 times per root, although this DAG contains only 13 
distinct transformers.
+    for (int i = 0; i < 12; i++) {
+      shared = graph.add(shared, shared);
+    }
+    try (Operator operator =
+        project(graph, new TsBlock[] {block(1), block(2)}, shared, 
graph.add(shared, 1))) {
+      assertEquals(1, input.childTraversalCount);
+      assertBlock(operator.next(), new Double[] {4096.0}, new Double[] 
{4097.0});
+      assertEquals(1, input.childTraversalCount);
+      assertBlock(operator.next(), new Double[] {8192.0}, new Double[] 
{8193.0});
+      assertEquals(1, input.childTraversalCount);
+    }
+  }
+
+  @Test
+  public void testFilterAndCommonExpressionsCollectSharedDagOnlyOnce() throws 
Exception {
+    Graph filter = new Graph();
+    CountingIdentityColumnTransformer input = new 
CountingIdentityColumnTransformer();
+    filter.leaves.add(input);
+    ColumnTransformer shared = filter.add(input, 1);
+    ColumnTransformer predicate = filter.greaterThan(shared, 0);
+    Graph projection = new Graph();
+    try (Operator operator =
+        operator(
+            new TsBlock[] {block(1), block(-2, 3)},
+            filter,
+            predicate,
+            Collections.singletonList(shared),
+            projection,
+            projection.input(0),
+            projection.input(1))) {
+      assertEquals(1, input.childTraversalCount);
+      assertBlock(operator.next(), new Double[] {1.0}, new Double[] {2.0});
+      assertEquals(1, input.childTraversalCount);
+      assertBlock(operator.next(), new Double[] {3.0}, new Double[] {4.0});
+      assertEquals(1, input.childTraversalCount);
+    }
+  }
+
+  @Test
+  public void testMappableUdfClearsCaseDescendantsAcrossBlocks() throws 
Exception {
+    Graph graph = new Graph();
+    ColumnTransformer input = graph.input(0);
+    ColumnTransformer caseWhen =
+        graph.caseWhen(graph.greaterThan(input, 0), graph.constant(1), 
graph.constant(0));
+    ColumnTransformer udf =
+        new MappableUDFColumnTransformer(
+            DOUBLE,
+            new ColumnTransformer[] {reference(caseWhen), reference(input)},
+            new UDTFExecutor("test", ZoneId.of("UTC")) {
+              @Override
+              public void execute(Column[] columns, ColumnBuilder builder) {
+                for (int i = 0; i < columns[0].getPositionCount(); i++) {
+                  builder.writeDouble(columns[0].getDouble(i) + 
columns[1].getDouble(i));
+                }
+              }
+
+              @Override
+              public void beforeDestroy() {
+                // No UDF resources are allocated by this test executor.
+              }
+            });
+    try (Operator operator =
+        project(graph, new TsBlock[] {block(1), block(-2, 3), block(4)}, udf)) 
{
+      assertBlock(operator.next(), new Double[] {2.0});
+      assertBlock(operator.next(), new Double[] {-2.0, 4.0});
+      assertBlock(operator.next(), new Double[] {5.0});
+    }
+  }
+
+  @Test
+  public void testReusingCasePreservesPendingSiblingColumn() throws Exception {
+    Graph graph = new Graph();
+    ColumnTransformer input = graph.input(0);
+    ColumnTransformer sharedCase =
+        graph.caseWhen(graph.greaterThan(input, 0), graph.constant(1), 
graph.constant(0));
+    ColumnTransformer nestedCase =
+        graph.caseWhen(graph.greaterThan(sharedCase, 0), graph.constant(2), 
graph.constant(3));
+    // The first output leaves sharedCase cached. In the second output, input 
is evaluated before
+    // nestedCase reuses sharedCase. Its cleanup must preserve input until the 
addition reads it.
+    try (Operator operator =
+        project(
+            graph,
+            new TsBlock[] {block(-3, 0, 2), block(4, -1)},
+            sharedCase,
+            graph.add(input, nestedCase))) {
+      assertBlock(operator.next(), new Double[] {0.0, 0.0, 1.0}, new Double[] 
{0.0, 3.0, 4.0});
+      assertBlock(operator.next(), new Double[] {1.0, 0.0}, new Double[] {6.0, 
2.0});
+    }
+  }
+
+  private static FilterAndProjectOperator project(
+      Graph graph, TsBlock[] blocks, ColumnTransformer... outputs) {
+    return operator(blocks, null, null, Collections.emptyList(), graph, 
outputs);
+  }
+
+  private static FilterAndProjectOperator operator(
+      TsBlock[] blocks,
+      Graph filter,
+      ColumnTransformer predicate,
+      List<ColumnTransformer> common,
+      Graph projection,
+      ColumnTransformer... outputs) {
+    List<ColumnTransformer> projectOutputs = new ArrayList<>();
+    for (ColumnTransformer output : outputs) {
+      projectOutputs.add(reference(output));
+    }
+    for (ColumnTransformer transformer : common) {
+      reference(transformer);
+    }
+    return new FilterAndProjectOperator(
+        new TestOperatorContext(),
+        new TsBlockSourceOperator(blocks),
+        Collections.nCopies(1 + common.size(), TSDataType.DOUBLE),
+        filter == null ? Collections.emptyList() : filter.leaves,
+        predicate == null ? null : reference(predicate),
+        common,
+        projection.leaves,
+        projectOutputs,
+        false,
+        predicate != null);
+  }
+
+  private static ColumnTransformer reference(ColumnTransformer transformer) {
+    transformer.addReferenceCount();
+    return transformer;
+  }
+
+  private static DoubleColumn doubleColumn(double... values) {
+    return new DoubleColumn(values.length, Optional.empty(), values);
+  }
+
+  private static TsBlock block(double... values) {
+    return block(false, values);
+  }
+
+  private static TsBlock block(boolean runLengthEncodedTime, double... values) 
{
+    Column timeColumn =
+        runLengthEncodedTime
+            ? new RunLengthEncodedColumn(new TimeColumn(1, new long[] {0}), 
values.length)
+            : new TimeColumn(values.length, new long[values.length]);
+    return new TsBlock(timeColumn, doubleColumn(values));
+  }
+
+  private static void assertBlock(TsBlock actual, Double[]... expectedColumns) 
{
+    assertEquals(expectedColumns.length, actual.getValueColumnCount());
+    assertEquals(expectedColumns[0].length, actual.getPositionCount());
+    for (int columnIndex = 0; columnIndex < expectedColumns.length; 
columnIndex++) {
+      Column column = actual.getColumn(columnIndex);
+      Double[] expected = expectedColumns[columnIndex];
+      assertEquals(expected.length, column.getPositionCount());
+      for (int row = 0; row < expected.length; row++) {
+        if (expected[row] == null) {
+          assertTrue(column.isNull(row));
+        } else {
+          assertFalse(column.isNull(row));
+          assertEquals(expected[row], column.getDouble(row), 0.0);
+        }
+      }
+    }
+  }
+
+  private static class Graph {
+    private final List<LeafColumnTransformer> leaves = new ArrayList<>();
+
+    private ColumnTransformer input(int index) {
+      return leaf(new IdentityColumnTransformer(DOUBLE, index));
+    }
+
+    private ColumnTransformer constant(double value) {
+      return leaf(new ConstantColumnTransformer(DOUBLE, doubleColumn(value)));
+    }
+
+    private ColumnTransformer nullValue() {
+      return leaf(new NullColumnTransformer());
+    }
+
+    private ColumnTransformer leaf(LeafColumnTransformer transformer) {
+      leaves.add(transformer);
+      return transformer;
+    }
+
+    private ColumnTransformer add(ColumnTransformer input, double value) {
+      return add(input, constant(value));
+    }
+
+    private ColumnTransformer add(ColumnTransformer left, ColumnTransformer 
right) {
+      return new ArithmeticAdditionColumnTransformer(DOUBLE, reference(left), 
reference(right));
+    }
+
+    private ColumnTransformer greaterThan(ColumnTransformer input, double 
value) {
+      return new CompareGreaterThanColumnTransformer(
+          BOOLEAN, reference(input), reference(constant(value)));
+    }
+
+    private ColumnTransformer caseWhen(
+        ColumnTransformer when, ColumnTransformer then, ColumnTransformer 
otherwise) {
+      return new TableCaseWhenThenColumnTransformer(
+          DOUBLE,
+          Collections.singletonList(reference(when)),
+          Collections.singletonList(reference(then)),
+          reference(otherwise));
+    }
+  }
+
+  private static class CountingIdentityColumnTransformer extends 
IdentityColumnTransformer {
+    private int evaluationCount;
+    private int childTraversalCount;
+
+    private CountingIdentityColumnTransformer() {
+      super(DOUBLE, 0);
+    }
+
+    @Override
+    protected void evaluate() {
+      evaluationCount++;
+      super.evaluate();
+    }
+
+    @Override
+    protected void forEachChild(Consumer<ColumnTransformer> action) {
+      childTraversalCount++;
+    }
+  }
+
+  private static class TestOperatorContext extends CommonOperatorContext {
+    private TestOperatorContext() {
+      super(0, new PlanNodeId("test"), "test");
+    }
+
+    @Override
+    public MemoryReservationManager getMemoryReservationContext() {
+      return null;
+    }
+
+    @Override
+    public int getFragmentId() {
+      return 0;
+    }
+
+    @Override
+    public int getPipelineId() {
+      return 0;
+    }
+
+    @Override
+    public long ramBytesUsed() {
+      return 0;
+    }
+  }
+
+  private static class TsBlockSourceOperator implements Operator {

Review Comment:
   Replaced the test-only input operator with the production `ValuesOperator`.



##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/schema/source/DevicePredicateHandlerTest.java:
##########
@@ -0,0 +1,323 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.db.queryengine.execution.operator.schema.source;
+
+import org.apache.iotdb.calc.transformation.dag.column.ColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.TableCaseWhenThenColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.binary.CompareEqualToColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.leaf.ConstantColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.leaf.IdentityColumnTransformer;
+import 
org.apache.iotdb.calc.transformation.dag.column.leaf.LeafColumnTransformer;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.schema.node.role.IDeviceMNode;
+import org.apache.iotdb.commons.schema.table.TsTable;
+import org.apache.iotdb.commons.schema.table.column.TagColumnSchema;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
+import org.apache.iotdb.db.schemaengine.rescon.MemSchemaRegionStatistics;
+import 
org.apache.iotdb.db.schemaengine.schemaregion.mtree.impl.mem.mnode.IMemMNode;
+import 
org.apache.iotdb.db.schemaengine.schemaregion.mtree.impl.mem.mnode.info.TableDeviceInfo;
+import 
org.apache.iotdb.db.schemaengine.schemaregion.read.resp.info.impl.ShowDevicesResult;
+
+import org.apache.tsfile.common.conf.TSFileConfig;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.read.common.block.column.BinaryColumn;
+import org.apache.tsfile.read.common.block.column.BooleanColumn;
+import org.apache.tsfile.utils.Binary;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+import static org.apache.tsfile.read.common.type.BooleanType.BOOLEAN;
+import static org.apache.tsfile.read.common.type.StringType.STRING;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class DevicePredicateHandlerTest {
+
+  private static final List<TsTableColumnSchema> COLUMNS =
+      Collections.singletonList(new TagColumnSchema("tag", TSDataType.STRING));
+
+  @Test
+  public void testPredicateFilterDoesNotReuseFullBatches() throws Exception {
+    Graph graph = new Graph();
+    ColumnTransformer predicate = graph.matchesCase(graph.input(0));
+    try (DevicePredicateFilter filter = predicateFilter(graph, predicate)) {
+      int batchSize = DevicePredicateHandler.DEFAULT_MAX_TS_BLOCK_LINE_NUMBER;
+      for (String value : new String[] {"match", "other"}) {
+        for (int i = 0; i < batchSize; i++) {
+          filter.addBatch(device(value));
+        }
+        // addBatch evaluates full batches automatically; exhausting the 
iterator resets the batch.
+        List<String> result = consume(filter);
+        assertEquals(batchSize, result.size());
+        for (String actual : result) {
+          assertEquals(value, actual);
+        }
+      }
+    }
+  }
+
+  @Test
+  public void testPredicateFilterDoesNotReuseDifferentSizedBatches() throws 
Exception {
+    Graph graph = new Graph();
+    ColumnTransformer predicate = graph.matchesCase(graph.input(0));
+    try (DevicePredicateFilter filter = predicateFilter(graph, predicate)) {
+      assertEquals(Collections.singletonList("match"), filterBatch(filter, 
"match"));
+      assertEquals(Arrays.asList("other", "match"), filterBatch(filter, 
"other", "match"));
+      assertEquals(Collections.emptyList(), filterBatch(filter, "skip"));
+    }
+  }
+
+  @Test
+  public void testBlackListConstructorDoesNotReusePredicateAcrossBatches() 
throws Exception {
+    Graph graph = new Graph();
+    ColumnTransformer predicate = graph.matchesCase(graph.input(0));
+    List<IDeviceMNode<IMemMNode>> nodes =
+        Arrays.asList(node("match", 0), node("other", 1), node("match", 2));
+    try (DeviceBlackListConstructor constructor =
+        new DeviceBlackListConstructor(
+            graph.leaves,
+            reference(predicate),
+            "table",
+            COLUMNS,
+            (pointer, name) -> null,
+            mock(MemSchemaRegionStatistics.class),
+            "test",
+            new TsTable("table"))) {
+      constructor.handleDeviceNode(nodes.get(0));
+      constructor.prepareBatchResult();
+      constructor.update();
+      constructor.handleDeviceNode(nodes.get(1));
+      constructor.handleDeviceNode(nodes.get(2));
+      constructor.prepareBatchResult();
+      constructor.update();
+      assertEquals(3, constructor.getPreDeletedNum());
+      for (IDeviceMNode<IMemMNode> node : nodes) {
+        verify(node).preDeactivateSelfOrTemplate();
+      }
+    }
+  }
+
+  @Test
+  public void testAttributeUpdaterDoesNotReuseFilterAcrossBatches() throws 
Exception {
+    Graph filter = new Graph();
+    ColumnTransformer predicate = filter.matchesCase(filter.input(0));
+    Graph projection = new Graph();
+    List<List<String>> updates = new ArrayList<>();
+    try (DeviceAttributeUpdater updater =
+        attributeUpdater(
+            filter, predicate, Collections.emptyList(), projection, updates, 
projection.input(0))) {
+      updateBatch(updater, "match");
+      updateBatch(updater, "other", "match");
+      assertEquals(
+          Arrays.asList(
+              Arrays.asList("match", "match"),
+              Arrays.asList("other", "other"),
+              Arrays.asList("match", "match")),
+          updates);
+    }
+  }
+
+  @Test
+  public void testAttributeUpdaterDoesNotReuseProjectionAcrossBatches() throws 
Exception {
+    Graph projection = new Graph();
+    ColumnTransformer input = projection.input(0);
+    ColumnTransformer caseWhen = projection.caseResult(input);
+    List<List<String>> updates = new ArrayList<>();
+    try (DeviceAttributeUpdater updater =
+        attributeUpdater(
+            null, null, Collections.emptyList(), projection, updates, 
caseWhen, input)) {
+      updateBatch(updater, "match");
+      updateBatch(updater, "other", "match");
+      assertEquals(
+          Arrays.asList(
+              Arrays.asList("match", "match", "match"),
+              Arrays.asList("other", "other", "other"),
+              Arrays.asList("match", "match", "match")),
+          updates);
+    }
+  }
+
+  @Test
+  public void testAttributeUpdaterRecomputesCommonExpressionClearedByCase() 
throws Exception {
+    Graph filter = new Graph();
+    ColumnTransformer shared = filter.input(0);

Review Comment:
   Changed the common-expression fixture from a bare leaf to a shared 
`UpperColumnTransformer`.



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