private static Task<?> addSinglePartition(ImportTableDesc tblDesc,
                                            Table table, Warehouse wh, AlterTableAddPartitionDesc addPartitionDesc,
                                            ReplicationSpec replicationSpec,
                                            EximUtil.SemanticAnalyzerWrapperContext x, Long writeId, int stmtId,
                                            boolean isReplication, String dumpRoot,
                                            ReplicationMetricCollector metricCollector)
          throws MetaException, IOException, HiveException {
    AlterTableAddPartitionDesc.PartitionDesc partSpec = addPartitionDesc.getPartitions().get(0);
    boolean isSkipTrash = false;
    boolean needRecycle = false;

    if (shouldSkipDataCopyInReplScope(tblDesc, replicationSpec)
            || (tblDesc.isExternal() && tblDesc.getLocation() == null)) {
      x.getLOG().debug("Adding AddPart and skipped data copy for partition "
              + partSpecToString(partSpec.getPartSpec()));
      // addPartitionDesc already has the right partition location
      @SuppressWarnings("unchecked")
      Task<?> addPartTask = TaskFactory.get(
              new DDLWork(x.getInputs(), x.getOutputs(), addPartitionDesc, isReplication,
                      dumpRoot, metricCollector), x.getConf());
      return addPartTask;
    } else {
      String srcLocation = partSpec.getLocation();
      if (replicationSpec.isInReplicationScope()
          && !ReplicationSpec.Type.IMPORT.equals(replicationSpec.getReplSpecType())) {
        Path partLocation = new Path(partSpec.getLocation());
        Path dataDirBase = partLocation.getParent();
        String bucketDir = partLocation.getName();
        for (int i=1; i<partSpec.getPartSpec().size(); i++) {
          bucketDir =  dataDirBase.getName() + File.separator + bucketDir;
          dataDirBase = dataDirBase.getParent();
        }
        String relativePartDataPath = EximUtil.DATA_PATH_NAME + File.separator + bucketDir;
        srcLocation =  new Path(dataDirBase, relativePartDataPath).toString();
      }
      fixLocationInPartSpec(tblDesc, table, wh, replicationSpec, partSpec, x);
      x.getLOG().debug("adding dependent CopyWork/AddPart/MoveWork for partition "
              + partSpecToString(partSpec.getPartSpec())
              + " with source location: " + srcLocation);
      Path tgtLocation = new Path(partSpec.getLocation());

      LoadFileType loadFileType;
      Path destPath;
      if (replicationSpec.isInReplicationScope()) {
        loadFileType = LoadFileType.IGNORE;
        destPath = tgtLocation;
        isSkipTrash = MetaStoreUtils.isSkipTrash(table.getParameters());
        if (table.isTemporary()) {
          needRecycle = false;
        } else {
          org.apache.hadoop.hive.metastore.api.Database db = x.getHive().getDatabase(table.getDbName());
          needRecycle = db != null && ReplChangeManager.shouldEnableCm(db, table.getTTable());
        }
      } else {
        loadFileType = replicationSpec.isReplace() ?
                LoadFileType.REPLACE_ALL : LoadFileType.OVERWRITE_EXISTING;
        //Replication scope the write id will be invalid
        boolean useStagingDirectory = !AcidUtils.isTransactionalTable(table.getParameters()) ||
                replicationSpec.isInReplicationScope();
        destPath = useStagingDirectory ? x.getCtx().getExternalTmpPath(tgtLocation)
                : new Path(tgtLocation, AcidUtils.deltaSubdir(writeId, writeId, stmtId));
      }

      Path moveTaskSrc = !AcidUtils.isTransactionalTable(table.getParameters()) ||
              replicationSpec.isInReplicationScope() ? destPath : tgtLocation;
      if (Utilities.FILE_OP_LOGGER.isTraceEnabled()) {
        Utilities.FILE_OP_LOGGER.trace("adding import work for partition with source location: "
                + srcLocation + "; target: " + tgtLocation + "; copy dest " + destPath + "; mm "
                + writeId + " for " + partSpecToString(partSpec.getPartSpec()) + ": " +
                (AcidUtils.isFullAcidTable(table) ? "acid" :
                        (AcidUtils.isInsertOnlyTable(table) ? "mm" : "flat")
                )
        );
      }

      Task<?> copyTask = null;
      if (replicationSpec.isInReplicationScope()) {
        boolean copyAtLoad = x.getConf().getBoolVar(HiveConf.ConfVars.REPL_RUN_DATA_COPY_TASKS_ON_TARGET);
        copyTask = ReplCopyTask.getLoadCopyTask(replicationSpec, new Path(srcLocation), destPath,
                x.getConf(), isSkipTrash, needRecycle, copyAtLoad, dumpRoot, metricCollector);
      } else {
        copyTask = TaskFactory.get(new CopyWork(new Path(srcLocation), destPath, false,
                                                dumpRoot, metricCollector, isReplication));
      }

      Task<?> addPartTask = null;
      if (x.getEventType() != DumpType.EVENT_COMMIT_TXN) {
        // During replication, by the time we are applying commit transaction event, we expect
        // the partition/s to be already added or altered by previous events. So no need to
        // create add partition event again.
        addPartTask = TaskFactory.get(new DDLWork(x.getInputs(), x.getOutputs(), addPartitionDesc,
                                      isReplication, dumpRoot, metricCollector), x.getConf());
      }

      MoveWork moveWork = new MoveWork(x.getInputs(), x.getOutputs(),
              null, null, false, dumpRoot, metricCollector, isReplication);

      // Note: this sets LoadFileType incorrectly for ACID; is that relevant for import?
      //       See setLoadFileType and setIsAcidIow calls elsewhere for an example.
      if (replicationSpec.isInReplicationScope() && AcidUtils.isTransactionalTable(tblDesc.getTblProps())) {
        LoadMultiFilesDesc loadFilesWork = new LoadMultiFilesDesc(
                Collections.singletonList(destPath),
                Collections.singletonList(tgtLocation),
                true, null, null);
        moveWork.setMultiFilesDesc(loadFilesWork);
        moveWork.setNeedCleanTarget(replicationSpec.isReplace());
      } else {
        LoadTableDesc loadTableWork = new LoadTableDesc(moveTaskSrc, Utilities.getTableDesc(table),
                partSpec.getPartSpec(),
                loadFileType,
                writeId);
        loadTableWork.setStmtId(stmtId);
        loadTableWork.setInheritTableSpecs(false);
        moveWork.setLoadTableWork(loadTableWork);
      }

      if (loadFileType == LoadFileType.IGNORE) {
        // if file is coped directly to the target location, then no need of move task in case the operation getting
        // replayed is add partition. As add partition will add the event for create partition. Even the statics are
        // updated properly in create partition flow as the copy is done directly to the partition location. For insert
        // operations, add partition task is anyways a no-op as alter partition operation does just some statistics
        // update which is again done in load operations as part of move task.
        if (x.getEventType() == DumpType.EVENT_INSERT) {
          copyTask.addDependentTask(TaskFactory.get(moveWork, x.getConf()));
        } else {
          if (addPartTask != null) {
            copyTask.addDependentTask(addPartTask);
          }
        }
        return copyTask;
      }
      Task<?> loadPartTask = TaskFactory.get(moveWork, x.getConf());
      copyTask.addDependentTask(loadPartTask);
      if (addPartTask != null) {
        addPartTask.addDependentTask(loadPartTask);
        x.getTasks().add(copyTask);
        return addPartTask;
      }
      return copyTask;
    }
  }
The same code is used from both the "repl load" as well as "import".
Given that "repl load" now supports two modes "repl load dbName [location]" and
"repl load [location]" in which case the database name has to be taken from the table metadata
by default and then over-ridden if something specified on the command line.
<p>
hence for import to work correctly we have to pass in the sessionState default Db via the
parsedDbName parameter
ql/src/java/org/apache/hadoop/hive/ql/parse/ImportSemanticAnalyzer.java