A vendor sends five CSV files, one per stock, 33,493 rows in all. Before DuckDB, the first hour went into loading them into something you could query. Here you point SQL at the files and read the answer, and when the same data is stored as Parquet the query touches only the columns and blocks it needs.
The idea in plain words
Where we are. Topic 1 queried data that was already in Python. This one queries files directly. The next topic joins two time series that do not share timestamps, with DuckDB's ASOF JOIN.
Two savings a columnar file allows. Column pruning reads only the columns the query names. Predicate pushdown uses each block's stored minimum and maximum to skip blocks that cannot hold a matching row, before reading them.
A file is a table
# Put a file name where a table name goes, and ask DuckDB what it foundprint(csv_summary(con, "data/KO.csv"))print(con.sql("DESCRIBE SELECT * FROM read_csv_auto('data/KO.csv')"))1
(6699, datetime.date(2000, 1, 3), datetime.date(2026, 8, 21)) ┌─────────────┬─────────────┬─────────┬─────────┬─────────┬─────────┐ │ column_name │ column_type │ null │ key │ default │ extra │ │ varchar │ varchar │ varchar │ varchar │ varchar │ varchar │ ├─────────────┼─────────────┼─────────┼─────────┼─────────┼─────────┤ │ date │ DATE │ YES │ NULL │ NULL │ NULL │ │ open │ DOUBLE │ YES │ NULL │ NULL │ NULL │ │ high │ DOUBLE │ YES │ NULL │ NULL │ NULL │ │ low │ DOUBLE │ YES │ NULL │ NULL │ NULL │ │ close │ DOUBLE │ YES │ NULL │ NULL │ NULL │ │ volume │ BIGINT │ YES │ NULL │ NULL │ NULL │ └─────────────┴─────────────┴─────────┴─────────┴─────────┴─────────┘
- 1DuckDB sniffed the delimiter, the header and each column's type from the file.
A glob reads many files as one table. Adding filename = true gives every row a column with the file it came from, so one query can count the rows of every file at once.
# Five files, one queryprint(rows_per_file(con, "data/*.csv"))
{'JNJ.csv': 6699, 'JPM.csv': 6698, 'KO.csv': 6699, 'AAPL.csv': 6699, 'XOM.csv': 6698}Parquet: smaller, and read only in part
# Write a CSV as zstd Parquet and compare sizesimport osprint(csv_to_parquet(con, "data/JPM.csv", "jpm.parquet"), "rows written")print("CSV bytes:", os.path.getsize("data/JPM.csv"), "| Parquet bytes:", os.path.getsize("jpm.parquet"))print(con.sql("SELECT row_group_id, row_group_num_rows, compression FROM parquet_metadata('jpm.parquet') WHERE path_in_schema = 'close'"))1
6698 rows written CSV bytes: 340314 | Parquet bytes: 144313 ┌──────────────┬────────────────────┬─────────────┐ │ row_group_id │ row_group_num_rows │ compression │ │ int64 │ int64 │ varchar │ ├──────────────┼────────────────────┼─────────────┤ │ 0 │ 6698 │ ZSTD │ └──────────────┴────────────────────┴─────────────┘
- 1
parquet_metadatareads the file's footer: how many row groups, and how each column is compressed.
EXPLAIN shows what a query will read. For a Parquet file with four columns, a query for two columns and one symbol lists just those columns as projections and the symbol and date tests as filters, handed to the Parquet reader instead of applied afterwards.
# Read two columns of one symbol; the plan shows what was pushed into the file readercon.execute("COPY (SELECT symbol, date, close, volume FROM bars) TO 'bars.parquet' (FORMAT parquet)").fetchall()print(parquet_slice(con, "bars.parquet", ["date", "close"], "JPM", "2026-08-19"))print(con.sql("EXPLAIN SELECT date, close FROM read_parquet('bars.parquet') WHERE symbol = 'JPM' AND date >= '2026-08-19'").fetchall()[0][1])
[(datetime.date(2026, 8, 19), 357.26), (datetime.date(2026, 8, 20), 351.55), (datetime.date(2026, 8, 21), 351.58)] ╭─ Read Parquet ───────────────────────╮ │ Function: READ_PARQUET │ │ Projections: date, close │ │ Filters: symbol = 'JPM', │ │ date >= '2026-08-19'::DATE │ │ ~6,698 rows │ ╰──────────────────────────────────────╯
A file name goes into the SQL text as a string literal, so a name with a single quote (it's data/prices.csv) breaks a hand-built statement. Pass every path through quote() and every column name through ident(), as the earlier SQL topics did for values.
What the tasks ask
- 1`csv_summary` and `rows_per_file`Query a CSV and a glob of CSVs with
read_csv_autoandread_csv; DuckDB reads the files, not Python. - 2`csv_to_parquet`
COPY (query) TO file (FORMAT parquet, COMPRESSION zstd), returning the row count. - 3`parquet_slice`Select only the requested columns, filter on symbol and date, and reject an unknown column with a clear error.
Lab · what's graded
4 tasks · 100 pts- A CSV as a table:
csv_summary25 pts - A glob of files:
rows_per_file25 pts - CSV to Parquet:
csv_to_parquet25 pts - Read only what you need:
parquet_slice25 pts