Skip to content

Commit

Permalink
Support for group by time sql paser
Browse files Browse the repository at this point in the history
  • Loading branch information
JackieTien97 authored Jun 18, 2024
1 parent 1264141 commit 625269b
Show file tree
Hide file tree
Showing 14 changed files with 487 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,42 +117,39 @@ public void testManageDatabase() {
// create with strange name
try {
statement.execute("create database 1test");
fail("create database 1test shouldn't succeed because test1 doesn't exist");
fail(
"create database 1test shouldn't succeed because 1test is not a legal identifier; identifiers must not start with a digit; surround the identifier with double quotes");
} catch (SQLException e) {
assertTrue(
e.getMessage(),
e.getMessage()
.endsWith(
"identifiers must not start with a digit; surround the identifier with double quotes"));
assertTrue(e.getMessage(), e.getMessage().contains("mismatched input '1'"));
}

statement.execute("create database \"1test\"");
statement.execute("use \"1test\"");
statement.execute("drop database \"1test\"");

// try {
// statement.execute("create database 1");
// fail("create database 1test shouldn't succeed because test1 doesn't exist");
// } catch (SQLException e) {
// // TODO add error msg assert
// }
try {
statement.execute("create database 1");
fail("create database 1 shouldn't succeed because 1 is not a legal identifier");
} catch (SQLException e) {
assertTrue(e.getMessage(), e.getMessage().contains("mismatched input '1'"));
}
//
// // TODO fix it, should succeed
// statement.execute("create database \"1\"");
// statement.execute("use \"1\"");
// statement.execute("drop database \"1\"");
//
// try {
// statement.execute("create database a.b");
// fail("create database 1test shouldn't succeed because test1 doesn't exist");
// } catch (SQLException e) {
// // TODO add error msg assert
// }
try {
statement.execute("create database a.b");
fail("create database a.b shouldn't succeed because a.b is not a legal identifier");
} catch (SQLException e) {
assertTrue(e.getMessage(), e.getMessage().contains("mismatched input '.'"));
}
//
// // TODO fix it, should succeed
// statement.execute("create database \"a.b\"");
statement.execute("create database \"a.b\"");
// statement.execute("use \"a.b\"");
// statement.execute("drop database \"a.b\"");
statement.execute("drop database \"a.b\"");

} catch (SQLException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ private TSExecuteStatementResp executeStatementInternal(
req.getTimeout());
} else {
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Statement s =
relationSqlParser.createStatement(statement);
relationSqlParser.createStatement(statement, clientSession.getZoneId());

if (s == null) {
return RpcUtils.getTSExecuteStatementResp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2158,8 +2158,8 @@ public long parseDateTimeFormat(String timestampStr) {
}
}

public long parseDateTimeFormat(String timestampStr, long currentTime) {
if (timestampStr == null || "".equals(timestampStr.trim())) {
public static long parseDateTimeFormat(String timestampStr, long currentTime, ZoneId zoneId) {
if (timestampStr == null || timestampStr.trim().isEmpty()) {
throw new SemanticException("input timestamp cannot be empty");
}
if (timestampStr.equalsIgnoreCase(SqlConstant.NOW_FUNC)) {
Expand Down Expand Up @@ -3138,7 +3138,7 @@ public Long parseDateExpression(IoTDBSqlParser.DateExpressionContext ctx, String

private Long parseDateExpression(IoTDBSqlParser.DateExpressionContext ctx, long currentTime) {
long time;
time = parseDateTimeFormat(ctx.getChild(0).getText(), currentTime);
time = parseDateTimeFormat(ctx.getChild(0).getText(), currentTime, zoneId);
for (int i = 1; i < ctx.getChildCount(); i = i + 2) {
if ("+".equals(ctx.getChild(i).getText())) {
time += DateTimeUtils.convertDurationStrToLong(time, ctx.getChild(i + 1).getText(), false);
Expand All @@ -3163,7 +3163,7 @@ private long parseTimeValue(IoTDBSqlParser.TimeValueContext ctx, long currentTim
} else if (ctx.dateExpression() != null) {
return parseDateExpression(ctx.dateExpression(), currentTime);
} else {
return parseDateTimeFormat(ctx.datetimeLiteral().getText(), currentTime);
return parseDateTimeFormat(ctx.datetimeLiteral().getText(), currentTime, zoneId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static QualifiedObjectName createQualifiedObjectName(
.orElseThrow(
() ->
new SemanticException(
"Catalog must be specified when session catalog is not set"));
"Database must be specified when session database is not set"));

return new QualifiedObjectName(databaseName, objectName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ protected R visitGroupingElement(GroupingElement node, C context) {
return visitNode(node, context);
}

protected R visitGroupByTime(GroupByTime node, C context) {
return visitGroupingElement(node, context);
}

protected R visitGroupingSets(GroupingSets node, C context) {
return visitGroupingElement(node, context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.iotdb.db.queryengine.plan.relational.sql.ast;

import org.apache.tsfile.utils.TimeDuration;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class GroupByTime extends GroupingElement {

// [startTime, endTime)
private final long startTime;
private final long endTime;
// time interval
private final TimeDuration interval;
// sliding step
private final TimeDuration slidingStep;
// if it is left close and right open interval
private final boolean leftCRightO;

public GroupByTime(
long startTime,
long endTime,
TimeDuration interval,
TimeDuration slidingStep,
boolean leftCRightO) {
super(null);
this.startTime = startTime;
this.endTime = endTime;
this.interval = interval;
this.slidingStep = slidingStep;
this.leftCRightO = leftCRightO;
}

public GroupByTime(
NodeLocation location,
long startTime,
long endTime,
TimeDuration interval,
TimeDuration slidingStep,
boolean leftCRightO) {
super(location);
this.startTime = startTime;
this.endTime = endTime;
this.interval = interval;
this.slidingStep = slidingStep;
this.leftCRightO = leftCRightO;
}

@Override
public List<Expression> getExpressions() {
return Collections.emptyList();
}

@Override
protected <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitGroupByTime(this, context);
}

@Override
public List<? extends Node> getChildren() {
return Collections.emptyList();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
GroupByTime that = (GroupByTime) o;
return startTime == that.startTime
&& endTime == that.endTime
&& leftCRightO == that.leftCRightO
&& Objects.equals(interval, that.interval)
&& Objects.equals(slidingStep, that.slidingStep);
}

@Override
public int hashCode() {
return Objects.hash(startTime, endTime, interval, slidingStep, leftCRightO);
}

@Override
public String toString() {
return "GroupByTime{"
+ "startTime="
+ startTime
+ ", endTime="
+ endTime
+ ", interval="
+ interval
+ ", slidingStep="
+ slidingStep
+ ", leftCRightO="
+ leftCRightO
+ '}';
}

@Override
public boolean shallowEquals(Node other) {
return sameClass(this, other);
}

public long getStartTime() {
return startTime;
}

public long getEndTime() {
return endTime;
}

public TimeDuration getInterval() {
return interval;
}

public TimeDuration getSlidingStep() {
return slidingStep;
}

public boolean isLeftCRightO() {
return leftCRightO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected <R, C> R accept(AstVisitor<R, C> visitor, C context) {

@Override
public List<? extends Node> getChildren() {
return columns;
return getExpressions();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.iotdb.db.queryengine.plan.relational.sql.ast;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class TimeRange extends Node {

// [startTime, endTime)
private final long startTime;
private final long endTime;
// if it is left close and right open interval
private final boolean leftCRightO;

public TimeRange(long startTime, long endTime, boolean leftCRightO) {
super(null);
this.startTime = startTime;
this.endTime = endTime;
this.leftCRightO = leftCRightO;
}

public TimeRange(NodeLocation location, long startTime, long endTime, boolean leftCRightO) {
super(location);
this.startTime = startTime;
this.endTime = endTime;
this.leftCRightO = leftCRightO;
}

@Override
public List<? extends Node> getChildren() {
return Collections.emptyList();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TimeRange timeRange = (TimeRange) o;
return startTime == timeRange.startTime
&& endTime == timeRange.endTime
&& leftCRightO == timeRange.leftCRightO;
}

@Override
public int hashCode() {
return Objects.hash(startTime, endTime, leftCRightO);
}

@Override
public String toString() {
return "TimeRange{"
+ "startTime="
+ startTime
+ ", endTime="
+ endTime
+ ", leftCRightO="
+ leftCRightO
+ '}';
}

public long getStartTime() {
return startTime;
}

public long getEndTime() {
return endTime;
}

public boolean isLeftCRightO() {
return leftCRightO;
}
}
Loading

0 comments on commit 625269b

Please sign in to comment.