clickhouse

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 29 Imported by: 0

Documentation

Overview

Package clickhouse is a database/sql driver for ClickHouse, which takes a statement over its HTTP interface. It registers the one name "clickhouse", and takes a DSN of the form clickhouse://user:password@host:8123/database (D177).

db, err := sql.Open("clickhouse", "clickhouse://default:secret@localhost:8123/default")

The driver sends a statement with POST /, and reads the answer in the format JSONCompactEachRowWithNamesAndTypes, one row at a time, with the settings that make each value exact (D176). It binds each argument as a typed parameter of the server, such as {p1:Int64} with param_p1 in the query string (D176). An error after some rows comes from the marker that the server writes in the stream, and wraps dbimp.ErrIncomplete (D107 and D176). The server runs a query on when its client leaves, so the driver names each query and sends KILL QUERY when the context ends and when the caller closes the rows early (D176). The server has no transaction, so BeginTx fails with dbimp.ErrNotSupported (D177). docs/CLICKHOUSE.md holds what the driver knows about the server.

Index

Constants

View Source
const Name = "clickhouse"

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 server.
	Host string
	// Port is the port of the HTTP interface, 8123 by default and 8443 with
	// TLS.
	Port int
	// TLS is true to speak HTTPS.
	TLS bool
	// User and Password are the credentials, which the driver sends with
	// basic authentication, if either one is not empty. With neither, the
	// server uses its user default.
	User     string
	Password string
	// Database is the database of each statement, and empty to leave the
	// choice to the server.
	Database 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 clickhouse://user:password@host:port/database?tls=true (D27, D35 and D177). The path is the database, and it is optional. The one key is tls, and the driver refuses any other.

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(ctx context.Context) (driver.Conn, error)

Connect satisfies driver.Connector. It runs SELECT version() once for the connection, which tells how the server frames an error after some rows (D177).

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

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 the code of the error, such as 60, from the header
	// X-ClickHouse-Exception-Code or from the text. It is 0 when neither
	// holds one.
	Code int
	// Name is the name of the code, such as UNKNOWN_TABLE, and empty when the
	// text has none.
	Name string
	// Message is the text of the error without the code, the name and the
	// version.
	Message string
	// contains filtered or unexported fields
}

Error is an error that ClickHouse reported. The text of each error has the form "Code: 60. DB::Exception: Table t does not exist. (UNKNOWN_TABLE) (version 26.9.2.8 (official build))" (measured). An error that comes before the first byte of the body has the status of HTTP that follows its code: 400 for a syntax error, 404 for an unknown table, database or setting, 401 or 403 for a wrong password, 500 for a type error, and 501 for a feature that is not there. An error after some rows has HTTP 200, or HTTP 500 on 26.9, and the text comes after the rows (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 when the server refused the credential: the code 194 or 516, or HTTP 401 (D197). A missing privilege, code 497 with HTTP 403, does not match.

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 database of one statement, as the path of the DSN does, with the query key database.

func WithParameter

func WithParameter(name string, value any) Option

WithParameter sets a setting of the server for one statement by its name, such as "max_threads". The value is written with fmt.Sprint, and a bool is 1 or 0. A name that the driver sets itself, such as "query_id", takes this value in its place. So a caller who replaces "default_format" breaks the reading of the answer, and a caller who replaces "query_id" names the query that the driver cancels. A name that the server does not know fails the statement with the code 115 (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, with the setting readonly set to 1 (D109). The server then refuses a write with the code 164, and a change of a setting in the text of the statement, such as a SETTINGS clause. It still accepts the settings in the query string, which are the settings of the driver (measured).

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the time that the server gives the statement, as the setting max_execution_time, which counts seconds with a fraction. The server stops a statement that runs longer with the code 159, which is HTTP 408 on 26.9, and an error after the rows on 25.3 and 25.8 (measured). Zero sends no timeout, and the server uses its own. The DSN has no key for it, because a user who cannot change the setting gets an error (D176).

Jump to

Keyboard shortcuts

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