DuckDB
swaleio/[email protected]
DuckDB
Runs sql with the DuckDB 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 (MIT) straight from the Hub, print a profile of its columns, and write the questions with short answers to the workspace as Parquet:
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 in the Swale documentation.
Licensed under the MIT License.