Sub topic 6
Why re-running COPY INTO is safe
This chapter explains how COPY INTO avoids loading the same file twice. It assumes you can run a basic COPY INTO statement from a volume into a Delta table and that you know what a Delta table is. It also assumes you know that Delta tables keep a transaction log.
The idempotency guarantee
A command is idempotent if running it again leaves the result unchanged. COPY INTO is idempotent at the file level. Each run looks at the files in the source path, skips the ones it has already loaded into the target table, and loads only the new ones.
This lets you run the same statement on a schedule without writing your own "what's new" logic. If new files land in the folder, the next run picks them up. If nothing new has arrived, the run loads zero rows.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');Run this statement twice with no new files in between. The second run adds no rows.
How COPY INTO tracks files
COPY INTO records the files it has loaded as part of the target table's state. It does not keep a separate list that you manage. On each run it compares the files it finds at the source with that record and loads only the ones it has not seen.
Two consequences follow:
- The tracking belongs to the target table. Loading the same folder into a different table loads every file again, because that table has no record of them.
- Tracking works at the level of the file, not the row. COPY INTO never inspects the contents to decide whether data is a duplicate.
What the result tells you
A COPY INTO statement returns a result row with counts. For a re-run with no new files, the number of rows inserted is 0. You can use this to confirm that a repeated run did nothing.
Edge cases and common errors
Idempotency is based on file identity, so a few situations behave in ways that surprise people.
- Changed file contents. If you overwrite a file with the same name and path after it was loaded, COPY INTO still treats it as loaded. The new contents are not picked up.
- Renamed or re-uploaded files. A file copied to a new path is a new file to COPY INTO, so its rows load again. This is a common source of duplicates when an upstream job re-delivers data under a new name.
- Different target tables. Each target keeps its own record, as noted above.
- Overlapping sources. If two COPY INTO statements point at the same folder and the same table, the second one skips files the first already loaded. This is safe, but run them one after another rather than at the same moment.
Overriding the guarantee
Sometimes you want to load files again, for example after fixing a bad table. The force copy option turns file tracking off for that run:
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true')
COPY_OPTIONS ('force' = 'true');With force set to true, every matching file loads again, including files from earlier runs. This can create duplicate rows. A later chapter covers when this is appropriate. For now, treat force as an explicit opt-out, and leave it out of scheduled jobs.
Exercise
A file named orders_01.csv was loaded into main.sales.orders last week. Yesterday, the upstream system replaced it with a corrected file of the same name and path. You run the same COPY INTO statement without any copy options. What happens to the corrected data, and what are two ways to get it into the table?
Show answer
Nothing loads from that file. COPY INTO has already recorded the path as loaded, so it skips the corrected file and inserts 0 rows for it. To get the corrected data in, you can (1) rerun with COPY_OPTIONS ('force' = 'true'), after deleting the old rows so you do not create duplicates, or (2) have the upstream system deliver the corrected file under a new name, which COPY INTO treats as a new file.
Exercise
You run the same COPY INTO statement against the same source folder but with a new target table, main.sales.orders_copy. Does it skip the files that were loaded into main.sales.orders? Why?
Show answer
No. File tracking belongs to each target table, and orders_copy has no record of any loaded files, so every file loads.