1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
Supported types
===============
Each ClickHouse type is deserialized to a corresponding Python type when SELECT queries are prepared.
When serializing INSERT queries, clickhouse-driver accepts a broader range of Python types.
The following ClickHouse types are supported by clickhouse-driver:
[U]Int8/16/32/64/128/256
------------------------
INSERT types: :class:`int`, :class:`long`.
SELECT type: :class:`int`.
Float32/64
----------
INSERT types: :class:`float`, :class:`int`, :class:`long`.
SELECT type: :class:`float`.
Date/Date32
-----------
*Date32 support is new in version 0.2.2.*
INSERT types: :class:`~datetime.date`, :class:`~datetime.datetime`.
SELECT type: :class:`~datetime.date`.
DateTime('timezone')/DateTime64('timezone')
-------------------------------------------
*Timezone support is new in version 0.0.11.*
*DateTime64 support is new in version 0.1.3.*
INSERT types: :class:`~datetime.datetime`, :class:`int`, :class:`long`.
Integers are interpreted as seconds without timezone (UNIX timestamps). Integers can be used when
insertion of datetime column is a bottleneck.
SELECT type: :class:`~datetime.datetime`.
Setting `use_client_time_zone <https://clickhouse.com/docs/en/sql-reference/data-types/datetime/#usage-remarks>`_ is taken into consideration.
You can cast DateTime column to integers if you are facing performance issues when selecting large amount of rows.
Due to Python's current limitations minimal DateTime64 resolution is one microsecond.
String/FixedString(N)
---------------------
INSERT types: :class:`str`/:func:`basestring <basestring>`, :class:`bytes`. See note below.
SELECT type: :class:`str`/:func:`basestring <basestring>`, :class:`bytes`. See note below.
String column is encoded/decoded with encoding specified by ``strings_encoding`` setting. Default encoding is UTF-8.
You can specify custom encoding:
.. code-block:: python
>>> settings = {'strings_encoding': 'cp1251'}
>>> rows = client.execute(
... 'SELECT * FROM table_with_strings',
... settings=settings
... )
Encoding is applied to all string fields in query.
String columns can be returned without any decoding. In this case return values are `bytes`:
.. code-block:: python
>>> settings = {'strings_as_bytes': True}
>>> rows = client.execute(
... 'SELECT * FROM table_with_strings',
... settings=settings
... )
If a column has FixedString type, upon returning from SELECT it may contain trailing zeroes
in accordance with ClickHouse's storage format. Trailing zeroes are stripped by driver for convenience.
During SELECT, if a string cannot be decoded with specified encoding, it will return as :class:`bytes`.
During INSERT, if ``strings_as_bytes`` setting is not specified and string cannot be encoded with encoding,
a ``UnicodeEncodeError`` will be raised.
Enum8/16
--------
INSERT types: :class:`~enum.Enum`, :class:`int`, :class:`long`, :class:`str`/:func:`basestring <basestring>`.
SELECT type: :class:`str`/:func:`basestring <basestring>`.
.. code-block:: python
>>> from enum import IntEnum
>>>
>>> class MyEnum(IntEnum):
... foo = 1
... bar = 2
...
>>> client.execute('DROP TABLE IF EXISTS test')
[]
>>> client.execute('''
... CREATE TABLE test
... (
... x Enum8('foo' = 1, 'bar' = 2)
... ) ENGINE = Memory
... ''')
[]
>>> client.execute(
... 'INSERT INTO test (x) VALUES',
... [{'x': MyEnum.foo}, {'x': 'bar'}, {'x': 1}]
... )
3
>>> client.execute('SELECT * FROM test')
[('foo',), ('bar',), ('foo',)]
Currently clickhouse-driver can't handle empty enum value due to Python's `Enum` mechanics.
Enum member name must be not empty. See `issue`_ and `workaround`_.
.. _issue: https://github.com/mymarilyn/clickhouse-driver/issues/48
.. _workaround: https://github.com/mymarilyn/clickhouse-driver/issues/48#issuecomment-412480613
Array(T)
--------
INSERT types: :class:`list`, :class:`tuple`.
SELECT type: :class:`list`.
*Versions before 0.1.4:* SELECT type: :class:`tuple`.
.. code-block:: python
>>> client.execute('DROP TABLE IF EXISTS test')
[]
>>> client.execute(
... 'CREATE TABLE test (x Array(Int32)) '
... 'ENGINE = Memory'
... )
[]
>>> client.execute(
... 'INSERT INTO test (x) VALUES',
... [{'x': [10, 20, 30]}, {'x': [11, 21, 31]}]
... )
2
>>> client.execute('SELECT * FROM test')
[((10, 20, 30),), ((11, 21, 31),)]
Nullable(T)
-----------
INSERT types: :data:`~types.NoneType`, ``T``.
SELECT type: :data:`~types.NoneType`, ``T``.
Bool
----
INSERT types: :class:`bool`,
SELECT type: :class:`bool`.
UUID
----
INSERT types: :class:`str`/:func:`basestring <basestring>`, :class:`~uuid.UUID`.
SELECT type: :class:`~uuid.UUID`.
Decimal
-------
*New in version 0.0.16.*
INSERT types: :class:`~decimal.Decimal`, :class:`float`, :class:`int`, :class:`long`.
SELECT type: :class:`~decimal.Decimal`.
Supported subtypes:
* Decimal(P, S).
* Decimal32(S).
* Decimal64(S).
* Decimal128(S).
* Decimal256(S). *New in version 0.2.1.*
IPv4/IPv6
---------
*New in version 0.0.19.*
INSERT types: :class:`~ipaddress.IPv4Address`/:class:`~ipaddress.IPv6Address`, :class:`int`, :class:`long`, :class:`str`/:func:`basestring <basestring>`.
SELECT type: :class:`~ipaddress.IPv4Address`/:class:`~ipaddress.IPv6Address`.
.. code-block:: python
>>> from ipaddress import IPv4Address, IPv6Address
>>>
>>> client.execute('DROP TABLE IF EXISTS test')
[]
>>> client.execute(
... 'CREATE TABLE test (x IPv4) '
... 'ENGINE = Memory'
... )
[]
>>> client.execute(
... 'INSERT INTO test (x) VALUES', [
... {'x': '192.168.253.42'},
... {'x': 167772161},
... {'x': IPv4Address('192.168.253.42')}
... ])
3
>>> client.execute('SELECT * FROM test')
[(IPv4Address('192.168.253.42'),), (IPv4Address('10.0.0.1'),), (IPv4Address('192.168.253.42'),)]
>>>
>>> client.execute('DROP TABLE IF EXISTS test')
[]
>>> client.execute(
... 'CREATE TABLE test (x IPv6) '
... 'ENGINE = Memory'
... )
[]
>>> client.execute(
... 'INSERT INTO test (x) VALUES', [
... {'x': '79f4:e698:45de:a59b:2765:28e3:8d3a:35ae'},
... {'x': IPv6Address('12ff:0000:0000:0000:0000:0000:0000:0001')},
... {'x': b"y\xf4\xe6\x98E\xde\xa5\x9b'e(\xe3\x8d:5\xae"}
... ])
3
>>> client.execute('SELECT * FROM test')
[(IPv6Address('79f4:e698:45de:a59b:2765:28e3:8d3a:35ae'),), (IPv6Address('12ff::1'),), (IPv6Address('79f4:e698:45de:a59b:2765:28e3:8d3a:35ae'),)]
>>>
LowCardinality(T)
-----------------
*New in version 0.0.20.*
INSERT types: ``T``.
SELECT type: ``T``.
SimpleAggregateFunction(F, T)
-----------------------------
*New in version 0.0.21.*
INSERT types: ``T``.
SELECT type: ``T``.
AggregateFunctions for `AggregatingMergeTree` Engine are not supported.
Tuple(T1, T2, ...)
------------------
*New in version 0.1.4.*
INSERT types: :class:`list`, :class:`tuple`.
SELECT type: :class:`tuple`.
Nested(flatten_nested=1, default)
---------------------------------
Nested type is represented by sequence of arrays when flatten_nested=1. In example below actual
columns for are ``col.name`` and ``col.version``.
.. code-block:: sql
:) CREATE TABLE test_nested (col Nested(name String, version UInt32)) Engine = Memory;
CREATE TABLE test_nested
(
`col` Nested(name String, version UInt32)
)
ENGINE = Memory
Ok.
0 rows in set. Elapsed: 0.005 sec.
:) DESCRIBE TABLE test_nested FORMAT TSV;
DESCRIBE TABLE test_nested
FORMAT TSV
col.name Array(String)
col.version Array(UInt32)
2 rows in set. Elapsed: 0.004 sec.
Inserting data into nested column in ``clickhouse-client``:
.. code-block:: sql
:) INSERT INTO test_nested VALUES (['a', 'b', 'c'], [100, 200, 300]);
INSERT INTO test_nested VALUES
Ok.
1 rows in set. Elapsed: 0.003 sec.
Inserting data into nested column with ``clickhouse-driver``:
.. code-block:: python
client.execute('INSERT INTO test_nested VALUES', [
(['a', 'b', 'c'], [100, 200, 300]),
])
Nested(flatten_nested=0)
------------------------
Nested type is represented by array of named tuples when flatten_nested=0.
.. code-block:: sql
:) SET flatten_nested = 0;
SET flatten_nested = 0
Ok.
0 rows in set. Elapsed: 0.006 sec.
:) CREATE TABLE test_nested (col Nested(name String, version UInt32)) Engine = Memory;
CREATE TABLE test_nested
(
`col` Nested(name String, version UInt32)
)
ENGINE = Memory
Ok.
0 rows in set. Elapsed: 0.005 sec.
:) DESCRIBE TABLE test_nested FORMAT TSV;
DESCRIBE TABLE test_nested
FORMAT TSV
col Nested(name String, version UInt32)
1 rows in set. Elapsed: 0.004 sec.
Inserting data into nested column in ``clickhouse-client``:
.. code-block:: sql
:) INSERT INTO test_nested VALUES ([('a', 100), ('b', 200), ('c', 300)]);
INSERT INTO test_nested VALUES
Ok.
1 rows in set. Elapsed: 0.003 sec.
Inserting data into nested column with ``clickhouse-driver``:
.. code-block:: python
client.execute(
'INSERT INTO test_nested VALUES', [
([('a', 100), ('b', 200), ('c', 300)], )
])
# or
client.execute(
'INSERT INTO test_nested VALUES', [
{'col': [
{'name': 'a', 'version': 100},
{'name': 'b', 'version': 200},
{'name': 'c', 'version': 300}
]}
])
Map(key, value)
------------------
*New in version 0.2.1.*
INSERT types: :class:`dict`.
SELECT type: :class:`dict`.
Geo
---
*New in version 0.2.4.*
Point, Ring, Polygon, MultiPolygon.
These types are just aliases:
* Point: Tuple(Float64, Float64)
* Ring: Array(Point)
* Polygon: Array(Ring)
* MultiPolygon: Array(Polygon)
|