Skip to content

Commit

Permalink
Block transpose optimization in get_rows
Browse files Browse the repository at this point in the history
  • Loading branch information
xzkostyan committed Oct 12, 2017
1 parent f45bf47 commit 6c1d6c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
26 changes: 18 additions & 8 deletions src/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,23 @@ def get_rows(self):
if not self.data:
return self.data

n_rows = self.rows
# Transpose results: columns -> rows.
n_columns = self.columns
# Preallocate memory to avoid .append calls.
rv = [None] * n_columns
n_rows = self.rows

flat_data = [None] * n_columns * n_rows

for j in range(n_columns):
column = self.data[j]

for i in range(n_rows):
flat_data[i * n_columns + j] = column[i]

for i in range(n_columns):
rv[i] = tuple([self.data[j][i] for j in range(n_rows)])
# Make rows from slices.
rv = [None] * n_rows
for i in range(n_rows):
offset = i * n_columns
rv[i] = tuple(flat_data[offset:offset + n_columns])

return rv

Expand All @@ -121,9 +131,9 @@ def check_dict_row_type(self, row):
)

@property
def rows(self):
def columns(self):
return len(self.data)

@property
def columns(self):
return len(self.data[0]) if self.rows else 0
def rows(self):
return len(self.data[0]) if self.columns else 0
7 changes: 3 additions & 4 deletions src/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ def store_query_result(self, packet, result, columnar=False):

# Header block contains no rows. Pick columns from it.
if block.rows:
if columnar:
result.data.extend(block.get_columns())
else:
result.data.extend(block.get_rows())
block_data = block.get_columns() if columnar else block.get_rows()
result.data.extend(block_data)

elif not result.columns_with_types:
result.columns_with_types = block.columns_with_types

Expand Down
7 changes: 4 additions & 3 deletions src/streams/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def write(self, block):
if self.server_revision >= defines.DBMS_MIN_REVISION_WITH_BLOCK_INFO:
block.info.write(self.fout)

n_columns = block.columns
n_rows = block.rows
# We write transposed data.
n_columns = block.rows
n_rows = block.columns

write_varint(n_columns, self.fout)
write_varint(n_rows, self.fout)
Expand All @@ -29,7 +30,7 @@ def write(self, block):
write_binary_str(col_name, self.fout)
write_binary_str(col_type, self.fout)

if n_rows:
if n_columns:
try:
items = [row[i] for row in block.data]
except IndexError:
Expand Down

0 comments on commit 6c1d6c1

Please sign in to comment.