Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for NUMERIC type #86

Merged
merged 9 commits into from Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions google/cloud/spanner_v1/_helpers.py
Expand Up @@ -15,6 +15,7 @@
"""Helper functions for Cloud Spanner."""

import datetime
import decimal
import math

import six
Expand Down Expand Up @@ -127,6 +128,8 @@ def _make_value_pb(value):
return Value(string_value=value)
if isinstance(value, ListValue):
return Value(list_value=value)
if isinstance(value, decimal.Decimal):
return Value(string_value=str(value))
raise ValueError("Unknown type: %s" % (value,))


Expand Down Expand Up @@ -201,6 +204,8 @@ def _parse_value_pb(value_pb, field_type):
_parse_value_pb(item_pb, field_type.struct_type.fields[i].type)
for (i, item_pb) in enumerate(value_pb.list_value.values)
]
elif field_type.code == type_pb2.NUMERIC:
result = decimal.Decimal(value_pb.string_value)
else:
raise ValueError("Unknown type: %s" % (field_type,))
return result
Expand Down
1 change: 1 addition & 0 deletions google/cloud/spanner_v1/param_types.py
Expand Up @@ -25,6 +25,7 @@
FLOAT64 = type_pb2.Type(code=type_pb2.FLOAT64)
DATE = type_pb2.Type(code=type_pb2.DATE)
TIMESTAMP = type_pb2.Type(code=type_pb2.TIMESTAMP)
NUMERIC = type_pb2.Type(code=type_pb2.NUMERIC)


def Array(element_type): # pylint: disable=invalid-name
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test__helpers.py
Expand Up @@ -208,6 +208,15 @@ def test_w_datetime(self):
self.assertIsInstance(value_pb, Value)
self.assertEqual(value_pb.string_value, datetime_helpers.to_rfc3339(now))

def test_w_numeric(self):
import decimal
from google.protobuf.struct_pb2 import Value

value = decimal.Decimal("9999999999999999999999999999.999999999")
value_pb = self._callFUT(value)
self.assertIsInstance(value_pb, Value)
self.assertEqual(value_pb.string_value, str(value))

def test_w_unknown_type(self):
with self.assertRaises(ValueError):
self._callFUT(object())
Expand Down Expand Up @@ -431,6 +440,17 @@ def test_w_struct(self):

self.assertEqual(self._callFUT(value_pb, field_type), VALUES)

def test_w_numeric(self):
import decimal
from google.protobuf.struct_pb2 import Value
from google.cloud.spanner_v1.proto.type_pb2 import Type, NUMERIC

VALUE = decimal.Decimal("99999999999999999999999999999.999999999")
larkee marked this conversation as resolved.
Show resolved Hide resolved
field_type = Type(code=NUMERIC)
value_pb = Value(string_value=str(VALUE))

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

def test_w_unknown_type(self):
from google.protobuf.struct_pb2 import Value
from google.cloud.spanner_v1.proto.type_pb2 import Type
Expand Down