Duckdb motherduck local olap

From CraftOS-dev/craftbot-agent-bundles · View interactive version

Duckdb motherduck local olap is an AI agent skill from the GitHub repository CraftOS-dev/craftbot-agent-bundles. DuckDB: https://duckdb.org/docs/

How to use this skill

Skill file

<!--

DuckDB: https://duckdb.org/docs/

MotherDuck: https://motherduck.com/docs/

Companion: role.md → "Capability reference → DuckDB" + "SQL refactor playbook"

-->

DuckDB + MotherDuck — local-first OLAP

Use DuckDB as the analyst's "embedded warehouse" for prototyping queries before promotion to Snowflake/BigQuery, for analyzing Parquet/CSV/JSON files in-place, and for joining cloud + local data via MotherDuck's hybrid execution. Drop-in SQL, single binary, zero infrastructure.

When to use

Defer warehouse-grade queries to snowflake-bigquery-databricks-warehousing. Defer dataframe-level transforms to polars-pandas-modern-dataframes.

Setup

# CLI binary
brew install duckdb               # macOS
# OR
curl -sSL https://install.duckdb.org | sh    # Linux
duckdb --version                  # verify 1.x

# Python library
pip install duckdb pyarrow         # + pyarrow for zero-copy Arrow integration

# dbt-duckdb adapter
pip install dbt-duckdb

# MotherDuck — cloud-managed DuckDB
# Sign up at app.motherduck.com → copy token
export motherduck_token="..."     # env var DuckDB reads automatically

Auth requirements:

Common recipes

Recipe 1 — REPL + one-shot query

# Launch REPL on in-memory DB
duckdb
# > SELECT 1; .quit

# Launch on persistent file
duckdb ~/analytics.duckdb

# One-shot from shell
duckdb -c "SELECT count(*) FROM 'data/sales/*.parquet'"

# Run a SQL script
duckdb analytics.duckdb < query.sql

Recipe 2 — Query Parquet / CSV / JSON in-place

-- Single file
SELECT customer_id, sum(amount) AS revenue
FROM 'data/orders.parquet'
GROUP BY 1
ORDER BY revenue DESC
LIMIT 10;

-- Glob across many files (parallel + pushdown)
SELECT date_trunc('month', order_date) AS month, sum(amount) AS revenue
FROM 'data/orders/year=2025/*.parquet'
GROUP BY 1;

-- Read with explicit schema + CSV options
SELECT * FROM read_csv(
    'sales.csv',
    delim=',',
    header=true,
    columns={'id':'INTEGER','amount':'DOUBLE','date':'DATE'},
    sample_size=-1
);

-- JSON (newline-delimited)
SELECT * FROM read_json_auto('events.jsonl', format='newline_delimited');

-- All files in S3
INSTALL httpfs; LOAD httpfs;
SET s3_region='us-east-1';
SET s3_access_key_id='...';
SET s3_secret_access_key='...';
SELECT count(*) FROM 's3://my-bucket/data/*.parquet';

Recipe 3 — Python integration (zero-copy)

import duckdb
import pandas as pd
import polars as pl

# Query a pandas DataFrame directly — DuckDB sees it as a table
df = pd.read_csv("sales.csv")
result = duckdb.query("SELECT region, sum(amount) FROM df GROUP BY 1").df()

# Query a Polars DataFrame
pl_df = pl.read_csv("sales.csv")
result = duckdb.query("SELECT region, sum(amount) FROM pl_df GROUP BY 1").pl()

# Persistent connection
con = duckdb.connect("analytics.duckdb")
con.execute("""
    CREATE OR REPLACE TABLE orders AS
    SELECT * FROM 'data/orders/*.parquet'
""")
con.execute("CREATE INDEX idx_orders_customer ON orders(customer_id)")
df_out = con.execute("SELECT * FROM orders WHERE customer_id = 42").pl()
con.close()

Recipe 4 — Materialize a heavy query to a local table

-- One-time materialization of an expensive aggregation
CREATE TABLE customer_summary AS
SELECT
    customer_id,
    min(order_date) AS first_order,
    max(order_date) AS last_order,
    count(*) AS order_count,
    sum(amount) AS total_spend,
    avg(amount) AS avg_order_value
FROM 's3://bucket/orders/*.parquet'
GROUP BY customer_id;

-- Persist as Parquet for downstream consumers
COPY customer_summary TO 'output/customer_summary.parquet' (FORMAT PARQUET, COMPRESSION ZSTD);

