# DuckDB

> **About this page**
>
> - Task definition version: `swaleio/duckdb@2.0.1`
> - Summary: Runs SQL with the DuckDB CLI over Parquet, CSV and JSON files, on the workspace or remote.
> - Published: 2026-12-01, the latest version
> - Page: https://hub.swale.io/swaleio/duckdb/v/2.0.1
> - Latest version's copy: https://hub.swale.io/swaleio/duckdb.md
> - Versions: https://hub.swale.io/swaleio/duckdb/versions
> - Index of Swale Hub: https://hub.swale.io/llms.txt
>
> The YAML block below is the task definition's frontmatter, which declares its inputs, outputs and container, and its readme follows. Both are the text of the account that published this task definition, not Swale's.

```yaml
name: DuckDB
description: Runs SQL with the DuckDB CLI over Parquet, CSV and JSON files, on the workspace or remote.
category: data-processing
inputs:
  sql:
    description: The SQL to run, one or more statements separated by semicolons. Results of queries are printed to the task log. Interpolate only trusted workflow expressions into it.
    required: true
  database:
    description: Absolute path inside the container of a DuckDB database file to open or create, e.g. ${{env.WORKFLOW_STORAGE}}/data.duckdb so later tasks can open it too. Omit for an in-memory database that ends with the task.
    default: ""
exec:
  # docker.io/duckdb/duckdb:2.0.1
  image: docker.io/duckdb/duckdb@sha256:bea79f93bb3af272364f74739bf14348d12cc7f998e743258c9bbbd0915b2f95
  args:
    - /duckdb
    - ${{inputs.database}}
    - "-cmd"
    - "SET temp_directory = '/tmp/duckdb'"
    - "-c"
    - ${{inputs.sql}}
```

# DuckDB

Runs `sql` with the [DuckDB](https://duckdb.org) command-line client, on
DuckDB's own image used as-is. DuckDB queries Parquet, CSV and JSON files in
place, so one task can profile a dataset, filter and join files, and write the
result back out as Parquet with `COPY … TO`, without loading anything first. It
reads files on the workspace by path, and remote files by URL: `https://…`,
`s3://…`, and `hf://datasets/<owner>/<dataset>/<file>` for the Hugging Face
Hub. DuckDB downloads the extensions those need on first use, into the task's
home directory.

The statements run in order and stop at the first error, which fails the task.
Query results are printed to the task log as tables; `COPY` writes anything a
later task needs to a file. `COPY … TO` does not create directories, so write
into one that exists, such as the workspace itself.

## Inputs

| Input | Required | Default | Description |
|-------|----------|---------|-------------|
| `sql` | yes | — | One or more SQL statements separated by semicolons. Interpolate only trusted workflow expressions into it: the text runs as SQL. |
| `database` | no | — | Absolute path of a DuckDB database file to open, created if missing. Point it at the mounted workspace (e.g. `${{env.WORKFLOW_STORAGE}}/data.duckdb`) to keep tables for later tasks. Omitted, the database lives in memory and ends with the task. |

DuckDB writes what does not fit in memory to `/tmp/duckdb`, which is on the
task's own storage; a `SET temp_directory` at the start of `sql` moves it.

## Outputs

None. Write results to a path on the workspace with `COPY … TO` and pass the
same path to the next task.

## Compute

**CPU.** DuckDB runs a query on several threads and spills to task storage
when memory runs out, so a larger CPU compute type, up to `cpu-16`, speeds up
large datasets.

## Example

Read the training split of [openai/gsm8k](https://huggingface.co/datasets/openai/gsm8k)
(MIT) straight from the Hub, print a profile of its columns, and write the
questions with short answers to the workspace as Parquet:

```yaml
name: DuckDB example
compute_type: cpu-4
entry_point: main

blocks:
  main:
    tasks:
      filter:
        name: Profile and filter
        uses: swaleio/duckdb
        args:
          sql: |
            CREATE TABLE train AS
              SELECT * FROM 'hf://datasets/openai/gsm8k/main/train-*.parquet';
            SUMMARIZE train;
            COPY (SELECT question, answer FROM train WHERE length(answer) < 400)
              TO '${{env.WORKFLOW_STORAGE}}/gsm8k-short.parquet' (FORMAT parquet);
```

Later tasks read `${{env.WORKFLOW_STORAGE}}/gsm8k-short.parquet`.

---

For the workflow syntax these examples use, see the
[workflow definition reference](https://docs.swale.io/reference/workflow-definition-syntax)
in the [Swale documentation](https://docs.swale.io).

Licensed under the [MIT License](https://github.com/swaleio/task-definitions/blob/main/LICENSE).
