diff --git a/packages/google-cloud-spanner/google/cloud/spanner_dbapi/connection.py b/packages/google-cloud-spanner/google/cloud/spanner_dbapi/connection.py index fe4fb6f93a8f..03a012ae2ed7 100644 --- a/packages/google-cloud-spanner/google/cloud/spanner_dbapi/connection.py +++ b/packages/google-cloud-spanner/google/cloud/spanner_dbapi/connection.py @@ -737,6 +737,38 @@ def partition_query( query_options=None, data_boost_enabled=None, ): + """Partition a read-only query so each partition can be executed independently. + + Returns a list of string-encoded partition IDs that can be passed to + :meth:`run_partition` (or ``RUN PARTITION ``) on this connection or + another connection targeting the same database. + + Each encoded partition ID is a base64-encoded, gzip-compressed JSON + payload containing the ``BatchTransactionId`` (``session_id``, + ``transaction_id``, ``read_timestamp``), the Spanner partition token, + the SQL query, query parameters (when present), and execution options + (such as ``data_boost_enabled``, ``directed_read_options``, and + ``query_options``). It is neither encrypted nor signed, and it is tied + to the lifetime of the underlying session and read-only snapshot (bounded + by Spanner version retention). Share encoded partition IDs only with + trusted workers over trusted channels. + + :type parsed_statement: :class:`~google.cloud.spanner_dbapi.parsed_statement.ParsedStatement` + :param parsed_statement: Parsed ``PARTITION `` statement. + + :type query_options: dict or :class:`~google.cloud.spanner_v1.types.ExecuteSqlRequest.QueryOptions` + :param query_options: (Optional) Query optimizer options. + + :type data_boost_enabled: Optional[bool] + :param data_boost_enabled: (Optional) Whether to enable Data Boost. + Defaults to :attr:`data_boost_enabled`. + + :rtype: list[str] + :returns: Encoded partition IDs for use with :meth:`run_partition`. + + :raises: :class:`~google.cloud.spanner_dbapi.exceptions.ProgrammingError` + if the connection is currently in a read/write transaction. + """ statement = parsed_statement.statement partitioned_query = parsed_statement.client_side_statement_params[0] self._partitioned_query_validation(partitioned_query, statement) @@ -765,6 +797,25 @@ def partition_query( @check_not_closed def run_partition(self, encoded_partition_id): + """Execute a single partition produced by :meth:`partition_query`. + + Decodes ``encoded_partition_id`` (from :meth:`partition_query` or + ``RUN PARTITION ``) and executes the contained query descriptor in an + independent :class:`~google.cloud.spanner_v1.database.BatchSnapshot` + against this connection's database. It does not join the connection's + active transaction. + + The encoded partition ID contains plaintext query, parameter, and + session details and is neither encrypted nor signed. Only accept + encoded partition IDs from trusted producers over trusted channels. + + :type encoded_partition_id: str + :param encoded_partition_id: Encoded partition ID returned by + :meth:`partition_query` or ``PARTITION ``. + + :rtype: :class:`~google.cloud.spanner_v1.streamed.StreamedResultSet` + :returns: Result set for the executed partition. + """ partition_id: PartitionId = partition_helper.decode_from_string( encoded_partition_id ) diff --git a/packages/google-cloud-spanner/google/cloud/spanner_dbapi/partition_helper.py b/packages/google-cloud-spanner/google/cloud/spanner_dbapi/partition_helper.py index bbb5288f8e22..13aa14807de3 100644 --- a/packages/google-cloud-spanner/google/cloud/spanner_dbapi/partition_helper.py +++ b/packages/google-cloud-spanner/google/cloud/spanner_dbapi/partition_helper.py @@ -91,6 +91,23 @@ def _deserialize_value(val: Any) -> Any: def decode_from_string(encoded_partition_id): + """Decode a string-encoded partition ID into a :class:`PartitionId`. + + The input string is expected to be produced by :func:`encode_to_string` + (base64-encoded, gzip-compressed JSON). Because the payload is neither + encrypted nor signed, encoded partition IDs must only be accepted from + trusted producers over trusted channels. + + Any query parameters under ``partition_result["query"]["params"]`` are + returned as JSON-compatible protobuf primitives (for example, 64-bit + integers as strings) for re-execution via ``BatchSnapshot.process``. + + :type encoded_partition_id: str + :param encoded_partition_id: Serialized partition ID string. + + :rtype: :class:`PartitionId` + :returns: Decoded ``BatchTransactionId`` and partition descriptor. + """ gzip_bytes = base64.b64decode(bytes(encoded_partition_id, "utf-8")) partition_id_bytes = gzip.decompress(gzip_bytes) @@ -113,6 +130,26 @@ def decode_from_string(encoded_partition_id): def encode_to_string(batch_transaction_id, partition_result): + """Encode a ``BatchTransactionId`` and partition batch into a string. + + Serializes the transaction metadata, Spanner partition token, SQL/read + descriptor, parameters, and execution options as gzip-compressed, + base64-encoded JSON so the partition can be executed on another connection + targeting the same database. The encoded string is neither encrypted nor + signed and should only be shared with trusted workers over trusted + channels. + + :type batch_transaction_id: :class:`~google.cloud.spanner_v1.BatchTransactionId` + :param batch_transaction_id: Transaction metadata for the batch snapshot. + + :type partition_result: dict + :param partition_result: Partition descriptor produced by + ``BatchSnapshot.generate_query_batches`` or + ``BatchSnapshot.generate_read_batches``. + + :rtype: str + :returns: Base64-encoded, gzip-compressed JSON partition ID. + """ # Copy to avoid modifying the caller's dictionary in connection.py partition_result = copy.deepcopy(partition_result) @@ -139,5 +176,7 @@ def encode_to_string(batch_transaction_id, partition_result): @dataclass class PartitionId: + """Decoded partition descriptor containing transaction and batch details.""" + batch_transaction_id: BatchTransactionId partition_result: Any