

# Translate queries to DuckDB

Translate Athena engine version 3 queries to DuckDB's SQL dialect, and review the changes. Translation rewrites a query in DuckDB's SQL dialect, so that you don't change functions and syntax by hand, and lists the changes. Translation doesn't run the query.

The migration agent translates queries in the following ways:
+ **On demand** – In the query editor, choose **Translate query**. The result opens on the **Translate results** tab. You don't need to turn on the **Athena migration agent** setting.
+ **Queries that you run** – When the **Athena migration agent** setting is on, the migration agent translates each query that succeeds in the workgroup. The result appears on the **Recent queries** tab.
+ **Queries that you save** – When the **Athena migration agent** setting is on, the migration agent translates each saved query when you create or update it. The result appears on the **Saved queries** tab.

To turn on the **Athena migration agent** setting, see [Before you start](duckdb-migration-agent.md#athena-duckdb-migration-agent-before-you-start).

## In the console
<a name="athena-duckdb-migration-agent-translate-console"></a>

1. Open the Athena console at [https://console.aws.amazon.com/athena/](https://console.aws.amazon.com/athena/home).

1. In the query editor, choose an engine version 3 workgroup.

1. Enter the engine version 3 query that you want to translate.

1. Choose **Use migration agent**, and then choose **Translate query**. Athena opens the **Translate results** tab with the change summary and the original and translated SQL. If the migration agent can't translate the query, the tab shows **Translation failed** with the reason.  
![The Translate results tab, with the change summary and the original and translated SQL side by side.](https://docs.aws.amazon.com/athena/latest/ug/images/athena-duckdb-migration-agent-shot-5.png)

For example, the migration agent translates the following query:

```sql
SELECT category,
       CARDINALITY(ARRAY_AGG(DISTINCT product_name)) AS product_count
FROM products
GROUP BY category
```

The translated query is:

```sql
SELECT category,
       ARRAY_LENGTH(ARRAY_AGG(DISTINCT product_name)) AS product_count
FROM products
GROUP BY category
```

The **Change summary** shows "CARDINALITY(ARRAY\_AGG(DISTINCT product\_name)) -> ARRAY\_LENGTH(ARRAY\_AGG(DISTINCT product\_name))".

### View translation results
<a name="athena-duckdb-migration-agent-translate-view"></a>

The **Recent queries** tab shows the translations of the queries that you run when the **Athena migration agent** setting is on, and of the queries that an assessment reads. The **Saved queries** tab shows the translations of your saved queries. A query that you translate on demand appears only on the **Translate results** tab.

**To view a translation**

1. Open the Athena console at [https://console.aws.amazon.com/athena/](https://console.aws.amazon.com/athena/home).

1. In the query editor, choose the engine version 3 workgroup.

1. Choose the **Recent queries** tab or the **Saved queries** tab. The **Translation status** column shows each query's status, such as **Translated**, **No translation needed**, **Needs review**, **Validated**, or **Failed**.

1. Choose a query. A split view opens with the change summary and the original and translated SQL.

![The Recent queries tab with a translated query selected. The split view shows the change summary and the SQL.](https://docs.aws.amazon.com/athena/latest/ug/images/athena-duckdb-migration-agent-shot-7.png)


A query with the status **Needs review** (`TRANSLATION_NEEDS_REVIEW`) means that the migration agent isn't confident in its translation, or didn't produce one. Choose the query on the **Recent queries** or **Saved queries** tab, and then review and edit the translated SQL. If the migration agent didn't produce a translation, the translated SQL panel shows the original query unchanged, and you rewrite it yourself. Then validate the query.