Skip to content
This repository was archived by the owner on Jun 8, 2026. It is now read-only.

Commit 3b1792a

Browse files
feat: add uuid support (#1310)
Signed-off-by: Sri Harsha CH <sriharshach@google.com> Co-authored-by: Subham Sinha <suvham@google.com>
1 parent 9ec95b7 commit 3b1792a

5 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎google/cloud/spanner_v1/_helpers.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
importbase64
2222
importthreading
2323
importlogging
24+
importuuid
2425

2526
fromgoogle.protobuf.struct_pb2importListValue
2627
fromgoogle.protobuf.struct_pb2importValue
@@ -298,6 +299,8 @@ def _make_value_pb(value):
298299
returnValue(string_value=base64.b64encode(value))
299300
ifisinstance(value, Interval):
300301
returnValue(string_value=str(value))
302+
ifisinstance(value, uuid.UUID):
303+
returnValue(string_value=str(value))
301304

302305
raiseValueError("Unknown type: %s"% (value,))
303306

@@ -399,6 +402,8 @@ def _get_type_decoder(field_type, field_name, column_info=None):
399402
return_parse_numeric
400403
eliftype_code==TypeCode.JSON:
401404
return_parse_json
405+
eliftype_code==TypeCode.UUID:
406+
return_parse_uuid
402407
eliftype_code==TypeCode.PROTO:
403408
returnlambdavalue_pb: _parse_proto(value_pb, column_info, field_name)
404409
eliftype_code==TypeCode.ENUM:
@@ -481,6 +486,10 @@ def _parse_json(value_pb):
481486
returnJsonObject.from_str(value_pb.string_value)
482487

483488

489+
def_parse_uuid(value_pb):
490+
returnuuid.UUID(value_pb.string_value)
491+
492+
484493
def_parse_proto(value_pb, column_info, field_name):
485494
bytes_value=base64.b64decode(value_pb.string_value)
486495
ifcolumn_infoisnotNoneandcolumn_info.get(field_name) isnotNone:

‎google/cloud/spanner_v1/param_types.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
TIMESTAMP=Type(code=TypeCode.TIMESTAMP)
3434
NUMERIC=Type(code=TypeCode.NUMERIC)
3535
JSON=Type(code=TypeCode.JSON)
36+
UUID=Type(code=TypeCode.UUID)
3637
PG_NUMERIC=Type(code=TypeCode.NUMERIC, type_annotation=TypeAnnotationCode.PG_NUMERIC)
3738
PG_JSONB=Type(code=TypeCode.JSON, type_annotation=TypeAnnotationCode.PG_JSONB)
3839
PG_OID=Type(code=TypeCode.INT64, type_annotation=TypeAnnotationCode.PG_OID)

‎google/cloud/spanner_v1/streamed.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ def _merge_struct(lhs, rhs, type_):
394394
TypeCode.PROTO: _merge_string,
395395
TypeCode.INTERVAL: _merge_string,
396396
TypeCode.ENUM: _merge_string,
397+
TypeCode.UUID: _merge_string,
397398
}
398399

399400

‎tests/system/test_session_api.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
importstruct
2121
importthreading
2222
importtime
23+
importuuid
2324
importpytest
2425

2526
importgrpc
@@ -3056,6 +3057,18 @@ def test_execute_sql_returning_transfinite_floats(sessions_database, not_postgre
30563057
assertmath.isnan(float_array[2])
30573058

30583059

3060+
deftest_execute_sql_w_uuid_bindings(sessions_database, database_dialect):
3061+
ifdatabase_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+
30593072
deftest_partition_query(sessions_database, not_emulator, not_experimental_host):
30603073
row_count=40
30613074
sql=f"SELECT * FROM {_sample_data.TABLE}"

‎tests/unit/test__helpers.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515

1616
importunittest
17+
importuuid
1718
importmock
1819

1920
fromopentelemetry.sdk.resourcesimportResource
@@ -786,6 +787,18 @@ def test_w_proto_enum(self):
786787
self._callFUT(value_pb, field_type, field_name, column_info), VALUE
787788
)
788789

790+
deftest_w_uuid(self):
791+
fromgoogle.protobuf.struct_pb2importValue
792+
fromgoogle.cloud.spanner_v1importType
793+
fromgoogle.cloud.spanner_v1importTypeCode
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+
789802

790803
classTest_parse_list_value_pbs(unittest.TestCase):
791804
def_callFUT(self, *args, **kw):

0 commit comments

Comments
 (0)