Skip to content

uptrace/go-clickhouse

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

The documentation (http://devdoc.net/database/ClickhouseDocs_19.4.1.3-docs/query_language/select/)
says FINAL has to be placed right after the table name, before SAMPLE:
  SELECT [DISTINCT] expr_list
    [FROM [db.]table | (subquery) | table_function] [FINAL]
    [SAMPLE sample_coeff]

At the moment it's appended to the end of the query which results in
invalid queries being generated when there are WHERE, ORDER BY or any
other clauses used.
31f2e73

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
ch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

ClickHouse client for Go 1.18+

build workflow PkgGoDev Documentation Chat

go-clickhouse is brought to you by uptrace/uptrace. Uptrace is an open source and blazingly fast distributed tracing tool powered by OpenTelemetry and ClickHouse. Give it a star as well!

This client uses native protocol to communicate with ClickHouse server and requires Go 1.18+ in order to use generics. This is not a database/sql driver, but the API is compatible.

Main features are:

  • ClickHouse native protocol support and efficient column-oriented design.
  • API compatible with database/sql.
  • Bun-like query builder.
  • Selecting into scalars, structs, maps, slices of maps/structs/scalars.
  • Date, DateTime, and DateTime64.
  • Array(T) including nested arrays.
  • Enums and LowCardinality(String).
  • Nullable(T) except Nullable(Array(T)).
  • Migrations.
  • OpenTelemetry support.
  • In production at Uptrace

Resources:

Benchmark

Read (best of 3 runs):

Library Timing
This library 655ms
ClickHouse/clickhouse-go 849ms

Write (best of 3 runs):

Library Timing
This library 475ms
ClickHouse/clickhouse-go 881ms

Installation

go get github.com/uptrace/go-clickhouse@latest

Example

A basic example:

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/uptrace/go-clickhouse/ch"
	"github.com/uptrace/go-clickhouse/chdebug"
)

type Model struct {
	ch.CHModel `ch:"partition:toYYYYMM(time)"`

	ID   uint64
	Text string    `ch:",lc"`
	Time time.Time `ch:",pk"`
}

func main() {
	ctx := context.Background()

	db := ch.Connect(ch.WithDatabase("test"))
	db.AddQueryHook(chdebug.NewQueryHook(chdebug.WithVerbose(true)))

	if err := db.Ping(ctx); err != nil {
		panic(err)
	}

	var num int
	if err := db.QueryRowContext(ctx, "SELECT 123").Scan(&num); err != nil {
		panic(err)
	}
	fmt.Println(num)

	if err := db.ResetModel(ctx, (*Model)(nil)); err != nil {
		panic(err)
	}

	src := &Model{ID: 1, Text: "hello", Time: time.Now()}
	if _, err := db.NewInsert().Model(src).Exec(ctx); err != nil {
		panic(err)
	}

	dest := new(Model)
	if err := db.NewSelect().Model(dest).Where("id = ?", src.ID).Limit(1).Scan(ctx); err != nil {
		panic(err)
	}
	fmt.Println(dest)
}

See also