Skip to content
◀ Blog

Query multiple Parquet files with DuckDB

Query multiple Parquet files in DuckDB using a filename pattern, combine exports with different columns, and trace an unexpected result to its source file.

Riley Hilliard3 min read

opendatasql

If you have a folder of monthly Parquet exports, DuckDB can run one SQL query across the whole folder. Open the DuckDB command-line client in the directory containing your files. For files with matching columns, this counts all the rows without importing anything into a database first.

SELECT count(*) FROM read_parquet('*.parquet');

The quotes belong in the SQL. DuckDB expands the filename pattern itself. Use 'exports/**/*.parquet' for files under a directory tree, or pass an explicit list when you want only certain months. A wildcard also picks up any backup or combined export that matches it. DuckDB’s guide to reading multiple files

Try it with two monthly exports

Start duckdb in an empty directory for this example. These two statements create small, fictional exports for a pair of bike counters. Each row gives one station’s total rides for the month in the filename. In February the operator added a column to record how it collected the count.

COPY (SELECT * FROM (VALUES ('Pier', 80), ('Hill', 20))
      AS t(station, rides)) TO 'rides-01.parquet';

COPY (SELECT * FROM (VALUES ('Pier', 90, 'sensor'), ('Hill', 30, 'manual'))
      AS t(station, rides, method)) TO 'rides-02.parquet';

union_by_name lines up columns by name across the exports. A row from a file without a column gets SQL NULL in that column. You can see which counts have a recorded method by grouping on the source filename.

SELECT parse_filename(filename) AS source_file, sum(rides) AS rides,
       count(method) AS rows_with_method
FROM read_parquet('rides-*.parquet', union_by_name = true, filename = true)
GROUP BY filename
ORDER BY filename;
source_file rides rows_with_method
rides-01.parquet 100 0
rides-02.parquet 120 2

January’s export doesn’t say how the operator collected those 100 rides. Filtering to WHERE method = 'sensor' would drop all of them, even if the operator used sensors both months. How DuckDB combines schemas

Find the file that changed the total

If you expected 220 rides and got 440, list the matched files before changing the arithmetic. This shows filenames even when an export contains no rows.

SELECT file FROM glob('rides-*.parquet') ORDER BY file;

A file called rides-combined.parquet would match the same pattern as the monthly originals. An explicit list keeps the analysis reproducible when someone adds files to the folder.

SELECT station, sum(rides) AS total_rides
FROM read_parquet(['rides-01.parquet', 'rides-02.parquet'],
                  union_by_name = true)
GROUP BY station
ORDER BY total_rides DESC;

Pier accounts for 170 rides and Hill for 50. That sum works because the two exports cover separate months. Some publishers release a full year-to-date snapshot each month. In that case February already contains January’s observations, and even a perfectly matched pair of schemas will double-count them if you add both files. For a year-to-date total through February, query the February snapshot alone before you turn the result into a chart.