Skip to content

Commit 8420bea

Browse files
authored
Fix wrong push limit down to AggTableScanNode (apache#16696)
1 parent 2488001 commit 8420bea

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBTableAggregationIT.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5491,4 +5491,17 @@ public void emptyTimeRangeQueryTest() {
54915491
retArray,
54925492
DATABASE_NAME);
54935493
}
5494+
5495+
@Test
5496+
public void orderByLimitTest() {
5497+
String[] expectedHeader =
5498+
new String[] {"province", "city", "region", "device_id", "_col4", "_col5"};
5499+
String[] retArray = new String[] {"beijing,beijing,chaoyang,d09,2024-09-24T06:00:00.000Z,2,"};
5500+
5501+
tableResultSetEqualTest(
5502+
"select province, city, region, device_id, date_bin(1h, time), count(s1) from table1 where s1 >= 40 group by 1,2,3,4,5 order by province, city, region, device_id, date_bin(1h, time) limit 1",
5503+
expectedHeader,
5504+
retArray,
5505+
DATABASE_NAME);
5506+
}
54945507
}

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/distribute/TableDistributedPlanGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ public List<PlanNode> visitTopK(TopKNode node, PlanContext context) {
423423
private boolean canTopKEliminated(OrderingScheme orderingScheme, long k, PlanNode child) {
424424
// if DeviceTableScanNode has limit <= K and with same order, we can directly return
425425
// DeviceTableScanNode
426-
if (child instanceof DeviceTableScanNode) {
426+
if (child instanceof DeviceTableScanNode && !(child instanceof AggregationTableScanNode)) {
427427
DeviceTableScanNode tableScanNode = (DeviceTableScanNode) child;
428428
if (canSortEliminated(orderingScheme, nodeOrderingMap.get(child.getPlanNodeId()))) {
429429
if (tableScanNode.getPushDownLimit() <= 0) {

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/iterative/rule/PushDownOffsetIntoTableScan.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.rule;
2121

2222
import org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.Rule;
23+
import org.apache.iotdb.db.queryengine.plan.relational.planner.node.AggregationTableScanNode;
2324
import org.apache.iotdb.db.queryengine.plan.relational.planner.node.DeviceTableScanNode;
2425
import org.apache.iotdb.db.queryengine.plan.relational.planner.node.OffsetNode;
2526
import org.apache.iotdb.db.queryengine.plan.relational.planner.node.TableScanNode;
@@ -53,7 +54,8 @@ public Pattern<OffsetNode> getPattern() {
5354
@Override
5455
public Result apply(OffsetNode parent, Captures captures, Context context) {
5556
TableScanNode tableScanNode = captures.get(CHILD);
56-
if (tableScanNode instanceof DeviceTableScanNode
57+
if ((tableScanNode instanceof DeviceTableScanNode
58+
&& !(tableScanNode instanceof AggregationTableScanNode))
5759
&& !((DeviceTableScanNode) tableScanNode).isPushLimitToEachDevice()) {
5860
tableScanNode.setPushDownOffset(parent.getCount());
5961
// consider case that there is no limit

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/node/AggregationTableScanNode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,4 +623,14 @@ public void setDeviceCountMap(Map<DeviceEntry, Integer> deviceCountMap) {
623623
public Map<DeviceEntry, Integer> getDeviceCountMap() {
624624
return deviceCountMap;
625625
}
626+
627+
@Override
628+
public void setPushDownLimit(long pushDownLimit) {
629+
throw new IllegalStateException("Should never push down limit to AggregationTableScanNode.");
630+
}
631+
632+
@Override
633+
public void setPushDownOffset(long pushDownOffset) {
634+
throw new IllegalStateException("Should never push down offset to AggregationTableScanNode.");
635+
}
626636
}

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/PushLimitOffsetIntoTableScan.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import static org.apache.iotdb.db.queryengine.plan.relational.planner.optimizations.PushPredicateIntoTableScan.containsDiffFunction;
5151

5252
/**
53-
* <b>Optimization phase:</b> Distributed plan planning.
53+
* <b>Optimization phase:</b> Logical plan planning.
5454
*
5555
* <p>The LIMIT OFFSET condition can be pushed down to the DeviceTableScanNode, when the following
5656
* conditions are met:

0 commit comments

Comments
 (0)