View a markdown version of this page

Read query results - Amazon Athena

Read query results

You can use two operations to read the results of a DuckDB query. GetQueryResults returns the result in pages of rows, with every value as a string. GetQueryResultStream returns the whole result as one Apache Arrow IPC stream, with every column in its native type. For a description of the stream format, see Serialization and Interprocess Communication (IPC) on the Apache Arrow website.

Query results use DuckDB data types, such as VARCHAR, INTEGER, DECIMAL, STRUCT, and MAP. GetQueryResults reports each column's DuckDB type name and returns each value as the text that DuckDB writes for that type. GetQueryResultStream returns each column as the corresponding Apache Arrow type.

Use GetQueryResultStream when the workgroup uses managed query results and your application can read Apache Arrow. Use GetQueryResults when the workgroup stores results in your own Amazon S3 bucket (a customer managed bucket), or when you can't use Apache Arrow. The AWS SDKs return GetQueryResults rows as objects in your programming language.

Prerequisites

Before you read results, make sure that you have the following:

  • A completed query – The query is in the SUCCEEDED state. With managed query results, its results are less than 24 hours old.

  • IAM permissions – athena:GetQueryResults or athena:GetQueryResultStream on the workgroup. The two permissions are separate. Allowing one doesn't allow the other.

  • A supported client – The AWS SDKs and the AWS CLI support both operations. The JDBC and ODBC drivers require version 4.0.0 or later. For more information, see Read results with drivers.

To confirm that a query succeeded, use the get-query-execution command. Replace the example query execution ID with your own.

aws athena get-query-execution --query-execution-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111

Make sure that QueryExecution.Status.State is SUCCEEDED. If the query failed, Status.AthenaError and Status.StateChangeReason describe the error.

Read results with GetQueryResults

GetQueryResults returns a ResultSet, which the AWS SDKs deserialize into objects in your programming language. Each row is a list of Datum objects, and each value is a string in VarCharValue, whatever the column type. A NULL value is a Datum with no VarCharValue. Column names and types are in ResultSetMetadata.ColumnInfo.

Keep the following in mind:

  • Each page holds up to 1,000 rows. Use NextToken to get the next page.

  • The first page starts with a header row that contains the column names. The header row counts toward MaxResults. Skip it when you process data rows.

  • ColumnInfo.Type uses DuckDB type names, such as VARCHAR, INTEGER, DATE, DECIMAL(4,1), INTEGER[], STRUCT(a INTEGER, b VARCHAR), and MAP(VARCHAR, INTEGER).

  • A column without a name, such as SELECT 1 + 1, is named after its expression, for example (1 + 1). GetQueryResultStream uses the same column names.

  • Statements that don't return rows, such as INSERT INTO and CTAS, return no data rows. UpdateCount contains the number of rows that the statement wrote.

AWS CLI

The following example gets the first page of results. Replace the example query execution ID with your own.

aws athena get-query-results \ --query-execution-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111 \ --max-results 10

The following is an abridged example response for SELECT order_id, customer, amount, order_date FROM duckdb_tutorial.orders ORDER BY order_id.

{ "UpdateCount": 0, "ResultSet": { "Rows": [ { "Data": [ { "VarCharValue": "order_id" }, { "VarCharValue": "customer" }, { "VarCharValue": "amount" }, { "VarCharValue": "order_date" } ] }, { "Data": [ { "VarCharValue": "1" }, { "VarCharValue": "Alice" }, { "VarCharValue": "19.99" }, { "VarCharValue": "2026-11-01" } ] }, { "Data": [ { "VarCharValue": "2" }, { "VarCharValue": "Bob" }, {}, { "VarCharValue": "2026-11-02" } ] }, { "Data": [ { "VarCharValue": "3" }, { "VarCharValue": "Carol" }, { "VarCharValue": "7.50" }, { "VarCharValue": "2026-11-03" } ] } ], "ResultSetMetadata": { "ColumnInfo": [ { "Name": "order_id", "Label": "order_id", "Type": "INTEGER" }, { "Name": "customer", "Label": "customer", "Type": "VARCHAR" }, { "Name": "amount", "Label": "amount", "Type": "DECIMAL(10,2)" }, { "Name": "order_date", "Label": "order_date", "Type": "DATE" } ] } } }

In this example, the first row is the header row. Every value is a string, such as "1" for the INTEGER column and "19.99" for the DECIMAL(10,2) column. Bob's amount is NULL, shown as an empty Datum. The response has no NextToken because all rows fit on one page.

Python (Boto3)

The following Python example uses the AWS SDK for Python (Boto3) to get all pages of results and print each row. Replace the example query execution ID with your own.

import boto3 client = boto3.client("athena") query_execution_id = "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" paginator = client.get_paginator("get_query_results") columns = None for page in paginator.paginate(QueryExecutionId=query_execution_id): rows = page["ResultSet"]["Rows"] if columns is None: info = page["ResultSet"]["ResultSetMetadata"]["ColumnInfo"] columns = [c["Name"] for c in info] rows = rows[1:] # The first row of the first page is the header row for row in rows: values = [d.get("VarCharValue") for d in row["Data"]] print(dict(zip(columns, values)))

For the query SELECT order_id, customer, amount, order_date FROM duckdb_tutorial.orders ORDER BY order_id, the example prints the following. Each value is a string, and a NULL value is None.

{'order_id': '1', 'customer': 'Alice', 'amount': '19.99', 'order_date': '2026-11-01'} {'order_id': '2', 'customer': 'Bob', 'amount': None, 'order_date': '2026-11-02'} {'order_id': '3', 'customer': 'Carol', 'amount': '7.50', 'order_date': '2026-11-03'}

