Sub topic 10
Handling new columns with mergeSchema
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 know that COPY INTO infers a schema from your files. Here you will see what happens when new files carry columns the target table lacks, and how mergeSchema handles that.
The problem: source files change
Source systems add columns over time. Suppose main.sales.orders_raw was created from files with order_id and amount. A new file arrives with a third column, coupon_code. By default the table schema is fixed, so the load does not silently widen the table. The extra column causes a schema mismatch and the statement fails.
Setting mergeSchema to true tells COPY INTO to add the new columns to the table as part of the load.
Two places to set mergeSchema
The option appears in two clauses, and they do different jobs.
| Clause | What it controls |
|---|---|
| `FORMAT_OPTIONS ('mergeSchema' = 'true')` | When inferring the schema, combine the columns from all files read, not just a sample from one. |
| `COPY_OPTIONS ('mergeSchema' = 'true')` | Add columns that are missing from the target table, so the table schema evolves. |
For evolving a table you normally set both.
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', 'mergeSchema' = 'true')
COPY_OPTIONS ('mergeSchema' = 'true');After the load, run DESCRIBE TABLE main.sales.orders_raw to confirm coupon_code now exists.
What happens to existing rows
Added columns are nullable. Rows loaded before the change get NULL for the new column. Rows from the new file get the real values.
Files that COPY INTO already loaded are not read again, so their rows never gain a value for the new column. If you need that, you must reload them with force, which can create duplicates. Plan for it before you reach for it.
Common errors
- Setting only one of the two options. With only the
COPY_OPTIONSsetting, inference can miss a column that appears only in some files. With only theFORMAT_OPTIONSsetting, the table is never changed and the load still fails. - Expecting type changes.
mergeSchemaadds columns. It does not rewrite an existing column fromINTtoSTRING. A type conflict still fails the load. - Evolving with a fixed SELECT list. If the source is a
SELECTthat names its columns, only those columns exist in the load, so a new source column is never seen until you add it to the list. - Letting drift go unnoticed. Automatic evolution means a typo in a header, such as
ammount, becomes a new column. Preview unfamiliar files first withVALIDATE, and review the schema after loads.
Choosing when to enable it
Use mergeSchema for raw landing tables, where capturing every field matters more than strict structure. Keep it off for curated tables with a defined contract. A common pattern is to evolve the raw table automatically, then add new columns to downstream tables deliberately.
Exercise
A table was loaded from files with columns order_id and amount. A new file adds coupon_code. You run COPY INTO with only FORMAT_OPTIONS ('mergeSchema' = 'true'). What happens, and what do you change?
Show answer
The table schema is not changed, so the load still fails with a schema mismatch. The FORMATOPTIONS setting only affects schema inference across files. Add `COPYOPTIONS ('mergeSchema' = 'true') so the missing column is added to the table. Rows loaded earlier will read NULL for coupon_code`.