Skip to content

Commit

Permalink
Merge pull request #279 from adrian17/bool-type
Browse files Browse the repository at this point in the history
Bool support
  • Loading branch information
xzkostyan committed Jan 6, 2022
2 parents d03bef8 + 4cbf21c commit 0f5cab7
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- "pypy-3.6"
- "pypy-3.7"
clickhouse-version:
- 21.12.3.32
- 21.9.3.30
- 21.9.3.30
- 21.4.6.55
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Features
* Enum8/16
* Array(T)
* Nullable(T)
* Bool
* UUID
* Decimal
* IPv4/IPv6
Expand Down
7 changes: 7 additions & 0 deletions clickhouse_driver/columns/boolcolumn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .base import FormatColumn


class BoolColumn(FormatColumn):
ch_type = 'Bool'
py_types = (bool, )
format = '?'
3 changes: 2 additions & 1 deletion clickhouse_driver/columns/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .. import errors
from .arraycolumn import create_array_column
from .boolcolumn import BoolColumn
from .datecolumn import DateColumn, Date32Column
from .datetimecolumn import create_datetime_column
from .decimalcolumn import create_decimal_column
Expand Down Expand Up @@ -40,7 +41,7 @@
NothingColumn, NullColumn, UUIDColumn,
IntervalYearColumn, IntervalMonthColumn, IntervalWeekColumn,
IntervalDayColumn, IntervalHourColumn, IntervalMinuteColumn,
IntervalSecondColumn, IPv4Column, IPv6Column
IntervalSecondColumn, IPv4Column, IPv6Column, BoolColumn
]}

logger = logging.getLogger(__name__)
Expand Down
8 changes: 8 additions & 0 deletions docs/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ INSERT types: :data:`~types.NoneType`, ``T``.
SELECT type: :data:`~types.NoneType`, ``T``.


Bool
----

INSERT types: :class:`bool`,

SELECT type: :class:`bool`.


UUID
----

Expand Down
71 changes: 71 additions & 0 deletions tests/columns/test_bool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from tests.testcase import BaseTestCase
from clickhouse_driver import errors


class BoolTestCase(BaseTestCase):
required_server_version = (21, 12)

def test_simple(self):
columns = ("a Bool")

data = [(1,), (0,), (True,), (False,), (None,), ("False",), ("",)]
with self.create_table(columns):
self.client.execute('INSERT INTO test (a) VALUES', data)

query = 'SELECT * FROM test'
inserted = self.emit_cli(query)
self.assertEqual(
inserted, (
'true\n'
'false\n'
'true\n'
'false\n'
'false\n'
'true\n'
'false\n'
)
)

inserted = self.client.execute(query)
self.assertEqual(
inserted, [
(True, ),
(False, ),
(True, ),
(False, ),
(False, ),
(True, ),
(False, ),
]
)

def test_errors(self):
columns = "a Bool"
with self.create_table(columns):
with self.assertRaises(errors.TypeMismatchError):
self.client.execute(
'INSERT INTO test (a) VALUES', [(1, )],
types_check=True
)

def test_nullable(self):
columns = "a Nullable(Bool)"

data = [(None, ), (True, ), (False, )]
with self.create_table(columns):
self.client.execute('INSERT INTO test (a) VALUES', data)

query = 'SELECT * FROM test'
inserted = self.emit_cli(query)
self.assertEqual(
inserted, (
'\\N\ntrue\nfalse\n'
)
)

inserted = self.client.execute(query)
self.assertEqual(
inserted, [
(None, ), (True, ), (False, ),
]
)

0 comments on commit 0f5cab7

Please sign in to comment.