trino

package
v0.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 28 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package trino is a database/sql driver for Trino and Presto, which run SQL over data that other systems hold, and take a statement over HTTP. It registers the one name "trino", and takes a DSN of the form trino://user@host:8080/catalog/schema?key=value (D175).

db, err := sql.Open("trino", "trino://user@localhost:8080/memory/default")

One driver serves both products. It asks GET /v1/info which one answered, and the key flavor of the DSN skips that request (D175). The driver sends a statement with POST /v1/statement, and polls the nextUri of each page until a page has none. It reads each page one token at a time. An error arrives in the body of a page with HTTP 200, and after some rows it wraps dbimp.ErrIncomplete (D107 and D175). The server runs a query on when its client leaves, so the driver sends DELETE to the nextUri when the caller closes the rows early and when the context ends (D175).

The driver sends each argument as a literal of EXECUTE name USING, on a statement that it names in the prepared-statement header (D175). It starts a transaction with START TRANSACTION and the transaction headers. The state of a session, such as USE and SET SESSION, stays on the connection, as the server asks in its response headers. docs/TRINO.md holds what the driver knows about the servers.

Index

Constants

View Source
const (
	FlavorTrino  = "trino"
	FlavorPresto = "presto"
)

The two products that the driver serves, as the key flavor names them (D173 and D175).

View Source
const Name = "trino"

Name is the name that the driver registers with database/sql, and the scheme of its DSN.

Variables

This section is empty.

Functions

func WithOptions

func WithOptions(ctx context.Context, opts ...Option) context.Context

WithOptions returns a context that carries opts. Each statement started with the context applies them after the options of the DSN.

Types

type Config

type Config struct {
	// Host is the host of the coordinator.
	Host string
	// Port is the port of the coordinator, 8080 by default and 8443 with
	// TLS.
	Port int
	// TLS is true to speak HTTPS.
	TLS bool
	// User is the user of each statement. The servers need one. Password is
	// the secret that the driver sends with basic authentication, if it is
	// not empty.
	User     string
	Password string
	// Catalog and Schema are the catalog and the schema that a connection
	// starts in. Each is empty to start in none, and Schema needs a Catalog.
	Catalog string
	Schema  string
	// Source is the source of each statement, which the server shows in its
	// list of queries. It is "dbimp" by default.
	Source string
	// TimeZone is the time zone of each statement, which the driver sends as
	// the header Time-Zone, such as UTC or Asia/Jakarta. It is empty by
	// default, and the server uses its own.
	TimeZone string
	// Timeout is the time that the server gives each statement, which the
	// driver sends as the session property query_max_execution_time. Zero
	// sends none.
	Timeout time.Duration
	// Session holds the properties of the session that each connection
	// starts with, from the keys session.<name> of the DSN.
	Session map[string]string
	// Flavor is FlavorTrino or FlavorPresto, and empty to ask the server
	// with GET /v1/info.
	Flavor string
}

Config is the configuration of a connector, which the DSN holds. The caller owns it (D7).

func ParseDSN

func ParseDSN(dsn string) (*Config, error)

ParseDSN parses a DSN of the form trino://user@host:port/catalog/schema?key=value (D27, D35 and D175). The path names the catalog and the schema, and each is optional. A key that the driver does not know is refused.

func (*Config) FormatDSN

func (cfg *Config) FormatDSN() string

FormatDSN returns the DSN of cfg. ParseDSN reads it back as cfg, for a Config that ParseDSN or NewConnector filled.

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

Connector opens connections to one coordinator. It owns its transport, and every connection shares it. A caller can build one from a Config and open it with sql.OpenDB.

func NewConnector

func NewConnector(cfg Config) *Connector

NewConnector returns a Connector for cfg. The connector keeps a copy of cfg, and fills each field that is zero with its default. Session is copied.

func (*Connector) Close

func (c *Connector) Close() error

Close closes the idle connections of the transport.

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context) (driver.Conn, error)

Connect satisfies driver.Connector. It asks the server which product it is, once, unless the DSN says. A connection holds its state on the client.

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver satisfies driver.Connector.

func (*Connector) Flavor

