private void upsertMeasurementAttributes()

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


  private void upsertMeasurementAttributes(SessionPool sessionPool, DeviceDTO deviceDTO)
      throws BaseException {
    try {
      String attributes =
          executeQueryOneLine(
              sessionPool, "show timeseries " + deviceDTO.getTimeseries(), "attributes");
      Map<String, String> existAttributes = handleTagsString(attributes);
      List<List<String>> newAttributes = deviceDTO.getAttributes();
      Map<String, String> addAttributes = handleTagsList(newAttributes);

      if (addAttributes == null && existAttributes == null) {
        return;
      }
      if (addAttributes != null && addAttributes.equals(existAttributes)) {
        return;
      }

      if (existAttributes != null) {
        String sql =
            "alter timeseries "
                + deviceDTO.getTimeseries()
                + " drop "
                + String.join(",", existAttributes.keySet());
        sessionPool.executeNonQueryStatement(sql);
      }
      if (newAttributes != null) {
        for (List<String> newAttribute : newAttributes) {
          String sql =
              "alter timeseries "
                  + deviceDTO.getTimeseries()
                  + " add attributes "
                  + newAttribute.get(0)
                  + "="
                  + newAttribute.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_ATTRIBUTES_FAIL, ErrorCode.UPSERT_ATTRIBUTES_FAIL_MSG);
    } catch (IoTDBConnectionException e) {
      logger.error(e.getMessage());
      throw new BaseException(ErrorCode.GET_SESSION_FAIL, ErrorCode.GET_SESSION_FAIL_MSG);
    }
  }