Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 0 additions & 140 deletions website/docs/components/catalogs/cayenne.md

This file was deleted.

56 changes: 0 additions & 56 deletions website/docs/reference/sql/dml.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,59 +81,3 @@ SELECT order_id, customer_id, total, order_date
FROM orders
WHERE order_date < '2024-01-01';
```

## MERGE INTO

Update rows in a target table based on matching rows from a source table. Currently supported for [Spice Cayenne](../../components/data-accelerators/cayenne) catalog tables only.

### Syntax

```sql
MERGE INTO target [AS alias]
USING source [AS alias]
ON <join_condition>
WHEN MATCHED THEN UPDATE SET column1 = expr1 [, column2 = expr2, ...]
```

### Parameters

- **`target`**: The table to update. Must be a Cayenne catalog table.
- **`source`**: The table containing new values. Must be a Cayenne catalog table.
- **`join_condition`**: A conjunction of equality predicates (e.g., `target.id = source.id AND target.region = source.region`).
- **`column = expr`**: Column assignments applied to matched rows. Expressions can reference columns from both target and source tables using aliases.

### Constraints

- Only `WHEN MATCHED THEN UPDATE SET` is supported (no `INSERT`, `DELETE`, or `NOT MATCHED` clauses).
- The `ON` clause must use equality predicates only, joined with `AND`.
- The target table must not have `primary_key` or `on_conflict` configured.
- The source table must not contain duplicate rows for the same join key(s). If duplicates are detected, the MERGE operation fails with an error to prevent data loss. Deduplicate the source table before running MERGE.
- In distributed deployments, both source and target tables must be partitioned by the same column, and the partition column must appear in the `ON` clause join keys. Use [`CREATE TABLE ... LIKE`](../../components/catalogs/cayenne#create-table--like) to create staging tables that share partition routing with the target table.

### Examples

#### Update Matching Rows

```sql
MERGE INTO catalog.schema.customers AS t
USING catalog.schema.customer_updates AS s
ON t.id = s.id
WHEN MATCHED THEN UPDATE SET name = s.name, email = s.email;
```

```text
+-------+
| count |
+-------+
| 42 |
+-------+
```

#### Composite Join Keys

```sql
MERGE INTO catalog.schema.inventory AS t
USING catalog.schema.new_stock AS s
ON t.product_id = s.product_id AND t.warehouse_id = s.warehouse_id
WHEN MATCHED THEN UPDATE SET quantity = s.quantity, updated_at = s.updated_at;
```
Loading