Skip to content

Commit

Permalink
fix: Only override metadata for PartialResultSet when it is present i…
Browse files Browse the repository at this point in the history
…n the response (#7334)

Only override metadata for PartialResultSet when it is present in response
  • Loading branch information
saranshdhingra committed May 24, 2024
1 parent cece74c commit c2078a6
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions Spanner/src/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,21 @@ public function __construct(array $config = [])
PartialResultSet::class => function ($msg) {
$data = json_decode($msg->serializeToJsonString(), true);

// The transaction id is serialized as a base64 encoded string in $data. So, we
// add a step to get the transaction id using a getter instead of the serialized value.
// The null-safe operator is used to handle edge cases where the relevant fields are not present.
$data['metadata']['transaction']['id'] = (string) $msg?->getMetadata()?->getTransaction()?->getId();

// Helps convert metadata enum values from string types to their respective code/annotation
// pairs. Ex: INT64 is converted to {code: 2, typeAnnotation: 0}.
$fields = $msg->getMetadata()?->getRowType()?->getFields();
$data['metadata']['rowType']['fields'] = $this->getFieldDataFromRepeatedFields($fields);
// We only override metadata fields, if it actually exists in the response.
// This is specially important for large data sets which is received in chunks.
// Metadata is only received in the first 'chunk' and we don't want to set empty metadata fields
// when metadata was not returned from the server.
if (isset($data['metadata'])) {
// The transaction id is serialized as a base64 encoded string in $data. So, we
// add a step to get the transaction id using a getter instead of the serialized value.
// The null-safe operator is used to handle edge cases where the relevant fields are not present.
$data['metadata']['transaction']['id'] = (string) $msg?->getMetadata()?->getTransaction()?->getId();

// Helps convert metadata enum values from string types to their respective code/annotation
// pairs. Ex: INT64 is converted to {code: 2, typeAnnotation: 0}.
$fields = $msg->getMetadata()?->getRowType()?->getFields();
$data['metadata']['rowType']['fields'] = $this->getFieldDataFromRepeatedFields($fields);
}

// These fields in stats should be an int
if (isset($data['stats']['rowCountLowerBound'])) {
Expand Down

0 comments on commit c2078a6

Please sign in to comment.