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: handle Unmergable errors when merging struct responses #152

Merged
merged 1 commit into from Oct 13, 2020
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
8 changes: 7 additions & 1 deletion google/cloud/spanner_v1/streamed.py
Expand Up @@ -286,7 +286,13 @@ def _merge_struct(lhs, rhs, type_):
lhs.append(first)
else:
last = lhs.pop()
lhs.append(_merge_by_type(last, first, candidate_type))
try:
merged = _merge_by_type(last, first, candidate_type)
except Unmergeable:
lhs.append(last)
lhs.append(first)
else:
lhs.append(merged)
return Value(list_value=ListValue(values=lhs + rhs))


Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_streamed.py
Expand Up @@ -448,6 +448,26 @@ def test__merge_chunk_array_of_struct_unmergeable(self):
self.assertEqual(merged, expected)
self.assertIsNone(streamed._pending_chunk)

def test__merge_chunk_array_of_struct_unmergeable_split(self):
iterator = _MockCancellableIterator()
streamed = self._make_one(iterator)
struct_type = self._make_struct_type(
[("name", "STRING"), ("height", "FLOAT64"), ("eye_color", "STRING")]
)
FIELDS = [self._make_array_field("test", element_type=struct_type)]
streamed._metadata = self._make_result_set_metadata(FIELDS)
partial = self._make_list_value([u"Phred Phlyntstone", 1.65])
streamed._pending_chunk = self._make_list_value(value_pbs=[partial])
rest = self._make_list_value(["brown"])
chunk = self._make_list_value(value_pbs=[rest])

merged = streamed._merge_chunk(chunk)

struct = self._make_list_value([u"Phred Phlyntstone", 1.65, "brown"])
expected = self._make_list_value(value_pbs=[struct])
self.assertEqual(merged, expected)
self.assertIsNone(streamed._pending_chunk)

def test_merge_values_empty_and_empty(self):
iterator = _MockCancellableIterator()
streamed = self._make_one(iterator)
Expand Down