Skip to content

Commit 625fa47

Browse files
committed
convert json parse to classes
1 parent 739a382 commit 625fa47

File tree

8 files changed

+956
-874
lines changed

8 files changed

+956
-874
lines changed

google/cloud/bigquery/_helpers.py

Lines changed: 247 additions & 218 deletions
Large diffs are not rendered by default.

google/cloud/bigquery/_pandas_helpers.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,9 +759,6 @@ def _row_iterator_page_to_arrow(page, column_names, arrow_types):
759759

760760
arrays = []
761761
for column_index, arrow_type in enumerate(arrow_types):
762-
# RowIterator parses JSON, but for arrow, we actually want to keep them
763-
# as strings.
764-
# TODO: Support STRUCT<JSON> and ARRAY<JSON>.
765762
arrays.append(pyarrow.array(page._columns[column_index], type=arrow_type))
766763

767764
if isinstance(column_names, pyarrow.Schema):

google/cloud/bigquery/query.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from typing import Any, Optional, Dict, Union
2222

2323
from google.cloud.bigquery.table import _parse_schema_resource
24+
from google.cloud.bigquery import _helpers
2425
from google.cloud.bigquery._helpers import _rows_from_json
25-
from google.cloud.bigquery._helpers import _QUERY_PARAMS_FROM_JSON
2626
from google.cloud.bigquery._helpers import _SCALAR_VALUE_TO_JSON_PARAM
2727
from google.cloud.bigquery._helpers import _SUPPORTED_RANGE_ELEMENTS
2828

@@ -571,14 +571,19 @@ def from_api_repr(cls, resource: dict) -> "ScalarQueryParameter":
571571
Returns:
572572
google.cloud.bigquery.query.ScalarQueryParameter: Instance
573573
"""
574+
# Import here to avoid circular imports.
575+
from google.cloud.bigquery import schema
576+
574577
name = resource.get("name")
575578
type_ = resource["parameterType"]["type"]
576579

577580
# parameterValue might not be present if JSON resource originates
578581
# from the back-end - the latter omits it for None values.
579582
value = resource.get("parameterValue", {}).get("value")
580583
if value is not None:
581-
converted = _QUERY_PARAMS_FROM_JSON[type_](value, None)
584+
converted = _helpers.SCALAR_QUERY_PARAM_PARSER.to_py(
585+
value, schema.SchemaField(name, type_)
586+
)
582587
else:
583588
converted = None
584589

@@ -693,13 +698,20 @@ def _from_api_repr_struct(cls, resource):
693698

694699
@classmethod
695700
def _from_api_repr_scalar(cls, resource):
701+
"""Converts REST resource into a list of scalar values."""
702+
# Import here to avoid circular imports.
703+
from google.cloud.bigquery import schema
704+
696705
name = resource.get("name")
697706
array_type = resource["parameterType"]["arrayType"]["type"]
698707
parameter_value = resource.get("parameterValue", {})
699708
array_values = parameter_value.get("arrayValues", ())
700709
values = [value["value"] for value in array_values]
701710
converted = [
702-
_QUERY_PARAMS_FROM_JSON[array_type](value, None) for value in values
711+
_helpers.SCALAR_QUERY_PARAM_PARSER.to_py(
712+
value, schema.SchemaField(name, array_type)
713+
)
714+
for value in values
703715
]
704716
return cls(name, array_type, converted)
705717

@@ -850,6 +862,9 @@ def from_api_repr(cls, resource: dict) -> "StructQueryParameter":
850862
Returns:
851863
google.cloud.bigquery.query.StructQueryParameter: Instance
852864
"""
865+
# Import here to avoid circular imports.
866+
from google.cloud.bigquery import schema
867+
853868
name = resource.get("name")
854869
instance = cls(name)
855870
type_resources = {}
@@ -877,7 +892,9 @@ def from_api_repr(cls, resource: dict) -> "StructQueryParameter":
877892
converted = ArrayQueryParameter.from_api_repr(struct_resource)
878893
else:
879894
value = value["value"]
880-
converted = _QUERY_PARAMS_FROM_JSON[type_](value, None)
895+
converted = _helpers.SCALAR_QUERY_PARAM_PARSER.to_py(
896+
value, schema.SchemaField(name, type_)
897+
)
881898
instance.struct_values[key] = converted
882899
return instance
883900

google/cloud/bigquery/table.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,22 +3531,14 @@ def _row_iterator_page_columns(schema, response):
35313531
columns = []
35323532
rows = response.get("rows", [])
35333533

3534-
def get_column_data(field_index):
3534+
def get_column_data(field_index, field):
35353535
for row in rows:
3536-
yield row["f"][field_index]["v"]
3537-
3538-
def parse_column_data(column, field):
3539-
# pyarrow.json_() type needs to keep the data as a string, not parsed.
3540-
# TODO: support STRUCT<JSON> and ARRAY<JSON>
3541-
if field.field_type.casefold() == "json":
3542-
for value in column:
3543-
yield value
3544-
3545-
for value in column:
3546-
yield _helpers._field_from_json(value, field)
3536+
yield _helpers.DATA_FRAME_CELL_DATA_PARSER.to_py(
3537+
row["f"][field_index]["v"], field
3538+
)
35473539

35483540
for field_index, field in enumerate(schema):
3549-
columns.append(parse_column_data(get_column_data(field_index), field))
3541+
columns.append(get_column_data(field_index, field))
35503542

35513543
return columns
35523544

0 commit comments

Comments
 (0)