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

Client.insert_rows throws exception when coercing string to float from release 2.21.0 #818

Closed
Alan-Robinson opened this issue Jul 27, 2021 · 2 comments · Fixed by #824
Closed
Assignees
Labels
api: bigquery Issues related to the googleapis/python-bigquery API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@Alan-Robinson
Copy link

The behaviour of Client.insert_rows changed between release 2.20.0 and 2.21.0.

In 2.20.0, where the schema defines a field to be of type FLOAT, a string passed in would be coerced to a float in the resulting JSON.

In 2.21.0, where the schema defines a field to be of type FLOAT, a string passed in throws an exception.

I've tracked this down to #728 , where a check is added for isnan or isinf. However, those functions require a float to be provided so throw an exception if a string is provided. This check occurs before you get to the code that coerces the input to a float.

def _float_to_json(value):
    """Coerce 'value' to an JSON-compatible representation."""
    if value is None:
        return None
    elif math.isnan(value) or math.isinf(value):
        return str(value)
    else:
        return float(value)

Environment details

  • OS type and version:
  • Python version: Python 3.6.10
  • pip version: pip 21.2.1
  • google-cloud-bigquery version: 2.22.1

Steps to reproduce

  1. Setup table with a field defined to be FLOAT
  2. Pass a string to Client.insert_rows() for that field

Code example

from google.cloud.bigquery import Client

client = Client(project='redacted')
client.insert_rows(table=client.get_table('python_test.test'), rows=[{'bar': "0.01"}])

Stack trace

  File "site-packages/google/cloud/bigquery/client.py", line 3329, in insert_rows
    json_rows = [_record_field_to_json(schema, row) for row in rows]
  File "site-packages/google/cloud/bigquery/client.py", line 3329, in <listcomp>
    json_rows = [_record_field_to_json(schema, row) for row in rows]
  File "site-packages/google/cloud/bigquery/_helpers.py", line 522, in _record_field_to_json
    record[subname] = _field_to_json(subfield, subvalue)
  File "site-packages/google/cloud/bigquery/_helpers.py", line 589, in _field_to_json
    return _single_field_to_json(field, row_value)
  File "site-packages/google/cloud/bigquery/_helpers.py", line 565, in _single_field_to_json
    return _scalar_field_to_json(field, row_value)
  File "site-packages/google/cloud/bigquery/_helpers.py", line 464, in _scalar_field_to_json
    return converter(row_value)
  File "site-packages/google/cloud/bigquery/_helpers.py", line 345, in _float_to_json
    elif math.isnan(value) or math.isinf(value):
TypeError: must be real number, not str
@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-bigquery API. label Jul 27, 2021
@tswast
Copy link
Contributor

tswast commented Jul 27, 2021

Does insert_rows_json work for you? It doesn't do any type conversion.

@tswast tswast added the type: question Request for information or clarification. Not an issue. label Jul 27, 2021
@plamut plamut self-assigned this Jul 28, 2021
@plamut
Copy link
Contributor

plamut commented Jul 28, 2021

I was able to reproduce this and it's indeed the change in in #728 that caused this.

Previously, the string values were simply converted to floats, but then extra checks were added to handle special float values (Nan, inf...). Unfortunately, those checks do not like strings.

I'll submit a fix soon.

@plamut plamut added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. and removed type: question Request for information or clarification. Not an issue. labels Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants