Skip to content

Commit

Permalink
Fix null value on bytestring columns
Browse files Browse the repository at this point in the history
  • Loading branch information
vivienm committed Jan 2, 2019
1 parent 293b596 commit c6b6a2a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clickhouse_driver/columns/stringcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
class String(Column):
ch_type = 'String'
py_types = compat.string_types
null_value = ''

# TODO: pass user encoding here

def prepare_null(self, value):
if self.nullable and value is None:
return '', True
return self.null_value, True

else:
return value, False
Expand All @@ -34,6 +35,7 @@ def read_items(self, n_items, buf):

class ByteString(String):
py_types = (bytearray, bytes)
null_value = b''

def write_items(self, items, buf):
for value in items:
Expand Down Expand Up @@ -93,6 +95,7 @@ def write_items(self, items, buf):

class ByteFixedString(FixedString):
py_types = (bytearray, bytes)
null_value = b''

def read_items(self, n_items, buf):
length = self.length
Expand Down
22 changes: 22 additions & 0 deletions tests/columns/test_fixedstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,25 @@ def test_not_decoded(self):
)
self.assertIsInstance(inserted[0][0], bytes)
self.assertIsInstance(inserted[1][0], bytes)

def test_nullable(self):
with self.create_table('a Nullable(FixedString(10))'):
data = [
(None, ),
(b'test\x00\x00\x00\x00\x00\x00', ),
(None, ),
(b'nullable\x00\x00', )
]
self.client.execute(
'INSERT INTO test (a) VALUES', data
)

query = 'SELECT * FROM test'
inserted = self.emit_cli(query)
self.assertEqual(
inserted,
'\\N\ntest\\0\\0\\0\\0\\0\\0\n\\N\nnullable\\0\\0\n'
)

inserted = self.client.execute(query)
self.assertEqual(inserted, data)
14 changes: 14 additions & 0 deletions tests/columns/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,17 @@ def test_not_decoded_bytearray_expected(self):
)

self.assertIn('Column a', str(e.exception))

def test_nullable(self):
with self.create_table('a Nullable(String)'):
data = [(None, ), (b'test', ), (None, ), (b'nullable', )]
self.client.execute(
'INSERT INTO test (a) VALUES', data
)

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

inserted = self.client.execute(query)
self.assertEqual(inserted, data)

0 comments on commit c6b6a2a

Please sign in to comment.