void setWriteIdForAcidFileSinks() throws SemanticException, LockException {
    if (!driverContext.getPlan().getAcidSinks().isEmpty()) {
      List<FileSinkDesc> acidSinks = new ArrayList<>(driverContext.getPlan().getAcidSinks());
      //sorting makes tests easier to write since file names and ROW__IDs depend on statementId
      //so this makes (file name -> data) mapping stable
      acidSinks.sort(Comparator.comparing(FileSinkDesc::getDirName));

      // If the direct insert is on, sort the FSOs by moveTaskId as well because the dir is the same for all except the union use cases.
      boolean isDirectInsertOn = false;
      for (FileSinkDesc acidSink : acidSinks) {
        if (acidSink.isDirectInsert()) {
          isDirectInsertOn = true;
          break;
        }
      }
      if (isDirectInsertOn) {
        acidSinks.sort(Comparator.comparing(FileSinkDesc::getMoveTaskId));
      }

      int maxStmtId = -1;
      for (FileSinkDesc acidSink : acidSinks) {
        TableDesc tableInfo = acidSink.getTableInfo();
        TableName tableName = HiveTableName.of(tableInfo.getTableName());
        long writeId = driverContext.getTxnManager().getTableWriteId(tableName.getDb(), tableName.getTable());
        acidSink.setTableWriteId(writeId);

        /**
         * it's possible to have > 1 FileSink writing to the same table/partition
         * e.g. Merge stmt, multi-insert stmt when mixing DP and SP writes
         * Insert ... Select ... Union All Select ... using
         * {@link org.apache.hadoop.hive.ql.exec.AbstractFileMergeOperator#UNION_SUDBIR_PREFIX}
         */
        acidSink.setStatementId(driverContext.getTxnManager().getStmtIdAndIncrement());
        maxStmtId = Math.max(acidSink.getStatementId(), maxStmtId);
        String unionAllSubdir = "/" + AbstractFileMergeOperator.UNION_SUDBIR_PREFIX;
        if (acidSink.getInsertOverwrite() && acidSink.getDirName().toString().contains(unionAllSubdir) &&
            acidSink.isFullAcidTable()) {
          throw new UnsupportedOperationException("QueryId=" + driverContext.getPlan().getQueryId() +
              " is not supported due to OVERWRITE and UNION ALL.  Please use truncate + insert");
        }
      }
      if (HiveConf.getBoolVar(driverContext.getConf(), ConfVars.HIVE_EXTEND_BUCKET_ID_RANGE)) {
        for (FileSinkDesc each : acidSinks) {
          each.setMaxStmtId(maxStmtId);
        }
      }
    }
  }
Acquire read and write locks needed by the statement. The list of objects to be locked are obtained from the inputs
and outputs populated by the compiler. Locking strategy depends on HiveTxnManager and HiveLockManager configured.

This method also records the list of valid transactions. This must be done after any transactions have been opened.
ql/src/java/org/apache/hadoop/hive/ql/DriverTxnHandler.java