Skip to content

Commit

Permalink
perf: use protobuf for metadata to reduce type conversions (#325)
Browse files Browse the repository at this point in the history
* perf: use protobuf for metadata to reduce type conversions

* fix: ensure the metadata return type remains the same

* fix: only wrap non-None metadata pbs

Co-authored-by: larkee <larkee@users.noreply.github.com>
  • Loading branch information
larkee and larkee committed Apr 29, 2021
1 parent eccbd0d commit 5110b9b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion google/cloud/spanner_v1/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def _parse_value_pb(value_pb, field_type):
_parse_value_pb(item_pb, field_type.struct_type.fields[i].type_)
for (i, item_pb) in enumerate(value_pb.list_value.values)
]
elif field_type.code == TypeCode.NUMERIC:
elif type_code == TypeCode.NUMERIC:
return decimal.Decimal(value_pb.string_value)
else:
raise ValueError("Unknown type: %s" % (field_type,))
Expand Down
7 changes: 5 additions & 2 deletions google/cloud/spanner_v1/streamed.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from google.protobuf.struct_pb2 import Value
from google.cloud import exceptions
from google.cloud.spanner_v1 import PartialResultSet
from google.cloud.spanner_v1 import ResultSetMetadata
from google.cloud.spanner_v1 import TypeCode
import six

Expand Down Expand Up @@ -65,7 +66,9 @@ def metadata(self):
:rtype: :class:`~google.cloud.spanner_v1.types.ResultSetMetadata`
:returns: structure describing the results
"""
return self._metadata
if self._metadata:
return ResultSetMetadata.wrap(self._metadata)
return None

@property
def stats(self):
Expand Down Expand Up @@ -119,7 +122,7 @@ def _consume_next(self):
response_pb = PartialResultSet.pb(response)

if self._metadata is None: # first response
metadata = self._metadata = response.metadata
metadata = self._metadata = response_pb.metadata

source = self._source
if source is not None and source._transaction_id is None:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_streamed.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_properties_set(self):
metadata = streamed._metadata = self._make_result_set_metadata(FIELDS)
stats = streamed._stats = self._make_result_set_stats()
self.assertEqual(list(streamed.fields), FIELDS)
self.assertIs(streamed.metadata, metadata)
self.assertIs(streamed.metadata._pb, metadata)
self.assertIs(streamed.stats, stats)

def test__merge_chunk_bool(self):
Expand Down

0 comments on commit 5110b9b

Please sign in to comment.