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 2023/09/12 00:55:15 UTC

[iotdb] branch rel/1.2 updated: [To rel/1.2] Fix count_time bug when count_time aggregation using with other aggregation

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

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


The following commit(s) were added to refs/heads/rel/1.2 by this push:
     new 7418c23675c [To rel/1.2] Fix count_time bug when count_time aggregation using with other aggregation
7418c23675c is described below

commit 7418c23675c3ef11684e7ecfa2923574e4097a3b
Author: Beyyes <cg...@foxmail.com>
AuthorDate: Tue Sep 12 08:55:08 2023 +0800

    [To rel/1.2] Fix count_time bug when count_time aggregation using with other aggregation
---
 .../plan/statement/crud/QueryStatement.java        | 26 +++++++++--------
 .../queryengine/plan/analyze/AnalyzeFailTest.java  | 33 ++++++++++++++--------
 2 files changed, 36 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 06458367fd4..094b27d1153 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
@@ -519,12 +519,9 @@ public class QueryStatement extends Statement {
   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.";
@@ -549,7 +546,8 @@ public class QueryStatement extends Statement {
 
         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);
@@ -766,21 +764,27 @@ public class QueryStatement extends Statement {
     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;",