

# Choose where query results are stored

A DuckDB workgroup uses managed query results by default. Optionally, you can configure the workgroup to store query results in your own Amazon S3 bucket (a customer managed bucket). Choose based on how you use the results:
+ To have Athena store, encrypt, and delete results for you, or to read results as Apache Arrow with `GetQueryResultStream`, use managed query results.
+ If your applications read result files directly from Amazon S3, for example with the `GetObject` API, or you need to keep results longer than 24 hours, use a customer managed bucket.

`GetQueryResults` returns the same values whichever option you choose.

The following table compares the two options.


| Feature | Managed query results (default) | Customer managed bucket | 
| --- | --- | --- | 
| Result files that you can read in Amazon S3 | No. To create files with SQL, see [Download and export query results](duckdb-query-results-export.md). | Yes. Each query writes a .csv file and a .csv.metadata file. | 
| Retention | 24 hours | Controlled by you, for example with Amazon S3 lifecycle rules. | 
| Encryption | An AWS owned key (default) or a customer managed key in AWS KMS. | Server-side encryption with Amazon S3 managed keys (SSE-S3) or with AWS KMS keys (SSE-KMS). Client-side encryption (CSE-KMS) is not supported. | 
| GetQueryResults | Supported | Supported | 
| GetQueryResultStream (Apache Arrow) | Supported | Not supported | 
| Download from the console | Up to 200 MB for each download. | The full result file. | 

## Use managed query results
<a name="athena-duckdb-query-results-storage-managed"></a>

New DuckDB workgroups use managed query results by default. Workgroups that you migrate from Athena engine version 3 keep their query result location. You don't need to create or manage a results bucket. Athena keeps each result for 24 hours. During that time, you can read the result as many times as you need, with `GetQueryResults` or `GetQueryResultStream`. After 24 hours, Athena deletes the result. To keep a result longer, write it to files or a table, as described in [Download and export query results](duckdb-query-results-export.md), or store results in your own bucket.

Managed query results are encrypted at rest. Choose one of the following options:
+ **AWS owned key** – The default. You don't manage the key, and you aren't charged for it.
+ **Customer managed key** – An AWS KMS key that you create and manage. Set the key on `ManagedQueryResultsConfiguration.EncryptionConfiguration.KmsKey`. Athena encrypts each result with the key that the workgroup uses when the query runs. If you later change the key, Athena doesn't re-encrypt existing results.

If you use a customer managed key, the key policy must allow the `encryption.athena.amazonaws.com` service principal to use `kms:Decrypt`, `kms:GenerateDataKey`, and `kms:DescribeKey`.

The principal that runs the query and reads its results must also be allowed to use the key. Athena checks access to the key when you submit the query, and rejects the query if the key can't be used.

With managed query results, you can't access results as files in Amazon S3. To read results, use `GetQueryResults` or `GetQueryResultStream`. To create files, see [Download and export query results](duckdb-query-results-export.md).

## Use a customer managed bucket
<a name="athena-duckdb-query-results-storage-location"></a>

To store results in your own bucket, set the workgroup's query result location to that bucket. The principal that runs queries needs write access to the location. Principals that read results need read access to it. If you use SSE-KMS, both principals also need permission to use the AWS KMS key.

Query results in your bucket can be encrypted with SSE-S3 or SSE-KMS. CSE-KMS is not supported.

------
#### [ AWS Management Console ]

**To set a query result location in the console**

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

1. In the navigation pane, choose **Workgroups**, and then choose your DuckDB workgroup.

1. Choose **Edit**.

1. Under **Query result configuration**, select **Customer managed**, and then for **Location of query result**, enter the Amazon S3 location for results, such as `s3://{{amzn-s3-demo-bucket}}/{{results}}/`.

1. (Optional) Select **Encrypt query results**, and then for **Encryption type**, select **SSE\_S3** or **SSE\_KMS**.

1. (Optional) For **Expected bucket owner**, enter the AWS account ID that must own the bucket. Queries fail if a different account owns the bucket.

1. (Optional) Select **Assign bucket owner full control over query results** if another account owns the bucket and the bucket has access control lists (ACLs) enabled.

1. Choose **Save changes**.

------
#### [ AWS CLI ]

Use the [update-work-group](https://docs.aws.amazon.com/cli/latest/reference/athena/update-work-group.html) command to set the query result location. The following example also sets SSE-KMS encryption, the expected bucket owner, and bucket owner full control. Replace {{my-duckdb-workgroup}}, {{amzn-s3-demo-bucket}}, {{results}}, {{us-east-1}}, {{111122223333}}, and {{key-id}} with your own values.

```bash
aws athena update-work-group \
    --work-group {{my-duckdb-workgroup}} \
    --configuration-updates '{
        "ResultConfigurationUpdates": {
            "OutputLocation": "s3://{{amzn-s3-demo-bucket}}/{{results}}/",
            "EncryptionConfiguration": {
                "EncryptionOption": "SSE_KMS",
                "KmsKey": "arn:aws:kms:{{us-east-1}}:{{111122223333}}:key/{{key-id}}"
            },
            "ExpectedBucketOwner": "{{111122223333}}",
            "AclConfiguration": { "S3AclOption": "BUCKET_OWNER_FULL_CONTROL" }
        }
    }'
```

Verify the configuration with the [get-work-group](https://docs.aws.amazon.com/cli/latest/reference/athena/get-work-group.html) command.

```bash
aws athena get-work-group --work-group {{my-duckdb-workgroup}}
```

Make sure that `WorkGroup.Configuration.ResultConfiguration.OutputLocation` is your location.

------

When you set an expected bucket owner, Athena verifies that the account owns the bucket when you save the workgroup. The only supported ACL option is `BUCKET_OWNER_FULL_CONTROL`.

**Note**  
S3 Access Grants are not supported for DuckDB query results.

## Result settings in a query request
<a name="athena-duckdb-query-results-storage-request-settings"></a>

`StartQueryExecution` accepts a `ResultConfiguration` with `OutputLocation`, `EncryptionConfiguration`, `ExpectedBucketOwner`, and `AclConfiguration`. How Athena uses it depends on the workgroup:
+ If the workgroup uses managed query results, Athena rejects the request with the message `ManagedQueryResultsConfiguration and ResultConfiguration cannot be set together.`
+ If the workgroup uses a query result location, Athena uses the values from the request. If the workgroup has **Override client-side settings** turned on, the workgroup's value takes precedence for each setting that the workgroup defines.

The same rules apply to settings that a client sends for you, such as the `OutputLocation` connection parameter of the JDBC and ODBC drivers.

## Result files in a customer managed bucket
<a name="athena-duckdb-query-results-storage-files"></a>

Each query writes its files directly under the query result location. Files are named with the query execution ID.


| File | Name | Description | 
| --- | --- | --- | 
| Result file | {{QueryExecutionId}}.csv | The result in CSV format, with a header row. Every statement type writes a CSV file, including SHOW, DESCRIBE, and EXPLAIN. | 
| Metadata file | {{QueryExecutionId}}.csv.metadata | Column names and types in a binary format internal to Athena. Athena uses this file to return results with GetQueryResults. Don't delete or change it. | 

To find the result file for a query, use the [get-query-execution](https://docs.aws.amazon.com/cli/latest/reference/athena/get-query-execution.html) command and read `ResultConfiguration.OutputLocation`. Replace the example query execution ID with your own.

```bash
aws athena get-query-execution --query-execution-id {{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}}
```