Read results with GetQueryResultStream

GetQueryResultStream is available for workgroups that use managed query results. For results in your own bucket, use GetQueryResults.

GetQueryResultStream returns the whole result as one Apache Arrow IPC stream. The stream contains a schema, then one or more record batches, then an end-of-stream marker. Each record batch holds a block of rows stored by column. Each column keeps its native type, so your client doesn't parse values from strings. Apache Arrow libraries are available for many languages, including C++, Go, Java, JavaScript, Python, R, and Rust.

The response includes a Content-Length header. A dropped connection can end the stream between record batches without an error. To check that you received the whole result, make sure that the stream ends with the end-of-stream marker, or compare the bytes you received with Content-Length. If a download is interrupted, request the rest with the Range parameter, for example bytes=1048576-. The stream can be resumed at any byte offset.

AWS CLI

The following example saves the stream to a file. Replace the example query execution ID with your own, and results.arrows with the name of the output file.

aws athena get-query-result-stream \ --query-execution-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111 \ results.arrows

The command saves the stream to the file and prints the response metadata, for example:

{ "ContentType": "application/vnd.apache.arrow.stream", "AcceptRanges": "bytes" }

To resume an interrupted download, add --range "bytes=offset-" and append the output to the partial file.

Python (Boto3)

The following Python example uses the AWS SDK for Python (Boto3) and the Apache Arrow library for Python (PyArrow) to read the stream into a table and print it as a pandas DataFrame. To run it, install pyarrow and pandas. Replace the example query execution ID with your own.

import boto3 import pyarrow as pa client = boto3.client("athena") query_execution_id = "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" response = client.get_query_result_stream(QueryExecutionId=query_execution_id) with pa.ipc.open_stream(response["Body"]) as reader: table = reader.read_all() print(table.to_pandas())

For the same query, the example prints the following. Unlike GetQueryResults, the values keep their types. For example, amount values are decimals and order_date values are dates, not strings.

order_id customer amount order_date 0 1 Alice 19.99 2026-11-01 1 2 Bob None 2026-11-02 2 3 Carol 7.50 2026-11-03

Most types map directly to Apache Arrow types. The following types arrive as Apache Arrow canonical extension types, so some readers display them differently. The values are exact. For more information about canonical extension types, see Canonical Extension Types on the Apache Arrow website.

  • BOOLEAN arrives as the extension type arrow.bool8, which is stored as int8. Readers that don't recognize bool8 show 0 and 1 instead of true and false.

  • UUID arrives as the extension type arrow.uuid, which is stored as 16-byte fixed_size_binary. JSON arrives as the extension type arrow.json, which is stored as utf8. Readers that don't recognize these types show the raw bytes for UUID and plain strings for JSON.

  • HUGEINT and UHUGEINT (128-bit integers) arrive as the extension type arrow.opaque, which is stored as 16-byte fixed_size_binary. Readers that don't recognize arrow.opaque show raw bytes instead of numbers.

  • TIME WITH TIME ZONE arrives as arrow.opaque, which is stored as 8-byte fixed_size_binary. The value and its UTC offset are preserved, but readers that don't recognize arrow.opaque show raw bytes.

  • BIT arrives as arrow.opaque, which is stored as binary in the DuckDB bit encoding.

Every column also carries its Athena type in the Apache Arrow field metadata, so your application can tell which type a column holds. The key ATHENA:type holds the base type name, such as DECIMAL, LIST, STRUCT, or HUGEINT, and the key ATHENA:type_details holds the full type, such as DECIMAL(4,1). For DECIMAL columns, the keys ATHENA:type_precision and ATHENA:type_scale hold the precision and scale.

How result values are formatted

GetQueryResults returns each value as the text that DuckDB writes when it exports CSV. The values are the same whether the workgroup uses managed query results or your own bucket.

The following table shows examples.

Type Value in the query Text returned by GetQueryResults
STRUCT {'a': 1} {'a': 1}
STRUCT {'x': 'hi, there', 'y': NULL} {'x': 'hi, there', 'y': NULL}
MAP MAP {'k': 'v'} {k=v}
LIST [1, 2] [1, 2]
LIST ['a b', NULL] [a b, NULL]
VARCHAR 'a,"b"' a,"b"
Any type NULL No VarCharValue (an empty Datum)
DOUBLE 'NaN'::DOUBLE nan
DOUBLE 'Infinity'::DOUBLE, '-Infinity'::DOUBLE inf, -inf
BLOB '\xAA'::BLOB \xAA
BOOLEAN true true

GetQueryResultStream returns the same data as typed Apache Arrow values instead of text. For example, a BOOLEAN column is "true" in GetQueryResults and an arrow.bool8 value in GetQueryResultStream. Both represent the same result.

SHOW, DESCRIBE, and EXPLAIN statements return a structured result with a header row and one or more columns, the same way DuckDB returns them. Read these results the same way that you read a SELECT result.

Read results with drivers

The following clients read DuckDB results:

  • JDBC and ODBC drivers version 4.0.0 or later – The default ResultFetcher value is auto. With this value, the drivers read results with GetQueryResultStream on DuckDB workgroups. They use GetQueryResults when streaming isn't available. Earlier driver versions can't read DuckDB results. The drivers also call GetWorkGroup, so the principal needs athena:GetWorkGroup.

  • ADBC driver – The Apache Arrow Database Connectivity (ADBC) driver reads results with GetQueryResultStream or GetQueryResults.