You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2024/09/19 10:18:11 UTC

(iotdb) branch ty/LimitOffset created (now b9d621be8dc)

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

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


      at b9d621be8dc Support OFFSET XXX LIMIT XXXX in relational model

This branch includes the following new commits:

     new b9d621be8dc Support OFFSET XXX LIMIT XXXX in relational model

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(iotdb) 01/01: Support OFFSET XXX LIMIT XXXX in relational model

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit b9d621be8dcf0854857cc84c3a9a53448df94917
Author: JackieTien97 <ja...@gmail.com>
AuthorDate: Thu Sep 19 18:17:56 2024 +0800

    Support OFFSET XXX LIMIT XXXX in relational model
---
 .../it/query/recent/IoTDBNullIdQueryIT.java        | 51 ++++++++++++++++++++++
 .../plan/relational/sql/parser/AstBuilder.java     | 14 +++---
 .../db/relational/grammar/sql/RelationalSql.g4     | 15 ++++---
 3 files changed, 67 insertions(+), 13 deletions(-)

diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBNullIdQueryIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBNullIdQueryIT.java
index 7c76e2fac66..d3565d40302 100644
--- a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBNullIdQueryIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBNullIdQueryIT.java
@@ -309,4 +309,55 @@ public class IoTDBNullIdQueryIT {
         retArray,
         DATABASE_NAME);
   }
+
+  @Test
+  public void limitOffsetTest() {
+    String[] expectedHeader = new String[] {"time", "device_id", "s2", "s3"};
+    String[] retArray =
+        new String[] {
+          "1970-01-01T00:00:00.003Z,d1,null,null,",
+        };
+    tableResultSetEqualTest(
+        "select time, device_id, s2, s3 from table1 OFFSET 2 limit 1",
+        expectedHeader,
+        retArray,
+        DATABASE_NAME);
+
+    expectedHeader = new String[] {"time", "device_id", "s2", "s3"};
+    retArray =
+        new String[] {
+          "1970-01-01T00:00:00.003Z,d1,null,null,",
+        };
+    tableResultSetEqualTest(
+        "select time, device_id, s2, s3 from table1 limit 1 OFFSET 2",
+        expectedHeader,
+        retArray,
+        DATABASE_NAME);
+
+    expectedHeader = new String[] {"time", "device_id", "s2", "s3"};
+    retArray =
+        new String[] {
+          "1970-01-01T00:00:00.003Z,d1,null,null,",
+          "1970-01-01T00:00:00.004Z,d1,true,44,",
+          "1970-01-01T00:00:00.005Z,d1,null,null,",
+          "1970-01-01T00:00:00.006Z,d1,null,null,",
+          "1970-01-01T00:00:00.007Z,d1,false,77,"
+        };
+    tableResultSetEqualTest(
+        "select time, device_id, s2, s3 from table1 OFFSET 2",
+        expectedHeader,
+        retArray,
+        DATABASE_NAME);
+
+    expectedHeader = new String[] {"time", "device_id", "s2", "s3"};
+    retArray =
+        new String[] {
+          "1970-01-01T00:00:00.001Z,d1,false,11,", "1970-01-01T00:00:00.002Z,d1,null,null,",
+        };
+    tableResultSetEqualTest(
+        "select time, device_id, s2, s3 from table1 limit 2",
+        expectedHeader,
+        retArray,
+        DATABASE_NAME);
+  }
 }
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
index 52792876c31..7df892dbeda 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
@@ -553,8 +553,8 @@ public class AstBuilder extends RelationalSqlBaseVisitor<Node> {
         getLocation(ctx),
         new Table(getLocation(ctx), getQualifiedName(ctx.qualifiedName())),
         visitIfPresent(ctx.where, Expression.class).orElse(null),
-        visitIfPresent(ctx.offset, Offset.class).orElse(null),
-        visitIfPresent(ctx.limit, Node.class).orElse(null));
+        visitIfPresent(ctx.limitOffsetClause().offset, Offset.class).orElse(null),
+        visitIfPresent(ctx.limitOffsetClause().limit, Node.class).orElse(null));
   }
 
   @Override
@@ -797,16 +797,16 @@ public class AstBuilder extends RelationalSqlBaseVisitor<Node> {
     }
 
     Optional<Offset> offset = Optional.empty();
-    if (ctx.OFFSET() != null) {
-      offset = visitIfPresent(ctx.offset, Offset.class);
+    if (ctx.limitOffsetClause().OFFSET() != null) {
+      offset = visitIfPresent(ctx.limitOffsetClause().offset, Offset.class);
     }
 
     Optional<Node> limit = Optional.empty();
-    if (ctx.LIMIT() != null) {
-      if (ctx.limit == null) {
+    if (ctx.limitOffsetClause().LIMIT() != null) {
+      if (ctx.limitOffsetClause().limit == null) {
         throw new IllegalStateException("Missing LIMIT value");
       }
-      limit = visitIfPresent(ctx.limit, Node.class);
+      limit = visitIfPresent(ctx.limitOffsetClause().limit, Node.class);
     }
 
     if (term instanceof QuerySpecification) {
diff --git a/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4 b/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4
index 8866fd6c65c..bfcdaf96fb1 100644
--- a/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4
+++ b/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4
@@ -237,8 +237,7 @@ loadTsFileStatement
 showDevicesStatement
     : SHOW DEVICES FROM tableName=qualifiedName
         (WHERE where=booleanExpression)?
-        (OFFSET offset=rowCount (ROW | ROWS)?)?
-        (LIMIT limit=limitRowCount)?
+        limitOffsetClause
     ;
 
 countDevicesStatement
@@ -322,8 +321,7 @@ showQueriesStatement
     : SHOW (QUERIES | QUERY PROCESSLIST)
         (WHERE where=booleanExpression)?
         (ORDER BY sortItem (',' sortItem)*)?
-        (OFFSET offset=rowCount (ROW | ROWS)?)?
-        (LIMIT limit=limitRowCount)?
+        limitOffsetClause
     ;
 
 
@@ -383,10 +381,15 @@ queryNoWith
     : queryTerm
       (ORDER BY sortItem (',' sortItem)*)?
       (FILL '(' (LINEAR | PREVIOUS | literalExpression) (',' duration=timeDuration)? ')')?
-      (OFFSET offset=rowCount)?
-      (LIMIT limit=limitRowCount)?
+      limitOffsetClause
     ;
 
+limitOffsetClause
+    : (OFFSET offset=rowCount)? (LIMIT limit=limitRowCount)?
+    | (LIMIT limit=limitRowCount)? (OFFSET offset=rowCount)?
+    ;
+
+
 limitRowCount
     : ALL
     | rowCount