Skip to content

Commit

Permalink
Array(String) fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
xzkostyan committed Oct 15, 2017
1 parent 00ca8d2 commit 70a68bd
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 13 deletions.
20 changes: 8 additions & 12 deletions src/columns/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,12 @@ def write(self, value, buf):
def _write_null(self, buf):
raise NotImplementedError

def write_data(self, items, buf):
def _write_data(self, items, buf):
if self.types_check_enabled:
check_item_type = self.check_item_type
else:
check_item_type = False

if self.nullable:
self._write_nulls_map(items, buf)

for x in items:
self.write_item(x, buf, check_item_type)

Expand All @@ -183,15 +180,14 @@ def write_item(self, x, buf, check_item_type):

self.write(x, buf)

def read_data(self, n_items, buf):
if self.nullable:
nulls_map = self._read_nulls_map(n_items, buf)
else:
nulls_map = [0] * n_items
def _read_data(self, n_items, buf, nulls_map=None):
if nulls_map is not None:
return tuple(
self.read_item(buf, is_null=is_null) for is_null in nulls_map
)

return tuple(
self.read_item(buf, is_null=is_null) for is_null in nulls_map
)
else:
return tuple(self.read(buf) for i in range(n_items))

def read_item(self, buf, is_null=False):
return self._read_null(buf) if is_null else self.read(buf)
86 changes: 86 additions & 0 deletions tests/columns/test_array.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from uuid import UUID

from tests.testcase import BaseTestCase
from src import errors

Expand Down Expand Up @@ -121,3 +123,87 @@ def test_type_mismatch_error(self):
with self.create_table(columns):
with self.assertRaises(errors.TypeMismatchError):
self.client.execute('INSERT INTO test (a) VALUES', data)

def test_string_array(self):
columns = 'a Array(String)'
data = [(self.entuple(['aaa', 'bbb']), )]

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, "['aaa','bbb']\n"
)

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

def test_string_nullable_array(self):
columns = 'a Array(Nullable(String))'
data = [(self.entuple(['aaa', None, 'bbb']), )]

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, "['aaa',NULL,'bbb']\n"
)

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

def test_uuid_array(self):
columns = 'a Array(UUID)'
data = [(self.entuple([
UUID('c0fcbba9-0752-44ed-a5d6-4dfb4342b89d'),
UUID('2efcead4-ff55-4db5-bdb4-6b36a308d8e0')
]), )]

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,
"['c0fcbba9-0752-44ed-a5d6-4dfb4342b89d',"
"'2efcead4-ff55-4db5-bdb4-6b36a308d8e0']\n"
)

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

def test_uuid_nullable_array(self):
columns = 'a Array(Nullable(UUID))'
data = [(self.entuple([
UUID('c0fcbba9-0752-44ed-a5d6-4dfb4342b89d'),
None,
UUID('2efcead4-ff55-4db5-bdb4-6b36a308d8e0')
]), )]

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,
"['c0fcbba9-0752-44ed-a5d6-4dfb4342b89d',"
"NULL,"
"'2efcead4-ff55-4db5-bdb4-6b36a308d8e0']\n"
)

inserted = self.client.execute(query)
self.assertEqual(inserted, data)
2 changes: 1 addition & 1 deletion tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_columnar_block_extend(self):
self.client.execute('INSERT INTO test (a) VALUES', [(1, )])
self.client.execute('INSERT INTO test (a) VALUES', [(2, )])

query = 'SELECT * FROM test'
query = 'SELECT * FROM test ORDER BY a'

inserted = self.client.execute(query, columnar=True)
self.assertEqual(inserted, [(1, 2)])
Expand Down

0 comments on commit 70a68bd

Please sign in to comment.