@duckdb-analytics

Query, aggregate, and transform Parquet, Arrow, and Polars datasets with DuckDB. Verify Hive partition pruning, tune memory spill limits, and isolate worker cursors.

DuckDB Analytics

When to Use

  • Run analytical SQL over local or partitioned Parquet, CSV, or JSON files without provisioning a database server.
  • Combine DuckDB SQL queries with Polars or PyArrow tables via zero-copy Arrow exchange.
  • Debug slow scans, out-of-memory errors, or thread-concurrency issues in embedded DuckDB pipelines.

Prerequisites

Identify the DuckDB version (duckdb.__version__ or SELECT version()), host language binding, available RAM and scratch disk, and input file layout (single files, glob patterns, or Hive-partitioned directories). Inspect how connections, cursors, and registered DataFrames are shared across threads, and confirm read-only vs write access before attaching persistent .duckdb files or writing Parquet output.

Verify partition and filter pushdown

  1. Pass explicit options such as hive_partitioning = true and union_by_name = true (only when schema drift across files is expected) to read_parquet. Bind query paths and filter values with positional ($1) or named ($region) parameters instead of interpolating untrusted strings into SQL.
  2. Run EXPLAIN (FORMAT JSON) before executing expensive aggregations over large directory globs (avoid matching exact boxed ASCII line wrapping, which varies across DuckDB releases). Inspect the READ_PARQUET operator's extra_info to confirm File Filters and Scanning Files show partition pruning and that row-level predicates appear as Filters inside READ_PARQUET rather than only after a full scan.
  3. Project only the columns required by the query. Avoid SELECT * on wide Parquet datasets, since DuckDB's columnar reader skips unreferenced column chunks.

Exchange DataFrames cleanly and isolate cursors

  • In Python DuckDB 2.x, use the Relation API (con.sql(query, params=[...]).pl() or .to_arrow_table()) for zero-copy Polars/Arrow output rather than calling .to_arrow_table() on a DB-API con.execute(...) cursor, which makes an extra copy.
  • Do not share a single DuckDBPyConnection concurrently across threads. Create an isolated worker cursor via con.cursor() per thread.
  • Remember that cursor.register("name", df) binds a Python DataFrame only on that cursor. Register DataFrames on the worker cursor executing the query, or materialize shared data into a DuckDB table when multiple cursors need it.

Bound memory, spill disk, and file writes

  • Set explicit SET memory_limit = '...', SET temp_directory = '...', and SET threads = N in container or CI environments so DuckDB spills predictably instead of triggering the OS OOM killer.
  • Open persistent database files with read_only = True when only querying so multiple reader processes can attach safely.
  • When exporting partitioned datasets with COPY (...) TO '...' (FORMAT PARQUET, PARTITION_BY (...)), write into a staging directory or check OVERWRITE_OR_IGNORE / ALLOW_OVERWRITE explicitly rather than mutating live output paths in place.

Example

Query a Hive-partitioned Parquet order dataset, prove DuckDB prunes unneeded partition files, and join Polars DataFrames across isolated cursors without extra Arrow copies.

Run the worked Parquet and Polars test to verify EXPLAIN file pruning (Scanning Files: 2/4), zero-warning sql(..., params=...).pl() output, and cursor-local registration isolation.

Limitations

  • DuckDB is designed for OLAP queries and batch analytics, not high-concurrency OLTP row updates across multiple writer processes.
  • Remote object-store scans (httpfs / s3:// / gs://) require extension loading and explicit credentials; test network and permission behavior in the target environment.
  • DuckDB 1.x and 2.x differ in some Python cursor/relation convenience methods (such as .pl() on relations vs DB-API connections); check the installed version before writing bindings.

Safety and Side Effects

Inspect file globs and output paths before running COPY ... TO. Use read_only = True when inspecting existing .duckdb files, avoid loading unreviewed third-party community extensions, and never interpolate untrusted user input into SQL strings or file paths. Keep credentials out of query strings and logs.