This is an automated email from the ASF dual-hosted git repository. ColinLeeo pushed a commit to branch fix/table-scan-offset-exhausts-limit in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit df5c6734e81834f60389fee3382e74deeba1deed Author: ColinLee <[email protected]> AuthorDate: Thu Dec 3 18:02:36 2026 +0800 Preserve exhausted scan limits during OFFSET pushdown Keep the OffsetNode above a table scan when its count reaches or exceeds the scan's finite limit. Subtracting the offset used to encode the empty remainder as zero or a negative value, which the scan treats as unbounded. IoTDBUnionTableIT.exceptDistinctWithOffsetOverLimitedScan exercises three EXCEPT DISTINCT inputs. The left input sees 1 then 2 at times 3 and 4, limits the input to the first row, then skips it. The middle input returns 3 and the last input skips its only row. On master the query returned 2. The regression requires zero rows. The IT also checks an offset greater than the limit, a partially consumed limit, and an unbounded scan. LimitOffsetPushDownTest checks that the rule leaves both scan pagination fields intact when it declines the rewrite. Run the regression with either TableSimpleIT (1C1D) or TableClusterIT (1C3D), for example mvn verify -pl integration-test -am -Pwith-integration-tests,TableSimpleIT \ -DskipUTs -DfailIfNoTests=false -Dfailsafe.failIfNoSpecifiedTests=false \ '-Dit.test=IoTDBUnionTableIT#exceptDistinctWithOffsetOverLimitedScan' --- .../it/query/recent/IoTDBUnionTableIT.java | 25 +++++++++++++++++++ .../rule/PushDownOffsetIntoTableScan.java | 16 ++++++++----- .../analyzer/LimitOffsetPushDownTest.java | 28 ++++++++++++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBUnionTableIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBUnionTableIT.java index ebe7ab506cc..2783780617e 100644 --- a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBUnionTableIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBUnionTableIT.java @@ -33,6 +33,7 @@ import org.junit.runner.RunWith; import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData; import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail; import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest; +import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTestUnordered; @RunWith(IoTDBTestRunner.class) @Category({TableLocalStandaloneIT.class, TableClusterIT.class}) @@ -274,6 +275,30 @@ public class IoTDBUnionTableIT { EXCEPT_DATABASE_NAME); } + @Test + public void exceptDistinctWithOffsetOverLimitedScan() { + // At times 3 and 4, table1 contains 1 and 2. LIMIT 1 selects 1 and OFFSET 1 skips it. + assertExceptAfterOffset("select s1 from table1 where time >= 3 order by time limit 1", 1); + // Three source rows are available, but OFFSET 2 must still exhaust the one-row subquery. + assertExceptAfterOffset("select s1 from table1 where time >= 2 order by time limit 1", 2); + // The first two rows both contain 1, so skipping one must still leave 1 in the difference. + assertExceptAfterOffset("select s1 from table1 where time <= 2 order by time limit 2", 1, "1,"); + assertExceptAfterOffset("select s1 from table1 where time <= 2 order by time", 1, "1,"); + } + + private static void assertExceptAfterOffset(String orderedInput, int offset, String... expected) { + // The middle branch contributes 3 and the last branch skips its only row. + String sql = + "(select s1 from (" + + orderedInput + + ") q offset " + + offset + + ")" + + " except distinct (select s1 from table2 where time = 2)" + + " except distinct (select s1 from (select s1 from table1 order by time limit 1) q offset 1)"; + tableResultSetEqualTestUnordered(sql, new String[] {"s1"}, expected, EXCEPT_DATABASE_NAME); + } + // Checks EXCEPT aliases preserve mapping for distinct and duplicate-preserving forms. @Test public void exceptMappingTest() { 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 437e02a9672..81427e3471a 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 @@ -59,12 +59,16 @@ public class PushDownOffsetIntoTableScan implements Rule<OffsetNode> { || ((tableScanNode instanceof DeviceTableScanNode && !(tableScanNode instanceof AggregationTableScanNode)) && !((DeviceTableScanNode) tableScanNode).isPushLimitToEachDevice())) { - tableScanNode.setPushDownOffset(parent.getCount()); - // consider case that there is no limit - tableScanNode.setPushDownLimit( - tableScanNode.getPushDownLimit() == 0 - ? 0 - : tableScanNode.getPushDownLimit() - parent.getCount()); + long limit = tableScanNode.getPushDownLimit(); + long offset = parent.getCount(); + if (limit > 0) { + // Zero means unlimited in the scan, so keep the OffsetNode when no rows would remain. + if (offset >= limit) { + return Result.empty(); + } + tableScanNode.setPushDownLimit(limit - offset); + } + tableScanNode.setPushDownOffset(offset); return Result.ofPlanNode(tableScanNode); } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/LimitOffsetPushDownTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/LimitOffsetPushDownTest.java index 470cd83e3d1..d8d6df67e58 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/LimitOffsetPushDownTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/LimitOffsetPushDownTest.java @@ -19,12 +19,16 @@ package org.apache.iotdb.db.queryengine.plan.relational.analyzer; +import org.apache.iotdb.calc.plan.relational.utils.matching.Captures; import org.apache.iotdb.commons.conf.IoTDBConstant; import org.apache.iotdb.commons.queryengine.common.SessionInfo; import org.apache.iotdb.commons.queryengine.common.SqlDialect; import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode; +import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeId; +import org.apache.iotdb.commons.queryengine.plan.relational.metadata.QualifiedObjectName; import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.CollectNode; import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.LimitNode; +import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.OffsetNode; import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.ProjectNode; import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.StreamSortNode; import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.TopKNode; @@ -37,6 +41,8 @@ import org.apache.iotdb.db.queryengine.plan.relational.planner.PlanTester; import org.apache.iotdb.db.queryengine.plan.relational.planner.SymbolAllocator; import org.apache.iotdb.db.queryengine.plan.relational.planner.TableLogicalPlanner; import org.apache.iotdb.db.queryengine.plan.relational.planner.distribute.TableDistributedPlanner; +import org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.Lookup; +import org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.rule.PushDownOffsetIntoTableScan; import org.apache.iotdb.db.queryengine.plan.relational.planner.node.DeviceTableScanNode; import org.apache.iotdb.db.queryengine.plan.relational.planner.node.ExchangeNode; @@ -44,6 +50,7 @@ import org.junit.BeforeClass; import org.junit.Test; import java.time.ZoneId; +import java.util.Collections; import static org.apache.iotdb.db.queryengine.plan.relational.analyzer.AnalyzerTest.analyzeSQL; import static org.apache.iotdb.db.queryengine.plan.relational.analyzer.MockTableModelDataPartition.DEVICES_REGION_GROUP; @@ -323,4 +330,25 @@ public class LimitOffsetPushDownTest { // the offset node has been push down into TableScanNode assertPlan(planTester.getFragmentPlan(0), output(tableScan("testdb.table1"))); } + + @Test + public void keepOffsetWhenItExhaustsScanLimit() { + PushDownOffsetIntoTableScan rule = new PushDownOffsetIntoTableScan(); + DeviceTableScanNode scan = + new DeviceTableScanNode( + new PlanNodeId("scan"), + new QualifiedObjectName("db", "table1"), + Collections.emptyList(), + Collections.emptyMap(), + Collections.emptyMap()); + scan.setPushDownLimit(1); + for (long offset : new long[] {1, 2}) { + OffsetNode parent = new OffsetNode(new PlanNodeId("offset"), scan, offset); + Captures captures = + rule.getPattern().match(parent, Lookup.noLookup()).findFirst().get().captures(); + assertTrue(rule.apply(parent, captures, null).isEmpty()); + assertEquals(1, scan.getPushDownLimit()); + assertEquals(0, scan.getPushDownOffset()); + } + } }
