feat(spanner): add lazy decode to partitioned query#1411
Merged
sinhasubham merged 2 commits intomainfrom Oct 9, 2025
Merged
Conversation
ac31c75 to
0dff8f6
Compare
olavloite
reviewed
Sep 23, 2025
| *, | ||
| retry=gapic_v1.method.DEFAULT, | ||
| timeout=gapic_v1.method.DEFAULT, | ||
| lazy_decode=False, |
Contributor
There was a problem hiding this comment.
nit: add documentation for this new argument
tests/unit/test_merged_result_set.py
Outdated
| @@ -0,0 +1,128 @@ | |||
| # Copyright 2024 Google LLC All rights reserved. | |||
Comment on lines
156
to
191
| def decode_row(self, row: []) -> []: | ||
| """Decodes a row from protobuf values to Python objects. This function | ||
| should only be called for result sets that use ``lazy_decoding=True``. | ||
| The array that is returned by this function is the same as the array | ||
| that would have been returned by the rows iterator if ``lazy_decoding=False``. | ||
|
|
||
| :returns: an array containing the decoded values of all the columns in the given row | ||
| """ | ||
| if not isinstance(row, (list, tuple)): | ||
| raise TypeError("row must be an array of protobuf values") | ||
| decoders = self._decoders | ||
| return [ | ||
| _parse_nullable(row[index], decoders[index]) for index in range(len(row)) | ||
| ] | ||
|
|
||
| def decode_column(self, row: [], column_index: int): | ||
| """Decodes a column from a protobuf value to a Python object. This function | ||
| should only be called for result sets that use ``lazy_decoding=True``. | ||
| The object that is returned by this function is the same as the object | ||
| that would have been returned by the rows iterator if ``lazy_decoding=False``. | ||
|
|
||
| :returns: the decoded column value | ||
| """ | ||
| if not isinstance(row, (list, tuple)): | ||
| raise TypeError("row must be an array of protobuf values") | ||
| decoders = self._decoders | ||
| return _parse_nullable(row[column_index], decoders[column_index]) | ||
|
|
||
| @property | ||
| def _decoders(self): | ||
| if self.metadata is None: | ||
| raise ValueError("iterator not started") | ||
| return [ | ||
| _get_type_decoder(field.type_, field.name, None) | ||
| for field in self.metadata.row_type.fields | ||
| ] |
Contributor
There was a problem hiding this comment.
Is this code really needed? A MergedResultSet just delegates all underlying logic to the 'normal' ResultSets. Those already support lazy decoding. So could we not just call the decode_row/decode_column function of one of the underlying ResultSets instead?
| """ | ||
| if not isinstance(row, (list, tuple)): | ||
| raise TypeError("row must be an array of protobuf values") | ||
| decoders = self._decoders |
Contributor
There was a problem hiding this comment.
Can we cache this instead of reconstructing it for every row?
0dff8f6 to
059cfff
Compare
olavloite
approved these changes
Oct 8, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add lazy decode to partitioned query
This commit introduces a
lazy_decodeoption toBatchSnapshot.run_partitioned_query. When set toTrue, the result set yields raw protobuf objects instead of decoded Python objects.This allows the CPU-intensive decoding work to be deferred and managed by the caller, which can significantly improve performance in multi-threaded applications by reducing GIL contention.
To support this,
MergedResultSetnow includesdecode_row()anddecode_column()methods for manual decoding of results.