public static boolean prepareImport(boolean isImportCmd,
                                      boolean isLocationSet, boolean isExternalSet, boolean isPartSpecSet,
                                      boolean waitOnPrecursor,
                                      String parsedLocation, String parsedTableName, String overrideDBName,
                                      LinkedHashMap<String, String> parsedPartSpec,
                                      String fromLocn, EximUtil.SemanticAnalyzerWrapperContext x,
                                      UpdatedMetaDataTracker updatedMetadata, HiveTxnManager txnMgr,
                                      long writeId, // Initialize with 0 for non-ACID and non-MM tables.
                                      MetaData rv,
                                      String dumpRoot,
                                      ReplicationMetricCollector metricCollector
  ) throws IOException, MetaException, HiveException, URISyntaxException {

    // initialize load path
    URI fromURI = EximUtil.getValidatedURI(x.getConf(), stripQuotes(fromLocn));
    Path fromPath = new Path(fromURI.getScheme(), fromURI.getAuthority(), fromURI.getPath());

    FileSystem fs = FileSystem.get(fromURI, x.getConf());
    x.getInputs().add(toReadEntity(fromPath, x.getConf()));

    if (rv.getTable() == null) {
      // nothing to do here, silently return.
      return false;
    }

    ReplicationSpec replicationSpec = rv.getReplicationSpec();
    if (replicationSpec.isNoop()) {
      // nothing to do here, silently return.
      x.getLOG().debug("Current update with ID:{} is noop",
              replicationSpec.getCurrentReplicationState());
      return false;
    }

    if (isImportCmd) {
      replicationSpec.setReplSpecType(ReplicationSpec.Type.IMPORT);
    }

    String dbname = rv.getTable().getDbName();
    if ((overrideDBName != null) && (!overrideDBName.isEmpty())) {
      // If the parsed statement contained a db.tablename specification, prefer that.
      dbname = overrideDBName;
    }

    // Create table associated with the import
    // Executed if relevant, and used to contain all the other details about the table if not.
    ImportTableDesc tblDesc;
    org.apache.hadoop.hive.metastore.api.Table tblObj = rv.getTable();
    try {
      tblDesc = getBaseCreateTableDescFromTable(dbname, tblObj);
    } catch (Exception e) {
      throw new HiveException(e);
    }

    boolean inReplicationScope = false;
    if ((replicationSpec != null) && replicationSpec.isInReplicationScope()) {
      tblDesc.setReplicationSpec(replicationSpec);
      inReplicationScope = true;
      tblDesc.setReplWriteId(writeId);
      tblDesc.setOwnerName(tblObj.getOwner());
    }

    if (isExternalSet) {
      tblDesc.setExternal(isExternalSet);
      // This condition-check could have been avoided, but to honour the old
      // default of not calling if it wasn't set, we retain that behaviour.
      // TODO:cleanup after verification that the outer if isn't really needed here
    }

    if (isLocationSet) {
      STATIC_LOG.debug("table {} location is {}", tblDesc.getTableName(), parsedLocation);
      tblDesc.setLocation(parsedLocation);
      x.getInputs().add(toReadEntity(new Path(parsedLocation), x.getConf()));
    }

    if (StringUtils.isNotBlank(parsedTableName)) {
      tblDesc.setTableName(TableName.fromString(parsedTableName, null, dbname));
    }

    if (tblDesc.getTableName() == null) {
      // Either we got the tablename from the IMPORT statement (first priority) or from the export dump.
      throw new SemanticException(ErrorMsg.NEED_TABLE_SPECIFICATION.getMsg());
    } else {
      x.getConf().set("import.destination.table", tblDesc.getTableName());
    }

    List<AlterTableAddPartitionDesc> partitionDescs = new ArrayList<>();
    Iterable<Partition> partitions = rv.getPartitions();
    for (Partition partition : partitions) {
      // TODO: this should ideally not create AddPartitionDesc per partition
      AlterTableAddPartitionDesc partsDesc =
              getBaseAddPartitionDescFromPartition(fromPath, dbname, tblDesc, partition,
                      replicationSpec, x.getConf());
      partitionDescs.add(partsDesc);
    }

    if (isPartSpecSet) {
      // The import specification asked for only a particular partition to be loaded
      // We load only that, and ignore all the others.
      boolean found = false;
      for (Iterator<AlterTableAddPartitionDesc> partnIter = partitionDescs
              .listIterator(); partnIter.hasNext(); ) {
        AlterTableAddPartitionDesc addPartitionDesc = partnIter.next();
        if (!found && addPartitionDesc.getPartitions().get(0).getPartSpec().equals(parsedPartSpec)) {
          found = true;
        } else {
          partnIter.remove();
        }
      }
      if (!found) {
        throw new SemanticException(
                ErrorMsg.INVALID_PARTITION
                        .getMsg(" - Specified partition not found in import directory"));
      }
    }

    Warehouse wh = new Warehouse(x.getConf());
    Table table = tableIfExists(tblDesc, x.getHive());
    boolean tableExists = false;

    if (table != null) {
      checkTable(table, tblDesc, replicationSpec, x.getConf());
      x.getLOG().debug("table " + tblDesc.getTableName() + " exists: metadata checked");
      tableExists = true;
    }

    if (!tableExists && isExternalSet) {
      // If the user is explicitly importing a new external table, clear txn flags from the spec.
      AcidUtils.setNonTransactional(tblDesc.getTblProps());
    }

    int stmtId = 0;
    if (!replicationSpec.isInReplicationScope()
            && ((tableExists && AcidUtils.isTransactionalTable(table))
            || (!tableExists && AcidUtils.isTablePropertyTransactional(tblDesc.getTblProps())))) {
      //if importing into existing transactional table or will create a new transactional table
      //(because Export was done from transactional table), need a writeId
      // Explain plan doesn't open a txn and hence no need to allocate write id.
      // In replication flow, no need to allocate write id. It will be allocated using the alloc write id event.
      if (x.getCtx().getExplainConfig() == null && !inReplicationScope) {
        writeId = txnMgr.getTableWriteId(tblDesc.getDatabaseName(), tblDesc.getTableName());
        stmtId = txnMgr.getStmtIdAndIncrement();
      }
    }

    if (inReplicationScope) {
      createReplImportTasks(
              tblDesc, partitionDescs,
              replicationSpec, waitOnPrecursor, table,
              fromURI, wh, x, writeId, stmtId, updatedMetadata, dumpRoot, metricCollector);
    } else {
      createRegularImportTasks(
              tblDesc, partitionDescs,
              isPartSpecSet, replicationSpec, table,
              fromURI, fs, wh, x, writeId, stmtId);
    }
    return tableExists;
  }
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