druid

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 23 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 druid is a database/sql driver for Apache Druid, which runs SQL on its Router or its Broker over HTTP. It registers the one name "druid", and takes a DSN of the form druid://user:pass@host:8888?key=value (D164).

db, err := sql.Open("druid", "druid://admin:pass@localhost:8888")

The driver reads only. The SQL API of Druid refuses INSERT, REPLACE, UPDATE and DELETE, and the driver returns that refusal (D163). The server binds each argument as a typed parameter (D164). The driver reads each answer as arrayLines, one line at a time, with the names and the types of the columns in its first three lines. An answer that ends with no empty line is cut short, and the driver returns an error for it (D21 and D164). Each query has an id of its own, and when the context of a query ends while the query runs, the driver cancels it on the server by that id, because the server runs a query on when its client leaves (D164). Druid has no transactions. docs/DRUID.md holds what the driver knows about the server.

Index

Constants

View Source
const ErrCut dbimp.Error = "the answer was cut short"

ErrCut is the error of an answer that ends with no empty line. The server ends an answer so when the query fails after its first rows, with HTTP 200 and no other sign (measured). After a row, it comes wrapped with dbimp.ErrIncomplete (D107 and D164).

View Source
const Name = "druid"

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 Router or of the Broker.
	Host string
	// Port is the port of the server, 8888 by default.
	Port int
	// TLS is true to speak HTTPS.
	TLS bool
	// User and Password are the credentials, which the driver sends with
	// basic authentication.
	User     string
	Password string
	// TimeZone is the time zone of each query, which the driver sends as
	// sqlTimeZone, such as UTC or Asia/Jakarta. It is UTC by default.
	TimeZone string
	// Timeout is the time that the server gives each query, which the driver
	// sends as timeout in the context of the query. Zero sends none.
	Timeout time.Duration
}

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 druid://user:pass@host:port?key=value (D27, D35 and D164). A DSN with a path is refused, because Druid has no database to choose, and so is every key that the driver does not know.

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 server. 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.

func (*Connector) Close

func (c *Connector) Close() error

Close closes the idle connections of the transport.

func (*Connector) Connect

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

Connect satisfies driver.Connector. A connection holds nothing on the server, so it sends no request.

func (*Connector) Driver

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

Driver satisfies driver.Connector.

type Driver

type Driver struct{}

Driver is the database/sql driver for Apache Druid.

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.
	HTTPStatus int
	// Code is errorCode, such as invalidInput or legacyQueryException, and
	// empty when the answer has none.
	Code string
	// Category is category, such as INVALID_INPUT, RUNTIME_FAILURE or
	// TIMEOUT, and empty when the answer has none.
	Category string
	// Persona is persona, USER or OPERATOR, the person who can fix the
	// fault, and empty when the answer has none.
	Persona string
	// Class is errorClass, the class of the exception on the server, such
	// as java.lang.ArithmeticException, and empty when the answer has none.
	Class string
	// Message is errorMessage, or the text of the answer when it has none.
	Message string
	// contains filtered or unexported fields
}

Error is an error that Druid reported. Druid answers an error that comes before any row with a status that is not 2xx and a JSON object, such as HTTP 400 with the category INVALID_INPUT for a syntax error, HTTP 500 with RUNTIME_FAILURE for a division by zero, and HTTP 504 with TIMEOUT for a query that passed its timeout (measured). A wrong password is HTTP 401 with a page of HTML, and a refused privilege is HTTP 403 (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 err matches target. It matches dbimp.ErrAuthentication for HTTP 401, which is the answer to a wrong password. HTTP 403 with Access-Check-Result, and the task state FORBIDDEN, are a missing privilege and never match (D197). The error of HTTP 401 also matches through its *dbimp.StatusError.

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns the *dbimp.StatusError of the response.

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 names the database of one statement. Druid has no database to choose (D164), so a statement with it fails with dbimp.ErrNotSupported.

func WithParameter

func WithParameter(name string, value any) Option

WithParameter sets any key of the body of POST /druid/v2/sql by its name, such as "context". The value is encoded with json/v2. A key named here replaces one that the driver sets itself, as in Couchbase. So "context" replaces the whole context that the driver sends, the id of the query and the time zone too, and the driver can then no longer cancel the query by its id.

func WithReadonly

func WithReadonly(bool) Option

WithReadonly asks that the statement write nothing. The SQL API of Druid takes no write of any kind (D163), so every statement is read-only, and the option changes nothing.

func WithTimeZone

func WithTimeZone(name string) Option

WithTimeZone sets the time zone of the query, as sqlTimeZone, as the key timezone of the DSN does (D164).

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the time that the server gives the query, as timeout in the context of the query, 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 query that runs longer with HTTP 504 and the category TIMEOUT (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