Trino Go Client

repository·master·Indexed 41 days ago

https://github.com/trinodb/trino-go-client

A native Go implementation of a Trino database/sql driver that enables Go applications to execute SQL statements against Trino clusters via HTTP/HTTPS. It supports HTTP Basic, Kerberos, and JWT authentication, as well as the Trino spooling protocol for efficient retrieval of large datasets. Requires Go 1.25.5+ and Trino 372+.

Tokens
2.5K
Snippets
9
Records
16
Agent score
2%

What's inside Trino Go Client

  1. Construct a Data Source Name (DSN)

    master

    The DSN is a URL used to connect to Trino. The format is: http[s]://user[:pass]@host[:port][?parameters]

    Parameters are case-sensitive. You can use the Config.FormatDSN helper function to build a DSN from a trino.Config struct.

  2. Configure HTTP Basic authentication

    master

    If your DSN includes a password (e.g., http://user:pass@host), the client automatically enables HTTP Basic authentication by setting the Authorization header.

    Note: HTTP Basic authentication is only supported on encrypted connections over HTTPS.

  3. Use the Trino Spooling Protocol for large result sets

    master

    The client supports the Trino spooling protocol, which allows efficient retrieval of large datasets by downloading segments in parallel and potentially out-of-order. The client uses json encoding by default if the server supports it.

    Configuring Encodings

    You can specify the encoding via sql.Named("encoding", ...) arguments. Supported encodings include json, json+lz4, and json+zstd.

    You can provide a single encoding or a comma-separated list of preferred encodings.

    // Single encoding
    rows, err := db.Query(query, sql.Named("encoding", "json+zstd"))
    
    // Preference list
    rows, err := db.Query(query, sql.Named("encoding", "json+zstd, json+lz4, json"))
  4. Forward Authorization Headers using JWT

    master

    To forward an access token via an authorization header instead of including it in the query text, follow these two steps:

    1. Add a sql.NamedArg with the name accessToken (e.g., sql.Named("accessToken", "<your_token>")).
    2. Set the ForwardAuthorizationHeader field to true in the trino.Config struct.

    This configuration overrides the AccessToken set in the Config struct and prevents the token from being logged in Trino's query history.

  5. Register and use a custom HTTP client

    master

    You can use a custom http.Client (e.g., for custom TLS configs, proxies, or OpenTelemetry instrumentation) by registering it with trino.RegisterCustomClient and referencing it in the DSN via the custom_client parameter.

    // Register the client
    trino.RegisterCustomClient("foobar", foobarClient)
    
    // Use it in the DSN
    db, err := sql.Open("trino", "https://user@localhost:8080?custom_client=foobar")
  6. Basic usage with database/sql

    master

    The Trino client implements the Go database/sql/driver interface. To use it, import the package with a blank identifier and use sql.Open with trino as the driver name and a valid DSN as the data source name.

    import "database/sql"
    import _ "github.com/trinodb/trino-go-client/trino"
    
    dsn := "http://user@localhost:8080?catalog=default&schema=test"
    db, err := sql.Open("trino", dsn)
  7. Configure Trino roles via DSN or Query Parameters

    master

    Roles can be assigned per catalog for the session.

    Via DSN: Use the roles parameter with the format catalog1:role1;catalog2:role2.

    Via Query Parameters: Override DSN roles by adding a sql.NamedArg with the key X-Trino-Role containing a map[string]string of catalog-to-role assignments.

    // Via Config struct (for DSN generation)
    c := &Config{
    	ServerURI: "https://foobar@localhost:8090",
    	Roles:     map[string]string{"catalog1": "role1", "catalog2": "role2"},
    }
    dsn, _ := c.FormatDSN()
    
    // Via Query parameter (overrides DSN)
    sql.Named("X-Trino-Role", map[string]string{
        "catalog1": "role1",
        "catalog2": "role2",
    })
  8. Pass per-query user information for access control

    master

    To execute a query as a user different from the authenticated principal, add a sql.NamedArg with the key X-Trino-User to your query parameters. This value is used by the driver to inform Trino of the executing user but is not passed as part of the SQL query text itself.

    db.Query("SELECT * FROM foobar WHERE id=?", 1, sql.Named("X-Trino-User", string("Alice")))