wren-sql
Wren SQL
Version check
Silently fetch https://raw.githubusercontent.com/Canner/wren-engine/main/skills/versions.json.
Compare the wren-sql key with this skill's version (from the frontmatter above).
If the remote version is newer, notify the user before proceeding:
A newer version of the wren-sql skill is available. Update with:
npx skills add Canner/wren-engine --skill wren-sql --agent claude-code
Then continue with the workflow below regardless of update status.
Wren Engine translates SQL through a semantic layer (MDL — Model Definition Language) before executing it against a backend database. SQL must target MDL model names, not raw database tables.
For specific topics, load the relevant reference file:
| Topic | Reference |
|---|---|
| SQL error diagnosis and correction | references/correction.md |
| Date/time functions and intervals | references/datetime.md |
| ARRAY, STRUCT, JSON/VARIANT types | references/types.md |
| BigQuery dialect quirks | references/bigquery.md |
Context
- You are querying a semantic layer, not a database directly.
- Only use model/view/column names defined in the MDL — never raw database table references.
- Wren Engine uses a generic SQL dialect similar to ANSI SQL (DataFusion/Postgres/DuckDB), but with differences.
- Check the
dataSourcefield to identify the backend and apply dialect-specific rules if needed.
Core SQL Rules
- Only
SELECTstatements. NoDELETE,UPDATE,INSERT. - Only use tables and columns from the MDL schema.
- Do not include comments in generated SQL.
- Prefer CTEs over subqueries.
- Identifiers are case-sensitive. Quote identifiers containing unicode, special characters (except
_), or starting with a digit using double quotes.- Examples:
"客户"."姓名","table-name"."col","123column"
- Examples:
- Identifier quotes:
"(double quotes). String literal quotes:'(single quotes). - For specific date queries, use a range:
WHERE ts >= CAST('2024-11-01 00:00:00' AS TIMESTAMP WITH TIME ZONE) AND ts < CAST('2024-11-02 00:00:00' AS TIMESTAMP WITH TIME ZONE) - For ranking, use
DENSE_RANK()+WHERE. Include the ranking column inSELECT. - Avoid correlated subqueries — use JOINs instead.
- Use
SAFE_CASTwhen casting might fail:SAFE_CAST(col AS INT)
Filter Strategies
| Column type | Strategy |
|---|---|
| Text | LIKE '%value%' for partial match |
| Numeric | BETWEEN 30 AND 40 |
| Date/Timestamp | >= '2024-01-01' AND < '2024-02-01' |
| Exact value | = or IN (...) |
| Primary key / indexed | Prefer equality (=) |
Supported Cast Types
bool, boolean, int, integer, bigint, smallint, tinyint, float, double, real, decimal, numeric, varchar, char, string, text, date, time, timestamp, timestamp with time zone, bytea
Example: CAST(col AS INT), TIMESTAMP '2024-11-09 00:00:00'
Aggregation
- All non-aggregated
SELECTcolumns must appear inGROUP BY(window functions excepted). - Aggregate conditions go in
HAVING, notWHERE. - Prefer ordinal
GROUP BYfor long column names:SELECT very_long_column_name AS alias, COUNT(*) FROM t GROUP BY 1
Sorting and Limiting
ORDER BYfor sort;LIMITto restrict rows.- When
ORDER BYappears in a subquery or CTE, always includeLIMIT.
Subquery Patterns
- Prefer CTEs (
WITHclause) over nested subqueries. - Subquery in
SELECTmust return a single value per row. - Subquery in
WHERE: useIN,EXISTS, or comparison operators. IN SUBQUERYinJOINconditions is not supported — useJOIN ... ONinstead.RECURSIVECTEs are not supported.
Calculated Fields
Columns marked as Calculated Field in the MDL have pre-defined computation logic. Use them directly instead of re-implementing the calculation.
Read the column comment (e.g., column expression: avg(reviews.Score)) to understand what the field represents.
-- Schema has: Rating DOUBLE (Calculated Field: avg(reviews.Score))
-- ReviewCount BIGINT (Calculated Field: count(reviews.Id))
-- Correct — use Calculated Fields directly:
SELECT AVG(Rating) FROM orders WHERE ReviewCount > 10
-- Incorrect — do not re-join and re-aggregate manually
More from canner/wren-engine
wren-usage
Wren Engine CLI workflow guide for AI agents. Answer data questions end-to-end using the wren CLI: gather schema context, recall past queries, write SQL through the MDL semantic layer, execute, and learn from confirmed results. Use when: user asks a data question, requests a report or analysis, asks about metrics, revenue, customers, orders, trends, or any business data; user says 'how many', 'show me', 'what is the', 'top N', 'compare', 'trend', 'growth', 'breakdown'; user wants to explore, analyze, filter, aggregate, or summarize data from a database; agent needs to query data, connect a data source, handle errors, or manage MDL changes via the wren CLI.
21wren-generate-mdl
Generate a Wren MDL project by exploring a database with available tools (SQLAlchemy, database drivers, MCP connectors, or raw SQL). Guides agents through schema discovery, type normalization, and MDL YAML generation using the wren CLI. Use when: user wants to create or set up a new MDL, onboard a new data source, or scaffold a project from an existing database.
21wren-dlt-connector
Connect SaaS data (HubSpot, Stripe, Salesforce, GitHub, Slack, etc.) to Wren Engine for SQL analysis. Guides the user through the full flow: install dlt, pick a SaaS source, set up credentials, run the data pipeline into DuckDB, then auto-generate a Wren semantic project from the loaded data. Use this skill whenever the user mentions: connecting SaaS data, importing data from an API, dlt pipelines, loading HubSpot/Stripe/Salesforce/GitHub/Slack data, querying SaaS data with SQL, or setting up a new data source from a REST API. Also trigger when the user already has a dlt-produced DuckDB file and wants to create a Wren project from it.
17wren-onboarding
Onboard a user to Wren Engine end-to-end. Walks through environment checks, project scaffolding, connection configuration via .env, and first query. Use when: user wants to install Wren Engine, set up a new data source connection, or bootstrap a new project from scratch. Triggers: '/wren-onboarding', 'install wren', 'set up wren engine', 'wren onboarding', 'connect new database to wren'.
7wren-project
Save, load, and build Wren MDL manifests as YAML project directories for version control. Use when a user wants to persist an MDL as human-readable YAML files, load a YAML project back into MDL JSON, or compile a YAML project to a deployable mdl.json file.
3wren-quickstart
End-to-end quickstart for Wren Engine — create a workspace, generate an MDL from a live database, save it as a versioned project, start the Wren MCP Docker container, and verify the setup with a health check. Trigger when a user wants to set up Wren Engine from scratch, onboard a new data source, or get started with Wren MCP. Requires dependent skills already installed (use /wren-usage to install them first).
3