private void copyMSD(MStorageDescriptor newSd, MStorageDescriptor oldSd) {
    oldSd.setLocation(newSd.getLocation());
    // If the columns of the old column descriptor != the columns of the new one,
    // then change the old storage descriptor's column descriptor.
    // Convert the MFieldSchema's to their thrift object counterparts, because we maintain
    // datastore identity (i.e., identity of the model objects are managed by JDO,
    // not the application).
    List<FieldSchema> oldCols = oldSd.getCD() != null && oldSd.getCD().getCols() != null ?
        convertToFieldSchemas(oldSd.getCD().getCols()) : null;
    List<FieldSchema> newCols = newSd.getCD() != null && newSd.getCD().getCols() != null ?
        convertToFieldSchemas(newSd.getCD().getCols()) : null;
    if (oldCols == null || !oldCols.equals(newCols)) {
      // First replace any constraints that may be associated with this CD
      // Create mapping from old col indexes to new col indexes
      if (oldCols != null && newCols != null) {
        Map<Integer, Integer> mapping = new HashMap<>();
        for (int i = 0; i < oldCols.size(); i++) {
          FieldSchema oldCol = oldCols.get(i);
          //TODO: replace for loop with list.indexOf()
          for (int j = 0; j < newCols.size(); j++) {
            FieldSchema newCol = newCols.get(j);
            if (oldCol.equals(newCol)) {
              mapping.put(i, j);
              break;
            }
          }
        }
        // If we find it, we will change the reference for the CD.
        // If we do not find it, i.e., the column will be deleted, we do not change it
        // and we let the logic in removeUnusedColumnDescriptor take care of it
        try (QueryWrapper query = new QueryWrapper(pm.newQuery(MConstraint.class, "parentColumn == inCD || childColumn == inCD"))) {
          query.declareParameters("MColumnDescriptor inCD");
          List<MConstraint> mConstraintsList = (List<MConstraint>) query.execute(oldSd.getCD());
          pm.retrieveAll(mConstraintsList);
          for (MConstraint mConstraint : mConstraintsList) {
            if (oldSd.getCD().equals(mConstraint.getParentColumn())) {
              Integer newIdx = mapping.get(mConstraint.getParentIntegerIndex());
              if (newIdx != null) {
                mConstraint.setParentColumn(newSd.getCD());
                mConstraint.setParentIntegerIndex(newIdx);
              }
            }
            if (oldSd.getCD().equals(mConstraint.getChildColumn())) {
              Integer newIdx = mapping.get(mConstraint.getChildIntegerIndex());
              if (newIdx != null) {
                mConstraint.setChildColumn(newSd.getCD());
                mConstraint.setChildIntegerIndex(newIdx);
              }
            }
          }
          pm.makePersistentAll(mConstraintsList);
        }
        // Finally replace CD
        oldSd.setCD(newSd.getCD());
      }
    }

    oldSd.setBucketCols(newSd.getBucketCols());
    oldSd.setIsCompressed(newSd.isCompressed());
    oldSd.setInputFormat(newSd.getInputFormat());
    oldSd.setOutputFormat(newSd.getOutputFormat());
    oldSd.setNumBuckets(newSd.getNumBuckets());
    oldSd.getSerDeInfo().setName(newSd.getSerDeInfo().getName());
    oldSd.getSerDeInfo().setSerializationLib(
        newSd.getSerDeInfo().getSerializationLib());
    oldSd.getSerDeInfo().setParameters(newSd.getSerDeInfo().getParameters());
    oldSd.getSerDeInfo().setDescription(newSd.getSerDeInfo().getDescription());
    oldSd.setSkewedColNames(newSd.getSkewedColNames());
    oldSd.setSkewedColValues(newSd.getSkewedColValues());
    oldSd.setSkewedColValueLocationMaps(newSd.getSkewedColValueLocationMaps());
    oldSd.setSortCols(newSd.getSortCols());
    oldSd.setParameters(newSd.getParameters());
    oldSd.setStoredAsSubDirectories(newSd.isStoredAsSubDirectories());
  }
Alters an existing partition. Initiates copy of SD. Returns the old CD.
@param part_vals Partition values (of the original partition instance)
@param newPart Partition object containing new information
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java