private static boolean analyzeHaving(Analysis analysis, QueryStatement queryStatement) {
    if (!queryStatement.hasHaving()) {
      return true;
    }

    Set<String> measurementSet = new HashSet<>(analysis.getMeasurementList());
    Set<Expression> aggregationExpressions = analysis.getAggregationExpressions();
    Expression havingExpression = queryStatement.getHavingCondition().getPredicate();
    for (Expression aggregationExpression : searchAggregationExpressions(havingExpression)) {
      Expression normalizedAggregationExpression = normalizeExpression(aggregationExpression);

      // not support having agg(s1+s2) temporarily
      if (!((normalizedAggregationExpression).getExpressions().get(0)
          instanceof TimeSeriesOperand)) {
        return false;
      }

      String measurement =
          normalizedAggregationExpression.getExpressions().get(0).getOutputSymbol();
      if (!measurementSet.contains(measurement)) {
        // adapt this case: select agg(s1) from xx having agg(s3)
        measurementSet.add(measurement);
        analysis.getMeasurementList().add(measurement);
        analysis
            .getMeasurementSchemaList()
            .add(analysis.getDeviceTemplate().getSchema(measurement));
      }

      analyzeExpressionType(analysis, aggregationExpression);
      analyzeExpressionType(analysis, normalizedAggregationExpression);

      aggregationExpressions.add(aggregationExpression);
    }

    TSDataType outputType = analyzeExpressionType(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);

    return true;
  }
Methods in this class are used for aggregation, templated with align by device situation.
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/TemplatedAggregationAnalyze.java