Sub topic 9
Checking a load first with VALIDATE
This chapter assumes you can write a basic COPY INTO statement and set FORMAT_OPTIONS. It also assumes you know how the target table's schema relates to the source files.
What VALIDATE does
A bad load is annoying to undo. If a file has a malformed row, a wrong delimiter, or a column that breaks a constraint, you only find out after the statement runs. The VALIDATE clause lets you find these problems first.
With VALIDATE, COPY INTO reads the source files and checks them against the target table. It returns the parsed rows to you and writes nothing to the table. The checks cover three things:
- Whether the data can be parsed with your format options.
- Whether the file schema matches the table schema, or can be merged into it.
- Whether the data satisfies the table's nullability and check constraints.
Previewing a sample of rows
Add VALIDATE n ROWS to a statement that is otherwise the same as your real load. This example reads and checks the first 10 rows.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
VALIDATE 10 ROWS
FORMAT_OPTIONS ('header' = 'true');The result is a table of at most 10 rows. Read it as you would read a SELECT result. Check that columns line up with the header, that numbers did not turn into NULL, and that dates were parsed as dates.
If you omit the row count and write VALIDATE ALL, Databricks checks all the data that would be loaded. This is slower on large folders, so start with a small sample.
A validate-then-load workflow
Because VALIDATE writes nothing, it also records nothing. No file is marked as loaded, so the real run afterwards still picks up every file. The workflow is to run the statement with VALIDATE, fix what you find, then remove the clause and run it again.
Common problems it exposes
| Symptom in the preview | Likely cause | Fix |
|---|---|---|
| All values in one column | Wrong `delimiter` | Set `'delimiter'` in `FORMAT_OPTIONS` |
| Header text appears as a data row | `header` not set | Add `'header' = 'true'` |
| Many `NULL` values | Type mismatch or wrong date format | Cast in a `SELECT` source or set format options |
| Error about missing or extra columns | Schema mismatch | Adjust the table, or use `mergeSchema` |
| Constraint violation | `NOT NULL` or `CHECK` rule broken | Clean the source or fix the transformation |
Validating a SELECT source
You can combine VALIDATE with a SELECT source. This checks your casts as well as the raw files, which is the best time to find that CAST(amount AS DECIMAL(10, 2)) fails on some values.
COPY INTO main.sales.orders
FROM (
SELECT
CAST(order_id AS BIGINT) AS order_id,
CAST(amount AS DECIMAL(10, 2)) AS amount,
_metadata.file_path AS source_file
FROM '/Volumes/main/sales/landing/orders/'
)
FILEFORMAT = CSV
VALIDATE 10 ROWS
FORMAT_OPTIONS ('header' = 'true');Summary
VALIDATE turns a risky load into a two-step process. Preview with a small row count, inspect the result, and fix the options. Then remove the clause and load for real.
Exercise
You run a COPY INTO statement with VALIDATE 10 ROWS and the preview looks correct. You then remove the clause and run the statement again. Will the files be skipped because the validation already "loaded" them? Explain why or why not.
Show answer
No. VALIDATE writes nothing to the table and does not record any files as loaded, so the real run loads every matching file. Only a real load makes COPY INTO track a file and skip it on later runs.
Exercise
A preview shows the entire row in a single column. Which FORMAT_OPTIONS setting do you check first?
Show answer
The delimiter. The files probably use a character other than a comma, such as a semicolon or a tab, so set 'delimiter' to match.