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

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


The following commit(s) were added to refs/heads/DefaultOrderByTime by this 
push:
     new 9bd34e5fdc1 Opt order by time desc limit case
9bd34e5fdc1 is described below

commit 9bd34e5fdc1eb7cfa7df32df750b005df8578948
Author: JackieTien97 <[email protected]>
AuthorDate: Fri May 16 09:46:45 2025 +0800

    Opt order by time desc limit case
---
 .../distribute/TableDistributedPlanGenerator.java  | 53 +++++++++++++---------
 1 file changed, 31 insertions(+), 22 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 57bf0a79660..d99fa3da1e3 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
@@ -273,19 +273,8 @@ public class TableDistributedPlanGenerator
         node.getChildren().size() == 1, "Size of TopKNode can only be 1 in 
logical plan.");
     List<PlanNode> childrenNodes = node.getChildren().get(0).accept(this, 
context);
     if (childrenNodes.size() == 1) {
-      // if DeviceTableScanNode has limit <= K and with same order, we can 
directly return
-      // DeviceTableScanNode
-      if (childrenNodes.get(0) instanceof DeviceTableScanNode) {
-        DeviceTableScanNode tableScanNode = (DeviceTableScanNode) 
childrenNodes.get(0);
-        if (node.getCount() >= tableScanNode.getPushDownLimit()
-            && (!tableScanNode.isPushLimitToEachDevice()
-                || (tableScanNode.isPushLimitToEachDevice()
-                    && tableScanNode.getDeviceEntries().size() == 1))
-            && canSortEliminated(
-                node.getOrderingScheme(),
-                nodeOrderingMap.get(childrenNodes.get(0).getPlanNodeId()))) {
-          return childrenNodes;
-        }
+      if (canTopKEliminated(node.getOrderingScheme(), node.getCount(), 
childrenNodes.get(0))) {
+        return childrenNodes;
       }
       node.setChildren(Collections.singletonList(childrenNodes.get(0)));
       return Collections.singletonList(node);
@@ -293,21 +282,41 @@ public class TableDistributedPlanGenerator
 
     TopKNode newTopKNode = (TopKNode) node.clone();
     for (PlanNode child : childrenNodes) {
-      TopKNode subTopKNode =
-          new TopKNode(
-              queryId.genPlanNodeId(),
-              Collections.singletonList(child),
-              node.getOrderingScheme(),
-              node.getCount(),
-              node.getOutputSymbols(),
-              node.isChildrenDataInOrder());
-      newTopKNode.addChild(subTopKNode);
+      PlanNode newChild;
+      if (canTopKEliminated(node.getOrderingScheme(), node.getCount(), child)) 
{
+        newChild = child;
+      } else {
+        newChild =
+            new TopKNode(
+                queryId.genPlanNodeId(),
+                Collections.singletonList(child),
+                node.getOrderingScheme(),
+                node.getCount(),
+                node.getOutputSymbols(),
+                node.isChildrenDataInOrder());
+      }
+      newTopKNode.addChild(newChild);
     }
     nodeOrderingMap.put(newTopKNode.getPlanNodeId(), 
newTopKNode.getOrderingScheme());
 
     return Collections.singletonList(newTopKNode);
   }
 
+  // if DeviceTableScanNode has limit <= K and with same order, we can 
eliminate TopK
+  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) {
+      DeviceTableScanNode tableScanNode = (DeviceTableScanNode) child;
+      return k >= tableScanNode.getPushDownLimit()
+          && (!tableScanNode.isPushLimitToEachDevice()
+              || (tableScanNode.isPushLimitToEachDevice()
+                  && tableScanNode.getDeviceEntries().size() == 1))
+          && canSortEliminated(orderingScheme, 
nodeOrderingMap.get(child.getPlanNodeId()));
+    }
+    return false;
+  }
+
   @Override
   public List<PlanNode> visitGroup(GroupNode node, PlanContext context) {
     context.setExpectedOrderingScheme(node.getOrderingScheme());

Reply via email to