@Override
  public int execute() throws HiveException {
    if (!AlterTableUtils.allowOperationInReplicationScope(context.getDb(), desc.getDbTableName(), null,
        desc.getReplicationSpec())) {
      // no alter, the table is missing either due to drop/rename which follows the alter.
      // or the existing table is newer than our update.
      LOG.debug("DDLTask: Alter Table is skipped as table {} is newer than update", desc.getDbTableName());
      return 0;
    }

    Table oldTable = context.getDb().getTable(desc.getDbTableName());
    List<Partition> partitions = getPartitions(oldTable, desc.getPartitionSpec(), context);

    // Don't change the table object returned by the metastore, as we'll mess with its caches.
    Table table = oldTable.copy();

    environmentContext = initializeEnvironmentContext(oldTable, desc.getEnvironmentContext());

    if (partitions == null) {
      doAlteration(table, null);
    } else {
      for (Partition partition : partitions) {
        doAlteration(table, partition);
      }
    }

    finalizeAlterTableWithWriteIdOp(table, oldTable, partitions, context, environmentContext);
    return 0;
  }
Operation process of running some alter table command that requires write id.
ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableOperation.java