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

jackietien pushed a commit to branch ty/fixLimitPushDown
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 7e6f98b5f4594351f68d971c9c60042a58206069
Author: JackieTien97 <[email protected]>
AuthorDate: Mon Nov 3 20:30:12 2025 +0800

    Fix wrong push limit down to AggNode
---
 .../planner/distribute/TableDistributedPlanGenerator.java      |  2 +-
 .../planner/iterative/rule/PushDownOffsetIntoTableScan.java    |  4 +++-
 .../plan/relational/planner/node/AggregationTableScanNode.java | 10 ++++++++++
 .../planner/optimizations/PushLimitOffsetIntoTableScan.java    |  2 +-
 4 files changed, 15 insertions(+), 3 deletions(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/distribute/TableDistributedPlanGenerator.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/distribute/TableDistributedPlanGenerator.java
index 850ef0a81e3..069d1df7469 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/distribute/TableDistributedPlanGenerator.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/distribute/TableDistributedPlanGenerator.java
@@ -423,7 +423,7 @@ public class TableDistributedPlanGenerator
   private boolean canTopKEliminated(OrderingScheme orderingScheme, long k, 
PlanNode child) {
     // if DeviceTableScanNode has limit <= K and with same order, we can 
directly return
     // DeviceTableScanNode
-    if (child instanceof DeviceTableScanNode) {
+    if (child instanceof DeviceTableScanNode && !(child instanceof 
AggregationTableScanNode)) {
       DeviceTableScanNode tableScanNode = (DeviceTableScanNode) child;
       if (canSortEliminated(orderingScheme, 
nodeOrderingMap.get(child.getPlanNodeId()))) {
         if (tableScanNode.getPushDownLimit() <= 0) {
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/iterative/rule/PushDownOffsetIntoTableScan.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/iterative/rule/PushDownOffsetIntoTableScan.java
index d683dbe3fdb..77143f90374 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/iterative/rule/PushDownOffsetIntoTableScan.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/iterative/rule/PushDownOffsetIntoTableScan.java
@@ -20,6 +20,7 @@
 package org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.rule;
 
 import org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.Rule;
+import 
org.apache.iotdb.db.queryengine.plan.relational.planner.node.AggregationTableScanNode;
 import 
org.apache.iotdb.db.queryengine.plan.relational.planner.node.DeviceTableScanNode;
 import org.apache.iotdb.db.queryengine.plan.relational.planner.node.OffsetNode;
 import 
org.apache.iotdb.db.queryengine.plan.relational.planner.node.TableScanNode;
@@ -53,7 +54,8 @@ public class PushDownOffsetIntoTableScan implements 
Rule<OffsetNode> {
   @Override
   public Result apply(OffsetNode parent, Captures captures, Context context) {
     TableScanNode tableScanNode = captures.get(CHILD);
-    if (tableScanNode instanceof DeviceTableScanNode
+    if ((tableScanNode instanceof DeviceTableScanNode
+            && !(tableScanNode instanceof AggregationTableScanNode))
         && !((DeviceTableScanNode) tableScanNode).isPushLimitToEachDevice()) {
       tableScanNode.setPushDownOffset(parent.getCount());
       // consider case that there is no limit
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/node/AggregationTableScanNode.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/node/AggregationTableScanNode.java
index b6840d7200a..56d39f2f77d 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/node/AggregationTableScanNode.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/node/AggregationTableScanNode.java
@@ -623,4 +623,14 @@ public class AggregationTableScanNode extends 
DeviceTableScanNode {
   public Map<DeviceEntry, Integer> getDeviceCountMap() {
     return deviceCountMap;
   }
+
+  @Override
+  public void setPushDownLimit(long pushDownLimit) {
+    throw new IllegalStateException("Should never push down limit to 
AggregationTableScanNode.");
+  }
+
+  @Override
+  public void setPushDownOffset(long pushDownOffset) {
+    throw new IllegalStateException("Should never push down offset to 
AggregationTableScanNode.");
+  }
 }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/PushLimitOffsetIntoTableScan.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/PushLimitOffsetIntoTableScan.java
index 2993c8f3696..2bd9b778aec 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/PushLimitOffsetIntoTableScan.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/PushLimitOffsetIntoTableScan.java
@@ -50,7 +50,7 @@ import java.util.Set;
 import static 
org.apache.iotdb.db.queryengine.plan.relational.planner.optimizations.PushPredicateIntoTableScan.containsDiffFunction;
 
 /**
- * <b>Optimization phase:</b> Distributed plan planning.
+ * <b>Optimization phase:</b> Logical plan planning.
  *
  * <p>The LIMIT OFFSET condition can be pushed down to the 
DeviceTableScanNode, when the following
  * conditions are met:

Reply via email to