// Path: src/java/com/google/cloud/bigquery/dwhassessment/extractiontool/db/AvroHelper.java
// public static GenericRecord parseRowToAvro(ResultSet row, Schema schema) throws SQLException {
//   GenericRecordBuilder recordBuilder = new GenericRecordBuilder(schema);
//   ResultSetMetaData metaData = row.getMetaData();
//   for (int columnIndex = 1; columnIndex <= row.getMetaData().getColumnCount(); columnIndex++) {
//     recordBuilder.set(
//         metaData.getColumnName(columnIndex), getRowObject(metaData, row, columnIndex));
//   }
//   return recordBuilder.build();
// }
// 
// Path: src/java/com/google/cloud/bigquery/dwhassessment/extractiontool/dumper/DataEntityManager.java
// public interface DataEntityManager extends Closeable {
// 
//   /**
//    * Get the output stream for an entity.
//    *
//    * @param name The name of the output entity.
//    */
//   OutputStream getEntityOutputStream(String name) throws IOException;
// 
//   /**
//    * Indicate whether the data entity allows resumable processing.
//    *
//    * @return true if some progress can be retained after the writing is interrupted.
//    */
//   boolean isResumable();
// 
//   /**
//    * Get the absolute path of a file given its name.
//    *
//    * @param name The name of the file.
//    */
//   Path getAbsolutePath(String name);
// }


import static com.google.cloud.bigquery.dwhassessment.extractiontool.db.AvroHelper.parseRowToAvro;
import static java.nio.file.StandardCopyOption.ATOMIC_MOVE;
import com.google.cloud.bigquery.dwhassessment.extractiontool.dumper.DataEntityManager;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
import java.util.function.Supplier;
import java.util.logging.Logger;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericRecord;
      while (!resultSet.isAfterLast()) {
        executeScriptChunk(
            resultSet, schema, dataEntityManager, chunkRows, labelColumn, scriptName, chunkNumber);
        chunkNumber++;
      }
      return;
    }
    executeScriptOneSwoop(connection, scriptName, script, schema, dataEntityManager);
  }

  private void executeScriptChunk(
      ResultSet resultSet,
      Schema schema,
      DataEntityManager dataEntityManager,
      Integer chunkRows,
      String labelColumn,
      String scriptName,
      Integer chunkNumber)
      throws SQLException, IOException {
    Timestamp previousTimestamp = new Timestamp(0);
    Timestamp currentTimestamp = resultSet.getTimestamp(labelColumn);
    String firstRowStamp = getUtcTimeStringFromTimestamp(currentTimestamp);
    String tempFileName =
        String.format("%s-%s_%d_temp.avro", scriptName, firstRowStamp, chunkNumber);
    try (ResultSetRecorder<GenericRecord> dumper =
        AvroResultSetRecorder.create(
            schema, dataEntityManager.getEntityOutputStream(tempFileName))) {
      int rowCount = 0;
      while (rowCount < chunkRows || currentTimestamp.equals(previousTimestamp)) {
        // Process first, then advance the row.
// Path: src/java/com/google/cloud/bigquery/dwhassessment/extractiontool/db/AvroHelper.java
// public static GenericRecord parseRowToAvro(ResultSet row, Schema schema) throws SQLException {
//   GenericRecordBuilder recordBuilder = new GenericRecordBuilder(schema);
//   ResultSetMetaData metaData = row.getMetaData();
//   for (int columnIndex = 1; columnIndex <= row.getMetaData().getColumnCount(); columnIndex++) {
//     recordBuilder.set(
//         metaData.getColumnName(columnIndex), getRowObject(metaData, row, columnIndex));
//   }
//   return recordBuilder.build();
// }
// 
// Path: src/java/com/google/cloud/bigquery/dwhassessment/extractiontool/dumper/DataEntityManager.java
// public interface DataEntityManager extends Closeable {
// 
//   /**
//    * Get the output stream for an entity.
//    *
//    * @param name The name of the output entity.
//    */
//   OutputStream getEntityOutputStream(String name) throws IOException;
// 
//   /**
//    * Indicate whether the data entity allows resumable processing.
//    *
//    * @return true if some progress can be retained after the writing is interrupted.
//    */
//   boolean isResumable();
// 
//   /**
//    * Get the absolute path of a file given its name.
//    *
//    * @param name The name of the file.
//    */
//   Path getAbsolutePath(String name);
// }


// Path: src/java/com/google/cloud/bigquery/dwhassessment/extractiontool/db/ScriptManagerImpl.java
import static com.google.cloud.bigquery.dwhassessment.extractiontool.db.AvroHelper.parseRowToAvro;
import static java.nio.file.StandardCopyOption.ATOMIC_MOVE;
import com.google.cloud.bigquery.dwhassessment.extractiontool.dumper.DataEntityManager;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
import java.util.function.Supplier;
import java.util.logging.Logger;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericRecord;

      while (!resultSet.isAfterLast()) {
        executeScriptChunk(
            resultSet, schema, dataEntityManager, chunkRows, labelColumn, scriptName, chunkNumber);
        chunkNumber++;
      }
      return;
    }
    executeScriptOneSwoop(connection, scriptName, script, schema, dataEntityManager);
  }

  private void executeScriptChunk(
      ResultSet resultSet,
      Schema schema,
      DataEntityManager dataEntityManager,
      Integer chunkRows,
      String labelColumn,
      String scriptName,
      Integer chunkNumber)
      throws SQLException, IOException {
    Timestamp previousTimestamp = new Timestamp(0);
    Timestamp currentTimestamp = resultSet.getTimestamp(labelColumn);
    String firstRowStamp = getUtcTimeStringFromTimestamp(currentTimestamp);
    String tempFileName =
        String.format("%s-%s_%d_temp.avro", scriptName, firstRowStamp, chunkNumber);
    try (ResultSetRecorder<GenericRecord> dumper =
        AvroResultSetRecorder.create(
            schema, dataEntityManager.getEntityOutputStream(tempFileName))) {
      int rowCount = 0;
      while (rowCount < chunkRows || currentTimestamp.equals(previousTimestamp)) {
        // Process first, then advance the row.
        dumper.add(parseRowToAvro(resultSet, schema));