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
9 changes: 9 additions & 0 deletions google/cloud/spanner_v1/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import time
import base64
import threading
import uuid

from google.protobuf.struct_pb2 import ListValue
from google.protobuf.struct_pb2 import Value
Expand Down Expand Up @@ -219,6 +220,8 @@ def _make_value_pb(value):
return Value(null_value="NULL_VALUE")
else:
return Value(string_value=base64.b64encode(value))
if isinstance(value, uuid.UUID):
return Value(string_value=str(value))

raise ValueError("Unknown type: %s" % (value,))

Expand Down Expand Up @@ -320,6 +323,8 @@ def _get_type_decoder(field_type, field_name, column_info=None):
return _parse_numeric
elif type_code == TypeCode.JSON:
return _parse_json
elif type_code == TypeCode.UUID:
return _parse_uuid
elif type_code == TypeCode.PROTO:
return lambda value_pb: _parse_proto(value_pb, column_info, field_name)
elif type_code == TypeCode.ENUM:
Expand Down Expand Up @@ -400,6 +405,10 @@ def _parse_json(value_pb):
return JsonObject.from_str(value_pb.string_value)


def _parse_uuid(value_pb):
return uuid.UUID(value_pb.string_value)


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
1 change: 1 addition & 0 deletions google/cloud/spanner_v1/param_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
TIMESTAMP = Type(code=TypeCode.TIMESTAMP)
NUMERIC = Type(code=TypeCode.NUMERIC)
JSON = Type(code=TypeCode.JSON)
UUID = Type(code=TypeCode.UUID)
PG_NUMERIC = Type(code=TypeCode.NUMERIC, type_annotation=TypeAnnotationCode.PG_NUMERIC)
PG_JSONB = Type(code=TypeCode.JSON, type_annotation=TypeAnnotationCode.PG_JSONB)
PG_OID = Type(code=TypeCode.INT64, type_annotation=TypeAnnotationCode.PG_OID)
Expand Down
1 change: 1 addition & 0 deletions google/cloud/spanner_v1/streamed.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ def _merge_struct(lhs, rhs, type_):
TypeCode.JSON: _merge_string,
TypeCode.PROTO: _merge_string,
TypeCode.ENUM: _merge_string,
TypeCode.UUID: _merge_string,
}


Expand Down
11 changes: 11 additions & 0 deletions tests/system/test_session_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import struct
import threading
import time
import uuid
import pytest

import grpc
Expand Down Expand Up @@ -2825,6 +2826,16 @@ def test_execute_sql_returning_transfinite_floats(sessions_database, not_postgre
assert math.isnan(float_array[2])


def test_execute_sql_w_uuid_bindings(sessions_database, database_dialect):
_bind_test_helper(
sessions_database,
database_dialect,
spanner_v1.param_types.UUID,
uuid.uuid4(),
[uuid.uuid4(), uuid.uuid4()],
)


def test_partition_query(sessions_database, not_emulator):
row_count = 40
sql = f"SELECT * FROM {_sample_data.TABLE}"
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


import unittest
import uuid
import mock


Expand Down Expand Up @@ -738,6 +739,18 @@ def test_w_proto_enum(self):
self._callFUT(value_pb, field_type, field_name, column_info), VALUE
)

def test_w_uuid(self):
from google.protobuf.struct_pb2 import Value
from google.cloud.spanner_v1 import Type
from google.cloud.spanner_v1 import TypeCode

VALUE = uuid.uuid4()
field_type = Type(code=TypeCode.UUID)
field_name = "uuid_column"
value_pb = Value(string_value=str(VALUE))

self.assertEqual(self._callFUT(value_pb, field_type, field_name), VALUE)


class Test_parse_list_value_pbs(unittest.TestCase):
def _callFUT(self, *args, **kw):
Expand Down
Loading