A ClickHouse client over the HTTP interface: statements, typed rows, batch inserts and the shared db interface.
import db.clickhouse
ClickHouse is a column oriented database for analytics: it scans billions of rows fast, and takes inserts in large batches. This package talks to it over its HTTP interface (port 8123, or 8443 with TLS) with net.http, so there is no driver library to install and no native protocol to speak. Results arrive as JSONCompact, with 64 bit integers quoted so that none of them loses digits.
Open a connection with Open or OpenURL, send statements with Exec, read rows with Query, and load many rows at once with a batch. Values go into the SQL through the argument functions, such as StrArg and IntArg, which escape them, or through ClickHouse's own typed {name:Type} placeholders. AsDb adapts a connection to the shared db interface. A statement that fails behind a 200 status, because the server hit an exception while already streaming the reply, is caught too: it shows in Error and Code like any other failure.
The package does not speak the native TCP protocol or RowBinary, does not compress, and has no SQL dialect in db.sql yet. Each page section below is one part of the API, and each has a runnable example.
Put values into SQL safely
Never build SQL by joining strings. Make each value with an argument function, which quotes and escapes it, and let Bind put the values into the ? places in order. Bind does not run anything, so it is also a good way to see the final text. A ? inside quotes or a comment is not a place, and when the number of values differs from the number of places the result is empty.
db-clickhouse-guide-args.salam
import db.clickhouse
funcmain:
mut args := Vector {} asVector<str>
args.Push(clickhouse.StrArg("o'brien"))
args.Push(clickhouse.IntArg(42asi64))
println clickhouse.Bind("SELECT * FROM users WHERE name = ? AND age > ?", args)
mut one := Vector {} asVector<str>
one.Push(clickhouse.IntArg(7asi64))
println clickhouse.Bind("SELECT '?' AS mark, ? /* ? */", one)
println clickhouse.QuoteIdent("my table")
println clickhouse.Bind("SELECT ?", args).Len()
end
Output
SELECT * FROM users WHERE name = 'o\'brien' AND age > 42
SELECT '?' AS mark, 7 /* ? */
`my table`
0
The quote in o'brien is escaped for you. Table and column names are not values, so they go through QuoteIdent. Exec and Query do the same binding when you give them the values.
Connect and create a table
Start from DefaultOptions (a server on this machine, user default) and change what differs. Open sends nothing yet, so call Ping to see whether the server answers. Exec runs a statement that returns no rows and gives true on success; on failure Error and Code say why.
db-clickhouse-guide-connect.salam
import db.clickhouse
funcmain:
mut o := clickhouse.DefaultOptions()
o.addr = "127.0.0.1:8123"
o.database = "default"
o.username = "default"mut c := clickhouse.Open(o)
defer c.Close()
ifnot c.Ping():
println"no ClickHouse at", o.addr
ret1endprintln c.ServerVersion()
none := Vector {} asVector<str>
ok := c.Exec(
"CREATE TABLE IF NOT EXISTS visits (day Date, page String, hits UInt32) ENGINE = MergeTree ORDER BY (day, page)",
none
)
ifnot ok:
println"create failed:", c.Code(), c.Error()
ret1endprintln"table ready"end
The table uses the MergeTree engine, the usual one, and its ORDER BY is the sort key that makes queries on day and page fast. ClickHouse has no auto increment key and no transactions to wait for; a statement is done when Exec returns.
Insert in batches
ClickHouse likes a few large inserts, not many small ones. PrepareBatch takes the INSERT INTO table (columns) part, Append adds one row of ready SQL values, and SendBatch sends all the rows in one request. Append returns false if the row has a different number of values than there are columns.
db-clickhouse-guide-insert.salam
import db.clickhouse
funcrow(day: str, page: str, hits: i64): Vector<str>:
mut r := Vector {} asVector<str>
r.Push(clickhouse.StrArg(day))
r.Push(clickhouse.StrArg(page))
r.Push(clickhouse.IntArg(hits))
ret r
endfuncmain:
mut c := clickhouse.Open(clickhouse.DefaultOptions())
defer c.Close()
mut b := c.PrepareBatch("INSERT INTO visits (day, page, hits)")
if b.Error().Len() > 0:
println b.Error()
ret1end
b.Append(row("2026-11-01", "/", 120asi64))
b.Append(row("2026-11-01", "/docs", 45asi64))
b.Append(row("2026-11-02", "/", 98asi64))
println b.Len(), "rows queued"ifnot c.SendBatch(b):
println"insert failed:", c.Error()
ret1endprintln"inserted"end
Hundreds or thousands of rows per batch is a good size to start with. A batch with no rows, or one that already has an error, is not sent.
Read the rows back
Query returns a Rows. Check Ok, then call Next in a loop and read the current row by column number with Text, Int64 and the other readers; Close it when done. The loop is until rows.Next(): because until runs while its condition is true. For a value that comes from outside, the typed placeholders {name:Type} of QueryParams let the server check the type.
db-clickhouse-guide-query.salam
import db.clickhouse
funcmain:
mut c := clickhouse.Open(clickhouse.DefaultOptions())
defer c.Close()
mut args := Vector {} asVector<str>
args.Push(clickhouse.StrArg("2026-11-01"))
mut rows := c.Query(
"SELECT page, sum(hits) AS total FROM visits WHERE day >= ? GROUP BY page ORDER BY total DESC",
args
)
ifnot rows.Ok():
println rows.Error()
ret1enduntil rows.Next():
println rows.Text(0), rows.Int64(1)
end
rows.Close()
mut params := HashMap {} asHashMap<str, str>
params.Put("page", "/docs")
mut one := c.QueryParams("SELECT sum(hits) FROM visits WHERE page = {page:String}", params)
until one.Next():
println"docs:", one.Int64(0)
end
one.Close()
end
An aggregate such as sum(hits) is a 64 bit integer in ClickHouse, which arrives quoted in the JSON and is read back without losing digits by Int64.
A connection to a ClickHouse server over its HTTP interface. Each call is one HTTP request, so there is nothing to keep alive. After a failed call Ok is false and Error and Code say why; the next call clears them.
Name
Type
Description
opts
Options
The options the connection was opened with.
base
str = ""
The base URL, such as http://127.0.0.1:8123.
connected
bool = false
False after Close or when the address was invalid.
status
int = 0
The HTTP status of the last reply; 0 when no reply arrived.
err
str = ""
The last error text.
code
int = 0
The ClickHouse exception code of the last failure, 0 if none.
query_id
str = ""
The query id the server gave the last request.
read_rows
i64 = 0
Rows the last request read, from the server's summary header.
written_rows
i64 = 0
Rows the last request wrote, from the server's summary header.
The last error text; empty when the last call worked. A failure that the server reported partway through a reply is caught too, even though the status was 200.
Runs a statement that returns no rows. Each ? is replaced by the next value of args, built with StrArg, IntArg and the other argument functions. Question marks inside quotes and comments are left alone.
Wraps a connection as a dyn db.Connection, so code written against the shared db interface runs on ClickHouse too. Exec returns 0 or -1, Changes is the rows the last request wrote, LastInsertId is always 0, and a transaction runs each statement at once: ClickHouse has none, so Commit returns 0 and Rollback returns -1.
db-clickhouse-Shared.salam
import db
import db.clickhouse
functotal(c: dyn db.Connection, table: str): int:
ret db.QueryInt(c, "SELECT count() FROM " + table)
endfuncmain:
c := clickhouse.Open(clickhouse.DefaultOptions())
d := clickhouse.AsDb(c)
defer d.Close()
println d.Kind() == db.CLICKHOUSE, d.Ping()
printlntotal(d, "events")
r := d.Query("SELECT id, name FROM events LIMIT 5")
until r.Next():
println r.Int64(0), r.Text(1), r.ColumnType(0)
end
r.Close()
t := d.Begin()
println t.Exec("INSERT INTO events VALUES (99, 'x')"), t.Commit()
end
Makes a connection from an address like clickhouse://user:pass@host:8123/db?secure=true&max_threads=4. clickhouses://, http:// and https:// also work. database, username, password, secure and timeout set options; any other key becomes a ClickHouse setting.