> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mantrixflow.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SQL transformations

> Write destination-owned SQL drafts, validate and preview them, and publish revisions for delivery.

SQL transformations are managed from **Transformations**, not from a canvas or
Destination panel.

## Create or open a model

1. Open the pipeline's **Transformations** tab.
2. Select a destination.
3. Click **New transformation** or open an existing row.
4. Choose input streams, enter a stable key and output model, and write SQL.

## Source references

Use `source()` references to read staged source data:

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
{{ source('raw', 'public__orders') }}
```

The source stream `public.orders` becomes `public__orders`. SaaS sources use
names such as `stripe__customers`; Airtable uses the lowercase table-ID staging
name shown by the editor.

## Example

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT
  id,
  customer_id,
  LOWER(order_status) AS order_status,
  CAST(total_amount AS DECIMAL(12, 2)) AS total_amount,
  created_at,
  updated_at
FROM {{ source('raw', 'public__orders') }}
WHERE order_status IS NOT NULL
```

## Draft and publish lifecycle

1. **Save draft** stores the SQL without affecting runs.
2. **Validate draft** checks the SQL and output model.
3. **Preview output** executes the valid draft for inspection.
4. **Publish revision** makes it the destination's active run input.

The editor also shows revision history and the currently published version.

## Delivery assignment

Publishing creates an output that can be assigned in the owning destination
editor. Under **Published output targets**, enter the exact final table and
Upsert keys. Airtable destinations map output columns to writable fields and
select merge fields.

The final destination table must exist before delivery.