Recipe 5 — EXPLAIN + query profile

-- Logical plan
EXPLAIN
SELECT customer_id, sum(amount) FROM orders GROUP BY 1;

-- Execution stats (run + measure)
EXPLAIN ANALYZE
SELECT customer_id, sum(amount) FROM orders GROUP BY 1;

-- Inspect storage
PRAGMA storage_info('orders');
PRAGMA show_tables;
DESCRIBE orders;
SUMMARIZE orders;          -- column stats: min/max/distinct/null counts

Recipe 6 — dbt-duckdb (test models locally before warehouse)

profiles.yml:

analytics:
  target: dev
  outputs:
    dev:
      type: duckdb
      path: ./analytics.duckdb
      threads: 4
      extensions: [httpfs, parquet]

Then dbt build. Iterate locally in seconds. CI runs against the same DuckDB target.

Recipe 7 — MotherDuck hybrid execution

-- Connect from DuckDB CLI/Python to MotherDuck
ATTACH 'md:my_db';        -- env var motherduck_token must be set
USE my_db;

-- Now queries run in cloud
SELECT count(*) FROM big_cloud_table;

-- Hybrid: join cloud and local
SELECT c.customer_id, c.name, sum(o.amount)
FROM md:my_db.dim_customers c
JOIN 'local_orders.parquet' o ON c.customer_id = o.customer_id
GROUP BY 1, 2;

-- Share a db with a teammate
CREATE SHARE my_db_share FROM my_db;
GRANT USAGE ON SHARE my_db_share TO USER 'alice@company.com';

Recipe 8 — Window + QUALIFY + PIVOT (full SQL coverage)

-- DuckDB supports QUALIFY, PIVOT, EXCLUDE/REPLACE
SELECT * EXCLUDE (raw_payload, _loaded_at)
FROM events
QUALIFY row_number() OVER (PARTITION BY user_id ORDER BY event_at DESC) = 1;

-- PIVOT (native)
PIVOT orders
ON status
USING sum(amount)
GROUP BY order_month;

-- Recursive CTE (date spine)
WITH RECURSIVE dates(d) AS (
    SELECT DATE '2025-01-01'
    UNION ALL
    SELECT d + INTERVAL '1 day' FROM dates WHERE d < DATE '2025-12-31'
)
SELECT * FROM dates;

Recipe 9 — UDFs in Python

import duckdb

def parse_user_agent(ua: str) -> str:
    if "Mobile" in (ua or ""): return "mobile"
    if "Tablet" in (ua or ""): return "tablet"
    return "desktop"

con = duckdb.connect()
con.create_function("device_class", parse_user_agent, ["VARCHAR"], "VARCHAR")
con.sql("SELECT device_class(user_agent), count(*) FROM events GROUP BY 1").show()

Recipe 10 — Export to a warehouse (DuckDB → Snowflake)

import duckdb
import snowflake.connector

con = duckdb.connect("analytics.duckdb")
arrow_table = con.execute("SELECT * FROM customer_summary").fetch_arrow_table()

sf = snowflake.connector.connect(**creds)
from snowflake.connector.pandas_tools import write_pandas
write_pandas(sf, arrow_table.to_pandas(), "CUSTOMER_SUMMARY",
             auto_create_table=True, overwrite=True)

For larger datasets: write Parquet from DuckDB, then COPY INTO from S3/stage on Snowflake side.

Example end-to-end

Goal: Analyze 200 Parquet files in S3 with a 10GB join against local reference data, then promote the summary to Snowflake.

  1. INSTALL httpfs; LOAD httpfs; SET s3_*.
  2. CREATE OR REPLACE VIEW raw_events AS SELECT * FROM 's3://bucket/events/year=2025/*.parquet';
  3. Load reference CSV: CREATE TABLE dim_segments AS SELECT * FROM 'segments.csv';
  4. Run aggregation: CREATE TABLE summary AS SELECT segment_id, count(*), sum(revenue) FROM raw_events JOIN dim_segments USING(user_id) GROUP BY 1;
  5. Profile: EXPLAIN ANALYZE SELECT * FROM summary; — verify Parquet pushdown + parallel scan.
  6. Export: COPY summary TO 's3://bucket/output/summary.parquet' (FORMAT PARQUET);
  7. Snowflake: COPY INTO marts.event_summary FROM @my_s3_stage/output/summary.parquet.

Edge cases / gotchas

Sources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.