func (c *Connector) Flavor() string

Flavor returns FlavorTrino or FlavorPresto, as the DSN names it or as the server answered, and "" while neither is known.

type Driver

type Driver struct{}

Driver is the database/sql driver for Trino and Presto.

func (Driver) Open

func (Driver) Open(string) (driver.Conn, error)

Open satisfies driver.Driver. database/sql opens a connection through OpenConnector, and Open returns an error, because opening a connection needs a context.

func (Driver) OpenConnector

func (Driver) OpenConnector(dsn string) (driver.Connector, error)

OpenConnector satisfies driver.DriverContext. It parses the DSN.

type Error

type Error struct {
	// HTTPStatus is the status code of the response, and 0 for an error that
	// came in the body of a page.
	HTTPStatus int
	// Name is errorName, such as SYNTAX_ERROR or TABLE_NOT_FOUND, and empty
	// for an error of the protocol.
	Name string
	// Type is errorType, such as USER_ERROR, and empty for an error of the
	// protocol.
	Type string
	// Code is errorCode, a number that the product gives the name, and 0 for
	// an error of the protocol.
	Code int
	// Message is message, or the text of the answer for an error of the
	// protocol.
	Message string
	// Line and Column are the place of the error in the statement, from
	// errorLocation, and 0 when the error has no place.
	Line   int
	Column int
	// contains filtered or unexported fields
}

Error is an error that Trino or Presto reported. A statement that fails answers HTTP 200 and a page with an error member, before any row or after some (measured). An error of the protocol has the status of HTTP, and a body of plain text or, on Presto, a page of HTML: HTTP 400 for an empty statement or a user that Presto lacks, HTTP 401 for a user that Trino lacks, HTTP 404 for a query or a token that the server does not know, and HTTP 410 for a nextUri that the client read before (measured).

func (*Error) Error

func (err *Error) Error() string

Error satisfies the error interface.

func (*Error) Is added in v0.18.0

func (err *Error) Is(target error) bool

Is reports whether target is dbimp.ErrAuthentication, which the error matches for HTTP 401 only. Trino answers 401 for a request with no valid credential, and an error in the body of a page, such as PERMISSION_DENIED, never matches (D197).

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns the *dbimp.StatusError of the response, if the error has one.

type Option

type Option = dbimp.Option[options]

Option sets an option of one statement (D109). An option comes from the DSN, then from the context through WithOptions, then from an argument of the statement, and a later one wins.

func WithDatabase

func WithDatabase(name string) Option

WithDatabase sets the catalog of one statement, as the first part of the path of the DSN does. Neither server has a database, and the catalog is the nearest name (D175).

func WithParameter

func WithParameter(name string, value any) Option

WithParameter sets a property of the session for one statement by its name, such as "query_max_run_time", as the keys session.<name> of the DSN do. The servers take no body of keys, so the property of the session is the setting that a caller names. The value is written with fmt.Sprint. A property named here replaces the one of the connection, and a name that the server does not know fails the statement with INVALID_SESSION_PROPERTY (measured). WithParameter(name, nil) writes no value, and fails the statement with dbimp.ErrInvalidValue.

func WithReadonly

func WithReadonly(readonly bool) Option

WithReadonly asks that the statement write nothing. Neither server has a setting for one statement. A transaction can be read only, which BeginTx sets from sql.TxOptions, so WithReadonly(true) fails the statement with dbimp.ErrNotSupported (D109).

func WithSchema

func WithSchema(name string) Option

WithSchema sets the schema of one statement, as the second part of the path of the DSN does.

func WithSource

func WithSource(name string) Option

WithSource sets the source of one statement, as the key source of the DSN does.

func WithTimeZone

func WithTimeZone(name string) Option

WithTimeZone sets the time zone of one statement, as the key timezone of the DSN does.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the time that the server gives the statement, as the session property query_max_execution_time, as the key timeout of the DSN does. The server counts whole milliseconds, so the driver rounds d up to the next millisecond. The server stops a statement that runs longer with the error EXCEEDED_TIME_LIMIT (measured). Zero sends no timeout, and the server uses its own.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL