Issue
using load_table_from_json() to load data into a string column fails when using a value that can be interpolated as an int (eg. "123")
see code snippet, which shows inconsistency with the other data loading apis
Environment details
- OS type and version: macos 12.2.1
- Python version: 3.9.5 (also errors on 3.8)
- pip version: pip --version
- google-cloud-bigquery version: 2.31.0 & 3.0.1
Code example
import io
import json
from google.api_core.exceptions import BadRequest
from google.cloud.bigquery import Client, Table, SchemaField
data = [{"col_1": "123"}]
project = 'my-project'
bq = Client(project)
table = Table(f'{project}.aaa.data_load_test', schema=[SchemaField("col_1", "STRING"), ])
bq.delete_table(table, not_found_ok=True)
bq.create_table(table)
# WORKS: bq load from byte stream
with io.StringIO() as fp:
json.dump(data, fp)
fp.seek(0)
bq.load_table_from_file(fp, table).result()
# WORKS: streaming api
bq.insert_rows_json(table, data)
try:
# FAILS: bq load from python dict.
# 123 is interpolated as an int, even though i supply it as a string
bq.load_table_from_json(data, table).result()
except BadRequest as e:
print("ERROR")
print(e)
# WORKS: bq load from python dict, but 123d cannot be interpolated as an int
bq.load_table_from_json([{"col_1": "123d"}], table).result()
Stack trace
Traceback (most recent call last):
File "/Users/ben.marengo/code/scratch/bq_load.py", line 28, in <module>
bq.load_table_from_json(data, table).result()
File "/Users/ben.marengo/code/pyenvs/scratch/lib/python3.9/site-packages/google/cloud/bigquery/job/base.py", line 728, in result
return super(_AsyncJob, self).result(timeout=timeout, **kwargs)
File "/Users/ben.marengo/code/pyenvs/scratch/lib/python3.9/site-packages/google/api_core/future/polling.py", line 137, in result
raise self._exception
google.api_core.exceptions.BadRequest: 400 Provided Schema does not match Table my-project:aaa.data_load_test. Field col_1 has changed type from STRING to INTEGER
Reactions are currently unavailable
Issue
using load_table_from_json() to load data into a string column fails when using a value that can be interpolated as an int (eg. "123")
see code snippet, which shows inconsistency with the other data loading apis
Environment details
Code example
Stack trace
Traceback (most recent call last): File "/Users/ben.marengo/code/scratch/bq_load.py", line 28, in <module> bq.load_table_from_json(data, table).result() File "/Users/ben.marengo/code/pyenvs/scratch/lib/python3.9/site-packages/google/cloud/bigquery/job/base.py", line 728, in result return super(_AsyncJob, self).result(timeout=timeout, **kwargs) File "/Users/ben.marengo/code/pyenvs/scratch/lib/python3.9/site-packages/google/api_core/future/polling.py", line 137, in result raise self._exception google.api_core.exceptions.BadRequest: 400 Provided Schema does not match Table my-project:aaa.data_load_test. Field col_1 has changed type from STRING to INTEGER