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 2022/12/15 03:50:01 UTC

[GitHub] [iotdb] liuminghui233 commented on a diff in pull request #8467: [IOTDB-4816] Show Queries - support antlrParse & analyze process

liuminghui233 commented on code in PR #8467:
URL: https://github.com/apache/iotdb/pull/8467#discussion_r1049179550


##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/ExpressionTypeAnalyzer.java:
##########
@@ -54,7 +54,7 @@ public class ExpressionTypeAnalyzer {
 
   private final Map<NodeRef<Expression>, TSDataType> expressionTypes = new LinkedHashMap<>();
 
-  private ExpressionTypeAnalyzer() {}
+  public ExpressionTypeAnalyzer() {}

Review Comment:
   no need to modify



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/AnalyzeVisitor.java:
##########
@@ -2532,4 +2541,65 @@ public Analysis visitShowPipeSinkType(
     analysis.setFinishQueryAfterAnalyze(true);
     return analysis;
   }
+
+  @Override
+  public Analysis visitShowQueries(
+      ShowQueriesStatement showQueriesStatement, MPPQueryContext context) {
+    Analysis analysis = new Analysis();
+    analysis.setStatement(showQueriesStatement);
+    analysis.setRespDatasetHeader(DatasetHeaderFactory.getShowQueriesHeader());
+
+    List<TDataNodeLocation> allRunningDataNodeLocations = getRunningDataNodeLocations();
+    if (allRunningDataNodeLocations.isEmpty()) {
+      analysis.setFinishQueryAfterAnalyze(true);
+    }
+    // TODO Constant folding optimization for Where Predicate after True/False Constant introduced
+    analysis.setRunningDataNodeLocations(allRunningDataNodeLocations);
+
+    analyzeWhere(analysis, showQueriesStatement);
+
+    return analysis;
+  }
+
+  private List<TDataNodeLocation> getRunningDataNodeLocations() {
+    try (ConfigNodeClient client =
+        ConfigNodeClientManager.getInstance().borrowClient(ConfigNodeInfo.configNodeRegionId)) {
+      TGetDataNodeLocationsResp showDataNodesResp = client.getRunningDataNodeLocations();
+      if (showDataNodesResp.getStatus().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+        throw new StatementAnalyzeException(
+            "An error occurred when executing getRunningDataNodeLocations():"
+                + showDataNodesResp.getStatus().getMessage());
+      }
+      return showDataNodesResp.getDataNodeLocationList();
+    } catch (TException | IOException e) {
+      throw new StatementAnalyzeException(
+          "An error occurred when executing getRunningDataNodeLocations():" + e.getMessage());
+    }
+  }
+
+  private void analyzeWhere(Analysis analysis, ShowQueriesStatement showQueriesStatement) {
+    WhereCondition whereCondition = showQueriesStatement.getWhereCondition();
+    if (whereCondition == null) {
+      return;
+    }
+
+    ExpressionTypeAnalyzer analyzer = new ExpressionTypeAnalyzer();
+    Expression whereExpression =
+        ExpressionAnalyzer.replaceTimeSeriesOperand(
+            whereCondition.getPredicate(),
+            ColumnHeaderConstant.showQueriesColumnHeaders,
+            analyzer.getExpressionTypes());
+    analyzer.analyze(whereExpression);
+    ExpressionTypeAnalyzer.updateAnalysis(analysis, analyzer);
+
+    TSDataType outputType = analyzer.getExpressionTypes().get(NodeRef.of(whereExpression));

Review Comment:
   ```suggestion
       Expression whereExpression =
           ExpressionAnalyzer.replaceTimeSeriesOperand(
               whereCondition.getPredicate(),
               ColumnHeaderConstant.showQueriesColumnHeaders,
               analyzer.getExpressionTypes());
       TSDataType outputType = analyzeExpression(whereExpression);
   ```



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/ExpressionTypeAnalyzer.java:
##########
@@ -71,7 +71,7 @@ public static void analyzeExpression(
     types.putAll(analyzer.getExpressionTypes());
   }
 
-  private static void updateAnalysis(Analysis analysis, ExpressionTypeAnalyzer analyzer) {
+  public static void updateAnalysis(Analysis analysis, ExpressionTypeAnalyzer analyzer) {

Review Comment:
   no need to modify



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/ExpressionAnalyzer.java:
##########
@@ -1223,4 +1227,78 @@ public static Expression evaluatePredicate(Expression predicate) {
     }
     return predicate;
   }
+
+  /**
+   * Replace TimeSeriesOperand in Expression with according ColumnHeaderName, and put Type of new
+   * TimeSeriesOperand to TypeMap. eg:
+   *
+   * <p>columnHeaders: [[QueryId, TEXT], [DataNodeId, INT32]...]
+   *
+   * <p>dataNodeID > 1 -> DataNodeId > 1, reconstruct and put Type of `DataNodeID` as INT32
+   *
+   * <p>errorInput > 1, no according ColumnHeaderName of `errorInput`, throw exception
+   */
+  public static Expression replaceTimeSeriesOperand(
+      Expression predicate,
+      List<ColumnHeader> columnHeaders,
+      Map<NodeRef<Expression>, TSDataType> expressionTypes) {

Review Comment:
   ```suggestion
     public static Expression bindTypeForTimeSeriesOperand(
         Expression expression,
         CaseInsensitiveMap<String, TSDataType> timeseriesToTypeMap) {
   ```



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/ExpressionAnalyzer.java:
##########
@@ -1223,4 +1227,78 @@ public static Expression evaluatePredicate(Expression predicate) {
     }
     return predicate;
   }
+
+  /**
+   * Replace TimeSeriesOperand in Expression with according ColumnHeaderName, and put Type of new
+   * TimeSeriesOperand to TypeMap. eg:
+   *
+   * <p>columnHeaders: [[QueryId, TEXT], [DataNodeId, INT32]...]
+   *
+   * <p>dataNodeID > 1 -> DataNodeId > 1, reconstruct and put Type of `DataNodeID` as INT32
+   *
+   * <p>errorInput > 1, no according ColumnHeaderName of `errorInput`, throw exception
+   */
+  public static Expression replaceTimeSeriesOperand(
+      Expression predicate,
+      List<ColumnHeader> columnHeaders,
+      Map<NodeRef<Expression>, TSDataType> expressionTypes) {
+    if (predicate instanceof TernaryExpression) {
+      Expression firstExpression =
+          replaceTimeSeriesOperand(
+              ((TernaryExpression) predicate).getFirstExpression(), columnHeaders, expressionTypes);
+      Expression secondExpression =
+          replaceTimeSeriesOperand(
+              ((TernaryExpression) predicate).getSecondExpression(),
+              columnHeaders,
+              expressionTypes);
+      Expression thirdExpression =
+          replaceTimeSeriesOperand(
+              ((TernaryExpression) predicate).getThirdExpression(), columnHeaders, expressionTypes);
+      return reconstructTernaryExpression(
+          predicate, firstExpression, secondExpression, thirdExpression);
+    } else if (predicate instanceof BinaryExpression) {
+      Expression leftExpression =
+          replaceTimeSeriesOperand(
+              ((BinaryExpression) predicate).getLeftExpression(), columnHeaders, expressionTypes);
+      Expression rightExpression =
+          replaceTimeSeriesOperand(
+              ((BinaryExpression) predicate).getRightExpression(), columnHeaders, expressionTypes);
+      return reconstructBinaryExpression(
+          predicate.getExpressionType(), leftExpression, rightExpression);
+    } else if (predicate instanceof UnaryExpression) {
+      Expression expression =
+          replaceTimeSeriesOperand(
+              ((UnaryExpression) predicate).getExpression(), columnHeaders, expressionTypes);
+      return reconstructUnaryExpression((UnaryExpression) predicate, expression);
+    } else if (predicate instanceof FunctionExpression) {
+      List<Expression> expressions = predicate.getExpressions();
+      List<Expression> childrenExpressions = new ArrayList<>();
+      for (Expression expression : expressions) {
+        childrenExpressions.add(
+            replaceTimeSeriesOperand(expression, columnHeaders, expressionTypes));
+      }
+      return reconstructFunctionExpression((FunctionExpression) predicate, childrenExpressions);
+    } else if (predicate instanceof TimeSeriesOperand) {
+      String oldPathString = ((TimeSeriesOperand) predicate).getPath().getFullPath();
+
+      for (ColumnHeader columnHeader : columnHeaders) {
+        if (oldPathString.equalsIgnoreCase(columnHeader.getColumnName())) {
+          try {
+            Expression newTimeSeriesOperand =
+                reconstructTimeSeriesOperand(new PartialPath(columnHeader.getColumnName()));
+            expressionTypes.put(NodeRef.of(newTimeSeriesOperand), columnHeader.getColumnType());
+            return newTimeSeriesOperand;
+          } catch (IllegalPathException ignored) {
+          }
+        }
+      }
+      throw new SemanticException(
+          String.format("please ensure input[%s] is correct", oldPathString));

Review Comment:
   ```suggestion
   PartialPath path = ((TimeSeriesOperand) expression).getPath();
       if(timeseriesToTypeMap.containsKey(path.toString())) {
         return reconstructTimeSeriesOperand(
                 new MeasurementPath(path, timeseriesToTypeMap.get(path.toString()))
         );
       } else {
       return expression;
       }
   ```



-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org