| name | wren-sql |
| description | Write and correct SQL queries targeting Wren Engine — covers MDL query rules, filter strategies, data types (ARRAY, STRUCT, JSON/VARIANT), date/time functions, Calculated Fields, BigQuery dialect quirks, and error diagnosis. Use when generating or debugging SQL for any Wren Engine data source. |
| license | Apache-2.0 |
| metadata | {"author":"wren-engine","version":"1.0"} |
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:
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
dataSource field to identify the backend and apply dialect-specific rules if needed.
Core SQL Rules
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
Sorting and Limiting
ORDER BY for sort; LIMIT to restrict rows.
- When
ORDER BY appears in a subquery or CTE, always include LIMIT.
Subquery Patterns
- Prefer CTEs (
WITH clause) over nested subqueries.
- Subquery in
SELECT must return a single value per row.
- Subquery in
WHERE: use IN, EXISTS, or comparison operators.
IN SUBQUERY in JOIN conditions is not supported — use JOIN ... ON instead.
RECURSIVE CTEs 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.
SELECT AVG(Rating) FROM orders WHERE ReviewCount > 10