Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null value on bytestring columns #65

Merged
merged 1 commit into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)