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

fix: DB API depends on pyarrow when decimal query parameters are used #551

Merged
merged 3 commits into from Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 15 additions & 8 deletions google/cloud/bigquery/dbapi/_helpers.py
Expand Up @@ -19,16 +19,15 @@
import functools
import numbers

try:
import pyarrow
except ImportError: # pragma: NO COVER
pyarrow = None

from google.cloud import bigquery
from google.cloud.bigquery import table
from google.cloud.bigquery.dbapi import exceptions


_NUMERIC_SERVER_MIN = decimal.Decimal("-9.9999999999999999999999999999999999999E+28")
_NUMERIC_SERVER_MAX = decimal.Decimal("9.9999999999999999999999999999999999999E+28")


def scalar_to_query_parameter(value, name=None):
"""Convert a scalar value into a query parameter.

Expand Down Expand Up @@ -189,12 +188,20 @@ def bigquery_scalar_type(value):
elif isinstance(value, numbers.Real):
return "FLOAT64"
elif isinstance(value, decimal.Decimal):
# We check for NUMERIC before BIGNUMERIC in order to support pyarrow < 3.0.
scalar_object = pyarrow.scalar(value)
if isinstance(scalar_object, pyarrow.Decimal128Scalar):
vtuple = value.as_tuple()
# NUMERIC values have precision of 38 (number of digits) and scale of 9 (number
# of fractional digits), and their max absolute value must be strictly smaller
# than 1.0E+29.
# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#decimal_types
if (
len(vtuple.digits) <= 38 # max precision: 38
and vtuple.exponent >= -9 # max scale: 9
and _NUMERIC_SERVER_MIN <= value <= _NUMERIC_SERVER_MAX
):
return "NUMERIC"
Comment on lines +192 to 201
Copy link
Contributor Author

@plamut plamut Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic differs from the docs, because testing the actual backend response showed that 9.9999999999999999999999999999999999999E+29 is not accepted as a valid NUMERIC value:

SELECT CAST((SELECT "9.9999999999999999999999999999999999999E+29" AS literal) AS NUMERIC);  # error

Even 1.0E+29 is too large, but 9.999...9E+28 works, meaning that there's probably an off-by-one error in the docs for the exponent.

Update: Confirmed, the docs are indeed not correct, an internal issue 182900100 has been created to track this.

else:
return "BIGNUMERIC"

elif isinstance(value, str):
return "STRING"
elif isinstance(value, bytes):
Expand Down
66 changes: 45 additions & 21 deletions tests/unit/test_dbapi__helpers.py
Expand Up @@ -25,7 +25,6 @@

import google.cloud._helpers
from google.cloud.bigquery import table
from google.cloud.bigquery._pandas_helpers import _BIGNUMERIC_SUPPORT
from google.cloud.bigquery.dbapi import _helpers
from google.cloud.bigquery.dbapi import exceptions
from tests.unit.helpers import _to_pyarrow
Expand All @@ -39,9 +38,8 @@ def test_scalar_to_query_parameter(self):
(123, "INT64"),
(-123456789, "INT64"),
(1.25, "FLOAT64"),
(decimal.Decimal("1.25"), "NUMERIC"),
(b"I am some bytes", "BYTES"),
(u"I am a string", "STRING"),
("I am a string", "STRING"),
(datetime.date(2017, 4, 1), "DATE"),
(datetime.time(12, 34, 56), "TIME"),
(datetime.datetime(2012, 3, 4, 5, 6, 7), "DATETIME"),
Expand All @@ -51,14 +49,17 @@ def test_scalar_to_query_parameter(self):
),
"TIMESTAMP",
),
(decimal.Decimal("1.25"), "NUMERIC"),
(decimal.Decimal("9.9999999999999999999999999999999999999E+28"), "NUMERIC"),
(decimal.Decimal("1.0E+29"), "BIGNUMERIC"), # more than max NUMERIC value
(decimal.Decimal("1.123456789"), "NUMERIC"),
(decimal.Decimal("1.1234567891"), "BIGNUMERIC"), # scale > 9
(decimal.Decimal("12345678901234567890123456789.012345678"), "NUMERIC"),
(
decimal.Decimal("12345678901234567890123456789012345678"),
"BIGNUMERIC", # larger than max NUMERIC value, despite precision <=38
),
]
if _BIGNUMERIC_SUPPORT:
expected_types.append(
(
decimal.Decimal("1.1234567890123456789012345678901234567890"),
"BIGNUMERIC",
)
)

for value, expected_type in expected_types:
msg = "value: {} expected_type: {}".format(value, expected_type)
Expand All @@ -71,6 +72,33 @@ def test_scalar_to_query_parameter(self):
self.assertEqual(named_parameter.type_, expected_type, msg=msg)
self.assertEqual(named_parameter.value, value, msg=msg)

def test_decimal_to_query_parameter(self): # TODO: merge with previous test
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have removed that, it's just a subset of the test_scalar_to_query_parameter test above it.


expected_types = [
(decimal.Decimal("9.9999999999999999999999999999999999999E+28"), "NUMERIC"),
(decimal.Decimal("1.0E+29"), "BIGNUMERIC"), # more than max value
(decimal.Decimal("1.123456789"), "NUMERIC"),
(decimal.Decimal("1.1234567891"), "BIGNUMERIC"), # scale > 9
(decimal.Decimal("12345678901234567890123456789.012345678"), "NUMERIC"),
(
decimal.Decimal("12345678901234567890123456789012345678"),
"BIGNUMERIC", # larger than max size, even if precision <=38
),
]

for value, expected_type in expected_types:
msg = f"value: {value} expected_type: {expected_type}"

parameter = _helpers.scalar_to_query_parameter(value)
self.assertIsNone(parameter.name, msg=msg)
self.assertEqual(parameter.type_, expected_type, msg=msg)
self.assertEqual(parameter.value, value, msg=msg)

named_parameter = _helpers.scalar_to_query_parameter(value, name="myvar")
self.assertEqual(named_parameter.name, "myvar", msg=msg)
self.assertEqual(named_parameter.type_, expected_type, msg=msg)
self.assertEqual(named_parameter.value, value, msg=msg)

def test_scalar_to_query_parameter_w_unexpected_type(self):
with self.assertRaises(exceptions.ProgrammingError):
_helpers.scalar_to_query_parameter(value={"a": "dictionary"})
Expand All @@ -89,8 +117,9 @@ def test_array_to_query_parameter_valid_argument(self):
([123, -456, 0], "INT64"),
([1.25, 2.50], "FLOAT64"),
([decimal.Decimal("1.25")], "NUMERIC"),
([decimal.Decimal("{d38}.{d38}".format(d38="9" * 38))], "BIGNUMERIC"),
([b"foo", b"bar"], "BYTES"),
([u"foo", u"bar"], "STRING"),
(["foo", "bar"], "STRING"),
([datetime.date(2017, 4, 1), datetime.date(2018, 4, 1)], "DATE"),
([datetime.time(12, 34, 56), datetime.time(10, 20, 30)], "TIME"),
(
Expand All @@ -113,11 +142,6 @@ def test_array_to_query_parameter_valid_argument(self):
),
]

if _BIGNUMERIC_SUPPORT:
expected_types.append(
([decimal.Decimal("{d38}.{d38}".format(d38="9" * 38))], "BIGNUMERIC")
)

for values, expected_type in expected_types:
msg = "value: {} expected_type: {}".format(values, expected_type)
parameter = _helpers.array_to_query_parameter(values)
Expand All @@ -134,7 +158,7 @@ def test_array_to_query_parameter_empty_argument(self):
_helpers.array_to_query_parameter([])

def test_array_to_query_parameter_unsupported_sequence(self):
unsupported_iterables = [{10, 20, 30}, u"foo", b"bar", bytearray([65, 75, 85])]
unsupported_iterables = [{10, 20, 30}, "foo", b"bar", bytearray([65, 75, 85])]
for iterable in unsupported_iterables:
with self.assertRaises(exceptions.ProgrammingError):
_helpers.array_to_query_parameter(iterable)
Expand All @@ -144,7 +168,7 @@ def test_array_to_query_parameter_sequence_w_invalid_elements(self):
_helpers.array_to_query_parameter([object(), 2, 7])

def test_to_query_parameters_w_dict(self):
parameters = {"somebool": True, "somestring": u"a-string-value"}
parameters = {"somebool": True, "somestring": "a-string-value"}
query_parameters = _helpers.to_query_parameters(parameters)
query_parameter_tuples = []
for param in query_parameters:
Expand All @@ -154,7 +178,7 @@ def test_to_query_parameters_w_dict(self):
sorted(
[
("somebool", "BOOL", True),
("somestring", "STRING", u"a-string-value"),
("somestring", "STRING", "a-string-value"),
]
),
)
Expand All @@ -177,14 +201,14 @@ def test_to_query_parameters_w_dict_dict_param(self):
_helpers.to_query_parameters(parameters)

def test_to_query_parameters_w_list(self):
parameters = [True, u"a-string-value"]
parameters = [True, "a-string-value"]
query_parameters = _helpers.to_query_parameters(parameters)
query_parameter_tuples = []
for param in query_parameters:
query_parameter_tuples.append((param.name, param.type_, param.value))
self.assertSequenceEqual(
sorted(query_parameter_tuples),
sorted([(None, "BOOL", True), (None, "STRING", u"a-string-value")]),
sorted([(None, "BOOL", True), (None, "STRING", "a-string-value")]),
)

def test_to_query_parameters_w_list_array_param(self):
Expand Down