private void upsertMeasurementTags()

in backend/src/main/java/org/apache/iotdb/admin/service/impl/IotDBServiceImpl.java [1727:1774]


  private void upsertMeasurementTags(SessionPool sessionPool, DeviceDTO deviceDTO)
      throws BaseException {
    try {
      String tags =
          executeQueryOneLine(sessionPool, "show timeseries " + deviceDTO.getTimeseries(), "tags");
      Map<String, String> existTags = handleTagsString(tags);
      List<List<String>> newTags = deviceDTO.getTags();
      Map<String, String> addTags = handleTagsList(newTags);

      if (addTags == null && existTags == null) {
        return;
      }
      if (addTags != null && addTags.equals(existTags)) {
        return;
      }

      if (existTags != null) {
        String sql =
            "alter timeseries "
                + deviceDTO.getTimeseries()
                + " drop "
                + String.join(",", existTags.keySet());
        sessionPool.executeNonQueryStatement(sql);
      }
      if (newTags != null) {
        for (List<String> newTag : newTags) {
          String sql =
              "alter timeseries "
                  + deviceDTO.getTimeseries()
                  + " add tags "
                  + newTag.get(0)
                  + "="
                  + newTag.get(1);
          sessionPool.executeNonQueryStatement(sql);
        }
      }
    } catch (BaseException | StatementExecutionException e) {
      logger.error(e.getMessage());
      if (e.getMessage().contains("No permissions")) {
        throw new BaseException(
            ErrorCode.NO_PRI_ALTER_MEASUREMENT, ErrorCode.NO_PRI_ALTER_MEASUREMENT_MSG);
      }
      throw new BaseException(ErrorCode.UPSERT_TAGS_FAIL, ErrorCode.UPSERT_TAGS_FAIL_MSG);
    } catch (IoTDBConnectionException e) {
      logger.error(e.getMessage());
      throw new BaseException(ErrorCode.GET_SESSION_FAIL, ErrorCode.GET_SESSION_FAIL_MSG);
    }
  }