Sub topic 7
Controlling types with schema hints
This chapter assumes you can write a Spark Structured Streaming job, you know what a Delta table is, and you have already seen Auto Loader infer a schema into a cloudFiles.schemaLocation. Here you will learn how to override that inference for the columns where it guesses wrong.
Why inference needs help
For JSON and CSV, Auto Loader samples your files to build a schema. By default, it reads every column as a string. This is a deliberate choice. A string column never fails because one file has "12" and another has "twelve".
You can turn on type inference with cloudFiles.inferColumnTypes. Auto Loader then picks types such as BIGINT, DOUBLE, or TIMESTAMP from the sampled data. This is convenient, but the sample may not match the rest of your data. A column of IDs that looks like integers in the sample can contain a long value later. A date-like string may be a free-text field. Inference can also pick a type you do not want, such as DOUBLE for a money column.
Schema hints let you pin the types of specific columns while inference handles the rest.
Setting hints
Pass a comma-separated list of column definitions to cloudFiles.schemaHints. The syntax is the same as a SQL column list: a name, then a type.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.option("cloudFiles.inferColumnTypes", "true")
.option("cloudFiles.schemaHints", "order_id BIGINT, order_ts TIMESTAMP")
.load("/Volumes/main/raw/landing/orders"))Here order_id is always BIGINT and order_ts is always TIMESTAMP. Every other column keeps its inferred type. Hints apply to columns that are not in the sampled data as well. If a hinted column is missing from the files, it appears in the schema with the hinted type and is filled with nulls.
Hints can also describe nested fields and complex types. For example, you can declare an array or a struct such as items ARRAY<STRUCT<sku: STRING, qty: INT>>, or give a single nested field its own type.
How hints interact with the schema location
Auto Loader stores the inferred schema in schemaLocation. Hints are applied on top of that stored schema each time the stream starts. This produces two practical rules.
- You can change a hint and restart the stream. The new hint is used for the columns it names.
- A hint cannot repair data that already conflicts with it. If a value cannot be cast to the hinted type, it does not stop the stream. It goes to the
_rescued_datacolumn, which you met in the previous chapter.
Choosing which columns to hint
Hinting everything defeats the purpose of inference. A short list works better. Hint columns that fit one of these cases:
| Case | Why hint it | Example hint |
|---|---|---|
| Identifiers | Inference may pick `INT`, which overflows later | `order_id BIGINT` |
| Timestamps | Strings that look like dates need an explicit type | `order_ts TIMESTAMP` |
| Money | `DOUBLE` loses precision | `amount DECIMAL(18,2)` |
| Leading zeros | Codes such as `007` become `7` if read as numbers | `zip_code STRING` |
The last row matters for CSV. Without a hint, a column of codes with leading zeros can lose its zeros if it is inferred as a number.
A common error
If you change a hint and see no effect, check that the stream is using the same schemaLocation. Auto Loader reads the stored schema first. A new schemaLocation starts inference from scratch, and that can change other columns too. Also confirm the hint names match the column names in the files exactly, including case handling in your session.
Exercise
A CSV column account_code holds values like 00417. Inference with cloudFiles.inferColumnTypes set to true turns it into a number, and the leading zeros disappear. What option fixes this without turning off inference for other columns?
Show answer
Add account_code STRING to cloudFiles.schemaHints, for example .option("cloudFiles.schemaHints", "account_code STRING"). The column keeps its text form, and all other columns keep their inferred types.