You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2019/10/24 06:16:54 UTC

[GitHub] [incubator-iotdb] jixuan1989 commented on a change in pull request #462: [IOTDB-253]time expression

jixuan1989 commented on a change in pull request #462: [IOTDB-253]time expression
URL: https://github.com/apache/incubator-iotdb/pull/462#discussion_r338396873
 
 

 ##########
 File path: server/src/main/java/org/apache/iotdb/db/qp/strategy/LogicalGenerator.java
 ##########
 @@ -800,60 +815,87 @@ private TSDataType parseTypeNode(AstNode typeNode) throws LogicalOperatorExcepti
   }
 
   private long parseTimeUnit(AstNode node) throws LogicalOperatorException {
-    long timeInterval = Long.parseLong(node.getChild(0).getText());
+    long timeInterval = parseTokenDuration(node);
     if (timeInterval <= 0) {
       throw new LogicalOperatorException("Interval must more than 0.");
     }
-    String granu = node.getChild(1).getText();
-    switch (granu) {
-      case "w":
-        timeInterval *= 7;
-      case "d":
-        timeInterval *= 24;
-      case "h":
-        timeInterval *= 60;
-      case "m":
-        timeInterval *= 60;
-      case "s":
-        timeInterval *= 1000;
-      default:
-        break;
-    }
     return timeInterval;
   }
 
   private Pair<Path, String> parseLeafNode(AstNode node) throws LogicalOperatorException {
     if (node.getChildCount() != 2) {
       throw new LogicalOperatorException(
-              "error format in SQL statement, please check whether SQL statement is correct.");
+          "error format in SQL statement, please check whether SQL statement is correct.");
     }
     AstNode col = node.getChild(0);
     if (col.getType() != TqlParser.TOK_PATH) {
       throw new LogicalOperatorException(
-              "error format in SQL statement, please check whether SQL statement is correct.");
+          "error format in SQL statement, please check whether SQL statement is correct.");
     }
     Path seriesPath = parsePath(col);
     AstNode rightKey = node.getChild(1);
     String seriesValue;
-    if(rightKey.getChild(0).getType() == TqlParser.TOK_DATETIME){
+    if (rightKey.getChild(0).getType() == TqlParser.TOK_DATETIME) {
       if (!seriesPath.equals(SQLConstant.RESERVED_TIME)) {
         throw new LogicalOperatorException("Date can only be used to time");
       }
-      seriesValue = parseTokenTime(rightKey.getChild(0));
-    }else{
+      seriesValue = parseTokenTime(rightKey.getChild(0)) + "";
+    } else if (rightKey.getType() == TqlParser.TOK_DATE_EXPR) {
+      seriesValue = parseTokenDataExpresssion(rightKey.getChild(0)) + "";
+    } else {
       seriesValue = cascadeChildrenText(rightKey);
     }
     return new Pair<>(seriesPath, seriesValue);
   }
 
-  private String parseTokenTime(AstNode astNode) throws LogicalOperatorException {
-    return parseTimeFormat(cascadeChildrenText(astNode)) + "";
+  private Long parseTokenDataExpresssion(AstNode astNode) throws LogicalOperatorException {
+    if (astNode.getType() == TqlParser.PLUS) {
+      return parseTokenDataExpresssion(astNode.getChild(0)) + parseTokenDataExpresssion(
+          astNode.getChild(1));
+    } else if (astNode.getType() == TqlParser.MINUS) {
+      return parseTokenDataExpresssion(astNode.getChild(0)) - parseTokenDataExpresssion(
+          astNode.getChild(1));
+    } else {
+      return parseTokenTime(astNode);
+    }
+  }
+
+  private Long parseTokenTime(AstNode astNode) throws LogicalOperatorException {
+    if (astNode.getType() == TqlParser.TOK_DURATION) {
+      return parseTokenDuration(astNode);
+    }
+    return parseTimeFormat(cascadeChildrenText((astNode)));
+  }
+
+  private Long parseTokenDuration(AstNode astNode) {
+    String durationStr = cascadeChildrenText(astNode);
+    String timestampPrecision = IoTDBDescriptor.getInstance().getConfig().getTimestampPrecision();
+
+    long total = 0;
+    long tmp = 0;
+    for (int i = 0; i < durationStr.length(); i++) {
+      char ch = durationStr.charAt(i);
+      if (Character.isDigit(ch)) {
+        tmp *= 10;
+        tmp += (ch - '0');
+      } else {
+        String unit = durationStr.charAt(i) + "";
+        if (i + 1 < durationStr.length() && !Character.isDigit(durationStr.charAt(i + 1))) {
+          i++;
+          unit += durationStr.charAt(i);
 
 Review comment:
   e.. is this for recognize "mo"? 
   
   why not put `DurationUnit durationUnit = DurationUnit.valueOf(unit);` here?
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services