Sub topic 14
Doing the same ingestion in SQL
This chapter shows how to write the Auto Loader ingestion from the earlier chapters in SQL. It assumes you know the cloudFiles source, checkpoints, schema inference, and the _metadata column. It also assumes you know basic Delta table and Unity Catalog volume paths.
Why use SQL for ingestion
The Python stream needs a readStream, a writeStream, a checkpoint path, and a trigger. A SQL streaming table packs the same work into one statement. You declare the target table and the query that feeds it. The platform manages the checkpoint and the incremental state, and it processes only new files on each refresh.
Under the hood, read_files uses Auto Loader when you wrap it in STREAM. The behavior you already know still applies: each file is processed once, and the schema is inferred and can evolve.
The streaming table statement
This statement ingests the same landing folder as the Python example.
CREATE OR REFRESH STREAMING TABLE orders_bronze AS
SELECT *, _metadata.file_path AS source_file
FROM STREAM read_files(
'/Volumes/main/raw/landing/orders',
format => 'json'
);Each part maps to something you configured in Python:
| SQL | Python equivalent |
|---|---|
| `STREAM read_files(...)` | `spark.readStream.format("cloudFiles")` |
| `format => 'json'` | `cloudFiles.format` option |
| `_metadata.file_path AS source_file` | `withColumn("source_file", col("_metadata.file_path"))` |
| `CREATE OR REFRESH STREAMING TABLE` | `writeStream.toTable(...)` with a managed checkpoint |
The STREAM keyword matters. Without it, read_files does a plain batch read and rereads every file each time.
Passing options
You pass other Auto Loader settings as named arguments to read_files. Reader options such as schemaHints and maxFilesPerTrigger go in the same list.
CREATE OR REFRESH STREAMING TABLE orders_bronze AS
SELECT *, _metadata.file_path AS source_file
FROM STREAM read_files(
'/Volumes/main/raw/landing/orders',
format => 'json',
schemaHints => 'order_id BIGINT, order_ts TIMESTAMP',
schemaEvolutionMode => 'addNewColumns'
);Note that you do not set a schema location or a checkpoint location. The platform stores them for the streaming table.
What happens on each refresh
A streaming table updates when you refresh it, either by running the statement again or through a scheduled pipeline. The refresh reads files that arrived since the last one, appends the rows, and records progress. This behaves like the availableNow trigger: it processes what is pending and then stops.
Common errors
- Forgetting
STREAM. The query becomes a batch read, and a streaming table definition fails or reprocesses data. - Running it in the wrong place.
CREATE STREAMING TABLEruns on a Databricks SQL warehouse or in a pipeline. It does not run in a classic notebook cell on arbitrary compute. - Expecting to edit the checkpoint. You cannot choose its path. To reprocess everything, do a full refresh of the table, which clears the saved progress and reloads all files.
- Changing the source path. A different path is a different source. Plan for a full refresh if you move the landing folder.
When to prefer each approach
Use SQL when the ingestion is a plain load with simple transformations and you want the platform to own the state. Use Python when you need custom checkpoint paths, foreachBatch logic, or fine control over triggers.
Exercise
A teammate writes FROM read_files('/Volumes/main/raw/landing/orders', format => 'json') inside a streaming table definition and finds that the table does not ingest incrementally. What is missing, and where does the checkpoint come from in the corrected version?
Show answer
The STREAM keyword is missing. Write FROM STREAM read_files(...). The platform creates and manages the checkpoint for the streaming table, so you do not set a checkpoint location yourself.