Sub topic 7
Target tables and schema inference
This chapter assumes you can write basic COPY INTO statements, know how to point them at a volume or external location, and understand Delta tables and common file formats such as CSV and JSON. Here you learn how the target table's schema affects a load, and how Databricks infers a schema when the table has none.
Two kinds of target table
COPY INTO writes to an existing Delta table. The table can be in one of two states.
- Defined schema. The table has columns and types.
COPY INTOmaps the incoming data onto them. - Empty schema. The table was created with no columns.
COPY INTOinfers a schema from the source files and adds it to the table.
You will usually use a defined schema in production, because the types are explicit and reviewed. An empty schema is convenient when you are exploring a new source.
Loading into an empty table
Create the table without columns, then load with schema inference and mergeSchema turned on:
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');Each option has a separate job:
header = truereads the first row of each file as column names. Without it, columns get generic names such as_c0.inferSchema = truesamples the data to guess types such asBIGINT,DOUBLEorTIMESTAMP.mergeSchema = trueletsCOPY INTOadd the inferred columns to the table. Without it, loading into a table with no columns fails.
What inference does for each format
| Format | Inference behavior |
|---|---|
| CSV | Every column is a string unless you set `inferSchema = true`. |
| JSON | Types are inferred by default. Nested objects become structs. |
| Parquet and Avro | The schema is read from the file metadata. No guessing is needed. |
Inference for CSV and JSON reads a sample of the data, so it can guess wrong. A column that holds integers in the sampled files may contain a value like N/A in a later file. A column of ZIP codes may be inferred as an integer, which drops leading zeros.
Loading into a defined schema
When the table already has columns, COPY INTO converts the source values to the table's types. Matching is by column name for formats such as CSV with a header, JSON and Parquet. A value that cannot be converted, such as the text abc for a BIGINT column, makes the load fail.
CREATE TABLE IF NOT EXISTS main.sales.orders (
order_id BIGINT,
amount DECIMAL(10, 2),
order_ts TIMESTAMP
);
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');Without mergeSchema, a source file with extra columns does not change the table. The later chapter on schema evolution covers that case.
The decision in one picture
Common errors
- Every column is a string. You loaded CSV into an empty table without
inferSchema. Add the option, or define the table first. - The load fails on an empty table.
mergeSchemais missing fromCOPY_OPTIONS. - A cast fails partway through. The table type is stricter than the data. Check the offending file, or load the column as a string and cast it later.
- Column names do not match. With a header, names are matched, so a typo in the header leaves the column empty or fails the load. Check spelling and case.
Exercise
You run COPY INTO on a CSV file into a table created with CREATE TABLE main.sales.t;. You set header = true and mergeSchema = true but not inferSchema. What types do the columns get, and why?
Show answer
All columns are strings. CSV has no type information, and without inferSchema = true Databricks does not guess types, so each column is read as a string. mergeSchema only allows the columns to be added to the table. It does not affect their types.
Exercise
Why might you define the target table's schema yourself instead of relying on inference in production?
Show answer
Inference samples the data and can guess wrong, for example turning ZIP codes into integers or failing on a later file with unexpected values. An explicit schema makes the types predictable and reviewed, and it makes bad data fail the load instead of silently changing the table.