private List<org.apache.hadoop.hive.ql.metadata.Partition> addPartitionsWithReplication(Table table,
      List<Partition> partitions, long writeId) throws HiveException {
    // For replication add-ptns, we need to follow a insert-if-not-exist, alter-if-exists scenario.
    // TODO : ideally, we should push this mechanism to the metastore, because, otherwise, we have
    // no choice but to iterate over the partitions here.

    List<Partition> partitionsToAdd = new ArrayList<>();
    List<Partition> partitionsToAlter = new ArrayList<>();
    List<String> partitionNames = new ArrayList<>();
    Map<String, String> dbParams = context.getDb().getDatabase(desc.getDbName()).getParameters();
    for (Partition partition : partitions) {
      partitionNames.add(getPartitionName(table, partition));
      try {
        Partition p = context.getDb().getPartition(table, desc.getDbName(), desc.getTableName(), partition.getValues());
        if (desc.getReplicationSpec().allowReplacementInto(dbParams)) {
          partitionsToAlter.add(partition);
        } // else ptn already exists, but we do nothing with it.
      } catch (HiveException e){
        if (e.getCause() instanceof NoSuchObjectException) {
          // if the object does not exist, we want to add it.
          partitionsToAdd.add(partition);
        } else {
          throw e;
        }
      }
    }

    List<org.apache.hadoop.hive.ql.metadata.Partition> outPartitions = new ArrayList<>();
    if (!partitionsToAdd.isEmpty()) {
      LOG.debug("Calling AddPartition for {}", partitionsToAdd);
      for (Partition outPartition : context.getDb()
          .addPartitions(partitionsToAdd, desc.isIfNotExists(), true)) {
        outPartitions.add(
            new org.apache.hadoop.hive.ql.metadata.Partition(table,
                outPartition));
      }
    }
    // In case of replication, statistics is obtained from the source, so do not update those on replica.
    if (!partitionsToAlter.isEmpty()) {
      LOG.debug("Calling AlterPartition for {}", partitionsToAlter);
      EnvironmentContext ec = new EnvironmentContext();
      ec.putToProperties(StatsSetupConst.DO_NOT_UPDATE_STATS,
          StatsSetupConst.TRUE);
      String validWriteIdList = getValidWriteIdList(table, writeId);
      context.getDb().alterPartitions(desc.getDbName(), desc.getTableName(),
          partitionsToAlter, ec, validWriteIdList, writeId);

      for (Partition outPartition : context.getDb()
          .getPartitionsByNames(desc.getDbName(), desc.getTableName(),
              partitionNames, table)) {
        outPartitions.add(
            new org.apache.hadoop.hive.ql.metadata.Partition(table,
                outPartition));
      }
    }
    return outPartitions;
  }
Operation process of adding a partition to a table.
ql/src/java/org/apache/hadoop/hive/ql/ddl/table/partition/add/AlterTableAddPartitionOperation.java