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(spanner): be permissive about merging an empty struct #10079

Merged
merged 1 commit into from Jan 23, 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
6 changes: 6 additions & 0 deletions spanner/google/cloud/spanner_v1/streamed.py
Expand Up @@ -276,6 +276,12 @@ def _merge_struct(lhs, rhs, type_):
"""Helper for '_merge_by_type'."""
fields = type_.struct_type.fields
lhs, rhs = list(lhs.list_value.values), list(rhs.list_value.values)

# Sanity check: If either list is empty, short-circuit.
# This is effectively a no-op.
if not len(lhs) or not len(rhs):
return Value(list_value=ListValue(values=(lhs + rhs)))

candidate_type = fields[len(lhs) - 1].type
first = rhs.pop(0)
if first.HasField("null_value") or candidate_type.code in _UNMERGEABLE_TYPES:
Expand Down
17 changes: 17 additions & 0 deletions spanner/tests/unit/test_streamed.py
Expand Up @@ -411,6 +411,23 @@ def test__merge_chunk_array_of_struct(self):
self.assertEqual(merged, expected)
self.assertIsNone(streamed._pending_chunk)

def test__merge_chunk_array_of_struct_with_empty(self):
iterator = _MockCancellableIterator()
streamed = self._make_one(iterator)
struct_type = self._make_struct_type([("name", "STRING"), ("age", "INT64")])
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 "])
streamed._pending_chunk = self._make_list_value(value_pbs=[partial])
rest = self._make_list_value([])
chunk = self._make_list_value(value_pbs=[rest])

merged = streamed._merge_chunk(chunk)

expected = self._make_list_value(value_pbs=[partial])
self.assertEqual(merged, expected)
self.assertIsNone(streamed._pending_chunk)

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