|
Expand Up
|
@@ -21,6 +21,7 @@ |
|
|
from google.cloud._helpers import UTC |
|
|
from google.cloud._helpers import _date_from_iso8601_date |
|
|
from google.cloud._helpers import _datetime_from_microseconds |
|
|
from google.cloud._helpers import _microseconds_from_datetime |
|
|
from google.cloud._helpers import _RFC3339_NO_FRACTION |
|
|
from google.cloud._helpers import _time_from_iso8601_time_naive |
|
|
from google.cloud._helpers import _to_bytes |
|
Expand Down
Expand Up
|
@@ -122,6 +123,38 @@ def _record_from_json(value, field): |
|
|
} |
|
|
|
|
|
|
|
|
def _row_from_json(row, schema): |
|
|
"""Convert JSON row data to row with appropriate types. |
|
|
|
|
|
Note: ``row['f']`` and ``schema`` are presumed to be of the same length. |
|
|
|
|
|
:type row: dict |
|
|
:param row: A JSON response row to be converted. |
|
|
|
|
|
:type schema: tuple |
|
|
:param schema: A tuple of |
|
|
:class:`~google.cloud.bigquery.schema.SchemaField`. |
|
|
|
|
|
:rtype: tuple |
|
|
:returns: A tuple of data converted to native types. |
|
|
""" |
|
|
row_data = [] |
|
|
for field, cell in zip(schema, row['f']): |
Comment thread
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
converter = _CELLDATA_FROM_JSON[field.field_type] |
|
|
if field.mode == 'REPEATED': |
|
|
row_data.append([converter(item['v'], field) |
|
|
for item in cell['v']]) |
|
|
else: |
|
|
row_data.append(converter(cell['v'], field)) |
|
|
|
|
|
return tuple(row_data) |
|
|
|
|
|
|
|
|
def _rows_from_json(rows, schema): |
|
|
"""Convert JSON row data to rows with appropriate types.""" |
|
|
return [_row_from_json(row, schema) for row in rows] |
|
|
|
|
|
|
|
|
def _int_to_json(value): |
|
|
"""Coerce 'value' to an JSON-compatible representation.""" |
|
|
if isinstance(value, int): |
|
Expand All
|
@@ -148,8 +181,11 @@ def _bytes_to_json(value): |
|
|
return value |
|
|
|
|
|
|
|
|
def _timestamp_to_json(value): |
|
|
"""Coerce 'value' to an JSON-compatible representation.""" |
|
|
def _timestamp_to_json_parameter(value): |
|
|
"""Coerce 'value' to an JSON-compatible representation. |
|
|
|
|
|
This version returns the string representation used in query parameters. |
|
|
""" |
|
|
if isinstance(value, datetime.datetime): |
|
|
if value.tzinfo not in (None, UTC): |
|
|
# Convert to UTC and remove the time zone info. |
|
Expand All
|
@@ -159,6 +195,16 @@ def _timestamp_to_json(value): |
|
|
return value |
|
|
|
|
|
|
|
|
def _timestamp_to_json_row(value): |
|
|
"""Coerce 'value' to an JSON-compatible representation. |
|
|
|
|
|
This version returns floating-point seconds value used in row data. |
|
|
""" |
|
|
if isinstance(value, datetime.datetime): |
|
|
value = _microseconds_from_datetime(value) * 1e-6 |
|
|
return value |
|
|
|
|
|
|
|
|
def _datetime_to_json(value): |
|
|
"""Coerce 'value' to an JSON-compatible representation.""" |
|
|
if isinstance(value, datetime.datetime): |
|
Expand All
|
@@ -180,49 +226,25 @@ def _time_to_json(value): |
|
|
return value |
|
|
|
|
|
|
|
|
_SCALAR_VALUE_TO_JSON = { |
|
|
# Converters used for scalar values marshalled as row data. |
|
|
_SCALAR_VALUE_TO_JSON_ROW = { |
|
|
'INTEGER': _int_to_json, |
|
|
'INT64': _int_to_json, |
|
|
'FLOAT': _float_to_json, |
|
|
'FLOAT64': _float_to_json, |
|
|
'BOOLEAN': _bool_to_json, |
|
|
'BOOL': _bool_to_json, |
|
|
'BYTES': _bytes_to_json, |
|
|
'TIMESTAMP': _timestamp_to_json, |
|
|
'TIMESTAMP': _timestamp_to_json_row, |
|
|
'DATETIME': _datetime_to_json, |
|
|
'DATE': _date_to_json, |
|
|
'TIME': _time_to_json, |
|
|
} |
|
|
|
|
|
|
|
|
def _row_from_json(row, schema): |
|
|
"""Convert JSON row data to row with appropriate types. |
|
|
|
|
|
:type row: dict |
|
|
:param row: A JSON response row to be converted. |
|
|
|
|
|
:type schema: tuple |
|
|
:param schema: A tuple of |
|
|
:class:`~google.cloud.bigquery.schema.SchemaField`. |
|
|
|
|
|
:rtype: tuple |
|
|
:returns: A tuple of data converted to native types. |
|
|
""" |
|
|
row_data = [] |
|
|
for field, cell in zip(schema, row['f']): |
|
|
converter = _CELLDATA_FROM_JSON[field.field_type] |
|
|
if field.mode == 'REPEATED': |
|
|
row_data.append([converter(item['v'], field) |
|
|
for item in cell['v']]) |
|
|
else: |
|
|
row_data.append(converter(cell['v'], field)) |
|
|
|
|
|
return tuple(row_data) |
|
|
|
|
|
|
|
|
def _rows_from_json(rows, schema): |
|
|
"""Convert JSON row data to rows with appropriate types.""" |
|
|
return [_row_from_json(row, schema) for row in rows] |
|
|
# Converters used for scalar values marshalled as query parameters. |
|
|
_SCALAR_VALUE_TO_JSON_PARAM = _SCALAR_VALUE_TO_JSON_ROW.copy() |
|
|
_SCALAR_VALUE_TO_JSON_PARAM['TIMESTAMP'] = _timestamp_to_json_parameter |
|
|
|
|
|
|
|
|
class _ConfigurationProperty(object): |
|
Expand Down
Expand Up
|
@@ -420,7 +442,7 @@ def to_api_repr(self): |
|
|
:returns: JSON mapping |
|
|
""" |
|
|
value = self.value |
|
|
converter = _SCALAR_VALUE_TO_JSON.get(self.type_) |
|
|
converter = _SCALAR_VALUE_TO_JSON_PARAM.get(self.type_) |
|
|
if converter is not None: |
|
|
value = converter(value) |
|
|
resource = { |
|
Expand Down
Expand Up
|
@@ -506,7 +528,7 @@ def to_api_repr(self): |
|
|
a_values = [repr_['parameterValue'] for repr_ in reprs] |
|
|
else: |
|
|
a_type = {'type': self.array_type} |
|
|
converter = _SCALAR_VALUE_TO_JSON.get(self.array_type) |
|
|
converter = _SCALAR_VALUE_TO_JSON_PARAM.get(self.array_type) |
|
|
if converter is not None: |
|
|
values = [converter(value) for value in values] |
|
|
a_values = [{'value': value} for value in values] |
|
Expand Down
Expand Up
|
@@ -600,7 +622,7 @@ def to_api_repr(self): |
|
|
values[name] = repr_['parameterValue'] |
|
|
else: |
|
|
s_types[name] = {'name': name, 'type': {'type': type_}} |
|
|
converter = _SCALAR_VALUE_TO_JSON.get(type_) |
|
|
converter = _SCALAR_VALUE_TO_JSON_PARAM.get(type_) |
|
|
if converter is not None: |
|
|
value = converter(value) |
|
|
values[name] = {'value': value} |
|
Expand Down
|
|