Skip to content

Commit

Permalink
Support OFFSET XXX LIMIT XXXX in relational model
Browse files Browse the repository at this point in the history
  • Loading branch information
JackieTien97 authored Sep 19, 2024
1 parent 6cca2e1 commit e5e4980
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,55 @@ public void nullSelectTest() {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,8 @@ public Node visitShowDevicesStatement(final RelationalSqlParser.ShowDevicesState
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
Expand Down Expand Up @@ -797,16 +797,16 @@ public Node visitQueryNoWith(RelationalSqlParser.QueryNoWithContext ctx) {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ loadTsFileStatement
showDevicesStatement
: SHOW DEVICES FROM tableName=qualifiedName
(WHERE where=booleanExpression)?
(OFFSET offset=rowCount (ROW | ROWS)?)?
(LIMIT limit=limitRowCount)?
limitOffsetClause
;

countDevicesStatement
Expand Down Expand Up @@ -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
;


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e5e4980

Please sign in to comment.