Sub topic 3
Checking and Troubleshooting COPY INTO Loads
Assume you know basic SQL, Delta tables, cloud storage, and Databricks permissions. COPY INTO loads files into a Delta table. The options below help you test, repeat, and monitor that load safely.
Checking a load first with VALIDATE
VALIDATE reads matching files and checks whether their rows can be loaded. It returns validation results without writing rows to the target table.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
VALIDATE 10 ROWS
FORMAT_OPTIONS ('header' = 'true');The number limits how many rows Databricks checks. Use this before a first load or after changing parsing options. Inspect errors such as invalid numbers, malformed CSV records, or missing columns.
Handling new columns with mergeSchema
If source files gain columns, the target Delta table may reject them. Set mergeSchema to add compatible columns during the load.
COPY INTO main.sales.orders_raw
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true', 'inferSchema' = 'true')
COPY_OPTIONS ('mergeSchema' = 'true');Use schema evolution deliberately. A misspelled source column can become a new target column instead of producing an obvious failure. Review the table schema after the load.
Reloading already-loaded files with force
COPY INTO records which files it has processed. Re-running the command normally skips those files. Set force to true when you intentionally need to reload them.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true')
COPY_OPTIONS ('force' = 'true');A forced load can create duplicate rows. Use it only after removing or isolating existing data, or when the target process is designed for duplicates.
COPY INTO or Auto Loader?
| Workload | Better choice |
|---|---|
| Occasional or scheduled batches with manageable file counts | `COPY INTO` |
| Continuous ingestion or very large, growing file collections | Auto Loader |
Auto Loader maintains scalable file discovery state. COPY INTO is simpler when a SQL warehouse or scheduled job can process the landing directory.
Scheduling and troubleshooting
Create a Databricks Job with a SQL task that runs COPY INTO on a schedule. The command result reports loaded files, skipped files, and errors. Store job logs and alert on failed runs.
Common fixes include:
- Check volume or external-location permissions.
- Verify the file format, delimiter, and header settings.
- Run
VALIDATEto identify bad records. - Inspect schema differences before enabling
mergeSchema. - Confirm whether
forcecaused duplicate rows.
Exercise
Which option should you use to test ten rows without inserting them, and which option reloads processed files?
Show answer
Use VALIDATE 10 ROWS to test rows. Use COPY_OPTIONS ('force' = 'true') to reload processed files.