Documentation
¶
Index ¶
- Constants
- Variables
- func Connector(opt *Options) driver.Connector
- func Context(parent context.Context, options ...QueryOption) context.Context
- func DateNamed(name string, value time.Time, scale TimeUnit) driver.NamedDateValue
- func ExtractJSONPathAs[T any](o *JSON, path string) (valueAs T, ok bool)
- func Named(name string, value any) driver.NamedValue
- func Open(opt *Options) (driver.Conn, error)
- func OpenDB(opt *Options) *sql.DB
- type ArraySet
- type AsyncOptions
- type Auth
- type ClientInfo
- type ClusterCredentials
- type ColumnNameAndType
- type Compression
- type CompressionMethod
- type Conn
- type ConnOpenStrategy
- type CustomSetting
- type Dial
- type DialResult
- type Dynamic
- type Exception
- type GetJWTFunc
- type GroupSet
- type HTTPError
- type HTTPProxy
- type HTTPReaderWriter
- type JSON
- type JSONDeserializer
- type JSONSerializer
- type Log
- type OpError
- type Options
- type Parameters
- type Pool
- type ProfileEvent
- type ProfileInfo
- type Progress
- type Protocol
- type QueryOption
- func WithAsync(wait bool) QueryOption
- func WithBlockBufferSize(size uint8) QueryOption
- func WithClientInfo(ci ClientInfo) QueryOption
- func WithColumnNamesAndTypes(columnNamesAndTypes []ColumnNameAndType) QueryOption
- func WithExternalTable(t ...*ext.Table) QueryOption
- func WithInitialUser(user string) QueryOption
- func WithJWT(jwt string) QueryOption
- func WithLogs(fn func(*Log)) QueryOption
- func WithParameters(params Parameters) QueryOption
- func WithProfileEvents(fn func([]ProfileEvent)) QueryOption
- func WithProfileInfo(fn func(*ProfileInfo)) QueryOption
- func WithProgress(fn func(*Progress)) QueryOption
- func WithQueryID(queryID string) QueryOption
- func WithQuotaKey(quotaKey string) QueryOption
- func WithSettings(settings Settings) QueryOption
- func WithSpan(span trace.SpanContext) QueryOption
- func WithStdAsync(wait bool) QueryOptiondeprecated
- func WithUserLocation(location *time.Location) QueryOption
- func WithoutProfileEvents() QueryOption
- type QueryOptions
- type ServerVersion
- type Settings
- type TimeUnit
- type Variant
Examples ¶
Constants ¶
const ( CompressionNone = CompressionMethod(compress.None) CompressionLZ4 = CompressionMethod(compress.LZ4) CompressionLZ4HC = CompressionMethod(compress.LZ4HC) CompressionZSTD = CompressionMethod(compress.ZSTD) CompressionGZIP = CompressionMethod(0x95) CompressionDeflate = CompressionMethod(0x96) CompressionBrotli = CompressionMethod(0x97) )
const ( ClientVersionMajor = 2 ClientVersionMinor = 50 ClientVersionPatch = 0 ClientTCPProtocolVersion = proto.DBMS_TCP_PROTOCOL_VERSION )
const ClientName = "clickhouse-go"
Variables ¶
var ( ErrBatchInvalid = errors.New("clickhouse: batch is invalid. check appended data is correct") ErrBatchAlreadySent = errors.New("clickhouse: batch has already been sent") ErrBatchNotSent = errors.New("clickhouse: invalid retry, batch not sent yet") ErrAcquireConnTimeout = errors.New("clickhouse: acquire conn timeout. you can increase the number of max open conn or the dial timeout") ErrUnsupportedServerRevision = errors.New("clickhouse: unsupported server revision") ErrBindMixedParamsFormats = errors.New("clickhouse [bind]: mixed named, numeric or positional parameters") ErrAcquireConnNoAddress = errors.New("clickhouse: no valid address supplied") ErrServerUnexpectedData = errors.New("code: 101, message: Unexpected packet Data received from client") ErrConnectionClosed = errors.New("clickhouse: connection is closed") ErrClusterSecretRequiresName = errors.New("clickhouse: cluster secret requires a cluster name") ErrClusterSecretNeedsNative = errors.New("clickhouse: cluster secret is only supported with the native protocol") ErrClusterSecretRequiresUsername = errors.New("clickhouse: cluster secret requires an explicit auth username so the impersonated user is never the implicit \"default\"") ErrClusterSecretWithJWT = errors.New("clickhouse: cluster secret cannot be combined with JWT authentication") ErrFormatNativeUnsupported = errors.New("clickhouse: QueryFormat and InsertFormat are only supported over the HTTP protocol, where the server converts every format; connect with Options{Protocol: clickhouse.HTTP} or an http:// DSN") )
var ( ErrInvalidValueInNamedDateValue = errors.New("invalid value in NamedDateValue for query parameter") ErrUnsupportedQueryParameter = errors.New("unsupported query parameter type") )
var (
ErrInvalidTimezone = errors.New("invalid timezone value")
)
Functions ¶
func Context ¶
func Context(parent context.Context, options ...QueryOption) context.Context
Context returns a derived context with the given ClickHouse QueryOptions. Existing QueryOptions will be overwritten per option if present. The QueryOptions Settings map will be initialized if nil.
func DateNamed ¶ added in v2.1.1
DateNamed is Named for a time.Time with the precision chosen by you instead of inferred from the value: the scale decides how many fractional digits are sent (Seconds none, MilliSeconds 3, and so on), and anything finer is dropped. Pick the scale that matches the parameter's type — Seconds for `DateTime`, MilliSeconds for `DateTime64(3)`. Like Named, the moment the value points to is preserved regardless of timezones.
func ExtractJSONPathAs ¶ added in v2.31.0
ExtractJSONPathAs is a convenience function for asserting a path to a specific type. The underlying value is also extracted from its Dynamic wrapper if present. T cannot be a Dynamic, if you want a Dynamic simply use ExtractJSONPathAsDynamic.
func Named ¶
func Named(name string, value any) driver.NamedValue
Named gives a query argument a name. It works with both placeholder styles: with server-side query parameters (`{name:Type}`) the value is sent to the server separately from the query, with client-side binding (`@name`) it is written into the query text as a SQL literal.
Either way, a time.Time keeps the moment it points to, whatever timezone it or the target carries: query parameters send it as epoch seconds, and client-side binding emits a SQL form that carries the zone when it needs to. On the query-parameter path, sub-second precision is kept when the value has any — fine for `DateTime64`, but a plain `DateTime` parameter rejects fractions. Use DateNamed to choose the precision yourself.
Types ¶
type AsyncOptions ¶ added in v2.33.1
type AsyncOptions struct {
// contains filtered or unexported fields
}
type ClientInfo ¶ added in v2.6.0
func (ClientInfo) Append ¶ added in v2.42.0
func (a ClientInfo) Append(b ClientInfo) ClientInfo
Append returns a new copy of the combined ClientInfo structs
func (ClientInfo) String ¶ added in v2.6.0
func (o ClientInfo) String() string
type ClusterCredentials ¶ added in v2.49.0
type ClusterCredentials struct {
// Name is the configured cluster name.
Name string
// Secret is the shared secret. Empty disables interserver mode.
Secret string
}
ClusterCredentials configures interserver-secret authentication.
func (ClusterCredentials) GoString ¶ added in v2.49.0
func (c ClusterCredentials) GoString() string
GoString redacts Secret for Go-syntax formatting.
func (ClusterCredentials) String ¶ added in v2.49.0
func (c ClusterCredentials) String() string
String redacts Secret.
type ColumnNameAndType ¶ added in v2.37.0
ColumnNameAndType represents a column name and type
type Compression ¶
type Compression struct {
Method CompressionMethod
// this only applies to lz4, lz4hc, zlib, zstd, and brotli compression algorithms
Level int
}
type CompressionMethod ¶ added in v2.3.0
type CompressionMethod byte
func (CompressionMethod) String ¶ added in v2.3.0
func (c CompressionMethod) String() string
type ConnOpenStrategy ¶
type ConnOpenStrategy uint8
const ( ConnOpenInOrder ConnOpenStrategy = iota ConnOpenRoundRobin ConnOpenRandom )
type CustomSetting ¶ added in v2.9.3
type CustomSetting struct {
Value string
}
CustomSetting is a helper struct to distinguish custom settings from important ones. For native protocol, is_important flag is set to value 0x02 (see https://github.com/ClickHouse/ClickHouse/blob/c873560fe7185f45eed56520ec7d033a7beb1551/src/Core/BaseSettings.h#L516-L521) Only string value is supported until formatting logic that exists in ClickHouse is implemented in clickhouse-go. (https://github.com/ClickHouse/ClickHouse/blob/master/src/Core/Field.cpp#L312 and https://github.com/ClickHouse/clickhouse-go/issues/992)
type DialResult ¶ added in v2.5.0
type DialResult struct {
// contains filtered or unexported fields
}
func DefaultDialStrategy ¶ added in v2.5.0
type Dynamic ¶ added in v2.30.1
Dynamic is an alias for the Variant type
func ExtractJSONPathAsDynamic ¶ added in v2.38.0
ExtractJSONPathAsDynamic is a convenience function for asserting a path to a Dynamic. If the value is not a Dynamic, the value is wrapped in an untyped Dynamic with false returned.
func NewDynamic ¶ added in v2.30.1
NewDynamic creates a new Dynamic with the given value
func NewDynamicWithType ¶ added in v2.30.1
NewDynamicWithType creates a new Dynamic with the given value and ClickHouse type
type HTTPError ¶ added in v2.48.0
HTTPError is returned for requests over the HTTP protocol that fail with a non-200 status code. Err is a *Exception when the response body parsed as a ClickHouse server exception, otherwise a plain error carrying the raw body.
Use errors.As to branch on either layer:
var httpErr *clickhouse.HTTPError
var chErr *clickhouse.Exception
if errors.As(err, &chErr) { /* chErr.Code — works on native and HTTP */ }
if errors.As(err, &httpErr) { /* httpErr.StatusCode — HTTP only */ }
Note that a query can also fail after the server has started streaming a 200 response; the status code was flushed before the query failed, so it carries no signal. Such mid-stream failures surface as a bare *Exception, not an HTTPError — StatusCode is therefore always the status of a genuine non-200 response.
Example ¶
Server-side failures carry a typed *clickhouse.Exception on both the native and HTTP protocols. Over HTTP, a non-200 response is additionally wrapped in *clickhouse.HTTPError; exceptions that occur after the server started streaming a 200 response surface as a bare *clickhouse.Exception.
package main
import (
"context"
"errors"
"fmt"
"github.com/ClickHouse/clickhouse-go/v2"
)
func main() {
conn, err := clickhouse.Open(&clickhouse.Options{
Protocol: clickhouse.HTTP,
Addr: []string{"localhost:8123"},
})
if err != nil {
panic(err)
}
defer conn.Close()
_, err = conn.Query(context.Background(), "SELECT * FROM non_existent_table")
var chErr *clickhouse.Exception
if errors.As(err, &chErr) {
// works on the native protocol too; CodeName (e.g. "UNKNOWN_TABLE")
// is best-effort and currently only set over HTTP
fmt.Println(chErr.Code, chErr.CodeName, chErr.Message)
}
var httpErr *clickhouse.HTTPError
if errors.As(err, &httpErr) {
fmt.Println(httpErr.StatusCode)
}
}
Output:
type HTTPReaderWriter ¶ added in v2.3.0
type HTTPReaderWriter struct {
// contains filtered or unexported fields
}
type JSON ¶ added in v2.30.1
JSON represents a ClickHouse JSON type that can hold multiple possible types
type JSONDeserializer ¶ added in v2.31.0
type JSONDeserializer = chcol.JSONDeserializer
JSONDeserializer interface allows a struct to load its data from an optimized JSON structure instead of relying on recursive reflection to set its fields.
type JSONSerializer ¶ added in v2.31.0
type JSONSerializer = chcol.JSONSerializer
JSONSerializer interface allows a struct to be manually converted to an optimized JSON structure instead of relying on recursive reflection. Note that the struct must be a pointer in order for the interface to be matched, reflection will be used otherwise.
type Options ¶
type Options struct {
Protocol Protocol
ClientInfo ClientInfo
TLS *tls.Config
Addr []string
Auth Auth
// Cluster configures native interserver-secret authentication.
Cluster ClusterCredentials
DialContext func(ctx context.Context, addr string) (net.Conn, error)
DialStrategy func(ctx context.Context, connID int, options *Options, dial Dial) (DialResult, error)
// Deprecated: Use Logger instead. Debug enables legacy debug logging to stdout.
// For structured logging with levels, use the Logger field.
Debug bool
// Deprecated: Use Logger instead. Debugf provides a custom debug logging function.
// For structured logging with levels and custom handlers, use the Logger field with
// a custom slog.Handler.
Debugf func(format string, v ...any)
// Logger provides structured logging using Go's standard log/slog package.
// If nil, no logging occurs (default). To enable logging, provide a configured
// slog.Logger:
//
// logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
// Level: slog.LevelDebug,
// }))
// opts := &clickhouse.Options{
// Logger: logger,
// }
//
// For backward compatibility, if Debug=true and Debugf is set, those will be used
// instead of Logger.
Logger *slog.Logger
Settings Settings
Compression *Compression
DialTimeout time.Duration // default 30 second
MaxOpenConns int // default MaxIdleConns + 5
MaxIdleConns int // default 5
ConnMaxLifetime time.Duration // default 1 hour
ConnOpenStrategy ConnOpenStrategy
FreeBufOnConnRelease bool // drop preserved memory buffer after each query
HttpHeaders map[string]string // set additional headers on HTTP requests
HttpUrlPath string // set additional URL path for HTTP requests
HttpMaxConnsPerHost int // MaxConnsPerHost for http.Transport
BlockBufferSize uint8 // default 2 - can be overwritten on query
MaxCompressionBuffer int // default 10485760 - measured in bytes i.e.
// HTTPProxy specifies an HTTP proxy URL to use for requests made by the client.
HTTPProxyURL *url.URL
// GetJWT should return a JWT for authentication with ClickHouse Cloud.
// This is called per connection/request, so you may cache the token in your app if needed.
// Use this instead of Auth.Username and Auth.Password if you're using JWT auth.
GetJWT GetJWTFunc
// ReadTimeout is the maximum duration the client will wait for ClickHouse
// to respond to a single Read call for bytes over the connection.
// Can be overridden with context.WithDeadline.
ReadTimeout time.Duration
// Set a custom transport for the http client.
// The default transport configured by the library is passed in as an argument.
TransportFunc func(*http.Transport) (http.RoundTripper, error)
// contains filtered or unexported fields
}
type Parameters ¶ added in v2.5.0
type ProfileEvent ¶
type ProfileInfo ¶
type ProfileInfo = proto.ProfileInfo
type QueryOption ¶
type QueryOption func(*QueryOptions) error
func WithAsync ¶ added in v2.41.0
func WithAsync(wait bool) QueryOption
WithAsync enables async inserts. It sets `async_insert` to 1. When wait is true, it sets `wait_for_async_insert` to 1, and the insert returns after the server writes the data. When wait is false, it sets `wait_for_async_insert` to 0, and the insert returns after the server receives the data.
WithAsync applies to Exec and to batches from PrepareBatch, on the Native and HTTP protocols. It overrides the same settings from WithSettings.
A batch uses only the context that you give to PrepareBatch. With database/sql, a batch uses only the context that you give to PrepareContext. The context that you give to Stmt.ExecContext has no effect on the batch.
func WithBlockBufferSize ¶ added in v2.4.0
func WithBlockBufferSize(size uint8) QueryOption
func WithClientInfo ¶ added in v2.42.0
func WithClientInfo(ci ClientInfo) QueryOption
WithClientInfo appends client info data to the query, visible in the system.query_log table. This does not replace the client info provided in the connection options, it appends to it. Can be called multiple times to append more info.
func WithColumnNamesAndTypes ¶ added in v2.37.0
func WithColumnNamesAndTypes(columnNamesAndTypes []ColumnNameAndType) QueryOption
WithColumnNamesAndTypes supplies metadata for HTTP inserts.
func WithExternalTable ¶
func WithExternalTable(t ...*ext.Table) QueryOption
func WithInitialUser ¶ added in v2.49.0
func WithInitialUser(user string) QueryOption
WithInitialUser sets the user for interserver-secret queries.
func WithJWT ¶ added in v2.35.0
func WithJWT(jwt string) QueryOption
WithJWT overrides the existing authentication with the given JWT. This only applies for clients connected with HTTPS to ClickHouse Cloud.
func WithLogs ¶
func WithLogs(fn func(*Log)) QueryOption
func WithParameters ¶ added in v2.5.0
func WithParameters(params Parameters) QueryOption
func WithProfileEvents ¶
func WithProfileEvents(fn func([]ProfileEvent)) QueryOption
func WithProfileInfo ¶
func WithProfileInfo(fn func(*ProfileInfo)) QueryOption
func WithProgress ¶
func WithProgress(fn func(*Progress)) QueryOption
func WithQueryID ¶
func WithQueryID(queryID string) QueryOption
func WithQuotaKey ¶
func WithQuotaKey(quotaKey string) QueryOption
func WithSettings ¶
func WithSettings(settings Settings) QueryOption
func WithSpan ¶
func WithSpan(span trace.SpanContext) QueryOption
func WithStdAsync
deprecated
func WithStdAsync(wait bool) QueryOption
Deprecated: use `WithAsync` instead.
func WithUserLocation ¶ added in v2.7.0
func WithUserLocation(location *time.Location) QueryOption
func WithoutProfileEvents ¶ added in v2.44.0
func WithoutProfileEvents() QueryOption
WithoutProfileEvents instructs the server not to send profile events for this query. This is a performance optimization for servers >= 25.11 that support the send_profile_events setting. On older servers, the setting is unknown and the server will return an error.
type QueryOptions ¶
type QueryOptions struct {
// contains filtered or unexported fields
}
type ServerVersion ¶
type ServerVersion = proto.ServerHandshake
type Variant ¶ added in v2.30.1
Variant represents a ClickHouse Variant type that can hold multiple possible types
func NewVariant ¶ added in v2.30.1
NewVariant creates a new Variant with the given value
func NewVariantWithType ¶ added in v2.30.1
NewVariantWithType creates a new Variant with the given value and ClickHouse type
Source Files
¶
- batch.go
- bind.go
- chcol.go
- clickhouse.go
- clickhouse_options.go
- clickhouse_rows.go
- clickhouse_rows_column_type.go
- clickhouse_std.go
- client_info.go
- conn.go
- conn_async_insert.go
- conn_batch.go
- conn_check.go
- conn_exec.go
- conn_format.go
- conn_handshake.go
- conn_http.go
- conn_http_async_insert.go
- conn_http_batch.go
- conn_http_errors.go
- conn_http_exec.go
- conn_http_format.go
- conn_http_query.go
- conn_logs.go
- conn_ping.go
- conn_pool.go
- conn_process.go
- conn_profile_events.go
- conn_query.go
- conn_send_query.go
- context.go
- context_watchdog.go
- format.go
- jwt.go
- logger.go
- query_parameters.go
- scan.go
- struct_map.go
Directories
¶
| Path | Synopsis |
|---|---|
|
benchmark
|
|
|
v2/read
command
|
|
|
v2/read-native
command
|
|
|
v2/write
command
|
|
|
v2/write-async
command
|
|
|
v2/write-async-std
command
|
|
|
v2/write-native
command
|
|
|
v2/write-native-columnar
command
|
|
|
v2/write-native-struct
command
|
|
|
examples
|
|
|
internal
|
|
|
cmd/release
command
|
|
|
lib
|
|
|
cityhash102
* COPY from https://github.com/zentures/cityhash/
|
* COPY from https://github.com/zentures/cityhash/ |
|
column/codegen
command
|
|
|
issues/209
command
|
|
|
issues/360
command
|
|
|
issues/470
command
|
|
|
issues/476
command
|
|
|
issues/484
command
|
|
|
issues/485
command
|
|
|
stress
command
|