Skip to content

BigQuery - Standard SQL - DATETIME cast issue #2775

@VelizarVESSELINOV

Description

@VelizarVESSELINOV
  1. OS type and version

Latest Mac, not relevant

  1. Python version and virtual environment information python --version

Latest Python, not relevant

  1. google-cloud-python version pip show google-cloud, pip show google-<service> or pip freeze

Latest google-cloud package

  1. Stacktrace if available
/usr/local/lib/python2.7/dist-packages/google/cloud/bigquery/query.pyc in fetch_data(self, max_results, page_token, start_index, timeout_ms, client)
    402             total_rows = int(total_rows)
    403         page_token = response.get('pageToken')
--> 404         rows_data = _rows_from_json(response.get('rows', ()), self.schema)
    405 
    406         return rows_data, total_rows, page_token

/usr/local/lib/python2.7/dist-packages/google/cloud/bigquery/_helpers.pyc in _rows_from_json(rows, schema)
    114 def _rows_from_json(rows, schema):
    115     """Convert JSON row data to rows with appropriate types."""
--> 116     return [_row_from_json(row, schema) for row in rows]
    117 
    118 

/usr/local/lib/python2.7/dist-packages/google/cloud/bigquery/_helpers.pyc in _row_from_json(row, schema)
    102     row_data = []
    103     for field, cell in zip(schema, row['f']):
--> 104         converter = _CELLDATA_FROM_JSON[field.field_type]
    105         if field.mode == 'REPEATED':
    106             row_data.append([converter(item, field)

KeyError: u'DATETIME'
  1. Steps to reproduce
    Standard SQL query that it is working well on web UI:
SELECT CAST(STR AS DATETIME) AS DT FROM UNNEST (['2016-1-1']) AS STR;
  1. Code example
    qry = bqr.run_sync_query("SELECT CAST(STR AS DATETIME) AS DT FROM UNNEST (['2016-1-1']) AS STR")
    qry.use_legacy_sql = False
    qry.run()

Very easy to fix just add DATETIME in

_CELLDATA_FROM_JSON = {
    'INTEGER': _int_from_json,
    'INT64': _int_from_json,
    'FLOAT': _float_from_json,
    'FLOAT64': _float_from_json,
    'BOOLEAN': _bool_from_json,
    'TIMESTAMP': _datetime_from_json,
    'DATE': _date_from_json,
    'RECORD': _record_from_json,
    'STRING': _string_from_json,
}

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the BigQuery API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions