-
Notifications
You must be signed in to change notification settings - Fork 308
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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"), | ||
|
@@ -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) | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should have removed that, it's just a subset of the |
||
|
||
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"}) | ||
|
@@ -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"), | ||
( | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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: | ||
|
@@ -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"), | ||
] | ||
), | ||
) | ||
|
@@ -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): | ||
|
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.
There was a problem hiding this comment.
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:Even
1.0E+29
is too large, but9.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.