Sub topic 9
Checking a load first with VALIDATE
This chapter assumes you can write a basic COPY INTO statement, set FORMAT_OPTIONS, and select files with PATTERN. It also assumes you know that a Delta table accepts only data that matches its schema and constraints. You need a Unity Catalog volume or external location with a few test files.
Why preview a load
A bulk load can fail late or load data you did not intend. A wrong delimiter can put whole rows into one column. A header flag you forgot can turn the header row into data. A pattern can match more files than you expected.
COPY INTO tracks the files it loads, so a mistaken load is not trivial to undo. You would have to delete the rows and then force a reload. VALIDATE lets you catch these problems before any data is written.
VALIDATE is an optional clause that makes COPY INTO read the source data and check it against the target table. It then returns the result and leaves the table unchanged.
What VALIDATE does
With VALIDATE, COPY INTO runs the same read and parse steps as a normal load. It then stops before the write. It checks these things:
- Whether the files can be parsed with the format options you gave.
- Whether the parsed columns match the table schema, or whether the schema would need to evolve.
- Whether nullability rules and check constraints on the table are met.
The statement returns a preview of the data instead of the usual load summary. Nothing is committed to the table, and the files are not recorded as loaded. A later real load will therefore still pick them up.
The following diagram shows the flow.
Syntax
The clause has two forms:
VALIDATE [ ALL | num_rows ROWS ]VALIDATE ALLchecks all the data that would be loaded. This is also what a bareVALIDATEdoes.VALIDATE n ROWSchecks only the firstnrows. It is faster and cheaper on large sources.
The clause goes after FILEFORMAT and before FORMAT_OPTIONS, as in this example:
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
VALIDATE 10 ROWS
FORMAT_OPTIONS ('header' = 'true');The result is a preview of up to 10 rows, shaped like the target table. If a row cannot be parsed or cast, the statement raises an error instead. Compare the preview to what you expect: column order, types, and whether the first row is data and not a header.
The rest of the statement is identical to the real load. Keep every other clause the same, including PATTERN, FILES, FORMAT_OPTIONS, and COPY_OPTIONS. Then delete only the VALIDATE line to run the real load. This way you test the exact statement you will run.
A worked example
Suppose the landing folder holds December files for orders, and the target table has these constraints:
CREATE TABLE IF NOT EXISTS main.sales.orders (
order_id BIGINT NOT NULL,
amount DECIMAL(10, 2),
order_ts TIMESTAMP
);
ALTER TABLE main.sales.orders
ADD CONSTRAINT amount_not_negative CHECK (amount >= 0);Start with a small, fast check of the files you plan to load:
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
PATTERN = '2026-12-*.csv'
VALIDATE 10 ROWS
FORMAT_OPTIONS ('header' = 'true', 'delimiter' = ',');Read the preview. If the columns look right, widen the check to the whole source:
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
PATTERN = '2026-12-*.csv'
VALIDATE ALL
FORMAT_OPTIONS ('header' = 'true', 'delimiter' = ',');The second check reads every matching file, so it can find problems that the first 10 rows hide, such as a null order_id or a negative amount further down. If it passes, run the load without VALIDATE:
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
PATTERN = '2026-12-*.csv'
FORMAT_OPTIONS ('header' = 'true', 'delimiter' = ',');Using VALIDATE with a SELECT source
When the source is a SELECT, validation covers the transformed result. That makes it a good way to test casts, as the following example shows:
COPY INTO main.sales.orders
FROM (
SELECT
CAST(order_id AS BIGINT) AS order_id,
CAST(amount AS DECIMAL(10, 2)) AS amount,
CAST(order_ts AS TIMESTAMP) AS order_ts
FROM '/Volumes/main/sales/landing/orders/'
)
FILEFORMAT = CSV
VALIDATE 20 ROWS
FORMAT_OPTIONS ('header' = 'true');Check the preview for values that became null. A cast of a value that does not fit the target type can give a null, depending on your SQL settings. In that case the check can pass while the data is wrong, so scan the preview columns for unexpected nulls.
What VALIDATE cannot tell you
A passing validation is evidence, not proof. Keep these limits in mind:
- Sampling.
VALIDATE 10 ROWSchecks only the first rows. A bad value in row 5,000 passes. UseVALIDATE ALLbefore important loads. - State can change. New files may arrive between the check and the real load, and they will be loaded too if they match. If you need the exact same file set, use
FILES = (...)with explicit names. - Business rules. It checks parsing, schema, nullability, and check constraints. It cannot know that an amount is implausible or that a date is in the wrong year. Add a check constraint if a rule matters.
- Cost.
VALIDATE ALLreads all the matching data. On a large source it costs roughly as much as the read half of a real load.
Common failure modes during validation
| Symptom | Likely cause | What to change |
|---|---|---|
| All values land in the first column | Wrong `delimiter` | Set `'delimiter'` in `FORMAT_OPTIONS` |
| A header value appears as a data row | Missing `'header' = 'true'` | Add the option |
| Error about a column not in the table | Source has a new column | Use `'mergeSchema' = 'true'` or fix the table |
| Not-null or check constraint error | Bad rows in the source | Clean the source or relax the rule |
| Empty preview | `PATTERN` or path matches no files, or the files were already loaded | Check the path and pattern, and the load history |
| Cast or parse error | Value does not fit the target type | Fix the `SELECT` cast or the file |
The empty-preview case has two causes that look alike. A pattern that matches nothing gives no rows. Files that the table has already loaded also give no rows, because COPY INTO skips them. If you expect data, list the folder first and check the table history.
A repeatable workflow
Use this sequence for any new source or any changed option:
- Write the full
COPY INTOstatement, with the finalPATTERNorFILES. - Add
VALIDATE 10 ROWSand read the preview. - Fix options until the preview matches the table.
- Change to
VALIDATE ALLfor a full check when the source is small or the load is important. - Remove
VALIDATEand run the load. - Read the load result to confirm that the number of rows matches your expectation.
Exercise
You run COPY INTO with VALIDATE 10 ROWS and it returns a clean preview. You then run the same statement without VALIDATE, and the load fails on a row with a null order_id, which the table declares NOT NULL. Why did the validation not catch it, and what should you have run?
Show answer
VALIDATE 10 ROWS checks only the first 10 rows, and the bad row was later in the data. VALIDATE ALL checks all the data that would be loaded, so it would have reported the nullability violation before any write. Because validation writes nothing and does not mark files as loaded, running it first is safe.
Exercise
A validated COPY INTO returns an empty preview even though you can see new CSV files in the folder. Give two possible causes and one way to tell them apart.
Show answer
One cause is that PATTERN or the path does not match the files. The other is that the table has already loaded those files, so COPY INTO skips them. To tell them apart, check the exact file names against the pattern and look at the table history for earlier COPY INTO operations that loaded those files. You can also test with a broader pattern to see whether any file is picked up.