skip to navigation
skip to content

clickhouse-driver 0.0.7

Python driver with native interface for ClickHouse

Latest Version: 0.0.8

ClickHouse Python Driver

ClickHouse Python Driver with native (TCP) interface support.

Asynchronous wrapper is available here:


  • Compression support:
  • Basic types support:
    • Float32/64
    • [U]Int8/16/32/64
    • Date/DateTime
    • String/FixedString(N)
    • Enum8/16
    • Array(T)
    • Nullable(T)
    • UUID
  • External data for query processing.
  • Query settings.
  • Query progress information.


The package can be installed using pip:

pip install clickhouse-driver

You can install extras packages if you need compression support. Example of LZ4 compression requirements installation:

pip install clickhouse-driver[lz4]

You also can specify multiple extras by using comma. Install LZ4 and ZSTD requirements:

pip install clickhouse-driver[lz4,zstd]

Usage example:

from clickhouse_driver import Client

client = Client('localhost')

print(client.execute('SHOW TABLES'))

client.execute('DROP TABLE IF EXISTS test')

client.execute('CREATE TABLE test (x Int32) ENGINE = Memory')

    'INSERT INTO test (x) VALUES',
    [{'x': 1}, {'x': 2}, {'x': 3}, {'x': 100}]
client.execute('INSERT INTO test (x) VALUES', [[200]])

print(client.execute('SELECT sum(x) FROM test'))


client.execute('CREATE TABLE test2 (x Array(Int32)) ENGINE = Memory')
    'INSERT INTO test2 (x) VALUES',
    [{'x': [10, 20, 30]}, {'x': (11, 21, 31)}]

print(client.execute('SELECT * FROM test2'))


from enum import Enum

class MyEnum(Enum):
    foo = 1
    bar = 2

    CREATE TABLE test3
        x Enum8('foo' = 1, 'bar' = 2)
    ) ENGINE = Memory
    'INSERT INTO test3 (x) VALUES',
    [{'x':}, {'x': 'bar'}, {'x': 1}]

print(client.execute('SELECT * FROM test3'))

Data compression:

from clickhouse_driver import Client

client_with_lz4 = Client('localhost', compression=True)
client_with_lz4 = Client('localhost', compression='lz4')
client_with_zstd = Client('localhost', compression='zstd')

External data for query processing:

tables = [{
    'name': 'ext',
    'structure': [('x', 'Int32'), ('y', 'Array(Int32)')],
    'data': [
        {'x': 100, 'y': [2, 4, 6, 8]},
        {'x': 500, 'y': [1, 3, 5, 7]},
rv = client.execute(
    'SELECT sum(x) FROM ext', external_tables=tables)

Query progress information:

from datetime import datetime

progress = client.execute_with_progress('LONG AND COMPLICATED QUERY')

timeout = 20
started_at =

for num_rows, total_rows in progress:
    done = float(num_rows) / total_rows if total_rows else total_rows
    now =
    # Cancel query if it takes more than 20 seconds to process 50% of rows.
    if (now - started_at).total_seconds() > timeout and done < 0.5:
    rv = progress.get_result()

CityHash algorithm notes

Unfortunately ClickHouse server comes with built-in old version of CityHash hashing algorithm. That’s why we can’t use original CityHash package. Downgraded version of this algorithm is placed at PyPI.

Client Parameters

The first parameter host is required. There are some optional parameters:

  • port is port ClickHouse server is bound to. Default is 9000.
  • database is database connect to. Default is 'default'.
  • user. Default is 'default'.
  • password. Default is '' (no password).
  • client_name. This name will appear in server logs. Default is 'python-driver'.
  • compression. Whether or not use compression. Default is False. Possible choices:
    • True is equivalent to 'lz4'.
    • 'lz4'.
    • 'lz4hc' high-compression variant of 'lz4'.
    • 'zstd'.
  • insert_block_size. Chunk size to split rows for INSERT. Default is 1048576.

You can also specify timeouts via:

  • connect_timeout. Default is 10 seconds.
  • send_receive_timeout. Default is 300 seconds.
  • sync_request_timeout. Default is 5 seconds.


Specifying query_id:

from uuid import uuid1

query_id = str(uuid1())
print(client.execute('SHOW TABLES', query_id=query_id))

Overriding default query settings:

# Set lower priority to query and limit max number threads to execute the request.
settings = {'max_threads': 2, 'priority': 10}
print(client.execute('SHOW TABLES', settings=settings))

Retrieving results in columnar form. This is also more faster:

print(client.execute('SELECT arrayJoin(range(3))', columnar=True)

Data types check is disabled for performance on INSERT queries. You can turn it on by types_check option:

client.execute('INSERT INTO test (x) VALUES', [('abc', )], types_check=True)


ClickHouse Python Driver is distributed under the MIT license.

File Type Py Version Uploaded on Size
clickhouse-driver-0.0.7.tar.gz (md5) Source 2017-10-12 30KB