Skip to content
Skills Library
Data & analytics · Building & coding

wren-sql

/wren-sql

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.

Repository
wren-engine ↗
Licence
Apache-2.0
Last repository change
06.05.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
5
Scripts
0
HTML comments
0
URLs in SKILL.md
1
Deprecation

No signals detected. Repository changed on 06.05.2026, without references to retired models or APIs.

Licence

Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.

What it does and when to use it

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.

Original author 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.

How it works

  1. Version check
  2. Context
  3. Core SQL Rules
  4. Filter Strategies
  5. Supported Cast Types
  6. Aggregation
  7. Sorting and Limiting
  8. Subquery Patterns
  9. Calculated Fields
Original SKILL.md headings · 125 lines, 635 words
SKILL.md preview
---
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:

| Topic | Reference |
|-------|-----------|
| SQL error diagnosis and correction | [references/correction.md](references/correction.md) |
| Date/time functions and intervals | [references/datetime.md](references/datetime.md) |
| ARRAY, STRUCT, JSON/VARIANT types | [references/types.md](references/types.md) |
| BigQuery dialect quirks | [references/bigquery.md](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 `dataSource` field to identify the backend and apply dialect-specific rules if needed.

---

## Core SQL Rules

- Only `SELECT` statements. No `DELETE`, `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"`
- Identifier quotes: `"` (double quotes). String literal quotes: `'` (single quotes).
- For specific date queries, use a range:
  ```sql
  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 in `SELECT`.
- Avoid correlated subqueries — use JOINs instead.
- Use `SAFE_CAST` when 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 `SELECT` columns must appear in `GROUP BY` (window functions excepted).
- Aggregate conditions go in `HAVING`, not `WHERE`.
- Prefer ordinal `GROUP BY` for long column names:
  ```sql
  SELECT very_long_column_name AS alias, COUNT(*) FROM t GROUP BY 1
  ```

---

## 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.

```sql
-- 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
```
Files in this package (5)
  • SKILL.md 5 KB
  • references/bigquery.md 2 KB
  • references/correction.md 3 KB
  • references/datetime.md 1 KB
  • references/types.md 3 KB

Versions

The database has no other skills named wren-sql.

Database snapshot: 13.11.2026 · package version c6a3779d95ab (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100wren-usage
    Wren Engine — semantic SQL engine for AI agents. Query 22+ data sources (PostgreSQL, BigQuery, Snowflake, MySQL, ClickHouse, etc.) through a modeling layer (MDL). This skill is the main entry point: it guides setup, delegates to focused sub-skills for SQL authoring, MDL…
  • 100wren-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…
  • 100generate-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…
  • 100usage
    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…
  • 100generating-sql
    Translates business questions into correct, readable SQL for PostgreSQL, BigQuery, Snowflake, SQL Server, MySQL or SQLite, explains the query in plain language and adds validation checks. Also reviews and fixes existing queries. Use when someone asks for a SQL query, a report or…
  • 90bigquery-sql
    Provides BigQuery SQL query optimization techniques, execution best practices, and performance tuning rules for high-efficiency querying. Use when optimizing BigQuery SQL queries, reducing query costs, or designing performant SQL transformations.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.