Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

A configured Trino client.

Created with ClientBuilder. Cheap to share: it wraps a connection-pooled HTTP client, so build one and reuse it for all queries. The main entry points are get_all (buffer the result), stream (stream it lazily) and execute (run a statement).

Implementations§

Source§

impl Client

Source

pub async fn stream<'a, T>( &'a self, sql: impl Into<String>, ) -> Result<RowStream<'a, T>>
where for<'de> T: Trino + Send + 'static + Deserialize<'de>,

Execute sql and stream the resulting rows lazily, page by page, without buffering the whole result set in memory.

Trino returns results as a chain of pages linked by nextUri. This method first drives the query far enough to resolve the result schema (so RowStream::columns is available up front), then hands back a RowStream that follows the remaining pages on demand, yielding each row as it is decoded. Prefer it over Client::get_all for large result sets.

Both the Direct and (with the spooling feature) Spooled protocols are supported. With spooling, rows are still materialized one segment at a time rather than for the entire query, keeping peak memory bounded.

Unlike Client::get_all, this does not reject a query that mixes the Direct and Spooled protocols across pages; each page is decoded according to its own protocol.

The returned stream borrows self, so it must not outlive the Client.

§Example
use futures::StreamExt;

let client = ClientBuilder::new("user", "localhost").port(8080).build()?;
let mut rows = client.stream::<Row>("SELECT 1").await?;
println!("columns: {:?}", rows.columns());
while let Some(row) = rows.next().await {
    let row = row?;
    // use row
}
Source

pub async fn get_all<T>(&self, sql: impl Into<String>) -> Result<DataSet<T>>
where for<'de> T: Trino + 'static + Deserialize<'de> + Serialize,

Run sql and return the whole result set as a DataSet.

The entire result is buffered in memory — for large results prefer stream. T is a #[derive(Trino)] row struct, or Row for a dynamically-typed result.

Source

pub async fn execute(&self, sql: impl Into<String>) -> Result<ExecuteResult>

  • Execute a SQL statement and return the result.
  • If the TRINO query returns an error, the method returns an error of type Error::Query
  • @param sql The SQL statement to execute
  • @return Result<ExecuteResult>` The result of the execution
Source

pub async fn transaction_id(&self) -> TransactionId

The transaction this client’s session is currently bound to.

Source

pub async fn set_transaction_id(&self, id: TransactionId)

Bind the session to a transaction.

Normally unnecessary — begin_transaction captures the identifier Trino issues. Use this to adopt a transaction started elsewhere.

Source

pub async fn begin_transaction(&self) -> Result<()>

Start a transaction.

Issues START TRANSACTION and captures the identifier Trino returns, so statements issued afterwards on this client run inside the transaction until commit or rollback.

§Concurrency

A transaction is a property of the whole client, so treat a client as single-threaded for as long as one is open. Statements already in flight when the transaction starts do not join it, and statements issued concurrently from another task will run inside it whether or not that was intended.

The nesting check below is best-effort, not atomic: the session lock is released before START TRANSACTION is sent (holding it would deadlock against the write lock taken when the response is processed). Two tasks calling this concurrently can therefore both pass the check and open two transactions, of which only the last is retained — the other is orphaned on the coordinator until it times out. Use a separate client per transaction if you need concurrency.

§Errors

Returns Error::Transaction if a transaction is already active — Trino does not support nested transactions.

Also returns Error::Transaction if the statement succeeded but no usable identifier came back in X-Trino-Started-Transaction-Id. A healthy coordinator always sends it, so this signals something between client and coordinator dropping or rewriting the header. The transaction may be open on the coordinator, and because its identifier never reached the client it cannot be committed or rolled back — it stays open until the coordinator times it out. Surfacing that as an error is what lets Ok(()) mean a transaction is genuinely active.

When the statement itself fails the transaction may nevertheless have been started, since the identifier is captured before the statement finishes. Call rollback to discard it; that also clears an identifier the coordinator has already expired.

Source

pub async fn commit(&self) -> Result<()>

Commit the active transaction.

§Errors

Returns Error::Transaction if no transaction is active.

Source

pub async fn rollback(&self) -> Result<()>

Roll back the active transaction.

§Errors

Returns Error::Transaction if no transaction is active.

Source

pub async fn get<T>(&self, sql: impl Into<String>) -> Result<QueryResult<T>>
where for<'de> T: Trino + 'static + Deserialize<'de>,

Submit sql and return the first result page.

Low-level building block: the returned QueryResult may carry a next_uri that you must follow with get_next to retrieve the rest. Most callers should use get_all or stream, which handle pagination.

Source

pub async fn get_next<T>(&self, url: &str) -> Result<QueryResult<T>>
where for<'de> T: Trino + 'static + Deserialize<'de>,

Fetch the next result page from a next_uri returned by a previous get / get_next call.

Source

pub async fn cancel(&self, query_id: &str) -> Result<()>

Cancel a running query by its id, releasing its resources on the coordinator.

Auto Trait Implementations§

§

impl !Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl !UnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnsafeUnpin for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more