Trino client REST API#
The REST API allows clients to submit SQL queries to Trino and receive the results. Clients include the CLI, the JDBC driver, and others provided by the community. The preferred method to interact with Trino is to use these existing clients. This document provides details about the API for reference. It can also be used to implement your own client, if necessary.
Find more information about client drivers, client applications, and the client protocol configuration in the client documentation.
HTTP methods#
A
POSTto/v1/statementruns the query string in thePOSTbody, and returns a JSON document containing the query results. If there are more results, the JSON document contains anextUriURL attribute.A
GETto thenextUriattribute returns the next batch of query results.A
DELETEtonextUriterminates a running query.
Overview of query processing#
A Trino client request is initiated by an HTTP POST to the endpoint
/v1/statement, with a POST body consisting of the SQL query string. The
caller may set various Client request headers. The headers are only
required in the initial POST request, and not when following the nextUri
links.
If the client request returns an HTTP 502, 503, or 504, that means there was an intermittent problem processing request and the client should try again in 50-100 ms. Trino does not generate those codes by itself, but those can be generated by load balancers in front of Trino.
Additionally, if the request returns a 429 status code, the client should
retry the request using the Retry-After header value provided.
Any HTTP status other than 200, 429, 502, 503 or 504 means that query processing has failed.
The /v1/statement POST request returns a JSON document of type
QueryResults, as well as a collection of response headers. The QueryResults
document contains an error field of type QueryError if the query has failed,
and if that object is not present, the query succeeded. Important aspects of
QueryResults are documented in the following sections.
If the data field of the JSON document is set, it contains a list of the rows
of data. The columns field is set to a list of the names and types of the
columns returned by the query. Most of the response headers are treated like
browser cookies by the client, and echoed back as request headers in subsequent
client requests, as documented below.
If the JSON document returned by the POST to /v1/statement does not contain
a nextUri link, the query has completed, either successfully or
unsuccessfully, and no additional requests need to be made. If the nextUri
link is present in the document, there are more query results to be fetched. The
client should loop executing a GET request to the nextUri returned in the
QueryResults response object until nextUri is absent from the response.
The status field of the JSON document is for human consumption only, and
provides a hint about the query state. It cannot be used to tell if the query
is finished.
Spooling protocol#
By default a client receives results with the direct protocol: the data field
of each QueryResults document holds the rows inline. A client can instead opt
in to the spooling protocol, in which larger results are
staged to object storage and the client fetches them out of band. The spooling
protocol must be enabled and configured on the server; the
client requests it per query. For how to enable the spooling protocol in a
specific client driver or application, see the client documentation.
Requesting the spooling protocol#
A client opts in by sending the X-Trino-Query-Data-Encoding request header on
the initial POST to /v1/statement, with a comma-separated list of the
encodings it supports, in order of preference, for example:
X-Trino-Query-Data-Encoding: json+zstd,json+lz4,json
If the server has spooling enabled and supports one of the listed encodings, it
responds with the spooling protocol and echoes the selected encoding in the
X-Trino-Query-Data-Encoding response header. If the header is absent or no
encoding matches, the server falls back to the direct protocol. A client must
therefore be prepared to handle both protocols in the response.
Segments in the response#
With the spooling protocol, the data field of QueryResults is an object
rather than a list of rows:
"data": {
"encoding": "json+zstd",
"segments": [
{
"type": "inline",
"data": "<base64-encoded encoded rows>",
"metadata": { "rowOffset": 0, "rowsCount": 10, "segmentSize": 1024 }
},
{
"type": "spooled",
"uri": "https://coordinator.example/v1/spooled/download/<id>",
"ackUri": "https://coordinator.example/v1/spooled/ack/<id>",
"metadata": { "rowOffset": 10, "rowsCount": 5000, "segmentSize": 4194304 },
"headers": {
"<header-name>": ["<header-value>"]
}
}
]
}
A segment is one of two types, distinguished by the type field:
inline— the rows are embedded directly in thedatafield of the segment, base64-encoded in the negotiated encoding. No further request is needed.spooled— the rows are staged on object storage. The segment carries aurito retrieve the data, anackUrito acknowledge it, and optionalheadersthat must be sent with the retrieval and acknowledgement requests. BothuriandackUriare opaque absolute URIs that may point at different hosts, so a client must use each as given rather than parsing it or assuming a common host.
Both types carry a metadata object describing the segment. A client decodes
segments in order, using the negotiated encoding, to reconstruct the result rows.
Attribute |
Type |
Description |
|---|---|---|
|
number |
Absolute offset of the segment’s first row within the result set. |
|
number |
Number of rows in the segment. |
|
number |
Size of the segment in bytes, as stored or transferred. |
|
number |
Size of the segment in bytes after decompression. Present only for compressed encodings. |
|
string |
Time after which a spooled segment may no longer be retrievable from storage. |
Early pages of a large result are often returned as inline segments even when
spooling is requested, because small results do not benefit from spooling. The
server controls this with protocol.spooling.inlining.enabled, which defaults to true.
Retrieving a spooled segment#
How a client fetches the data for a spooled segment depends on the server’s
protocol.spooling.retrieval-mode. STORAGE is the default, and the other modes
change where the data is read from:
Mode |
Description |
Required client network access |
|---|---|---|
|
The segment |
Object storage |
|
The |
Coordinator and object storage |
|
The |
Coordinator only |
|
The |
Coordinator and workers |
In STORAGE and COORDINATOR_STORAGE_REDIRECT, the client reads from object
storage directly, so it must have network access to the configured storage
location. In COORDINATOR_PROXY and WORKER_PROXY, the cluster reads from
storage on the client’s behalf, so the client does not need storage access.
A spooled segment may include a headers map. These headers are opaque to the
client. It must send them unchanged on the retrieval request, and on the
acknowledgement request, without interpreting or transforming them.
Acknowledging a segment#
After reading a spooled segment, a client must acknowledge processing with a
GET to the segment’s ackUri, sending the same headers that came with the
segment. This lets the server release the segment from object storage rather than
waiting for TTL-based cleanup.
The ackUri, like the segment uri, is opaque. A client issues the GET to it
exactly as provided. An intermediary between the client and Trino, such as a load
balancer, router, or Trino Gateway, must forward requests to these URIs so that
segment retrieval and acknowledgement reach their destination.
A correct client acknowledges every segment it reads. Whether the server acts on the acknowledgement immediately or falls back to TTL-based cleanup is a server-side decision.
Important QueryResults attributes#
The most important attributes of the QueryResults JSON document returned by
the REST API endpoints are listed in this table. For more details, refer to the
class io.trino.client.QueryResults in module trino-client in the
client directory of the Trino source code.
Attribute |
Description |
|---|---|
|
The ID of the query. |
|
If present, the URL to use for subsequent |
|
A list of the names and types of the columns returned by the query. |
|
The |
|
A human-readable string representing the operation. For a |
|
If query failed, the |
QueryResults diagnostic attributes#
These QueryResults data members may be useful in tracking down problems:
Attribute |
Type |
Description |
|---|---|---|
|
|
Non-null only if the query resulted in an error. |
|
|
|
|
|
A usually empty list of warnings. |
|
|
A class containing statistics about the query execution. Of particular
interest is |
Client request headers#
This table lists all supported client request headers. Many of the headers can be updated in the client as response headers, and supplied in subsequent requests, just like browser cookies.
Header name |
Description |
|---|---|
|
Specifies the session user. If not supplied, the session user is automatically determined via User mapping. |
|
Specifies the session’s original user. |
|
For reporting purposes, this supplies the name of the software that submitted the query. |
|
The catalog context for query processing. Set by response header
|
|
The schema context for query processing. Set by response header
|
|
The timezone for query processing. Defaults to the timezone of the Trino cluster, and not the timezone of the client. |
|
The language to use when processing the query and formatting results,
formatted as a Java |
|
Supplies a trace token to the Trino engine to help identify log lines that originate with this query request. |
|
Supplies a comma-separated list of name=value pairs as session properties.
When the Trino client runs a |
|
Sets the “role” for query processing. A “role” represents a collection of
permissions. Set by response header |
|
A comma-separated list of the name=value pairs, where the names are names of previously prepared SQL statements, and the values are keys that identify the executable form of the named prepared statements. |
|
The transaction ID to use for query processing. Set by response header
|
|
Contains arbitrary information about the client program submitting the query. |
|
A comma-separated list of “tag” strings, used to identify Trino resource groups. |
|
A comma-separated list of optional protocol features supported by the
client. Supported values include |
|
A comma-separated list of |
|
Provides extra credentials to the connector. The header is a name=value
string that is saved in the session |
|
A comma-separated list of the spooling protocol encodings the client supports, in order of preference. If set and supported by the server, results are returned with the spooling protocol; otherwise the server falls back to the direct protocol. |
Client response headers#
This table lists the supported client response headers. After receiving a response, a client must update the request headers used in subsequent requests to be consistent with the response headers received.
Header name |
Description |
|---|---|
|
Instructs the client to set the catalog in the |
|
Instructs the client to set the schema in the |
|
Instructs the client to set the session authorization user in the
|
|
Instructs the client to reset |
|
Instructs the client to set the roles of the original user in the
|
|
The value of the |
|
Instructs the client to remove the session property whose name is the value
of the |
|
Instructs the client to set |
|
Instructs the client to add the name=value pair to the set of prepared
statements in the |
|
Instructs the client to remove the prepared statement whose name is the
value of the |
|
Provides the transaction ID that the client should pass back in the
|
|
Instructs the client to clear the |
|
The spooling protocol encoding selected by the server, present when the query uses the spooling protocol. |
ProtocolHeaders#
Class io.trino.client.ProtocolHeaders in module trino-client in the
client directory of Trino source enumerates all the HTTP request and
response headers allowed by the Trino client REST API.