You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ca...@apache.org on 2023/09/11 03:44:33 UTC

[iotdb] 01/02: fix count_time bug

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

caogaofei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 7c760f29d008756fe3e3d5952f44777c1c77b1bc
Author: Beyyes <cg...@foxmail.com>
AuthorDate: Sat Sep 9 23:40:20 2023 +0800

    fix count_time bug
---
 .../plan/statement/crud/QueryStatement.java        | 25 ++++++++--------
 .../queryengine/plan/analyze/AnalyzeFailTest.java  | 33 ++++++++++++++--------
 2 files changed, 35 insertions(+), 23 deletions(-)

diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/crud/QueryStatement.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/crud/QueryStatement.java
index 244d992cc36..8d0ddb0d1f2 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/crud/QueryStatement.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/crud/QueryStatement.java
@@ -542,12 +542,8 @@ public class QueryStatement extends AuthorityInformationStatement {
   public static final String COUNT_TIME_NOT_SUPPORT_GROUP_BY_TAG =
       "Count_time aggregation function using with group by tag is not supported.";
 
-  public static final String COUNT_TIME_NOT_SUPPORT_USED_WITH_OTHER_OPERATION =
-      "Count_time aggregation function used with arithmetic operation "
-          + "or other aggregation is not supported.";
-
-  public static final String COUNT_TIME_CAN_ONLY_EXIST_ONE_IN_SELECT =
-      "Count_time aggregation function can only exist one in select clause.";
+  public static final String COUNT_TIME_CAN_ONLY_EXIST_ALONE =
+      "Count_time aggregation can only exist alone, and cannot used with other queries or aggregations.";
 
   public static final String COUNT_TIME_NOT_SUPPORT_USE_WITH_HAVING =
       "Count_time aggregation function can not be used with having clause.";
@@ -617,7 +613,8 @@ public class QueryStatement extends AuthorityInformationStatement {
 
         String expressionString = resultColumn.getExpression().getExpressionString();
         if (expressionString.toLowerCase().contains(COUNT_TIME)) {
-          checkCountTimeValidationInSelect(resultColumn.getExpression(), outputColumn);
+          checkCountTimeValidationInSelect(
+              resultColumn.getExpression(), outputColumn, selectComponent.getResultColumns());
         }
         outputColumn.add(
             resultColumn.getAlias() != null ? resultColumn.getAlias() : expressionString);
@@ -834,21 +831,27 @@ public class QueryStatement extends AuthorityInformationStatement {
     return visitor.visitQuery(this, context);
   }
 
-  private void checkCountTimeValidationInSelect(Expression expression, Set<String> outputColumn) {
+  private void checkCountTimeValidationInSelect(
+      Expression expression, Set<String> outputColumn, List<ResultColumn> resultColumns) {
     int countTimeAggSize = new CountTimeAggregationAmountVisitor().process(expression, null).size();
 
     if (countTimeAggSize > 1) {
       // e.g. select count_time(*) + count_time(*) from root.**
-      throw new SemanticException(COUNT_TIME_CAN_ONLY_EXIST_ONE_IN_SELECT);
+      throw new SemanticException(COUNT_TIME_CAN_ONLY_EXIST_ALONE);
     } else if (countTimeAggSize == 1) {
       // e.g. select count_time(*) / 2; select sum(*) / count_time(*)
       if (!(expression instanceof FunctionExpression)) {
-        throw new SemanticException(COUNT_TIME_NOT_SUPPORT_USED_WITH_OTHER_OPERATION);
+        throw new SemanticException(COUNT_TIME_CAN_ONLY_EXIST_ALONE);
       }
 
       // e.g. select count(*), count(*) from root.db.**
       if (!outputColumn.isEmpty()) {
-        throw new SemanticException(COUNT_TIME_CAN_ONLY_EXIST_ONE_IN_SELECT);
+        throw new SemanticException(COUNT_TIME_CAN_ONLY_EXIST_ALONE);
+      }
+
+      // e.g. select count_time(*), count(*)
+      if (resultColumns.size() > 1) {
+        throw new SemanticException(COUNT_TIME_CAN_ONLY_EXIST_ALONE);
       }
 
       if (isGroupByTag()) {
diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeFailTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeFailTest.java
index 9fd663e3c65..fdcc12645e9 100644
--- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeFailTest.java
+++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeFailTest.java
@@ -36,11 +36,11 @@ import static org.apache.iotdb.db.queryengine.plan.statement.component.IntoCompo
 import static org.apache.iotdb.db.queryengine.plan.statement.component.IntoComponent.FORBID_PLACEHOLDER_ERROR_MSG;
 import static org.apache.iotdb.db.queryengine.plan.statement.component.IntoComponent.PATH_NUM_MISMATCH_ERROR_MSG;
 import static org.apache.iotdb.db.queryengine.plan.statement.component.IntoComponent.PLACEHOLDER_MISMATCH_ERROR_MSG;
-import static org.apache.iotdb.db.queryengine.plan.statement.crud.QueryStatement.COUNT_TIME_CAN_ONLY_EXIST_ONE_IN_SELECT;
+import static org.apache.iotdb.db.queryengine.plan.statement.crud.QueryStatement.COUNT_TIME_CAN_ONLY_EXIST_ALONE;
 import static org.apache.iotdb.db.queryengine.plan.statement.crud.QueryStatement.COUNT_TIME_NOT_SUPPORT_GROUP_BY_LEVEL;
 import static org.apache.iotdb.db.queryengine.plan.statement.crud.QueryStatement.COUNT_TIME_NOT_SUPPORT_GROUP_BY_TAG;
-import static org.apache.iotdb.db.queryengine.plan.statement.crud.QueryStatement.COUNT_TIME_NOT_SUPPORT_USED_WITH_OTHER_OPERATION;
 import static org.apache.iotdb.db.queryengine.plan.statement.crud.QueryStatement.COUNT_TIME_NOT_SUPPORT_USE_WITH_HAVING;
+import static org.apache.iotdb.db.queryengine.plan.statement.crud.QueryStatement.RAW_AGGREGATION_HYBRID_QUERY_ERROR_MSG;
 import static org.junit.Assert.fail;
 
 public class AnalyzeFailTest {
@@ -182,32 +182,41 @@ public class AnalyzeFailTest {
         "select count_time(* + *) from root.sg.*;", COUNT_TIME_ONLY_SUPPORT_ONE_WILDCARD);
 
     assertAnalyzeSemanticException(
-        "select count_time(*) / 2 from root.sg.*;",
-        COUNT_TIME_NOT_SUPPORT_USED_WITH_OTHER_OPERATION);
+        "select count_time(*) / 2 from root.sg.*;", COUNT_TIME_CAN_ONLY_EXIST_ALONE);
 
     assertAnalyzeSemanticException(
-        "select sum(s1) / count_time(*) from root.sg.*;",
-        COUNT_TIME_NOT_SUPPORT_USED_WITH_OTHER_OPERATION);
+        "select sum(s1) / count_time(*) from root.sg.*;", COUNT_TIME_CAN_ONLY_EXIST_ALONE);
 
     assertAnalyzeSemanticException(
-        "select count_time(*),count_time(*) from root.sg.**;",
-        COUNT_TIME_CAN_ONLY_EXIST_ONE_IN_SELECT);
+        "select count_time(*), count(*) from root.sg.*;", COUNT_TIME_CAN_ONLY_EXIST_ALONE);
+
+    assertAnalyzeSemanticException(
+        "select count(*), count_time(*) from root.sg.*;", COUNT_TIME_CAN_ONLY_EXIST_ALONE);
+
+    assertAnalyzeSemanticException(
+        "select sum(*), count_time(*), sum(*) from root.sg.*;", COUNT_TIME_CAN_ONLY_EXIST_ALONE);
+
+    assertAnalyzeSemanticException(
+        "select s1, count_time(*), sum(*) from root.sg.*;", RAW_AGGREGATION_HYBRID_QUERY_ERROR_MSG);
+
+    assertAnalyzeSemanticException(
+        "select count_time(*),count_time(*) from root.sg.**;", COUNT_TIME_CAN_ONLY_EXIST_ALONE);
 
     assertAnalyzeSemanticException(
         "select count_time(*),count_time(*) from root.sg.d1,root.sg.d2;",
-        COUNT_TIME_CAN_ONLY_EXIST_ONE_IN_SELECT);
+        COUNT_TIME_CAN_ONLY_EXIST_ALONE);
 
     assertAnalyzeSemanticException(
         "select COUNT_TIME(*),COUNT_TIME(*) from root.sg.d1,root.sg.d2;",
-        COUNT_TIME_CAN_ONLY_EXIST_ONE_IN_SELECT);
+        COUNT_TIME_CAN_ONLY_EXIST_ALONE);
 
     assertAnalyzeSemanticException(
         "select COUNT_TIME(*),count_time(*) from root.sg.d1,root.sg.d2;",
-        COUNT_TIME_CAN_ONLY_EXIST_ONE_IN_SELECT);
+        COUNT_TIME_CAN_ONLY_EXIST_ALONE);
 
     assertAnalyzeSemanticException(
         "select COUNT_TIME(*),COUNT_time(*) from root.sg.d1,root.sg.d2;",
-        COUNT_TIME_CAN_ONLY_EXIST_ONE_IN_SELECT);
+        COUNT_TIME_CAN_ONLY_EXIST_ALONE);
 
     assertAnalyzeSemanticException(
         "select count_time(*) from root.sg.* having count(*) > 1;",