Sub topic 14
Troubleshooting common load problems
This chapter assumes you can write a basic COPY INTO statement and know how it skips files it has already loaded. It also assumes you have Unity Catalog permissions to create tables and read from a volume. You should know the options covered earlier: FORMAT_OPTIONS, PATTERN, VALIDATE, mergeSchema and force.
Most COPY INTO problems fall into four groups:
- Nothing loaded.
- Too much loaded.
- The load failed.
- The data loaded but looks wrong.
Each group has a different first check. The chapter covers them in that order.
Start with the result row
Every COPY INTO run returns a one-row result. Read it before you change anything.
| Column | Meaning |
|---|---|
| `num_affected_rows` | Rows written by this run |
| `num_inserted_rows` | Rows inserted into the table |
| `num_skipped_corrupt_files` | Files skipped because they could not be read |
The table's history keeps the same numbers for earlier runs, so you can compare runs over time:
DESCRIBE HISTORY main.sales.orders;Look at rows where operation is COPY INTO. The operationMetrics column holds the counts for each run. A sudden drop to zero, or a spike far above normal, tells you which group of problems you have.
Nothing loaded: zero rows and no error
A statement that succeeds with num_affected_rows = 0 is the most common surprise. There are three usual causes.
- The files were already loaded. This is the idempotency feature working as designed.
COPY INTOrecords which files it has loaded and skips them. If you replaced a file's contents but kept its name, it is still treated as loaded. - The path or pattern matches nothing.
PATTERNis a glob applied to paths under theFROMlocation. It is not a full path. A pattern such as'/Volumes/main/sales/landing/orders/2026-12-*.csv'will match nothing, while'2026-12-*.csv'will match the files you want. You also cannot combinePATTERNwithFILESin the same statement. - The path has a typo or a case difference. Volume and object storage paths are case-sensitive.
Check what is actually in the location before you suspect the table:
LIST '/Volumes/main/sales/landing/orders/';If LIST shows the files you expect, run the statement with VALIDATE. It reads the files and checks them against the target without writing:
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
PATTERN = '2026-12-*.csv'
VALIDATE 10 ROWS
FORMAT_OPTIONS ('header' = 'true');Too much loaded: duplicates
Duplicate rows have two common sources.
'force' = 'true'is set. It reloads every matching file, including ones loaded before. Remove it from scheduled jobs. Use it once, on purpose, and only when you also deal with the rows already in the table.- The same data arrives under a new file name.
COPY INTOtracks files, not row content. If an upstream system re-exportsorders_1204.csvasorders_1204_v2.csv, both files load.
Having a source_file column makes duplicates easy to find. This query uses the column from the SELECT-source example:
SELECT order_id, COUNT(*) AS copies, COLLECT_SET(source_file) AS files
FROM main.sales.orders
GROUP BY order_id
HAVING COUNT(*) > 1;If duplicates must never reach the table, load into a raw table first and deduplicate into a curated table with MERGE. Do not rely on COPY INTO alone for this.
The statement fails: permissions
Permission errors usually name the missing privilege or the location. Check these in order:
| What you need | Privilege |
|---|---|
| Read a volume | `READ VOLUME` on the volume, plus `USE CATALOG` and `USE SCHEMA` |
| Read an external location path | `READ FILES` on the external location |
| Write to the target | `MODIFY` on the table, plus `USE CATALOG` and `USE SCHEMA` |
A frequent trap is access that works in a notebook but fails in a job. The job runs as a different identity, such as a service principal. Grant the privileges to the identity that runs the job, not to your own user.
GRANT READ VOLUME ON VOLUME main.sales.landing TO `etl-service-principal`;
GRANT MODIFY ON TABLE main.sales.orders TO `etl-service-principal`;Two other errors look like permission problems. If the target table does not exist, COPY INTO fails. Only the schemaless empty table pattern (CREATE TABLE ... ; with no columns) lets it create columns for you. The target must also be a Delta table.
The statement fails: schema and type errors
When the files and the table disagree, you see errors about casting, a missing column, or a column count.
- Type mismatch. With
inferSchemaon, a column whose first sample values look like integers may later containN/A. The load then fails when it writes into a numeric column. Fix it by loading the column as text and casting in a SELECT source, or by cleaning the source. - Header handling. Without
'header' = 'true', the header row is read as data. Castingorder_idfrom the textorder_idfails. - Extra columns in the source. If a new column appears in the files and the table does not have it, it will not show up in the table unless you add
'mergeSchema' = 'true'toCOPY_OPTIONS. - Column name or order differences. With a SELECT source, you choose names and casts explicitly, which removes most ambiguity.
COPY INTO main.sales.orders
FROM (
SELECT
CAST(order_id AS BIGINT) AS order_id,
TRY_CAST(amount AS DECIMAL(10, 2)) AS amount,
CAST(order_ts AS TIMESTAMP) AS order_ts,
_metadata.file_path AS source_file
FROM '/Volumes/main/sales/landing/orders/'
)
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');TRY_CAST returns NULL for values it cannot convert instead of failing the statement. That keeps the load moving, but it hides bad values. Count the resulting nulls after the load and decide whether they are acceptable.
The data loads but looks wrong
Parsing errors that do not raise an error are the hardest to notice. Check the shape of the loaded rows first.
| Symptom | Likely cause | Fix |
|---|---|---|
| Everything lands in one column | Wrong delimiter | Set `'delimiter' = ';'` or `'\t'` to match the file |
| Rows split in the middle of a text field | Fields contain line breaks | Set `'multiLine' = 'true'` |
| Quotes appear inside values | Quote or escape character differs | Set `'quote'` and `'escape'` |
| Garbled characters | Wrong encoding | Set `'encoding'` (for example `'ISO-8859-1'`) |
| Nulls where values should be | Malformed rows or failed casts | Check `'mode'` and your casts |
For CSV, the mode format option controls what happens to malformed rows. PERMISSIVE (the default) keeps the row and sets fields it cannot parse to null. DROPMALFORMED silently drops the row. FAILFAST stops the load on the first bad row. During development, use FAILFAST so mistakes surface immediately. In production, choose deliberately, because DROPMALFORMED can make rows vanish without any signal.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
VALIDATE 10 ROWS
FORMAT_OPTIONS (
'header' = 'true',
'delimiter' = ';',
'multiLine' = 'true',
'mode' = 'FAILFAST'
);Run this with VALIDATE first. If the preview rows look right, remove the VALIDATE clause and run the real load.
Corrupt and unreadable files
A truncated Parquet file or a half-written upload can fail the whole statement. The num_skipped_corrupt_files column of the result shows how many files were skipped when the run is set to ignore them. A non-zero value means some data is missing from the table. Because the run was otherwise successful, scheduled jobs tend to hide this. Add a check that fails the job, or raises an alert, when the value is above zero.
A related cause is a file that is still being written when the load starts. Have the producer write to a temporary name or folder and move the file into the landing path only when it is complete. Then COPY INTO never sees partial files.
A repeatable diagnosis routine
When a load misbehaves, work through the same steps each time.
- Read the error text, or the result row if there was no error.
- Run
LISTon the source path to confirm the files exist and the names match your pattern. - Run the statement again with
VALIDATE 10 ROWSand look at the preview. - Query
DESCRIBE HISTORYon the target to see what earlier runs did. - Check the identity that runs the statement, and its privileges.
- Fix one thing at a time, and validate again before the real load.
Changing several options at once makes it impossible to tell which change helped.
Exercise
A scheduled COPY INTO job succeeds every night, but today num_affected_rows is 0 even though the data team says new files arrived. LIST shows the new files under the path. Name two causes you would check next, and the command or option you would use for each.
Show answer
First, check the PATTERN. It is relative to the FROM path, so a full path or a wrong date prefix matches nothing. Test it with a VALIDATE 10 ROWS run. Second, check whether the files reuse names of files that were already loaded. COPY INTO tracks files by path and skips them. Compare the LIST output with the source_file values in the table and DESCRIBE HISTORY. If the producer overwrote old files, change the delivery so each file has a new name. Use 'force' = 'true' only for a deliberate one-time reload.
Exercise
After adding 'force' = 'true' to fix a missing-data problem, the table contains each order twice. How do you find the duplicated rows, and what is a safer design?
Show answer
Group by the business key and filter with HAVING COUNT(*) > 1, using a sourcefile column (from metadata.file_path) to see which files contributed each copy. A safer design loads into a raw table with COPY INTO without force, then uses MERGE to write deduplicated rows into the curated table, so reloads cannot create duplicates there.