GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add decimal validation for numeric precision and scale supported by Spanner by vi3k6i5 · Pull Request #340 · googleapis/python-spanner

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

feat: add decimal validation for numeric precision and scale supported by Spanner #340

Merged
vi3k6i5 merged 13 commits into googleapis:master from vi3k6i5:decimal_support
May 18, 2021

Uh oh!

There was an error while loading. Please reload this page.

Merged

feat: add decimal validation for numeric precision and scale supported by Spanner #340

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
d0f6d41
Merge pull request #2 from googleapis/master
vi3k6i5 Mar 16, 2021
d051236
Merge pull request #3 from googleapis/master
vi3k6i5 Mar 18, 2021
b3b9805
Merge pull request #4 from googleapis/master
vi3k6i5 Mar 25, 2021
0ca3deb
Merge pull request #5 from googleapis/master
vi3k6i5 Apr 5, 2021
80a8240
feat: updated googleapis proto changes for request tags
vi3k6i5 Apr 5, 2021
b200cdb
Merge pull request #6 from googleapis/master
vi3k6i5 Apr 22, 2021
b8b4827
Merge pull request #7 from googleapis/master
vi3k6i5 Apr 23, 2021
222921c
Merge branch 'googleapis:master' into master
vi3k6i5 May 7, 2021
42cc4ba
Merge branch 'googleapis:master' into master
vi3k6i5 May 11, 2021
ed9b8f1
Merge branch 'googleapis:master' into master
vi3k6i5 May 12, 2021
a4bb797
feat: added support for numberic for python decimal value
vi3k6i5 Apr 22, 2021
2a7c9bb
feat: add decimal validation for numeric precission and scale support…
vi3k6i5 May 12, 2021
74e9551
fix: moved decimal validation from spanner_dbapi to spanner_v1/helpe…
vi3k6i5 May 14, 2021
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

33 changes: 33 additions & 0 deletions 33 google/cloud/spanner_v1/_helpers.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
from google.cloud.spanner_v1 import ExecuteSqlRequest


# Validation error messages
NUMERIC_MAX_SCALE_ERR_MSG = (
"Max scale for a numeric is 9. The requested numeric has scale {}"
)
NUMERIC_MAX_PRECISION_ERR_MSG = (
"Max precision for the whole component of a numeric is 29. The requested "
+ "numeric has a whole component with precision {}"
)


def _try_to_coerce_bytes(bytestring):
"""Try to coerce a byte string into the right thing based on Python
version and whether or not it is base64 encoded.
Expand Down Expand Up @@ -87,6 +97,28 @@ def _merge_query_options(base, merge):
return combined


def _assert_numeric_precision_and_scale(value):
"""
Asserts that input numeric field is within Spanner supported range.

Spanner supports fixed 38 digits of precision and 9 digits of scale.
This number can be optionally prefixed with a plus or minus sign.
Read more here: https://cloud.google.com/spanner/docs/data-types#numeric_type

:type value: decimal.Decimal
:param value: The value to check for Cloud Spanner compatibility.

:raises NotSupportedError: If value is not within supported precision or scale of Spanner.
"""
scale = value.as_tuple().exponent
precision = len(value.as_tuple().digits)

if scale < -9:
raise ValueError(NUMERIC_MAX_SCALE_ERR_MSG.format(abs(scale)))
if precision + scale > 29:
raise ValueError(NUMERIC_MAX_PRECISION_ERR_MSG.format(precision + scale))


# pylint: disable=too-many-return-statements,too-many-branches
def _make_value_pb(value):
"""Helper for :func:`_make_list_value_pbs`.
Expand Down Expand Up @@ -129,6 +161,7 @@ def _make_value_pb(value):
if isinstance(value, ListValue):
return Value(list_value=value)
if isinstance(value, decimal.Decimal):
_assert_numeric_precision_and_scale(value)
return Value(string_value=str(value))
raise ValueError("Unknown type: %s" % (value,))

Expand Down
12 changes: 0 additions & 12 deletions 12 tests/unit/spanner_dbapi/test_parse_utils.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,6 @@ def test_rows_for_insert_or_update(self):

@unittest.skipIf(skip_condition, skip_message)
def test_sql_pyformat_args_to_spanner(self):
import decimal

from google.cloud.spanner_dbapi.parse_utils import sql_pyformat_args_to_spanner

cases = [
Expand Down Expand Up @@ -300,16 +298,6 @@ def test_sql_pyformat_args_to_spanner(self):
("SELECT * from t WHERE id=10", {"f1": "app", "f2": "name"}),
("SELECT * from t WHERE id=10", {"f1": "app", "f2": "name"}),
),
(
(
"SELECT (an.p + %s) AS np FROM an WHERE (an.p + %s) = %s",
(1, 1.0, decimal.Decimal("31")),
),
(
"SELECT (an.p + @a0) AS np FROM an WHERE (an.p + @a1) = @a2",
{"a0": 1, "a1": 1.0, "a2": decimal.Decimal("31")},
),
),
]
for ((sql_in, params), sql_want) in cases:
with self.subTest(sql=sql_in):
Expand Down
59 changes: 59 additions & 0 deletions 59 tests/unit/test__helpers.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,65 @@ def test_w_unknown_type(self):
with self.assertRaises(ValueError):
self._callFUT(object())

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

cases = [
decimal.Decimal("42"),
decimal.Decimal("9.9999999999999999999999999999999999999E+28"),
decimal.Decimal("-9.9999999999999999999999999999999999999E+28"),
decimal.Decimal("99999999999999999999999999999.999999999"),
decimal.Decimal("1E+28"),
decimal.Decimal("1E-9"),
]
for value in cases:
with self.subTest(value=value):
value_pb = self._callFUT(value)
self.assertIsInstance(value_pb, Value)
self.assertEqual(value_pb.string_value, str(value))

def test_w_numeric_precision_and_scale_invalid(self):
import decimal
from google.cloud.spanner_v1._helpers import (
NUMERIC_MAX_SCALE_ERR_MSG,
NUMERIC_MAX_PRECISION_ERR_MSG,
)

max_precision_error_msg = NUMERIC_MAX_PRECISION_ERR_MSG.format("30")
max_scale_error_msg = NUMERIC_MAX_SCALE_ERR_MSG.format("10")

cases = [
(
decimal.Decimal("9.9999999999999999999999999999999999999E+29"),
max_precision_error_msg,
),
(
decimal.Decimal("-9.9999999999999999999999999999999999999E+29"),
max_precision_error_msg,
),
(
decimal.Decimal("999999999999999999999999999999.99999999"),
max_precision_error_msg,
),
(
decimal.Decimal("-999999999999999999999999999999.99999999"),
max_precision_error_msg,
),
(
decimal.Decimal("999999999999999999999999999999"),
max_precision_error_msg,
),
(decimal.Decimal("1E+29"), max_precision_error_msg),
(decimal.Decimal("1E-10"), max_scale_error_msg),
]

for value, err_msg in cases:
with self.subTest(value=value, err_msg=err_msg):
self.assertRaisesRegex(
ValueError, err_msg, lambda: self._callFUT(value),
)


class Test_make_list_value_pb(unittest.TestCase):
def _callFUT(self, *args, **kw):
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.