Skip to content

Commit 8683a8b

Browse files
authored
Revert "feat: switch timestamp representation to int64 usec (#1332)"
This reverts commit fb10f03.
1 parent 024b1f6 commit 8683a8b

File tree

6 files changed

+28
-73
lines changed

6 files changed

+28
-73
lines changed

src/bigquery.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,13 +2217,15 @@ export class BigQueryTimestamp {
22172217
if (/^\d{4}-\d{1,2}-\d{1,2}/.test(value)) {
22182218
pd = new PreciseDate(value);
22192219
} else {
2220-
pd = new PreciseDate(BigInt(value) * BigInt(1000));
2220+
const floatValue = Number.parseFloat(value);
2221+
if (!Number.isNaN(floatValue)) {
2222+
pd = this.fromFloatValue_(floatValue);
2223+
} else {
2224+
pd = new PreciseDate(value);
2225+
}
22212226
}
2222-
} else if (value) {
2223-
pd = new PreciseDate(BigInt(value) * BigInt(1000));
22242227
} else {
2225-
// Nan or 0 - invalid dates
2226-
pd = new PreciseDate(value);
2228+
pd = this.fromFloatValue_(value);
22272229
}
22282230
// to keep backward compatibility, only converts with microsecond
22292231
// precision if needed.
@@ -2233,6 +2235,15 @@ export class BigQueryTimestamp {
22332235
this.value = new Date(pd.getTime()).toJSON();
22342236
}
22352237
}
2238+
2239+
fromFloatValue_(value: number): PreciseDate {
2240+
const secs = Math.trunc(value);
2241+
// Timestamps in BigQuery have microsecond precision, so we must
2242+
// return a round number of microseconds.
2243+
const micros = Math.trunc((value - secs) * 1e6 + 0.5);
2244+
const pd = new PreciseDate([secs, micros * 1000]);
2245+
return pd;
2246+
}
22362247
}
22372248

