GeezerVison is showing github.com — feat: add uuid support (#1310) · googleapis/python-spanner@3b1792a
This repository was archived by the owner on Jun 8, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google/cloud/spanner_v1/_helpers.py +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | import base64 | 22 | 22 | import threading | 23 | 23 | import logging | | 24 | +import uuid | 24 | 25 |
| 25 | 26 | from google.protobuf.struct_pb2 import ListValue | 26 | 27 | from google.protobuf.struct_pb2 import Value | @@ -298,6 +299,8 @@ def _make_value_pb(value): | 298 | 299 | return Value(string_value=base64.b64encode(value)) | 299 | 300 | if isinstance(value, Interval): | 300 | 301 | return Value(string_value=str(value)) | | 302 | + if isinstance(value, uuid.UUID): | | 303 | + return Value(string_value=str(value)) | 301 | 304 |
| 302 | 305 | raise ValueError("Unknown type: %s" % (value,)) | 303 | 306 |
| @@ -399,6 +402,8 @@ def _get_type_decoder(field_type, field_name, column_info=None): | 399 | 402 | return _parse_numeric | 400 | 403 | elif type_code == TypeCode.JSON: | 401 | 404 | return _parse_json | | 405 | + elif type_code == TypeCode.UUID: | | 406 | + return _parse_uuid | 402 | 407 | elif type_code == TypeCode.PROTO: | 403 | 408 | return lambda value_pb: _parse_proto(value_pb, column_info, field_name) | 404 | 409 | elif type_code == TypeCode.ENUM: | @@ -481,6 +486,10 @@ def _parse_json(value_pb): | 481 | 486 | return JsonObject.from_str(value_pb.string_value) | 482 | 487 |
| 483 | 488 |
| | 489 | +def _parse_uuid(value_pb): | | 490 | + return uuid.UUID(value_pb.string_value) | | 491 | + | | 492 | + | 484 | 493 | def _parse_proto(value_pb, column_info, field_name): | 485 | 494 | bytes_value = base64.b64decode(value_pb.string_value) | 486 | 495 | if column_info is not None and column_info.get(field_name) is not None: | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/spanner_v1/param_types.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 33 | 33 | TIMESTAMP = Type(code=TypeCode.TIMESTAMP) | 34 | 34 | NUMERIC = Type(code=TypeCode.NUMERIC) | 35 | 35 | JSON = Type(code=TypeCode.JSON) | | 36 | +UUID = Type(code=TypeCode.UUID) | 36 | 37 | PG_NUMERIC = Type(code=TypeCode.NUMERIC, type_annotation=TypeAnnotationCode.PG_NUMERIC) | 37 | 38 | PG_JSONB = Type(code=TypeCode.JSON, type_annotation=TypeAnnotationCode.PG_JSONB) | 38 | 39 | PG_OID = Type(code=TypeCode.INT64, type_annotation=TypeAnnotationCode.PG_OID) | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/spanner_v1/streamed.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -394,6 +394,7 @@ def _merge_struct(lhs, rhs, type_): | 394 | 394 | TypeCode.PROTO: _merge_string, | 395 | 395 | TypeCode.INTERVAL: _merge_string, | 396 | 396 | TypeCode.ENUM: _merge_string, | | 397 | + TypeCode.UUID: _merge_string, | 397 | 398 | } | 398 | 399 |
| 399 | 400 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/system/test_session_api.py +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | import struct | 21 | 21 | import threading | 22 | 22 | import time | | 23 | +import uuid | 23 | 24 | import pytest | 24 | 25 |
| 25 | 26 | import grpc | @@ -3056,6 +3057,18 @@ def test_execute_sql_returning_transfinite_floats(sessions_database, not_postgre | 3056 | 3057 | assert math.isnan(float_array[2]) | 3057 | 3058 |
| 3058 | 3059 |
| | 3060 | +def test_execute_sql_w_uuid_bindings(sessions_database, database_dialect): | | 3061 | + if database_dialect == DatabaseDialect.POSTGRESQL: | | 3062 | + pytest.skip("UUID parameter type is not yet supported in PostgreSQL dialect.") | | 3063 | + _bind_test_helper( | | 3064 | + sessions_database, | | 3065 | + database_dialect, | | 3066 | + spanner_v1.param_types.UUID, | | 3067 | + uuid.uuid4(), | | 3068 | + [uuid.uuid4(), uuid.uuid4()], | | 3069 | + ) | | 3070 | + | | 3071 | + | 3059 | 3072 | def test_partition_query(sessions_database, not_emulator, not_experimental_host): | 3060 | 3073 | row_count = 40 | 3061 | 3074 | sql = f"SELECT * FROM {_sample_data.TABLE}" | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/test__helpers.py +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 |
| 15 | 15 |
| 16 | 16 | import unittest | | 17 | +import uuid | 17 | 18 | import mock | 18 | 19 |
| 19 | 20 | from opentelemetry.sdk.resources import Resource | @@ -786,6 +787,18 @@ def test_w_proto_enum(self): | 786 | 787 | self._callFUT(value_pb, field_type, field_name, column_info), VALUE | 787 | 788 | ) | 788 | 789 |
| | 790 | + def test_w_uuid(self): | | 791 | + from google.protobuf.struct_pb2 import Value | | 792 | + from google.cloud.spanner_v1 import Type | | 793 | + from google.cloud.spanner_v1 import TypeCode | | 794 | + | | 795 | + VALUE = uuid.uuid4() | | 796 | + field_type = Type(code=TypeCode.UUID) | | 797 | + field_name = "uuid_column" | | 798 | + value_pb = Value(string_value=str(VALUE)) | | 799 | + | | 800 | + self.assertEqual(self._callFUT(value_pb, field_type, field_name), VALUE) | | 801 | + | 789 | 802 |
| 790 | 803 | class Test_parse_list_value_pbs(unittest.TestCase): | 791 | 804 | def _callFUT(self, *args, **kw): | |
|