Skip to content


feat(dbapi): remove string conversion for numeric fields (#317)
Browse files Browse the repository at this point in the history
* feat: updated googleapis proto changes for request tags

* feat: added support for numberic for python decimal value

* feat: added support for converting decimal field to numeric field and removed it's conversion to string
  • Loading branch information
vi3k6i5 committed Apr 26, 2021
1 parent 070a171 commit 772aa3c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 27 deletions.
16 changes: 1 addition & 15 deletions google/cloud/spanner_dbapi/
Original file line number Diff line number Diff line change
Expand Up @@ -509,25 +509,11 @@ def sql_pyformat_args_to_spanner(sql, params):
resolved_value = pyfmt % params
named_args[key] = resolved_value
named_args[key] = cast_for_spanner(params[i])
named_args[key] = params[i]

return sanitize_literals_for_upload(sql), named_args

def cast_for_spanner(value):
"""Convert the param to its Cloud Spanner equivalent type.
:type value: Any
:param value: The value to convert to a Cloud Spanner type.
:rtype: Any
:returns: The value converted to a Cloud Spanner type.
if isinstance(value, decimal.Decimal):
return str(value)
return value

def get_param_types(params):
"""Determine Cloud Spanner types for the given parameters.
Expand Down
13 changes: 1 addition & 12 deletions tests/unit/spanner_dbapi/
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def test_sql_pyformat_args_to_spanner(self):
"SELECT (an.p + @a0) AS np FROM an WHERE (an.p + @a1) = @a2",
{"a0": 1, "a1": 1.0, "a2": str(31)},
{"a0": 1, "a1": 1.0, "a2": decimal.Decimal("31")},
Expand Down Expand Up @@ -339,17 +339,6 @@ def test_sql_pyformat_args_to_spanner_invalid(self):
lambda: sql_pyformat_args_to_spanner(sql, params),

def test_cast_for_spanner(self):
import decimal

from import cast_for_spanner

dec = 3
value = decimal.Decimal(dec)
self.assertEqual(cast_for_spanner(value), str(dec))
self.assertEqual(cast_for_spanner(5), 5)
self.assertEqual(cast_for_spanner("string"), "string")

@unittest.skipIf(skip_condition, skip_message)
def test_get_param_types(self):
import datetime
Expand Down

0 comments on commit 772aa3c

Please sign in to comment.