Fix the batch plan for a bulk CSV load

Review the AI's codeEasy10 min

Your task

An AI's ETL step read an 80 million row CSV into memory with one pd.read_csv call, then inserted it one row at a time. It ran out of memory, and on smaller files it took hours. The rewrite streams the file in chunks and sends each chunk to Postgres in multi-row batches. You plan the statement sizes before running it.

An assistant wrote plan_bulk_load(total_rows, chunk_size, batch_size). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • total_rows: rows in the file.
  • chunk_size: the most rows read into memory at a time.
  • batch_size: the most rows sent in one insert statement.
  • Return the number of rows in each insert statement, in order.

Rules the tests check:

  • If any argument is 0 or less, return [].
  • Read the file in chunks of chunk_size rows. The last chunk holds whatever is left, so every row is read exactly once.
  • Split each chunk into batches of batch_size rows. The last batch of a chunk holds whatever is left of that chunk. A batch never mixes rows from two chunks.
  • The sizes add up to total_rows.

Examples

plan_bulk_load(total_rows: int, chunk_size: int, batch_size: int) -> list[int]
Example 1
Inputplan_bulk_load(10, 4, 3)
Output[3, 1, 3, 1, 2]
Example 2
Inputplan_bulk_load(5, 10, 2)
Output[2, 2, 1]
Example 3
Inputplan_bulk_load(6, 3, 3)
Output[3, 3]