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)
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
SUCCEEDEDstate. With managed query results, its results are less than 24 hours old. -
IAM permissions –
athena:GetQueryResultsorathena:GetQueryResultStreamon 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-ida1b2c3d4-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
NextTokento 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.Typeuses DuckDB type names, such asVARCHAR,INTEGER,DATE,DECIMAL(4,1),INTEGER[],STRUCT(a INTEGER, b VARCHAR), andMAP(VARCHAR, INTEGER). -
A column without a name, such as
SELECT 1 + 1, is named after its expression, for example(1 + 1).GetQueryResultStreamuses the same column names. -
Statements that don't return rows, such as
INSERT INTOand CTAS, return no data rows.UpdateCountcontains the number of rows that the statement wrote.
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.
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
-
BOOLEANarrives as the extension typearrow.bool8, which is stored asint8. Readers that don't recognizebool8show0and1instead oftrueandfalse. -
UUIDarrives as the extension typearrow.uuid, which is stored as 16-bytefixed_size_binary.JSONarrives as the extension typearrow.json, which is stored asutf8. Readers that don't recognize these types show the raw bytes forUUIDand plain strings forJSON. -
HUGEINTandUHUGEINT(128-bit integers) arrive as the extension typearrow.opaque, which is stored as 16-bytefixed_size_binary. Readers that don't recognizearrow.opaqueshow raw bytes instead of numbers. -
TIME WITH TIME ZONEarrives asarrow.opaque, which is stored as 8-bytefixed_size_binary. The value and its UTC offset are preserved, but readers that don't recognizearrow.opaqueshow raw bytes. -
BITarrives asarrow.opaque, which is stored asbinaryin 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
ResultFetchervalue isauto. With this value, the drivers read results withGetQueryResultStreamon DuckDB workgroups. They useGetQueryResultswhen streaming isn't available. Earlier driver versions can't read DuckDB results. The drivers also callGetWorkGroup, so the principal needsathena:GetWorkGroup. -
ADBC driver – The Apache Arrow Database Connectivity (ADBC) driver reads results with
GetQueryResultStreamorGetQueryResults.