Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
StreamedManyResultSets,
)
from google.cloud.spanner_v1 import RequestOptions
from google.cloud.spanner_v1._helpers import _get_read_timestamp
from google.cloud.spanner_v1.merged_result_set import MergedResultSet

ColumnDetails = namedtuple("column_details", ["null_ok", "spanner_type"])
Expand Down Expand Up @@ -603,10 +604,9 @@ def _handle_DQL_with_snapshot(self, snapshot, sql, params):
# Unfortunately, Spanner doesn't seem to send back
# information about the number of rows available.
self._row_count = None
if self._result_set.metadata.transaction.read_timestamp is not None:
snapshot._transaction_read_timestamp = (
self._result_set.metadata.transaction.read_timestamp
)
read_timestamp = _get_read_timestamp(self._result_set.metadata.transaction)
if read_timestamp is not None:
snapshot._transaction_read_timestamp = read_timestamp

def _handle_DQL(self, sql, params):
if self.connection.database is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from google.cloud.spanner_v1._helpers import (
_augment_error_with_request_id,
_check_rst_stream_error,
_get_read_timestamp,
_make_execute_sql_request,
_make_value_pb,
_merge_client_context,
Expand Down Expand Up @@ -1047,8 +1048,12 @@ def _update_for_result_set_pb(
self, result_set_pb: Union[ResultSet, PartialResultSet]
) -> None:
"""Updates the snapshot for the given result set."""
if result_set_pb.metadata and result_set_pb.metadata.transaction:
self._update_for_transaction_pb(result_set_pb.metadata.transaction)
# Check for presence instead of truthiness: the truth value of a
# proto-plus message converts every field, including the read
# timestamp (see ``_get_read_timestamp``).
metadata = result_set_pb.metadata
if metadata is not None and metadata._pb.HasField("transaction"):
self._update_for_transaction_pb(metadata.transaction)

def _update_for_transaction_pb(self, transaction_pb: Transaction) -> None:
"""Updates the snapshot for the given transaction."""
Expand Down Expand Up @@ -1148,5 +1153,6 @@ def _build_transaction_options_pb(self) -> TransactionOptions:
def _update_for_transaction_pb(self, transaction_pb: Transaction) -> None:
"""Updates the snapshot for the given transaction."""
super(Snapshot, self)._update_for_transaction_pb(transaction_pb)
if transaction_pb.read_timestamp is not None:
self._transaction_read_timestamp = transaction_pb.read_timestamp
read_timestamp = _get_read_timestamp(transaction_pb)
if read_timestamp is not None:
self._transaction_read_timestamp = read_timestamp
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,28 @@ def _parse_timestamp(value_pb):
raise ValueError("Timestamp: {} does not match pattern".format(val)) from e


def _get_read_timestamp(transaction_pb):
"""Return the read timestamp of a ``Transaction``, or ``None``.

``None`` is also returned when the timestamp is outside the range of
:class:`datetime.datetime`, which Spanner can return for change stream
queries. proto-plus raises ``OverflowError`` when converting it.

:type transaction_pb: :class:`~google.cloud.spanner_v1.types.Transaction`
:param transaction_pb: the transaction returned by Spanner.

:rtype: :class:`~google.api_core.datetime_helpers.DatetimeWithNanoseconds`
or None
:returns: the read timestamp, or ``None``.
"""
if not transaction_pb._pb.HasField("read_timestamp"):
return None
try:
return transaction_pb.read_timestamp
except OverflowError:
return None
Comment on lines +834 to +839

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To make _get_read_timestamp more robust, we should handle cases where transaction_pb is None to avoid an AttributeError. Additionally, in performance-critical hot paths, we should avoid the overhead of protobuf methods like HasField by checking field truthiness directly. Catching both OverflowError and ValueError ensures that any platform-specific or version-specific conversion failures for out-of-range timestamps are safely handled.

Suggested change
if not transaction_pb._pb.HasField("read_timestamp"):
return None
try:
return transaction_pb.read_timestamp
except OverflowError:
return None
if transaction_pb is None:
return None
if not transaction_pb.read_timestamp:
return None
try:
return transaction_pb.read_timestamp
except (OverflowError, ValueError):
return None
References
  1. In performance-critical hot paths, avoid the overhead of protobuf methods like WhichOneof or HasField by checking field truthiness directly.



def _parse_proto(value_pb, column_info, field_name):
bytes_value = base64.b64decode(value_pb.string_value)
if column_info is not None and column_info.get(field_name) is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
_augment_error_with_request_id,
_check_rst_stream_error,
_drain_stream,
_get_read_timestamp,
_make_execute_sql_request,
_make_value_pb,
_merge_client_context,
Expand Down Expand Up @@ -943,8 +944,9 @@ def _update_for_result_set_pb(
self, result_set_pb: Union[ResultSet, PartialResultSet]
) -> None:
"""Updates the snapshot for the given result set."""
if result_set_pb.metadata and result_set_pb.metadata.transaction:
self._update_for_transaction_pb(result_set_pb.metadata.transaction)
metadata = result_set_pb.metadata
if metadata is not None and metadata._pb.HasField("transaction"):
self._update_for_transaction_pb(metadata.transaction)

def _update_for_transaction_pb(self, transaction_pb: Transaction) -> None:
"""Updates the snapshot for the given transaction."""
Expand Down Expand Up @@ -1029,5 +1031,6 @@ def _build_transaction_options_pb(self) -> TransactionOptions:
def _update_for_transaction_pb(self, transaction_pb: Transaction) -> None:
"""Updates the snapshot for the given transaction."""
super(Snapshot, self)._update_for_transaction_pb(transaction_pb)
if transaction_pb.read_timestamp is not None:
self._transaction_read_timestamp = transaction_pb.read_timestamp
read_timestamp = _get_read_timestamp(transaction_pb)
if read_timestamp is not None:
self._transaction_read_timestamp = read_timestamp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,31 @@ def test__update_for_transaction_pb_multi_use(self):
self.assertEqual(snapshot._transaction_read_timestamp, TIMESTAMP)
self.assertTrue(snapshot._transaction_begin_event.is_set())

def test__update_for_result_set_pb_w_read_timestamp(self):
snapshot = self._make_snapshot(_Session(), multi_use=False)
result_set_pb = PartialResultSet(
metadata={"transaction": {"read_timestamp": TIMESTAMP}}
)
snapshot._update_for_result_set_pb(result_set_pb)
self.assertIsNone(snapshot._transaction_id)
self.assertEqual(snapshot._transaction_read_timestamp, TIMESTAMP)

def test__update_for_result_set_pb_w_read_timestamp_out_of_range(self):
for multi_use in (False, True):
with self.subTest(multi_use=multi_use):
snapshot = self._make_snapshot(_Session(), multi_use=multi_use)
result_set_pb = PartialResultSet()
transaction_pb = result_set_pb._pb.metadata.transaction
if multi_use:
transaction_pb.id = TXN_ID
# 10000-01-01T00:00:00Z does not fit in a datetime.
transaction_pb.read_timestamp.seconds = 253402300800
snapshot._update_for_result_set_pb(result_set_pb)
self.assertEqual(
snapshot._transaction_id, TXN_ID if multi_use else None
)
self.assertIsNone(snapshot._transaction_read_timestamp)

async def test_restart_on_unavailable_precommit(self):
from google.cloud.spanner_v1._async.snapshot import _restart_on_unavailable
from tests._builders import build_precommit_token_pb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,24 @@ def test_handle_dql(self, MockedPeekIterator):
self.assertEqual(cursor._itr, MockedPeekIterator())
self.assertEqual(cursor._row_count, None)

@mock.patch("google.cloud.spanner_dbapi.cursor.PeekIterator")
def test_handle_dql_w_read_timestamp_out_of_range(self, MockedPeekIterator):
from google.cloud.spanner_v1 import Transaction

connection = self._make_connection(self.INSTANCE, mock.MagicMock())
mock_snapshot = mock.MagicMock(_transaction_read_timestamp=None)
cursor = self._make_one(connection)

transaction_pb = Transaction(id=b"txn-id")
# 10000-01-01T00:00:00Z does not fit in a datetime.
transaction_pb._pb.read_timestamp.seconds = 253402300800
_result_set = mock.Mock()
_result_set.metadata.transaction = transaction_pb
mock_snapshot.execute_sql.return_value = _result_set
cursor._handle_DQL_with_snapshot(mock_snapshot, "sql", params=None)
self.assertEqual(cursor._result_set, _result_set)
self.assertIsNone(mock_snapshot._transaction_read_timestamp)

@mock.patch("google.cloud.spanner_dbapi.cursor.PeekIterator")
def test_handle_dql_priority(self, MockedPeekIterator):
from google.cloud.spanner_v1 import RequestOptions
Expand Down
30 changes: 30 additions & 0 deletions packages/google-cloud-spanner/tests/unit/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,36 @@ def test_none_is_returned_unchanged(self):
self.assertIsNone(self._callFUT(None, RequestOptions))


class Test_get_read_timestamp(unittest.TestCase):
def _callFUT(self, *args, **kw):
from google.cloud.spanner_v1._helpers import _get_read_timestamp

return _get_read_timestamp(*args, **kw)

def test_not_set(self):
from google.cloud.spanner_v1 import Transaction

self.assertIsNone(self._callFUT(Transaction(id=b"txn-id")))

def test_in_range(self):
from google.cloud.spanner_v1 import Transaction

timestamp = datetime.datetime(2026, 11, 18, 21, 23, 35, tzinfo=timezone.utc)
transaction_pb = Transaction(read_timestamp=timestamp)
self.assertEqual(self._callFUT(transaction_pb), timestamp)

def test_out_of_range(self):
from google.cloud.spanner_v1 import Transaction

# 10000-01-01T00:00:00Z and the second before 0001-01-01T00:00:00Z
# do not fit in a datetime.
for seconds in (253402300800, -62135596801):
with self.subTest(seconds=seconds):
transaction_pb = Transaction()
transaction_pb._pb.read_timestamp.seconds = seconds
self.assertIsNone(self._callFUT(transaction_pb))


class Test_make_execute_sql_request(unittest.TestCase):
SESSION_NAME = "projects/p/instances/i/databases/d/sessions/s"
SQL = "SELECT * FROM Singers WHERE SingerId = @singer_id"
Expand Down
32 changes: 32 additions & 0 deletions packages/google-cloud-spanner/tests/unit/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3027,6 +3027,38 @@ def test__update_for_transaction_pb_multi_use(self):
self.assertEqual(snapshot._transaction_read_timestamp, timestamp)
self.assertTrue(snapshot._transaction_begin_event.is_set())

def test__update_for_result_set_pb_w_read_timestamp(self):
from datetime import timezone

from google.cloud.spanner_v1.types.result_set import PartialResultSet

timestamp = datetime.now(timezone.utc)
snapshot = self._make_one(_Session(), multi_use=False)
result_set_pb = PartialResultSet(
metadata={"transaction": {"read_timestamp": timestamp}}
)
snapshot._update_for_result_set_pb(result_set_pb)
self.assertIsNone(snapshot._transaction_id)
self.assertEqual(snapshot._transaction_read_timestamp, timestamp)

def test__update_for_result_set_pb_w_read_timestamp_out_of_range(self):
from google.cloud.spanner_v1.types.result_set import PartialResultSet

for multi_use in (False, True):
with self.subTest(multi_use=multi_use):
snapshot = self._make_one(_Session(), multi_use=multi_use)
result_set_pb = PartialResultSet()
transaction_pb = result_set_pb._pb.metadata.transaction
if multi_use:
transaction_pb.id = TXN_ID
# 10000-01-01T00:00:00Z does not fit in a datetime.
transaction_pb.read_timestamp.seconds = 253402300800
snapshot._update_for_result_set_pb(result_set_pb)
self.assertEqual(
snapshot._transaction_id, TXN_ID if multi_use else None
)
self.assertIsNone(snapshot._transaction_read_timestamp)

def test_execute_sql_twice_single_use_fails(self):
from google.cloud.spanner_v1 import PartialResultSet

Expand Down
Loading