You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by hu...@apache.org on 2022/12/08 08:03:37 UTC

[iotdb] 01/01: add output type check for WHERE & HAVING clause

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

hui pushed a commit to branch lmh/fixWhere
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit b4e28c676ce79d2f480e19e0ffc77f4c18163f66
Author: Minghui Liu <li...@foxmail.com>
AuthorDate: Thu Dec 8 16:03:14 2022 +0800

    add output type check for WHERE & HAVING clause
---
 .../iotdb/db/mpp/plan/analyze/AnalyzeVisitor.java  | 38 ++++++++++++++++++----
 1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/AnalyzeVisitor.java b/server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/AnalyzeVisitor.java
index aff917599b..81910ef650 100644
--- a/server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/AnalyzeVisitor.java
+++ b/server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/AnalyzeVisitor.java
@@ -569,8 +569,13 @@ public class AnalyzeVisitor extends StatementVisitor<Analysis, MPPQueryContext>
     Expression havingExpression =
         ExpressionUtils.constructQueryFilter(
             conJunctions.stream().distinct().collect(Collectors.toList()));
-    analyzeExpression(analysis, havingExpression);
-
+    TSDataType outputType = analyzeExpression(analysis, havingExpression);
+    if (outputType != TSDataType.BOOLEAN) {
+      throw new SemanticException(
+          String.format(
+              "The output type of the expression in HAVING clause should be BOOLEAN, actual data type: %s.",
+              outputType));
+    }
     analysis.setHavingExpression(havingExpression);
   }
 
@@ -610,7 +615,13 @@ public class AnalyzeVisitor extends StatementVisitor<Analysis, MPPQueryContext>
     }
 
     havingExpression = ExpressionUtils.constructQueryFilter(new ArrayList<>(conJunctions));
-    analyzeExpression(analysis, havingExpression);
+    TSDataType outputType = analyzeExpression(analysis, havingExpression);
+    if (outputType != TSDataType.BOOLEAN) {
+      throw new SemanticException(
+          String.format(
+              "The output type of the expression in HAVING clause should be BOOLEAN, actual data type: %s.",
+              outputType));
+    }
     analysis.setHavingExpression(havingExpression);
   }
 
@@ -962,8 +973,16 @@ public class AnalyzeVisitor extends StatementVisitor<Analysis, MPPQueryContext>
         }
         throw e;
       }
+
+      TSDataType outputType = analyzeExpression(analysis, whereExpression);
+      if (outputType != TSDataType.BOOLEAN) {
+        throw new SemanticException(
+            String.format(
+                "The output type of the expression in WHERE clause should be BOOLEAN, actual data type: %s.",
+                outputType));
+      }
+
       deviceToWhereExpression.put(devicePath.getFullPath(), whereExpression);
-      analyzeExpression(analysis, whereExpression);
     }
     analysis.setDeviceToWhereExpression(deviceToWhereExpression);
   }
@@ -982,7 +1001,13 @@ public class AnalyzeVisitor extends StatementVisitor<Analysis, MPPQueryContext>
     Expression whereExpression =
         ExpressionUtils.constructQueryFilter(
             conJunctions.stream().distinct().collect(Collectors.toList()));
-    analyzeExpression(analysis, whereExpression);
+    TSDataType outputType = analyzeExpression(analysis, whereExpression);
+    if (outputType != TSDataType.BOOLEAN) {
+      throw new SemanticException(
+          String.format(
+              "The output type of the expression in WHERE clause should be BOOLEAN, actual data type: %s.",
+              outputType));
+    }
     analysis.setWhereExpression(whereExpression);
   }
 
@@ -1093,8 +1118,9 @@ public class AnalyzeVisitor extends StatementVisitor<Analysis, MPPQueryContext>
     analysis.setMergeOrderParameter(new OrderByParameter(queryStatement.getSortItemList()));
   }
 
-  private void analyzeExpression(Analysis analysis, Expression expression) {
+  private TSDataType analyzeExpression(Analysis analysis, Expression expression) {
     ExpressionTypeAnalyzer.analyzeExpression(analysis, expression);
+    return analysis.getType(expression);
   }
 
   private void analyzeGroupBy(Analysis analysis, QueryStatement queryStatement) {