Sub topic 3
Your first COPY INTO statement
This chapter assumes you know basic SQL, understand Delta tables, can reach a Databricks workspace with a SQL warehouse or cluster, and know what CSV, JSON and Parquet files are. It also assumes your source files already sit in a Unity Catalog volume or external location that you can read.
What the statement does
COPY INTO reads files from a source path and appends their rows to a Delta table. Databricks records which files it has loaded, so a second run skips them. Later chapters cover that tracking in detail. Here the goal is to write a statement that runs.
The three required parts
Every basic statement has three parts.
- Target: the Delta table that receives the rows.
- Source: a directory path, or a SELECT over a path, that holds the files.
- FILEFORMAT: the format of the source files, such as
CSV,JSON,PARQUET,AVRO,ORCorTEXT.
Two optional clauses shape how files are read and loaded. FORMAT_OPTIONS controls parsing, and COPY_OPTIONS controls the load itself.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true', 'delimiter' = ',');The target main.sales.orders uses the three-level Unity Catalog name: catalog, schema, table. The source is a volume path. With 'header' = 'true', the first row of each file is read as column names instead of data.
How the flow works
Letting COPY INTO create the columns
The target table must exist before the load. If you do not want to define columns by hand, create an empty table with no schema and let COPY INTO fill it in.
CREATE TABLE IF NOT EXISTS main.sales.orders_raw;
COPY INTO main.sales.orders_raw
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true', 'inferSchema' = 'true')
COPY_OPTIONS ('mergeSchema' = 'true');Two settings make this work:
'inferSchema' = 'true'makes Databricks sample the files to guess column names and types.'mergeSchema' = 'true'lets the table take on the inferred columns, because the table starts with none.
Without inferSchema, CSV columns are read as strings. Inference needs an extra pass over the data, so it costs time on large inputs.
Reading the result
A successful run returns one row of counts. The columns include num_affected_rows, num_inserted_rows and num_skipped_corrupt_files. On a first run, num_inserted_rows equals the number of rows in the files. Run the same statement again with no new files and it returns zero inserted rows, because every file is already recorded as loaded.
Common first-run errors
| Symptom | Likely cause |
|---|---|
| Permission denied on the path | You lack `READ VOLUME` on the volume, or `READ FILES` on the external location. |
| Table or view not found | The target table does not exist. Create it first. |
| Columns shifted or named `_c0`, `_c1` | `header` was not set to `true`. |
| Zero rows inserted on the first run | The path is wrong, or the files do not match the `FILEFORMAT`. |
| Schema mismatch error | The file columns differ from the table and `mergeSchema` is not enabled. |
Exercise
You run a COPY INTO statement against a folder of CSV files and the result shows zero inserted rows. You then run it a second time on the same folder, with no new files, and again see zero inserted rows. For which of the two runs should you investigate the cause, and why?
Show answer
Investigate the first run. Zero inserted rows after the second run is expected, because the files were already loaded and are skipped. Zero rows on the first run means something is wrong, such as an incorrect path, files that do not match FILEFORMAT, or files that were already loaded by an earlier statement into the same table.