Skip to content

Commit b427bed

Browse files
committed
Merge pull request #1128 from tseaver/1125-fix_bq_timestamp_fetch_conversion
Fix conversion of backend-generated timestamps
2 parents 219fe75 + 8225dd8 commit b427bed

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

gcloud/bigquery/table.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def create(self, client=None):
411411
"""API call: create the dataset via a PUT request
412412
413413
See:
414-
https://cloud.google.com/bigquery/reference/rest/v2/tables/insert
414+
https://cloud.google.com/bigquery/docs/reference/v2/tables/insert
415415
416416
:type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
417417
:param client: the client to use. If not passed, falls back to the
@@ -552,7 +552,7 @@ def delete(self, client=None):
552552
"""API call: delete the table via a DELETE request
553553
554554
See:
555-
https://cloud.google.com/bigquery/reference/rest/v2/tables/delete
555+
https://cloud.google.com/bigquery/docs/reference/v2/tables/delete
556556
557557
:type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
558558
:param client: the client to use. If not passed, falls back to the
@@ -565,7 +565,7 @@ def fetch_data(self, max_results=None, page_token=None, client=None):
565565
"""API call: fetch the table data via a GET request
566566
567567
See:
568-
https://cloud.google.com/bigquery/reference/rest/v2/tabledata/list
568+
https://cloud.google.com/bigquery/docs/reference/v2/tabledata/list
569569
570570
.. note::
571571
@@ -631,7 +631,7 @@ def insert_data(self,
631631
"""API call: insert table data via a POST request
632632
633633
See:
634-
https://cloud.google.com/bigquery/reference/rest/v2/tabledata/insertAll
634+
https://cloud.google.com/bigquery/docs/reference/v2/tabledata/insertAll
635635
636636
:type rows: list of tuples
637637
:param rows: row data to be inserted
@@ -761,8 +761,8 @@ def _bool_from_json(value, field):
761761

762762
def _datetime_from_json(value, field):
763763
if _not_null(value, field):
764-
# Field value will be in milliseconds.
765-
return _datetime_from_microseconds(1000.0 * float(value))
764+
# value will be a float in seconds, to microsecond precision, in UTC.
765+
return _datetime_from_microseconds(1e6 * float(value))
766766

767767

768768
def _record_from_json(value, field):

gcloud/bigquery/test_table.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,6 @@ def test_fetch_data_w_bound_client(self):
824824
import datetime
825825
from gcloud._helpers import UTC
826826
from gcloud.bigquery.table import SchemaField
827-
from gcloud._helpers import _millis_from_datetime
828827

829828
PATH = 'projects/%s/datasets/%s/tables/%s/data' % (
830829
self.PROJECT, self.DS_NAME, self.TABLE_NAME)
@@ -835,24 +834,29 @@ def test_fetch_data_w_bound_client(self):
835834
WHEN_2 = WHEN + datetime.timedelta(seconds=2)
836835
ROWS = 1234
837836
TOKEN = 'TOKEN'
837+
838+
def _bigquery_timestamp_float_repr(ts_float):
839+
# Preserve microsecond precision for E+09 timestamps
840+
return '%0.15E' % (ts_float,)
841+
838842
DATA = {
839843
'totalRows': ROWS,
840844
'pageToken': TOKEN,
841845
'rows': [
842846
{'f': [
843847
{'v': 'Phred Phlyntstone'},
844848
{'v': '32'},
845-
{'v': _millis_from_datetime(WHEN)},
849+
{'v': _bigquery_timestamp_float_repr(WHEN_TS)},
846850
]},
847851
{'f': [
848852
{'v': 'Bharney Rhubble'},
849853
{'v': '33'},
850-
{'v': _millis_from_datetime(WHEN_1)},
854+
{'v': _bigquery_timestamp_float_repr(WHEN_TS + 1)},
851855
]},
852856
{'f': [
853857
{'v': 'Wylma Phlyntstone'},
854858
{'v': '29'},
855-
{'v': _millis_from_datetime(WHEN_2)},
859+
{'v': _bigquery_timestamp_float_repr(WHEN_TS + 2)},
856860
]},
857861
{'f': [
858862
{'v': 'Bhettye Rhubble'},
@@ -861,6 +865,7 @@ def test_fetch_data_w_bound_client(self):
861865
]},
862866
]
863867
}
868+
864869
conn = _Connection(DATA)
865870
client = _Client(project=self.PROJECT, connection=conn)
866871
dataset = _Dataset(client)

0 commit comments

Comments
 (0)