22382249
/**

src/job.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,10 @@ class Job extends Operation {
529529
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
530530
const callback =
531531
typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
532+
532533
const qs = extend(
533534
{
534535
location: this.location,
535-
'formatOptions.useInt64Timestamp': true,
536536
},
537537
options
538538
);

src/table.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,17 +1848,10 @@ class Table extends ServiceObject {
18481848
callback!(null, rows, nextQuery, resp);
18491849
};
18501850

1851-
const qs = extend(
1852-
{
1853-
'formatOptions.useInt64Timestamp': true,
1854-
},
1855-
options
1856-
);
1857-
18581851
this.request(
18591852
{
18601853
uri: '/data',
1861-
qs,
1854+
qs: options,
18621855
},
18631856
(err, resp) => {
18641857
if (err) {
@@ -1867,7 +1860,7 @@ class Table extends ServiceObject {
18671860
}
18681861
let nextQuery: GetRowsOptions | null = null;
18691862
if (resp.pageToken) {
1870-
nextQuery = Object.assign({}, qs, {
1863+
nextQuery = Object.assign({}, options, {
18711864
pageToken: resp.pageToken,
18721865
});
18731866
}

test/bigquery.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -850,10 +850,8 @@ describe('BigQuery', () => {
850850
describe('timestamp', () => {
851851
const INPUT_STRING = '2016-12-06T12:00:00.000Z';
852852
const INPUT_STRING_MICROS = '2016-12-06T12:00:00.123456Z';
853-
const INPUT_STRING_NEGATIVE = '1969-12-25T00:00:00.000Z';
854853
const INPUT_DATE = new Date(INPUT_STRING);
855854
const INPUT_PRECISE_DATE = new PreciseDate(INPUT_STRING_MICROS);
856-
const INPUT_PRECISE_NEGATIVE_DATE = new PreciseDate(INPUT_STRING_NEGATIVE);
857855
const EXPECTED_VALUE = INPUT_DATE.toJSON();
858856
const EXPECTED_VALUE_MICROS = INPUT_PRECISE_DATE.toISOString();
859857

@@ -883,26 +881,6 @@ describe('BigQuery', () => {
883881
assert.strictEqual(timestamp.value, EXPECTED_VALUE);
884882
});
885883

886-
it('should accept a number in microseconds', () => {
887-
let ms = INPUT_PRECISE_DATE.valueOf(); // milliseconds
888-
let us = ms * 1000 + INPUT_PRECISE_DATE.getMicroseconds(); // microseconds
889-
let timestamp = bq.timestamp(us);
890-
assert.strictEqual(timestamp.value, EXPECTED_VALUE_MICROS);
891-
892-
let usStr = `${us}`;
893-
timestamp = bq.timestamp(usStr);
894-
assert.strictEqual(timestamp.value, EXPECTED_VALUE_MICROS);
895-
896-
ms = INPUT_PRECISE_NEGATIVE_DATE.valueOf();
897-
us = ms * 1000;
898-
timestamp = bq.timestamp(us);
899-
assert.strictEqual(timestamp.value, INPUT_STRING_NEGATIVE);
900-
901-
usStr = `${us}`;
902-
timestamp = bq.timestamp(usStr);
903-
assert.strictEqual(timestamp.value, INPUT_STRING_NEGATIVE);
904-
});
905-
906884
it('should accept a string with microseconds', () => {
907885
const timestamp = bq.timestamp(INPUT_STRING_MICROS);
908886
assert.strictEqual(timestamp.value, EXPECTED_VALUE_MICROS);

test/job.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,7 @@ describe('BigQuery/Job', () => {
238238

239239
it('should optionally accept options', done => {
240240
const options = {a: 'b'};
241-
const expectedOptions = Object.assign(
242-
{location: undefined, 'formatOptions.useInt64Timestamp': true},
243-
options
244-
);
241+
const expectedOptions = Object.assign({location: undefined}, options);
245242

246243
BIGQUERY.request = (reqOpts: DecorateRequestOptions) => {
247244
assert.deepStrictEqual(reqOpts.qs, expectedOptions);
@@ -255,10 +252,7 @@ describe('BigQuery/Job', () => {
255252
const job = new Job(BIGQUERY, JOB_ID, {location: LOCATION});
256253

257254
BIGQUERY.request = (reqOpts: DecorateRequestOptions) => {
258-
assert.deepStrictEqual(reqOpts.qs, {
259-
location: LOCATION,
260-
'formatOptions.useInt64Timestamp': true,
261-
});
255+
assert.deepStrictEqual(reqOpts.qs, {location: LOCATION});
262256
done();
263257
};
264258

@@ -267,11 +261,7 @@ describe('BigQuery/Job', () => {
267261

268262
it('should delete any cached jobs', done => {
269263
const options = {job: {}, a: 'b'};
270-
const expectedOptions = {
271-
location: undefined,
272-
a: 'b',
273-
'formatOptions.useInt64Timestamp': true,
274-
};
264+
const expectedOptions = {location: undefined, a: 'b'};
275265

276266
BIGQUERY.request = (reqOpts: DecorateRequestOptions) => {
277267
assert.deepStrictEqual(reqOpts.qs, expectedOptions);
@@ -350,10 +340,7 @@ describe('BigQuery/Job', () => {
350340
const mergedRows: Array<{}> = [];
351341

352342
const options = {wrapIntegers: true};
353-
const expectedOptions = Object.assign({
354-
location: undefined,
355-
'formatOptions.useInt64Timestamp': true,
356-
});
343+
const expectedOptions = Object.assign({location: undefined});
357344

358345
BIGQUERY.request = (reqOpts: DecorateRequestOptions) => {
359346
assert.deepStrictEqual(reqOpts.qs, expectedOptions);
@@ -381,10 +368,7 @@ describe('BigQuery/Job', () => {
381368
const mergedRows: Array<{}> = [];
382369

383370
const options = {parseJSON: true};
384-
const expectedOptions = Object.assign({
385-
location: undefined,
386-
'formatOptions.useInt64Timestamp': true,
387-
});
371+
const expectedOptions = Object.assign({location: undefined});
388372

389373
BIGQUERY.request = (reqOpts: DecorateRequestOptions) => {
390374
assert.deepStrictEqual(reqOpts.qs, expectedOptions);

test/table.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,10 +1979,7 @@ describe('BigQuery/Table', () => {
19791979

19801980
table.request = (reqOpts: DecorateRequestOptions, callback: Function) => {
19811981
assert.strictEqual(reqOpts.uri, '/data');
1982-
assert.deepStrictEqual(reqOpts.qs, {
1983-
...options,
1984-
'formatOptions.useInt64Timestamp': true,
1985-
});
1982+
assert.strictEqual(reqOpts.qs, options);
19861983
callback(null, {});
19871984
};
19881985

@@ -2132,18 +2129,14 @@ describe('BigQuery/Table', () => {
21322129
table.metadata = {schema: {}};
21332130

21342131
table.request = (reqOpts: DecorateRequestOptions, callback: Function) => {
2135-
callback(null, {
2136-
'formatOptions.useInt64Timestamp': true,
2137-
pageToken,
2138-
});
2132+
callback(null, {pageToken});
21392133
};
21402134

21412135
table.getRows(options, (err: Error, rows: {}, nextQuery: {}) => {
21422136
assert.ifError(err);
21432137
assert.deepStrictEqual(nextQuery, {
21442138
a: 'b',
21452139
c: 'd',
2146-
'formatOptions.useInt64Timestamp': true,
21472140
pageToken,
21482141
});
21492142
// Original object isn't affected.
@@ -2264,9 +2257,7 @@ describe('BigQuery/Table', () => {
22642257
const merged = [{name: 'stephen'}];
22652258

22662259
table.request = (reqOpts: DecorateRequestOptions, callback: Function) => {
2267-
assert.deepStrictEqual(reqOpts.qs, {
2268-
'formatOptions.useInt64Timestamp': true,
2269-
});
2260+
assert.deepStrictEqual(reqOpts.qs, {});
22702261
callback(null, {});
22712262
};
22722263

@@ -2288,9 +2279,7 @@ describe('BigQuery/Table', () => {
22882279
const merged = [{name: 'stephen'}];
22892280

22902281
table.request = (reqOpts: DecorateRequestOptions, callback: Function) => {
2291-
assert.deepStrictEqual(reqOpts.qs, {
2292-
'formatOptions.useInt64Timestamp': true,
2293-
});
2282+
assert.deepStrictEqual(reqOpts.qs, {});
22942283
callback(null, {});
22952284
};
22962285

0 commit comments

Comments
 (0)