Sub topic 10
Handling new columns with mergeSchema
Source systems add columns over time. A team adds a coupon_code field to its order export, and the next CSV file has one more column than your target table. This chapter explains what COPY INTO does in that case and how the mergeSchema copy option lets the table grow with the data.
This chapter assumes you can write a basic COPY INTO statement, you know how FORMAT_OPTIONS and COPY_OPTIONS differ, and you have seen schema inference on an empty target table. It also assumes you know that COPY INTO skips files it has already loaded.
What mergeSchema does
mergeSchema is a copy option, not a format option. You set it in COPY_OPTIONS:
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true', 'inferSchema' = 'true')
COPY_OPTIONS ('mergeSchema' = 'true');With mergeSchema set to true, COPY INTO compares the schema of the incoming files with the schema of the target Delta table. Columns that exist in the files but not in the table are added to the table. The load then writes the data into those new columns.
Three rules follow from this behavior:
- The change applies to the table, not only to the current load. After the statement finishes,
DESCRIBE TABLEshows the new column. - Rows already in the table get
NULLin the new column. Delta does not rewrite old data files to do this. - Columns that exist in the table but not in the incoming files stay in the table. Rows from those files get
NULLin them.
The option has a second use that you have already seen. In the first chapter on schema inference, you created an empty table with no columns and let COPY INTO build the schema from the files. That works because mergeSchema adds every inferred column to a table that has none.
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');The first load defines the columns. Every later load can extend them.
A worked example
Start with a table that has three columns:
CREATE OR REPLACE TABLE main.sales.orders (
order_id BIGINT,
amount DECIMAL(10, 2),
order_ts TIMESTAMP
);Suppose the landing volume holds 2026-12-01.csv with those three columns. A normal load picks it up:
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true')
COPY_OPTIONS ('mergeSchema' = 'true');The next day, the source system delivers 2026-12-02.csv. Its header is now order_id,amount,order_ts,coupon_code. Run the same statement again. COPY INTO skips 2026-12-01.csv because it has already loaded it, then reads the new file, sees the extra column, and adds it to the table.
DESCRIBE TABLE main.sales.orders;The output now lists four columns, with coupon_code last. Check the data:
SELECT order_id, coupon_code
FROM main.sales.orders
ORDER BY order_id;Orders from the first file show NULL for coupon_code. Orders from the second file show the real values.
The type of an added column comes from the data. With inferSchema set to true for CSV, COPY INTO samples values and picks a type. Without it, CSV columns are read as STRING. For JSON and Parquet, the type comes from the file itself.
What happens without mergeSchema
Without the option, the target table's schema is fixed. A new column in the source is not added to the table. What happens to the data depends on the file format and on how the columns line up, and the load can either ignore the extra column or fail. Do not rely on either outcome. If you need to know, preview the load with VALIDATE against a copy of the table.
This matters for CSV in particular. A silent drop is the worst case, because the load succeeds and the new field never reaches your table. If a source can add columns without warning, setting mergeSchema is the safer default for raw landing tables.
The decision flow looks like this:
Existing files are not reloaded
A common surprise: adding mergeSchema to your statement does not backfill a column for files that were already loaded.
Say you loaded 2026-12-02.csv earlier without mergeSchema, and the load dropped coupon_code. You now add the option and re-run. COPY INTO still treats 2026-12-02.csv as loaded, so it skips it. The column is never added from that file, and the values are not recovered.
To fix this, you have two choices:
- Reload the affected files with the
forceoption. This loads every matching file again, so it duplicates rows unless you first remove the earlier rows or load into a fresh table. - Load into a new table with
mergeSchemafrom the start, then swap the new table in.
Because of this, set mergeSchema before the first file with a new column arrives, not after.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
PATTERN = '2026-12-02.csv'
FORMAT_OPTIONS ('header' = 'true')
COPY_OPTIONS ('mergeSchema' = 'true', 'force' = 'true');If you run this after the table already holds rows from 2026-12-02.csv, those rows now appear twice. Delete them first, for example with a DELETE filtered on the order IDs in that file.
Using mergeSchema with a SELECT source
When the source is a SELECT, you choose the columns yourself. The statement can only add columns that your SELECT returns. If you list columns explicitly, a new column in the files is never selected, so it is never added.
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,
coupon_code,
_metadata.file_path AS source_file
FROM '/Volumes/main/sales/landing/orders/'
)
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true')
COPY_OPTIONS ('mergeSchema' = 'true');Here coupon_code and source_file are both added to the table if it lacks them. The source_file column comes from the _metadata column, which is a name you chose in the SELECT, so it is added just like any other new column.
You have two ways to pick up future columns automatically. Use SELECT * along with your casts, or review the file headers on a schedule and update the list when new fields appear. The first is convenient but gives up the explicit control that the SELECT form exists to provide.
Limits and failure modes
mergeSchema adds columns. It does not rewrite your table in other ways, so plan for these cases.
- Type conflicts. If an existing column is
BIGINTand a new file holds text such asA-1001in that column,mergeSchemadoes not change the column type. Expect the load to fail or the value to be rejected, and treat it as a data problem to fix upstream or with a cast in aSELECTsource. - Renamed columns. A rename looks like one dropped column and one new column. You get the new column added, and the old column stays in the table with
NULLfor new rows. The table does not link the two. - Dropped columns. Columns that disappear from the source are never removed from the table.
- Column order. Added columns go at the end of the table. Do not depend on position in downstream queries. Name columns explicitly.
- Permissions. Changing a table's schema needs more than reading it. The user or service principal running the load needs write access on the target table. In Unity Catalog, that means
MODIFYon the table, plus the usual access to the source location. - Downstream readers. Views with
SELECT *, exported schemas, and BI models can behave differently when a column appears. Check them when you enable schema growth on a table many people read.
Choosing when to enable it
| Situation | Recommendation |
|---|---|
| Raw or bronze table fed by a source that changes often | Set `mergeSchema` to `true` |
| Empty table created to let `COPY INTO` infer the schema | Set `mergeSchema` to `true` |
| Curated table with a contract on its columns | Leave it off and change the schema with `ALTER TABLE` |
| `SELECT` source with an explicit column list | Add new columns to the list, then keep `mergeSchema` on |
| Files were loaded before the column was added | Reload with `force` after cleaning the table, or rebuild |
If the schema of your source changes constantly and you need to track unexpected fields rather than only add them, look at Auto Loader. It offers schema evolution modes and a rescued data column, which the later chapter on choosing between the two covers.
Exercise
A table main.sales.orders already holds rows loaded from 2026-12-01.csv and 2026-12-02.csv. The second file contains a new column, coupon_code, but you loaded it without mergeSchema and the column was not added. You now re-run the same COPY INTO with COPY_OPTIONS ('mergeSchema' = 'true'). Will coupon_code appear in the table with its values? Why, and what would you do to get the values?
Show answer
No. COPY INTO tracks the files it has loaded, so it skips 2026-12-02.csv on the re-run, and the new column is never read from it. To get the values, remove the rows that came from that file, then reload it with COPY_OPTIONS ('mergeSchema' = 'true', 'force' = 'true') and a PATTERN or FILES list that matches only that file. Without removing the old rows first, the force reload duplicates them.
Exercise
Your COPY INTO uses a SELECT source that lists order_id, amount and order_ts. A new file adds coupon_code, and mergeSchema is true. Is coupon_code added to the table? What do you change so that it is?
Show answer
No. The SELECT returns only the columns you listed, so coupon_code is never part of the data being written and mergeSchema has nothing to add. Add coupon_code to the SELECT list, or use SELECT * alongside your casts, and keep mergeSchema set to true.