Skip to content
Permalink
Browse files
perf: use protobuf for metadata to reduce type conversions (#325)
* 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 5110b9bc31804db9777a23fca60360119840640c
Showing with 7 additions and 4 deletions.
  1. +1 −1 google/cloud/spanner_v1/_helpers.py
  2. +5 −2 google/cloud/spanner_v1/streamed.py
  3. +1 −1 tests/unit/test_streamed.py
@@ -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,))
@@ -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

@@ -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):
@@ -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:
@@ -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):

0 comments on commit 5110b9b

Please sign in to comment.