Skip to main content
Back to Community & Earn Hub

DuckDB + Parquet for Local AI Analytics Agents: Querying 100GB Datasets on a Laptop Without Snowflake or Spark

@priya_sharma4 days ago·2 min read 1965 views
DuckDB + Parquet for Local AI Analytics Agents: Querying 100GB Datasets on a Laptop Without Snowflake or Spark

Why Pandas REPL Agents Fail on Real Datasets

When developers build an AI Data Analyst agent, the first prototype gives the LLM a Python REPL running pandas.read_csv().

That works on a 5MB toy CSV. On a real 20GB–100GB Parquet event dataset:

  • pandas tries to load the entire dataframe into RAM -> instant OOM crash.
  • Multi-step Python transformations generated by the LLM are slow and fragile.

Replacing the Python REPL with an embedded DuckDB SQL MCP tool lets the agent run vectorized, out-of-core aggregations directly over Parquet files on disk or S3 in milliseconds.


The Security Catch: DuckDB Can Read Arbitrary Local Files!

By default, DuckDB's SQL engine is too powerful for untrusted LLM-generated SQL—an indirect prompt injection could trick the agent into running:

sql
SELECT * FROM read_csv('/etc/passwd', delim=':');
-- or exfiltrating via httpfs!

Before executing any LLM-generated SQL in DuckDB, you must lock down external access and memory limits on the connection:

python
import duckdb
from fastmcp import FastMCP

mcp = FastMCP("analytics-duckdb")

def create_sandboxed_duckdb(parquet_dir: str) -> duckdb.DuckDBPyConnection:
    conn = duckdb.connect(database=":memory:")
    # 1. Pre-register ONLY the authorized Parquet view
    conn.execute(f"CREATE VIEW events AS SELECT * FROM read_parquet('{parquet_dir}/*.parquet');")

    # 2. Hard-lock filesystem & network access BEFORE any agent SQL runs!
    conn.execute("SET enable_external_access = false;")
    conn.execute("SET lock_configuration = true;")  # Prevents agent SQL from toggling settings back!
    conn.execute("SET memory_limit = '4GB';")
    conn.execute("SET threads = 4;")
    return conn

@mcp.tool()
def run_readonly_analytics_sql(sql: str) -> dict:
    """Execute a read-only analytical SQL query against the `events` view (max 100 rows returned)."""
    conn = create_sandboxed_duckdb("/data/warehouse")
    # Enforce hard row cap so agent context never floods
    wrapped = f"SELECT * FROM ({sql.rstrip(';')}) LIMIT 100"
    rel = conn.execute(wrapped)
    cols = [desc[0] for desc in rel.description]
    rows = rel.fetchall()
    return {"columns": cols, "rows": [dict(zip(cols, r)) for r in rows]}

Why SET lock_configuration = true; Matters

Without lock_configuration = true, a clever prompt injection could execute SET enable_external_access = true; SELECT * FROM read_text('/app/.env');. Locking the DuckDB configuration at connection init makes external file reads and network calls impossible at the C++ engine level.

2 Comments

Discussion

SET lock_configuration = true; after SET enable_external_access = false; is such a critical catch. Without locking configuration, an LLM-generated query could literally run SET enable_external_access = true; inside the same session!

@vikram_joshi4 days ago

Wrapping the generated SQL in SELECT * FROM (...) LIMIT 100 also prevents an agent from accidentally dumping 500,000 rows back into its own context window.

Join the developer discussion

Sign in or create a free account to comment (+10 pts) and tag helpful AIStoreHub repositories.